blob: 4b68eeb8f18ac9bce36398adc04e73e5d78288a1 [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataevb08f89f2015-08-14 12:25:37 +000015#include "TreeTransform.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000016#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000017#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva839ddd2016-03-17 10:19:46 +000018#include "clang/AST/CXXInheritance.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000019#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000020#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000021#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000022#include "clang/AST/StmtCXX.h"
23#include "clang/AST/StmtOpenMP.h"
24#include "clang/AST/StmtVisitor.h"
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000025#include "clang/AST/TypeOrdering.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000026#include "clang/Basic/OpenMPKinds.h"
Samuel Antaof8b50122015-07-13 22:54:53 +000027#include "clang/Basic/TargetInfo.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000028#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000029#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000030#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000031#include "clang/Sema/Scope.h"
32#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000033#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000034using namespace clang;
35
Alexey Bataev758e55e2013-09-06 18:03:48 +000036//===----------------------------------------------------------------------===//
37// Stack of data-sharing attributes for variables
38//===----------------------------------------------------------------------===//
39
40namespace {
41/// \brief Default data sharing attributes, which can be applied to directive.
42enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000043 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
44 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
45 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000046};
Alexey Bataev7ff55242014-06-19 09:13:45 +000047
Alexey Bataev758e55e2013-09-06 18:03:48 +000048/// \brief Stack for tracking declarations used in OpenMP directives and
49/// clauses and their data-sharing attributes.
Alexey Bataev7ace49d2016-05-17 08:55:33 +000050class DSAStackTy final {
Alexey Bataev758e55e2013-09-06 18:03:48 +000051public:
Alexey Bataev7ace49d2016-05-17 08:55:33 +000052 struct DSAVarData final {
53 OpenMPDirectiveKind DKind = OMPD_unknown;
54 OpenMPClauseKind CKind = OMPC_unknown;
55 Expr *RefExpr = nullptr;
56 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000057 SourceLocation ImplicitDSALoc;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000058 DSAVarData() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000059 };
Alexey Bataev8b427062016-05-25 12:36:08 +000060 typedef llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>
61 OperatorOffsetTy;
Alexey Bataeved09d242014-05-28 05:53:51 +000062
Alexey Bataev758e55e2013-09-06 18:03:48 +000063private:
Alexey Bataev7ace49d2016-05-17 08:55:33 +000064 struct DSAInfo final {
65 OpenMPClauseKind Attributes = OMPC_unknown;
66 /// Pointer to a reference expression and a flag which shows that the
67 /// variable is marked as lastprivate(true) or not (false).
68 llvm::PointerIntPair<Expr *, 1, bool> RefExpr;
69 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000070 };
Alexey Bataev90c228f2016-02-08 09:29:13 +000071 typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy;
72 typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +000073 typedef std::pair<unsigned, VarDecl *> LCDeclInfo;
74 typedef llvm::DenseMap<ValueDecl *, LCDeclInfo> LoopControlVariablesMapTy;
Samuel Antao90927002016-04-26 14:54:23 +000075 typedef llvm::DenseMap<
76 ValueDecl *, OMPClauseMappableExprCommon::MappableExprComponentLists>
77 MappedExprComponentsTy;
Alexey Bataev28c75412015-12-15 08:19:24 +000078 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
79 CriticalsWithHintsTy;
Alexey Bataev8b427062016-05-25 12:36:08 +000080 typedef llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>
81 DoacrossDependMapTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000082
Alexey Bataev7ace49d2016-05-17 08:55:33 +000083 struct SharingMapTy final {
Alexey Bataev758e55e2013-09-06 18:03:48 +000084 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000085 AlignedMapTy AlignedMap;
Samuel Antao90927002016-04-26 14:54:23 +000086 MappedExprComponentsTy MappedExprComponents;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000087 LoopControlVariablesMapTy LCVMap;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000088 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
Alexey Bataevbae9a792014-06-27 10:37:06 +000089 SourceLocation DefaultAttrLoc;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000090 OpenMPDirectiveKind Directive = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +000091 DeclarationNameInfo DirectiveName;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000092 Scope *CurScope = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000093 SourceLocation ConstructLoc;
Alexey Bataev8b427062016-05-25 12:36:08 +000094 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
95 /// get the data (loop counters etc.) about enclosing loop-based construct.
96 /// This data is required during codegen.
97 DoacrossDependMapTy DoacrossDepends;
Alexey Bataev346265e2015-09-25 10:37:12 +000098 /// \brief first argument (Expr *) contains optional argument of the
99 /// 'ordered' clause, the second one is true if the regions has 'ordered'
100 /// clause, false otherwise.
101 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000102 bool NowaitRegion = false;
103 bool CancelRegion = false;
104 unsigned AssociatedLoops = 1;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000105 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000106 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000107 Scope *CurScope, SourceLocation Loc)
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000108 : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
109 ConstructLoc(Loc) {}
110 SharingMapTy() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000111 };
112
Axel Naumann323862e2016-02-03 10:45:22 +0000113 typedef SmallVector<SharingMapTy, 4> StackTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000114
115 /// \brief Stack of used declaration and their data-sharing attributes.
116 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000117 /// \brief true, if check for DSA must be from parent directive, false, if
118 /// from current directive.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000119 OpenMPClauseKind ClauseKindMode = OMPC_unknown;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000120 Sema &SemaRef;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000121 bool ForceCapturing = false;
Alexey Bataev28c75412015-12-15 08:19:24 +0000122 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000123
124 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
125
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000126 DSAVarData getDSA(StackTy::reverse_iterator& Iter, ValueDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000127
128 /// \brief Checks if the variable is a local for OpenMP region.
129 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000130
Alexey Bataev758e55e2013-09-06 18:03:48 +0000131public:
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000132 explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000133
Alexey Bataevaac108a2015-06-23 04:51:00 +0000134 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
135 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000136
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000137 bool isForceVarCapturing() const { return ForceCapturing; }
138 void setForceVarCapturing(bool V) { ForceCapturing = V; }
139
Alexey Bataev758e55e2013-09-06 18:03:48 +0000140 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000141 Scope *CurScope, SourceLocation Loc) {
142 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
143 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000144 }
145
146 void pop() {
147 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
148 Stack.pop_back();
149 }
150
Alexey Bataev28c75412015-12-15 08:19:24 +0000151 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
152 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
153 }
154 const std::pair<OMPCriticalDirective *, llvm::APSInt>
155 getCriticalWithHint(const DeclarationNameInfo &Name) const {
156 auto I = Criticals.find(Name.getAsString());
157 if (I != Criticals.end())
158 return I->second;
159 return std::make_pair(nullptr, llvm::APSInt());
160 }
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000161 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000162 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000163 /// for diagnostics.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000164 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000165
Alexey Bataev9c821032015-04-30 04:23:23 +0000166 /// \brief Register specified variable as loop control variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000167 void addLoopControlVariable(ValueDecl *D, VarDecl *Capture);
Alexey Bataev9c821032015-04-30 04:23:23 +0000168 /// \brief Check if the specified variable is a loop control variable for
169 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000170 /// \return The index of the loop control variable in the list of associated
171 /// for-loops (from outer to inner).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000172 LCDeclInfo isLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000173 /// \brief Check if the specified variable is a loop control variable for
174 /// parent region.
175 /// \return The index of the loop control variable in the list of associated
176 /// for-loops (from outer to inner).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000177 LCDeclInfo isParentLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000178 /// \brief Get the loop control variable for the I-th loop (or nullptr) in
179 /// parent directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000180 ValueDecl *getParentLoopControlVariable(unsigned I);
Alexey Bataev9c821032015-04-30 04:23:23 +0000181
Alexey Bataev758e55e2013-09-06 18:03:48 +0000182 /// \brief Adds explicit data sharing attribute to the specified declaration.
Alexey Bataev90c228f2016-02-08 09:29:13 +0000183 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
184 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000185
Alexey Bataev758e55e2013-09-06 18:03:48 +0000186 /// \brief Returns data sharing attributes from top of the stack for the
187 /// specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000188 DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000189 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000190 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000191 /// \brief Checks if the specified variables has data-sharing attributes which
192 /// match specified \a CPred predicate in any directive which matches \a DPred
193 /// predicate.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000194 DSAVarData hasDSA(ValueDecl *D,
195 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
196 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
197 bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000198 /// \brief Checks if the specified variables has data-sharing attributes which
199 /// match specified \a CPred predicate in any innermost directive which
200 /// matches \a DPred predicate.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000201 DSAVarData
202 hasInnermostDSA(ValueDecl *D,
203 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
204 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
205 bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000206 /// \brief Checks if the specified variables has explicit data-sharing
207 /// attributes which match specified \a CPred predicate at the specified
208 /// OpenMP region.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000209 bool hasExplicitDSA(ValueDecl *D,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000210 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000211 unsigned Level, bool NotLastprivate = false);
Samuel Antao4be30e92015-10-02 17:14:03 +0000212
213 /// \brief Returns true if the directive at level \Level matches in the
214 /// specified \a DPred predicate.
215 bool hasExplicitDirective(
216 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
217 unsigned Level);
218
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000219 /// \brief Finds a directive which matches specified \a DPred predicate.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000220 bool hasDirective(const llvm::function_ref<bool(OpenMPDirectiveKind,
221 const DeclarationNameInfo &,
222 SourceLocation)> &DPred,
223 bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000224
Alexey Bataev758e55e2013-09-06 18:03:48 +0000225 /// \brief Returns currently analyzed directive.
226 OpenMPDirectiveKind getCurrentDirective() const {
227 return Stack.back().Directive;
228 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000229 /// \brief Returns parent directive.
230 OpenMPDirectiveKind getParentDirective() const {
231 if (Stack.size() > 2)
232 return Stack[Stack.size() - 2].Directive;
233 return OMPD_unknown;
234 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000235
236 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000237 void setDefaultDSANone(SourceLocation Loc) {
238 Stack.back().DefaultAttr = DSA_none;
239 Stack.back().DefaultAttrLoc = Loc;
240 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000241 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000242 void setDefaultDSAShared(SourceLocation Loc) {
243 Stack.back().DefaultAttr = DSA_shared;
244 Stack.back().DefaultAttrLoc = Loc;
245 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000246
247 DefaultDataSharingAttributes getDefaultDSA() const {
248 return Stack.back().DefaultAttr;
249 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000250 SourceLocation getDefaultDSALocation() const {
251 return Stack.back().DefaultAttrLoc;
252 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000253
Alexey Bataevf29276e2014-06-18 04:14:57 +0000254 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000255 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000256 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000257 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000258 }
259
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000260 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000261 void setOrderedRegion(bool IsOrdered, Expr *Param) {
262 Stack.back().OrderedRegion.setInt(IsOrdered);
263 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000264 }
265 /// \brief Returns true, if parent region is ordered (has associated
266 /// 'ordered' clause), false - otherwise.
267 bool isParentOrderedRegion() const {
268 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000269 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000270 return false;
271 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000272 /// \brief Returns optional parameter for the ordered region.
273 Expr *getParentOrderedRegionParam() const {
274 if (Stack.size() > 2)
275 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
276 return nullptr;
277 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000278 /// \brief Marks current region as nowait (it has a 'nowait' clause).
279 void setNowaitRegion(bool IsNowait = true) {
280 Stack.back().NowaitRegion = IsNowait;
281 }
282 /// \brief Returns true, if parent region is nowait (has associated
283 /// 'nowait' clause), false - otherwise.
284 bool isParentNowaitRegion() const {
285 if (Stack.size() > 2)
286 return Stack[Stack.size() - 2].NowaitRegion;
287 return false;
288 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000289 /// \brief Marks parent region as cancel region.
290 void setParentCancelRegion(bool Cancel = true) {
291 if (Stack.size() > 2)
292 Stack[Stack.size() - 2].CancelRegion =
293 Stack[Stack.size() - 2].CancelRegion || Cancel;
294 }
295 /// \brief Return true if current region has inner cancel construct.
296 bool isCancelRegion() const {
297 return Stack.back().CancelRegion;
298 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000299
Alexey Bataev9c821032015-04-30 04:23:23 +0000300 /// \brief Set collapse value for the region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000301 void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000302 /// \brief Return collapse value for region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000303 unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000304
Alexey Bataev13314bf2014-10-09 04:18:56 +0000305 /// \brief Marks current target region as one with closely nested teams
306 /// region.
307 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
308 if (Stack.size() > 2)
309 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
310 }
311 /// \brief Returns true, if current region has closely nested teams region.
312 bool hasInnerTeamsRegion() const {
313 return getInnerTeamsRegionLoc().isValid();
314 }
315 /// \brief Returns location of the nested teams region (if any).
316 SourceLocation getInnerTeamsRegionLoc() const {
317 if (Stack.size() > 1)
318 return Stack.back().InnerTeamsRegionLoc;
319 return SourceLocation();
320 }
321
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000322 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000323 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000324 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000325
Samuel Antao90927002016-04-26 14:54:23 +0000326 // Do the check specified in \a Check to all component lists and return true
327 // if any issue is found.
328 bool checkMappableExprComponentListsForDecl(
329 ValueDecl *VD, bool CurrentRegionOnly,
330 const llvm::function_ref<bool(
331 OMPClauseMappableExprCommon::MappableExprComponentListRef)> &Check) {
Samuel Antao5de996e2016-01-22 20:21:36 +0000332 auto SI = Stack.rbegin();
333 auto SE = Stack.rend();
334
335 if (SI == SE)
336 return false;
337
338 if (CurrentRegionOnly) {
339 SE = std::next(SI);
340 } else {
341 ++SI;
342 }
343
344 for (; SI != SE; ++SI) {
Samuel Antao90927002016-04-26 14:54:23 +0000345 auto MI = SI->MappedExprComponents.find(VD);
346 if (MI != SI->MappedExprComponents.end())
347 for (auto &L : MI->second)
348 if (Check(L))
Samuel Antao5de996e2016-01-22 20:21:36 +0000349 return true;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000350 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000351 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000352 }
353
Samuel Antao90927002016-04-26 14:54:23 +0000354 // Create a new mappable expression component list associated with a given
355 // declaration and initialize it with the provided list of components.
356 void addMappableExpressionComponents(
357 ValueDecl *VD,
358 OMPClauseMappableExprCommon::MappableExprComponentListRef Components) {
359 assert(Stack.size() > 1 &&
360 "Not expecting to retrieve components from a empty stack!");
361 auto &MEC = Stack.back().MappedExprComponents[VD];
362 // Create new entry and append the new components there.
363 MEC.resize(MEC.size() + 1);
364 MEC.back().append(Components.begin(), Components.end());
Kelvin Li0bff7af2015-11-23 05:32:03 +0000365 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000366
367 unsigned getNestingLevel() const {
368 assert(Stack.size() > 1);
369 return Stack.size() - 2;
370 }
Alexey Bataev8b427062016-05-25 12:36:08 +0000371 void addDoacrossDependClause(OMPDependClause *C, OperatorOffsetTy &OpsOffs) {
372 assert(Stack.size() > 2);
373 assert(isOpenMPWorksharingDirective(Stack[Stack.size() - 2].Directive));
374 Stack[Stack.size() - 2].DoacrossDepends.insert({C, OpsOffs});
375 }
376 llvm::iterator_range<DoacrossDependMapTy::const_iterator>
377 getDoacrossDependClauses() const {
378 assert(Stack.size() > 1);
379 if (isOpenMPWorksharingDirective(Stack[Stack.size() - 1].Directive)) {
380 auto &Ref = Stack[Stack.size() - 1].DoacrossDepends;
381 return llvm::make_range(Ref.begin(), Ref.end());
382 }
383 return llvm::make_range(Stack[0].DoacrossDepends.end(),
384 Stack[0].DoacrossDepends.end());
385 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000386};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000387bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
Alexey Bataev35aaee62016-04-13 13:36:48 +0000388 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
389 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000390}
Alexey Bataeved09d242014-05-28 05:53:51 +0000391} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000392
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000393static ValueDecl *getCanonicalDecl(ValueDecl *D) {
394 auto *VD = dyn_cast<VarDecl>(D);
395 auto *FD = dyn_cast<FieldDecl>(D);
396 if (VD != nullptr) {
397 VD = VD->getCanonicalDecl();
398 D = VD;
399 } else {
400 assert(FD);
401 FD = FD->getCanonicalDecl();
402 D = FD;
403 }
404 return D;
405}
406
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000407DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator& Iter,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000408 ValueDecl *D) {
409 D = getCanonicalDecl(D);
410 auto *VD = dyn_cast<VarDecl>(D);
411 auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000412 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000413 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000414 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
415 // in a region but not in construct]
416 // File-scope or namespace-scope variables referenced in called routines
417 // in the region are shared unless they appear in a threadprivate
418 // directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000419 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000420 DVar.CKind = OMPC_shared;
421
422 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
423 // in a region but not in construct]
424 // Variables with static storage duration that are declared in called
425 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000426 if (VD && VD->hasGlobalStorage())
427 DVar.CKind = OMPC_shared;
428
429 // Non-static data members are shared by default.
430 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000431 DVar.CKind = OMPC_shared;
432
Alexey Bataev758e55e2013-09-06 18:03:48 +0000433 return DVar;
434 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000435
Alexey Bataev758e55e2013-09-06 18:03:48 +0000436 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000437 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
438 // in a Construct, C/C++, predetermined, p.1]
439 // Variables with automatic storage duration that are declared in a scope
440 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000441 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
442 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000443 DVar.CKind = OMPC_private;
444 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000445 }
446
Alexey Bataev758e55e2013-09-06 18:03:48 +0000447 // Explicitly specified attributes and local variables with predetermined
448 // attributes.
449 if (Iter->SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000450 DVar.RefExpr = Iter->SharingMap[D].RefExpr.getPointer();
Alexey Bataev90c228f2016-02-08 09:29:13 +0000451 DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000452 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000453 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000454 return DVar;
455 }
456
457 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
458 // in a Construct, C/C++, implicitly determined, p.1]
459 // In a parallel or task construct, the data-sharing attributes of these
460 // variables are determined by the default clause, if present.
461 switch (Iter->DefaultAttr) {
462 case DSA_shared:
463 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000464 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000465 return DVar;
466 case DSA_none:
467 return DVar;
468 case DSA_unspecified:
469 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
470 // in a Construct, implicitly determined, p.2]
471 // In a parallel construct, if no default clause is present, these
472 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000473 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000474 if (isOpenMPParallelDirective(DVar.DKind) ||
475 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000476 DVar.CKind = OMPC_shared;
477 return DVar;
478 }
479
480 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
481 // in a Construct, implicitly determined, p.4]
482 // In a task construct, if no default clause is present, a variable that in
483 // the enclosing context is determined to be shared by all implicit tasks
484 // bound to the current team is shared.
Alexey Bataev35aaee62016-04-13 13:36:48 +0000485 if (isOpenMPTaskingDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000486 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000487 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000488 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000489 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
Alexey Bataev35aaee62016-04-13 13:36:48 +0000490 // Referenced in a Construct, implicitly determined, p.6]
Alexey Bataev758e55e2013-09-06 18:03:48 +0000491 // In a task construct, if no default clause is present, a variable
492 // whose data-sharing attribute is not determined by the rules above is
493 // firstprivate.
494 DVarTemp = getDSA(I, D);
495 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000496 DVar.RefExpr = nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000497 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000498 return DVar;
499 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000500 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000501 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000502 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000503 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000504 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000505 return DVar;
506 }
507 }
508 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
509 // in a Construct, implicitly determined, p.3]
510 // For constructs other than task, if no default clause is present, these
511 // variables inherit their data-sharing attributes from the enclosing
512 // context.
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000513 return getDSA(++Iter, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000514}
515
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000516Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000517 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000518 D = getCanonicalDecl(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000519 auto It = Stack.back().AlignedMap.find(D);
520 if (It == Stack.back().AlignedMap.end()) {
521 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
522 Stack.back().AlignedMap[D] = NewDE;
523 return nullptr;
524 } else {
525 assert(It->second && "Unexpected nullptr expr in the aligned map");
526 return It->second;
527 }
528 return nullptr;
529}
530
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000531void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000532 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000533 D = getCanonicalDecl(D);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000534 Stack.back().LCVMap.insert(
535 std::make_pair(D, LCDeclInfo(Stack.back().LCVMap.size() + 1, Capture)));
Alexey Bataev9c821032015-04-30 04:23:23 +0000536}
537
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000538DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000539 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000540 D = getCanonicalDecl(D);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000541 return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D]
542 : LCDeclInfo(0, nullptr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000543}
544
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000545DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000546 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000547 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000548 return Stack[Stack.size() - 2].LCVMap.count(D) > 0
549 ? Stack[Stack.size() - 2].LCVMap[D]
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000550 : LCDeclInfo(0, nullptr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000551}
552
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000553ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000554 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
555 if (Stack[Stack.size() - 2].LCVMap.size() < I)
556 return nullptr;
557 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000558 if (Pair.second.first == I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000559 return Pair.first;
560 }
561 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000562}
563
Alexey Bataev90c228f2016-02-08 09:29:13 +0000564void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
565 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000566 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000567 if (A == OMPC_threadprivate) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000568 auto &Data = Stack[0].SharingMap[D];
569 Data.Attributes = A;
570 Data.RefExpr.setPointer(E);
571 Data.PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000572 } else {
573 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000574 auto &Data = Stack.back().SharingMap[D];
575 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
576 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
577 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
578 (isLoopControlVariable(D).first && A == OMPC_private));
579 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
580 Data.RefExpr.setInt(/*IntVal=*/true);
581 return;
582 }
583 const bool IsLastprivate =
584 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
585 Data.Attributes = A;
586 Data.RefExpr.setPointerAndInt(E, IsLastprivate);
587 Data.PrivateCopy = PrivateCopy;
588 if (PrivateCopy) {
589 auto &Data = Stack.back().SharingMap[PrivateCopy->getDecl()];
590 Data.Attributes = A;
591 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
592 Data.PrivateCopy = nullptr;
593 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000594 }
595}
596
Alexey Bataeved09d242014-05-28 05:53:51 +0000597bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000598 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000599 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000600 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000601 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000602 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000603 ++I;
604 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000605 if (I == E)
606 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000607 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000608 Scope *CurScope = getCurScope();
609 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000610 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000611 }
612 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000613 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000614 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000615}
616
Alexey Bataev39f915b82015-05-08 10:41:21 +0000617/// \brief Build a variable declaration for OpenMP loop iteration variable.
618static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000619 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000620 DeclContext *DC = SemaRef.CurContext;
621 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
622 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
623 VarDecl *Decl =
624 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000625 if (Attrs) {
626 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
627 I != E; ++I)
628 Decl->addAttr(*I);
629 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000630 Decl->setImplicit();
631 return Decl;
632}
633
634static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
635 SourceLocation Loc,
636 bool RefersToCapture = false) {
637 D->setReferenced();
638 D->markUsed(S.Context);
639 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
640 SourceLocation(), D, RefersToCapture, Loc, Ty,
641 VK_LValue);
642}
643
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000644DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
645 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000646 DSAVarData DVar;
647
648 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
649 // in a Construct, C/C++, predetermined, p.1]
650 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000651 auto *VD = dyn_cast<VarDecl>(D);
652 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
653 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
Samuel Antaof8b50122015-07-13 22:54:53 +0000654 SemaRef.getLangOpts().OpenMPUseTLS &&
655 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000656 (VD && VD->getStorageClass() == SC_Register &&
657 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
658 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
Alexey Bataev39f915b82015-05-08 10:41:21 +0000659 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000660 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000661 }
662 if (Stack[0].SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000663 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr.getPointer();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000664 DVar.CKind = OMPC_threadprivate;
665 return DVar;
666 }
667
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000668 if (Stack.size() == 1) {
669 // Not in OpenMP execution region and top scope was already checked.
670 return DVar;
671 }
672
Alexey Bataev758e55e2013-09-06 18:03:48 +0000673 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000674 // in a Construct, C/C++, predetermined, p.4]
675 // Static data members are shared.
676 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
677 // in a Construct, C/C++, predetermined, p.7]
678 // Variables with static storage duration that are declared in a scope
679 // inside the construct are shared.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000680 auto &&MatchesAlways = [](OpenMPDirectiveKind) -> bool { return true; };
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000681 if (VD && VD->isStaticDataMember()) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000682 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000683 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000684 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000685
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000686 DVar.CKind = OMPC_shared;
687 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000688 }
689
690 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000691 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
692 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000693 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
694 // in a Construct, C/C++, predetermined, p.6]
695 // Variables with const qualified type having no mutable member are
696 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000697 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000698 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +0000699 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
700 if (auto *CTD = CTSD->getSpecializedTemplate())
701 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000702 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +0000703 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
704 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000705 // Variables with const-qualified type having no mutable member may be
706 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000707 DSAVarData DVarTemp = hasDSA(
708 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_firstprivate; },
709 MatchesAlways, FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000710 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
711 return DVar;
712
Alexey Bataev758e55e2013-09-06 18:03:48 +0000713 DVar.CKind = OMPC_shared;
714 return DVar;
715 }
716
Alexey Bataev758e55e2013-09-06 18:03:48 +0000717 // Explicitly specified attributes and local variables with predetermined
718 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000719 auto StartI = std::next(Stack.rbegin());
720 auto EndI = std::prev(Stack.rend());
721 if (FromParent && StartI != EndI) {
722 StartI = std::next(StartI);
723 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000724 auto I = std::prev(StartI);
725 if (I->SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000726 DVar.RefExpr = I->SharingMap[D].RefExpr.getPointer();
Alexey Bataev90c228f2016-02-08 09:29:13 +0000727 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000728 DVar.CKind = I->SharingMap[D].Attributes;
729 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000730 }
731
732 return DVar;
733}
734
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000735DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
736 bool FromParent) {
737 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000738 auto StartI = Stack.rbegin();
739 auto EndI = std::prev(Stack.rend());
740 if (FromParent && StartI != EndI) {
741 StartI = std::next(StartI);
742 }
743 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000744}
745
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000746DSAStackTy::DSAVarData
747DSAStackTy::hasDSA(ValueDecl *D,
748 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
749 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
750 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000751 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000752 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000753 auto EndI = Stack.rend();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000754 if (FromParent && StartI != EndI) {
755 StartI = std::next(StartI);
756 }
757 for (auto I = StartI, EE = EndI; I != EE; ++I) {
758 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000759 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000760 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000761 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000762 return DVar;
763 }
764 return DSAVarData();
765}
766
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000767DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
768 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
769 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
770 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000771 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000772 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000773 auto EndI = Stack.rend();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000774 if (FromParent && StartI != EndI) {
775 StartI = std::next(StartI);
776 }
777 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000778 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000779 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000780 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000781 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000782 return DVar;
783 return DSAVarData();
784 }
785 return DSAVarData();
786}
787
Alexey Bataevaac108a2015-06-23 04:51:00 +0000788bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000789 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000790 unsigned Level, bool NotLastprivate) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000791 if (CPred(ClauseKindMode))
792 return true;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000793 D = getCanonicalDecl(D);
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000794 auto StartI = std::next(Stack.begin());
795 auto EndI = Stack.end();
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000796 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000797 return false;
798 std::advance(StartI, Level);
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000799 return (StartI->SharingMap.count(D) > 0) &&
800 StartI->SharingMap[D].RefExpr.getPointer() &&
801 CPred(StartI->SharingMap[D].Attributes) &&
802 (!NotLastprivate || !StartI->SharingMap[D].RefExpr.getInt());
Alexey Bataevaac108a2015-06-23 04:51:00 +0000803}
804
Samuel Antao4be30e92015-10-02 17:14:03 +0000805bool DSAStackTy::hasExplicitDirective(
806 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
807 unsigned Level) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000808 auto StartI = std::next(Stack.begin());
809 auto EndI = Stack.end();
Samuel Antao4be30e92015-10-02 17:14:03 +0000810 if (std::distance(StartI, EndI) <= (int)Level)
811 return false;
812 std::advance(StartI, Level);
813 return DPred(StartI->Directive);
814}
815
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000816bool DSAStackTy::hasDirective(
817 const llvm::function_ref<bool(OpenMPDirectiveKind,
818 const DeclarationNameInfo &, SourceLocation)>
819 &DPred,
820 bool FromParent) {
Samuel Antaof0d79752016-05-27 15:21:27 +0000821 // We look only in the enclosing region.
822 if (Stack.size() < 2)
823 return false;
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000824 auto StartI = std::next(Stack.rbegin());
825 auto EndI = std::prev(Stack.rend());
826 if (FromParent && StartI != EndI) {
827 StartI = std::next(StartI);
828 }
829 for (auto I = StartI, EE = EndI; I != EE; ++I) {
830 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
831 return true;
832 }
833 return false;
834}
835
Alexey Bataev758e55e2013-09-06 18:03:48 +0000836void Sema::InitDataSharingAttributesStack() {
837 VarDataSharingAttributesStack = new DSAStackTy(*this);
838}
839
840#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
841
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000842bool Sema::IsOpenMPCapturedByRef(ValueDecl *D, unsigned Level) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000843 assert(LangOpts.OpenMP && "OpenMP is not allowed");
844
845 auto &Ctx = getASTContext();
846 bool IsByRef = true;
847
848 // Find the directive that is associated with the provided scope.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000849 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000850
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000851 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000852 // This table summarizes how a given variable should be passed to the device
853 // given its type and the clauses where it appears. This table is based on
854 // the description in OpenMP 4.5 [2.10.4, target Construct] and
855 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
856 //
857 // =========================================================================
858 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
859 // | |(tofrom:scalar)| | pvt | | | |
860 // =========================================================================
861 // | scl | | | | - | | bycopy|
862 // | scl | | - | x | - | - | bycopy|
863 // | scl | | x | - | - | - | null |
864 // | scl | x | | | - | | byref |
865 // | scl | x | - | x | - | - | bycopy|
866 // | scl | x | x | - | - | - | null |
867 // | scl | | - | - | - | x | byref |
868 // | scl | x | - | - | - | x | byref |
869 //
870 // | agg | n.a. | | | - | | byref |
871 // | agg | n.a. | - | x | - | - | byref |
872 // | agg | n.a. | x | - | - | - | null |
873 // | agg | n.a. | - | - | - | x | byref |
874 // | agg | n.a. | - | - | - | x[] | byref |
875 //
876 // | ptr | n.a. | | | - | | bycopy|
877 // | ptr | n.a. | - | x | - | - | bycopy|
878 // | ptr | n.a. | x | - | - | - | null |
879 // | ptr | n.a. | - | - | - | x | byref |
880 // | ptr | n.a. | - | - | - | x[] | bycopy|
881 // | ptr | n.a. | - | - | x | | bycopy|
882 // | ptr | n.a. | - | - | x | x | bycopy|
883 // | ptr | n.a. | - | - | x | x[] | bycopy|
884 // =========================================================================
885 // Legend:
886 // scl - scalar
887 // ptr - pointer
888 // agg - aggregate
889 // x - applies
890 // - - invalid in this combination
891 // [] - mapped with an array section
892 // byref - should be mapped by reference
893 // byval - should be mapped by value
894 // null - initialize a local variable to null on the device
895 //
896 // Observations:
897 // - All scalar declarations that show up in a map clause have to be passed
898 // by reference, because they may have been mapped in the enclosing data
899 // environment.
900 // - If the scalar value does not fit the size of uintptr, it has to be
901 // passed by reference, regardless the result in the table above.
902 // - For pointers mapped by value that have either an implicit map or an
903 // array section, the runtime library may pass the NULL value to the
904 // device instead of the value passed to it by the compiler.
905
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000906
907 if (Ty->isReferenceType())
908 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
Samuel Antao86ace552016-04-27 22:40:57 +0000909
910 // Locate map clauses and see if the variable being captured is referred to
911 // in any of those clauses. Here we only care about variables, not fields,
912 // because fields are part of aggregates.
913 bool IsVariableUsedInMapClause = false;
914 bool IsVariableAssociatedWithSection = false;
915
916 DSAStack->checkMappableExprComponentListsForDecl(
917 D, /*CurrentRegionOnly=*/true,
918 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
919 MapExprComponents) {
920
921 auto EI = MapExprComponents.rbegin();
922 auto EE = MapExprComponents.rend();
923
924 assert(EI != EE && "Invalid map expression!");
925
926 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
927 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
928
929 ++EI;
930 if (EI == EE)
931 return false;
932
933 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
934 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
935 isa<MemberExpr>(EI->getAssociatedExpression())) {
936 IsVariableAssociatedWithSection = true;
937 // There is nothing more we need to know about this variable.
938 return true;
939 }
940
941 // Keep looking for more map info.
942 return false;
943 });
944
945 if (IsVariableUsedInMapClause) {
946 // If variable is identified in a map clause it is always captured by
947 // reference except if it is a pointer that is dereferenced somehow.
948 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
949 } else {
950 // By default, all the data that has a scalar type is mapped by copy.
951 IsByRef = !Ty->isScalarType();
952 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000953 }
954
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000955 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
956 IsByRef = !DSAStack->hasExplicitDSA(
957 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
958 Level, /*NotLastprivate=*/true);
959 }
960
Samuel Antao86ace552016-04-27 22:40:57 +0000961 // When passing data by copy, we need to make sure it fits the uintptr size
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000962 // and alignment, because the runtime library only deals with uintptr types.
963 // If it does not fit the uintptr size, we need to pass the data by reference
964 // instead.
965 if (!IsByRef &&
966 (Ctx.getTypeSizeInChars(Ty) >
967 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000968 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000969 IsByRef = true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000970 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000971
972 return IsByRef;
973}
974
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000975unsigned Sema::getOpenMPNestingLevel() const {
976 assert(getLangOpts().OpenMP);
977 return DSAStack->getNestingLevel();
978}
979
Alexey Bataev90c228f2016-02-08 09:29:13 +0000980VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000981 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000982 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000983
984 // If we are attempting to capture a global variable in a directive with
985 // 'target' we return true so that this global is also mapped to the device.
986 //
987 // FIXME: If the declaration is enclosed in a 'declare target' directive,
988 // then it should not be captured. Therefore, an extra check has to be
989 // inserted here once support for 'declare target' is added.
990 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000991 auto *VD = dyn_cast<VarDecl>(D);
992 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000993 if (DSAStack->getCurrentDirective() == OMPD_target &&
Alexey Bataev90c228f2016-02-08 09:29:13 +0000994 !DSAStack->isClauseParsingMode())
995 return VD;
Samuel Antaof0d79752016-05-27 15:21:27 +0000996 if (DSAStack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000997 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
998 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000999 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +00001000 },
Alexey Bataev90c228f2016-02-08 09:29:13 +00001001 false))
1002 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +00001003 }
1004
Alexey Bataev48977c32015-08-04 08:10:48 +00001005 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1006 (!DSAStack->isClauseParsingMode() ||
1007 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001008 auto &&Info = DSAStack->isLoopControlVariable(D);
1009 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001010 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001011 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001012 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001013 return VD ? VD : Info.second;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001014 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001015 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +00001016 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001017 DVarPrivate = DSAStack->hasDSA(
1018 D, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
1019 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +00001020 if (DVarPrivate.CKind != OMPC_unknown)
1021 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001022 }
Alexey Bataev90c228f2016-02-08 09:29:13 +00001023 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +00001024}
1025
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001026bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +00001027 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1028 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001029 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +00001030}
1031
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001032bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +00001033 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1034 // Return true if the current level is no longer enclosed in a target region.
1035
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001036 auto *VD = dyn_cast<VarDecl>(D);
1037 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001038 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1039 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +00001040}
1041
Alexey Bataeved09d242014-05-28 05:53:51 +00001042void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001043
1044void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1045 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001046 Scope *CurScope, SourceLocation Loc) {
1047 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001048 PushExpressionEvaluationContext(PotentiallyEvaluated);
1049}
1050
Alexey Bataevaac108a2015-06-23 04:51:00 +00001051void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1052 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001053}
1054
Alexey Bataevaac108a2015-06-23 04:51:00 +00001055void Sema::EndOpenMPClause() {
1056 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001057}
1058
Alexey Bataev758e55e2013-09-06 18:03:48 +00001059void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001060 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1061 // A variable of class type (or array thereof) that appears in a lastprivate
1062 // clause requires an accessible, unambiguous default constructor for the
1063 // class type, unless the list item is also specified in a firstprivate
1064 // clause.
1065 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001066 for (auto *C : D->clauses()) {
1067 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1068 SmallVector<Expr *, 8> PrivateCopies;
1069 for (auto *DE : Clause->varlists()) {
1070 if (DE->isValueDependent() || DE->isTypeDependent()) {
1071 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001072 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00001073 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001074 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataev005248a2016-02-25 05:25:57 +00001075 VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1076 QualType Type = VD->getType().getNonReferenceType();
1077 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001078 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001079 // Generate helper private variable and initialize it with the
1080 // default value. The address of the original variable is replaced
1081 // by the address of the new private variable in CodeGen. This new
1082 // variable is not added to IdResolver, so the code in the OpenMP
1083 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001084 auto *VDPrivate = buildVarDecl(
1085 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev005248a2016-02-25 05:25:57 +00001086 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001087 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1088 if (VDPrivate->isInvalidDecl())
1089 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001090 PrivateCopies.push_back(buildDeclRefExpr(
1091 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001092 } else {
1093 // The variable is also a firstprivate, so initialization sequence
1094 // for private copy is generated already.
1095 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001096 }
1097 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001098 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001099 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001100 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001101 }
1102 }
1103 }
1104
Alexey Bataev758e55e2013-09-06 18:03:48 +00001105 DSAStack->pop();
1106 DiscardCleanupsInEvaluationContext();
1107 PopExpressionEvaluationContext();
1108}
1109
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001110static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1111 Expr *NumIterations, Sema &SemaRef,
1112 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00001113
Alexey Bataeva769e072013-03-22 06:34:35 +00001114namespace {
1115
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001116class VarDeclFilterCCC : public CorrectionCandidateCallback {
1117private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001118 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001119
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001120public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001121 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001122 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001123 NamedDecl *ND = Candidate.getCorrectionDecl();
1124 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1125 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001126 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1127 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001128 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001129 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001130 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001131};
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001132
1133class VarOrFuncDeclFilterCCC : public CorrectionCandidateCallback {
1134private:
1135 Sema &SemaRef;
1136
1137public:
1138 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1139 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1140 NamedDecl *ND = Candidate.getCorrectionDecl();
1141 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
1142 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1143 SemaRef.getCurScope());
1144 }
1145 return false;
1146 }
1147};
1148
Alexey Bataeved09d242014-05-28 05:53:51 +00001149} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001150
1151ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1152 CXXScopeSpec &ScopeSpec,
1153 const DeclarationNameInfo &Id) {
1154 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1155 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1156
1157 if (Lookup.isAmbiguous())
1158 return ExprError();
1159
1160 VarDecl *VD;
1161 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001162 if (TypoCorrection Corrected = CorrectTypo(
1163 Id, LookupOrdinaryName, CurScope, nullptr,
1164 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001165 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001166 PDiag(Lookup.empty()
1167 ? diag::err_undeclared_var_use_suggest
1168 : diag::err_omp_expected_var_arg_suggest)
1169 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001170 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001171 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001172 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1173 : diag::err_omp_expected_var_arg)
1174 << Id.getName();
1175 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001176 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001177 } else {
1178 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001179 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001180 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1181 return ExprError();
1182 }
1183 }
1184 Lookup.suppressDiagnostics();
1185
1186 // OpenMP [2.9.2, Syntax, C/C++]
1187 // Variables must be file-scope, namespace-scope, or static block-scope.
1188 if (!VD->hasGlobalStorage()) {
1189 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001190 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1191 bool IsDecl =
1192 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001193 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001194 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1195 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001196 return ExprError();
1197 }
1198
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001199 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1200 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001201 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1202 // A threadprivate directive for file-scope variables must appear outside
1203 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001204 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1205 !getCurLexicalContext()->isTranslationUnit()) {
1206 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001207 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1208 bool IsDecl =
1209 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1210 Diag(VD->getLocation(),
1211 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1212 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001213 return ExprError();
1214 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001215 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1216 // A threadprivate directive for static class member variables must appear
1217 // in the class definition, in the same scope in which the member
1218 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001219 if (CanonicalVD->isStaticDataMember() &&
1220 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1221 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001222 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1223 bool IsDecl =
1224 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1225 Diag(VD->getLocation(),
1226 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1227 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001228 return ExprError();
1229 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001230 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1231 // A threadprivate directive for namespace-scope variables must appear
1232 // outside any definition or declaration other than the namespace
1233 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001234 if (CanonicalVD->getDeclContext()->isNamespace() &&
1235 (!getCurLexicalContext()->isFileContext() ||
1236 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1237 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001238 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1239 bool IsDecl =
1240 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1241 Diag(VD->getLocation(),
1242 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1243 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001244 return ExprError();
1245 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001246 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1247 // A threadprivate directive for static block-scope variables must appear
1248 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001249 if (CanonicalVD->isStaticLocal() && CurScope &&
1250 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001251 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001252 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1253 bool IsDecl =
1254 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1255 Diag(VD->getLocation(),
1256 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1257 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001258 return ExprError();
1259 }
1260
1261 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1262 // A threadprivate directive must lexically precede all references to any
1263 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001264 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001265 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001266 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001267 return ExprError();
1268 }
1269
1270 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001271 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1272 SourceLocation(), VD,
1273 /*RefersToEnclosingVariableOrCapture=*/false,
1274 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001275}
1276
Alexey Bataeved09d242014-05-28 05:53:51 +00001277Sema::DeclGroupPtrTy
1278Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1279 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001280 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001281 CurContext->addDecl(D);
1282 return DeclGroupPtrTy::make(DeclGroupRef(D));
1283 }
David Blaikie0403cb12016-01-15 23:43:25 +00001284 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001285}
1286
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001287namespace {
1288class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1289 Sema &SemaRef;
1290
1291public:
1292 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1293 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1294 if (VD->hasLocalStorage()) {
1295 SemaRef.Diag(E->getLocStart(),
1296 diag::err_omp_local_var_in_threadprivate_init)
1297 << E->getSourceRange();
1298 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1299 << VD << VD->getSourceRange();
1300 return true;
1301 }
1302 }
1303 return false;
1304 }
1305 bool VisitStmt(const Stmt *S) {
1306 for (auto Child : S->children()) {
1307 if (Child && Visit(Child))
1308 return true;
1309 }
1310 return false;
1311 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001312 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001313};
1314} // namespace
1315
Alexey Bataeved09d242014-05-28 05:53:51 +00001316OMPThreadPrivateDecl *
1317Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001318 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001319 for (auto &RefExpr : VarList) {
1320 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001321 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1322 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001323
Alexey Bataev376b4a42016-02-09 09:41:09 +00001324 // Mark variable as used.
1325 VD->setReferenced();
1326 VD->markUsed(Context);
1327
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001328 QualType QType = VD->getType();
1329 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1330 // It will be analyzed later.
1331 Vars.push_back(DE);
1332 continue;
1333 }
1334
Alexey Bataeva769e072013-03-22 06:34:35 +00001335 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1336 // A threadprivate variable must not have an incomplete type.
1337 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001338 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001339 continue;
1340 }
1341
1342 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1343 // A threadprivate variable must not have a reference type.
1344 if (VD->getType()->isReferenceType()) {
1345 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001346 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1347 bool IsDecl =
1348 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1349 Diag(VD->getLocation(),
1350 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1351 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001352 continue;
1353 }
1354
Samuel Antaof8b50122015-07-13 22:54:53 +00001355 // Check if this is a TLS variable. If TLS is not being supported, produce
1356 // the corresponding diagnostic.
1357 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1358 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1359 getLangOpts().OpenMPUseTLS &&
1360 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001361 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1362 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001363 Diag(ILoc, diag::err_omp_var_thread_local)
1364 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001365 bool IsDecl =
1366 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1367 Diag(VD->getLocation(),
1368 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1369 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001370 continue;
1371 }
1372
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001373 // Check if initial value of threadprivate variable reference variable with
1374 // local storage (it is not supported by runtime).
1375 if (auto Init = VD->getAnyInitializer()) {
1376 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001377 if (Checker.Visit(Init))
1378 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001379 }
1380
Alexey Bataeved09d242014-05-28 05:53:51 +00001381 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001382 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001383 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1384 Context, SourceRange(Loc, Loc)));
1385 if (auto *ML = Context.getASTMutationListener())
1386 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001387 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001388 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001389 if (!Vars.empty()) {
1390 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1391 Vars);
1392 D->setAccess(AS_public);
1393 }
1394 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001395}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001396
Alexey Bataev7ff55242014-06-19 09:13:45 +00001397static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001398 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001399 bool IsLoopIterVar = false) {
1400 if (DVar.RefExpr) {
1401 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1402 << getOpenMPClauseName(DVar.CKind);
1403 return;
1404 }
1405 enum {
1406 PDSA_StaticMemberShared,
1407 PDSA_StaticLocalVarShared,
1408 PDSA_LoopIterVarPrivate,
1409 PDSA_LoopIterVarLinear,
1410 PDSA_LoopIterVarLastprivate,
1411 PDSA_ConstVarShared,
1412 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001413 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001414 PDSA_LocalVarPrivate,
1415 PDSA_Implicit
1416 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001417 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001418 auto ReportLoc = D->getLocation();
1419 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001420 if (IsLoopIterVar) {
1421 if (DVar.CKind == OMPC_private)
1422 Reason = PDSA_LoopIterVarPrivate;
1423 else if (DVar.CKind == OMPC_lastprivate)
1424 Reason = PDSA_LoopIterVarLastprivate;
1425 else
1426 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00001427 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1428 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001429 Reason = PDSA_TaskVarFirstprivate;
1430 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001431 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001432 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001433 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001434 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001435 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001436 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001437 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001438 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001439 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001440 ReportHint = true;
1441 Reason = PDSA_LocalVarPrivate;
1442 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001443 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001444 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001445 << Reason << ReportHint
1446 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1447 } else if (DVar.ImplicitDSALoc.isValid()) {
1448 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1449 << getOpenMPClauseName(DVar.CKind);
1450 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001451}
1452
Alexey Bataev758e55e2013-09-06 18:03:48 +00001453namespace {
1454class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1455 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001456 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001457 bool ErrorFound;
1458 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001459 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001460 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001461
Alexey Bataev758e55e2013-09-06 18:03:48 +00001462public:
1463 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00001464 if (E->isTypeDependent() || E->isValueDependent() ||
1465 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1466 return;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001467 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001468 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001469 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1470 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001471
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001472 auto DVar = Stack->getTopDSA(VD, false);
1473 // Check if the variable has explicit DSA set and stop analysis if it so.
1474 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001475
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001476 auto ELoc = E->getExprLoc();
1477 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001478 // The default(none) clause requires that each variable that is referenced
1479 // in the construct, and does not have a predetermined data-sharing
1480 // attribute, must have its data-sharing attribute explicitly determined
1481 // by being listed in a data-sharing attribute clause.
1482 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001483 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001484 VarsWithInheritedDSA.count(VD) == 0) {
1485 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001486 return;
1487 }
1488
1489 // OpenMP [2.9.3.6, Restrictions, p.2]
1490 // A list item that appears in a reduction clause of the innermost
1491 // enclosing worksharing or parallel construct may not be accessed in an
1492 // explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001493 DVar = Stack->hasInnermostDSA(
1494 VD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1495 [](OpenMPDirectiveKind K) -> bool {
1496 return isOpenMPParallelDirective(K) ||
1497 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
1498 },
1499 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001500 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001501 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001502 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1503 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001504 return;
1505 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001506
1507 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001508 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001509 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1510 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001511 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001512 }
1513 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001514 void VisitMemberExpr(MemberExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00001515 if (E->isTypeDependent() || E->isValueDependent() ||
1516 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1517 return;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001518 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1519 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1520 auto DVar = Stack->getTopDSA(FD, false);
1521 // Check if the variable has explicit DSA set and stop analysis if it
1522 // so.
1523 if (DVar.RefExpr)
1524 return;
1525
1526 auto ELoc = E->getExprLoc();
1527 auto DKind = Stack->getCurrentDirective();
1528 // OpenMP [2.9.3.6, Restrictions, p.2]
1529 // A list item that appears in a reduction clause of the innermost
1530 // enclosing worksharing or parallel construct may not be accessed in
Alexey Bataevd985eda2016-02-10 11:29:16 +00001531 // an explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001532 DVar = Stack->hasInnermostDSA(
1533 FD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1534 [](OpenMPDirectiveKind K) -> bool {
1535 return isOpenMPParallelDirective(K) ||
1536 isOpenMPWorksharingDirective(K) ||
1537 isOpenMPTeamsDirective(K);
1538 },
1539 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001540 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001541 ErrorFound = true;
1542 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1543 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1544 return;
1545 }
1546
1547 // Define implicit data-sharing attributes for task.
1548 DVar = Stack->getImplicitDSA(FD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001549 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1550 !Stack->isLoopControlVariable(FD).first)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001551 ImplicitFirstprivate.push_back(E);
1552 }
1553 }
1554 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001555 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001556 for (auto *C : S->clauses()) {
1557 // Skip analysis of arguments of implicitly defined firstprivate clause
1558 // for task directives.
1559 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1560 for (auto *CC : C->children()) {
1561 if (CC)
1562 Visit(CC);
1563 }
1564 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001565 }
1566 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001567 for (auto *C : S->children()) {
1568 if (C && !isa<OMPExecutableDirective>(C))
1569 Visit(C);
1570 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001571 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001572
1573 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001574 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001575 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001576 return VarsWithInheritedDSA;
1577 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001578
Alexey Bataev7ff55242014-06-19 09:13:45 +00001579 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1580 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001581};
Alexey Bataeved09d242014-05-28 05:53:51 +00001582} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001583
Alexey Bataevbae9a792014-06-27 10:37:06 +00001584void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001585 switch (DKind) {
1586 case OMPD_parallel: {
1587 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001588 QualType KmpInt32PtrTy =
1589 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001590 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001591 std::make_pair(".global_tid.", KmpInt32PtrTy),
1592 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1593 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001594 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001595 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1596 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001597 break;
1598 }
1599 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001600 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001601 std::make_pair(StringRef(), QualType()) // __context with shared vars
1602 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001603 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1604 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001605 break;
1606 }
1607 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001608 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001609 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001610 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001611 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1612 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001613 break;
1614 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001615 case OMPD_for_simd: {
1616 Sema::CapturedParamNameType Params[] = {
1617 std::make_pair(StringRef(), QualType()) // __context with shared vars
1618 };
1619 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1620 Params);
1621 break;
1622 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001623 case OMPD_sections: {
1624 Sema::CapturedParamNameType Params[] = {
1625 std::make_pair(StringRef(), QualType()) // __context with shared vars
1626 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001627 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1628 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001629 break;
1630 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001631 case OMPD_section: {
1632 Sema::CapturedParamNameType Params[] = {
1633 std::make_pair(StringRef(), QualType()) // __context with shared vars
1634 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001635 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1636 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001637 break;
1638 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001639 case OMPD_single: {
1640 Sema::CapturedParamNameType Params[] = {
1641 std::make_pair(StringRef(), QualType()) // __context with shared vars
1642 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001643 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1644 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001645 break;
1646 }
Alexander Musman80c22892014-07-17 08:54:58 +00001647 case OMPD_master: {
1648 Sema::CapturedParamNameType Params[] = {
1649 std::make_pair(StringRef(), QualType()) // __context with shared vars
1650 };
1651 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1652 Params);
1653 break;
1654 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001655 case OMPD_critical: {
1656 Sema::CapturedParamNameType Params[] = {
1657 std::make_pair(StringRef(), QualType()) // __context with shared vars
1658 };
1659 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1660 Params);
1661 break;
1662 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001663 case OMPD_parallel_for: {
1664 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001665 QualType KmpInt32PtrTy =
1666 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001667 Sema::CapturedParamNameType Params[] = {
1668 std::make_pair(".global_tid.", KmpInt32PtrTy),
1669 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1670 std::make_pair(StringRef(), QualType()) // __context with shared vars
1671 };
1672 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1673 Params);
1674 break;
1675 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001676 case OMPD_parallel_for_simd: {
1677 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001678 QualType KmpInt32PtrTy =
1679 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001680 Sema::CapturedParamNameType Params[] = {
1681 std::make_pair(".global_tid.", KmpInt32PtrTy),
1682 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1683 std::make_pair(StringRef(), QualType()) // __context with shared vars
1684 };
1685 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1686 Params);
1687 break;
1688 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001689 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001690 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001691 QualType KmpInt32PtrTy =
1692 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001693 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001694 std::make_pair(".global_tid.", KmpInt32PtrTy),
1695 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001696 std::make_pair(StringRef(), QualType()) // __context with shared vars
1697 };
1698 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1699 Params);
1700 break;
1701 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001702 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001703 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001704 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1705 FunctionProtoType::ExtProtoInfo EPI;
1706 EPI.Variadic = true;
1707 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001708 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001709 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataev48591dd2016-04-20 04:01:36 +00001710 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1711 std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
1712 std::make_pair(".copy_fn.",
1713 Context.getPointerType(CopyFnType).withConst()),
1714 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001715 std::make_pair(StringRef(), QualType()) // __context with shared vars
1716 };
1717 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1718 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001719 // Mark this captured region as inlined, because we don't use outlined
1720 // function directly.
1721 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1722 AlwaysInlineAttr::CreateImplicit(
1723 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001724 break;
1725 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001726 case OMPD_ordered: {
1727 Sema::CapturedParamNameType Params[] = {
1728 std::make_pair(StringRef(), QualType()) // __context with shared vars
1729 };
1730 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1731 Params);
1732 break;
1733 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001734 case OMPD_atomic: {
1735 Sema::CapturedParamNameType Params[] = {
1736 std::make_pair(StringRef(), QualType()) // __context with shared vars
1737 };
1738 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1739 Params);
1740 break;
1741 }
Michael Wong65f367f2015-07-21 13:44:28 +00001742 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001743 case OMPD_target:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001744 case OMPD_target_parallel:
1745 case OMPD_target_parallel_for: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001746 Sema::CapturedParamNameType Params[] = {
1747 std::make_pair(StringRef(), QualType()) // __context with shared vars
1748 };
1749 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1750 Params);
1751 break;
1752 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001753 case OMPD_teams: {
1754 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001755 QualType KmpInt32PtrTy =
1756 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001757 Sema::CapturedParamNameType Params[] = {
1758 std::make_pair(".global_tid.", KmpInt32PtrTy),
1759 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1760 std::make_pair(StringRef(), QualType()) // __context with shared vars
1761 };
1762 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1763 Params);
1764 break;
1765 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001766 case OMPD_taskgroup: {
1767 Sema::CapturedParamNameType Params[] = {
1768 std::make_pair(StringRef(), QualType()) // __context with shared vars
1769 };
1770 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1771 Params);
1772 break;
1773 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00001774 case OMPD_taskloop:
1775 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00001776 QualType KmpInt32Ty =
1777 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1778 QualType KmpUInt64Ty =
1779 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
1780 QualType KmpInt64Ty =
1781 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
1782 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1783 FunctionProtoType::ExtProtoInfo EPI;
1784 EPI.Variadic = true;
1785 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00001786 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00001787 std::make_pair(".global_tid.", KmpInt32Ty),
1788 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1789 std::make_pair(".privates.",
1790 Context.VoidPtrTy.withConst().withRestrict()),
1791 std::make_pair(
1792 ".copy_fn.",
1793 Context.getPointerType(CopyFnType).withConst().withRestrict()),
1794 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1795 std::make_pair(".lb.", KmpUInt64Ty),
1796 std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty),
1797 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataev49f6e782015-12-01 04:18:41 +00001798 std::make_pair(StringRef(), QualType()) // __context with shared vars
1799 };
1800 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1801 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00001802 // Mark this captured region as inlined, because we don't use outlined
1803 // function directly.
1804 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1805 AlwaysInlineAttr::CreateImplicit(
1806 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev49f6e782015-12-01 04:18:41 +00001807 break;
1808 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001809 case OMPD_distribute: {
1810 Sema::CapturedParamNameType Params[] = {
1811 std::make_pair(StringRef(), QualType()) // __context with shared vars
1812 };
1813 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1814 Params);
1815 break;
1816 }
Kelvin Li4a39add2016-07-05 05:00:15 +00001817 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001818 case OMPD_distribute_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001819 case OMPD_distribute_parallel_for: {
1820 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1821 QualType KmpInt32PtrTy =
1822 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1823 Sema::CapturedParamNameType Params[] = {
1824 std::make_pair(".global_tid.", KmpInt32PtrTy),
1825 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1826 std::make_pair(".previous.lb.", Context.getSizeType()),
1827 std::make_pair(".previous.ub.", Context.getSizeType()),
1828 std::make_pair(StringRef(), QualType()) // __context with shared vars
1829 };
1830 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1831 Params);
1832 break;
1833 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001834 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001835 case OMPD_taskyield:
1836 case OMPD_barrier:
1837 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001838 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001839 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001840 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001841 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001842 case OMPD_target_exit_data:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001843 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001844 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001845 case OMPD_declare_target:
1846 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001847 case OMPD_target_update:
Alexey Bataev9959db52014-05-06 10:08:46 +00001848 llvm_unreachable("OpenMP Directive is not allowed");
1849 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001850 llvm_unreachable("Unknown OpenMP directive");
1851 }
1852}
1853
Alexey Bataev3392d762016-02-16 11:18:12 +00001854static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00001855 Expr *CaptureExpr, bool WithInit,
1856 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001857 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001858 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00001859 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00001860 QualType Ty = Init->getType();
1861 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1862 if (S.getLangOpts().CPlusPlus)
1863 Ty = C.getLValueReferenceType(Ty);
1864 else {
1865 Ty = C.getPointerType(Ty);
1866 ExprResult Res =
1867 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1868 if (!Res.isUsable())
1869 return nullptr;
1870 Init = Res.get();
1871 }
Alexey Bataev61205072016-03-02 04:57:40 +00001872 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001873 }
1874 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001875 if (!WithInit)
1876 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001877 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001878 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1879 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001880 return CED;
1881}
1882
Alexey Bataev61205072016-03-02 04:57:40 +00001883static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1884 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001885 OMPCapturedExprDecl *CD;
1886 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1887 CD = cast<OMPCapturedExprDecl>(VD);
1888 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00001889 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1890 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001891 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00001892 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00001893}
1894
Alexey Bataev5a3af132016-03-29 08:58:54 +00001895static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1896 if (!Ref) {
1897 auto *CD =
1898 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1899 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1900 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1901 CaptureExpr->getExprLoc());
1902 }
1903 ExprResult Res = Ref;
1904 if (!S.getLangOpts().CPlusPlus &&
1905 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1906 Ref->getType()->isPointerType())
1907 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1908 if (!Res.isUsable())
1909 return ExprError();
1910 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00001911}
1912
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001913StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1914 ArrayRef<OMPClause *> Clauses) {
1915 if (!S.isUsable()) {
1916 ActOnCapturedRegionError();
1917 return StmtError();
1918 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001919
1920 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001921 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001922 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001923 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001924 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001925 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001926 Clause->getClauseKind() == OMPC_copyprivate ||
1927 (getLangOpts().OpenMPUseTLS &&
1928 getASTContext().getTargetInfo().isTLSSupported() &&
1929 Clause->getClauseKind() == OMPC_copyin)) {
1930 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001931 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001932 for (auto *VarRef : Clause->children()) {
1933 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001934 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001935 }
1936 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001937 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001938 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001939 // Mark all variables in private list clauses as used in inner region.
1940 // Required for proper codegen of combined directives.
1941 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001942 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001943 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1944 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001945 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1946 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001947 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001948 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1949 if (auto *E = C->getPostUpdateExpr())
1950 MarkDeclarationsReferencedInExpr(E);
1951 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001952 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001953 if (Clause->getClauseKind() == OMPC_schedule)
1954 SC = cast<OMPScheduleClause>(Clause);
1955 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001956 OC = cast<OMPOrderedClause>(Clause);
1957 else if (Clause->getClauseKind() == OMPC_linear)
1958 LCs.push_back(cast<OMPLinearClause>(Clause));
1959 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001960 bool ErrorFound = false;
1961 // OpenMP, 2.7.1 Loop Construct, Restrictions
1962 // The nonmonotonic modifier cannot be specified if an ordered clause is
1963 // specified.
1964 if (SC &&
1965 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1966 SC->getSecondScheduleModifier() ==
1967 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1968 OC) {
1969 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1970 ? SC->getFirstScheduleModifierLoc()
1971 : SC->getSecondScheduleModifierLoc(),
1972 diag::err_omp_schedule_nonmonotonic_ordered)
1973 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1974 ErrorFound = true;
1975 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001976 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1977 for (auto *C : LCs) {
1978 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1979 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1980 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001981 ErrorFound = true;
1982 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001983 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1984 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1985 OC->getNumForLoops()) {
1986 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1987 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1988 ErrorFound = true;
1989 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001990 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001991 ActOnCapturedRegionError();
1992 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001993 }
1994 return ActOnCapturedRegionEnd(S.get());
1995}
1996
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001997static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1998 OpenMPDirectiveKind CurrentRegion,
1999 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002000 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002001 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002002 // Allowed nesting of constructs
2003 // +------------------+-----------------+------------------------------------+
2004 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
2005 // +------------------+-----------------+------------------------------------+
2006 // | parallel | parallel | * |
2007 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002008 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00002009 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002010 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002011 // | parallel | simd | * |
2012 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00002013 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002014 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002015 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002016 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002017 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002018 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002019 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002020 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002021 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002022 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002023 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002024 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002025 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002026 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002027 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002028 // | parallel | target parallel | * |
2029 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002030 // | parallel | target enter | * |
2031 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002032 // | parallel | target exit | * |
2033 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002034 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002035 // | parallel | cancellation | |
2036 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002037 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002038 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002039 // | parallel | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002040 // | parallel | distribute | + |
2041 // | parallel | distribute | + |
2042 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002043 // | parallel | distribute | + |
2044 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002045 // | parallel | distribute simd | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002046 // +------------------+-----------------+------------------------------------+
2047 // | for | parallel | * |
2048 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002049 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002050 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002051 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002052 // | for | simd | * |
2053 // | for | sections | + |
2054 // | for | section | + |
2055 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002056 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002057 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002058 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002059 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002060 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002061 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002062 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002063 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002064 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002065 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002066 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002067 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002068 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002069 // | for | target parallel | * |
2070 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002071 // | for | target enter | * |
2072 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002073 // | for | target exit | * |
2074 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002075 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002076 // | for | cancellation | |
2077 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002078 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002079 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002080 // | for | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002081 // | for | distribute | + |
2082 // | for | distribute | + |
2083 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002084 // | for | distribute | + |
2085 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002086 // | for | distribute simd | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002087 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00002088 // | master | parallel | * |
2089 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002090 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002091 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002092 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00002093 // | master | simd | * |
2094 // | master | sections | + |
2095 // | master | section | + |
2096 // | master | single | + |
2097 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002098 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00002099 // | master |parallel sections| * |
2100 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002101 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002102 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002103 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002104 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002105 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002106 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002107 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002108 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002109 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002110 // | master | target parallel | * |
2111 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002112 // | master | target enter | * |
2113 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002114 // | master | target exit | * |
2115 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002116 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002117 // | master | cancellation | |
2118 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002119 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002120 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002121 // | master | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002122 // | master | distribute | + |
2123 // | master | distribute | + |
2124 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002125 // | master | distribute | + |
2126 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002127 // | master | distribute simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002128 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002129 // | critical | parallel | * |
2130 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002131 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002132 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002133 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002134 // | critical | simd | * |
2135 // | critical | sections | + |
2136 // | critical | section | + |
2137 // | critical | single | + |
2138 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002139 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002140 // | critical |parallel sections| * |
2141 // | critical | task | * |
2142 // | critical | taskyield | * |
2143 // | critical | barrier | + |
2144 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002145 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002146 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002147 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002148 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002149 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002150 // | critical | target parallel | * |
2151 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002152 // | critical | target enter | * |
2153 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002154 // | critical | target exit | * |
2155 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002156 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002157 // | critical | cancellation | |
2158 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002159 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002160 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002161 // | critical | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002162 // | critical | distribute | + |
2163 // | critical | distribute | + |
2164 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002165 // | critical | distribute | + |
2166 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002167 // | critical | distribute simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002168 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002169 // | simd | parallel | |
2170 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002171 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00002172 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002173 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002174 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002175 // | simd | sections | |
2176 // | simd | section | |
2177 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002178 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002179 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002180 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002181 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002182 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002183 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002184 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002185 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002186 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002187 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002188 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002189 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002190 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002191 // | simd | target parallel | |
2192 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002193 // | simd | target enter | |
2194 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002195 // | simd | target exit | |
2196 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002197 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002198 // | simd | cancellation | |
2199 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002200 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002201 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002202 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002203 // | simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002204 // | simd | distribute | |
2205 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002206 // | simd | distribute | |
2207 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002208 // | simd | distribute simd | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002209 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002210 // | for simd | parallel | |
2211 // | for simd | for | |
2212 // | for simd | for simd | |
2213 // | for simd | master | |
2214 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002215 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002216 // | for simd | sections | |
2217 // | for simd | section | |
2218 // | for simd | single | |
2219 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002220 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002221 // | for simd |parallel sections| |
2222 // | for simd | task | |
2223 // | for simd | taskyield | |
2224 // | for simd | barrier | |
2225 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002226 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002227 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002228 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002229 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002230 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002231 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002232 // | for simd | target parallel | |
2233 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002234 // | for simd | target enter | |
2235 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002236 // | for simd | target exit | |
2237 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002238 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002239 // | for simd | cancellation | |
2240 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002241 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002242 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002243 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002244 // | for simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002245 // | for simd | distribute | |
2246 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002247 // | for simd | distribute | |
2248 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002249 // | for simd | distribute simd | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002250 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002251 // | parallel for simd| parallel | |
2252 // | parallel for simd| for | |
2253 // | parallel for simd| for simd | |
2254 // | parallel for simd| master | |
2255 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002256 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002257 // | parallel for simd| sections | |
2258 // | parallel for simd| section | |
2259 // | parallel for simd| single | |
2260 // | parallel for simd| parallel for | |
2261 // | parallel for simd|parallel for simd| |
2262 // | parallel for simd|parallel sections| |
2263 // | parallel for simd| task | |
2264 // | parallel for simd| taskyield | |
2265 // | parallel for simd| barrier | |
2266 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002267 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002268 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002269 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002270 // | parallel for simd| atomic | |
2271 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002272 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002273 // | parallel for simd| target parallel | |
2274 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002275 // | parallel for simd| target enter | |
2276 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002277 // | parallel for simd| target exit | |
2278 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002279 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002280 // | parallel for simd| cancellation | |
2281 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002282 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002283 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002284 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002285 // | parallel for simd| distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002286 // | parallel for simd| distribute | |
2287 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002288 // | parallel for simd| distribute | |
2289 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002290 // | parallel for simd| distribute simd | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002291 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002292 // | sections | parallel | * |
2293 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002294 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002295 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002296 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002297 // | sections | simd | * |
2298 // | sections | sections | + |
2299 // | sections | section | * |
2300 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002301 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002302 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002303 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002304 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002305 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002306 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002307 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002308 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002309 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002310 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002311 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002312 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002313 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002314 // | sections | target parallel | * |
2315 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002316 // | sections | target enter | * |
2317 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002318 // | sections | target exit | * |
2319 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002320 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002321 // | sections | cancellation | |
2322 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002323 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002324 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002325 // | sections | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002326 // | sections | distribute | + |
2327 // | sections | distribute | + |
2328 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002329 // | sections | distribute | + |
2330 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002331 // | sections | distribute simd | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002332 // +------------------+-----------------+------------------------------------+
2333 // | section | parallel | * |
2334 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002335 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002336 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002337 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002338 // | section | simd | * |
2339 // | section | sections | + |
2340 // | section | section | + |
2341 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002342 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002343 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002344 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002345 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002346 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002347 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002348 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002349 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002350 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002351 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002352 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002353 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002354 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002355 // | section | target parallel | * |
2356 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002357 // | section | target enter | * |
2358 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002359 // | section | target exit | * |
2360 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002361 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002362 // | section | cancellation | |
2363 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002364 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002365 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002366 // | section | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002367 // | section | distribute | + |
2368 // | section | distribute | + |
2369 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002370 // | section | distribute | + |
2371 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002372 // | section | distribute simd | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002373 // +------------------+-----------------+------------------------------------+
2374 // | single | parallel | * |
2375 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002376 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002377 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002378 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002379 // | single | simd | * |
2380 // | single | sections | + |
2381 // | single | section | + |
2382 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002383 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002384 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002385 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002386 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002387 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002388 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002389 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002390 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002391 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002392 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002393 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002394 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002395 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002396 // | single | target parallel | * |
2397 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002398 // | single | target enter | * |
2399 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002400 // | single | target exit | * |
2401 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002402 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002403 // | single | cancellation | |
2404 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002405 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002406 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002407 // | single | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002408 // | single | distribute | + |
2409 // | single | distribute | + |
2410 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002411 // | single | distribute | + |
2412 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002413 // | single | distribute simd | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002414 // +------------------+-----------------+------------------------------------+
2415 // | parallel for | parallel | * |
2416 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002417 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002418 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002419 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002420 // | parallel for | simd | * |
2421 // | parallel for | sections | + |
2422 // | parallel for | section | + |
2423 // | parallel for | single | + |
2424 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002425 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002426 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002427 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002428 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002429 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002430 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002431 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002432 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002433 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002434 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002435 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002436 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002437 // | parallel for | target parallel | * |
2438 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002439 // | parallel for | target enter | * |
2440 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002441 // | parallel for | target exit | * |
2442 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002443 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002444 // | parallel for | cancellation | |
2445 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002446 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002447 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002448 // | parallel for | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002449 // | parallel for | distribute | + |
2450 // | parallel for | distribute | + |
2451 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002452 // | parallel for | distribute | + |
2453 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002454 // | parallel for | distribute simd | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002455 // +------------------+-----------------+------------------------------------+
2456 // | parallel sections| parallel | * |
2457 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002458 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002459 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002460 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002461 // | parallel sections| simd | * |
2462 // | parallel sections| sections | + |
2463 // | parallel sections| section | * |
2464 // | parallel sections| single | + |
2465 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002466 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002467 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002468 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002469 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002470 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002471 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002472 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002473 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002474 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002475 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002476 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002477 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002478 // | parallel sections| target parallel | * |
2479 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002480 // | parallel sections| target enter | * |
2481 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002482 // | parallel sections| target exit | * |
2483 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002484 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002485 // | parallel sections| cancellation | |
2486 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002487 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002488 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002489 // | parallel sections| taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002490 // | parallel sections| distribute | + |
2491 // | parallel sections| distribute | + |
2492 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002493 // | parallel sections| distribute | + |
2494 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002495 // | parallel sections| distribute simd | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002496 // +------------------+-----------------+------------------------------------+
2497 // | task | parallel | * |
2498 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002499 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002500 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002501 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002502 // | task | simd | * |
2503 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002504 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002505 // | task | single | + |
2506 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002507 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002508 // | task |parallel sections| * |
2509 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002510 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002511 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002512 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002513 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002514 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002515 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002516 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002517 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002518 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002519 // | task | target parallel | * |
2520 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002521 // | task | target enter | * |
2522 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002523 // | task | target exit | * |
2524 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002525 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002526 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002527 // | | point | ! |
2528 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002529 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002530 // | task | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002531 // | task | distribute | + |
2532 // | task | distribute | + |
2533 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002534 // | task | distribute | + |
2535 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002536 // | task | distribute simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002537 // +------------------+-----------------+------------------------------------+
2538 // | ordered | parallel | * |
2539 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002540 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002541 // | ordered | master | * |
2542 // | ordered | critical | * |
2543 // | ordered | simd | * |
2544 // | ordered | sections | + |
2545 // | ordered | section | + |
2546 // | ordered | single | + |
2547 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002548 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002549 // | ordered |parallel sections| * |
2550 // | ordered | task | * |
2551 // | ordered | taskyield | * |
2552 // | ordered | barrier | + |
2553 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002554 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002555 // | ordered | flush | * |
2556 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002557 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002558 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002559 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002560 // | ordered | target parallel | * |
2561 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002562 // | ordered | target enter | * |
2563 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002564 // | ordered | target exit | * |
2565 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002566 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002567 // | ordered | cancellation | |
2568 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002569 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002570 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002571 // | ordered | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002572 // | ordered | distribute | + |
2573 // | ordered | distribute | + |
2574 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002575 // | ordered | distribute | + |
2576 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002577 // | ordered | distribute simd | + |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002578 // +------------------+-----------------+------------------------------------+
2579 // | atomic | parallel | |
2580 // | atomic | for | |
2581 // | atomic | for simd | |
2582 // | atomic | master | |
2583 // | atomic | critical | |
2584 // | atomic | simd | |
2585 // | atomic | sections | |
2586 // | atomic | section | |
2587 // | atomic | single | |
2588 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002589 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002590 // | atomic |parallel sections| |
2591 // | atomic | task | |
2592 // | atomic | taskyield | |
2593 // | atomic | barrier | |
2594 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002595 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002596 // | atomic | flush | |
2597 // | atomic | ordered | |
2598 // | atomic | atomic | |
2599 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002600 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002601 // | atomic | target parallel | |
2602 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002603 // | atomic | target enter | |
2604 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002605 // | atomic | target exit | |
2606 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002607 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002608 // | atomic | cancellation | |
2609 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002610 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002611 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002612 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002613 // | atomic | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002614 // | atomic | distribute | |
2615 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002616 // | atomic | distribute | |
2617 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002618 // | atomic | distribute simd | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002619 // +------------------+-----------------+------------------------------------+
2620 // | target | parallel | * |
2621 // | target | for | * |
2622 // | target | for simd | * |
2623 // | target | master | * |
2624 // | target | critical | * |
2625 // | target | simd | * |
2626 // | target | sections | * |
2627 // | target | section | * |
2628 // | target | single | * |
2629 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002630 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002631 // | target |parallel sections| * |
2632 // | target | task | * |
2633 // | target | taskyield | * |
2634 // | target | barrier | * |
2635 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002636 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002637 // | target | flush | * |
2638 // | target | ordered | * |
2639 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002640 // | target | target | |
2641 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002642 // | target | target parallel | |
2643 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002644 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002645 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002646 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002647 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002648 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002649 // | target | cancellation | |
2650 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002651 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002652 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002653 // | target | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002654 // | target | distribute | + |
2655 // | target | distribute | + |
2656 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002657 // | target | distribute | + |
2658 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002659 // | target | distribute simd | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002660 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002661 // | target parallel | parallel | * |
2662 // | target parallel | for | * |
2663 // | target parallel | for simd | * |
2664 // | target parallel | master | * |
2665 // | target parallel | critical | * |
2666 // | target parallel | simd | * |
2667 // | target parallel | sections | * |
2668 // | target parallel | section | * |
2669 // | target parallel | single | * |
2670 // | target parallel | parallel for | * |
2671 // | target parallel |parallel for simd| * |
2672 // | target parallel |parallel sections| * |
2673 // | target parallel | task | * |
2674 // | target parallel | taskyield | * |
2675 // | target parallel | barrier | * |
2676 // | target parallel | taskwait | * |
2677 // | target parallel | taskgroup | * |
2678 // | target parallel | flush | * |
2679 // | target parallel | ordered | * |
2680 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002681 // | target parallel | target | |
2682 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002683 // | target parallel | target parallel | |
2684 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002685 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002686 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002687 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002688 // | | data | |
2689 // | target parallel | teams | |
2690 // | target parallel | cancellation | |
2691 // | | point | ! |
2692 // | target parallel | cancel | ! |
2693 // | target parallel | taskloop | * |
2694 // | target parallel | taskloop simd | * |
2695 // | target parallel | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002696 // | target parallel | distribute | |
2697 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002698 // | target parallel | distribute | |
2699 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002700 // | target parallel | distribute simd | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002701 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002702 // | target parallel | parallel | * |
2703 // | for | | |
2704 // | target parallel | for | * |
2705 // | for | | |
2706 // | target parallel | for simd | * |
2707 // | for | | |
2708 // | target parallel | master | * |
2709 // | for | | |
2710 // | target parallel | critical | * |
2711 // | for | | |
2712 // | target parallel | simd | * |
2713 // | for | | |
2714 // | target parallel | sections | * |
2715 // | for | | |
2716 // | target parallel | section | * |
2717 // | for | | |
2718 // | target parallel | single | * |
2719 // | for | | |
2720 // | target parallel | parallel for | * |
2721 // | for | | |
2722 // | target parallel |parallel for simd| * |
2723 // | for | | |
2724 // | target parallel |parallel sections| * |
2725 // | for | | |
2726 // | target parallel | task | * |
2727 // | for | | |
2728 // | target parallel | taskyield | * |
2729 // | for | | |
2730 // | target parallel | barrier | * |
2731 // | for | | |
2732 // | target parallel | taskwait | * |
2733 // | for | | |
2734 // | target parallel | taskgroup | * |
2735 // | for | | |
2736 // | target parallel | flush | * |
2737 // | for | | |
2738 // | target parallel | ordered | * |
2739 // | for | | |
2740 // | target parallel | atomic | * |
2741 // | for | | |
2742 // | target parallel | target | |
2743 // | for | | |
2744 // | target parallel | target parallel | |
2745 // | for | | |
2746 // | target parallel | target parallel | |
2747 // | for | for | |
2748 // | target parallel | target enter | |
2749 // | for | data | |
2750 // | target parallel | target exit | |
2751 // | for | data | |
2752 // | target parallel | teams | |
2753 // | for | | |
2754 // | target parallel | cancellation | |
2755 // | for | point | ! |
2756 // | target parallel | cancel | ! |
2757 // | for | | |
2758 // | target parallel | taskloop | * |
2759 // | for | | |
2760 // | target parallel | taskloop simd | * |
2761 // | for | | |
2762 // | target parallel | distribute | |
2763 // | for | | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002764 // | target parallel | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002765 // | for | parallel for | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002766 // | target parallel | distribute | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002767 // | for |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002768 // | target parallel | distribute simd | |
2769 // | for | | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002770 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002771 // | teams | parallel | * |
2772 // | teams | for | + |
2773 // | teams | for simd | + |
2774 // | teams | master | + |
2775 // | teams | critical | + |
2776 // | teams | simd | + |
2777 // | teams | sections | + |
2778 // | teams | section | + |
2779 // | teams | single | + |
2780 // | teams | parallel for | * |
2781 // | teams |parallel for simd| * |
2782 // | teams |parallel sections| * |
2783 // | teams | task | + |
2784 // | teams | taskyield | + |
2785 // | teams | barrier | + |
2786 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002787 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002788 // | teams | flush | + |
2789 // | teams | ordered | + |
2790 // | teams | atomic | + |
2791 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002792 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002793 // | teams | target parallel | + |
2794 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002795 // | teams | target enter | + |
2796 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002797 // | teams | target exit | + |
2798 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002799 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002800 // | teams | cancellation | |
2801 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002802 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002803 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002804 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002805 // | teams | distribute | ! |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002806 // | teams | distribute | ! |
2807 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002808 // | teams | distribute | ! |
2809 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002810 // | teams | distribute simd | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002811 // +------------------+-----------------+------------------------------------+
2812 // | taskloop | parallel | * |
2813 // | taskloop | for | + |
2814 // | taskloop | for simd | + |
2815 // | taskloop | master | + |
2816 // | taskloop | critical | * |
2817 // | taskloop | simd | * |
2818 // | taskloop | sections | + |
2819 // | taskloop | section | + |
2820 // | taskloop | single | + |
2821 // | taskloop | parallel for | * |
2822 // | taskloop |parallel for simd| * |
2823 // | taskloop |parallel sections| * |
2824 // | taskloop | task | * |
2825 // | taskloop | taskyield | * |
2826 // | taskloop | barrier | + |
2827 // | taskloop | taskwait | * |
2828 // | taskloop | taskgroup | * |
2829 // | taskloop | flush | * |
2830 // | taskloop | ordered | + |
2831 // | taskloop | atomic | * |
2832 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002833 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002834 // | taskloop | target parallel | * |
2835 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002836 // | taskloop | target enter | * |
2837 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002838 // | taskloop | target exit | * |
2839 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002840 // | taskloop | teams | + |
2841 // | taskloop | cancellation | |
2842 // | | point | |
2843 // | taskloop | cancel | |
2844 // | taskloop | taskloop | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002845 // | taskloop | distribute | + |
2846 // | taskloop | distribute | + |
2847 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002848 // | taskloop | distribute | + |
2849 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002850 // | taskloop | distribute simd | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002851 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002852 // | taskloop simd | parallel | |
2853 // | taskloop simd | for | |
2854 // | taskloop simd | for simd | |
2855 // | taskloop simd | master | |
2856 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002857 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002858 // | taskloop simd | sections | |
2859 // | taskloop simd | section | |
2860 // | taskloop simd | single | |
2861 // | taskloop simd | parallel for | |
2862 // | taskloop simd |parallel for simd| |
2863 // | taskloop simd |parallel sections| |
2864 // | taskloop simd | task | |
2865 // | taskloop simd | taskyield | |
2866 // | taskloop simd | barrier | |
2867 // | taskloop simd | taskwait | |
2868 // | taskloop simd | taskgroup | |
2869 // | taskloop simd | flush | |
2870 // | taskloop simd | ordered | + (with simd clause) |
2871 // | taskloop simd | atomic | |
2872 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002873 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002874 // | taskloop simd | target parallel | |
2875 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002876 // | taskloop simd | target enter | |
2877 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002878 // | taskloop simd | target exit | |
2879 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002880 // | taskloop simd | teams | |
2881 // | taskloop simd | cancellation | |
2882 // | | point | |
2883 // | taskloop simd | cancel | |
2884 // | taskloop simd | taskloop | |
2885 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002886 // | taskloop simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002887 // | taskloop simd | distribute | |
2888 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002889 // | taskloop simd | distribute | |
2890 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002891 // | taskloop simd | distribute simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002892 // +------------------+-----------------+------------------------------------+
2893 // | distribute | parallel | * |
2894 // | distribute | for | * |
2895 // | distribute | for simd | * |
2896 // | distribute | master | * |
2897 // | distribute | critical | * |
2898 // | distribute | simd | * |
2899 // | distribute | sections | * |
2900 // | distribute | section | * |
2901 // | distribute | single | * |
2902 // | distribute | parallel for | * |
2903 // | distribute |parallel for simd| * |
2904 // | distribute |parallel sections| * |
2905 // | distribute | task | * |
2906 // | distribute | taskyield | * |
2907 // | distribute | barrier | * |
2908 // | distribute | taskwait | * |
2909 // | distribute | taskgroup | * |
2910 // | distribute | flush | * |
2911 // | distribute | ordered | + |
2912 // | distribute | atomic | * |
2913 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002914 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002915 // | distribute | target parallel | |
2916 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002917 // | distribute | target enter | |
2918 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002919 // | distribute | target exit | |
2920 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002921 // | distribute | teams | |
2922 // | distribute | cancellation | + |
2923 // | | point | |
2924 // | distribute | cancel | + |
2925 // | distribute | taskloop | * |
2926 // | distribute | taskloop simd | * |
2927 // | distribute | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002928 // | distribute | distribute | |
2929 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002930 // | distribute | distribute | |
2931 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002932 // | distribute | distribute simd | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002933 // +------------------+-----------------+------------------------------------+
2934 // | distribute | parallel | * |
2935 // | parallel for | | |
2936 // | distribute | for | * |
2937 // | parallel for | | |
2938 // | distribute | for simd | * |
2939 // | parallel for | | |
2940 // | distribute | master | * |
2941 // | parallel for | | |
2942 // | distribute | critical | * |
2943 // | parallel for | | |
2944 // | distribute | simd | * |
2945 // | parallel for | | |
2946 // | distribute | sections | * |
2947 // | parallel for | | |
2948 // | distribute | section | * |
2949 // | parallel for | | |
2950 // | distribute | single | * |
2951 // | parallel for | | |
2952 // | distribute | parallel for | * |
2953 // | parallel for | | |
2954 // | distribute |parallel for simd| * |
2955 // | parallel for | | |
2956 // | distribute |parallel sections| * |
2957 // | parallel for | | |
2958 // | distribute | task | * |
2959 // | parallel for | | |
2960 // | parallel for | | |
2961 // | distribute | taskyield | * |
2962 // | parallel for | | |
2963 // | distribute | barrier | * |
2964 // | parallel for | | |
2965 // | distribute | taskwait | * |
2966 // | parallel for | | |
2967 // | distribute | taskgroup | * |
2968 // | parallel for | | |
2969 // | distribute | flush | * |
2970 // | parallel for | | |
2971 // | distribute | ordered | + |
2972 // | parallel for | | |
2973 // | distribute | atomic | * |
2974 // | parallel for | | |
2975 // | distribute | target | |
2976 // | parallel for | | |
2977 // | distribute | target parallel | |
2978 // | parallel for | | |
2979 // | distribute | target parallel | |
2980 // | parallel for | for | |
2981 // | distribute | target enter | |
2982 // | parallel for | data | |
2983 // | distribute | target exit | |
2984 // | parallel for | data | |
2985 // | distribute | teams | |
2986 // | parallel for | | |
2987 // | distribute | cancellation | + |
2988 // | parallel for | point | |
2989 // | distribute | cancel | + |
2990 // | parallel for | | |
2991 // | distribute | taskloop | * |
2992 // | parallel for | | |
2993 // | distribute | taskloop simd | * |
2994 // | parallel for | | |
2995 // | distribute | distribute | |
2996 // | parallel for | | |
2997 // | distribute | distribute | |
2998 // | parallel for | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002999 // | distribute | distribute | |
3000 // | parallel for |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00003001 // | distribute | distribute simd | |
3002 // | parallel for | | |
Kelvin Li4a39add2016-07-05 05:00:15 +00003003 // +------------------+-----------------+------------------------------------+
3004 // | distribute | parallel | * |
3005 // | parallel for simd| | |
3006 // | distribute | for | * |
3007 // | parallel for simd| | |
3008 // | distribute | for simd | * |
3009 // | parallel for simd| | |
3010 // | distribute | master | * |
3011 // | parallel for simd| | |
3012 // | distribute | critical | * |
3013 // | parallel for simd| | |
3014 // | distribute | simd | * |
3015 // | parallel for simd| | |
3016 // | distribute | sections | * |
3017 // | parallel for simd| | |
3018 // | distribute | section | * |
3019 // | parallel for simd| | |
3020 // | distribute | single | * |
3021 // | parallel for simd| | |
3022 // | distribute | parallel for | * |
3023 // | parallel for simd| | |
3024 // | distribute |parallel for simd| * |
3025 // | parallel for simd| | |
3026 // | distribute |parallel sections| * |
3027 // | parallel for simd| | |
3028 // | distribute | task | * |
3029 // | parallel for simd| | |
3030 // | distribute | taskyield | * |
3031 // | parallel for simd| | |
3032 // | distribute | barrier | * |
3033 // | parallel for simd| | |
3034 // | distribute | taskwait | * |
3035 // | parallel for simd| | |
3036 // | distribute | taskgroup | * |
3037 // | parallel for simd| | |
3038 // | distribute | flush | * |
3039 // | parallel for simd| | |
3040 // | distribute | ordered | + |
3041 // | parallel for simd| | |
3042 // | distribute | atomic | * |
3043 // | parallel for simd| | |
3044 // | distribute | target | |
3045 // | parallel for simd| | |
3046 // | distribute | target parallel | |
3047 // | parallel for simd| | |
3048 // | distribute | target parallel | |
3049 // | parallel for simd| for | |
3050 // | distribute | target enter | |
3051 // | parallel for simd| data | |
3052 // | distribute | target exit | |
3053 // | parallel for simd| data | |
3054 // | distribute | teams | |
3055 // | parallel for simd| | |
3056 // | distribute | cancellation | + |
3057 // | parallel for simd| point | |
3058 // | distribute | cancel | + |
3059 // | parallel for simd| | |
3060 // | distribute | taskloop | * |
3061 // | parallel for simd| | |
3062 // | distribute | taskloop simd | * |
3063 // | parallel for simd| | |
3064 // | distribute | distribute | |
3065 // | parallel for simd| | |
3066 // | distribute | distribute | * |
3067 // | parallel for simd| parallel for | |
3068 // | distribute | distribute | * |
3069 // | parallel for simd|parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00003070 // | distribute | distribute simd | * |
3071 // | parallel for simd| | |
3072 // +------------------+-----------------+------------------------------------+
3073 // | distribute simd | parallel | * |
3074 // | distribute simd | for | * |
3075 // | distribute simd | for simd | * |
3076 // | distribute simd | master | * |
3077 // | distribute simd | critical | * |
3078 // | distribute simd | simd | * |
3079 // | distribute simd | sections | * |
3080 // | distribute simd | section | * |
3081 // | distribute simd | single | * |
3082 // | distribute simd | parallel for | * |
3083 // | distribute simd |parallel for simd| * |
3084 // | distribute simd |parallel sections| * |
3085 // | distribute simd | task | * |
3086 // | distribute simd | taskyield | * |
3087 // | distribute simd | barrier | * |
3088 // | distribute simd | taskwait | * |
3089 // | distribute simd | taskgroup | * |
3090 // | distribute simd | flush | * |
3091 // | distribute simd | ordered | + |
3092 // | distribute simd | atomic | * |
3093 // | distribute simd | target | * |
3094 // | distribute simd | target parallel | * |
3095 // | distribute simd | target parallel | * |
3096 // | | for | |
3097 // | distribute simd | target enter | * |
3098 // | | data | |
3099 // | distribute simd | target exit | * |
3100 // | | data | |
3101 // | distribute simd | teams | * |
3102 // | distribute simd | cancellation | + |
3103 // | | point | |
3104 // | distribute simd | cancel | + |
3105 // | distribute simd | taskloop | * |
3106 // | distribute simd | taskloop simd | * |
3107 // | distribute simd | distribute | |
3108 // | distribute simd | distribute | * |
3109 // | | parallel for | |
3110 // | distribute simd | distribute | * |
3111 // | |parallel for simd| |
3112 // | distribute simd | distribute simd | * |
3113 // | | | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003114 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00003115 if (Stack->getCurScope()) {
3116 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003117 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003118 bool NestingProhibited = false;
3119 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003120 enum {
3121 NoRecommend,
3122 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00003123 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003124 ShouldBeInTargetRegion,
3125 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003126 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00003127 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003128 // OpenMP [2.16, Nesting of Regions]
3129 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003130 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00003131 // An ordered construct with the simd clause is the only OpenMP
3132 // construct that can appear in the simd region.
3133 // Allowing a SIMD consruct nested in another SIMD construct is an
3134 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3135 // message.
3136 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3137 ? diag::err_omp_prohibited_region_simd
3138 : diag::warn_omp_nesting_simd);
3139 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00003140 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003141 if (ParentRegion == OMPD_atomic) {
3142 // OpenMP [2.16, Nesting of Regions]
3143 // OpenMP constructs may not be nested inside an atomic region.
3144 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3145 return true;
3146 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003147 if (CurrentRegion == OMPD_section) {
3148 // OpenMP [2.7.2, sections Construct, Restrictions]
3149 // Orphaned section directives are prohibited. That is, the section
3150 // directives must appear within the sections construct and must not be
3151 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003152 if (ParentRegion != OMPD_sections &&
3153 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003154 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3155 << (ParentRegion != OMPD_unknown)
3156 << getOpenMPDirectiveName(ParentRegion);
3157 return true;
3158 }
3159 return false;
3160 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003161 // Allow some constructs to be orphaned (they could be used in functions,
3162 // called from OpenMP regions with the required preconditions).
3163 if (ParentRegion == OMPD_unknown)
3164 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00003165 if (CurrentRegion == OMPD_cancellation_point ||
3166 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003167 // OpenMP [2.16, Nesting of Regions]
3168 // A cancellation point construct for which construct-type-clause is
3169 // taskgroup must be nested inside a task construct. A cancellation
3170 // point construct for which construct-type-clause is not taskgroup must
3171 // be closely nested inside an OpenMP construct that matches the type
3172 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00003173 // A cancel construct for which construct-type-clause is taskgroup must be
3174 // nested inside a task construct. A cancel construct for which
3175 // construct-type-clause is not taskgroup must be closely nested inside an
3176 // OpenMP construct that matches the type specified in
3177 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003178 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003179 !((CancelRegion == OMPD_parallel &&
3180 (ParentRegion == OMPD_parallel ||
3181 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00003182 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003183 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
3184 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003185 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3186 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00003187 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3188 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003189 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00003190 // OpenMP [2.16, Nesting of Regions]
3191 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003192 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00003193 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003194 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003195 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3196 // OpenMP [2.16, Nesting of Regions]
3197 // A critical region may not be nested (closely or otherwise) inside a
3198 // critical region with the same name. Note that this restriction is not
3199 // sufficient to prevent deadlock.
3200 SourceLocation PreviousCriticalLoc;
3201 bool DeadLock =
3202 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
3203 OpenMPDirectiveKind K,
3204 const DeclarationNameInfo &DNI,
3205 SourceLocation Loc)
3206 ->bool {
3207 if (K == OMPD_critical &&
3208 DNI.getName() == CurrentName.getName()) {
3209 PreviousCriticalLoc = Loc;
3210 return true;
3211 } else
3212 return false;
3213 },
3214 false /* skip top directive */);
3215 if (DeadLock) {
3216 SemaRef.Diag(StartLoc,
3217 diag::err_omp_prohibited_region_critical_same_name)
3218 << CurrentName.getName();
3219 if (PreviousCriticalLoc.isValid())
3220 SemaRef.Diag(PreviousCriticalLoc,
3221 diag::note_omp_previous_critical_region);
3222 return true;
3223 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003224 } else if (CurrentRegion == OMPD_barrier) {
3225 // OpenMP [2.16, Nesting of Regions]
3226 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003227 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003228 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3229 isOpenMPTaskingDirective(ParentRegion) ||
3230 ParentRegion == OMPD_master ||
3231 ParentRegion == OMPD_critical ||
3232 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00003233 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00003234 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003235 // OpenMP [2.16, Nesting of Regions]
3236 // A worksharing region may not be closely nested inside a worksharing,
3237 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003238 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3239 isOpenMPTaskingDirective(ParentRegion) ||
3240 ParentRegion == OMPD_master ||
3241 ParentRegion == OMPD_critical ||
3242 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003243 Recommend = ShouldBeInParallelRegion;
3244 } else if (CurrentRegion == OMPD_ordered) {
3245 // OpenMP [2.16, Nesting of Regions]
3246 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00003247 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003248 // An ordered region must be closely nested inside a loop region (or
3249 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003250 // OpenMP [2.8.1,simd Construct, Restrictions]
3251 // An ordered construct with the simd clause is the only OpenMP construct
3252 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003253 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003254 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003255 !(isOpenMPSimdDirective(ParentRegion) ||
3256 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003257 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003258 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
3259 // OpenMP [2.16, Nesting of Regions]
3260 // If specified, a teams construct must be contained within a target
3261 // construct.
3262 NestingProhibited = ParentRegion != OMPD_target;
3263 Recommend = ShouldBeInTargetRegion;
3264 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
3265 }
3266 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
3267 // OpenMP [2.16, Nesting of Regions]
3268 // distribute, parallel, parallel sections, parallel workshare, and the
3269 // parallel loop and parallel loop SIMD constructs are the only OpenMP
3270 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003271 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3272 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00003273 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003274 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003275 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
3276 // OpenMP 4.5 [2.17 Nesting of Regions]
3277 // The region associated with the distribute construct must be strictly
3278 // nested inside a teams region
3279 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
3280 Recommend = ShouldBeInTeamsRegion;
3281 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003282 if (!NestingProhibited &&
3283 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3284 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3285 // OpenMP 4.5 [2.17 Nesting of Regions]
3286 // If a target, target update, target data, target enter data, or
3287 // target exit data construct is encountered during execution of a
3288 // target region, the behavior is unspecified.
3289 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00003290 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
3291 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003292 if (isOpenMPTargetExecutionDirective(K)) {
3293 OffendingRegion = K;
3294 return true;
3295 } else
3296 return false;
3297 },
3298 false /* don't skip top directive */);
3299 CloseNesting = false;
3300 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003301 if (NestingProhibited) {
3302 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003303 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3304 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00003305 return true;
3306 }
3307 }
3308 return false;
3309}
3310
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003311static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3312 ArrayRef<OMPClause *> Clauses,
3313 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3314 bool ErrorFound = false;
3315 unsigned NamedModifiersNumber = 0;
3316 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3317 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003318 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003319 for (const auto *C : Clauses) {
3320 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3321 // At most one if clause without a directive-name-modifier can appear on
3322 // the directive.
3323 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3324 if (FoundNameModifiers[CurNM]) {
3325 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
3326 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3327 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3328 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003329 } else if (CurNM != OMPD_unknown) {
3330 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003331 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003332 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003333 FoundNameModifiers[CurNM] = IC;
3334 if (CurNM == OMPD_unknown)
3335 continue;
3336 // Check if the specified name modifier is allowed for the current
3337 // directive.
3338 // At most one if clause with the particular directive-name-modifier can
3339 // appear on the directive.
3340 bool MatchFound = false;
3341 for (auto NM : AllowedNameModifiers) {
3342 if (CurNM == NM) {
3343 MatchFound = true;
3344 break;
3345 }
3346 }
3347 if (!MatchFound) {
3348 S.Diag(IC->getNameModifierLoc(),
3349 diag::err_omp_wrong_if_directive_name_modifier)
3350 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3351 ErrorFound = true;
3352 }
3353 }
3354 }
3355 // If any if clause on the directive includes a directive-name-modifier then
3356 // all if clauses on the directive must include a directive-name-modifier.
3357 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3358 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3359 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
3360 diag::err_omp_no_more_if_clause);
3361 } else {
3362 std::string Values;
3363 std::string Sep(", ");
3364 unsigned AllowedCnt = 0;
3365 unsigned TotalAllowedNum =
3366 AllowedNameModifiers.size() - NamedModifiersNumber;
3367 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3368 ++Cnt) {
3369 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3370 if (!FoundNameModifiers[NM]) {
3371 Values += "'";
3372 Values += getOpenMPDirectiveName(NM);
3373 Values += "'";
3374 if (AllowedCnt + 2 == TotalAllowedNum)
3375 Values += " or ";
3376 else if (AllowedCnt + 1 != TotalAllowedNum)
3377 Values += Sep;
3378 ++AllowedCnt;
3379 }
3380 }
3381 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
3382 diag::err_omp_unnamed_if_clause)
3383 << (TotalAllowedNum > 1) << Values;
3384 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00003385 for (auto Loc : NameModifierLoc) {
3386 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3387 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003388 ErrorFound = true;
3389 }
3390 return ErrorFound;
3391}
3392
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003393StmtResult Sema::ActOnOpenMPExecutableDirective(
3394 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3395 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3396 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003397 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003398 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3399 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003400 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003401
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003402 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003403 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003404 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003405 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00003406 if (AStmt) {
3407 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3408
3409 // Check default data sharing attributes for referenced variables.
3410 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3411 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
3412 if (DSAChecker.isErrorFound())
3413 return StmtError();
3414 // Generate list of implicitly defined firstprivate variables.
3415 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003416
3417 if (!DSAChecker.getImplicitFirstprivate().empty()) {
3418 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3419 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
3420 SourceLocation(), SourceLocation())) {
3421 ClausesWithImplicit.push_back(Implicit);
3422 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3423 DSAChecker.getImplicitFirstprivate().size();
3424 } else
3425 ErrorFound = true;
3426 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003427 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003428
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003429 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003430 switch (Kind) {
3431 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003432 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3433 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003434 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003435 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003436 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003437 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3438 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003439 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003440 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003441 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3442 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003443 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003444 case OMPD_for_simd:
3445 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3446 EndLoc, VarsWithInheritedDSA);
3447 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003448 case OMPD_sections:
3449 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3450 EndLoc);
3451 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003452 case OMPD_section:
3453 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003454 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003455 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3456 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003457 case OMPD_single:
3458 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3459 EndLoc);
3460 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003461 case OMPD_master:
3462 assert(ClausesWithImplicit.empty() &&
3463 "No clauses are allowed for 'omp master' directive");
3464 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3465 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003466 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003467 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3468 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003469 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003470 case OMPD_parallel_for:
3471 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3472 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003473 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003474 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003475 case OMPD_parallel_for_simd:
3476 Res = ActOnOpenMPParallelForSimdDirective(
3477 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003478 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003479 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003480 case OMPD_parallel_sections:
3481 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3482 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003483 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003484 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003485 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003486 Res =
3487 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003488 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003489 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003490 case OMPD_taskyield:
3491 assert(ClausesWithImplicit.empty() &&
3492 "No clauses are allowed for 'omp taskyield' directive");
3493 assert(AStmt == nullptr &&
3494 "No associated statement allowed for 'omp taskyield' directive");
3495 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3496 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003497 case OMPD_barrier:
3498 assert(ClausesWithImplicit.empty() &&
3499 "No clauses are allowed for 'omp barrier' directive");
3500 assert(AStmt == nullptr &&
3501 "No associated statement allowed for 'omp barrier' directive");
3502 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3503 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003504 case OMPD_taskwait:
3505 assert(ClausesWithImplicit.empty() &&
3506 "No clauses are allowed for 'omp taskwait' directive");
3507 assert(AStmt == nullptr &&
3508 "No associated statement allowed for 'omp taskwait' directive");
3509 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3510 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003511 case OMPD_taskgroup:
3512 assert(ClausesWithImplicit.empty() &&
3513 "No clauses are allowed for 'omp taskgroup' directive");
3514 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3515 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003516 case OMPD_flush:
3517 assert(AStmt == nullptr &&
3518 "No associated statement allowed for 'omp flush' directive");
3519 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3520 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003521 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003522 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3523 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003524 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003525 case OMPD_atomic:
3526 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3527 EndLoc);
3528 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003529 case OMPD_teams:
3530 Res =
3531 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3532 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003533 case OMPD_target:
3534 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3535 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003536 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003537 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003538 case OMPD_target_parallel:
3539 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3540 StartLoc, EndLoc);
3541 AllowedNameModifiers.push_back(OMPD_target);
3542 AllowedNameModifiers.push_back(OMPD_parallel);
3543 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003544 case OMPD_target_parallel_for:
3545 Res = ActOnOpenMPTargetParallelForDirective(
3546 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3547 AllowedNameModifiers.push_back(OMPD_target);
3548 AllowedNameModifiers.push_back(OMPD_parallel);
3549 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003550 case OMPD_cancellation_point:
3551 assert(ClausesWithImplicit.empty() &&
3552 "No clauses are allowed for 'omp cancellation point' directive");
3553 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3554 "cancellation point' directive");
3555 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3556 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003557 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003558 assert(AStmt == nullptr &&
3559 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003560 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3561 CancelRegion);
3562 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003563 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003564 case OMPD_target_data:
3565 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3566 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003567 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003568 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003569 case OMPD_target_enter_data:
3570 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3571 EndLoc);
3572 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3573 break;
Samuel Antao72590762016-01-19 20:04:50 +00003574 case OMPD_target_exit_data:
3575 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3576 EndLoc);
3577 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3578 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003579 case OMPD_taskloop:
3580 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3581 EndLoc, VarsWithInheritedDSA);
3582 AllowedNameModifiers.push_back(OMPD_taskloop);
3583 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003584 case OMPD_taskloop_simd:
3585 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3586 EndLoc, VarsWithInheritedDSA);
3587 AllowedNameModifiers.push_back(OMPD_taskloop);
3588 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003589 case OMPD_distribute:
3590 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3591 EndLoc, VarsWithInheritedDSA);
3592 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003593 case OMPD_target_update:
3594 assert(!AStmt && "Statement is not allowed for target update");
3595 Res =
3596 ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, EndLoc);
3597 AllowedNameModifiers.push_back(OMPD_target_update);
3598 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003599 case OMPD_distribute_parallel_for:
3600 Res = ActOnOpenMPDistributeParallelForDirective(
3601 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3602 AllowedNameModifiers.push_back(OMPD_parallel);
3603 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003604 case OMPD_distribute_parallel_for_simd:
3605 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3606 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3607 AllowedNameModifiers.push_back(OMPD_parallel);
3608 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003609 case OMPD_distribute_simd:
3610 Res = ActOnOpenMPDistributeSimdDirective(
3611 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3612 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003613 case OMPD_declare_target:
3614 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003615 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003616 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003617 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003618 llvm_unreachable("OpenMP Directive is not allowed");
3619 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003620 llvm_unreachable("Unknown OpenMP directive");
3621 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003622
Alexey Bataev4acb8592014-07-07 13:01:15 +00003623 for (auto P : VarsWithInheritedDSA) {
3624 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3625 << P.first << P.second->getSourceRange();
3626 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003627 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3628
3629 if (!AllowedNameModifiers.empty())
3630 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3631 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003632
Alexey Bataeved09d242014-05-28 05:53:51 +00003633 if (ErrorFound)
3634 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003635 return Res;
3636}
3637
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003638Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3639 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003640 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003641 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3642 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003643 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003644 assert(Linears.size() == LinModifiers.size());
3645 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003646 if (!DG || DG.get().isNull())
3647 return DeclGroupPtrTy();
3648
3649 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003650 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003651 return DG;
3652 }
3653 auto *ADecl = DG.get().getSingleDecl();
3654 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3655 ADecl = FTD->getTemplatedDecl();
3656
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003657 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3658 if (!FD) {
3659 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003660 return DeclGroupPtrTy();
3661 }
3662
Alexey Bataev2af33e32016-04-07 12:45:37 +00003663 // OpenMP [2.8.2, declare simd construct, Description]
3664 // The parameter of the simdlen clause must be a constant positive integer
3665 // expression.
3666 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003667 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003668 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003669 // OpenMP [2.8.2, declare simd construct, Description]
3670 // The special this pointer can be used as if was one of the arguments to the
3671 // function in any of the linear, aligned, or uniform clauses.
3672 // The uniform clause declares one or more arguments to have an invariant
3673 // value for all concurrent invocations of the function in the execution of a
3674 // single SIMD loop.
Alexey Bataevecba70f2016-04-12 11:02:11 +00003675 llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3676 Expr *UniformedLinearThis = nullptr;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003677 for (auto *E : Uniforms) {
3678 E = E->IgnoreParenImpCasts();
3679 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3680 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3681 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3682 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003683 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3684 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003685 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003686 }
3687 if (isa<CXXThisExpr>(E)) {
3688 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003689 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003690 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003691 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3692 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003693 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003694 // OpenMP [2.8.2, declare simd construct, Description]
3695 // The aligned clause declares that the object to which each list item points
3696 // is aligned to the number of bytes expressed in the optional parameter of
3697 // the aligned clause.
3698 // The special this pointer can be used as if was one of the arguments to the
3699 // function in any of the linear, aligned, or uniform clauses.
3700 // The type of list items appearing in the aligned clause must be array,
3701 // pointer, reference to array, or reference to pointer.
3702 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3703 Expr *AlignedThis = nullptr;
3704 for (auto *E : Aligneds) {
3705 E = E->IgnoreParenImpCasts();
3706 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3707 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3708 auto *CanonPVD = PVD->getCanonicalDecl();
3709 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3710 FD->getParamDecl(PVD->getFunctionScopeIndex())
3711 ->getCanonicalDecl() == CanonPVD) {
3712 // OpenMP [2.8.1, simd construct, Restrictions]
3713 // A list-item cannot appear in more than one aligned clause.
3714 if (AlignedArgs.count(CanonPVD) > 0) {
3715 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3716 << 1 << E->getSourceRange();
3717 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3718 diag::note_omp_explicit_dsa)
3719 << getOpenMPClauseName(OMPC_aligned);
3720 continue;
3721 }
3722 AlignedArgs[CanonPVD] = E;
3723 QualType QTy = PVD->getType()
3724 .getNonReferenceType()
3725 .getUnqualifiedType()
3726 .getCanonicalType();
3727 const Type *Ty = QTy.getTypePtrOrNull();
3728 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3729 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3730 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3731 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3732 }
3733 continue;
3734 }
3735 }
3736 if (isa<CXXThisExpr>(E)) {
3737 if (AlignedThis) {
3738 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3739 << 2 << E->getSourceRange();
3740 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3741 << getOpenMPClauseName(OMPC_aligned);
3742 }
3743 AlignedThis = E;
3744 continue;
3745 }
3746 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3747 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3748 }
3749 // The optional parameter of the aligned clause, alignment, must be a constant
3750 // positive integer expression. If no optional parameter is specified,
3751 // implementation-defined default alignments for SIMD instructions on the
3752 // target platforms are assumed.
3753 SmallVector<Expr *, 4> NewAligns;
3754 for (auto *E : Alignments) {
3755 ExprResult Align;
3756 if (E)
3757 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3758 NewAligns.push_back(Align.get());
3759 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003760 // OpenMP [2.8.2, declare simd construct, Description]
3761 // The linear clause declares one or more list items to be private to a SIMD
3762 // lane and to have a linear relationship with respect to the iteration space
3763 // of a loop.
3764 // The special this pointer can be used as if was one of the arguments to the
3765 // function in any of the linear, aligned, or uniform clauses.
3766 // When a linear-step expression is specified in a linear clause it must be
3767 // either a constant integer expression or an integer-typed parameter that is
3768 // specified in a uniform clause on the directive.
3769 llvm::DenseMap<Decl *, Expr *> LinearArgs;
3770 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3771 auto MI = LinModifiers.begin();
3772 for (auto *E : Linears) {
3773 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3774 ++MI;
3775 E = E->IgnoreParenImpCasts();
3776 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3777 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3778 auto *CanonPVD = PVD->getCanonicalDecl();
3779 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3780 FD->getParamDecl(PVD->getFunctionScopeIndex())
3781 ->getCanonicalDecl() == CanonPVD) {
3782 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3783 // A list-item cannot appear in more than one linear clause.
3784 if (LinearArgs.count(CanonPVD) > 0) {
3785 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3786 << getOpenMPClauseName(OMPC_linear)
3787 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3788 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3789 diag::note_omp_explicit_dsa)
3790 << getOpenMPClauseName(OMPC_linear);
3791 continue;
3792 }
3793 // Each argument can appear in at most one uniform or linear clause.
3794 if (UniformedArgs.count(CanonPVD) > 0) {
3795 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3796 << getOpenMPClauseName(OMPC_linear)
3797 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3798 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3799 diag::note_omp_explicit_dsa)
3800 << getOpenMPClauseName(OMPC_uniform);
3801 continue;
3802 }
3803 LinearArgs[CanonPVD] = E;
3804 if (E->isValueDependent() || E->isTypeDependent() ||
3805 E->isInstantiationDependent() ||
3806 E->containsUnexpandedParameterPack())
3807 continue;
3808 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3809 PVD->getOriginalType());
3810 continue;
3811 }
3812 }
3813 if (isa<CXXThisExpr>(E)) {
3814 if (UniformedLinearThis) {
3815 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3816 << getOpenMPClauseName(OMPC_linear)
3817 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3818 << E->getSourceRange();
3819 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3820 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3821 : OMPC_linear);
3822 continue;
3823 }
3824 UniformedLinearThis = E;
3825 if (E->isValueDependent() || E->isTypeDependent() ||
3826 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3827 continue;
3828 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3829 E->getType());
3830 continue;
3831 }
3832 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3833 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3834 }
3835 Expr *Step = nullptr;
3836 Expr *NewStep = nullptr;
3837 SmallVector<Expr *, 4> NewSteps;
3838 for (auto *E : Steps) {
3839 // Skip the same step expression, it was checked already.
3840 if (Step == E || !E) {
3841 NewSteps.push_back(E ? NewStep : nullptr);
3842 continue;
3843 }
3844 Step = E;
3845 if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
3846 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3847 auto *CanonPVD = PVD->getCanonicalDecl();
3848 if (UniformedArgs.count(CanonPVD) == 0) {
3849 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3850 << Step->getSourceRange();
3851 } else if (E->isValueDependent() || E->isTypeDependent() ||
3852 E->isInstantiationDependent() ||
3853 E->containsUnexpandedParameterPack() ||
3854 CanonPVD->getType()->hasIntegerRepresentation())
3855 NewSteps.push_back(Step);
3856 else {
3857 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3858 << Step->getSourceRange();
3859 }
3860 continue;
3861 }
3862 NewStep = Step;
3863 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3864 !Step->isInstantiationDependent() &&
3865 !Step->containsUnexpandedParameterPack()) {
3866 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3867 .get();
3868 if (NewStep)
3869 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3870 }
3871 NewSteps.push_back(NewStep);
3872 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003873 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3874 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003875 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003876 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3877 const_cast<Expr **>(Linears.data()), Linears.size(),
3878 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3879 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003880 ADecl->addAttr(NewAttr);
3881 return ConvertDeclToDeclGroup(ADecl);
3882}
3883
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003884StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3885 Stmt *AStmt,
3886 SourceLocation StartLoc,
3887 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003888 if (!AStmt)
3889 return StmtError();
3890
Alexey Bataev9959db52014-05-06 10:08:46 +00003891 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3892 // 1.2.2 OpenMP Language Terminology
3893 // Structured block - An executable statement with a single entry at the
3894 // top and a single exit at the bottom.
3895 // The point of exit cannot be a branch out of the structured block.
3896 // longjmp() and throw() must not violate the entry/exit criteria.
3897 CS->getCapturedDecl()->setNothrow();
3898
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003899 getCurFunction()->setHasBranchProtectedScope();
3900
Alexey Bataev25e5b442015-09-15 12:52:43 +00003901 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3902 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003903}
3904
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003905namespace {
3906/// \brief Helper class for checking canonical form of the OpenMP loops and
3907/// extracting iteration space of each loop in the loop nest, that will be used
3908/// for IR generation.
3909class OpenMPIterationSpaceChecker {
3910 /// \brief Reference to Sema.
3911 Sema &SemaRef;
3912 /// \brief A location for diagnostics (when there is no some better location).
3913 SourceLocation DefaultLoc;
3914 /// \brief A location for diagnostics (when increment is not compatible).
3915 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003916 /// \brief A source location for referring to loop init later.
3917 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003918 /// \brief A source location for referring to condition later.
3919 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003920 /// \brief A source location for referring to increment later.
3921 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003922 /// \brief Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003923 ValueDecl *LCDecl = nullptr;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003924 /// \brief Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003925 Expr *LCRef = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003926 /// \brief Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003927 Expr *LB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003928 /// \brief Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003929 Expr *UB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003930 /// \brief Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003931 Expr *Step = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003932 /// \brief This flag is true when condition is one of:
3933 /// Var < UB
3934 /// Var <= UB
3935 /// UB > Var
3936 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003937 bool TestIsLessOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003938 /// \brief This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003939 bool TestIsStrictOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003940 /// \brief This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003941 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003942
3943public:
3944 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003945 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003946 /// \brief Check init-expr for canonical loop form and save loop counter
3947 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003948 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003949 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3950 /// for less/greater and for strict/non-strict comparison.
3951 bool CheckCond(Expr *S);
3952 /// \brief Check incr-expr for canonical loop form and return true if it
3953 /// does not conform, otherwise save loop step (#Step).
3954 bool CheckInc(Expr *S);
3955 /// \brief Return the loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003956 ValueDecl *GetLoopDecl() const { return LCDecl; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003957 /// \brief Return the reference expression to loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003958 Expr *GetLoopDeclRefExpr() const { return LCRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003959 /// \brief Source range of the loop init.
3960 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3961 /// \brief Source range of the loop condition.
3962 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3963 /// \brief Source range of the loop increment.
3964 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3965 /// \brief True if the step should be subtracted.
3966 bool ShouldSubtractStep() const { return SubtractStep; }
3967 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003968 Expr *
3969 BuildNumIterations(Scope *S, const bool LimitedType,
3970 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003971 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003972 Expr *BuildPreCond(Scope *S, Expr *Cond,
3973 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003974 /// \brief Build reference expression to the counter be used for codegen.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00003975 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
3976 DSAStackTy &DSA) const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003977 /// \brief Build reference expression to the private counter be used for
3978 /// codegen.
3979 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003980 /// \brief Build initization of the counter be used for codegen.
3981 Expr *BuildCounterInit() const;
3982 /// \brief Build step of the counter be used for codegen.
3983 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003984 /// \brief Return true if any expression is dependent.
3985 bool Dependent() const;
3986
3987private:
3988 /// \brief Check the right-hand side of an assignment in the increment
3989 /// expression.
3990 bool CheckIncRHS(Expr *RHS);
3991 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003992 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003993 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003994 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003995 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003996 /// \brief Helper to set loop increment.
3997 bool SetStep(Expr *NewStep, bool Subtract);
3998};
3999
4000bool OpenMPIterationSpaceChecker::Dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004001 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004002 assert(!LB && !UB && !Step);
4003 return false;
4004 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004005 return LCDecl->getType()->isDependentType() ||
4006 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
4007 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004008}
4009
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004010static Expr *getExprAsWritten(Expr *E) {
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004011 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
4012 E = ExprTemp->getSubExpr();
4013
4014 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
4015 E = MTE->GetTemporaryExpr();
4016
4017 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
4018 E = Binder->getSubExpr();
4019
4020 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
4021 E = ICE->getSubExprAsWritten();
4022 return E->IgnoreParens();
4023}
4024
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004025bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
4026 Expr *NewLCRefExpr,
4027 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004028 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004029 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004030 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004031 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004032 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004033 LCDecl = getCanonicalDecl(NewLCDecl);
4034 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004035 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
4036 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004037 if ((Ctor->isCopyOrMoveConstructor() ||
4038 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4039 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004040 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004041 LB = NewLB;
4042 return false;
4043}
4044
4045bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00004046 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004047 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004048 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
4049 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004050 if (!NewUB)
4051 return true;
4052 UB = NewUB;
4053 TestIsLessOp = LessOp;
4054 TestIsStrictOp = StrictOp;
4055 ConditionSrcRange = SR;
4056 ConditionLoc = SL;
4057 return false;
4058}
4059
4060bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
4061 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004062 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004063 if (!NewStep)
4064 return true;
4065 if (!NewStep->isValueDependent()) {
4066 // Check that the step is integer expression.
4067 SourceLocation StepLoc = NewStep->getLocStart();
4068 ExprResult Val =
4069 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
4070 if (Val.isInvalid())
4071 return true;
4072 NewStep = Val.get();
4073
4074 // OpenMP [2.6, Canonical Loop Form, Restrictions]
4075 // If test-expr is of form var relational-op b and relational-op is < or
4076 // <= then incr-expr must cause var to increase on each iteration of the
4077 // loop. If test-expr is of form var relational-op b and relational-op is
4078 // > or >= then incr-expr must cause var to decrease on each iteration of
4079 // the loop.
4080 // If test-expr is of form b relational-op var and relational-op is < or
4081 // <= then incr-expr must cause var to decrease on each iteration of the
4082 // loop. If test-expr is of form b relational-op var and relational-op is
4083 // > or >= then incr-expr must cause var to increase on each iteration of
4084 // the loop.
4085 llvm::APSInt Result;
4086 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
4087 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
4088 bool IsConstNeg =
4089 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004090 bool IsConstPos =
4091 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004092 bool IsConstZero = IsConstant && !Result.getBoolValue();
4093 if (UB && (IsConstZero ||
4094 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00004095 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004096 SemaRef.Diag(NewStep->getExprLoc(),
4097 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004098 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004099 SemaRef.Diag(ConditionLoc,
4100 diag::note_omp_loop_cond_requres_compatible_incr)
4101 << TestIsLessOp << ConditionSrcRange;
4102 return true;
4103 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004104 if (TestIsLessOp == Subtract) {
4105 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
4106 NewStep).get();
4107 Subtract = !Subtract;
4108 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004109 }
4110
4111 Step = NewStep;
4112 SubtractStep = Subtract;
4113 return false;
4114}
4115
Alexey Bataev9c821032015-04-30 04:23:23 +00004116bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004117 // Check init-expr for canonical loop form and save loop counter
4118 // variable - #Var and its initialization value - #LB.
4119 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
4120 // var = lb
4121 // integer-type var = lb
4122 // random-access-iterator-type var = lb
4123 // pointer-type var = lb
4124 //
4125 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00004126 if (EmitDiags) {
4127 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
4128 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004129 return true;
4130 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004131 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4132 if (!ExprTemp->cleanupsHaveSideEffects())
4133 S = ExprTemp->getSubExpr();
4134
Alexander Musmana5f070a2014-10-01 06:03:56 +00004135 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004136 if (Expr *E = dyn_cast<Expr>(S))
4137 S = E->IgnoreParens();
4138 if (auto BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004139 if (BO->getOpcode() == BO_Assign) {
4140 auto *LHS = BO->getLHS()->IgnoreParens();
4141 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4142 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4143 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4144 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4145 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
4146 }
4147 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4148 if (ME->isArrow() &&
4149 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4150 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4151 }
4152 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004153 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
4154 if (DS->isSingleDecl()) {
4155 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00004156 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004157 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00004158 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004159 SemaRef.Diag(S->getLocStart(),
4160 diag::ext_omp_loop_not_canonical_init)
4161 << S->getSourceRange();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004162 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004163 }
4164 }
4165 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004166 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4167 if (CE->getOperator() == OO_Equal) {
4168 auto *LHS = CE->getArg(0);
4169 if (auto DRE = dyn_cast<DeclRefExpr>(LHS)) {
4170 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4171 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4172 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4173 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
4174 }
4175 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4176 if (ME->isArrow() &&
4177 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4178 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4179 }
4180 }
4181 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004182
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004183 if (Dependent() || SemaRef.CurContext->isDependentContext())
4184 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00004185 if (EmitDiags) {
4186 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
4187 << S->getSourceRange();
4188 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004189 return true;
4190}
4191
Alexey Bataev23b69422014-06-18 07:08:49 +00004192/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004193/// variable (which may be the loop variable) if possible.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004194static const ValueDecl *GetInitLCDecl(Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004195 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00004196 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004197 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004198 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
4199 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004200 if ((Ctor->isCopyOrMoveConstructor() ||
4201 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4202 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004203 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004204 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4205 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
4206 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
4207 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4208 return getCanonicalDecl(ME->getMemberDecl());
4209 return getCanonicalDecl(VD);
4210 }
4211 }
4212 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
4213 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4214 return getCanonicalDecl(ME->getMemberDecl());
4215 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004216}
4217
4218bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
4219 // Check test-expr for canonical form, save upper-bound UB, flags for
4220 // less/greater and for strict/non-strict comparison.
4221 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4222 // var relational-op b
4223 // b relational-op var
4224 //
4225 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004226 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004227 return true;
4228 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004229 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004230 SourceLocation CondLoc = S->getLocStart();
4231 if (auto BO = dyn_cast<BinaryOperator>(S)) {
4232 if (BO->isRelationalOp()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004233 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004234 return SetUB(BO->getRHS(),
4235 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4236 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4237 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004238 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004239 return SetUB(BO->getLHS(),
4240 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4241 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4242 BO->getSourceRange(), BO->getOperatorLoc());
4243 }
4244 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4245 if (CE->getNumArgs() == 2) {
4246 auto Op = CE->getOperator();
4247 switch (Op) {
4248 case OO_Greater:
4249 case OO_GreaterEqual:
4250 case OO_Less:
4251 case OO_LessEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004252 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004253 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
4254 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4255 CE->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004256 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004257 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
4258 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4259 CE->getOperatorLoc());
4260 break;
4261 default:
4262 break;
4263 }
4264 }
4265 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004266 if (Dependent() || SemaRef.CurContext->isDependentContext())
4267 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004268 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004269 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004270 return true;
4271}
4272
4273bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
4274 // RHS of canonical loop form increment can be:
4275 // var + incr
4276 // incr + var
4277 // var - incr
4278 //
4279 RHS = RHS->IgnoreParenImpCasts();
4280 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
4281 if (BO->isAdditiveOp()) {
4282 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004283 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004284 return SetStep(BO->getRHS(), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004285 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004286 return SetStep(BO->getLHS(), false);
4287 }
4288 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
4289 bool IsAdd = CE->getOperator() == OO_Plus;
4290 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004291 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004292 return SetStep(CE->getArg(1), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004293 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004294 return SetStep(CE->getArg(0), false);
4295 }
4296 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004297 if (Dependent() || SemaRef.CurContext->isDependentContext())
4298 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004299 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004300 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004301 return true;
4302}
4303
4304bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
4305 // Check incr-expr for canonical loop form and return true if it
4306 // does not conform.
4307 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4308 // ++var
4309 // var++
4310 // --var
4311 // var--
4312 // var += incr
4313 // var -= incr
4314 // var = var + incr
4315 // var = incr + var
4316 // var = var - incr
4317 //
4318 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004319 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004320 return true;
4321 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004322 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4323 if (!ExprTemp->cleanupsHaveSideEffects())
4324 S = ExprTemp->getSubExpr();
4325
Alexander Musmana5f070a2014-10-01 06:03:56 +00004326 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004327 S = S->IgnoreParens();
4328 if (auto UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004329 if (UO->isIncrementDecrementOp() &&
4330 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004331 return SetStep(
4332 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
4333 (UO->isDecrementOp() ? -1 : 1)).get(),
4334 false);
4335 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
4336 switch (BO->getOpcode()) {
4337 case BO_AddAssign:
4338 case BO_SubAssign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004339 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004340 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
4341 break;
4342 case BO_Assign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004343 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004344 return CheckIncRHS(BO->getRHS());
4345 break;
4346 default:
4347 break;
4348 }
4349 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4350 switch (CE->getOperator()) {
4351 case OO_PlusPlus:
4352 case OO_MinusMinus:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004353 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004354 return SetStep(
4355 SemaRef.ActOnIntegerConstant(
4356 CE->getLocStart(),
4357 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
4358 false);
4359 break;
4360 case OO_PlusEqual:
4361 case OO_MinusEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004362 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004363 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
4364 break;
4365 case OO_Equal:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004366 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004367 return CheckIncRHS(CE->getArg(1));
4368 break;
4369 default:
4370 break;
4371 }
4372 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004373 if (Dependent() || SemaRef.CurContext->isDependentContext())
4374 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004375 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004376 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004377 return true;
4378}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004379
Alexey Bataev5a3af132016-03-29 08:58:54 +00004380static ExprResult
4381tryBuildCapture(Sema &SemaRef, Expr *Capture,
4382 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4383 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4384 return SemaRef.PerformImplicitConversion(
4385 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4386 /*AllowExplicit=*/true);
4387 auto I = Captures.find(Capture);
4388 if (I != Captures.end())
4389 return buildCapture(SemaRef, Capture, I->second);
4390 DeclRefExpr *Ref = nullptr;
4391 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4392 Captures[Capture] = Ref;
4393 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004394}
4395
Alexander Musmana5f070a2014-10-01 06:03:56 +00004396/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004397Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
4398 Scope *S, const bool LimitedType,
4399 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004400 ExprResult Diff;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004401 auto VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004402 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004403 SemaRef.getLangOpts().CPlusPlus) {
4404 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004405 auto *UBExpr = TestIsLessOp ? UB : LB;
4406 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004407 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4408 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004409 if (!Upper || !Lower)
4410 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004411
4412 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4413
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004414 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004415 // BuildBinOp already emitted error, this one is to point user to upper
4416 // and lower bound, and to tell what is passed to 'operator-'.
4417 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
4418 << Upper->getSourceRange() << Lower->getSourceRange();
4419 return nullptr;
4420 }
4421 }
4422
4423 if (!Diff.isUsable())
4424 return nullptr;
4425
4426 // Upper - Lower [- 1]
4427 if (TestIsStrictOp)
4428 Diff = SemaRef.BuildBinOp(
4429 S, DefaultLoc, BO_Sub, Diff.get(),
4430 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4431 if (!Diff.isUsable())
4432 return nullptr;
4433
4434 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00004435 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
4436 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004437 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004438 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004439 if (!Diff.isUsable())
4440 return nullptr;
4441
4442 // Parentheses (for dumping/debugging purposes only).
4443 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4444 if (!Diff.isUsable())
4445 return nullptr;
4446
4447 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004448 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004449 if (!Diff.isUsable())
4450 return nullptr;
4451
Alexander Musman174b3ca2014-10-06 11:16:29 +00004452 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004453 QualType Type = Diff.get()->getType();
4454 auto &C = SemaRef.Context;
4455 bool UseVarType = VarType->hasIntegerRepresentation() &&
4456 C.getTypeSize(Type) > C.getTypeSize(VarType);
4457 if (!Type->isIntegerType() || UseVarType) {
4458 unsigned NewSize =
4459 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4460 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4461 : Type->hasSignedIntegerRepresentation();
4462 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004463 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4464 Diff = SemaRef.PerformImplicitConversion(
4465 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4466 if (!Diff.isUsable())
4467 return nullptr;
4468 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004469 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004470 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004471 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4472 if (NewSize != C.getTypeSize(Type)) {
4473 if (NewSize < C.getTypeSize(Type)) {
4474 assert(NewSize == 64 && "incorrect loop var size");
4475 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4476 << InitSrcRange << ConditionSrcRange;
4477 }
4478 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004479 NewSize, Type->hasSignedIntegerRepresentation() ||
4480 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004481 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4482 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4483 Sema::AA_Converting, true);
4484 if (!Diff.isUsable())
4485 return nullptr;
4486 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004487 }
4488 }
4489
Alexander Musmana5f070a2014-10-01 06:03:56 +00004490 return Diff.get();
4491}
4492
Alexey Bataev5a3af132016-03-29 08:58:54 +00004493Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4494 Scope *S, Expr *Cond,
4495 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004496 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4497 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4498 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004499
Alexey Bataev5a3af132016-03-29 08:58:54 +00004500 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4501 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4502 if (!NewLB.isUsable() || !NewUB.isUsable())
4503 return nullptr;
4504
Alexey Bataev62dbb972015-04-22 11:59:37 +00004505 auto CondExpr = SemaRef.BuildBinOp(
4506 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4507 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004508 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004509 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004510 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4511 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004512 CondExpr = SemaRef.PerformImplicitConversion(
4513 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4514 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004515 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004516 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4517 // Otherwise use original loop conditon and evaluate it in runtime.
4518 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4519}
4520
Alexander Musmana5f070a2014-10-01 06:03:56 +00004521/// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004522DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004523 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004524 auto *VD = dyn_cast<VarDecl>(LCDecl);
4525 if (!VD) {
4526 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4527 auto *Ref = buildDeclRefExpr(
4528 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004529 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4530 // If the loop control decl is explicitly marked as private, do not mark it
4531 // as captured again.
4532 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4533 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004534 return Ref;
4535 }
4536 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004537 DefaultLoc);
4538}
4539
4540Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004541 if (LCDecl && !LCDecl->isInvalidDecl()) {
4542 auto Type = LCDecl->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00004543 auto *PrivateVar =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004544 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4545 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004546 if (PrivateVar->isInvalidDecl())
4547 return nullptr;
4548 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4549 }
4550 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004551}
4552
4553/// \brief Build initization of the counter be used for codegen.
4554Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4555
4556/// \brief Build step of the counter be used for codegen.
4557Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4558
4559/// \brief Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004560struct LoopIterationSpace final {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004561 /// \brief Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004562 Expr *PreCond = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004563 /// \brief This expression calculates the number of iterations in the loop.
4564 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004565 Expr *NumIterations = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004566 /// \brief The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004567 Expr *CounterVar = nullptr;
Alexey Bataeva8899172015-08-06 12:30:57 +00004568 /// \brief Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004569 Expr *PrivateCounterVar = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004570 /// \brief This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004571 Expr *CounterInit = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004572 /// \brief This is step for the #CounterVar used to generate its update:
4573 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004574 Expr *CounterStep = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004575 /// \brief Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004576 bool Subtract = false;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004577 /// \brief Source range of the loop init.
4578 SourceRange InitSrcRange;
4579 /// \brief Source range of the loop condition.
4580 SourceRange CondSrcRange;
4581 /// \brief Source range of the loop increment.
4582 SourceRange IncSrcRange;
4583};
4584
Alexey Bataev23b69422014-06-18 07:08:49 +00004585} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004586
Alexey Bataev9c821032015-04-30 04:23:23 +00004587void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4588 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4589 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004590 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4591 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004592 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4593 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004594 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4595 if (auto *D = ISC.GetLoopDecl()) {
4596 auto *VD = dyn_cast<VarDecl>(D);
4597 if (!VD) {
4598 if (auto *Private = IsOpenMPCapturedDecl(D))
4599 VD = Private;
4600 else {
4601 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4602 /*WithInit=*/false);
4603 VD = cast<VarDecl>(Ref->getDecl());
4604 }
4605 }
4606 DSAStack->addLoopControlVariable(D, VD);
4607 }
4608 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004609 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004610 }
4611}
4612
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004613/// \brief Called on a for stmt to check and extract its iteration space
4614/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00004615static bool CheckOpenMPIterationSpace(
4616 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4617 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004618 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004619 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004620 LoopIterationSpace &ResultIterSpace,
4621 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004622 // OpenMP [2.6, Canonical Loop Form]
4623 // for (init-expr; test-expr; incr-expr) structured-block
4624 auto For = dyn_cast_or_null<ForStmt>(S);
4625 if (!For) {
4626 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004627 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4628 << getOpenMPDirectiveName(DKind) << NestedLoopCount
4629 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4630 if (NestedLoopCount > 1) {
4631 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4632 SemaRef.Diag(DSA.getConstructLoc(),
4633 diag::note_omp_collapse_ordered_expr)
4634 << 2 << CollapseLoopCountExpr->getSourceRange()
4635 << OrderedLoopCountExpr->getSourceRange();
4636 else if (CollapseLoopCountExpr)
4637 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4638 diag::note_omp_collapse_ordered_expr)
4639 << 0 << CollapseLoopCountExpr->getSourceRange();
4640 else
4641 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4642 diag::note_omp_collapse_ordered_expr)
4643 << 1 << OrderedLoopCountExpr->getSourceRange();
4644 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004645 return true;
4646 }
4647 assert(For->getBody());
4648
4649 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4650
4651 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00004652 auto Init = For->getInit();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004653 if (ISC.CheckInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004654 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004655
4656 bool HasErrors = false;
4657
4658 // Check loop variable's type.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004659 if (auto *LCDecl = ISC.GetLoopDecl()) {
4660 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004661
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004662 // OpenMP [2.6, Canonical Loop Form]
4663 // Var is one of the following:
4664 // A variable of signed or unsigned integer type.
4665 // For C++, a variable of a random access iterator type.
4666 // For C, a variable of a pointer type.
4667 auto VarType = LCDecl->getType().getNonReferenceType();
4668 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4669 !VarType->isPointerType() &&
4670 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4671 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4672 << SemaRef.getLangOpts().CPlusPlus;
4673 HasErrors = true;
4674 }
4675
4676 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4677 // a Construct
4678 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4679 // parallel for construct is (are) private.
4680 // The loop iteration variable in the associated for-loop of a simd
4681 // construct with just one associated for-loop is linear with a
4682 // constant-linear-step that is the increment of the associated for-loop.
4683 // Exclude loop var from the list of variables with implicitly defined data
4684 // sharing attributes.
4685 VarsWithImplicitDSA.erase(LCDecl);
4686
4687 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4688 // in a Construct, C/C++].
4689 // The loop iteration variable in the associated for-loop of a simd
4690 // construct with just one associated for-loop may be listed in a linear
4691 // clause with a constant-linear-step that is the increment of the
4692 // associated for-loop.
4693 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4694 // parallel for construct may be listed in a private or lastprivate clause.
4695 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4696 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4697 // declared in the loop and it is predetermined as a private.
4698 auto PredeterminedCKind =
4699 isOpenMPSimdDirective(DKind)
4700 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4701 : OMPC_private;
4702 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4703 DVar.CKind != PredeterminedCKind) ||
4704 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4705 isOpenMPDistributeDirective(DKind)) &&
4706 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4707 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4708 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4709 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4710 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4711 << getOpenMPClauseName(PredeterminedCKind);
4712 if (DVar.RefExpr == nullptr)
4713 DVar.CKind = PredeterminedCKind;
4714 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4715 HasErrors = true;
4716 } else if (LoopDeclRefExpr != nullptr) {
4717 // Make the loop iteration variable private (for worksharing constructs),
4718 // linear (for simd directives with the only one associated loop) or
4719 // lastprivate (for simd directives with several collapsed or ordered
4720 // loops).
4721 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004722 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4723 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004724 /*FromParent=*/false);
4725 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4726 }
4727
4728 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4729
4730 // Check test-expr.
4731 HasErrors |= ISC.CheckCond(For->getCond());
4732
4733 // Check incr-expr.
4734 HasErrors |= ISC.CheckInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004735 }
4736
Alexander Musmana5f070a2014-10-01 06:03:56 +00004737 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004738 return HasErrors;
4739
Alexander Musmana5f070a2014-10-01 06:03:56 +00004740 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004741 ResultIterSpace.PreCond =
4742 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00004743 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004744 DSA.getCurScope(),
4745 (isOpenMPWorksharingDirective(DKind) ||
4746 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4747 Captures);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004748 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
Alexey Bataeva8899172015-08-06 12:30:57 +00004749 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004750 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4751 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4752 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4753 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4754 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4755 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4756
Alexey Bataev62dbb972015-04-22 11:59:37 +00004757 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4758 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004759 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004760 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004761 ResultIterSpace.CounterInit == nullptr ||
4762 ResultIterSpace.CounterStep == nullptr);
4763
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004764 return HasErrors;
4765}
4766
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004767/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004768static ExprResult
4769BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4770 ExprResult Start,
4771 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004772 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004773 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4774 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004775 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004776 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004777 VarRef.get()->getType())) {
4778 NewStart = SemaRef.PerformImplicitConversion(
4779 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4780 /*AllowExplicit=*/true);
4781 if (!NewStart.isUsable())
4782 return ExprError();
4783 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004784
4785 auto Init =
4786 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4787 return Init;
4788}
4789
Alexander Musmana5f070a2014-10-01 06:03:56 +00004790/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004791static ExprResult
4792BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4793 ExprResult VarRef, ExprResult Start, ExprResult Iter,
4794 ExprResult Step, bool Subtract,
4795 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004796 // Add parentheses (for debugging purposes only).
4797 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4798 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4799 !Step.isUsable())
4800 return ExprError();
4801
Alexey Bataev5a3af132016-03-29 08:58:54 +00004802 ExprResult NewStep = Step;
4803 if (Captures)
4804 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004805 if (NewStep.isInvalid())
4806 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004807 ExprResult Update =
4808 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004809 if (!Update.isUsable())
4810 return ExprError();
4811
Alexey Bataevc0214e02016-02-16 12:13:49 +00004812 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4813 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004814 ExprResult NewStart = Start;
4815 if (Captures)
4816 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004817 if (NewStart.isInvalid())
4818 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004819
Alexey Bataevc0214e02016-02-16 12:13:49 +00004820 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4821 ExprResult SavedUpdate = Update;
4822 ExprResult UpdateVal;
4823 if (VarRef.get()->getType()->isOverloadableType() ||
4824 NewStart.get()->getType()->isOverloadableType() ||
4825 Update.get()->getType()->isOverloadableType()) {
4826 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4827 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4828 Update =
4829 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4830 if (Update.isUsable()) {
4831 UpdateVal =
4832 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4833 VarRef.get(), SavedUpdate.get());
4834 if (UpdateVal.isUsable()) {
4835 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4836 UpdateVal.get());
4837 }
4838 }
4839 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4840 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004841
Alexey Bataevc0214e02016-02-16 12:13:49 +00004842 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4843 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4844 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4845 NewStart.get(), SavedUpdate.get());
4846 if (!Update.isUsable())
4847 return ExprError();
4848
Alexey Bataev11481f52016-02-17 10:29:05 +00004849 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4850 VarRef.get()->getType())) {
4851 Update = SemaRef.PerformImplicitConversion(
4852 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4853 if (!Update.isUsable())
4854 return ExprError();
4855 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004856
4857 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4858 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004859 return Update;
4860}
4861
4862/// \brief Convert integer expression \a E to make it have at least \a Bits
4863/// bits.
4864static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4865 Sema &SemaRef) {
4866 if (E == nullptr)
4867 return ExprError();
4868 auto &C = SemaRef.Context;
4869 QualType OldType = E->getType();
4870 unsigned HasBits = C.getTypeSize(OldType);
4871 if (HasBits >= Bits)
4872 return ExprResult(E);
4873 // OK to convert to signed, because new type has more bits than old.
4874 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4875 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4876 true);
4877}
4878
4879/// \brief Check if the given expression \a E is a constant integer that fits
4880/// into \a Bits bits.
4881static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4882 if (E == nullptr)
4883 return false;
4884 llvm::APSInt Result;
4885 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4886 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4887 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004888}
4889
Alexey Bataev5a3af132016-03-29 08:58:54 +00004890/// Build preinits statement for the given declarations.
4891static Stmt *buildPreInits(ASTContext &Context,
4892 SmallVectorImpl<Decl *> &PreInits) {
4893 if (!PreInits.empty()) {
4894 return new (Context) DeclStmt(
4895 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4896 SourceLocation(), SourceLocation());
4897 }
4898 return nullptr;
4899}
4900
4901/// Build preinits statement for the given declarations.
4902static Stmt *buildPreInits(ASTContext &Context,
4903 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4904 if (!Captures.empty()) {
4905 SmallVector<Decl *, 16> PreInits;
4906 for (auto &Pair : Captures)
4907 PreInits.push_back(Pair.second->getDecl());
4908 return buildPreInits(Context, PreInits);
4909 }
4910 return nullptr;
4911}
4912
4913/// Build postupdate expression for the given list of postupdates expressions.
4914static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4915 Expr *PostUpdate = nullptr;
4916 if (!PostUpdates.empty()) {
4917 for (auto *E : PostUpdates) {
4918 Expr *ConvE = S.BuildCStyleCastExpr(
4919 E->getExprLoc(),
4920 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4921 E->getExprLoc(), E)
4922 .get();
4923 PostUpdate = PostUpdate
4924 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4925 PostUpdate, ConvE)
4926 .get()
4927 : ConvE;
4928 }
4929 }
4930 return PostUpdate;
4931}
4932
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004933/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004934/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4935/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004936static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004937CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4938 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4939 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004940 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004941 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004942 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004943 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004944 // Found 'collapse' clause - calculate collapse number.
4945 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004946 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004947 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004948 }
4949 if (OrderedLoopCountExpr) {
4950 // Found 'ordered' clause - calculate collapse number.
4951 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004952 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4953 if (Result.getLimitedValue() < NestedLoopCount) {
4954 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4955 diag::err_omp_wrong_ordered_loop_count)
4956 << OrderedLoopCountExpr->getSourceRange();
4957 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4958 diag::note_collapse_loop_count)
4959 << CollapseLoopCountExpr->getSourceRange();
4960 }
4961 NestedLoopCount = Result.getLimitedValue();
4962 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004963 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004964 // This is helper routine for loop directives (e.g., 'for', 'simd',
4965 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00004966 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004967 SmallVector<LoopIterationSpace, 4> IterSpaces;
4968 IterSpaces.resize(NestedLoopCount);
4969 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004970 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004971 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004972 NestedLoopCount, CollapseLoopCountExpr,
4973 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004974 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004975 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004976 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004977 // OpenMP [2.8.1, simd construct, Restrictions]
4978 // All loops associated with the construct must be perfectly nested; that
4979 // is, there must be no intervening code nor any OpenMP directive between
4980 // any two loops.
4981 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004982 }
4983
Alexander Musmana5f070a2014-10-01 06:03:56 +00004984 Built.clear(/* size */ NestedLoopCount);
4985
4986 if (SemaRef.CurContext->isDependentContext())
4987 return NestedLoopCount;
4988
4989 // An example of what is generated for the following code:
4990 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004991 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004992 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004993 // for (k = 0; k < NK; ++k)
4994 // for (j = J0; j < NJ; j+=2) {
4995 // <loop body>
4996 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004997 //
4998 // We generate the code below.
4999 // Note: the loop body may be outlined in CodeGen.
5000 // Note: some counters may be C++ classes, operator- is used to find number of
5001 // iterations and operator+= to calculate counter value.
5002 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
5003 // or i64 is currently supported).
5004 //
5005 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
5006 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
5007 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
5008 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
5009 // // similar updates for vars in clauses (e.g. 'linear')
5010 // <loop body (using local i and j)>
5011 // }
5012 // i = NI; // assign final values of counters
5013 // j = NJ;
5014 //
5015
5016 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
5017 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00005018 // Precondition tests if there is at least one iteration (all conditions are
5019 // true).
5020 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005021 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005022 ExprResult LastIteration32 = WidenIterationCount(
5023 32 /* Bits */, SemaRef.PerformImplicitConversion(
5024 N0->IgnoreImpCasts(), N0->getType(),
5025 Sema::AA_Converting, /*AllowExplicit=*/true)
5026 .get(),
5027 SemaRef);
5028 ExprResult LastIteration64 = WidenIterationCount(
5029 64 /* Bits */, SemaRef.PerformImplicitConversion(
5030 N0->IgnoreImpCasts(), N0->getType(),
5031 Sema::AA_Converting, /*AllowExplicit=*/true)
5032 .get(),
5033 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005034
5035 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
5036 return NestedLoopCount;
5037
5038 auto &C = SemaRef.Context;
5039 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
5040
5041 Scope *CurScope = DSA.getCurScope();
5042 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00005043 if (PreCond.isUsable()) {
5044 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
5045 PreCond.get(), IterSpaces[Cnt].PreCond);
5046 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005047 auto N = IterSpaces[Cnt].NumIterations;
5048 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5049 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005050 LastIteration32 = SemaRef.BuildBinOp(
5051 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
5052 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5053 Sema::AA_Converting,
5054 /*AllowExplicit=*/true)
5055 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005056 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005057 LastIteration64 = SemaRef.BuildBinOp(
5058 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
5059 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5060 Sema::AA_Converting,
5061 /*AllowExplicit=*/true)
5062 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005063 }
5064
5065 // Choose either the 32-bit or 64-bit version.
5066 ExprResult LastIteration = LastIteration64;
5067 if (LastIteration32.isUsable() &&
5068 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5069 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
5070 FitsInto(
5071 32 /* Bits */,
5072 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5073 LastIteration64.get(), SemaRef)))
5074 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00005075 QualType VType = LastIteration.get()->getType();
5076 QualType RealVType = VType;
5077 QualType StrideVType = VType;
5078 if (isOpenMPTaskLoopDirective(DKind)) {
5079 VType =
5080 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5081 StrideVType =
5082 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5083 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005084
5085 if (!LastIteration.isUsable())
5086 return 0;
5087
5088 // Save the number of iterations.
5089 ExprResult NumIterations = LastIteration;
5090 {
5091 LastIteration = SemaRef.BuildBinOp(
5092 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
5093 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5094 if (!LastIteration.isUsable())
5095 return 0;
5096 }
5097
5098 // Calculate the last iteration number beforehand instead of doing this on
5099 // each iteration. Do not do this if the number of iterations may be kfold-ed.
5100 llvm::APSInt Result;
5101 bool IsConstant =
5102 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5103 ExprResult CalcLastIteration;
5104 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005105 ExprResult SaveRef =
5106 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005107 LastIteration = SaveRef;
5108
5109 // Prepare SaveRef + 1.
5110 NumIterations = SemaRef.BuildBinOp(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005111 CurScope, SourceLocation(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005112 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5113 if (!NumIterations.isUsable())
5114 return 0;
5115 }
5116
5117 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5118
Alexander Musmanc6388682014-12-15 07:07:06 +00005119 // Build variables passed into runtime, nesessary for worksharing directives.
Carlo Bertolli9925f152016-06-27 14:55:37 +00005120 ExprResult LB, UB, IL, ST, EUB, PrevLB, PrevUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005121 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5122 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005123 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005124 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5125 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005126 SemaRef.AddInitializerToDecl(
5127 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5128 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5129
5130 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005131 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5132 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005133 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
5134 /*DirectInit*/ false,
5135 /*TypeMayContainAuto*/ false);
5136
5137 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5138 // This will be used to implement clause 'lastprivate'.
5139 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005140 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5141 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005142 SemaRef.AddInitializerToDecl(
5143 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5144 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5145
5146 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00005147 VarDecl *STDecl =
5148 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5149 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005150 SemaRef.AddInitializerToDecl(
5151 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5152 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5153
5154 // Build expression: UB = min(UB, LastIteration)
5155 // It is nesessary for CodeGen of directives with static scheduling.
5156 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5157 UB.get(), LastIteration.get());
5158 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5159 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
5160 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5161 CondOp.get());
5162 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
Carlo Bertolli9925f152016-06-27 14:55:37 +00005163
5164 // If we have a combined directive that combines 'distribute', 'for' or
5165 // 'simd' we need to be able to access the bounds of the schedule of the
5166 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5167 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5168 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5169 auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
5170
5171 // We expect to have at least 2 more parameters than the 'parallel'
5172 // directive does - the lower and upper bounds of the previous schedule.
5173 assert(CD->getNumParams() >= 4 &&
5174 "Unexpected number of parameters in loop combined directive");
5175
5176 // Set the proper type for the bounds given what we learned from the
5177 // enclosed loops.
5178 auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5179 auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
5180
5181 // Previous lower and upper bounds are obtained from the region
5182 // parameters.
5183 PrevLB =
5184 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5185 PrevUB =
5186 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5187 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005188 }
5189
5190 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005191 ExprResult IV;
5192 ExprResult Init;
5193 {
Alexey Bataev7292c292016-04-25 12:22:29 +00005194 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5195 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005196 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005197 isOpenMPTaskLoopDirective(DKind) ||
5198 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005199 ? LB.get()
5200 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5201 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
5202 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005203 }
5204
Alexander Musmanc6388682014-12-15 07:07:06 +00005205 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005206 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00005207 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005208 (isOpenMPWorksharingDirective(DKind) ||
5209 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005210 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5211 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5212 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005213
5214 // Loop increment (IV = IV + 1)
5215 SourceLocation IncLoc;
5216 ExprResult Inc =
5217 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5218 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5219 if (!Inc.isUsable())
5220 return 0;
5221 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005222 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
5223 if (!Inc.isUsable())
5224 return 0;
5225
5226 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5227 // Used for directives with static scheduling.
5228 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005229 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5230 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005231 // LB + ST
5232 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5233 if (!NextLB.isUsable())
5234 return 0;
5235 // LB = LB + ST
5236 NextLB =
5237 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
5238 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
5239 if (!NextLB.isUsable())
5240 return 0;
5241 // UB + ST
5242 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5243 if (!NextUB.isUsable())
5244 return 0;
5245 // UB = UB + ST
5246 NextUB =
5247 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
5248 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
5249 if (!NextUB.isUsable())
5250 return 0;
5251 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005252
5253 // Build updates and final values of the loop counters.
5254 bool HasErrors = false;
5255 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005256 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005257 Built.Updates.resize(NestedLoopCount);
5258 Built.Finals.resize(NestedLoopCount);
Alexey Bataev8b427062016-05-25 12:36:08 +00005259 SmallVector<Expr *, 4> LoopMultipliers;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005260 {
5261 ExprResult Div;
5262 // Go from inner nested loop to outer.
5263 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5264 LoopIterationSpace &IS = IterSpaces[Cnt];
5265 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5266 // Build: Iter = (IV / Div) % IS.NumIters
5267 // where Div is product of previous iterations' IS.NumIters.
5268 ExprResult Iter;
5269 if (Div.isUsable()) {
5270 Iter =
5271 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5272 } else {
5273 Iter = IV;
5274 assert((Cnt == (int)NestedLoopCount - 1) &&
5275 "unusable div expected on first iteration only");
5276 }
5277
5278 if (Cnt != 0 && Iter.isUsable())
5279 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5280 IS.NumIterations);
5281 if (!Iter.isUsable()) {
5282 HasErrors = true;
5283 break;
5284 }
5285
Alexey Bataev39f915b82015-05-08 10:41:21 +00005286 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005287 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
5288 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
5289 IS.CounterVar->getExprLoc(),
5290 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005291 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005292 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005293 if (!Init.isUsable()) {
5294 HasErrors = true;
5295 break;
5296 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00005297 ExprResult Update = BuildCounterUpdate(
5298 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5299 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005300 if (!Update.isUsable()) {
5301 HasErrors = true;
5302 break;
5303 }
5304
5305 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
5306 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005307 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005308 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005309 if (!Final.isUsable()) {
5310 HasErrors = true;
5311 break;
5312 }
5313
5314 // Build Div for the next iteration: Div <- Div * IS.NumIters
5315 if (Cnt != 0) {
5316 if (Div.isUnset())
5317 Div = IS.NumIterations;
5318 else
5319 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5320 IS.NumIterations);
5321
5322 // Add parentheses (for debugging purposes only).
5323 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00005324 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005325 if (!Div.isUsable()) {
5326 HasErrors = true;
5327 break;
5328 }
Alexey Bataev8b427062016-05-25 12:36:08 +00005329 LoopMultipliers.push_back(Div.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005330 }
5331 if (!Update.isUsable() || !Final.isUsable()) {
5332 HasErrors = true;
5333 break;
5334 }
5335 // Save results
5336 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005337 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005338 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005339 Built.Updates[Cnt] = Update.get();
5340 Built.Finals[Cnt] = Final.get();
5341 }
5342 }
5343
5344 if (HasErrors)
5345 return 0;
5346
5347 // Save results
5348 Built.IterationVarRef = IV.get();
5349 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005350 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005351 Built.CalcLastIteration =
5352 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005353 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005354 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005355 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005356 Built.Init = Init.get();
5357 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005358 Built.LB = LB.get();
5359 Built.UB = UB.get();
5360 Built.IL = IL.get();
5361 Built.ST = ST.get();
5362 Built.EUB = EUB.get();
5363 Built.NLB = NextLB.get();
5364 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005365 Built.PrevLB = PrevLB.get();
5366 Built.PrevUB = PrevUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005367
Alexey Bataev8b427062016-05-25 12:36:08 +00005368 Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get();
5369 // Fill data for doacross depend clauses.
5370 for (auto Pair : DSA.getDoacrossDependClauses()) {
5371 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
5372 Pair.first->setCounterValue(CounterVal);
5373 else {
5374 if (NestedLoopCount != Pair.second.size() ||
5375 NestedLoopCount != LoopMultipliers.size() + 1) {
5376 // Erroneous case - clause has some problems.
5377 Pair.first->setCounterValue(CounterVal);
5378 continue;
5379 }
5380 assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink);
5381 auto I = Pair.second.rbegin();
5382 auto IS = IterSpaces.rbegin();
5383 auto ILM = LoopMultipliers.rbegin();
5384 Expr *UpCounterVal = CounterVal;
5385 Expr *Multiplier = nullptr;
5386 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5387 if (I->first) {
5388 assert(IS->CounterStep);
5389 Expr *NormalizedOffset =
5390 SemaRef
5391 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div,
5392 I->first, IS->CounterStep)
5393 .get();
5394 if (Multiplier) {
5395 NormalizedOffset =
5396 SemaRef
5397 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul,
5398 NormalizedOffset, Multiplier)
5399 .get();
5400 }
5401 assert(I->second == OO_Plus || I->second == OO_Minus);
5402 BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub;
5403 UpCounterVal =
5404 SemaRef.BuildBinOp(CurScope, I->first->getExprLoc(), BOK,
5405 UpCounterVal, NormalizedOffset).get();
5406 }
5407 Multiplier = *ILM;
5408 ++I;
5409 ++IS;
5410 ++ILM;
5411 }
5412 Pair.first->setCounterValue(UpCounterVal);
5413 }
5414 }
5415
Alexey Bataevabfc0692014-06-25 06:52:00 +00005416 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005417}
5418
Alexey Bataev10e775f2015-07-30 11:36:16 +00005419static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005420 auto CollapseClauses =
5421 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5422 if (CollapseClauses.begin() != CollapseClauses.end())
5423 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005424 return nullptr;
5425}
5426
Alexey Bataev10e775f2015-07-30 11:36:16 +00005427static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005428 auto OrderedClauses =
5429 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5430 if (OrderedClauses.begin() != OrderedClauses.end())
5431 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005432 return nullptr;
5433}
5434
Alexey Bataev66b15b52015-08-21 11:14:16 +00005435static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
5436 const Expr *Safelen) {
5437 llvm::APSInt SimdlenRes, SafelenRes;
5438 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
5439 Simdlen->isInstantiationDependent() ||
5440 Simdlen->containsUnexpandedParameterPack())
5441 return false;
5442 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
5443 Safelen->isInstantiationDependent() ||
5444 Safelen->containsUnexpandedParameterPack())
5445 return false;
5446 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
5447 Safelen->EvaluateAsInt(SafelenRes, S.Context);
5448 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5449 // If both simdlen and safelen clauses are specified, the value of the simdlen
5450 // parameter must be less than or equal to the value of the safelen parameter.
5451 if (SimdlenRes > SafelenRes) {
5452 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
5453 << Simdlen->getSourceRange() << Safelen->getSourceRange();
5454 return true;
5455 }
5456 return false;
5457}
5458
Alexey Bataev4acb8592014-07-07 13:01:15 +00005459StmtResult Sema::ActOnOpenMPSimdDirective(
5460 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5461 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005462 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005463 if (!AStmt)
5464 return StmtError();
5465
5466 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005467 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005468 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5469 // define the nested loops number.
5470 unsigned NestedLoopCount = CheckOpenMPLoop(
5471 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5472 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005473 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005474 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005475
Alexander Musmana5f070a2014-10-01 06:03:56 +00005476 assert((CurContext->isDependentContext() || B.builtAll()) &&
5477 "omp simd loop exprs were not built");
5478
Alexander Musman3276a272015-03-21 10:12:56 +00005479 if (!CurContext->isDependentContext()) {
5480 // Finalize the clauses that need pre-built expressions for CodeGen.
5481 for (auto C : Clauses) {
5482 if (auto LC = dyn_cast<OMPLinearClause>(C))
5483 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005484 B.NumIterations, *this, CurScope,
5485 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005486 return StmtError();
5487 }
5488 }
5489
Alexey Bataev66b15b52015-08-21 11:14:16 +00005490 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5491 // If both simdlen and safelen clauses are specified, the value of the simdlen
5492 // parameter must be less than or equal to the value of the safelen parameter.
5493 OMPSafelenClause *Safelen = nullptr;
5494 OMPSimdlenClause *Simdlen = nullptr;
5495 for (auto *Clause : Clauses) {
5496 if (Clause->getClauseKind() == OMPC_safelen)
5497 Safelen = cast<OMPSafelenClause>(Clause);
5498 else if (Clause->getClauseKind() == OMPC_simdlen)
5499 Simdlen = cast<OMPSimdlenClause>(Clause);
5500 if (Safelen && Simdlen)
5501 break;
5502 }
5503 if (Simdlen && Safelen &&
5504 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5505 Safelen->getSafelen()))
5506 return StmtError();
5507
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005508 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005509 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5510 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005511}
5512
Alexey Bataev4acb8592014-07-07 13:01:15 +00005513StmtResult Sema::ActOnOpenMPForDirective(
5514 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5515 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005516 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005517 if (!AStmt)
5518 return StmtError();
5519
5520 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005521 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005522 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5523 // define the nested loops number.
5524 unsigned NestedLoopCount = CheckOpenMPLoop(
5525 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5526 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005527 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005528 return StmtError();
5529
Alexander Musmana5f070a2014-10-01 06:03:56 +00005530 assert((CurContext->isDependentContext() || B.builtAll()) &&
5531 "omp for loop exprs were not built");
5532
Alexey Bataev54acd402015-08-04 11:18:19 +00005533 if (!CurContext->isDependentContext()) {
5534 // Finalize the clauses that need pre-built expressions for CodeGen.
5535 for (auto C : Clauses) {
5536 if (auto LC = dyn_cast<OMPLinearClause>(C))
5537 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005538 B.NumIterations, *this, CurScope,
5539 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005540 return StmtError();
5541 }
5542 }
5543
Alexey Bataevf29276e2014-06-18 04:14:57 +00005544 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005545 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005546 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005547}
5548
Alexander Musmanf82886e2014-09-18 05:12:34 +00005549StmtResult Sema::ActOnOpenMPForSimdDirective(
5550 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5551 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005552 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005553 if (!AStmt)
5554 return StmtError();
5555
5556 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005557 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005558 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5559 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005560 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005561 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5562 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5563 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005564 if (NestedLoopCount == 0)
5565 return StmtError();
5566
Alexander Musmanc6388682014-12-15 07:07:06 +00005567 assert((CurContext->isDependentContext() || B.builtAll()) &&
5568 "omp for simd loop exprs were not built");
5569
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005570 if (!CurContext->isDependentContext()) {
5571 // Finalize the clauses that need pre-built expressions for CodeGen.
5572 for (auto C : Clauses) {
5573 if (auto LC = dyn_cast<OMPLinearClause>(C))
5574 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005575 B.NumIterations, *this, CurScope,
5576 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005577 return StmtError();
5578 }
5579 }
5580
Alexey Bataev66b15b52015-08-21 11:14:16 +00005581 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5582 // If both simdlen and safelen clauses are specified, the value of the simdlen
5583 // parameter must be less than or equal to the value of the safelen parameter.
5584 OMPSafelenClause *Safelen = nullptr;
5585 OMPSimdlenClause *Simdlen = nullptr;
5586 for (auto *Clause : Clauses) {
5587 if (Clause->getClauseKind() == OMPC_safelen)
5588 Safelen = cast<OMPSafelenClause>(Clause);
5589 else if (Clause->getClauseKind() == OMPC_simdlen)
5590 Simdlen = cast<OMPSimdlenClause>(Clause);
5591 if (Safelen && Simdlen)
5592 break;
5593 }
5594 if (Simdlen && Safelen &&
5595 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5596 Safelen->getSafelen()))
5597 return StmtError();
5598
Alexander Musmanf82886e2014-09-18 05:12:34 +00005599 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005600 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5601 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005602}
5603
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005604StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5605 Stmt *AStmt,
5606 SourceLocation StartLoc,
5607 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005608 if (!AStmt)
5609 return StmtError();
5610
5611 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005612 auto BaseStmt = AStmt;
5613 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5614 BaseStmt = CS->getCapturedStmt();
5615 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5616 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005617 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005618 return StmtError();
5619 // All associated statements must be '#pragma omp section' except for
5620 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005621 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005622 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5623 if (SectionStmt)
5624 Diag(SectionStmt->getLocStart(),
5625 diag::err_omp_sections_substmt_not_section);
5626 return StmtError();
5627 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005628 cast<OMPSectionDirective>(SectionStmt)
5629 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005630 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005631 } else {
5632 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5633 return StmtError();
5634 }
5635
5636 getCurFunction()->setHasBranchProtectedScope();
5637
Alexey Bataev25e5b442015-09-15 12:52:43 +00005638 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5639 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005640}
5641
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005642StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5643 SourceLocation StartLoc,
5644 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005645 if (!AStmt)
5646 return StmtError();
5647
5648 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005649
5650 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005651 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005652
Alexey Bataev25e5b442015-09-15 12:52:43 +00005653 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5654 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005655}
5656
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005657StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5658 Stmt *AStmt,
5659 SourceLocation StartLoc,
5660 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005661 if (!AStmt)
5662 return StmtError();
5663
5664 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005665
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005666 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005667
Alexey Bataev3255bf32015-01-19 05:20:46 +00005668 // OpenMP [2.7.3, single Construct, Restrictions]
5669 // The copyprivate clause must not be used with the nowait clause.
5670 OMPClause *Nowait = nullptr;
5671 OMPClause *Copyprivate = nullptr;
5672 for (auto *Clause : Clauses) {
5673 if (Clause->getClauseKind() == OMPC_nowait)
5674 Nowait = Clause;
5675 else if (Clause->getClauseKind() == OMPC_copyprivate)
5676 Copyprivate = Clause;
5677 if (Copyprivate && Nowait) {
5678 Diag(Copyprivate->getLocStart(),
5679 diag::err_omp_single_copyprivate_with_nowait);
5680 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5681 return StmtError();
5682 }
5683 }
5684
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005685 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5686}
5687
Alexander Musman80c22892014-07-17 08:54:58 +00005688StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5689 SourceLocation StartLoc,
5690 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005691 if (!AStmt)
5692 return StmtError();
5693
5694 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005695
5696 getCurFunction()->setHasBranchProtectedScope();
5697
5698 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5699}
5700
Alexey Bataev28c75412015-12-15 08:19:24 +00005701StmtResult Sema::ActOnOpenMPCriticalDirective(
5702 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5703 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005704 if (!AStmt)
5705 return StmtError();
5706
5707 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005708
Alexey Bataev28c75412015-12-15 08:19:24 +00005709 bool ErrorFound = false;
5710 llvm::APSInt Hint;
5711 SourceLocation HintLoc;
5712 bool DependentHint = false;
5713 for (auto *C : Clauses) {
5714 if (C->getClauseKind() == OMPC_hint) {
5715 if (!DirName.getName()) {
5716 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5717 ErrorFound = true;
5718 }
5719 Expr *E = cast<OMPHintClause>(C)->getHint();
5720 if (E->isTypeDependent() || E->isValueDependent() ||
5721 E->isInstantiationDependent())
5722 DependentHint = true;
5723 else {
5724 Hint = E->EvaluateKnownConstInt(Context);
5725 HintLoc = C->getLocStart();
5726 }
5727 }
5728 }
5729 if (ErrorFound)
5730 return StmtError();
5731 auto Pair = DSAStack->getCriticalWithHint(DirName);
5732 if (Pair.first && DirName.getName() && !DependentHint) {
5733 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5734 Diag(StartLoc, diag::err_omp_critical_with_hint);
5735 if (HintLoc.isValid()) {
5736 Diag(HintLoc, diag::note_omp_critical_hint_here)
5737 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5738 } else
5739 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5740 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5741 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5742 << 1
5743 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5744 /*Radix=*/10, /*Signed=*/false);
5745 } else
5746 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5747 }
5748 }
5749
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005750 getCurFunction()->setHasBranchProtectedScope();
5751
Alexey Bataev28c75412015-12-15 08:19:24 +00005752 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5753 Clauses, AStmt);
5754 if (!Pair.first && DirName.getName() && !DependentHint)
5755 DSAStack->addCriticalWithHint(Dir, Hint);
5756 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005757}
5758
Alexey Bataev4acb8592014-07-07 13:01:15 +00005759StmtResult Sema::ActOnOpenMPParallelForDirective(
5760 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5761 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005762 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005763 if (!AStmt)
5764 return StmtError();
5765
Alexey Bataev4acb8592014-07-07 13:01:15 +00005766 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5767 // 1.2.2 OpenMP Language Terminology
5768 // Structured block - An executable statement with a single entry at the
5769 // top and a single exit at the bottom.
5770 // The point of exit cannot be a branch out of the structured block.
5771 // longjmp() and throw() must not violate the entry/exit criteria.
5772 CS->getCapturedDecl()->setNothrow();
5773
Alexander Musmanc6388682014-12-15 07:07:06 +00005774 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005775 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5776 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005777 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005778 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5779 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5780 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005781 if (NestedLoopCount == 0)
5782 return StmtError();
5783
Alexander Musmana5f070a2014-10-01 06:03:56 +00005784 assert((CurContext->isDependentContext() || B.builtAll()) &&
5785 "omp parallel for loop exprs were not built");
5786
Alexey Bataev54acd402015-08-04 11:18:19 +00005787 if (!CurContext->isDependentContext()) {
5788 // Finalize the clauses that need pre-built expressions for CodeGen.
5789 for (auto C : Clauses) {
5790 if (auto LC = dyn_cast<OMPLinearClause>(C))
5791 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005792 B.NumIterations, *this, CurScope,
5793 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005794 return StmtError();
5795 }
5796 }
5797
Alexey Bataev4acb8592014-07-07 13:01:15 +00005798 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005799 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005800 NestedLoopCount, Clauses, AStmt, B,
5801 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005802}
5803
Alexander Musmane4e893b2014-09-23 09:33:00 +00005804StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5805 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5806 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005807 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005808 if (!AStmt)
5809 return StmtError();
5810
Alexander Musmane4e893b2014-09-23 09:33:00 +00005811 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5812 // 1.2.2 OpenMP Language Terminology
5813 // Structured block - An executable statement with a single entry at the
5814 // top and a single exit at the bottom.
5815 // The point of exit cannot be a branch out of the structured block.
5816 // longjmp() and throw() must not violate the entry/exit criteria.
5817 CS->getCapturedDecl()->setNothrow();
5818
Alexander Musmanc6388682014-12-15 07:07:06 +00005819 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005820 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5821 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005822 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005823 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5824 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5825 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005826 if (NestedLoopCount == 0)
5827 return StmtError();
5828
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005829 if (!CurContext->isDependentContext()) {
5830 // Finalize the clauses that need pre-built expressions for CodeGen.
5831 for (auto C : Clauses) {
5832 if (auto LC = dyn_cast<OMPLinearClause>(C))
5833 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005834 B.NumIterations, *this, CurScope,
5835 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005836 return StmtError();
5837 }
5838 }
5839
Alexey Bataev66b15b52015-08-21 11:14:16 +00005840 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5841 // If both simdlen and safelen clauses are specified, the value of the simdlen
5842 // parameter must be less than or equal to the value of the safelen parameter.
5843 OMPSafelenClause *Safelen = nullptr;
5844 OMPSimdlenClause *Simdlen = nullptr;
5845 for (auto *Clause : Clauses) {
5846 if (Clause->getClauseKind() == OMPC_safelen)
5847 Safelen = cast<OMPSafelenClause>(Clause);
5848 else if (Clause->getClauseKind() == OMPC_simdlen)
5849 Simdlen = cast<OMPSimdlenClause>(Clause);
5850 if (Safelen && Simdlen)
5851 break;
5852 }
5853 if (Simdlen && Safelen &&
5854 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5855 Safelen->getSafelen()))
5856 return StmtError();
5857
Alexander Musmane4e893b2014-09-23 09:33:00 +00005858 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005859 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005860 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005861}
5862
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005863StmtResult
5864Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5865 Stmt *AStmt, SourceLocation StartLoc,
5866 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005867 if (!AStmt)
5868 return StmtError();
5869
5870 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005871 auto BaseStmt = AStmt;
5872 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5873 BaseStmt = CS->getCapturedStmt();
5874 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5875 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005876 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005877 return StmtError();
5878 // All associated statements must be '#pragma omp section' except for
5879 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005880 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005881 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5882 if (SectionStmt)
5883 Diag(SectionStmt->getLocStart(),
5884 diag::err_omp_parallel_sections_substmt_not_section);
5885 return StmtError();
5886 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005887 cast<OMPSectionDirective>(SectionStmt)
5888 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005889 }
5890 } else {
5891 Diag(AStmt->getLocStart(),
5892 diag::err_omp_parallel_sections_not_compound_stmt);
5893 return StmtError();
5894 }
5895
5896 getCurFunction()->setHasBranchProtectedScope();
5897
Alexey Bataev25e5b442015-09-15 12:52:43 +00005898 return OMPParallelSectionsDirective::Create(
5899 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005900}
5901
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005902StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5903 Stmt *AStmt, SourceLocation StartLoc,
5904 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005905 if (!AStmt)
5906 return StmtError();
5907
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005908 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5909 // 1.2.2 OpenMP Language Terminology
5910 // Structured block - An executable statement with a single entry at the
5911 // top and a single exit at the bottom.
5912 // The point of exit cannot be a branch out of the structured block.
5913 // longjmp() and throw() must not violate the entry/exit criteria.
5914 CS->getCapturedDecl()->setNothrow();
5915
5916 getCurFunction()->setHasBranchProtectedScope();
5917
Alexey Bataev25e5b442015-09-15 12:52:43 +00005918 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5919 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005920}
5921
Alexey Bataev68446b72014-07-18 07:47:19 +00005922StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5923 SourceLocation EndLoc) {
5924 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5925}
5926
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005927StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5928 SourceLocation EndLoc) {
5929 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5930}
5931
Alexey Bataev2df347a2014-07-18 10:17:07 +00005932StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5933 SourceLocation EndLoc) {
5934 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5935}
5936
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005937StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5938 SourceLocation StartLoc,
5939 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005940 if (!AStmt)
5941 return StmtError();
5942
5943 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005944
5945 getCurFunction()->setHasBranchProtectedScope();
5946
5947 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5948}
5949
Alexey Bataev6125da92014-07-21 11:26:11 +00005950StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5951 SourceLocation StartLoc,
5952 SourceLocation EndLoc) {
5953 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5954 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5955}
5956
Alexey Bataev346265e2015-09-25 10:37:12 +00005957StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5958 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005959 SourceLocation StartLoc,
5960 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005961 OMPClause *DependFound = nullptr;
5962 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005963 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005964 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005965 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005966 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005967 for (auto *C : Clauses) {
5968 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5969 DependFound = C;
5970 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5971 if (DependSourceClause) {
5972 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5973 << getOpenMPDirectiveName(OMPD_ordered)
5974 << getOpenMPClauseName(OMPC_depend) << 2;
5975 ErrorFound = true;
5976 } else
5977 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005978 if (DependSinkClause) {
5979 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5980 << 0;
5981 ErrorFound = true;
5982 }
5983 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5984 if (DependSourceClause) {
5985 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5986 << 1;
5987 ErrorFound = true;
5988 }
5989 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005990 }
5991 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005992 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005993 else if (C->getClauseKind() == OMPC_simd)
5994 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005995 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005996 if (!ErrorFound && !SC &&
5997 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005998 // OpenMP [2.8.1,simd Construct, Restrictions]
5999 // An ordered construct with the simd clause is the only OpenMP construct
6000 // that can appear in the simd region.
6001 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00006002 ErrorFound = true;
6003 } else if (DependFound && (TC || SC)) {
6004 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
6005 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
6006 ErrorFound = true;
6007 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
6008 Diag(DependFound->getLocStart(),
6009 diag::err_omp_ordered_directive_without_param);
6010 ErrorFound = true;
6011 } else if (TC || Clauses.empty()) {
6012 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
6013 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
6014 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
6015 << (TC != nullptr);
6016 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
6017 ErrorFound = true;
6018 }
6019 }
6020 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006021 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00006022
6023 if (AStmt) {
6024 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6025
6026 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006027 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006028
6029 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006030}
6031
Alexey Bataev1d160b12015-03-13 12:27:31 +00006032namespace {
6033/// \brief Helper class for checking expression in 'omp atomic [update]'
6034/// construct.
6035class OpenMPAtomicUpdateChecker {
6036 /// \brief Error results for atomic update expressions.
6037 enum ExprAnalysisErrorCode {
6038 /// \brief A statement is not an expression statement.
6039 NotAnExpression,
6040 /// \brief Expression is not builtin binary or unary operation.
6041 NotABinaryOrUnaryExpression,
6042 /// \brief Unary operation is not post-/pre- increment/decrement operation.
6043 NotAnUnaryIncDecExpression,
6044 /// \brief An expression is not of scalar type.
6045 NotAScalarType,
6046 /// \brief A binary operation is not an assignment operation.
6047 NotAnAssignmentOp,
6048 /// \brief RHS part of the binary operation is not a binary expression.
6049 NotABinaryExpression,
6050 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
6051 /// expression.
6052 NotABinaryOperator,
6053 /// \brief RHS binary operation does not have reference to the updated LHS
6054 /// part.
6055 NotAnUpdateExpression,
6056 /// \brief No errors is found.
6057 NoError
6058 };
6059 /// \brief Reference to Sema.
6060 Sema &SemaRef;
6061 /// \brief A location for note diagnostics (when error is found).
6062 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006063 /// \brief 'x' lvalue part of the source atomic expression.
6064 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006065 /// \brief 'expr' rvalue part of the source atomic expression.
6066 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006067 /// \brief Helper expression of the form
6068 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6069 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6070 Expr *UpdateExpr;
6071 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
6072 /// important for non-associative operations.
6073 bool IsXLHSInRHSPart;
6074 BinaryOperatorKind Op;
6075 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006076 /// \brief true if the source expression is a postfix unary operation, false
6077 /// if it is a prefix unary operation.
6078 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006079
6080public:
6081 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00006082 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00006083 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00006084 /// \brief Check specified statement that it is suitable for 'atomic update'
6085 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00006086 /// expression. If DiagId and NoteId == 0, then only check is performed
6087 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006088 /// \param DiagId Diagnostic which should be emitted if error is found.
6089 /// \param NoteId Diagnostic note for the main error message.
6090 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00006091 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006092 /// \brief Return the 'x' lvalue part of the source atomic expression.
6093 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00006094 /// \brief Return the 'expr' rvalue part of the source atomic expression.
6095 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00006096 /// \brief Return the update expression used in calculation of the updated
6097 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6098 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6099 Expr *getUpdateExpr() const { return UpdateExpr; }
6100 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
6101 /// false otherwise.
6102 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6103
Alexey Bataevb78ca832015-04-01 03:33:17 +00006104 /// \brief true if the source expression is a postfix unary operation, false
6105 /// if it is a prefix unary operation.
6106 bool isPostfixUpdate() const { return IsPostfixUpdate; }
6107
Alexey Bataev1d160b12015-03-13 12:27:31 +00006108private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00006109 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6110 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006111};
6112} // namespace
6113
6114bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6115 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6116 ExprAnalysisErrorCode ErrorFound = NoError;
6117 SourceLocation ErrorLoc, NoteLoc;
6118 SourceRange ErrorRange, NoteRange;
6119 // Allowed constructs are:
6120 // x = x binop expr;
6121 // x = expr binop x;
6122 if (AtomicBinOp->getOpcode() == BO_Assign) {
6123 X = AtomicBinOp->getLHS();
6124 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
6125 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6126 if (AtomicInnerBinOp->isMultiplicativeOp() ||
6127 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6128 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006129 Op = AtomicInnerBinOp->getOpcode();
6130 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006131 auto *LHS = AtomicInnerBinOp->getLHS();
6132 auto *RHS = AtomicInnerBinOp->getRHS();
6133 llvm::FoldingSetNodeID XId, LHSId, RHSId;
6134 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6135 /*Canonical=*/true);
6136 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6137 /*Canonical=*/true);
6138 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6139 /*Canonical=*/true);
6140 if (XId == LHSId) {
6141 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006142 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006143 } else if (XId == RHSId) {
6144 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006145 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006146 } else {
6147 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6148 ErrorRange = AtomicInnerBinOp->getSourceRange();
6149 NoteLoc = X->getExprLoc();
6150 NoteRange = X->getSourceRange();
6151 ErrorFound = NotAnUpdateExpression;
6152 }
6153 } else {
6154 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6155 ErrorRange = AtomicInnerBinOp->getSourceRange();
6156 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6157 NoteRange = SourceRange(NoteLoc, NoteLoc);
6158 ErrorFound = NotABinaryOperator;
6159 }
6160 } else {
6161 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6162 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6163 ErrorFound = NotABinaryExpression;
6164 }
6165 } else {
6166 ErrorLoc = AtomicBinOp->getExprLoc();
6167 ErrorRange = AtomicBinOp->getSourceRange();
6168 NoteLoc = AtomicBinOp->getOperatorLoc();
6169 NoteRange = SourceRange(NoteLoc, NoteLoc);
6170 ErrorFound = NotAnAssignmentOp;
6171 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006172 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006173 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6174 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6175 return true;
6176 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006177 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006178 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006179}
6180
6181bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6182 unsigned NoteId) {
6183 ExprAnalysisErrorCode ErrorFound = NoError;
6184 SourceLocation ErrorLoc, NoteLoc;
6185 SourceRange ErrorRange, NoteRange;
6186 // Allowed constructs are:
6187 // x++;
6188 // x--;
6189 // ++x;
6190 // --x;
6191 // x binop= expr;
6192 // x = x binop expr;
6193 // x = expr binop x;
6194 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6195 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6196 if (AtomicBody->getType()->isScalarType() ||
6197 AtomicBody->isInstantiationDependent()) {
6198 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
6199 AtomicBody->IgnoreParenImpCasts())) {
6200 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006201 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006202 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006203 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006204 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006205 X = AtomicCompAssignOp->getLHS();
6206 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006207 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6208 AtomicBody->IgnoreParenImpCasts())) {
6209 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006210 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
6211 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006212 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00006213 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
6214 // Check for Unary Operation
6215 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006216 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006217 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6218 OpLoc = AtomicUnaryOp->getOperatorLoc();
6219 X = AtomicUnaryOp->getSubExpr();
6220 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6221 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006222 } else {
6223 ErrorFound = NotAnUnaryIncDecExpression;
6224 ErrorLoc = AtomicUnaryOp->getExprLoc();
6225 ErrorRange = AtomicUnaryOp->getSourceRange();
6226 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6227 NoteRange = SourceRange(NoteLoc, NoteLoc);
6228 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006229 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006230 ErrorFound = NotABinaryOrUnaryExpression;
6231 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6232 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6233 }
6234 } else {
6235 ErrorFound = NotAScalarType;
6236 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
6237 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6238 }
6239 } else {
6240 ErrorFound = NotAnExpression;
6241 NoteLoc = ErrorLoc = S->getLocStart();
6242 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6243 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006244 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006245 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6246 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6247 return true;
6248 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006249 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006250 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006251 // Build an update expression of form 'OpaqueValueExpr(x) binop
6252 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6253 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6254 auto *OVEX = new (SemaRef.getASTContext())
6255 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6256 auto *OVEExpr = new (SemaRef.getASTContext())
6257 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
6258 auto Update =
6259 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6260 IsXLHSInRHSPart ? OVEExpr : OVEX);
6261 if (Update.isInvalid())
6262 return true;
6263 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6264 Sema::AA_Casting);
6265 if (Update.isInvalid())
6266 return true;
6267 UpdateExpr = Update.get();
6268 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006269 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006270}
6271
Alexey Bataev0162e452014-07-22 10:10:35 +00006272StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6273 Stmt *AStmt,
6274 SourceLocation StartLoc,
6275 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006276 if (!AStmt)
6277 return StmtError();
6278
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006279 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006280 // 1.2.2 OpenMP Language Terminology
6281 // Structured block - An executable statement with a single entry at the
6282 // top and a single exit at the bottom.
6283 // The point of exit cannot be a branch out of the structured block.
6284 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006285 OpenMPClauseKind AtomicKind = OMPC_unknown;
6286 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006287 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006288 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006289 C->getClauseKind() == OMPC_update ||
6290 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006291 if (AtomicKind != OMPC_unknown) {
6292 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
6293 << SourceRange(C->getLocStart(), C->getLocEnd());
6294 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6295 << getOpenMPClauseName(AtomicKind);
6296 } else {
6297 AtomicKind = C->getClauseKind();
6298 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006299 }
6300 }
6301 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006302
Alexey Bataev459dec02014-07-24 06:46:57 +00006303 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006304 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6305 Body = EWC->getSubExpr();
6306
Alexey Bataev62cec442014-11-18 10:14:22 +00006307 Expr *X = nullptr;
6308 Expr *V = nullptr;
6309 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006310 Expr *UE = nullptr;
6311 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006312 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006313 // OpenMP [2.12.6, atomic Construct]
6314 // In the next expressions:
6315 // * x and v (as applicable) are both l-value expressions with scalar type.
6316 // * During the execution of an atomic region, multiple syntactic
6317 // occurrences of x must designate the same storage location.
6318 // * Neither of v and expr (as applicable) may access the storage location
6319 // designated by x.
6320 // * Neither of x and expr (as applicable) may access the storage location
6321 // designated by v.
6322 // * expr is an expression with scalar type.
6323 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6324 // * binop, binop=, ++, and -- are not overloaded operators.
6325 // * The expression x binop expr must be numerically equivalent to x binop
6326 // (expr). This requirement is satisfied if the operators in expr have
6327 // precedence greater than binop, or by using parentheses around expr or
6328 // subexpressions of expr.
6329 // * The expression expr binop x must be numerically equivalent to (expr)
6330 // binop x. This requirement is satisfied if the operators in expr have
6331 // precedence equal to or greater than binop, or by using parentheses around
6332 // expr or subexpressions of expr.
6333 // * For forms that allow multiple occurrences of x, the number of times
6334 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006335 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006336 enum {
6337 NotAnExpression,
6338 NotAnAssignmentOp,
6339 NotAScalarType,
6340 NotAnLValue,
6341 NoError
6342 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006343 SourceLocation ErrorLoc, NoteLoc;
6344 SourceRange ErrorRange, NoteRange;
6345 // If clause is read:
6346 // v = x;
6347 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
6348 auto AtomicBinOp =
6349 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6350 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6351 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6352 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6353 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6354 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6355 if (!X->isLValue() || !V->isLValue()) {
6356 auto NotLValueExpr = X->isLValue() ? V : X;
6357 ErrorFound = NotAnLValue;
6358 ErrorLoc = AtomicBinOp->getExprLoc();
6359 ErrorRange = AtomicBinOp->getSourceRange();
6360 NoteLoc = NotLValueExpr->getExprLoc();
6361 NoteRange = NotLValueExpr->getSourceRange();
6362 }
6363 } else if (!X->isInstantiationDependent() ||
6364 !V->isInstantiationDependent()) {
6365 auto NotScalarExpr =
6366 (X->isInstantiationDependent() || X->getType()->isScalarType())
6367 ? V
6368 : X;
6369 ErrorFound = NotAScalarType;
6370 ErrorLoc = AtomicBinOp->getExprLoc();
6371 ErrorRange = AtomicBinOp->getSourceRange();
6372 NoteLoc = NotScalarExpr->getExprLoc();
6373 NoteRange = NotScalarExpr->getSourceRange();
6374 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006375 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006376 ErrorFound = NotAnAssignmentOp;
6377 ErrorLoc = AtomicBody->getExprLoc();
6378 ErrorRange = AtomicBody->getSourceRange();
6379 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6380 : AtomicBody->getExprLoc();
6381 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6382 : AtomicBody->getSourceRange();
6383 }
6384 } else {
6385 ErrorFound = NotAnExpression;
6386 NoteLoc = ErrorLoc = Body->getLocStart();
6387 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006388 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006389 if (ErrorFound != NoError) {
6390 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6391 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006392 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6393 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006394 return StmtError();
6395 } else if (CurContext->isDependentContext())
6396 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006397 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006398 enum {
6399 NotAnExpression,
6400 NotAnAssignmentOp,
6401 NotAScalarType,
6402 NotAnLValue,
6403 NoError
6404 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006405 SourceLocation ErrorLoc, NoteLoc;
6406 SourceRange ErrorRange, NoteRange;
6407 // If clause is write:
6408 // x = expr;
6409 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
6410 auto AtomicBinOp =
6411 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6412 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006413 X = AtomicBinOp->getLHS();
6414 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006415 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6416 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6417 if (!X->isLValue()) {
6418 ErrorFound = NotAnLValue;
6419 ErrorLoc = AtomicBinOp->getExprLoc();
6420 ErrorRange = AtomicBinOp->getSourceRange();
6421 NoteLoc = X->getExprLoc();
6422 NoteRange = X->getSourceRange();
6423 }
6424 } else if (!X->isInstantiationDependent() ||
6425 !E->isInstantiationDependent()) {
6426 auto NotScalarExpr =
6427 (X->isInstantiationDependent() || X->getType()->isScalarType())
6428 ? E
6429 : X;
6430 ErrorFound = NotAScalarType;
6431 ErrorLoc = AtomicBinOp->getExprLoc();
6432 ErrorRange = AtomicBinOp->getSourceRange();
6433 NoteLoc = NotScalarExpr->getExprLoc();
6434 NoteRange = NotScalarExpr->getSourceRange();
6435 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006436 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006437 ErrorFound = NotAnAssignmentOp;
6438 ErrorLoc = AtomicBody->getExprLoc();
6439 ErrorRange = AtomicBody->getSourceRange();
6440 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6441 : AtomicBody->getExprLoc();
6442 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6443 : AtomicBody->getSourceRange();
6444 }
6445 } else {
6446 ErrorFound = NotAnExpression;
6447 NoteLoc = ErrorLoc = Body->getLocStart();
6448 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006449 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006450 if (ErrorFound != NoError) {
6451 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6452 << ErrorRange;
6453 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6454 << NoteRange;
6455 return StmtError();
6456 } else if (CurContext->isDependentContext())
6457 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006458 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006459 // If clause is update:
6460 // x++;
6461 // x--;
6462 // ++x;
6463 // --x;
6464 // x binop= expr;
6465 // x = x binop expr;
6466 // x = expr binop x;
6467 OpenMPAtomicUpdateChecker Checker(*this);
6468 if (Checker.checkStatement(
6469 Body, (AtomicKind == OMPC_update)
6470 ? diag::err_omp_atomic_update_not_expression_statement
6471 : diag::err_omp_atomic_not_expression_statement,
6472 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006473 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006474 if (!CurContext->isDependentContext()) {
6475 E = Checker.getExpr();
6476 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006477 UE = Checker.getUpdateExpr();
6478 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006479 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006480 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006481 enum {
6482 NotAnAssignmentOp,
6483 NotACompoundStatement,
6484 NotTwoSubstatements,
6485 NotASpecificExpression,
6486 NoError
6487 } ErrorFound = NoError;
6488 SourceLocation ErrorLoc, NoteLoc;
6489 SourceRange ErrorRange, NoteRange;
6490 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6491 // If clause is a capture:
6492 // v = x++;
6493 // v = x--;
6494 // v = ++x;
6495 // v = --x;
6496 // v = x binop= expr;
6497 // v = x = x binop expr;
6498 // v = x = expr binop x;
6499 auto *AtomicBinOp =
6500 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6501 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6502 V = AtomicBinOp->getLHS();
6503 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6504 OpenMPAtomicUpdateChecker Checker(*this);
6505 if (Checker.checkStatement(
6506 Body, diag::err_omp_atomic_capture_not_expression_statement,
6507 diag::note_omp_atomic_update))
6508 return StmtError();
6509 E = Checker.getExpr();
6510 X = Checker.getX();
6511 UE = Checker.getUpdateExpr();
6512 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6513 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006514 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006515 ErrorLoc = AtomicBody->getExprLoc();
6516 ErrorRange = AtomicBody->getSourceRange();
6517 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6518 : AtomicBody->getExprLoc();
6519 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6520 : AtomicBody->getSourceRange();
6521 ErrorFound = NotAnAssignmentOp;
6522 }
6523 if (ErrorFound != NoError) {
6524 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6525 << ErrorRange;
6526 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6527 return StmtError();
6528 } else if (CurContext->isDependentContext()) {
6529 UE = V = E = X = nullptr;
6530 }
6531 } else {
6532 // If clause is a capture:
6533 // { v = x; x = expr; }
6534 // { v = x; x++; }
6535 // { v = x; x--; }
6536 // { v = x; ++x; }
6537 // { v = x; --x; }
6538 // { v = x; x binop= expr; }
6539 // { v = x; x = x binop expr; }
6540 // { v = x; x = expr binop x; }
6541 // { x++; v = x; }
6542 // { x--; v = x; }
6543 // { ++x; v = x; }
6544 // { --x; v = x; }
6545 // { x binop= expr; v = x; }
6546 // { x = x binop expr; v = x; }
6547 // { x = expr binop x; v = x; }
6548 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6549 // Check that this is { expr1; expr2; }
6550 if (CS->size() == 2) {
6551 auto *First = CS->body_front();
6552 auto *Second = CS->body_back();
6553 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6554 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6555 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6556 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6557 // Need to find what subexpression is 'v' and what is 'x'.
6558 OpenMPAtomicUpdateChecker Checker(*this);
6559 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6560 BinaryOperator *BinOp = nullptr;
6561 if (IsUpdateExprFound) {
6562 BinOp = dyn_cast<BinaryOperator>(First);
6563 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6564 }
6565 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6566 // { v = x; x++; }
6567 // { v = x; x--; }
6568 // { v = x; ++x; }
6569 // { v = x; --x; }
6570 // { v = x; x binop= expr; }
6571 // { v = x; x = x binop expr; }
6572 // { v = x; x = expr binop x; }
6573 // Check that the first expression has form v = x.
6574 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6575 llvm::FoldingSetNodeID XId, PossibleXId;
6576 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6577 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6578 IsUpdateExprFound = XId == PossibleXId;
6579 if (IsUpdateExprFound) {
6580 V = BinOp->getLHS();
6581 X = Checker.getX();
6582 E = Checker.getExpr();
6583 UE = Checker.getUpdateExpr();
6584 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006585 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006586 }
6587 }
6588 if (!IsUpdateExprFound) {
6589 IsUpdateExprFound = !Checker.checkStatement(First);
6590 BinOp = nullptr;
6591 if (IsUpdateExprFound) {
6592 BinOp = dyn_cast<BinaryOperator>(Second);
6593 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6594 }
6595 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6596 // { x++; v = x; }
6597 // { x--; v = x; }
6598 // { ++x; v = x; }
6599 // { --x; v = x; }
6600 // { x binop= expr; v = x; }
6601 // { x = x binop expr; v = x; }
6602 // { x = expr binop x; v = x; }
6603 // Check that the second expression has form v = x.
6604 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6605 llvm::FoldingSetNodeID XId, PossibleXId;
6606 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6607 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6608 IsUpdateExprFound = XId == PossibleXId;
6609 if (IsUpdateExprFound) {
6610 V = BinOp->getLHS();
6611 X = Checker.getX();
6612 E = Checker.getExpr();
6613 UE = Checker.getUpdateExpr();
6614 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006615 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006616 }
6617 }
6618 }
6619 if (!IsUpdateExprFound) {
6620 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006621 auto *FirstExpr = dyn_cast<Expr>(First);
6622 auto *SecondExpr = dyn_cast<Expr>(Second);
6623 if (!FirstExpr || !SecondExpr ||
6624 !(FirstExpr->isInstantiationDependent() ||
6625 SecondExpr->isInstantiationDependent())) {
6626 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6627 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006628 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006629 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6630 : First->getLocStart();
6631 NoteRange = ErrorRange = FirstBinOp
6632 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006633 : SourceRange(ErrorLoc, ErrorLoc);
6634 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006635 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6636 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6637 ErrorFound = NotAnAssignmentOp;
6638 NoteLoc = ErrorLoc = SecondBinOp
6639 ? SecondBinOp->getOperatorLoc()
6640 : Second->getLocStart();
6641 NoteRange = ErrorRange =
6642 SecondBinOp ? SecondBinOp->getSourceRange()
6643 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006644 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006645 auto *PossibleXRHSInFirst =
6646 FirstBinOp->getRHS()->IgnoreParenImpCasts();
6647 auto *PossibleXLHSInSecond =
6648 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6649 llvm::FoldingSetNodeID X1Id, X2Id;
6650 PossibleXRHSInFirst->Profile(X1Id, Context,
6651 /*Canonical=*/true);
6652 PossibleXLHSInSecond->Profile(X2Id, Context,
6653 /*Canonical=*/true);
6654 IsUpdateExprFound = X1Id == X2Id;
6655 if (IsUpdateExprFound) {
6656 V = FirstBinOp->getLHS();
6657 X = SecondBinOp->getLHS();
6658 E = SecondBinOp->getRHS();
6659 UE = nullptr;
6660 IsXLHSInRHSPart = false;
6661 IsPostfixUpdate = true;
6662 } else {
6663 ErrorFound = NotASpecificExpression;
6664 ErrorLoc = FirstBinOp->getExprLoc();
6665 ErrorRange = FirstBinOp->getSourceRange();
6666 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6667 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6668 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006669 }
6670 }
6671 }
6672 }
6673 } else {
6674 NoteLoc = ErrorLoc = Body->getLocStart();
6675 NoteRange = ErrorRange =
6676 SourceRange(Body->getLocStart(), Body->getLocStart());
6677 ErrorFound = NotTwoSubstatements;
6678 }
6679 } else {
6680 NoteLoc = ErrorLoc = Body->getLocStart();
6681 NoteRange = ErrorRange =
6682 SourceRange(Body->getLocStart(), Body->getLocStart());
6683 ErrorFound = NotACompoundStatement;
6684 }
6685 if (ErrorFound != NoError) {
6686 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6687 << ErrorRange;
6688 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6689 return StmtError();
6690 } else if (CurContext->isDependentContext()) {
6691 UE = V = E = X = nullptr;
6692 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006693 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006694 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006695
6696 getCurFunction()->setHasBranchProtectedScope();
6697
Alexey Bataev62cec442014-11-18 10:14:22 +00006698 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006699 X, V, E, UE, IsXLHSInRHSPart,
6700 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006701}
6702
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006703StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6704 Stmt *AStmt,
6705 SourceLocation StartLoc,
6706 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006707 if (!AStmt)
6708 return StmtError();
6709
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006710 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6711 // 1.2.2 OpenMP Language Terminology
6712 // Structured block - An executable statement with a single entry at the
6713 // top and a single exit at the bottom.
6714 // The point of exit cannot be a branch out of the structured block.
6715 // longjmp() and throw() must not violate the entry/exit criteria.
6716 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006717
Alexey Bataev13314bf2014-10-09 04:18:56 +00006718 // OpenMP [2.16, Nesting of Regions]
6719 // If specified, a teams construct must be contained within a target
6720 // construct. That target construct must contain no statements or directives
6721 // outside of the teams construct.
6722 if (DSAStack->hasInnerTeamsRegion()) {
6723 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6724 bool OMPTeamsFound = true;
6725 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6726 auto I = CS->body_begin();
6727 while (I != CS->body_end()) {
6728 auto OED = dyn_cast<OMPExecutableDirective>(*I);
6729 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6730 OMPTeamsFound = false;
6731 break;
6732 }
6733 ++I;
6734 }
6735 assert(I != CS->body_end() && "Not found statement");
6736 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00006737 } else {
6738 auto *OED = dyn_cast<OMPExecutableDirective>(S);
6739 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00006740 }
6741 if (!OMPTeamsFound) {
6742 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6743 Diag(DSAStack->getInnerTeamsRegionLoc(),
6744 diag::note_omp_nested_teams_construct_here);
6745 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6746 << isa<OMPExecutableDirective>(S);
6747 return StmtError();
6748 }
6749 }
6750
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006751 getCurFunction()->setHasBranchProtectedScope();
6752
6753 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6754}
6755
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006756StmtResult
6757Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6758 Stmt *AStmt, SourceLocation StartLoc,
6759 SourceLocation EndLoc) {
6760 if (!AStmt)
6761 return StmtError();
6762
6763 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6764 // 1.2.2 OpenMP Language Terminology
6765 // Structured block - An executable statement with a single entry at the
6766 // top and a single exit at the bottom.
6767 // The point of exit cannot be a branch out of the structured block.
6768 // longjmp() and throw() must not violate the entry/exit criteria.
6769 CS->getCapturedDecl()->setNothrow();
6770
6771 getCurFunction()->setHasBranchProtectedScope();
6772
6773 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6774 AStmt);
6775}
6776
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006777StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6778 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6779 SourceLocation EndLoc,
6780 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6781 if (!AStmt)
6782 return StmtError();
6783
6784 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6785 // 1.2.2 OpenMP Language Terminology
6786 // Structured block - An executable statement with a single entry at the
6787 // top and a single exit at the bottom.
6788 // The point of exit cannot be a branch out of the structured block.
6789 // longjmp() and throw() must not violate the entry/exit criteria.
6790 CS->getCapturedDecl()->setNothrow();
6791
6792 OMPLoopDirective::HelperExprs B;
6793 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6794 // define the nested loops number.
6795 unsigned NestedLoopCount =
6796 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6797 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6798 VarsWithImplicitDSA, B);
6799 if (NestedLoopCount == 0)
6800 return StmtError();
6801
6802 assert((CurContext->isDependentContext() || B.builtAll()) &&
6803 "omp target parallel for loop exprs were not built");
6804
6805 if (!CurContext->isDependentContext()) {
6806 // Finalize the clauses that need pre-built expressions for CodeGen.
6807 for (auto C : Clauses) {
6808 if (auto LC = dyn_cast<OMPLinearClause>(C))
6809 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006810 B.NumIterations, *this, CurScope,
6811 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006812 return StmtError();
6813 }
6814 }
6815
6816 getCurFunction()->setHasBranchProtectedScope();
6817 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6818 NestedLoopCount, Clauses, AStmt,
6819 B, DSAStack->isCancelRegion());
6820}
6821
Samuel Antaodf67fc42016-01-19 19:15:56 +00006822/// \brief Check for existence of a map clause in the list of clauses.
6823static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
6824 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6825 I != E; ++I) {
6826 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6827 return true;
6828 }
6829 }
6830
6831 return false;
6832}
6833
Michael Wong65f367f2015-07-21 13:44:28 +00006834StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6835 Stmt *AStmt,
6836 SourceLocation StartLoc,
6837 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006838 if (!AStmt)
6839 return StmtError();
6840
6841 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6842
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006843 // OpenMP [2.10.1, Restrictions, p. 97]
6844 // At least one map clause must appear on the directive.
6845 if (!HasMapClause(Clauses)) {
6846 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6847 getOpenMPDirectiveName(OMPD_target_data);
6848 return StmtError();
6849 }
6850
Michael Wong65f367f2015-07-21 13:44:28 +00006851 getCurFunction()->setHasBranchProtectedScope();
6852
6853 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6854 AStmt);
6855}
6856
Samuel Antaodf67fc42016-01-19 19:15:56 +00006857StmtResult
6858Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6859 SourceLocation StartLoc,
6860 SourceLocation EndLoc) {
6861 // OpenMP [2.10.2, Restrictions, p. 99]
6862 // At least one map clause must appear on the directive.
6863 if (!HasMapClause(Clauses)) {
6864 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6865 << getOpenMPDirectiveName(OMPD_target_enter_data);
6866 return StmtError();
6867 }
6868
6869 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6870 Clauses);
6871}
6872
Samuel Antao72590762016-01-19 20:04:50 +00006873StmtResult
6874Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6875 SourceLocation StartLoc,
6876 SourceLocation EndLoc) {
6877 // OpenMP [2.10.3, Restrictions, p. 102]
6878 // At least one map clause must appear on the directive.
6879 if (!HasMapClause(Clauses)) {
6880 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6881 << getOpenMPDirectiveName(OMPD_target_exit_data);
6882 return StmtError();
6883 }
6884
6885 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6886}
6887
Samuel Antao686c70c2016-05-26 17:30:50 +00006888StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
6889 SourceLocation StartLoc,
6890 SourceLocation EndLoc) {
Samuel Antao686c70c2016-05-26 17:30:50 +00006891 bool seenMotionClause = false;
Samuel Antao661c0902016-05-26 17:39:58 +00006892 for (auto *C : Clauses) {
Samuel Antaoec172c62016-05-26 17:49:04 +00006893 if (C->getClauseKind() == OMPC_to || C->getClauseKind() == OMPC_from)
Samuel Antao661c0902016-05-26 17:39:58 +00006894 seenMotionClause = true;
6895 }
Samuel Antao686c70c2016-05-26 17:30:50 +00006896 if (!seenMotionClause) {
6897 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
6898 return StmtError();
6899 }
6900 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses);
6901}
6902
Alexey Bataev13314bf2014-10-09 04:18:56 +00006903StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6904 Stmt *AStmt, SourceLocation StartLoc,
6905 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006906 if (!AStmt)
6907 return StmtError();
6908
Alexey Bataev13314bf2014-10-09 04:18:56 +00006909 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6910 // 1.2.2 OpenMP Language Terminology
6911 // Structured block - An executable statement with a single entry at the
6912 // top and a single exit at the bottom.
6913 // The point of exit cannot be a branch out of the structured block.
6914 // longjmp() and throw() must not violate the entry/exit criteria.
6915 CS->getCapturedDecl()->setNothrow();
6916
6917 getCurFunction()->setHasBranchProtectedScope();
6918
6919 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6920}
6921
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006922StmtResult
6923Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6924 SourceLocation EndLoc,
6925 OpenMPDirectiveKind CancelRegion) {
6926 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6927 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6928 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6929 << getOpenMPDirectiveName(CancelRegion);
6930 return StmtError();
6931 }
6932 if (DSAStack->isParentNowaitRegion()) {
6933 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6934 return StmtError();
6935 }
6936 if (DSAStack->isParentOrderedRegion()) {
6937 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6938 return StmtError();
6939 }
6940 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6941 CancelRegion);
6942}
6943
Alexey Bataev87933c72015-09-18 08:07:34 +00006944StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6945 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006946 SourceLocation EndLoc,
6947 OpenMPDirectiveKind CancelRegion) {
6948 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6949 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6950 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6951 << getOpenMPDirectiveName(CancelRegion);
6952 return StmtError();
6953 }
6954 if (DSAStack->isParentNowaitRegion()) {
6955 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6956 return StmtError();
6957 }
6958 if (DSAStack->isParentOrderedRegion()) {
6959 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6960 return StmtError();
6961 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006962 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006963 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6964 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006965}
6966
Alexey Bataev382967a2015-12-08 12:06:20 +00006967static bool checkGrainsizeNumTasksClauses(Sema &S,
6968 ArrayRef<OMPClause *> Clauses) {
6969 OMPClause *PrevClause = nullptr;
6970 bool ErrorFound = false;
6971 for (auto *C : Clauses) {
6972 if (C->getClauseKind() == OMPC_grainsize ||
6973 C->getClauseKind() == OMPC_num_tasks) {
6974 if (!PrevClause)
6975 PrevClause = C;
6976 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6977 S.Diag(C->getLocStart(),
6978 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6979 << getOpenMPClauseName(C->getClauseKind())
6980 << getOpenMPClauseName(PrevClause->getClauseKind());
6981 S.Diag(PrevClause->getLocStart(),
6982 diag::note_omp_previous_grainsize_num_tasks)
6983 << getOpenMPClauseName(PrevClause->getClauseKind());
6984 ErrorFound = true;
6985 }
6986 }
6987 }
6988 return ErrorFound;
6989}
6990
Alexey Bataev49f6e782015-12-01 04:18:41 +00006991StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6992 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6993 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006994 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006995 if (!AStmt)
6996 return StmtError();
6997
6998 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6999 OMPLoopDirective::HelperExprs B;
7000 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7001 // define the nested loops number.
7002 unsigned NestedLoopCount =
7003 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007004 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00007005 VarsWithImplicitDSA, B);
7006 if (NestedLoopCount == 0)
7007 return StmtError();
7008
7009 assert((CurContext->isDependentContext() || B.builtAll()) &&
7010 "omp for loop exprs were not built");
7011
Alexey Bataev382967a2015-12-08 12:06:20 +00007012 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7013 // The grainsize clause and num_tasks clause are mutually exclusive and may
7014 // not appear on the same taskloop directive.
7015 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7016 return StmtError();
7017
Alexey Bataev49f6e782015-12-01 04:18:41 +00007018 getCurFunction()->setHasBranchProtectedScope();
7019 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7020 NestedLoopCount, Clauses, AStmt, B);
7021}
7022
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007023StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7024 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7025 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007026 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007027 if (!AStmt)
7028 return StmtError();
7029
7030 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7031 OMPLoopDirective::HelperExprs B;
7032 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7033 // define the nested loops number.
7034 unsigned NestedLoopCount =
7035 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
7036 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7037 VarsWithImplicitDSA, B);
7038 if (NestedLoopCount == 0)
7039 return StmtError();
7040
7041 assert((CurContext->isDependentContext() || B.builtAll()) &&
7042 "omp for loop exprs were not built");
7043
Alexey Bataev5a3af132016-03-29 08:58:54 +00007044 if (!CurContext->isDependentContext()) {
7045 // Finalize the clauses that need pre-built expressions for CodeGen.
7046 for (auto C : Clauses) {
7047 if (auto LC = dyn_cast<OMPLinearClause>(C))
7048 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007049 B.NumIterations, *this, CurScope,
7050 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007051 return StmtError();
7052 }
7053 }
7054
Alexey Bataev382967a2015-12-08 12:06:20 +00007055 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7056 // The grainsize clause and num_tasks clause are mutually exclusive and may
7057 // not appear on the same taskloop directive.
7058 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7059 return StmtError();
7060
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007061 getCurFunction()->setHasBranchProtectedScope();
7062 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7063 NestedLoopCount, Clauses, AStmt, B);
7064}
7065
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007066StmtResult Sema::ActOnOpenMPDistributeDirective(
7067 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7068 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007069 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007070 if (!AStmt)
7071 return StmtError();
7072
7073 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7074 OMPLoopDirective::HelperExprs B;
7075 // In presence of clause 'collapse' with number of loops, it will
7076 // define the nested loops number.
7077 unsigned NestedLoopCount =
7078 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
7079 nullptr /*ordered not a clause on distribute*/, AStmt,
7080 *this, *DSAStack, VarsWithImplicitDSA, B);
7081 if (NestedLoopCount == 0)
7082 return StmtError();
7083
7084 assert((CurContext->isDependentContext() || B.builtAll()) &&
7085 "omp for loop exprs were not built");
7086
7087 getCurFunction()->setHasBranchProtectedScope();
7088 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7089 NestedLoopCount, Clauses, AStmt, B);
7090}
7091
Carlo Bertolli9925f152016-06-27 14:55:37 +00007092StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7093 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7094 SourceLocation EndLoc,
7095 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7096 if (!AStmt)
7097 return StmtError();
7098
7099 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7100 // 1.2.2 OpenMP Language Terminology
7101 // Structured block - An executable statement with a single entry at the
7102 // top and a single exit at the bottom.
7103 // The point of exit cannot be a branch out of the structured block.
7104 // longjmp() and throw() must not violate the entry/exit criteria.
7105 CS->getCapturedDecl()->setNothrow();
7106
7107 OMPLoopDirective::HelperExprs B;
7108 // In presence of clause 'collapse' with number of loops, it will
7109 // define the nested loops number.
7110 unsigned NestedLoopCount = CheckOpenMPLoop(
7111 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7112 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7113 VarsWithImplicitDSA, B);
7114 if (NestedLoopCount == 0)
7115 return StmtError();
7116
7117 assert((CurContext->isDependentContext() || B.builtAll()) &&
7118 "omp for loop exprs were not built");
7119
7120 getCurFunction()->setHasBranchProtectedScope();
7121 return OMPDistributeParallelForDirective::Create(
7122 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7123}
7124
Kelvin Li4a39add2016-07-05 05:00:15 +00007125StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7126 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7127 SourceLocation EndLoc,
7128 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7129 if (!AStmt)
7130 return StmtError();
7131
7132 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7133 // 1.2.2 OpenMP Language Terminology
7134 // Structured block - An executable statement with a single entry at the
7135 // top and a single exit at the bottom.
7136 // The point of exit cannot be a branch out of the structured block.
7137 // longjmp() and throw() must not violate the entry/exit criteria.
7138 CS->getCapturedDecl()->setNothrow();
7139
7140 OMPLoopDirective::HelperExprs B;
7141 // In presence of clause 'collapse' with number of loops, it will
7142 // define the nested loops number.
7143 unsigned NestedLoopCount = CheckOpenMPLoop(
7144 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
7145 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7146 VarsWithImplicitDSA, B);
7147 if (NestedLoopCount == 0)
7148 return StmtError();
7149
7150 assert((CurContext->isDependentContext() || B.builtAll()) &&
7151 "omp for loop exprs were not built");
7152
7153 getCurFunction()->setHasBranchProtectedScope();
7154 return OMPDistributeParallelForSimdDirective::Create(
7155 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7156}
7157
Kelvin Li787f3fc2016-07-06 04:45:38 +00007158StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7159 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7160 SourceLocation EndLoc,
7161 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7162 if (!AStmt)
7163 return StmtError();
7164
7165 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7166 // 1.2.2 OpenMP Language Terminology
7167 // Structured block - An executable statement with a single entry at the
7168 // top and a single exit at the bottom.
7169 // The point of exit cannot be a branch out of the structured block.
7170 // longjmp() and throw() must not violate the entry/exit criteria.
7171 CS->getCapturedDecl()->setNothrow();
7172
7173 OMPLoopDirective::HelperExprs B;
7174 // In presence of clause 'collapse' with number of loops, it will
7175 // define the nested loops number.
7176 unsigned NestedLoopCount =
7177 CheckOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
7178 nullptr /*ordered not a clause on distribute*/, AStmt,
7179 *this, *DSAStack, VarsWithImplicitDSA, B);
7180 if (NestedLoopCount == 0)
7181 return StmtError();
7182
7183 assert((CurContext->isDependentContext() || B.builtAll()) &&
7184 "omp for loop exprs were not built");
7185
7186 getCurFunction()->setHasBranchProtectedScope();
7187 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7188 NestedLoopCount, Clauses, AStmt, B);
7189}
7190
Alexey Bataeved09d242014-05-28 05:53:51 +00007191OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007192 SourceLocation StartLoc,
7193 SourceLocation LParenLoc,
7194 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007195 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007196 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00007197 case OMPC_final:
7198 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
7199 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00007200 case OMPC_num_threads:
7201 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
7202 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007203 case OMPC_safelen:
7204 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
7205 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00007206 case OMPC_simdlen:
7207 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
7208 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00007209 case OMPC_collapse:
7210 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
7211 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007212 case OMPC_ordered:
7213 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
7214 break;
Michael Wonge710d542015-08-07 16:16:36 +00007215 case OMPC_device:
7216 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
7217 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00007218 case OMPC_num_teams:
7219 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
7220 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007221 case OMPC_thread_limit:
7222 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
7223 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00007224 case OMPC_priority:
7225 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
7226 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007227 case OMPC_grainsize:
7228 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
7229 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00007230 case OMPC_num_tasks:
7231 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
7232 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00007233 case OMPC_hint:
7234 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
7235 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007236 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007237 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007238 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007239 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007240 case OMPC_private:
7241 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007242 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007243 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007244 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007245 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007246 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007247 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007248 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00007249 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007250 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007251 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007252 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007253 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007254 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007255 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007256 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007257 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007258 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007259 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00007260 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007261 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007262 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00007263 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007264 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007265 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007266 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007267 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007268 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007269 case OMPC_from:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007270 llvm_unreachable("Clause is not allowed.");
7271 }
7272 return Res;
7273}
7274
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007275OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
7276 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007277 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007278 SourceLocation NameModifierLoc,
7279 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007280 SourceLocation EndLoc) {
7281 Expr *ValExpr = Condition;
7282 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7283 !Condition->isInstantiationDependent() &&
7284 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007285 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007286 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007287 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007288
Richard Smith03a4aa32016-06-23 19:02:52 +00007289 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007290 }
7291
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007292 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
7293 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007294}
7295
Alexey Bataev3778b602014-07-17 07:32:53 +00007296OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
7297 SourceLocation StartLoc,
7298 SourceLocation LParenLoc,
7299 SourceLocation EndLoc) {
7300 Expr *ValExpr = Condition;
7301 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7302 !Condition->isInstantiationDependent() &&
7303 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007304 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00007305 if (Val.isInvalid())
7306 return nullptr;
7307
Richard Smith03a4aa32016-06-23 19:02:52 +00007308 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00007309 }
7310
7311 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7312}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007313ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
7314 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00007315 if (!Op)
7316 return ExprError();
7317
7318 class IntConvertDiagnoser : public ICEConvertDiagnoser {
7319 public:
7320 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00007321 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00007322 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
7323 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007324 return S.Diag(Loc, diag::err_omp_not_integral) << T;
7325 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007326 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
7327 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007328 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
7329 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007330 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
7331 QualType T,
7332 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007333 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
7334 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007335 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
7336 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007337 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007338 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007339 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007340 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
7341 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007342 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
7343 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007344 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
7345 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007346 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007347 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007348 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007349 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
7350 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007351 llvm_unreachable("conversion functions are permitted");
7352 }
7353 } ConvertDiagnoser;
7354 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
7355}
7356
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007357static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00007358 OpenMPClauseKind CKind,
7359 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007360 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
7361 !ValExpr->isInstantiationDependent()) {
7362 SourceLocation Loc = ValExpr->getExprLoc();
7363 ExprResult Value =
7364 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
7365 if (Value.isInvalid())
7366 return false;
7367
7368 ValExpr = Value.get();
7369 // The expression must evaluate to a non-negative integer value.
7370 llvm::APSInt Result;
7371 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00007372 Result.isSigned() &&
7373 !((!StrictlyPositive && Result.isNonNegative()) ||
7374 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007375 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007376 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7377 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007378 return false;
7379 }
7380 }
7381 return true;
7382}
7383
Alexey Bataev568a8332014-03-06 06:15:19 +00007384OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
7385 SourceLocation StartLoc,
7386 SourceLocation LParenLoc,
7387 SourceLocation EndLoc) {
7388 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00007389
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007390 // OpenMP [2.5, Restrictions]
7391 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00007392 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
7393 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007394 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00007395
Alexey Bataeved09d242014-05-28 05:53:51 +00007396 return new (Context)
7397 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00007398}
7399
Alexey Bataev62c87d22014-03-21 04:51:18 +00007400ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007401 OpenMPClauseKind CKind,
7402 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007403 if (!E)
7404 return ExprError();
7405 if (E->isValueDependent() || E->isTypeDependent() ||
7406 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00007407 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007408 llvm::APSInt Result;
7409 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
7410 if (ICE.isInvalid())
7411 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007412 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
7413 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007414 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007415 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7416 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00007417 return ExprError();
7418 }
Alexander Musman09184fe2014-09-30 05:29:28 +00007419 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
7420 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
7421 << E->getSourceRange();
7422 return ExprError();
7423 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007424 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
7425 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00007426 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007427 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00007428 return ICE;
7429}
7430
7431OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
7432 SourceLocation LParenLoc,
7433 SourceLocation EndLoc) {
7434 // OpenMP [2.8.1, simd construct, Description]
7435 // The parameter of the safelen clause must be a constant
7436 // positive integer expression.
7437 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
7438 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007439 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007440 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007441 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00007442}
7443
Alexey Bataev66b15b52015-08-21 11:14:16 +00007444OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
7445 SourceLocation LParenLoc,
7446 SourceLocation EndLoc) {
7447 // OpenMP [2.8.1, simd construct, Description]
7448 // The parameter of the simdlen clause must be a constant
7449 // positive integer expression.
7450 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
7451 if (Simdlen.isInvalid())
7452 return nullptr;
7453 return new (Context)
7454 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
7455}
7456
Alexander Musman64d33f12014-06-04 07:53:32 +00007457OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
7458 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00007459 SourceLocation LParenLoc,
7460 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00007461 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007462 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00007463 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007464 // The parameter of the collapse clause must be a constant
7465 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00007466 ExprResult NumForLoopsResult =
7467 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
7468 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00007469 return nullptr;
7470 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00007471 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00007472}
7473
Alexey Bataev10e775f2015-07-30 11:36:16 +00007474OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
7475 SourceLocation EndLoc,
7476 SourceLocation LParenLoc,
7477 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00007478 // OpenMP [2.7.1, loop construct, Description]
7479 // OpenMP [2.8.1, simd construct, Description]
7480 // OpenMP [2.9.6, distribute construct, Description]
7481 // The parameter of the ordered clause must be a constant
7482 // positive integer expression if any.
7483 if (NumForLoops && LParenLoc.isValid()) {
7484 ExprResult NumForLoopsResult =
7485 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
7486 if (NumForLoopsResult.isInvalid())
7487 return nullptr;
7488 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00007489 } else
7490 NumForLoops = nullptr;
7491 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00007492 return new (Context)
7493 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
7494}
7495
Alexey Bataeved09d242014-05-28 05:53:51 +00007496OMPClause *Sema::ActOnOpenMPSimpleClause(
7497 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
7498 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007499 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007500 switch (Kind) {
7501 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007502 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00007503 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
7504 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007505 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007506 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00007507 Res = ActOnOpenMPProcBindClause(
7508 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
7509 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007510 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007511 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007512 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007513 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007514 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007515 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007516 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007517 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007518 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007519 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007520 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007521 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007522 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007523 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007524 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007525 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007526 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007527 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007528 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007529 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007530 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007531 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007532 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007533 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007534 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007535 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007536 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007537 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007538 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007539 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007540 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007541 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007542 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007543 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007544 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007545 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007546 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007547 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007548 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007549 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007550 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007551 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007552 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007553 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007554 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007555 case OMPC_from:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007556 llvm_unreachable("Clause is not allowed.");
7557 }
7558 return Res;
7559}
7560
Alexey Bataev6402bca2015-12-28 07:25:51 +00007561static std::string
7562getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
7563 ArrayRef<unsigned> Exclude = llvm::None) {
7564 std::string Values;
7565 unsigned Bound = Last >= 2 ? Last - 2 : 0;
7566 unsigned Skipped = Exclude.size();
7567 auto S = Exclude.begin(), E = Exclude.end();
7568 for (unsigned i = First; i < Last; ++i) {
7569 if (std::find(S, E, i) != E) {
7570 --Skipped;
7571 continue;
7572 }
7573 Values += "'";
7574 Values += getOpenMPSimpleClauseTypeName(K, i);
7575 Values += "'";
7576 if (i == Bound - Skipped)
7577 Values += " or ";
7578 else if (i != Bound + 1 - Skipped)
7579 Values += ", ";
7580 }
7581 return Values;
7582}
7583
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007584OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
7585 SourceLocation KindKwLoc,
7586 SourceLocation StartLoc,
7587 SourceLocation LParenLoc,
7588 SourceLocation EndLoc) {
7589 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00007590 static_assert(OMPC_DEFAULT_unknown > 0,
7591 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007592 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007593 << getListOfPossibleValues(OMPC_default, /*First=*/0,
7594 /*Last=*/OMPC_DEFAULT_unknown)
7595 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007596 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007597 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00007598 switch (Kind) {
7599 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007600 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007601 break;
7602 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007603 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007604 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007605 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007606 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00007607 break;
7608 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007609 return new (Context)
7610 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007611}
7612
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007613OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
7614 SourceLocation KindKwLoc,
7615 SourceLocation StartLoc,
7616 SourceLocation LParenLoc,
7617 SourceLocation EndLoc) {
7618 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007619 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007620 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
7621 /*Last=*/OMPC_PROC_BIND_unknown)
7622 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007623 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007624 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007625 return new (Context)
7626 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007627}
7628
Alexey Bataev56dafe82014-06-20 07:16:17 +00007629OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007630 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007631 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007632 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007633 SourceLocation EndLoc) {
7634 OMPClause *Res = nullptr;
7635 switch (Kind) {
7636 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007637 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
7638 assert(Argument.size() == NumberOfElements &&
7639 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007640 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007641 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
7642 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
7643 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
7644 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
7645 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007646 break;
7647 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007648 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
7649 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
7650 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
7651 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007652 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00007653 case OMPC_dist_schedule:
7654 Res = ActOnOpenMPDistScheduleClause(
7655 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
7656 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
7657 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007658 case OMPC_defaultmap:
7659 enum { Modifier, DefaultmapKind };
7660 Res = ActOnOpenMPDefaultmapClause(
7661 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
7662 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
7663 StartLoc, LParenLoc, ArgumentLoc[Modifier],
7664 ArgumentLoc[DefaultmapKind], EndLoc);
7665 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00007666 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007667 case OMPC_num_threads:
7668 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007669 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007670 case OMPC_collapse:
7671 case OMPC_default:
7672 case OMPC_proc_bind:
7673 case OMPC_private:
7674 case OMPC_firstprivate:
7675 case OMPC_lastprivate:
7676 case OMPC_shared:
7677 case OMPC_reduction:
7678 case OMPC_linear:
7679 case OMPC_aligned:
7680 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007681 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007682 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007683 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007684 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007685 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007686 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007687 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007688 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007689 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007690 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007691 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007692 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007693 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007694 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007695 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007696 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007697 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007698 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007699 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007700 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007701 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007702 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007703 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007704 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007705 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007706 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007707 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007708 case OMPC_from:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007709 llvm_unreachable("Clause is not allowed.");
7710 }
7711 return Res;
7712}
7713
Alexey Bataev6402bca2015-12-28 07:25:51 +00007714static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
7715 OpenMPScheduleClauseModifier M2,
7716 SourceLocation M1Loc, SourceLocation M2Loc) {
7717 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
7718 SmallVector<unsigned, 2> Excluded;
7719 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
7720 Excluded.push_back(M2);
7721 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
7722 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
7723 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
7724 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
7725 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
7726 << getListOfPossibleValues(OMPC_schedule,
7727 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
7728 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7729 Excluded)
7730 << getOpenMPClauseName(OMPC_schedule);
7731 return true;
7732 }
7733 return false;
7734}
7735
Alexey Bataev56dafe82014-06-20 07:16:17 +00007736OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007737 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007738 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007739 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
7740 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
7741 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
7742 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
7743 return nullptr;
7744 // OpenMP, 2.7.1, Loop Construct, Restrictions
7745 // Either the monotonic modifier or the nonmonotonic modifier can be specified
7746 // but not both.
7747 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
7748 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
7749 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
7750 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
7751 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
7752 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
7753 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
7754 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
7755 return nullptr;
7756 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007757 if (Kind == OMPC_SCHEDULE_unknown) {
7758 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00007759 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
7760 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
7761 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7762 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7763 Exclude);
7764 } else {
7765 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7766 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007767 }
7768 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
7769 << Values << getOpenMPClauseName(OMPC_schedule);
7770 return nullptr;
7771 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00007772 // OpenMP, 2.7.1, Loop Construct, Restrictions
7773 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
7774 // schedule(guided).
7775 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
7776 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
7777 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
7778 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
7779 diag::err_omp_schedule_nonmonotonic_static);
7780 return nullptr;
7781 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007782 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00007783 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00007784 if (ChunkSize) {
7785 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
7786 !ChunkSize->isInstantiationDependent() &&
7787 !ChunkSize->containsUnexpandedParameterPack()) {
7788 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
7789 ExprResult Val =
7790 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
7791 if (Val.isInvalid())
7792 return nullptr;
7793
7794 ValExpr = Val.get();
7795
7796 // OpenMP [2.7.1, Restrictions]
7797 // chunk_size must be a loop invariant integer expression with a positive
7798 // value.
7799 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00007800 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
7801 if (Result.isSigned() && !Result.isStrictlyPositive()) {
7802 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007803 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00007804 return nullptr;
7805 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +00007806 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
7807 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00007808 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7809 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7810 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007811 }
7812 }
7813 }
7814
Alexey Bataev6402bca2015-12-28 07:25:51 +00007815 return new (Context)
7816 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00007817 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007818}
7819
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007820OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
7821 SourceLocation StartLoc,
7822 SourceLocation EndLoc) {
7823 OMPClause *Res = nullptr;
7824 switch (Kind) {
7825 case OMPC_ordered:
7826 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
7827 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00007828 case OMPC_nowait:
7829 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
7830 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007831 case OMPC_untied:
7832 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
7833 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007834 case OMPC_mergeable:
7835 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
7836 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007837 case OMPC_read:
7838 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
7839 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00007840 case OMPC_write:
7841 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
7842 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00007843 case OMPC_update:
7844 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
7845 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00007846 case OMPC_capture:
7847 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
7848 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007849 case OMPC_seq_cst:
7850 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
7851 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00007852 case OMPC_threads:
7853 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
7854 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007855 case OMPC_simd:
7856 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
7857 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00007858 case OMPC_nogroup:
7859 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
7860 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007861 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007862 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007863 case OMPC_num_threads:
7864 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007865 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007866 case OMPC_collapse:
7867 case OMPC_schedule:
7868 case OMPC_private:
7869 case OMPC_firstprivate:
7870 case OMPC_lastprivate:
7871 case OMPC_shared:
7872 case OMPC_reduction:
7873 case OMPC_linear:
7874 case OMPC_aligned:
7875 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007876 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007877 case OMPC_default:
7878 case OMPC_proc_bind:
7879 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007880 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007881 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007882 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007883 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007884 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007885 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007886 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007887 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00007888 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007889 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007890 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007891 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007892 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007893 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007894 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007895 case OMPC_from:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007896 llvm_unreachable("Clause is not allowed.");
7897 }
7898 return Res;
7899}
7900
Alexey Bataev236070f2014-06-20 11:19:47 +00007901OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
7902 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007903 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00007904 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
7905}
7906
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007907OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
7908 SourceLocation EndLoc) {
7909 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
7910}
7911
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007912OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
7913 SourceLocation EndLoc) {
7914 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
7915}
7916
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007917OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
7918 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007919 return new (Context) OMPReadClause(StartLoc, EndLoc);
7920}
7921
Alexey Bataevdea47612014-07-23 07:46:59 +00007922OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
7923 SourceLocation EndLoc) {
7924 return new (Context) OMPWriteClause(StartLoc, EndLoc);
7925}
7926
Alexey Bataev67a4f222014-07-23 10:25:33 +00007927OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
7928 SourceLocation EndLoc) {
7929 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
7930}
7931
Alexey Bataev459dec02014-07-24 06:46:57 +00007932OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
7933 SourceLocation EndLoc) {
7934 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
7935}
7936
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007937OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
7938 SourceLocation EndLoc) {
7939 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
7940}
7941
Alexey Bataev346265e2015-09-25 10:37:12 +00007942OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
7943 SourceLocation EndLoc) {
7944 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
7945}
7946
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007947OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
7948 SourceLocation EndLoc) {
7949 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
7950}
7951
Alexey Bataevb825de12015-12-07 10:51:44 +00007952OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
7953 SourceLocation EndLoc) {
7954 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
7955}
7956
Alexey Bataevc5e02582014-06-16 07:08:35 +00007957OMPClause *Sema::ActOnOpenMPVarListClause(
7958 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
7959 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
7960 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007961 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00007962 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
7963 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
7964 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007965 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007966 switch (Kind) {
7967 case OMPC_private:
7968 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7969 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007970 case OMPC_firstprivate:
7971 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7972 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007973 case OMPC_lastprivate:
7974 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7975 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007976 case OMPC_shared:
7977 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
7978 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007979 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00007980 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
7981 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007982 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00007983 case OMPC_linear:
7984 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00007985 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00007986 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007987 case OMPC_aligned:
7988 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
7989 ColonLoc, EndLoc);
7990 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007991 case OMPC_copyin:
7992 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
7993 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007994 case OMPC_copyprivate:
7995 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7996 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00007997 case OMPC_flush:
7998 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
7999 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008000 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008001 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
8002 StartLoc, LParenLoc, EndLoc);
8003 break;
8004 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00008005 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
8006 DepLinMapLoc, ColonLoc, VarList, StartLoc,
8007 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008008 break;
Samuel Antao661c0902016-05-26 17:39:58 +00008009 case OMPC_to:
8010 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
8011 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00008012 case OMPC_from:
8013 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
8014 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008015 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008016 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00008017 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00008018 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008019 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00008020 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008021 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008022 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008023 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008024 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008025 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008026 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008027 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008028 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008029 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008030 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008031 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008032 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008033 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00008034 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008035 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008036 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008037 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008038 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008039 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008040 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008041 case OMPC_nogroup:
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 Bataev5ec3eb12013-07-19 03:13:43 +00008046 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008047 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008048 llvm_unreachable("Clause is not allowed.");
8049 }
8050 return Res;
8051}
8052
Alexey Bataev90c228f2016-02-08 09:29:13 +00008053ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00008054 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00008055 ExprResult Res = BuildDeclRefExpr(
8056 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
8057 if (!Res.isUsable())
8058 return ExprError();
8059 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
8060 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
8061 if (!Res.isUsable())
8062 return ExprError();
8063 }
8064 if (VK != VK_LValue && Res.get()->isGLValue()) {
8065 Res = DefaultLvalueConversion(Res.get());
8066 if (!Res.isUsable())
8067 return ExprError();
8068 }
8069 return Res;
8070}
8071
Alexey Bataev60da77e2016-02-29 05:54:20 +00008072static std::pair<ValueDecl *, bool>
8073getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
8074 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008075 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
8076 RefExpr->containsUnexpandedParameterPack())
8077 return std::make_pair(nullptr, true);
8078
Alexey Bataevd985eda2016-02-10 11:29:16 +00008079 // OpenMP [3.1, C/C++]
8080 // A list item is a variable name.
8081 // OpenMP [2.9.3.3, Restrictions, p.1]
8082 // A variable that is part of another variable (as an array or
8083 // structure element) cannot appear in a private clause.
8084 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008085 enum {
8086 NoArrayExpr = -1,
8087 ArraySubscript = 0,
8088 OMPArraySection = 1
8089 } IsArrayExpr = NoArrayExpr;
8090 if (AllowArraySection) {
8091 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
8092 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
8093 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8094 Base = TempASE->getBase()->IgnoreParenImpCasts();
8095 RefExpr = Base;
8096 IsArrayExpr = ArraySubscript;
8097 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
8098 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
8099 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
8100 Base = TempOASE->getBase()->IgnoreParenImpCasts();
8101 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8102 Base = TempASE->getBase()->IgnoreParenImpCasts();
8103 RefExpr = Base;
8104 IsArrayExpr = OMPArraySection;
8105 }
8106 }
8107 ELoc = RefExpr->getExprLoc();
8108 ERange = RefExpr->getSourceRange();
8109 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008110 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
8111 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
8112 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
8113 (S.getCurrentThisType().isNull() || !ME ||
8114 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
8115 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008116 if (IsArrayExpr != NoArrayExpr)
8117 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
8118 << ERange;
8119 else {
8120 S.Diag(ELoc,
8121 AllowArraySection
8122 ? diag::err_omp_expected_var_name_member_expr_or_array_item
8123 : diag::err_omp_expected_var_name_member_expr)
8124 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
8125 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008126 return std::make_pair(nullptr, false);
8127 }
8128 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
8129}
8130
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008131OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
8132 SourceLocation StartLoc,
8133 SourceLocation LParenLoc,
8134 SourceLocation EndLoc) {
8135 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00008136 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00008137 for (auto &RefExpr : VarList) {
8138 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008139 SourceLocation ELoc;
8140 SourceRange ERange;
8141 Expr *SimpleRefExpr = RefExpr;
8142 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008143 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008144 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008145 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008146 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008147 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008148 ValueDecl *D = Res.first;
8149 if (!D)
8150 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008151
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008152 QualType Type = D->getType();
8153 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008154
8155 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8156 // A variable that appears in a private clause must not have an incomplete
8157 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008158 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008159 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008160 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008161
Alexey Bataev758e55e2013-09-06 18:03:48 +00008162 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8163 // in a Construct]
8164 // Variables with the predetermined data-sharing attributes may not be
8165 // listed in data-sharing attributes clauses, except for the cases
8166 // listed below. For these exceptions only, listing a predetermined
8167 // variable in a data-sharing attribute clause is allowed and overrides
8168 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008169 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008170 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00008171 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8172 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008173 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008174 continue;
8175 }
8176
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008177 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008178 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008179 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008180 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8181 << getOpenMPClauseName(OMPC_private) << Type
8182 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8183 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008184 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008185 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008186 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008187 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008188 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008189 continue;
8190 }
8191
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008192 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8193 // A list item cannot appear in both a map clause and a data-sharing
8194 // attribute clause on the same construct
8195 if (DSAStack->getCurrentDirective() == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00008196 if (DSAStack->checkMappableExprComponentListsForDecl(
8197 VD, /* CurrentRegionOnly = */ true,
8198 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
8199 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008200 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
8201 << getOpenMPClauseName(OMPC_private)
8202 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8203 ReportOriginalDSA(*this, DSAStack, D, DVar);
8204 continue;
8205 }
8206 }
8207
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008208 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
8209 // A variable of class type (or array thereof) that appears in a private
8210 // clause requires an accessible, unambiguous default constructor for the
8211 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00008212 // Generate helper private variable and initialize it with the default
8213 // value. The address of the original variable is replaced by the address of
8214 // the new private variable in CodeGen. This new variable is not added to
8215 // IdResolver, so the code in the OpenMP region uses original variable for
8216 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008217 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008218 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8219 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008220 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008221 if (VDPrivate->isInvalidDecl())
8222 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008223 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008224 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008225
Alexey Bataev90c228f2016-02-08 09:29:13 +00008226 DeclRefExpr *Ref = nullptr;
8227 if (!VD)
Alexey Bataev61205072016-03-02 04:57:40 +00008228 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00008229 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
8230 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008231 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008232 }
8233
Alexey Bataeved09d242014-05-28 05:53:51 +00008234 if (Vars.empty())
8235 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008236
Alexey Bataev03b340a2014-10-21 03:16:40 +00008237 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8238 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008239}
8240
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008241namespace {
8242class DiagsUninitializedSeveretyRAII {
8243private:
8244 DiagnosticsEngine &Diags;
8245 SourceLocation SavedLoc;
8246 bool IsIgnored;
8247
8248public:
8249 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
8250 bool IsIgnored)
8251 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
8252 if (!IsIgnored) {
8253 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
8254 /*Map*/ diag::Severity::Ignored, Loc);
8255 }
8256 }
8257 ~DiagsUninitializedSeveretyRAII() {
8258 if (!IsIgnored)
8259 Diags.popMappings(SavedLoc);
8260 }
8261};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00008262}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008263
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008264OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
8265 SourceLocation StartLoc,
8266 SourceLocation LParenLoc,
8267 SourceLocation EndLoc) {
8268 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008269 SmallVector<Expr *, 8> PrivateCopies;
8270 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00008271 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008272 bool IsImplicitClause =
8273 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
8274 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
8275
Alexey Bataeved09d242014-05-28 05:53:51 +00008276 for (auto &RefExpr : VarList) {
8277 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008278 SourceLocation ELoc;
8279 SourceRange ERange;
8280 Expr *SimpleRefExpr = RefExpr;
8281 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008282 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008283 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008284 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008285 PrivateCopies.push_back(nullptr);
8286 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008287 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008288 ValueDecl *D = Res.first;
8289 if (!D)
8290 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008291
Alexey Bataev60da77e2016-02-29 05:54:20 +00008292 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008293 QualType Type = D->getType();
8294 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008295
8296 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8297 // A variable that appears in a private clause must not have an incomplete
8298 // type or a reference type.
8299 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00008300 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008301 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008302 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008303
8304 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
8305 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00008306 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008307 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008308 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008309
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008310 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00008311 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008312 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008313 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008314 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008315 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008316 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
8317 // A list item that specifies a given variable may not appear in more
8318 // than one clause on the same directive, except that a variable may be
8319 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008320 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00008321 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008322 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008323 << getOpenMPClauseName(DVar.CKind)
8324 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008325 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008326 continue;
8327 }
8328
8329 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8330 // in a Construct]
8331 // Variables with the predetermined data-sharing attributes may not be
8332 // listed in data-sharing attributes clauses, except for the cases
8333 // listed below. For these exceptions only, listing a predetermined
8334 // variable in a data-sharing attribute clause is allowed and overrides
8335 // the variable's predetermined data-sharing attributes.
8336 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8337 // in a Construct, C/C++, p.2]
8338 // Variables with const-qualified type having no mutable member may be
8339 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00008340 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008341 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
8342 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008343 << getOpenMPClauseName(DVar.CKind)
8344 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008345 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008346 continue;
8347 }
8348
Alexey Bataevf29276e2014-06-18 04:14:57 +00008349 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008350 // OpenMP [2.9.3.4, Restrictions, p.2]
8351 // A list item that is private within a parallel region must not appear
8352 // in a firstprivate clause on a worksharing construct if any of the
8353 // worksharing regions arising from the worksharing construct ever bind
8354 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00008355 if (isOpenMPWorksharingDirective(CurrDir) &&
8356 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008357 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008358 if (DVar.CKind != OMPC_shared &&
8359 (isOpenMPParallelDirective(DVar.DKind) ||
8360 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00008361 Diag(ELoc, diag::err_omp_required_access)
8362 << getOpenMPClauseName(OMPC_firstprivate)
8363 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008364 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008365 continue;
8366 }
8367 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008368 // OpenMP [2.9.3.4, Restrictions, p.3]
8369 // A list item that appears in a reduction clause of a parallel construct
8370 // must not appear in a firstprivate clause on a worksharing or task
8371 // construct if any of the worksharing or task regions arising from the
8372 // worksharing or task construct ever bind to any of the parallel regions
8373 // arising from the parallel construct.
8374 // OpenMP [2.9.3.4, Restrictions, p.4]
8375 // A list item that appears in a reduction clause in worksharing
8376 // construct must not appear in a firstprivate clause in a task construct
8377 // encountered during execution of any of the worksharing regions arising
8378 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00008379 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008380 DVar = DSAStack->hasInnermostDSA(
8381 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8382 [](OpenMPDirectiveKind K) -> bool {
8383 return isOpenMPParallelDirective(K) ||
8384 isOpenMPWorksharingDirective(K);
8385 },
8386 false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008387 if (DVar.CKind == OMPC_reduction &&
8388 (isOpenMPParallelDirective(DVar.DKind) ||
8389 isOpenMPWorksharingDirective(DVar.DKind))) {
8390 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
8391 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008392 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008393 continue;
8394 }
8395 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008396
8397 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8398 // A list item that is private within a teams region must not appear in a
8399 // firstprivate clause on a distribute construct if any of the distribute
8400 // regions arising from the distribute construct ever bind to any of the
8401 // teams regions arising from the teams construct.
8402 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8403 // A list item that appears in a reduction clause of a teams construct
8404 // must not appear in a firstprivate clause on a distribute construct if
8405 // any of the distribute regions arising from the distribute construct
8406 // ever bind to any of the teams regions arising from the teams construct.
8407 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8408 // A list item may appear in a firstprivate or lastprivate clause but not
8409 // both.
8410 if (CurrDir == OMPD_distribute) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008411 DVar = DSAStack->hasInnermostDSA(
8412 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_private; },
8413 [](OpenMPDirectiveKind K) -> bool {
8414 return isOpenMPTeamsDirective(K);
8415 },
8416 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008417 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
8418 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008419 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008420 continue;
8421 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008422 DVar = DSAStack->hasInnermostDSA(
8423 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8424 [](OpenMPDirectiveKind K) -> bool {
8425 return isOpenMPTeamsDirective(K);
8426 },
8427 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008428 if (DVar.CKind == OMPC_reduction &&
8429 isOpenMPTeamsDirective(DVar.DKind)) {
8430 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008431 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008432 continue;
8433 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008434 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008435 if (DVar.CKind == OMPC_lastprivate) {
8436 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008437 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008438 continue;
8439 }
8440 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008441 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8442 // A list item cannot appear in both a map clause and a data-sharing
8443 // attribute clause on the same construct
8444 if (CurrDir == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00008445 if (DSAStack->checkMappableExprComponentListsForDecl(
8446 VD, /* CurrentRegionOnly = */ true,
8447 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
8448 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008449 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
8450 << getOpenMPClauseName(OMPC_firstprivate)
8451 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8452 ReportOriginalDSA(*this, DSAStack, D, DVar);
8453 continue;
8454 }
8455 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008456 }
8457
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008458 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008459 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008460 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008461 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8462 << getOpenMPClauseName(OMPC_firstprivate) << Type
8463 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8464 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008465 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008466 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008467 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008468 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00008469 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008470 continue;
8471 }
8472
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008473 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008474 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8475 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008476 // Generate helper private variable and initialize it with the value of the
8477 // original variable. The address of the original variable is replaced by
8478 // the address of the new private variable in the CodeGen. This new variable
8479 // is not added to IdResolver, so the code in the OpenMP region uses
8480 // original variable for proper diagnostics and variable capturing.
8481 Expr *VDInitRefExpr = nullptr;
8482 // For arrays generate initializer for single element and replace it by the
8483 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008484 if (Type->isArrayType()) {
8485 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008486 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008487 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008488 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008489 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008490 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008491 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00008492 InitializedEntity Entity =
8493 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008494 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
8495
8496 InitializationSequence InitSeq(*this, Entity, Kind, Init);
8497 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
8498 if (Result.isInvalid())
8499 VDPrivate->setInvalidDecl();
8500 else
8501 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008502 // Remove temp variable declaration.
8503 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008504 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008505 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
8506 ".firstprivate.temp");
8507 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
8508 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00008509 AddInitializerToDecl(VDPrivate,
8510 DefaultLvalueConversion(VDInitRefExpr).get(),
8511 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008512 }
8513 if (VDPrivate->isInvalidDecl()) {
8514 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008515 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008516 diag::note_omp_task_predetermined_firstprivate_here);
8517 }
8518 continue;
8519 }
8520 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008521 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00008522 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
8523 RefExpr->getExprLoc());
8524 DeclRefExpr *Ref = nullptr;
Alexey Bataev417089f2016-02-17 13:19:37 +00008525 if (!VD) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008526 if (TopDVar.CKind == OMPC_lastprivate)
8527 Ref = TopDVar.PrivateCopy;
8528 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008529 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00008530 if (!IsOpenMPCapturedDecl(D))
8531 ExprCaptures.push_back(Ref->getDecl());
8532 }
Alexey Bataev417089f2016-02-17 13:19:37 +00008533 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008534 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
8535 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008536 PrivateCopies.push_back(VDPrivateRefExpr);
8537 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008538 }
8539
Alexey Bataeved09d242014-05-28 05:53:51 +00008540 if (Vars.empty())
8541 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008542
8543 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008544 Vars, PrivateCopies, Inits,
8545 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008546}
8547
Alexander Musman1bb328c2014-06-04 13:06:39 +00008548OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
8549 SourceLocation StartLoc,
8550 SourceLocation LParenLoc,
8551 SourceLocation EndLoc) {
8552 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00008553 SmallVector<Expr *, 8> SrcExprs;
8554 SmallVector<Expr *, 8> DstExprs;
8555 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00008556 SmallVector<Decl *, 4> ExprCaptures;
8557 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008558 for (auto &RefExpr : VarList) {
8559 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008560 SourceLocation ELoc;
8561 SourceRange ERange;
8562 Expr *SimpleRefExpr = RefExpr;
8563 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008564 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00008565 // It will be analyzed later.
8566 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00008567 SrcExprs.push_back(nullptr);
8568 DstExprs.push_back(nullptr);
8569 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008570 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008571 ValueDecl *D = Res.first;
8572 if (!D)
8573 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008574
Alexey Bataev74caaf22016-02-20 04:09:36 +00008575 QualType Type = D->getType();
8576 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008577
8578 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
8579 // A variable that appears in a lastprivate clause must not have an
8580 // incomplete type or a reference type.
8581 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00008582 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00008583 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008584 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00008585
8586 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8587 // in a Construct]
8588 // Variables with the predetermined data-sharing attributes may not be
8589 // listed in data-sharing attributes clauses, except for the cases
8590 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008591 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008592 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
8593 DVar.CKind != OMPC_firstprivate &&
8594 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
8595 Diag(ELoc, diag::err_omp_wrong_dsa)
8596 << getOpenMPClauseName(DVar.CKind)
8597 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008598 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008599 continue;
8600 }
8601
Alexey Bataevf29276e2014-06-18 04:14:57 +00008602 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8603 // OpenMP [2.14.3.5, Restrictions, p.2]
8604 // A list item that is private within a parallel region, or that appears in
8605 // the reduction clause of a parallel construct, must not appear in a
8606 // lastprivate clause on a worksharing construct if any of the corresponding
8607 // worksharing regions ever binds to any of the corresponding parallel
8608 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00008609 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00008610 if (isOpenMPWorksharingDirective(CurrDir) &&
8611 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00008612 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008613 if (DVar.CKind != OMPC_shared) {
8614 Diag(ELoc, diag::err_omp_required_access)
8615 << getOpenMPClauseName(OMPC_lastprivate)
8616 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008617 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008618 continue;
8619 }
8620 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008621
8622 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8623 // A list item may appear in a firstprivate or lastprivate clause but not
8624 // both.
8625 if (CurrDir == OMPD_distribute) {
8626 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8627 if (DVar.CKind == OMPC_firstprivate) {
8628 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
8629 ReportOriginalDSA(*this, DSAStack, D, DVar);
8630 continue;
8631 }
8632 }
8633
Alexander Musman1bb328c2014-06-04 13:06:39 +00008634 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00008635 // A variable of class type (or array thereof) that appears in a
8636 // lastprivate clause requires an accessible, unambiguous default
8637 // constructor for the class type, unless the list item is also specified
8638 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00008639 // A variable of class type (or array thereof) that appears in a
8640 // lastprivate clause requires an accessible, unambiguous copy assignment
8641 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00008642 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008643 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008644 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008645 D->hasAttrs() ? &D->getAttrs() : nullptr);
8646 auto *PseudoSrcExpr =
8647 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008648 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008649 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008650 D->hasAttrs() ? &D->getAttrs() : nullptr);
8651 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008652 // For arrays generate assignment operation for single element and replace
8653 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008654 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00008655 PseudoDstExpr, PseudoSrcExpr);
8656 if (AssignmentOp.isInvalid())
8657 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00008658 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00008659 /*DiscardedValue=*/true);
8660 if (AssignmentOp.isInvalid())
8661 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008662
Alexey Bataev74caaf22016-02-20 04:09:36 +00008663 DeclRefExpr *Ref = nullptr;
Alexey Bataev005248a2016-02-25 05:25:57 +00008664 if (!VD) {
8665 if (TopDVar.CKind == OMPC_firstprivate)
8666 Ref = TopDVar.PrivateCopy;
8667 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008668 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008669 if (!IsOpenMPCapturedDecl(D))
8670 ExprCaptures.push_back(Ref->getDecl());
8671 }
8672 if (TopDVar.CKind == OMPC_firstprivate ||
8673 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008674 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008675 ExprResult RefRes = DefaultLvalueConversion(Ref);
8676 if (!RefRes.isUsable())
8677 continue;
8678 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008679 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
8680 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008681 if (!PostUpdateRes.isUsable())
8682 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008683 ExprPostUpdates.push_back(
8684 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008685 }
8686 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008687 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008688 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00008689 SrcExprs.push_back(PseudoSrcExpr);
8690 DstExprs.push_back(PseudoDstExpr);
8691 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00008692 }
8693
8694 if (Vars.empty())
8695 return nullptr;
8696
8697 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00008698 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008699 buildPreInits(Context, ExprCaptures),
8700 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00008701}
8702
Alexey Bataev758e55e2013-09-06 18:03:48 +00008703OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
8704 SourceLocation StartLoc,
8705 SourceLocation LParenLoc,
8706 SourceLocation EndLoc) {
8707 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00008708 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008709 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008710 SourceLocation ELoc;
8711 SourceRange ERange;
8712 Expr *SimpleRefExpr = RefExpr;
8713 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008714 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00008715 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008716 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008717 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008718 ValueDecl *D = Res.first;
8719 if (!D)
8720 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008721
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008722 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008723 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8724 // in a Construct]
8725 // Variables with the predetermined data-sharing attributes may not be
8726 // listed in data-sharing attributes clauses, except for the cases
8727 // listed below. For these exceptions only, listing a predetermined
8728 // variable in a data-sharing attribute clause is allowed and overrides
8729 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008730 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00008731 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
8732 DVar.RefExpr) {
8733 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8734 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008735 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008736 continue;
8737 }
8738
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008739 DeclRefExpr *Ref = nullptr;
Alexey Bataev1efd1662016-03-29 10:59:56 +00008740 if (!VD && IsOpenMPCapturedDecl(D))
Alexey Bataev61205072016-03-02 04:57:40 +00008741 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008742 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataev1efd1662016-03-29 10:59:56 +00008743 Vars.push_back((VD || !Ref) ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008744 }
8745
Alexey Bataeved09d242014-05-28 05:53:51 +00008746 if (Vars.empty())
8747 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008748
8749 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
8750}
8751
Alexey Bataevc5e02582014-06-16 07:08:35 +00008752namespace {
8753class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
8754 DSAStackTy *Stack;
8755
8756public:
8757 bool VisitDeclRefExpr(DeclRefExpr *E) {
8758 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008759 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008760 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
8761 return false;
8762 if (DVar.CKind != OMPC_unknown)
8763 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008764 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
8765 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
8766 false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008767 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008768 return true;
8769 return false;
8770 }
8771 return false;
8772 }
8773 bool VisitStmt(Stmt *S) {
8774 for (auto Child : S->children()) {
8775 if (Child && Visit(Child))
8776 return true;
8777 }
8778 return false;
8779 }
Alexey Bataev23b69422014-06-18 07:08:49 +00008780 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00008781};
Alexey Bataev23b69422014-06-18 07:08:49 +00008782} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00008783
Alexey Bataev60da77e2016-02-29 05:54:20 +00008784namespace {
8785// Transform MemberExpression for specified FieldDecl of current class to
8786// DeclRefExpr to specified OMPCapturedExprDecl.
8787class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
8788 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
8789 ValueDecl *Field;
8790 DeclRefExpr *CapturedExpr;
8791
8792public:
8793 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
8794 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
8795
8796 ExprResult TransformMemberExpr(MemberExpr *E) {
8797 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
8798 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00008799 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008800 return CapturedExpr;
8801 }
8802 return BaseTransform::TransformMemberExpr(E);
8803 }
8804 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
8805};
8806} // namespace
8807
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008808template <typename T>
8809static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
8810 const llvm::function_ref<T(ValueDecl *)> &Gen) {
8811 for (auto &Set : Lookups) {
8812 for (auto *D : Set) {
8813 if (auto Res = Gen(cast<ValueDecl>(D)))
8814 return Res;
8815 }
8816 }
8817 return T();
8818}
8819
8820static ExprResult
8821buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
8822 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
8823 const DeclarationNameInfo &ReductionId, QualType Ty,
8824 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
8825 if (ReductionIdScopeSpec.isInvalid())
8826 return ExprError();
8827 SmallVector<UnresolvedSet<8>, 4> Lookups;
8828 if (S) {
8829 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
8830 Lookup.suppressDiagnostics();
8831 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
8832 auto *D = Lookup.getRepresentativeDecl();
8833 do {
8834 S = S->getParent();
8835 } while (S && !S->isDeclScope(D));
8836 if (S)
8837 S = S->getParent();
8838 Lookups.push_back(UnresolvedSet<8>());
8839 Lookups.back().append(Lookup.begin(), Lookup.end());
8840 Lookup.clear();
8841 }
8842 } else if (auto *ULE =
8843 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
8844 Lookups.push_back(UnresolvedSet<8>());
8845 Decl *PrevD = nullptr;
8846 for(auto *D : ULE->decls()) {
8847 if (D == PrevD)
8848 Lookups.push_back(UnresolvedSet<8>());
8849 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
8850 Lookups.back().addDecl(DRD);
8851 PrevD = D;
8852 }
8853 }
8854 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
8855 Ty->containsUnexpandedParameterPack() ||
8856 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
8857 return !D->isInvalidDecl() &&
8858 (D->getType()->isDependentType() ||
8859 D->getType()->isInstantiationDependentType() ||
8860 D->getType()->containsUnexpandedParameterPack());
8861 })) {
8862 UnresolvedSet<8> ResSet;
8863 for (auto &Set : Lookups) {
8864 ResSet.append(Set.begin(), Set.end());
8865 // The last item marks the end of all declarations at the specified scope.
8866 ResSet.addDecl(Set[Set.size() - 1]);
8867 }
8868 return UnresolvedLookupExpr::Create(
8869 SemaRef.Context, /*NamingClass=*/nullptr,
8870 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
8871 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
8872 }
8873 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8874 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
8875 if (!D->isInvalidDecl() &&
8876 SemaRef.Context.hasSameType(D->getType(), Ty))
8877 return D;
8878 return nullptr;
8879 }))
8880 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8881 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8882 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
8883 if (!D->isInvalidDecl() &&
8884 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
8885 !Ty.isMoreQualifiedThan(D->getType()))
8886 return D;
8887 return nullptr;
8888 })) {
8889 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
8890 /*DetectVirtual=*/false);
8891 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
8892 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
8893 VD->getType().getUnqualifiedType()))) {
8894 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
8895 /*DiagID=*/0) !=
8896 Sema::AR_inaccessible) {
8897 SemaRef.BuildBasePathArray(Paths, BasePath);
8898 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8899 }
8900 }
8901 }
8902 }
8903 if (ReductionIdScopeSpec.isSet()) {
8904 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
8905 return ExprError();
8906 }
8907 return ExprEmpty();
8908}
8909
Alexey Bataevc5e02582014-06-16 07:08:35 +00008910OMPClause *Sema::ActOnOpenMPReductionClause(
8911 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
8912 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008913 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
8914 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00008915 auto DN = ReductionId.getName();
8916 auto OOK = DN.getCXXOverloadedOperator();
8917 BinaryOperatorKind BOK = BO_Comma;
8918
8919 // OpenMP [2.14.3.6, reduction clause]
8920 // C
8921 // reduction-identifier is either an identifier or one of the following
8922 // operators: +, -, *, &, |, ^, && and ||
8923 // C++
8924 // reduction-identifier is either an id-expression or one of the following
8925 // operators: +, -, *, &, |, ^, && and ||
8926 // FIXME: Only 'min' and 'max' identifiers are supported for now.
8927 switch (OOK) {
8928 case OO_Plus:
8929 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008930 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008931 break;
8932 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008933 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008934 break;
8935 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008936 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008937 break;
8938 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008939 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008940 break;
8941 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008942 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008943 break;
8944 case OO_AmpAmp:
8945 BOK = BO_LAnd;
8946 break;
8947 case OO_PipePipe:
8948 BOK = BO_LOr;
8949 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008950 case OO_New:
8951 case OO_Delete:
8952 case OO_Array_New:
8953 case OO_Array_Delete:
8954 case OO_Slash:
8955 case OO_Percent:
8956 case OO_Tilde:
8957 case OO_Exclaim:
8958 case OO_Equal:
8959 case OO_Less:
8960 case OO_Greater:
8961 case OO_LessEqual:
8962 case OO_GreaterEqual:
8963 case OO_PlusEqual:
8964 case OO_MinusEqual:
8965 case OO_StarEqual:
8966 case OO_SlashEqual:
8967 case OO_PercentEqual:
8968 case OO_CaretEqual:
8969 case OO_AmpEqual:
8970 case OO_PipeEqual:
8971 case OO_LessLess:
8972 case OO_GreaterGreater:
8973 case OO_LessLessEqual:
8974 case OO_GreaterGreaterEqual:
8975 case OO_EqualEqual:
8976 case OO_ExclaimEqual:
8977 case OO_PlusPlus:
8978 case OO_MinusMinus:
8979 case OO_Comma:
8980 case OO_ArrowStar:
8981 case OO_Arrow:
8982 case OO_Call:
8983 case OO_Subscript:
8984 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00008985 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008986 case NUM_OVERLOADED_OPERATORS:
8987 llvm_unreachable("Unexpected reduction identifier");
8988 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008989 if (auto II = DN.getAsIdentifierInfo()) {
8990 if (II->isStr("max"))
8991 BOK = BO_GT;
8992 else if (II->isStr("min"))
8993 BOK = BO_LT;
8994 }
8995 break;
8996 }
8997 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008998 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00008999 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009000 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009001
9002 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009003 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009004 SmallVector<Expr *, 8> LHSs;
9005 SmallVector<Expr *, 8> RHSs;
9006 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00009007 SmallVector<Decl *, 4> ExprCaptures;
9008 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009009 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
9010 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009011 for (auto RefExpr : VarList) {
9012 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00009013 // OpenMP [2.1, C/C++]
9014 // A list item is a variable or array section, subject to the restrictions
9015 // specified in Section 2.4 on page 42 and in each of the sections
9016 // describing clauses and directives for which a list appears.
9017 // OpenMP [2.14.3.3, Restrictions, p.1]
9018 // A variable that is part of another variable (as an array or
9019 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009020 if (!FirstIter && IR != ER)
9021 ++IR;
9022 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009023 SourceLocation ELoc;
9024 SourceRange ERange;
9025 Expr *SimpleRefExpr = RefExpr;
9026 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9027 /*AllowArraySection=*/true);
9028 if (Res.second) {
9029 // It will be analyzed later.
9030 Vars.push_back(RefExpr);
9031 Privates.push_back(nullptr);
9032 LHSs.push_back(nullptr);
9033 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009034 // Try to find 'declare reduction' corresponding construct before using
9035 // builtin/overloaded operators.
9036 QualType Type = Context.DependentTy;
9037 CXXCastPath BasePath;
9038 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9039 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9040 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9041 if (CurContext->isDependentContext() &&
9042 (DeclareReductionRef.isUnset() ||
9043 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
9044 ReductionOps.push_back(DeclareReductionRef.get());
9045 else
9046 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009047 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00009048 ValueDecl *D = Res.first;
9049 if (!D)
9050 continue;
9051
Alexey Bataeva1764212015-09-30 09:22:36 +00009052 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009053 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
9054 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
9055 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00009056 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009057 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009058 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
9059 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
9060 Type = ATy->getElementType();
9061 else
9062 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00009063 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009064 } else
9065 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
9066 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00009067
Alexey Bataevc5e02582014-06-16 07:08:35 +00009068 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9069 // A variable that appears in a private clause must not have an incomplete
9070 // type or a reference type.
9071 if (RequireCompleteType(ELoc, Type,
9072 diag::err_omp_reduction_incomplete_type))
9073 continue;
9074 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00009075 // A list item that appears in a reduction clause must not be
9076 // const-qualified.
9077 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009078 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00009079 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009080 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009081 bool IsDecl = !VD ||
9082 VD->isThisDeclarationADefinition(Context) ==
9083 VarDecl::DeclarationOnly;
9084 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00009085 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00009086 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00009087 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00009088 continue;
9089 }
9090 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
9091 // If a list-item is a reference type then it must bind to the same object
9092 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00009093 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009094 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00009095 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009096 DSARefChecker Check(DSAStack);
9097 if (Check.Visit(VDDef->getInit())) {
9098 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
9099 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
9100 continue;
9101 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00009102 }
9103 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009104
Alexey Bataevc5e02582014-06-16 07:08:35 +00009105 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
9106 // in a Construct]
9107 // Variables with the predetermined data-sharing attributes may not be
9108 // listed in data-sharing attributes clauses, except for the cases
9109 // listed below. For these exceptions only, listing a predetermined
9110 // variable in a data-sharing attribute clause is allowed and overrides
9111 // the variable's predetermined data-sharing attributes.
9112 // OpenMP [2.14.3.6, Restrictions, p.3]
9113 // Any number of reduction clauses can be specified on the directive,
9114 // but a list item can appear only once in the reduction clauses for that
9115 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00009116 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009117 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009118 if (DVar.CKind == OMPC_reduction) {
9119 Diag(ELoc, diag::err_omp_once_referenced)
9120 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009121 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009122 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009123 } else if (DVar.CKind != OMPC_unknown) {
9124 Diag(ELoc, diag::err_omp_wrong_dsa)
9125 << getOpenMPClauseName(DVar.CKind)
9126 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009127 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009128 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009129 }
9130
9131 // OpenMP [2.14.3.6, Restrictions, p.1]
9132 // A list item that appears in a reduction clause of a worksharing
9133 // construct must be shared in the parallel regions to which any of the
9134 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009135 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
9136 if (isOpenMPWorksharingDirective(CurrDir) &&
9137 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009138 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009139 if (DVar.CKind != OMPC_shared) {
9140 Diag(ELoc, diag::err_omp_required_access)
9141 << getOpenMPClauseName(OMPC_reduction)
9142 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009143 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009144 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00009145 }
9146 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009147
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009148 // Try to find 'declare reduction' corresponding construct before using
9149 // builtin/overloaded operators.
9150 CXXCastPath BasePath;
9151 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9152 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9153 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9154 if (DeclareReductionRef.isInvalid())
9155 continue;
9156 if (CurContext->isDependentContext() &&
9157 (DeclareReductionRef.isUnset() ||
9158 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
9159 Vars.push_back(RefExpr);
9160 Privates.push_back(nullptr);
9161 LHSs.push_back(nullptr);
9162 RHSs.push_back(nullptr);
9163 ReductionOps.push_back(DeclareReductionRef.get());
9164 continue;
9165 }
9166 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
9167 // Not allowed reduction identifier is found.
9168 Diag(ReductionId.getLocStart(),
9169 diag::err_omp_unknown_reduction_identifier)
9170 << Type << ReductionIdRange;
9171 continue;
9172 }
9173
9174 // OpenMP [2.14.3.6, reduction clause, Restrictions]
9175 // The type of a list item that appears in a reduction clause must be valid
9176 // for the reduction-identifier. For a max or min reduction in C, the type
9177 // of the list item must be an allowed arithmetic data type: char, int,
9178 // float, double, or _Bool, possibly modified with long, short, signed, or
9179 // unsigned. For a max or min reduction in C++, the type of the list item
9180 // must be an allowed arithmetic data type: char, wchar_t, int, float,
9181 // double, or bool, possibly modified with long, short, signed, or unsigned.
9182 if (DeclareReductionRef.isUnset()) {
9183 if ((BOK == BO_GT || BOK == BO_LT) &&
9184 !(Type->isScalarType() ||
9185 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
9186 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
9187 << getLangOpts().CPlusPlus;
9188 if (!ASE && !OASE) {
9189 bool IsDecl = !VD ||
9190 VD->isThisDeclarationADefinition(Context) ==
9191 VarDecl::DeclarationOnly;
9192 Diag(D->getLocation(),
9193 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9194 << D;
9195 }
9196 continue;
9197 }
9198 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
9199 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
9200 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
9201 if (!ASE && !OASE) {
9202 bool IsDecl = !VD ||
9203 VD->isThisDeclarationADefinition(Context) ==
9204 VarDecl::DeclarationOnly;
9205 Diag(D->getLocation(),
9206 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9207 << D;
9208 }
9209 continue;
9210 }
9211 }
9212
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009213 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009214 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00009215 D->hasAttrs() ? &D->getAttrs() : nullptr);
9216 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
9217 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009218 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00009219 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00009220 (!ASE &&
9221 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00009222 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009223 // Create pseudo array type for private copy. The size for this array will
9224 // be generated during codegen.
9225 // For array subscripts or single variables Private Ty is the same as Type
9226 // (type of the variable or single array element).
9227 PrivateTy = Context.getVariableArrayType(
9228 Type, new (Context) OpaqueValueExpr(SourceLocation(),
9229 Context.getSizeType(), VK_RValue),
9230 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00009231 } else if (!ASE && !OASE &&
9232 Context.getAsArrayType(D->getType().getNonReferenceType()))
9233 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009234 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00009235 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
9236 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009237 // Add initializer for private variable.
9238 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009239 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
9240 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
9241 if (DeclareReductionRef.isUsable()) {
9242 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
9243 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
9244 if (DRD->getInitializer()) {
9245 Init = DRDRef;
9246 RHSVD->setInit(DRDRef);
9247 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009248 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009249 } else {
9250 switch (BOK) {
9251 case BO_Add:
9252 case BO_Xor:
9253 case BO_Or:
9254 case BO_LOr:
9255 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
9256 if (Type->isScalarType() || Type->isAnyComplexType())
9257 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
9258 break;
9259 case BO_Mul:
9260 case BO_LAnd:
9261 if (Type->isScalarType() || Type->isAnyComplexType()) {
9262 // '*' and '&&' reduction ops - initializer is '1'.
9263 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00009264 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009265 break;
9266 case BO_And: {
9267 // '&' reduction op - initializer is '~0'.
9268 QualType OrigType = Type;
9269 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
9270 Type = ComplexTy->getElementType();
9271 if (Type->isRealFloatingType()) {
9272 llvm::APFloat InitValue =
9273 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
9274 /*isIEEE=*/true);
9275 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9276 Type, ELoc);
9277 } else if (Type->isScalarType()) {
9278 auto Size = Context.getTypeSize(Type);
9279 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
9280 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
9281 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9282 }
9283 if (Init && OrigType->isAnyComplexType()) {
9284 // Init = 0xFFFF + 0xFFFFi;
9285 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
9286 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
9287 }
9288 Type = OrigType;
9289 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009290 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009291 case BO_LT:
9292 case BO_GT: {
9293 // 'min' reduction op - initializer is 'Largest representable number in
9294 // the reduction list item type'.
9295 // 'max' reduction op - initializer is 'Least representable number in
9296 // the reduction list item type'.
9297 if (Type->isIntegerType() || Type->isPointerType()) {
9298 bool IsSigned = Type->hasSignedIntegerRepresentation();
9299 auto Size = Context.getTypeSize(Type);
9300 QualType IntTy =
9301 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
9302 llvm::APInt InitValue =
9303 (BOK != BO_LT)
9304 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
9305 : llvm::APInt::getMinValue(Size)
9306 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
9307 : llvm::APInt::getMaxValue(Size);
9308 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9309 if (Type->isPointerType()) {
9310 // Cast to pointer type.
9311 auto CastExpr = BuildCStyleCastExpr(
9312 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
9313 SourceLocation(), Init);
9314 if (CastExpr.isInvalid())
9315 continue;
9316 Init = CastExpr.get();
9317 }
9318 } else if (Type->isRealFloatingType()) {
9319 llvm::APFloat InitValue = llvm::APFloat::getLargest(
9320 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
9321 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9322 Type, ELoc);
9323 }
9324 break;
9325 }
9326 case BO_PtrMemD:
9327 case BO_PtrMemI:
9328 case BO_MulAssign:
9329 case BO_Div:
9330 case BO_Rem:
9331 case BO_Sub:
9332 case BO_Shl:
9333 case BO_Shr:
9334 case BO_LE:
9335 case BO_GE:
9336 case BO_EQ:
9337 case BO_NE:
9338 case BO_AndAssign:
9339 case BO_XorAssign:
9340 case BO_OrAssign:
9341 case BO_Assign:
9342 case BO_AddAssign:
9343 case BO_SubAssign:
9344 case BO_DivAssign:
9345 case BO_RemAssign:
9346 case BO_ShlAssign:
9347 case BO_ShrAssign:
9348 case BO_Comma:
9349 llvm_unreachable("Unexpected reduction operation");
9350 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009351 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009352 if (Init && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009353 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
9354 /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009355 } else if (!Init)
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009356 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009357 if (RHSVD->isInvalidDecl())
9358 continue;
9359 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009360 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
9361 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009362 bool IsDecl =
9363 !VD ||
9364 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9365 Diag(D->getLocation(),
9366 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9367 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009368 continue;
9369 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009370 // Store initializer for single element in private copy. Will be used during
9371 // codegen.
9372 PrivateVD->setInit(RHSVD->getInit());
9373 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009374 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009375 ExprResult ReductionOp;
9376 if (DeclareReductionRef.isUsable()) {
9377 QualType RedTy = DeclareReductionRef.get()->getType();
9378 QualType PtrRedTy = Context.getPointerType(RedTy);
9379 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
9380 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
9381 if (!BasePath.empty()) {
9382 LHS = DefaultLvalueConversion(LHS.get());
9383 RHS = DefaultLvalueConversion(RHS.get());
9384 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9385 CK_UncheckedDerivedToBase, LHS.get(),
9386 &BasePath, LHS.get()->getValueKind());
9387 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9388 CK_UncheckedDerivedToBase, RHS.get(),
9389 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009390 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009391 FunctionProtoType::ExtProtoInfo EPI;
9392 QualType Params[] = {PtrRedTy, PtrRedTy};
9393 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
9394 auto *OVE = new (Context) OpaqueValueExpr(
9395 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
9396 DefaultLvalueConversion(DeclareReductionRef.get()).get());
9397 Expr *Args[] = {LHS.get(), RHS.get()};
9398 ReductionOp = new (Context)
9399 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
9400 } else {
9401 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
9402 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
9403 if (ReductionOp.isUsable()) {
9404 if (BOK != BO_LT && BOK != BO_GT) {
9405 ReductionOp =
9406 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9407 BO_Assign, LHSDRE, ReductionOp.get());
9408 } else {
9409 auto *ConditionalOp = new (Context) ConditionalOperator(
9410 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
9411 RHSDRE, Type, VK_LValue, OK_Ordinary);
9412 ReductionOp =
9413 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9414 BO_Assign, LHSDRE, ConditionalOp);
9415 }
9416 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
9417 }
9418 if (ReductionOp.isInvalid())
9419 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009420 }
9421
Alexey Bataev60da77e2016-02-29 05:54:20 +00009422 DeclRefExpr *Ref = nullptr;
9423 Expr *VarsExpr = RefExpr->IgnoreParens();
9424 if (!VD) {
9425 if (ASE || OASE) {
9426 TransformExprToCaptures RebuildToCapture(*this, D);
9427 VarsExpr =
9428 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
9429 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00009430 } else {
9431 VarsExpr = Ref =
9432 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +00009433 }
9434 if (!IsOpenMPCapturedDecl(D)) {
9435 ExprCaptures.push_back(Ref->getDecl());
9436 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9437 ExprResult RefRes = DefaultLvalueConversion(Ref);
9438 if (!RefRes.isUsable())
9439 continue;
9440 ExprResult PostUpdateRes =
9441 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9442 SimpleRefExpr, RefRes.get());
9443 if (!PostUpdateRes.isUsable())
9444 continue;
9445 ExprPostUpdates.push_back(
9446 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00009447 }
9448 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00009449 }
9450 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
9451 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009452 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009453 LHSs.push_back(LHSDRE);
9454 RHSs.push_back(RHSDRE);
9455 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009456 }
9457
9458 if (Vars.empty())
9459 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00009460
Alexey Bataevc5e02582014-06-16 07:08:35 +00009461 return OMPReductionClause::Create(
9462 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009463 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009464 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
9465 buildPostUpdate(*this, ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +00009466}
9467
Alexey Bataevecba70f2016-04-12 11:02:11 +00009468bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
9469 SourceLocation LinLoc) {
9470 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
9471 LinKind == OMPC_LINEAR_unknown) {
9472 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
9473 return true;
9474 }
9475 return false;
9476}
9477
9478bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
9479 OpenMPLinearClauseKind LinKind,
9480 QualType Type) {
9481 auto *VD = dyn_cast_or_null<VarDecl>(D);
9482 // A variable must not have an incomplete type or a reference type.
9483 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
9484 return true;
9485 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
9486 !Type->isReferenceType()) {
9487 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
9488 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
9489 return true;
9490 }
9491 Type = Type.getNonReferenceType();
9492
9493 // A list item must not be const-qualified.
9494 if (Type.isConstant(Context)) {
9495 Diag(ELoc, diag::err_omp_const_variable)
9496 << getOpenMPClauseName(OMPC_linear);
9497 if (D) {
9498 bool IsDecl =
9499 !VD ||
9500 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9501 Diag(D->getLocation(),
9502 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9503 << D;
9504 }
9505 return true;
9506 }
9507
9508 // A list item must be of integral or pointer type.
9509 Type = Type.getUnqualifiedType().getCanonicalType();
9510 const auto *Ty = Type.getTypePtrOrNull();
9511 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
9512 !Ty->isPointerType())) {
9513 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
9514 if (D) {
9515 bool IsDecl =
9516 !VD ||
9517 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9518 Diag(D->getLocation(),
9519 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9520 << D;
9521 }
9522 return true;
9523 }
9524 return false;
9525}
9526
Alexey Bataev182227b2015-08-20 10:54:39 +00009527OMPClause *Sema::ActOnOpenMPLinearClause(
9528 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
9529 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
9530 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009531 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009532 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00009533 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00009534 SmallVector<Decl *, 4> ExprCaptures;
9535 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009536 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +00009537 LinKind = OMPC_LINEAR_val;
Alexey Bataeved09d242014-05-28 05:53:51 +00009538 for (auto &RefExpr : VarList) {
9539 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009540 SourceLocation ELoc;
9541 SourceRange ERange;
9542 Expr *SimpleRefExpr = RefExpr;
9543 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9544 /*AllowArraySection=*/false);
9545 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009546 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009547 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009548 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00009549 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00009550 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009551 ValueDecl *D = Res.first;
9552 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00009553 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00009554
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009555 QualType Type = D->getType();
9556 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00009557
9558 // OpenMP [2.14.3.7, linear clause]
9559 // A list-item cannot appear in more than one linear clause.
9560 // A list-item that appears in a linear clause cannot appear in any
9561 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009562 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00009563 if (DVar.RefExpr) {
9564 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9565 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009566 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00009567 continue;
9568 }
9569
Alexey Bataevecba70f2016-04-12 11:02:11 +00009570 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +00009571 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009572 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +00009573
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009574 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009575 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
9576 D->hasAttrs() ? &D->getAttrs() : nullptr);
9577 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009578 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009579 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009580 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009581 DeclRefExpr *Ref = nullptr;
Alexey Bataev78849fb2016-03-09 09:49:00 +00009582 if (!VD) {
9583 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
9584 if (!IsOpenMPCapturedDecl(D)) {
9585 ExprCaptures.push_back(Ref->getDecl());
9586 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9587 ExprResult RefRes = DefaultLvalueConversion(Ref);
9588 if (!RefRes.isUsable())
9589 continue;
9590 ExprResult PostUpdateRes =
9591 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9592 SimpleRefExpr, RefRes.get());
9593 if (!PostUpdateRes.isUsable())
9594 continue;
9595 ExprPostUpdates.push_back(
9596 IgnoredValueConversions(PostUpdateRes.get()).get());
9597 }
9598 }
9599 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009600 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009601 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009602 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009603 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009604 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009605 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
9606 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
9607
9608 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
9609 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009610 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00009611 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00009612 }
9613
9614 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009615 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009616
9617 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00009618 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009619 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
9620 !Step->isInstantiationDependent() &&
9621 !Step->containsUnexpandedParameterPack()) {
9622 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00009623 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00009624 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009625 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00009626 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00009627
Alexander Musman3276a272015-03-21 10:12:56 +00009628 // Build var to save the step value.
9629 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009630 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00009631 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009632 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009633 ExprResult CalcStep =
9634 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009635 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00009636
Alexander Musman8dba6642014-04-22 13:09:42 +00009637 // Warn about zero linear step (it would be probably better specified as
9638 // making corresponding variables 'const').
9639 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00009640 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
9641 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00009642 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
9643 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00009644 if (!IsConstant && CalcStep.isUsable()) {
9645 // Calculate the step beforehand instead of doing this on each iteration.
9646 // (This is not used if the number of iterations may be kfold-ed).
9647 CalcStepExpr = CalcStep.get();
9648 }
Alexander Musman8dba6642014-04-22 13:09:42 +00009649 }
9650
Alexey Bataev182227b2015-08-20 10:54:39 +00009651 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
9652 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009653 StepExpr, CalcStepExpr,
9654 buildPreInits(Context, ExprCaptures),
9655 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +00009656}
9657
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009658static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
9659 Expr *NumIterations, Sema &SemaRef,
9660 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +00009661 // Walk the vars and build update/final expressions for the CodeGen.
9662 SmallVector<Expr *, 8> Updates;
9663 SmallVector<Expr *, 8> Finals;
9664 Expr *Step = Clause.getStep();
9665 Expr *CalcStep = Clause.getCalcStep();
9666 // OpenMP [2.14.3.7, linear clause]
9667 // If linear-step is not specified it is assumed to be 1.
9668 if (Step == nullptr)
9669 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009670 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +00009671 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009672 }
Alexander Musman3276a272015-03-21 10:12:56 +00009673 bool HasErrors = false;
9674 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009675 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009676 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00009677 for (auto &RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009678 SourceLocation ELoc;
9679 SourceRange ERange;
9680 Expr *SimpleRefExpr = RefExpr;
9681 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
9682 /*AllowArraySection=*/false);
9683 ValueDecl *D = Res.first;
9684 if (Res.second || !D) {
9685 Updates.push_back(nullptr);
9686 Finals.push_back(nullptr);
9687 HasErrors = true;
9688 continue;
9689 }
9690 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
9691 D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
9692 ->getMemberDecl();
9693 }
9694 auto &&Info = Stack->isLoopControlVariable(D);
Alexander Musman3276a272015-03-21 10:12:56 +00009695 Expr *InitExpr = *CurInit;
9696
9697 // Build privatized reference to the current linear var.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009698 auto DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009699 Expr *CapturedRef;
9700 if (LinKind == OMPC_LINEAR_uval)
9701 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
9702 else
9703 CapturedRef =
9704 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
9705 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
9706 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009707
9708 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009709 ExprResult Update;
9710 if (!Info.first) {
9711 Update =
9712 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
9713 InitExpr, IV, Step, /* Subtract */ false);
9714 } else
9715 Update = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009716 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
9717 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009718
9719 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009720 ExprResult Final;
9721 if (!Info.first) {
9722 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
9723 InitExpr, NumIterations, Step,
9724 /* Subtract */ false);
9725 } else
9726 Final = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009727 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
9728 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009729
Alexander Musman3276a272015-03-21 10:12:56 +00009730 if (!Update.isUsable() || !Final.isUsable()) {
9731 Updates.push_back(nullptr);
9732 Finals.push_back(nullptr);
9733 HasErrors = true;
9734 } else {
9735 Updates.push_back(Update.get());
9736 Finals.push_back(Final.get());
9737 }
Richard Trieucc3949d2016-02-18 22:34:54 +00009738 ++CurInit;
9739 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00009740 }
9741 Clause.setUpdates(Updates);
9742 Clause.setFinals(Finals);
9743 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00009744}
9745
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009746OMPClause *Sema::ActOnOpenMPAlignedClause(
9747 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
9748 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9749
9750 SmallVector<Expr *, 8> Vars;
9751 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +00009752 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9753 SourceLocation ELoc;
9754 SourceRange ERange;
9755 Expr *SimpleRefExpr = RefExpr;
9756 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9757 /*AllowArraySection=*/false);
9758 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009759 // It will be analyzed later.
9760 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009761 }
Alexey Bataev1efd1662016-03-29 10:59:56 +00009762 ValueDecl *D = Res.first;
9763 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009764 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009765
Alexey Bataev1efd1662016-03-29 10:59:56 +00009766 QualType QType = D->getType();
9767 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009768
9769 // OpenMP [2.8.1, simd construct, Restrictions]
9770 // The type of list items appearing in the aligned clause must be
9771 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009772 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009773 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +00009774 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009775 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009776 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009777 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +00009778 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009779 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +00009780 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009781 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009782 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009783 continue;
9784 }
9785
9786 // OpenMP [2.8.1, simd construct, Restrictions]
9787 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +00009788 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00009789 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009790 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
9791 << getOpenMPClauseName(OMPC_aligned);
9792 continue;
9793 }
9794
Alexey Bataev1efd1662016-03-29 10:59:56 +00009795 DeclRefExpr *Ref = nullptr;
9796 if (!VD && IsOpenMPCapturedDecl(D))
9797 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9798 Vars.push_back(DefaultFunctionArrayConversion(
9799 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
9800 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009801 }
9802
9803 // OpenMP [2.8.1, simd construct, Description]
9804 // The parameter of the aligned clause, alignment, must be a constant
9805 // positive integer expression.
9806 // If no optional parameter is specified, implementation-defined default
9807 // alignments for SIMD instructions on the target platforms are assumed.
9808 if (Alignment != nullptr) {
9809 ExprResult AlignResult =
9810 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
9811 if (AlignResult.isInvalid())
9812 return nullptr;
9813 Alignment = AlignResult.get();
9814 }
9815 if (Vars.empty())
9816 return nullptr;
9817
9818 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
9819 EndLoc, Vars, Alignment);
9820}
9821
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009822OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
9823 SourceLocation StartLoc,
9824 SourceLocation LParenLoc,
9825 SourceLocation EndLoc) {
9826 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009827 SmallVector<Expr *, 8> SrcExprs;
9828 SmallVector<Expr *, 8> DstExprs;
9829 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00009830 for (auto &RefExpr : VarList) {
9831 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
9832 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009833 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009834 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009835 SrcExprs.push_back(nullptr);
9836 DstExprs.push_back(nullptr);
9837 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009838 continue;
9839 }
9840
Alexey Bataeved09d242014-05-28 05:53:51 +00009841 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009842 // OpenMP [2.1, C/C++]
9843 // A list item is a variable name.
9844 // OpenMP [2.14.4.1, Restrictions, p.1]
9845 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00009846 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009847 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009848 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
9849 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009850 continue;
9851 }
9852
9853 Decl *D = DE->getDecl();
9854 VarDecl *VD = cast<VarDecl>(D);
9855
9856 QualType Type = VD->getType();
9857 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
9858 // It will be analyzed later.
9859 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009860 SrcExprs.push_back(nullptr);
9861 DstExprs.push_back(nullptr);
9862 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009863 continue;
9864 }
9865
9866 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
9867 // A list item that appears in a copyin clause must be threadprivate.
9868 if (!DSAStack->isThreadPrivate(VD)) {
9869 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00009870 << getOpenMPClauseName(OMPC_copyin)
9871 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009872 continue;
9873 }
9874
9875 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9876 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00009877 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009878 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009879 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009880 auto *SrcVD =
9881 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
9882 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00009883 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009884 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
9885 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009886 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
9887 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009888 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009889 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009890 // For arrays generate assignment operation for single element and replace
9891 // it by the original array element in CodeGen.
9892 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
9893 PseudoDstExpr, PseudoSrcExpr);
9894 if (AssignmentOp.isInvalid())
9895 continue;
9896 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
9897 /*DiscardedValue=*/true);
9898 if (AssignmentOp.isInvalid())
9899 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009900
9901 DSAStack->addDSA(VD, DE, OMPC_copyin);
9902 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009903 SrcExprs.push_back(PseudoSrcExpr);
9904 DstExprs.push_back(PseudoDstExpr);
9905 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009906 }
9907
Alexey Bataeved09d242014-05-28 05:53:51 +00009908 if (Vars.empty())
9909 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009910
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009911 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9912 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009913}
9914
Alexey Bataevbae9a792014-06-27 10:37:06 +00009915OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
9916 SourceLocation StartLoc,
9917 SourceLocation LParenLoc,
9918 SourceLocation EndLoc) {
9919 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00009920 SmallVector<Expr *, 8> SrcExprs;
9921 SmallVector<Expr *, 8> DstExprs;
9922 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009923 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +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) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009931 // It will be analyzed later.
9932 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009933 SrcExprs.push_back(nullptr);
9934 DstExprs.push_back(nullptr);
9935 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009936 }
Alexey Bataeve122da12016-03-17 10:50:17 +00009937 ValueDecl *D = Res.first;
9938 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +00009939 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009940
Alexey Bataeve122da12016-03-17 10:50:17 +00009941 QualType Type = D->getType();
9942 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009943
9944 // OpenMP [2.14.4.2, Restrictions, p.2]
9945 // A list item that appears in a copyprivate clause may not appear in a
9946 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +00009947 if (!VD || !DSAStack->isThreadPrivate(VD)) {
9948 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009949 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
9950 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009951 Diag(ELoc, diag::err_omp_wrong_dsa)
9952 << getOpenMPClauseName(DVar.CKind)
9953 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +00009954 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009955 continue;
9956 }
9957
9958 // OpenMP [2.11.4.2, Restrictions, p.1]
9959 // All list items that appear in a copyprivate clause must be either
9960 // threadprivate or private in the enclosing context.
9961 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009962 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009963 if (DVar.CKind == OMPC_shared) {
9964 Diag(ELoc, diag::err_omp_required_access)
9965 << getOpenMPClauseName(OMPC_copyprivate)
9966 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +00009967 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009968 continue;
9969 }
9970 }
9971 }
9972
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009973 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009974 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009975 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009976 << getOpenMPClauseName(OMPC_copyprivate) << Type
9977 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009978 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +00009979 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009980 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +00009981 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009982 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +00009983 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009984 continue;
9985 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009986
Alexey Bataevbae9a792014-06-27 10:37:06 +00009987 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9988 // A variable of class type (or array thereof) that appears in a
9989 // copyin clause requires an accessible, unambiguous copy assignment
9990 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009991 Type = Context.getBaseElementType(Type.getNonReferenceType())
9992 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00009993 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009994 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
9995 D->hasAttrs() ? &D->getAttrs() : nullptr);
9996 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009997 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009998 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
9999 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +000010000 auto *PseudoDstExpr =
Alexey Bataeve122da12016-03-17 10:50:17 +000010001 buildDeclRefExpr(*this, DstVD, Type, ELoc);
10002 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +000010003 PseudoDstExpr, PseudoSrcExpr);
10004 if (AssignmentOp.isInvalid())
10005 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +000010006 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +000010007 /*DiscardedValue=*/true);
10008 if (AssignmentOp.isInvalid())
10009 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010010
10011 // No need to mark vars as copyprivate, they are already threadprivate or
10012 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +000010013 assert(VD || IsOpenMPCapturedDecl(D));
10014 Vars.push_back(
10015 VD ? RefExpr->IgnoreParens()
10016 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000010017 SrcExprs.push_back(PseudoSrcExpr);
10018 DstExprs.push_back(PseudoDstExpr);
10019 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000010020 }
10021
10022 if (Vars.empty())
10023 return nullptr;
10024
Alexey Bataeva63048e2015-03-23 06:18:07 +000010025 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10026 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010027}
10028
Alexey Bataev6125da92014-07-21 11:26:11 +000010029OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
10030 SourceLocation StartLoc,
10031 SourceLocation LParenLoc,
10032 SourceLocation EndLoc) {
10033 if (VarList.empty())
10034 return nullptr;
10035
10036 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
10037}
Alexey Bataevdea47612014-07-23 07:46:59 +000010038
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010039OMPClause *
10040Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
10041 SourceLocation DepLoc, SourceLocation ColonLoc,
10042 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10043 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000010044 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010045 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000010046 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010047 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000010048 return nullptr;
10049 }
10050 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010051 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
10052 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000010053 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010054 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010055 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
10056 /*Last=*/OMPC_DEPEND_unknown, Except)
10057 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010058 return nullptr;
10059 }
10060 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000010061 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010062 llvm::APSInt DepCounter(/*BitWidth=*/32);
10063 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
10064 if (DepKind == OMPC_DEPEND_sink) {
10065 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
10066 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
10067 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010068 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010069 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010070 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
10071 DSAStack->getParentOrderedRegionParam()) {
10072 for (auto &RefExpr : VarList) {
10073 assert(RefExpr && "NULL expr in OpenMP shared clause.");
Alexey Bataev8b427062016-05-25 12:36:08 +000010074 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010075 // It will be analyzed later.
10076 Vars.push_back(RefExpr);
10077 continue;
10078 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010079
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010080 SourceLocation ELoc = RefExpr->getExprLoc();
10081 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
10082 if (DepKind == OMPC_DEPEND_sink) {
10083 if (DepCounter >= TotalDepCount) {
10084 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
10085 continue;
10086 }
10087 ++DepCounter;
10088 // OpenMP [2.13.9, Summary]
10089 // depend(dependence-type : vec), where dependence-type is:
10090 // 'sink' and where vec is the iteration vector, which has the form:
10091 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
10092 // where n is the value specified by the ordered clause in the loop
10093 // directive, xi denotes the loop iteration variable of the i-th nested
10094 // loop associated with the loop directive, and di is a constant
10095 // non-negative integer.
Alexey Bataev8b427062016-05-25 12:36:08 +000010096 if (CurContext->isDependentContext()) {
10097 // It will be analyzed later.
10098 Vars.push_back(RefExpr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010099 continue;
10100 }
Alexey Bataev8b427062016-05-25 12:36:08 +000010101 SimpleExpr = SimpleExpr->IgnoreImplicit();
10102 OverloadedOperatorKind OOK = OO_None;
10103 SourceLocation OOLoc;
10104 Expr *LHS = SimpleExpr;
10105 Expr *RHS = nullptr;
10106 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
10107 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
10108 OOLoc = BO->getOperatorLoc();
10109 LHS = BO->getLHS()->IgnoreParenImpCasts();
10110 RHS = BO->getRHS()->IgnoreParenImpCasts();
10111 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
10112 OOK = OCE->getOperator();
10113 OOLoc = OCE->getOperatorLoc();
10114 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10115 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
10116 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
10117 OOK = MCE->getMethodDecl()
10118 ->getNameInfo()
10119 .getName()
10120 .getCXXOverloadedOperator();
10121 OOLoc = MCE->getCallee()->getExprLoc();
10122 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
10123 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10124 }
10125 SourceLocation ELoc;
10126 SourceRange ERange;
10127 auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
10128 /*AllowArraySection=*/false);
10129 if (Res.second) {
10130 // It will be analyzed later.
10131 Vars.push_back(RefExpr);
10132 }
10133 ValueDecl *D = Res.first;
10134 if (!D)
10135 continue;
10136
10137 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
10138 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
10139 continue;
10140 }
10141 if (RHS) {
10142 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
10143 RHS, OMPC_depend, /*StrictlyPositive=*/false);
10144 if (RHSRes.isInvalid())
10145 continue;
10146 }
10147 if (!CurContext->isDependentContext() &&
10148 DSAStack->getParentOrderedRegionParam() &&
10149 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
10150 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
10151 << DSAStack->getParentLoopControlVariable(
10152 DepCounter.getZExtValue());
10153 continue;
10154 }
10155 OpsOffs.push_back({RHS, OOK});
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010156 } else {
10157 // OpenMP [2.11.1.1, Restrictions, p.3]
10158 // A variable that is part of another variable (such as a field of a
10159 // structure) but is not an array element or an array section cannot
10160 // appear in a depend clause.
10161 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
10162 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
10163 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
10164 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
10165 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +000010166 (ASE &&
10167 !ASE->getBase()
10168 ->getType()
10169 .getNonReferenceType()
10170 ->isPointerType() &&
10171 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010172 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
10173 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010174 continue;
10175 }
10176 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010177 Vars.push_back(RefExpr->IgnoreParenImpCasts());
10178 }
10179
10180 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
10181 TotalDepCount > VarList.size() &&
10182 DSAStack->getParentOrderedRegionParam()) {
10183 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
10184 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
10185 }
10186 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
10187 Vars.empty())
10188 return nullptr;
10189 }
Alexey Bataev8b427062016-05-25 12:36:08 +000010190 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10191 DepKind, DepLoc, ColonLoc, Vars);
10192 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source)
10193 DSAStack->addDoacrossDependClause(C, OpsOffs);
10194 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010195}
Michael Wonge710d542015-08-07 16:16:36 +000010196
10197OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
10198 SourceLocation LParenLoc,
10199 SourceLocation EndLoc) {
10200 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +000010201
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010202 // OpenMP [2.9.1, Restrictions]
10203 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010204 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
10205 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010206 return nullptr;
10207
Michael Wonge710d542015-08-07 16:16:36 +000010208 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10209}
Kelvin Li0bff7af2015-11-23 05:32:03 +000010210
10211static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
10212 DSAStackTy *Stack, CXXRecordDecl *RD) {
10213 if (!RD || RD->isInvalidDecl())
10214 return true;
10215
Alexey Bataevc9bd03d2015-12-17 06:55:08 +000010216 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
10217 if (auto *CTD = CTSD->getSpecializedTemplate())
10218 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010219 auto QTy = SemaRef.Context.getRecordType(RD);
10220 if (RD->isDynamicClass()) {
10221 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10222 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
10223 return false;
10224 }
10225 auto *DC = RD;
10226 bool IsCorrect = true;
10227 for (auto *I : DC->decls()) {
10228 if (I) {
10229 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
10230 if (MD->isStatic()) {
10231 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10232 SemaRef.Diag(MD->getLocation(),
10233 diag::note_omp_static_member_in_target);
10234 IsCorrect = false;
10235 }
10236 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
10237 if (VD->isStaticDataMember()) {
10238 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10239 SemaRef.Diag(VD->getLocation(),
10240 diag::note_omp_static_member_in_target);
10241 IsCorrect = false;
10242 }
10243 }
10244 }
10245 }
10246
10247 for (auto &I : RD->bases()) {
10248 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
10249 I.getType()->getAsCXXRecordDecl()))
10250 IsCorrect = false;
10251 }
10252 return IsCorrect;
10253}
10254
10255static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
10256 DSAStackTy *Stack, QualType QTy) {
10257 NamedDecl *ND;
10258 if (QTy->isIncompleteType(&ND)) {
10259 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
10260 return false;
10261 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
10262 if (!RD->isInvalidDecl() &&
10263 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
10264 return false;
10265 }
10266 return true;
10267}
10268
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010269/// \brief Return true if it can be proven that the provided array expression
10270/// (array section or array subscript) does NOT specify the whole size of the
10271/// array whose base type is \a BaseQTy.
10272static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
10273 const Expr *E,
10274 QualType BaseQTy) {
10275 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10276
10277 // If this is an array subscript, it refers to the whole size if the size of
10278 // the dimension is constant and equals 1. Also, an array section assumes the
10279 // format of an array subscript if no colon is used.
10280 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
10281 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10282 return ATy->getSize().getSExtValue() != 1;
10283 // Size can't be evaluated statically.
10284 return false;
10285 }
10286
10287 assert(OASE && "Expecting array section if not an array subscript.");
10288 auto *LowerBound = OASE->getLowerBound();
10289 auto *Length = OASE->getLength();
10290
10291 // If there is a lower bound that does not evaluates to zero, we are not
10292 // convering the whole dimension.
10293 if (LowerBound) {
10294 llvm::APSInt ConstLowerBound;
10295 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
10296 return false; // Can't get the integer value as a constant.
10297 if (ConstLowerBound.getSExtValue())
10298 return true;
10299 }
10300
10301 // If we don't have a length we covering the whole dimension.
10302 if (!Length)
10303 return false;
10304
10305 // If the base is a pointer, we don't have a way to get the size of the
10306 // pointee.
10307 if (BaseQTy->isPointerType())
10308 return false;
10309
10310 // We can only check if the length is the same as the size of the dimension
10311 // if we have a constant array.
10312 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
10313 if (!CATy)
10314 return false;
10315
10316 llvm::APSInt ConstLength;
10317 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10318 return false; // Can't get the integer value as a constant.
10319
10320 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
10321}
10322
10323// Return true if it can be proven that the provided array expression (array
10324// section or array subscript) does NOT specify a single element of the array
10325// whose base type is \a BaseQTy.
10326static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
10327 const Expr *E,
10328 QualType BaseQTy) {
10329 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10330
10331 // An array subscript always refer to a single element. Also, an array section
10332 // assumes the format of an array subscript if no colon is used.
10333 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
10334 return false;
10335
10336 assert(OASE && "Expecting array section if not an array subscript.");
10337 auto *Length = OASE->getLength();
10338
10339 // If we don't have a length we have to check if the array has unitary size
10340 // for this dimension. Also, we should always expect a length if the base type
10341 // is pointer.
10342 if (!Length) {
10343 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10344 return ATy->getSize().getSExtValue() != 1;
10345 // We cannot assume anything.
10346 return false;
10347 }
10348
10349 // Check if the length evaluates to 1.
10350 llvm::APSInt ConstLength;
10351 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10352 return false; // Can't get the integer value as a constant.
10353
10354 return ConstLength.getSExtValue() != 1;
10355}
10356
Samuel Antao661c0902016-05-26 17:39:58 +000010357// Return the expression of the base of the mappable expression or null if it
10358// cannot be determined and do all the necessary checks to see if the expression
10359// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000010360// components of the expression.
10361static Expr *CheckMapClauseExpressionBase(
10362 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000010363 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
10364 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010365 SourceLocation ELoc = E->getExprLoc();
10366 SourceRange ERange = E->getSourceRange();
10367
10368 // The base of elements of list in a map clause have to be either:
10369 // - a reference to variable or field.
10370 // - a member expression.
10371 // - an array expression.
10372 //
10373 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
10374 // reference to 'r'.
10375 //
10376 // If we have:
10377 //
10378 // struct SS {
10379 // Bla S;
10380 // foo() {
10381 // #pragma omp target map (S.Arr[:12]);
10382 // }
10383 // }
10384 //
10385 // We want to retrieve the member expression 'this->S';
10386
10387 Expr *RelevantExpr = nullptr;
10388
Samuel Antao5de996e2016-01-22 20:21:36 +000010389 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
10390 // If a list item is an array section, it must specify contiguous storage.
10391 //
10392 // For this restriction it is sufficient that we make sure only references
10393 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010394 // exist except in the rightmost expression (unless they cover the whole
10395 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000010396 //
10397 // r.ArrS[3:5].Arr[6:7]
10398 //
10399 // r.ArrS[3:5].x
10400 //
10401 // but these would be valid:
10402 // r.ArrS[3].Arr[6:7]
10403 //
10404 // r.ArrS[3].x
10405
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010406 bool AllowUnitySizeArraySection = true;
10407 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000010408
Dmitry Polukhin644a9252016-03-11 07:58:34 +000010409 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010410 E = E->IgnoreParenImpCasts();
10411
10412 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
10413 if (!isa<VarDecl>(CurE->getDecl()))
10414 break;
10415
10416 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010417
10418 // If we got a reference to a declaration, we should not expect any array
10419 // section before that.
10420 AllowUnitySizeArraySection = false;
10421 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010422
10423 // Record the component.
10424 CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent(
10425 CurE, CurE->getDecl()));
Samuel Antao5de996e2016-01-22 20:21:36 +000010426 continue;
10427 }
10428
10429 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
10430 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
10431
10432 if (isa<CXXThisExpr>(BaseE))
10433 // We found a base expression: this->Val.
10434 RelevantExpr = CurE;
10435 else
10436 E = BaseE;
10437
10438 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
10439 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
10440 << CurE->getSourceRange();
10441 break;
10442 }
10443
10444 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
10445
10446 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
10447 // A bit-field cannot appear in a map clause.
10448 //
10449 if (FD->isBitField()) {
Samuel Antao661c0902016-05-26 17:39:58 +000010450 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
10451 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010452 break;
10453 }
10454
10455 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10456 // If the type of a list item is a reference to a type T then the type
10457 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010458 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010459
10460 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
10461 // A list item cannot be a variable that is a member of a structure with
10462 // a union type.
10463 //
10464 if (auto *RT = CurType->getAs<RecordType>())
10465 if (RT->isUnionType()) {
10466 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
10467 << CurE->getSourceRange();
10468 break;
10469 }
10470
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010471 // If we got a member expression, we should not expect any array section
10472 // before that:
10473 //
10474 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
10475 // If a list item is an element of a structure, only the rightmost symbol
10476 // of the variable reference can be an array section.
10477 //
10478 AllowUnitySizeArraySection = false;
10479 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010480
10481 // Record the component.
10482 CurComponents.push_back(
10483 OMPClauseMappableExprCommon::MappableComponent(CurE, FD));
Samuel Antao5de996e2016-01-22 20:21:36 +000010484 continue;
10485 }
10486
10487 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
10488 E = CurE->getBase()->IgnoreParenImpCasts();
10489
10490 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
10491 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10492 << 0 << CurE->getSourceRange();
10493 break;
10494 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010495
10496 // If we got an array subscript that express the whole dimension we
10497 // can have any array expressions before. If it only expressing part of
10498 // the dimension, we can only have unitary-size array expressions.
10499 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
10500 E->getType()))
10501 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010502
10503 // Record the component - we don't have any declaration associated.
10504 CurComponents.push_back(
10505 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010506 continue;
10507 }
10508
10509 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010510 E = CurE->getBase()->IgnoreParenImpCasts();
10511
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010512 auto CurType =
10513 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
10514
Samuel Antao5de996e2016-01-22 20:21:36 +000010515 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10516 // If the type of a list item is a reference to a type T then the type
10517 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000010518 if (CurType->isReferenceType())
10519 CurType = CurType->getPointeeType();
10520
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010521 bool IsPointer = CurType->isAnyPointerType();
10522
10523 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010524 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10525 << 0 << CurE->getSourceRange();
10526 break;
10527 }
10528
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010529 bool NotWhole =
10530 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
10531 bool NotUnity =
10532 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
10533
10534 if (AllowWholeSizeArraySection && AllowUnitySizeArraySection) {
10535 // Any array section is currently allowed.
10536 //
10537 // If this array section refers to the whole dimension we can still
10538 // accept other array sections before this one, except if the base is a
10539 // pointer. Otherwise, only unitary sections are accepted.
10540 if (NotWhole || IsPointer)
10541 AllowWholeSizeArraySection = false;
10542 } else if ((AllowUnitySizeArraySection && NotUnity) ||
10543 (AllowWholeSizeArraySection && NotWhole)) {
10544 // A unity or whole array section is not allowed and that is not
10545 // compatible with the properties of the current array section.
10546 SemaRef.Diag(
10547 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
10548 << CurE->getSourceRange();
10549 break;
10550 }
Samuel Antao90927002016-04-26 14:54:23 +000010551
10552 // Record the component - we don't have any declaration associated.
10553 CurComponents.push_back(
10554 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010555 continue;
10556 }
10557
10558 // If nothing else worked, this is not a valid map clause expression.
10559 SemaRef.Diag(ELoc,
10560 diag::err_omp_expected_named_var_member_or_array_expression)
10561 << ERange;
10562 break;
10563 }
10564
10565 return RelevantExpr;
10566}
10567
10568// Return true if expression E associated with value VD has conflicts with other
10569// map information.
Samuel Antao90927002016-04-26 14:54:23 +000010570static bool CheckMapConflicts(
10571 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
10572 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000010573 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
10574 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010575 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000010576 SourceLocation ELoc = E->getExprLoc();
10577 SourceRange ERange = E->getSourceRange();
10578
10579 // In order to easily check the conflicts we need to match each component of
10580 // the expression under test with the components of the expressions that are
10581 // already in the stack.
10582
Samuel Antao5de996e2016-01-22 20:21:36 +000010583 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010584 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010585 "Map clause expression with unexpected base!");
10586
10587 // Variables to help detecting enclosing problems in data environment nests.
10588 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000010589 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000010590
Samuel Antao90927002016-04-26 14:54:23 +000010591 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
10592 VD, CurrentRegionOnly,
10593 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
10594 StackComponents) -> bool {
10595
Samuel Antao5de996e2016-01-22 20:21:36 +000010596 assert(!StackComponents.empty() &&
10597 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010598 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010599 "Map clause expression with unexpected base!");
10600
Samuel Antao90927002016-04-26 14:54:23 +000010601 // The whole expression in the stack.
10602 auto *RE = StackComponents.front().getAssociatedExpression();
10603
Samuel Antao5de996e2016-01-22 20:21:36 +000010604 // Expressions must start from the same base. Here we detect at which
10605 // point both expressions diverge from each other and see if we can
10606 // detect if the memory referred to both expressions is contiguous and
10607 // do not overlap.
10608 auto CI = CurComponents.rbegin();
10609 auto CE = CurComponents.rend();
10610 auto SI = StackComponents.rbegin();
10611 auto SE = StackComponents.rend();
10612 for (; CI != CE && SI != SE; ++CI, ++SI) {
10613
10614 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
10615 // At most one list item can be an array item derived from a given
10616 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000010617 if (CurrentRegionOnly &&
10618 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
10619 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
10620 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
10621 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
10622 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000010623 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000010624 << CI->getAssociatedExpression()->getSourceRange();
10625 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
10626 diag::note_used_here)
10627 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000010628 return true;
10629 }
10630
10631 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000010632 if (CI->getAssociatedExpression()->getStmtClass() !=
10633 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000010634 break;
10635
10636 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000010637 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000010638 break;
10639 }
10640
10641 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10642 // List items of map clauses in the same construct must not share
10643 // original storage.
10644 //
10645 // If the expressions are exactly the same or one is a subset of the
10646 // other, it means they are sharing storage.
10647 if (CI == CE && SI == SE) {
10648 if (CurrentRegionOnly) {
Samuel Antao661c0902016-05-26 17:39:58 +000010649 if (CKind == OMPC_map)
10650 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10651 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000010652 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000010653 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10654 << ERange;
10655 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010656 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10657 << RE->getSourceRange();
10658 return true;
10659 } else {
10660 // If we find the same expression in the enclosing data environment,
10661 // that is legal.
10662 IsEnclosedByDataEnvironmentExpr = true;
10663 return false;
10664 }
10665 }
10666
Samuel Antao90927002016-04-26 14:54:23 +000010667 QualType DerivedType =
10668 std::prev(CI)->getAssociatedDeclaration()->getType();
10669 SourceLocation DerivedLoc =
10670 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000010671
10672 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10673 // If the type of a list item is a reference to a type T then the type
10674 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010675 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010676
10677 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
10678 // A variable for which the type is pointer and an array section
10679 // derived from that variable must not appear as list items of map
10680 // clauses of the same construct.
10681 //
10682 // Also, cover one of the cases in:
10683 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10684 // If any part of the original storage of a list item has corresponding
10685 // storage in the device data environment, all of the original storage
10686 // must have corresponding storage in the device data environment.
10687 //
10688 if (DerivedType->isAnyPointerType()) {
10689 if (CI == CE || SI == SE) {
10690 SemaRef.Diag(
10691 DerivedLoc,
10692 diag::err_omp_pointer_mapped_along_with_derived_section)
10693 << DerivedLoc;
10694 } else {
10695 assert(CI != CE && SI != SE);
10696 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
10697 << DerivedLoc;
10698 }
10699 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10700 << RE->getSourceRange();
10701 return true;
10702 }
10703
10704 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10705 // List items of map clauses in the same construct must not share
10706 // original storage.
10707 //
10708 // An expression is a subset of the other.
10709 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Samuel Antao661c0902016-05-26 17:39:58 +000010710 if (CKind == OMPC_map)
10711 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10712 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000010713 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000010714 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10715 << ERange;
10716 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010717 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10718 << RE->getSourceRange();
10719 return true;
10720 }
10721
10722 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000010723 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000010724 if (!CurrentRegionOnly && SI != SE)
10725 EnclosingExpr = RE;
10726
10727 // The current expression is a subset of the expression in the data
10728 // environment.
10729 IsEnclosedByDataEnvironmentExpr |=
10730 (!CurrentRegionOnly && CI != CE && SI == SE);
10731
10732 return false;
10733 });
10734
10735 if (CurrentRegionOnly)
10736 return FoundError;
10737
10738 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10739 // If any part of the original storage of a list item has corresponding
10740 // storage in the device data environment, all of the original storage must
10741 // have corresponding storage in the device data environment.
10742 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
10743 // If a list item is an element of a structure, and a different element of
10744 // the structure has a corresponding list item in the device data environment
10745 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000010746 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000010747 // data environment prior to the task encountering the construct.
10748 //
10749 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
10750 SemaRef.Diag(ELoc,
10751 diag::err_omp_original_storage_is_shared_and_does_not_contain)
10752 << ERange;
10753 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
10754 << EnclosingExpr->getSourceRange();
10755 return true;
10756 }
10757
10758 return FoundError;
10759}
10760
Samuel Antao661c0902016-05-26 17:39:58 +000010761namespace {
10762// Utility struct that gathers all the related lists associated with a mappable
10763// expression.
10764struct MappableVarListInfo final {
10765 // The list of expressions.
10766 ArrayRef<Expr *> VarList;
10767 // The list of processed expressions.
10768 SmallVector<Expr *, 16> ProcessedVarList;
10769 // The mappble components for each expression.
10770 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
10771 // The base declaration of the variable.
10772 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
10773
10774 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
10775 // We have a list of components and base declarations for each entry in the
10776 // variable list.
10777 VarComponents.reserve(VarList.size());
10778 VarBaseDeclarations.reserve(VarList.size());
10779 }
10780};
10781}
10782
10783// Check the validity of the provided variable list for the provided clause kind
10784// \a CKind. In the check process the valid expressions, and mappable expression
10785// components and variables are extracted and used to fill \a Vars,
10786// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
10787// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
10788static void
10789checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
10790 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
10791 SourceLocation StartLoc,
10792 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
10793 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000010794 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
10795 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000010796 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010797
Samuel Antao90927002016-04-26 14:54:23 +000010798 // Keep track of the mappable components and base declarations in this clause.
10799 // Each entry in the list is going to have a list of components associated. We
10800 // record each set of the components so that we can build the clause later on.
10801 // In the end we should have the same amount of declarations and component
10802 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000010803
Samuel Antao661c0902016-05-26 17:39:58 +000010804 for (auto &RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000010805 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010806 SourceLocation ELoc = RE->getExprLoc();
10807
Kelvin Li0bff7af2015-11-23 05:32:03 +000010808 auto *VE = RE->IgnoreParenLValueCasts();
10809
10810 if (VE->isValueDependent() || VE->isTypeDependent() ||
10811 VE->isInstantiationDependent() ||
10812 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010813 // We can only analyze this information once the missing information is
10814 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000010815 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010816 continue;
10817 }
10818
10819 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010820
Samuel Antao5de996e2016-01-22 20:21:36 +000010821 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000010822 SemaRef.Diag(ELoc,
10823 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000010824 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010825 continue;
10826 }
10827
Samuel Antao90927002016-04-26 14:54:23 +000010828 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
10829 ValueDecl *CurDeclaration = nullptr;
10830
10831 // Obtain the array or member expression bases if required. Also, fill the
10832 // components array with all the components identified in the process.
Samuel Antao661c0902016-05-26 17:39:58 +000010833 auto *BE =
10834 CheckMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010835 if (!BE)
10836 continue;
10837
Samuel Antao90927002016-04-26 14:54:23 +000010838 assert(!CurComponents.empty() &&
10839 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010840
Samuel Antao90927002016-04-26 14:54:23 +000010841 // For the following checks, we rely on the base declaration which is
10842 // expected to be associated with the last component. The declaration is
10843 // expected to be a variable or a field (if 'this' is being mapped).
10844 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
10845 assert(CurDeclaration && "Null decl on map clause.");
10846 assert(
10847 CurDeclaration->isCanonicalDecl() &&
10848 "Expecting components to have associated only canonical declarations.");
10849
10850 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
10851 auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000010852
10853 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000010854 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000010855
10856 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000010857 // threadprivate variables cannot appear in a map clause.
10858 // OpenMP 4.5 [2.10.5, target update Construct]
10859 // threadprivate variables cannot appear in a from clause.
10860 if (VD && DSAS->isThreadPrivate(VD)) {
10861 auto DVar = DSAS->getTopDSA(VD, false);
10862 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
10863 << getOpenMPClauseName(CKind);
10864 ReportOriginalDSA(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010865 continue;
10866 }
10867
Samuel Antao5de996e2016-01-22 20:21:36 +000010868 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
10869 // A list item cannot appear in both a map clause and a data-sharing
10870 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010871
Samuel Antao5de996e2016-01-22 20:21:36 +000010872 // Check conflicts with other map clause expressions. We check the conflicts
10873 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000010874 // environment, because the restrictions are different. We only have to
10875 // check conflicts across regions for the map clauses.
10876 if (CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
10877 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000010878 break;
Samuel Antao661c0902016-05-26 17:39:58 +000010879 if (CKind == OMPC_map &&
10880 CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
10881 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000010882 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000010883
Samuel Antao661c0902016-05-26 17:39:58 +000010884 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000010885 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10886 // If the type of a list item is a reference to a type T then the type will
10887 // be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010888 QualType Type = CurDeclaration->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010889
Samuel Antao661c0902016-05-26 17:39:58 +000010890 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
10891 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000010892 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000010893 // A list item must have a mappable type.
Samuel Antao661c0902016-05-26 17:39:58 +000010894 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
10895 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000010896 continue;
10897
Samuel Antao661c0902016-05-26 17:39:58 +000010898 if (CKind == OMPC_map) {
10899 // target enter data
10900 // OpenMP [2.10.2, Restrictions, p. 99]
10901 // A map-type must be specified in all map clauses and must be either
10902 // to or alloc.
10903 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
10904 if (DKind == OMPD_target_enter_data &&
10905 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
10906 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
10907 << (IsMapTypeImplicit ? 1 : 0)
10908 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
10909 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010910 continue;
10911 }
Samuel Antao661c0902016-05-26 17:39:58 +000010912
10913 // target exit_data
10914 // OpenMP [2.10.3, Restrictions, p. 102]
10915 // A map-type must be specified in all map clauses and must be either
10916 // from, release, or delete.
10917 if (DKind == OMPD_target_exit_data &&
10918 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
10919 MapType == OMPC_MAP_delete)) {
10920 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
10921 << (IsMapTypeImplicit ? 1 : 0)
10922 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
10923 << getOpenMPDirectiveName(DKind);
10924 continue;
10925 }
10926
10927 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10928 // A list item cannot appear in both a map clause and a data-sharing
10929 // attribute clause on the same construct
10930 if (DKind == OMPD_target && VD) {
10931 auto DVar = DSAS->getTopDSA(VD, false);
10932 if (isOpenMPPrivate(DVar.CKind)) {
10933 SemaRef.Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
10934 << getOpenMPClauseName(DVar.CKind)
10935 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
10936 ReportOriginalDSA(SemaRef, DSAS, CurDeclaration, DVar);
10937 continue;
10938 }
10939 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010940 }
10941
Samuel Antao90927002016-04-26 14:54:23 +000010942 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000010943 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000010944
10945 // Store the components in the stack so that they can be used to check
10946 // against other clauses later on.
Samuel Antao661c0902016-05-26 17:39:58 +000010947 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents);
Samuel Antao90927002016-04-26 14:54:23 +000010948
10949 // Save the components and declaration to create the clause. For purposes of
10950 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000010951 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000010952 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
10953 MVLI.VarComponents.back().append(CurComponents.begin(),
10954 CurComponents.end());
10955 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
10956 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010957 }
Samuel Antao661c0902016-05-26 17:39:58 +000010958}
10959
10960OMPClause *
10961Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
10962 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
10963 SourceLocation MapLoc, SourceLocation ColonLoc,
10964 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10965 SourceLocation LParenLoc, SourceLocation EndLoc) {
10966 MappableVarListInfo MVLI(VarList);
10967 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
10968 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010969
Samuel Antao5de996e2016-01-22 20:21:36 +000010970 // We need to produce a map clause even if we don't have variables so that
10971 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000010972 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10973 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
10974 MVLI.VarComponents, MapTypeModifier, MapType,
10975 IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010976}
Kelvin Li099bb8c2015-11-24 20:50:12 +000010977
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010978QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
10979 TypeResult ParsedType) {
10980 assert(ParsedType.isUsable());
10981
10982 QualType ReductionType = GetTypeFromParser(ParsedType.get());
10983 if (ReductionType.isNull())
10984 return QualType();
10985
10986 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
10987 // A type name in a declare reduction directive cannot be a function type, an
10988 // array type, a reference type, or a type qualified with const, volatile or
10989 // restrict.
10990 if (ReductionType.hasQualifiers()) {
10991 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
10992 return QualType();
10993 }
10994
10995 if (ReductionType->isFunctionType()) {
10996 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
10997 return QualType();
10998 }
10999 if (ReductionType->isReferenceType()) {
11000 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
11001 return QualType();
11002 }
11003 if (ReductionType->isArrayType()) {
11004 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
11005 return QualType();
11006 }
11007 return ReductionType;
11008}
11009
11010Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
11011 Scope *S, DeclContext *DC, DeclarationName Name,
11012 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
11013 AccessSpecifier AS, Decl *PrevDeclInScope) {
11014 SmallVector<Decl *, 8> Decls;
11015 Decls.reserve(ReductionTypes.size());
11016
11017 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
11018 ForRedeclaration);
11019 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
11020 // A reduction-identifier may not be re-declared in the current scope for the
11021 // same type or for a type that is compatible according to the base language
11022 // rules.
11023 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
11024 OMPDeclareReductionDecl *PrevDRD = nullptr;
11025 bool InCompoundScope = true;
11026 if (S != nullptr) {
11027 // Find previous declaration with the same name not referenced in other
11028 // declarations.
11029 FunctionScopeInfo *ParentFn = getEnclosingFunction();
11030 InCompoundScope =
11031 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
11032 LookupName(Lookup, S);
11033 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
11034 /*AllowInlineNamespace=*/false);
11035 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
11036 auto Filter = Lookup.makeFilter();
11037 while (Filter.hasNext()) {
11038 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
11039 if (InCompoundScope) {
11040 auto I = UsedAsPrevious.find(PrevDecl);
11041 if (I == UsedAsPrevious.end())
11042 UsedAsPrevious[PrevDecl] = false;
11043 if (auto *D = PrevDecl->getPrevDeclInScope())
11044 UsedAsPrevious[D] = true;
11045 }
11046 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
11047 PrevDecl->getLocation();
11048 }
11049 Filter.done();
11050 if (InCompoundScope) {
11051 for (auto &PrevData : UsedAsPrevious) {
11052 if (!PrevData.second) {
11053 PrevDRD = PrevData.first;
11054 break;
11055 }
11056 }
11057 }
11058 } else if (PrevDeclInScope != nullptr) {
11059 auto *PrevDRDInScope = PrevDRD =
11060 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
11061 do {
11062 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
11063 PrevDRDInScope->getLocation();
11064 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
11065 } while (PrevDRDInScope != nullptr);
11066 }
11067 for (auto &TyData : ReductionTypes) {
11068 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
11069 bool Invalid = false;
11070 if (I != PreviousRedeclTypes.end()) {
11071 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
11072 << TyData.first;
11073 Diag(I->second, diag::note_previous_definition);
11074 Invalid = true;
11075 }
11076 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
11077 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
11078 Name, TyData.first, PrevDRD);
11079 DC->addDecl(DRD);
11080 DRD->setAccess(AS);
11081 Decls.push_back(DRD);
11082 if (Invalid)
11083 DRD->setInvalidDecl();
11084 else
11085 PrevDRD = DRD;
11086 }
11087
11088 return DeclGroupPtrTy::make(
11089 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
11090}
11091
11092void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
11093 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11094
11095 // Enter new function scope.
11096 PushFunctionScope();
11097 getCurFunction()->setHasBranchProtectedScope();
11098 getCurFunction()->setHasOMPDeclareReductionCombiner();
11099
11100 if (S != nullptr)
11101 PushDeclContext(S, DRD);
11102 else
11103 CurContext = DRD;
11104
11105 PushExpressionEvaluationContext(PotentiallyEvaluated);
11106
11107 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011108 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
11109 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
11110 // uses semantics of argument handles by value, but it should be passed by
11111 // reference. C lang does not support references, so pass all parameters as
11112 // pointers.
11113 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011114 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011115 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011116 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
11117 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
11118 // uses semantics of argument handles by value, but it should be passed by
11119 // reference. C lang does not support references, so pass all parameters as
11120 // pointers.
11121 // Create 'T omp_out;' variable.
11122 auto *OmpOutParm =
11123 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
11124 if (S != nullptr) {
11125 PushOnScopeChains(OmpInParm, S);
11126 PushOnScopeChains(OmpOutParm, S);
11127 } else {
11128 DRD->addDecl(OmpInParm);
11129 DRD->addDecl(OmpOutParm);
11130 }
11131}
11132
11133void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
11134 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11135 DiscardCleanupsInEvaluationContext();
11136 PopExpressionEvaluationContext();
11137
11138 PopDeclContext();
11139 PopFunctionScopeInfo();
11140
11141 if (Combiner != nullptr)
11142 DRD->setCombiner(Combiner);
11143 else
11144 DRD->setInvalidDecl();
11145}
11146
11147void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
11148 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11149
11150 // Enter new function scope.
11151 PushFunctionScope();
11152 getCurFunction()->setHasBranchProtectedScope();
11153
11154 if (S != nullptr)
11155 PushDeclContext(S, DRD);
11156 else
11157 CurContext = DRD;
11158
11159 PushExpressionEvaluationContext(PotentiallyEvaluated);
11160
11161 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011162 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
11163 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
11164 // uses semantics of argument handles by value, but it should be passed by
11165 // reference. C lang does not support references, so pass all parameters as
11166 // pointers.
11167 // Create 'T omp_priv;' variable.
11168 auto *OmpPrivParm =
11169 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011170 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
11171 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
11172 // uses semantics of argument handles by value, but it should be passed by
11173 // reference. C lang does not support references, so pass all parameters as
11174 // pointers.
11175 // Create 'T omp_orig;' variable.
11176 auto *OmpOrigParm =
11177 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011178 if (S != nullptr) {
11179 PushOnScopeChains(OmpPrivParm, S);
11180 PushOnScopeChains(OmpOrigParm, S);
11181 } else {
11182 DRD->addDecl(OmpPrivParm);
11183 DRD->addDecl(OmpOrigParm);
11184 }
11185}
11186
11187void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
11188 Expr *Initializer) {
11189 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11190 DiscardCleanupsInEvaluationContext();
11191 PopExpressionEvaluationContext();
11192
11193 PopDeclContext();
11194 PopFunctionScopeInfo();
11195
11196 if (Initializer != nullptr)
11197 DRD->setInitializer(Initializer);
11198 else
11199 DRD->setInvalidDecl();
11200}
11201
11202Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
11203 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
11204 for (auto *D : DeclReductions.get()) {
11205 if (IsValid) {
11206 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11207 if (S != nullptr)
11208 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
11209 } else
11210 D->setInvalidDecl();
11211 }
11212 return DeclReductions;
11213}
11214
Kelvin Li099bb8c2015-11-24 20:50:12 +000011215OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
11216 SourceLocation StartLoc,
11217 SourceLocation LParenLoc,
11218 SourceLocation EndLoc) {
11219 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011220
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011221 // OpenMP [teams Constrcut, Restrictions]
11222 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011223 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
11224 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011225 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011226
11227 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11228}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011229
11230OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
11231 SourceLocation StartLoc,
11232 SourceLocation LParenLoc,
11233 SourceLocation EndLoc) {
11234 Expr *ValExpr = ThreadLimit;
11235
11236 // OpenMP [teams Constrcut, Restrictions]
11237 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011238 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
11239 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011240 return nullptr;
11241
11242 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
11243 EndLoc);
11244}
Alexey Bataeva0569352015-12-01 10:17:31 +000011245
11246OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
11247 SourceLocation StartLoc,
11248 SourceLocation LParenLoc,
11249 SourceLocation EndLoc) {
11250 Expr *ValExpr = Priority;
11251
11252 // OpenMP [2.9.1, task Constrcut]
11253 // The priority-value is a non-negative numerical scalar expression.
11254 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
11255 /*StrictlyPositive=*/false))
11256 return nullptr;
11257
11258 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11259}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000011260
11261OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
11262 SourceLocation StartLoc,
11263 SourceLocation LParenLoc,
11264 SourceLocation EndLoc) {
11265 Expr *ValExpr = Grainsize;
11266
11267 // OpenMP [2.9.2, taskloop Constrcut]
11268 // The parameter of the grainsize clause must be a positive integer
11269 // expression.
11270 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
11271 /*StrictlyPositive=*/true))
11272 return nullptr;
11273
11274 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11275}
Alexey Bataev382967a2015-12-08 12:06:20 +000011276
11277OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
11278 SourceLocation StartLoc,
11279 SourceLocation LParenLoc,
11280 SourceLocation EndLoc) {
11281 Expr *ValExpr = NumTasks;
11282
11283 // OpenMP [2.9.2, taskloop Constrcut]
11284 // The parameter of the num_tasks clause must be a positive integer
11285 // expression.
11286 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
11287 /*StrictlyPositive=*/true))
11288 return nullptr;
11289
11290 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11291}
11292
Alexey Bataev28c75412015-12-15 08:19:24 +000011293OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
11294 SourceLocation LParenLoc,
11295 SourceLocation EndLoc) {
11296 // OpenMP [2.13.2, critical construct, Description]
11297 // ... where hint-expression is an integer constant expression that evaluates
11298 // to a valid lock hint.
11299 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
11300 if (HintExpr.isInvalid())
11301 return nullptr;
11302 return new (Context)
11303 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
11304}
11305
Carlo Bertollib4adf552016-01-15 18:50:31 +000011306OMPClause *Sema::ActOnOpenMPDistScheduleClause(
11307 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
11308 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
11309 SourceLocation EndLoc) {
11310 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
11311 std::string Values;
11312 Values += "'";
11313 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
11314 Values += "'";
11315 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
11316 << Values << getOpenMPClauseName(OMPC_dist_schedule);
11317 return nullptr;
11318 }
11319 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000011320 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000011321 if (ChunkSize) {
11322 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
11323 !ChunkSize->isInstantiationDependent() &&
11324 !ChunkSize->containsUnexpandedParameterPack()) {
11325 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
11326 ExprResult Val =
11327 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
11328 if (Val.isInvalid())
11329 return nullptr;
11330
11331 ValExpr = Val.get();
11332
11333 // OpenMP [2.7.1, Restrictions]
11334 // chunk_size must be a loop invariant integer expression with a positive
11335 // value.
11336 llvm::APSInt Result;
11337 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
11338 if (Result.isSigned() && !Result.isStrictlyPositive()) {
11339 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
11340 << "dist_schedule" << ChunkSize->getSourceRange();
11341 return nullptr;
11342 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +000011343 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
11344 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +000011345 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
11346 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11347 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011348 }
11349 }
11350 }
11351
11352 return new (Context)
11353 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000011354 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011355}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011356
11357OMPClause *Sema::ActOnOpenMPDefaultmapClause(
11358 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
11359 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
11360 SourceLocation KindLoc, SourceLocation EndLoc) {
11361 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
11362 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
11363 Kind != OMPC_DEFAULTMAP_scalar) {
11364 std::string Value;
11365 SourceLocation Loc;
11366 Value += "'";
11367 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
11368 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11369 OMPC_DEFAULTMAP_MODIFIER_tofrom);
11370 Loc = MLoc;
11371 } else {
11372 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11373 OMPC_DEFAULTMAP_scalar);
11374 Loc = KindLoc;
11375 }
11376 Value += "'";
11377 Diag(Loc, diag::err_omp_unexpected_clause_value)
11378 << Value << getOpenMPClauseName(OMPC_defaultmap);
11379 return nullptr;
11380 }
11381
11382 return new (Context)
11383 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
11384}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011385
11386bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
11387 DeclContext *CurLexicalContext = getCurLexicalContext();
11388 if (!CurLexicalContext->isFileContext() &&
11389 !CurLexicalContext->isExternCContext() &&
11390 !CurLexicalContext->isExternCXXContext()) {
11391 Diag(Loc, diag::err_omp_region_not_file_context);
11392 return false;
11393 }
11394 if (IsInOpenMPDeclareTargetContext) {
11395 Diag(Loc, diag::err_omp_enclosed_declare_target);
11396 return false;
11397 }
11398
11399 IsInOpenMPDeclareTargetContext = true;
11400 return true;
11401}
11402
11403void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
11404 assert(IsInOpenMPDeclareTargetContext &&
11405 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
11406
11407 IsInOpenMPDeclareTargetContext = false;
11408}
11409
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011410void
11411Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec,
11412 const DeclarationNameInfo &Id,
11413 OMPDeclareTargetDeclAttr::MapTypeTy MT,
11414 NamedDeclSetType &SameDirectiveDecls) {
11415 LookupResult Lookup(*this, Id, LookupOrdinaryName);
11416 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
11417
11418 if (Lookup.isAmbiguous())
11419 return;
11420 Lookup.suppressDiagnostics();
11421
11422 if (!Lookup.isSingleResult()) {
11423 if (TypoCorrection Corrected =
11424 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
11425 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
11426 CTK_ErrorRecovery)) {
11427 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
11428 << Id.getName());
11429 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
11430 return;
11431 }
11432
11433 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
11434 return;
11435 }
11436
11437 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
11438 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
11439 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
11440 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
11441
11442 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
11443 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
11444 ND->addAttr(A);
11445 if (ASTMutationListener *ML = Context.getASTMutationListener())
11446 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
11447 checkDeclIsAllowedInOpenMPTarget(nullptr, ND);
11448 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
11449 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
11450 << Id.getName();
11451 }
11452 } else
11453 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
11454}
11455
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011456static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
11457 Sema &SemaRef, Decl *D) {
11458 if (!D)
11459 return;
11460 Decl *LD = nullptr;
11461 if (isa<TagDecl>(D)) {
11462 LD = cast<TagDecl>(D)->getDefinition();
11463 } else if (isa<VarDecl>(D)) {
11464 LD = cast<VarDecl>(D)->getDefinition();
11465
11466 // If this is an implicit variable that is legal and we do not need to do
11467 // anything.
11468 if (cast<VarDecl>(D)->isImplicit()) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011469 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11470 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11471 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011472 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011473 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011474 return;
11475 }
11476
11477 } else if (isa<FunctionDecl>(D)) {
11478 const FunctionDecl *FD = nullptr;
11479 if (cast<FunctionDecl>(D)->hasBody(FD))
11480 LD = const_cast<FunctionDecl *>(FD);
11481
11482 // If the definition is associated with the current declaration in the
11483 // target region (it can be e.g. a lambda) that is legal and we do not need
11484 // to do anything else.
11485 if (LD == D) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011486 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11487 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11488 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011489 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011490 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011491 return;
11492 }
11493 }
11494 if (!LD)
11495 LD = D;
11496 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
11497 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
11498 // Outlined declaration is not declared target.
11499 if (LD->isOutOfLine()) {
11500 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11501 SemaRef.Diag(SL, diag::note_used_here) << SR;
11502 } else {
11503 DeclContext *DC = LD->getDeclContext();
11504 while (DC) {
11505 if (isa<FunctionDecl>(DC) &&
11506 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
11507 break;
11508 DC = DC->getParent();
11509 }
11510 if (DC)
11511 return;
11512
11513 // Is not declared in target context.
11514 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11515 SemaRef.Diag(SL, diag::note_used_here) << SR;
11516 }
11517 // Mark decl as declared target to prevent further diagnostic.
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011518 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11519 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11520 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011521 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011522 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011523 }
11524}
11525
11526static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
11527 Sema &SemaRef, DSAStackTy *Stack,
11528 ValueDecl *VD) {
11529 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
11530 return true;
11531 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
11532 return false;
11533 return true;
11534}
11535
11536void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
11537 if (!D || D->isInvalidDecl())
11538 return;
11539 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
11540 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
11541 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
11542 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
11543 if (DSAStack->isThreadPrivate(VD)) {
11544 Diag(SL, diag::err_omp_threadprivate_in_target);
11545 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
11546 return;
11547 }
11548 }
11549 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
11550 // Problem if any with var declared with incomplete type will be reported
11551 // as normal, so no need to check it here.
11552 if ((E || !VD->getType()->isIncompleteType()) &&
11553 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
11554 // Mark decl as declared target to prevent further diagnostic.
11555 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011556 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11557 Context, OMPDeclareTargetDeclAttr::MT_To);
11558 VD->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011559 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011560 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011561 }
11562 return;
11563 }
11564 }
11565 if (!E) {
11566 // Checking declaration inside declare target region.
11567 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
11568 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011569 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11570 Context, OMPDeclareTargetDeclAttr::MT_To);
11571 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011572 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011573 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011574 }
11575 return;
11576 }
11577 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
11578}
Samuel Antao661c0902016-05-26 17:39:58 +000011579
11580OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
11581 SourceLocation StartLoc,
11582 SourceLocation LParenLoc,
11583 SourceLocation EndLoc) {
11584 MappableVarListInfo MVLI(VarList);
11585 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
11586 if (MVLI.ProcessedVarList.empty())
11587 return nullptr;
11588
11589 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11590 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11591 MVLI.VarComponents);
11592}
Samuel Antaoec172c62016-05-26 17:49:04 +000011593
11594OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
11595 SourceLocation StartLoc,
11596 SourceLocation LParenLoc,
11597 SourceLocation EndLoc) {
11598 MappableVarListInfo MVLI(VarList);
11599 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
11600 if (MVLI.ProcessedVarList.empty())
11601 return nullptr;
11602
11603 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11604 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11605 MVLI.VarComponents);
11606}