blob: 14830f2155721f41f7f1ef06686d3ca1ce0037b3 [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) {
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000821 auto StartI = std::next(Stack.rbegin());
822 auto EndI = std::prev(Stack.rend());
823 if (FromParent && StartI != EndI) {
824 StartI = std::next(StartI);
825 }
826 for (auto I = StartI, EE = EndI; I != EE; ++I) {
827 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
828 return true;
829 }
830 return false;
831}
832
Alexey Bataev758e55e2013-09-06 18:03:48 +0000833void Sema::InitDataSharingAttributesStack() {
834 VarDataSharingAttributesStack = new DSAStackTy(*this);
835}
836
837#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
838
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000839bool Sema::IsOpenMPCapturedByRef(ValueDecl *D, unsigned Level) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000840 assert(LangOpts.OpenMP && "OpenMP is not allowed");
841
842 auto &Ctx = getASTContext();
843 bool IsByRef = true;
844
845 // Find the directive that is associated with the provided scope.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000846 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000847
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000848 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000849 // This table summarizes how a given variable should be passed to the device
850 // given its type and the clauses where it appears. This table is based on
851 // the description in OpenMP 4.5 [2.10.4, target Construct] and
852 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
853 //
854 // =========================================================================
855 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
856 // | |(tofrom:scalar)| | pvt | | | |
857 // =========================================================================
858 // | scl | | | | - | | bycopy|
859 // | scl | | - | x | - | - | bycopy|
860 // | scl | | x | - | - | - | null |
861 // | scl | x | | | - | | byref |
862 // | scl | x | - | x | - | - | bycopy|
863 // | scl | x | x | - | - | - | null |
864 // | scl | | - | - | - | x | byref |
865 // | scl | x | - | - | - | x | byref |
866 //
867 // | agg | n.a. | | | - | | byref |
868 // | agg | n.a. | - | x | - | - | byref |
869 // | agg | n.a. | x | - | - | - | null |
870 // | agg | n.a. | - | - | - | x | byref |
871 // | agg | n.a. | - | - | - | x[] | byref |
872 //
873 // | ptr | n.a. | | | - | | bycopy|
874 // | ptr | n.a. | - | x | - | - | bycopy|
875 // | ptr | n.a. | x | - | - | - | null |
876 // | ptr | n.a. | - | - | - | x | byref |
877 // | ptr | n.a. | - | - | - | x[] | bycopy|
878 // | ptr | n.a. | - | - | x | | bycopy|
879 // | ptr | n.a. | - | - | x | x | bycopy|
880 // | ptr | n.a. | - | - | x | x[] | bycopy|
881 // =========================================================================
882 // Legend:
883 // scl - scalar
884 // ptr - pointer
885 // agg - aggregate
886 // x - applies
887 // - - invalid in this combination
888 // [] - mapped with an array section
889 // byref - should be mapped by reference
890 // byval - should be mapped by value
891 // null - initialize a local variable to null on the device
892 //
893 // Observations:
894 // - All scalar declarations that show up in a map clause have to be passed
895 // by reference, because they may have been mapped in the enclosing data
896 // environment.
897 // - If the scalar value does not fit the size of uintptr, it has to be
898 // passed by reference, regardless the result in the table above.
899 // - For pointers mapped by value that have either an implicit map or an
900 // array section, the runtime library may pass the NULL value to the
901 // device instead of the value passed to it by the compiler.
902
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000903
904 if (Ty->isReferenceType())
905 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
Samuel Antao86ace552016-04-27 22:40:57 +0000906
907 // Locate map clauses and see if the variable being captured is referred to
908 // in any of those clauses. Here we only care about variables, not fields,
909 // because fields are part of aggregates.
910 bool IsVariableUsedInMapClause = false;
911 bool IsVariableAssociatedWithSection = false;
912
913 DSAStack->checkMappableExprComponentListsForDecl(
914 D, /*CurrentRegionOnly=*/true,
915 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
916 MapExprComponents) {
917
918 auto EI = MapExprComponents.rbegin();
919 auto EE = MapExprComponents.rend();
920
921 assert(EI != EE && "Invalid map expression!");
922
923 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
924 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
925
926 ++EI;
927 if (EI == EE)
928 return false;
929
930 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
931 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
932 isa<MemberExpr>(EI->getAssociatedExpression())) {
933 IsVariableAssociatedWithSection = true;
934 // There is nothing more we need to know about this variable.
935 return true;
936 }
937
938 // Keep looking for more map info.
939 return false;
940 });
941
942 if (IsVariableUsedInMapClause) {
943 // If variable is identified in a map clause it is always captured by
944 // reference except if it is a pointer that is dereferenced somehow.
945 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
946 } else {
947 // By default, all the data that has a scalar type is mapped by copy.
948 IsByRef = !Ty->isScalarType();
949 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000950 }
951
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000952 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
953 IsByRef = !DSAStack->hasExplicitDSA(
954 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
955 Level, /*NotLastprivate=*/true);
956 }
957
Samuel Antao86ace552016-04-27 22:40:57 +0000958 // When passing data by copy, we need to make sure it fits the uintptr size
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000959 // and alignment, because the runtime library only deals with uintptr types.
960 // If it does not fit the uintptr size, we need to pass the data by reference
961 // instead.
962 if (!IsByRef &&
963 (Ctx.getTypeSizeInChars(Ty) >
964 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000965 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000966 IsByRef = true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000967 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000968
969 return IsByRef;
970}
971
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000972unsigned Sema::getOpenMPNestingLevel() const {
973 assert(getLangOpts().OpenMP);
974 return DSAStack->getNestingLevel();
975}
976
Alexey Bataev90c228f2016-02-08 09:29:13 +0000977VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000978 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000979 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000980
981 // If we are attempting to capture a global variable in a directive with
982 // 'target' we return true so that this global is also mapped to the device.
983 //
984 // FIXME: If the declaration is enclosed in a 'declare target' directive,
985 // then it should not be captured. Therefore, an extra check has to be
986 // inserted here once support for 'declare target' is added.
987 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000988 auto *VD = dyn_cast<VarDecl>(D);
989 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000990 if (DSAStack->getCurrentDirective() == OMPD_target &&
Alexey Bataev90c228f2016-02-08 09:29:13 +0000991 !DSAStack->isClauseParsingMode())
992 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000993 if (DSAStack->getCurScope() &&
994 DSAStack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000995 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
996 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000997 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +0000998 },
Alexey Bataev90c228f2016-02-08 09:29:13 +0000999 false))
1000 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +00001001 }
1002
Alexey Bataev48977c32015-08-04 08:10:48 +00001003 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1004 (!DSAStack->isClauseParsingMode() ||
1005 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001006 auto &&Info = DSAStack->isLoopControlVariable(D);
1007 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001008 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001009 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001010 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001011 return VD ? VD : Info.second;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001012 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001013 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +00001014 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001015 DVarPrivate = DSAStack->hasDSA(
1016 D, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
1017 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +00001018 if (DVarPrivate.CKind != OMPC_unknown)
1019 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001020 }
Alexey Bataev90c228f2016-02-08 09:29:13 +00001021 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +00001022}
1023
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001024bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +00001025 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1026 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001027 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +00001028}
1029
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001030bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +00001031 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1032 // Return true if the current level is no longer enclosed in a target region.
1033
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001034 auto *VD = dyn_cast<VarDecl>(D);
1035 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001036 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1037 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +00001038}
1039
Alexey Bataeved09d242014-05-28 05:53:51 +00001040void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001041
1042void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1043 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001044 Scope *CurScope, SourceLocation Loc) {
1045 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001046 PushExpressionEvaluationContext(PotentiallyEvaluated);
1047}
1048
Alexey Bataevaac108a2015-06-23 04:51:00 +00001049void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1050 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001051}
1052
Alexey Bataevaac108a2015-06-23 04:51:00 +00001053void Sema::EndOpenMPClause() {
1054 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001055}
1056
Alexey Bataev758e55e2013-09-06 18:03:48 +00001057void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001058 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1059 // A variable of class type (or array thereof) that appears in a lastprivate
1060 // clause requires an accessible, unambiguous default constructor for the
1061 // class type, unless the list item is also specified in a firstprivate
1062 // clause.
1063 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001064 for (auto *C : D->clauses()) {
1065 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1066 SmallVector<Expr *, 8> PrivateCopies;
1067 for (auto *DE : Clause->varlists()) {
1068 if (DE->isValueDependent() || DE->isTypeDependent()) {
1069 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001070 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00001071 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001072 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataev005248a2016-02-25 05:25:57 +00001073 VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1074 QualType Type = VD->getType().getNonReferenceType();
1075 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001076 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001077 // Generate helper private variable and initialize it with the
1078 // default value. The address of the original variable is replaced
1079 // by the address of the new private variable in CodeGen. This new
1080 // variable is not added to IdResolver, so the code in the OpenMP
1081 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001082 auto *VDPrivate = buildVarDecl(
1083 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev005248a2016-02-25 05:25:57 +00001084 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001085 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1086 if (VDPrivate->isInvalidDecl())
1087 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001088 PrivateCopies.push_back(buildDeclRefExpr(
1089 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001090 } else {
1091 // The variable is also a firstprivate, so initialization sequence
1092 // for private copy is generated already.
1093 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001094 }
1095 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001096 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001097 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001098 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001099 }
1100 }
1101 }
1102
Alexey Bataev758e55e2013-09-06 18:03:48 +00001103 DSAStack->pop();
1104 DiscardCleanupsInEvaluationContext();
1105 PopExpressionEvaluationContext();
1106}
1107
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001108static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1109 Expr *NumIterations, Sema &SemaRef,
1110 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00001111
Alexey Bataeva769e072013-03-22 06:34:35 +00001112namespace {
1113
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001114class VarDeclFilterCCC : public CorrectionCandidateCallback {
1115private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001116 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001117
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001118public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001119 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001120 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001121 NamedDecl *ND = Candidate.getCorrectionDecl();
1122 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1123 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001124 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1125 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001126 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001127 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001128 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001129};
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001130
1131class VarOrFuncDeclFilterCCC : public CorrectionCandidateCallback {
1132private:
1133 Sema &SemaRef;
1134
1135public:
1136 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1137 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1138 NamedDecl *ND = Candidate.getCorrectionDecl();
1139 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
1140 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1141 SemaRef.getCurScope());
1142 }
1143 return false;
1144 }
1145};
1146
Alexey Bataeved09d242014-05-28 05:53:51 +00001147} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001148
1149ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1150 CXXScopeSpec &ScopeSpec,
1151 const DeclarationNameInfo &Id) {
1152 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1153 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1154
1155 if (Lookup.isAmbiguous())
1156 return ExprError();
1157
1158 VarDecl *VD;
1159 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001160 if (TypoCorrection Corrected = CorrectTypo(
1161 Id, LookupOrdinaryName, CurScope, nullptr,
1162 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001163 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001164 PDiag(Lookup.empty()
1165 ? diag::err_undeclared_var_use_suggest
1166 : diag::err_omp_expected_var_arg_suggest)
1167 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001168 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001169 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001170 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1171 : diag::err_omp_expected_var_arg)
1172 << Id.getName();
1173 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001174 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001175 } else {
1176 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001177 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001178 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1179 return ExprError();
1180 }
1181 }
1182 Lookup.suppressDiagnostics();
1183
1184 // OpenMP [2.9.2, Syntax, C/C++]
1185 // Variables must be file-scope, namespace-scope, or static block-scope.
1186 if (!VD->hasGlobalStorage()) {
1187 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001188 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1189 bool IsDecl =
1190 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001191 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001192 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1193 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001194 return ExprError();
1195 }
1196
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001197 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1198 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001199 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1200 // A threadprivate directive for file-scope variables must appear outside
1201 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001202 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1203 !getCurLexicalContext()->isTranslationUnit()) {
1204 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001205 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1206 bool IsDecl =
1207 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1208 Diag(VD->getLocation(),
1209 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1210 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001211 return ExprError();
1212 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001213 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1214 // A threadprivate directive for static class member variables must appear
1215 // in the class definition, in the same scope in which the member
1216 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001217 if (CanonicalVD->isStaticDataMember() &&
1218 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1219 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001220 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1221 bool IsDecl =
1222 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1223 Diag(VD->getLocation(),
1224 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1225 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001226 return ExprError();
1227 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001228 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1229 // A threadprivate directive for namespace-scope variables must appear
1230 // outside any definition or declaration other than the namespace
1231 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001232 if (CanonicalVD->getDeclContext()->isNamespace() &&
1233 (!getCurLexicalContext()->isFileContext() ||
1234 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1235 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001236 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1237 bool IsDecl =
1238 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1239 Diag(VD->getLocation(),
1240 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1241 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001242 return ExprError();
1243 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001244 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1245 // A threadprivate directive for static block-scope variables must appear
1246 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001247 if (CanonicalVD->isStaticLocal() && CurScope &&
1248 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001249 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001250 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1251 bool IsDecl =
1252 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1253 Diag(VD->getLocation(),
1254 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1255 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001256 return ExprError();
1257 }
1258
1259 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1260 // A threadprivate directive must lexically precede all references to any
1261 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001262 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001263 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001264 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001265 return ExprError();
1266 }
1267
1268 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001269 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1270 SourceLocation(), VD,
1271 /*RefersToEnclosingVariableOrCapture=*/false,
1272 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001273}
1274
Alexey Bataeved09d242014-05-28 05:53:51 +00001275Sema::DeclGroupPtrTy
1276Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1277 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001278 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001279 CurContext->addDecl(D);
1280 return DeclGroupPtrTy::make(DeclGroupRef(D));
1281 }
David Blaikie0403cb12016-01-15 23:43:25 +00001282 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001283}
1284
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001285namespace {
1286class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1287 Sema &SemaRef;
1288
1289public:
1290 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1291 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1292 if (VD->hasLocalStorage()) {
1293 SemaRef.Diag(E->getLocStart(),
1294 diag::err_omp_local_var_in_threadprivate_init)
1295 << E->getSourceRange();
1296 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1297 << VD << VD->getSourceRange();
1298 return true;
1299 }
1300 }
1301 return false;
1302 }
1303 bool VisitStmt(const Stmt *S) {
1304 for (auto Child : S->children()) {
1305 if (Child && Visit(Child))
1306 return true;
1307 }
1308 return false;
1309 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001310 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001311};
1312} // namespace
1313
Alexey Bataeved09d242014-05-28 05:53:51 +00001314OMPThreadPrivateDecl *
1315Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001316 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001317 for (auto &RefExpr : VarList) {
1318 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001319 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1320 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001321
Alexey Bataev376b4a42016-02-09 09:41:09 +00001322 // Mark variable as used.
1323 VD->setReferenced();
1324 VD->markUsed(Context);
1325
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001326 QualType QType = VD->getType();
1327 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1328 // It will be analyzed later.
1329 Vars.push_back(DE);
1330 continue;
1331 }
1332
Alexey Bataeva769e072013-03-22 06:34:35 +00001333 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1334 // A threadprivate variable must not have an incomplete type.
1335 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001336 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001337 continue;
1338 }
1339
1340 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1341 // A threadprivate variable must not have a reference type.
1342 if (VD->getType()->isReferenceType()) {
1343 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001344 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1345 bool IsDecl =
1346 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1347 Diag(VD->getLocation(),
1348 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1349 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001350 continue;
1351 }
1352
Samuel Antaof8b50122015-07-13 22:54:53 +00001353 // Check if this is a TLS variable. If TLS is not being supported, produce
1354 // the corresponding diagnostic.
1355 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1356 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1357 getLangOpts().OpenMPUseTLS &&
1358 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001359 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1360 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001361 Diag(ILoc, diag::err_omp_var_thread_local)
1362 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001363 bool IsDecl =
1364 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1365 Diag(VD->getLocation(),
1366 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1367 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001368 continue;
1369 }
1370
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001371 // Check if initial value of threadprivate variable reference variable with
1372 // local storage (it is not supported by runtime).
1373 if (auto Init = VD->getAnyInitializer()) {
1374 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001375 if (Checker.Visit(Init))
1376 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001377 }
1378
Alexey Bataeved09d242014-05-28 05:53:51 +00001379 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001380 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001381 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1382 Context, SourceRange(Loc, Loc)));
1383 if (auto *ML = Context.getASTMutationListener())
1384 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001385 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001386 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001387 if (!Vars.empty()) {
1388 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1389 Vars);
1390 D->setAccess(AS_public);
1391 }
1392 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001393}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001394
Alexey Bataev7ff55242014-06-19 09:13:45 +00001395static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001396 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001397 bool IsLoopIterVar = false) {
1398 if (DVar.RefExpr) {
1399 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1400 << getOpenMPClauseName(DVar.CKind);
1401 return;
1402 }
1403 enum {
1404 PDSA_StaticMemberShared,
1405 PDSA_StaticLocalVarShared,
1406 PDSA_LoopIterVarPrivate,
1407 PDSA_LoopIterVarLinear,
1408 PDSA_LoopIterVarLastprivate,
1409 PDSA_ConstVarShared,
1410 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001411 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001412 PDSA_LocalVarPrivate,
1413 PDSA_Implicit
1414 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001415 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001416 auto ReportLoc = D->getLocation();
1417 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001418 if (IsLoopIterVar) {
1419 if (DVar.CKind == OMPC_private)
1420 Reason = PDSA_LoopIterVarPrivate;
1421 else if (DVar.CKind == OMPC_lastprivate)
1422 Reason = PDSA_LoopIterVarLastprivate;
1423 else
1424 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00001425 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1426 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001427 Reason = PDSA_TaskVarFirstprivate;
1428 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001429 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001430 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001431 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001432 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001433 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001434 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001435 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001436 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001437 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001438 ReportHint = true;
1439 Reason = PDSA_LocalVarPrivate;
1440 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001441 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001442 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001443 << Reason << ReportHint
1444 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1445 } else if (DVar.ImplicitDSALoc.isValid()) {
1446 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1447 << getOpenMPClauseName(DVar.CKind);
1448 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001449}
1450
Alexey Bataev758e55e2013-09-06 18:03:48 +00001451namespace {
1452class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1453 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001454 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001455 bool ErrorFound;
1456 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001457 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001458 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001459
Alexey Bataev758e55e2013-09-06 18:03:48 +00001460public:
1461 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00001462 if (E->isTypeDependent() || E->isValueDependent() ||
1463 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1464 return;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001465 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001466 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001467 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1468 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001469
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001470 auto DVar = Stack->getTopDSA(VD, false);
1471 // Check if the variable has explicit DSA set and stop analysis if it so.
1472 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001473
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001474 auto ELoc = E->getExprLoc();
1475 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001476 // The default(none) clause requires that each variable that is referenced
1477 // in the construct, and does not have a predetermined data-sharing
1478 // attribute, must have its data-sharing attribute explicitly determined
1479 // by being listed in a data-sharing attribute clause.
1480 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001481 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001482 VarsWithInheritedDSA.count(VD) == 0) {
1483 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001484 return;
1485 }
1486
1487 // OpenMP [2.9.3.6, Restrictions, p.2]
1488 // A list item that appears in a reduction clause of the innermost
1489 // enclosing worksharing or parallel construct may not be accessed in an
1490 // explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001491 DVar = Stack->hasInnermostDSA(
1492 VD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1493 [](OpenMPDirectiveKind K) -> bool {
1494 return isOpenMPParallelDirective(K) ||
1495 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
1496 },
1497 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001498 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001499 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001500 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1501 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001502 return;
1503 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001504
1505 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001506 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001507 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1508 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001509 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001510 }
1511 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001512 void VisitMemberExpr(MemberExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00001513 if (E->isTypeDependent() || E->isValueDependent() ||
1514 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1515 return;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001516 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1517 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1518 auto DVar = Stack->getTopDSA(FD, false);
1519 // Check if the variable has explicit DSA set and stop analysis if it
1520 // so.
1521 if (DVar.RefExpr)
1522 return;
1523
1524 auto ELoc = E->getExprLoc();
1525 auto DKind = Stack->getCurrentDirective();
1526 // OpenMP [2.9.3.6, Restrictions, p.2]
1527 // A list item that appears in a reduction clause of the innermost
1528 // enclosing worksharing or parallel construct may not be accessed in
Alexey Bataevd985eda2016-02-10 11:29:16 +00001529 // an explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001530 DVar = Stack->hasInnermostDSA(
1531 FD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1532 [](OpenMPDirectiveKind K) -> bool {
1533 return isOpenMPParallelDirective(K) ||
1534 isOpenMPWorksharingDirective(K) ||
1535 isOpenMPTeamsDirective(K);
1536 },
1537 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001538 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001539 ErrorFound = true;
1540 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1541 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1542 return;
1543 }
1544
1545 // Define implicit data-sharing attributes for task.
1546 DVar = Stack->getImplicitDSA(FD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001547 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1548 !Stack->isLoopControlVariable(FD).first)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001549 ImplicitFirstprivate.push_back(E);
1550 }
1551 }
1552 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001553 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001554 for (auto *C : S->clauses()) {
1555 // Skip analysis of arguments of implicitly defined firstprivate clause
1556 // for task directives.
1557 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1558 for (auto *CC : C->children()) {
1559 if (CC)
1560 Visit(CC);
1561 }
1562 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001563 }
1564 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001565 for (auto *C : S->children()) {
1566 if (C && !isa<OMPExecutableDirective>(C))
1567 Visit(C);
1568 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001569 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001570
1571 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001572 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001573 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001574 return VarsWithInheritedDSA;
1575 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001576
Alexey Bataev7ff55242014-06-19 09:13:45 +00001577 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1578 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001579};
Alexey Bataeved09d242014-05-28 05:53:51 +00001580} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001581
Alexey Bataevbae9a792014-06-27 10:37:06 +00001582void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001583 switch (DKind) {
1584 case OMPD_parallel: {
1585 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001586 QualType KmpInt32PtrTy =
1587 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001588 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001589 std::make_pair(".global_tid.", KmpInt32PtrTy),
1590 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1591 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001592 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001593 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1594 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001595 break;
1596 }
1597 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001598 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001599 std::make_pair(StringRef(), QualType()) // __context with shared vars
1600 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001601 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1602 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001603 break;
1604 }
1605 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001606 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001607 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001608 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001609 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1610 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001611 break;
1612 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001613 case OMPD_for_simd: {
1614 Sema::CapturedParamNameType Params[] = {
1615 std::make_pair(StringRef(), QualType()) // __context with shared vars
1616 };
1617 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1618 Params);
1619 break;
1620 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001621 case OMPD_sections: {
1622 Sema::CapturedParamNameType Params[] = {
1623 std::make_pair(StringRef(), QualType()) // __context with shared vars
1624 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001625 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1626 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001627 break;
1628 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001629 case OMPD_section: {
1630 Sema::CapturedParamNameType Params[] = {
1631 std::make_pair(StringRef(), QualType()) // __context with shared vars
1632 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001633 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1634 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001635 break;
1636 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001637 case OMPD_single: {
1638 Sema::CapturedParamNameType Params[] = {
1639 std::make_pair(StringRef(), QualType()) // __context with shared vars
1640 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001641 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1642 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001643 break;
1644 }
Alexander Musman80c22892014-07-17 08:54:58 +00001645 case OMPD_master: {
1646 Sema::CapturedParamNameType Params[] = {
1647 std::make_pair(StringRef(), QualType()) // __context with shared vars
1648 };
1649 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1650 Params);
1651 break;
1652 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001653 case OMPD_critical: {
1654 Sema::CapturedParamNameType Params[] = {
1655 std::make_pair(StringRef(), QualType()) // __context with shared vars
1656 };
1657 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1658 Params);
1659 break;
1660 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001661 case OMPD_parallel_for: {
1662 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001663 QualType KmpInt32PtrTy =
1664 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001665 Sema::CapturedParamNameType Params[] = {
1666 std::make_pair(".global_tid.", KmpInt32PtrTy),
1667 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1668 std::make_pair(StringRef(), QualType()) // __context with shared vars
1669 };
1670 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1671 Params);
1672 break;
1673 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001674 case OMPD_parallel_for_simd: {
1675 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001676 QualType KmpInt32PtrTy =
1677 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001678 Sema::CapturedParamNameType Params[] = {
1679 std::make_pair(".global_tid.", KmpInt32PtrTy),
1680 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1681 std::make_pair(StringRef(), QualType()) // __context with shared vars
1682 };
1683 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1684 Params);
1685 break;
1686 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001687 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001688 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001689 QualType KmpInt32PtrTy =
1690 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001691 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001692 std::make_pair(".global_tid.", KmpInt32PtrTy),
1693 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001694 std::make_pair(StringRef(), QualType()) // __context with shared vars
1695 };
1696 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1697 Params);
1698 break;
1699 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001700 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001701 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001702 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1703 FunctionProtoType::ExtProtoInfo EPI;
1704 EPI.Variadic = true;
1705 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001706 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001707 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataev48591dd2016-04-20 04:01:36 +00001708 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1709 std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
1710 std::make_pair(".copy_fn.",
1711 Context.getPointerType(CopyFnType).withConst()),
1712 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001713 std::make_pair(StringRef(), QualType()) // __context with shared vars
1714 };
1715 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1716 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001717 // Mark this captured region as inlined, because we don't use outlined
1718 // function directly.
1719 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1720 AlwaysInlineAttr::CreateImplicit(
1721 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001722 break;
1723 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001724 case OMPD_ordered: {
1725 Sema::CapturedParamNameType Params[] = {
1726 std::make_pair(StringRef(), QualType()) // __context with shared vars
1727 };
1728 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1729 Params);
1730 break;
1731 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001732 case OMPD_atomic: {
1733 Sema::CapturedParamNameType Params[] = {
1734 std::make_pair(StringRef(), QualType()) // __context with shared vars
1735 };
1736 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1737 Params);
1738 break;
1739 }
Michael Wong65f367f2015-07-21 13:44:28 +00001740 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001741 case OMPD_target:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001742 case OMPD_target_parallel:
1743 case OMPD_target_parallel_for: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001744 Sema::CapturedParamNameType Params[] = {
1745 std::make_pair(StringRef(), QualType()) // __context with shared vars
1746 };
1747 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1748 Params);
1749 break;
1750 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001751 case OMPD_teams: {
1752 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001753 QualType KmpInt32PtrTy =
1754 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001755 Sema::CapturedParamNameType Params[] = {
1756 std::make_pair(".global_tid.", KmpInt32PtrTy),
1757 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1758 std::make_pair(StringRef(), QualType()) // __context with shared vars
1759 };
1760 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1761 Params);
1762 break;
1763 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001764 case OMPD_taskgroup: {
1765 Sema::CapturedParamNameType Params[] = {
1766 std::make_pair(StringRef(), QualType()) // __context with shared vars
1767 };
1768 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1769 Params);
1770 break;
1771 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00001772 case OMPD_taskloop:
1773 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00001774 QualType KmpInt32Ty =
1775 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1776 QualType KmpUInt64Ty =
1777 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
1778 QualType KmpInt64Ty =
1779 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
1780 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1781 FunctionProtoType::ExtProtoInfo EPI;
1782 EPI.Variadic = true;
1783 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00001784 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00001785 std::make_pair(".global_tid.", KmpInt32Ty),
1786 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1787 std::make_pair(".privates.",
1788 Context.VoidPtrTy.withConst().withRestrict()),
1789 std::make_pair(
1790 ".copy_fn.",
1791 Context.getPointerType(CopyFnType).withConst().withRestrict()),
1792 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1793 std::make_pair(".lb.", KmpUInt64Ty),
1794 std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty),
1795 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataev49f6e782015-12-01 04:18:41 +00001796 std::make_pair(StringRef(), QualType()) // __context with shared vars
1797 };
1798 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1799 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00001800 // Mark this captured region as inlined, because we don't use outlined
1801 // function directly.
1802 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1803 AlwaysInlineAttr::CreateImplicit(
1804 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev49f6e782015-12-01 04:18:41 +00001805 break;
1806 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001807 case OMPD_distribute: {
1808 Sema::CapturedParamNameType Params[] = {
1809 std::make_pair(StringRef(), QualType()) // __context with shared vars
1810 };
1811 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1812 Params);
1813 break;
1814 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001815 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001816 case OMPD_taskyield:
1817 case OMPD_barrier:
1818 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001819 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001820 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001821 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001822 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001823 case OMPD_target_exit_data:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001824 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001825 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001826 case OMPD_declare_target:
1827 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001828 case OMPD_target_update:
Alexey Bataev9959db52014-05-06 10:08:46 +00001829 llvm_unreachable("OpenMP Directive is not allowed");
1830 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001831 llvm_unreachable("Unknown OpenMP directive");
1832 }
1833}
1834
Alexey Bataev3392d762016-02-16 11:18:12 +00001835static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00001836 Expr *CaptureExpr, bool WithInit,
1837 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001838 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001839 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00001840 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00001841 QualType Ty = Init->getType();
1842 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1843 if (S.getLangOpts().CPlusPlus)
1844 Ty = C.getLValueReferenceType(Ty);
1845 else {
1846 Ty = C.getPointerType(Ty);
1847 ExprResult Res =
1848 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1849 if (!Res.isUsable())
1850 return nullptr;
1851 Init = Res.get();
1852 }
Alexey Bataev61205072016-03-02 04:57:40 +00001853 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001854 }
1855 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001856 if (!WithInit)
1857 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001858 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001859 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1860 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001861 return CED;
1862}
1863
Alexey Bataev61205072016-03-02 04:57:40 +00001864static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1865 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001866 OMPCapturedExprDecl *CD;
1867 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1868 CD = cast<OMPCapturedExprDecl>(VD);
1869 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00001870 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1871 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001872 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00001873 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00001874}
1875
Alexey Bataev5a3af132016-03-29 08:58:54 +00001876static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1877 if (!Ref) {
1878 auto *CD =
1879 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1880 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1881 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1882 CaptureExpr->getExprLoc());
1883 }
1884 ExprResult Res = Ref;
1885 if (!S.getLangOpts().CPlusPlus &&
1886 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1887 Ref->getType()->isPointerType())
1888 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1889 if (!Res.isUsable())
1890 return ExprError();
1891 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00001892}
1893
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001894StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1895 ArrayRef<OMPClause *> Clauses) {
1896 if (!S.isUsable()) {
1897 ActOnCapturedRegionError();
1898 return StmtError();
1899 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001900
1901 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001902 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001903 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001904 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001905 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001906 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001907 Clause->getClauseKind() == OMPC_copyprivate ||
1908 (getLangOpts().OpenMPUseTLS &&
1909 getASTContext().getTargetInfo().isTLSSupported() &&
1910 Clause->getClauseKind() == OMPC_copyin)) {
1911 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001912 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001913 for (auto *VarRef : Clause->children()) {
1914 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001915 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001916 }
1917 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001918 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001919 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001920 // Mark all variables in private list clauses as used in inner region.
1921 // Required for proper codegen of combined directives.
1922 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001923 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001924 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1925 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001926 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1927 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001928 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001929 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1930 if (auto *E = C->getPostUpdateExpr())
1931 MarkDeclarationsReferencedInExpr(E);
1932 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001933 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001934 if (Clause->getClauseKind() == OMPC_schedule)
1935 SC = cast<OMPScheduleClause>(Clause);
1936 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001937 OC = cast<OMPOrderedClause>(Clause);
1938 else if (Clause->getClauseKind() == OMPC_linear)
1939 LCs.push_back(cast<OMPLinearClause>(Clause));
1940 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001941 bool ErrorFound = false;
1942 // OpenMP, 2.7.1 Loop Construct, Restrictions
1943 // The nonmonotonic modifier cannot be specified if an ordered clause is
1944 // specified.
1945 if (SC &&
1946 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1947 SC->getSecondScheduleModifier() ==
1948 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1949 OC) {
1950 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1951 ? SC->getFirstScheduleModifierLoc()
1952 : SC->getSecondScheduleModifierLoc(),
1953 diag::err_omp_schedule_nonmonotonic_ordered)
1954 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1955 ErrorFound = true;
1956 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001957 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1958 for (auto *C : LCs) {
1959 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1960 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1961 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001962 ErrorFound = true;
1963 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001964 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1965 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1966 OC->getNumForLoops()) {
1967 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1968 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1969 ErrorFound = true;
1970 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001971 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001972 ActOnCapturedRegionError();
1973 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001974 }
1975 return ActOnCapturedRegionEnd(S.get());
1976}
1977
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001978static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1979 OpenMPDirectiveKind CurrentRegion,
1980 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001981 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001982 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001983 // Allowed nesting of constructs
1984 // +------------------+-----------------+------------------------------------+
1985 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1986 // +------------------+-----------------+------------------------------------+
1987 // | parallel | parallel | * |
1988 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001989 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001990 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001991 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001992 // | parallel | simd | * |
1993 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001994 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001995 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001996 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001997 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001998 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001999 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002000 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002001 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002002 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002003 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002004 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002005 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002006 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002007 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002008 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002009 // | parallel | target parallel | * |
2010 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002011 // | parallel | target enter | * |
2012 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002013 // | parallel | target exit | * |
2014 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002015 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002016 // | parallel | cancellation | |
2017 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002018 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002019 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002020 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002021 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002022 // +------------------+-----------------+------------------------------------+
2023 // | for | parallel | * |
2024 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002025 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002026 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002027 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002028 // | for | simd | * |
2029 // | for | sections | + |
2030 // | for | section | + |
2031 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002032 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002033 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002034 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002035 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002036 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002037 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002038 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002039 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002040 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002041 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002042 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002043 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002044 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002045 // | for | target parallel | * |
2046 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002047 // | for | target enter | * |
2048 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002049 // | for | target exit | * |
2050 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002051 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002052 // | for | cancellation | |
2053 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002054 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002055 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002056 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002057 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002058 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00002059 // | master | parallel | * |
2060 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002061 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002062 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002063 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00002064 // | master | simd | * |
2065 // | master | sections | + |
2066 // | master | section | + |
2067 // | master | single | + |
2068 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002069 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00002070 // | master |parallel sections| * |
2071 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002072 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002073 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002074 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002075 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002076 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002077 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002078 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002079 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002080 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002081 // | master | target parallel | * |
2082 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002083 // | master | target enter | * |
2084 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002085 // | master | target exit | * |
2086 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002087 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002088 // | master | cancellation | |
2089 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002090 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002091 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002092 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002093 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00002094 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002095 // | critical | parallel | * |
2096 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002097 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002098 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002099 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002100 // | critical | simd | * |
2101 // | critical | sections | + |
2102 // | critical | section | + |
2103 // | critical | single | + |
2104 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002105 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002106 // | critical |parallel sections| * |
2107 // | critical | task | * |
2108 // | critical | taskyield | * |
2109 // | critical | barrier | + |
2110 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002111 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002112 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002113 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002114 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002115 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002116 // | critical | target parallel | * |
2117 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002118 // | critical | target enter | * |
2119 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002120 // | critical | target exit | * |
2121 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002122 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002123 // | critical | cancellation | |
2124 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002125 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002126 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002127 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002128 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002129 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002130 // | simd | parallel | |
2131 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002132 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00002133 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002134 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002135 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002136 // | simd | sections | |
2137 // | simd | section | |
2138 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002139 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002140 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002141 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002142 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002143 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002144 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002145 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002146 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002147 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002148 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002149 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002150 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002151 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002152 // | simd | target parallel | |
2153 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002154 // | simd | target enter | |
2155 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002156 // | simd | target exit | |
2157 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002158 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002159 // | simd | cancellation | |
2160 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002161 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002162 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002163 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002164 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002165 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002166 // | for simd | parallel | |
2167 // | for simd | for | |
2168 // | for simd | for simd | |
2169 // | for simd | master | |
2170 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002171 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002172 // | for simd | sections | |
2173 // | for simd | section | |
2174 // | for simd | single | |
2175 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002176 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002177 // | for simd |parallel sections| |
2178 // | for simd | task | |
2179 // | for simd | taskyield | |
2180 // | for simd | barrier | |
2181 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002182 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002183 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002184 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002185 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002186 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002187 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002188 // | for simd | target parallel | |
2189 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002190 // | for simd | target enter | |
2191 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002192 // | for simd | target exit | |
2193 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002194 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002195 // | for simd | cancellation | |
2196 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002197 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002198 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002199 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002200 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002201 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002202 // | parallel for simd| parallel | |
2203 // | parallel for simd| for | |
2204 // | parallel for simd| for simd | |
2205 // | parallel for simd| master | |
2206 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002207 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002208 // | parallel for simd| sections | |
2209 // | parallel for simd| section | |
2210 // | parallel for simd| single | |
2211 // | parallel for simd| parallel for | |
2212 // | parallel for simd|parallel for simd| |
2213 // | parallel for simd|parallel sections| |
2214 // | parallel for simd| task | |
2215 // | parallel for simd| taskyield | |
2216 // | parallel for simd| barrier | |
2217 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002218 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002219 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002220 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002221 // | parallel for simd| atomic | |
2222 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002223 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002224 // | parallel for simd| target parallel | |
2225 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002226 // | parallel for simd| target enter | |
2227 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002228 // | parallel for simd| target exit | |
2229 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002230 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002231 // | parallel for simd| cancellation | |
2232 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002233 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002234 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002235 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002236 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002237 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002238 // | sections | parallel | * |
2239 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002240 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002241 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002242 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002243 // | sections | simd | * |
2244 // | sections | sections | + |
2245 // | sections | section | * |
2246 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002247 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002248 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002249 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002250 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002251 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002252 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002253 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002254 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002255 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002256 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002257 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002258 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002259 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002260 // | sections | target parallel | * |
2261 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002262 // | sections | target enter | * |
2263 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002264 // | sections | target exit | * |
2265 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002266 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002267 // | sections | cancellation | |
2268 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002269 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002270 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002271 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002272 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002273 // +------------------+-----------------+------------------------------------+
2274 // | section | parallel | * |
2275 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002276 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002277 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002278 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002279 // | section | simd | * |
2280 // | section | sections | + |
2281 // | section | section | + |
2282 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002283 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002284 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002285 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002286 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002287 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002288 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002289 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002290 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002291 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002292 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002293 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002294 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002295 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002296 // | section | target parallel | * |
2297 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002298 // | section | target enter | * |
2299 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002300 // | section | target exit | * |
2301 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002302 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002303 // | section | cancellation | |
2304 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002305 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002306 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002307 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002308 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002309 // +------------------+-----------------+------------------------------------+
2310 // | single | parallel | * |
2311 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002312 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002313 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002314 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002315 // | single | simd | * |
2316 // | single | sections | + |
2317 // | single | section | + |
2318 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002319 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002320 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002321 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002322 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002323 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002324 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002325 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002326 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002327 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002328 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002329 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002330 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002331 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002332 // | single | target parallel | * |
2333 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002334 // | single | target enter | * |
2335 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002336 // | single | target exit | * |
2337 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002338 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002339 // | single | cancellation | |
2340 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002341 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002342 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002343 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002344 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002345 // +------------------+-----------------+------------------------------------+
2346 // | parallel for | parallel | * |
2347 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002348 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002349 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002350 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002351 // | parallel for | simd | * |
2352 // | parallel for | sections | + |
2353 // | parallel for | section | + |
2354 // | parallel for | single | + |
2355 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002356 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002357 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002358 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002359 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002360 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002361 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002362 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002363 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002364 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002365 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002366 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002367 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002368 // | parallel for | target parallel | * |
2369 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002370 // | parallel for | target enter | * |
2371 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002372 // | parallel for | target exit | * |
2373 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002374 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002375 // | parallel for | cancellation | |
2376 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002377 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002378 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002379 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002380 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002381 // +------------------+-----------------+------------------------------------+
2382 // | parallel sections| parallel | * |
2383 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002384 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002385 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002386 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002387 // | parallel sections| simd | * |
2388 // | parallel sections| sections | + |
2389 // | parallel sections| section | * |
2390 // | parallel sections| single | + |
2391 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002392 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002393 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002394 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002395 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002396 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002397 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002398 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002399 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002400 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002401 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002402 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002403 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002404 // | parallel sections| target parallel | * |
2405 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002406 // | parallel sections| target enter | * |
2407 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002408 // | parallel sections| target exit | * |
2409 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002410 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002411 // | parallel sections| cancellation | |
2412 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002413 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002414 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002415 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002416 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002417 // +------------------+-----------------+------------------------------------+
2418 // | task | parallel | * |
2419 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002420 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002421 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002422 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002423 // | task | simd | * |
2424 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002425 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002426 // | task | single | + |
2427 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002428 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002429 // | task |parallel sections| * |
2430 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002431 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002432 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002433 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002434 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002435 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002436 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002437 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002438 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002439 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002440 // | task | target parallel | * |
2441 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002442 // | task | target enter | * |
2443 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002444 // | task | target exit | * |
2445 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002446 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002447 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002448 // | | point | ! |
2449 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002450 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002451 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002452 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002453 // +------------------+-----------------+------------------------------------+
2454 // | ordered | parallel | * |
2455 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002456 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002457 // | ordered | master | * |
2458 // | ordered | critical | * |
2459 // | ordered | simd | * |
2460 // | ordered | sections | + |
2461 // | ordered | section | + |
2462 // | ordered | single | + |
2463 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002464 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002465 // | ordered |parallel sections| * |
2466 // | ordered | task | * |
2467 // | ordered | taskyield | * |
2468 // | ordered | barrier | + |
2469 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002470 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002471 // | ordered | flush | * |
2472 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002473 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002474 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002475 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002476 // | ordered | target parallel | * |
2477 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002478 // | ordered | target enter | * |
2479 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002480 // | ordered | target exit | * |
2481 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002482 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002483 // | ordered | cancellation | |
2484 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002485 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002486 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002487 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002488 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002489 // +------------------+-----------------+------------------------------------+
2490 // | atomic | parallel | |
2491 // | atomic | for | |
2492 // | atomic | for simd | |
2493 // | atomic | master | |
2494 // | atomic | critical | |
2495 // | atomic | simd | |
2496 // | atomic | sections | |
2497 // | atomic | section | |
2498 // | atomic | single | |
2499 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002500 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002501 // | atomic |parallel sections| |
2502 // | atomic | task | |
2503 // | atomic | taskyield | |
2504 // | atomic | barrier | |
2505 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002506 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002507 // | atomic | flush | |
2508 // | atomic | ordered | |
2509 // | atomic | atomic | |
2510 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002511 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002512 // | atomic | target parallel | |
2513 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002514 // | atomic | target enter | |
2515 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002516 // | atomic | target exit | |
2517 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002518 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002519 // | atomic | cancellation | |
2520 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002521 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002522 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002523 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002524 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002525 // +------------------+-----------------+------------------------------------+
2526 // | target | parallel | * |
2527 // | target | for | * |
2528 // | target | for simd | * |
2529 // | target | master | * |
2530 // | target | critical | * |
2531 // | target | simd | * |
2532 // | target | sections | * |
2533 // | target | section | * |
2534 // | target | single | * |
2535 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002536 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002537 // | target |parallel sections| * |
2538 // | target | task | * |
2539 // | target | taskyield | * |
2540 // | target | barrier | * |
2541 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002542 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002543 // | target | flush | * |
2544 // | target | ordered | * |
2545 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002546 // | target | target | |
2547 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002548 // | target | target parallel | |
2549 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002550 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002551 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002552 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002553 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002554 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002555 // | target | cancellation | |
2556 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002557 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002558 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002559 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002560 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002561 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002562 // | target parallel | parallel | * |
2563 // | target parallel | for | * |
2564 // | target parallel | for simd | * |
2565 // | target parallel | master | * |
2566 // | target parallel | critical | * |
2567 // | target parallel | simd | * |
2568 // | target parallel | sections | * |
2569 // | target parallel | section | * |
2570 // | target parallel | single | * |
2571 // | target parallel | parallel for | * |
2572 // | target parallel |parallel for simd| * |
2573 // | target parallel |parallel sections| * |
2574 // | target parallel | task | * |
2575 // | target parallel | taskyield | * |
2576 // | target parallel | barrier | * |
2577 // | target parallel | taskwait | * |
2578 // | target parallel | taskgroup | * |
2579 // | target parallel | flush | * |
2580 // | target parallel | ordered | * |
2581 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002582 // | target parallel | target | |
2583 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002584 // | target parallel | target parallel | |
2585 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002586 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002587 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002588 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002589 // | | data | |
2590 // | target parallel | teams | |
2591 // | target parallel | cancellation | |
2592 // | | point | ! |
2593 // | target parallel | cancel | ! |
2594 // | target parallel | taskloop | * |
2595 // | target parallel | taskloop simd | * |
2596 // | target parallel | distribute | |
2597 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002598 // | target parallel | parallel | * |
2599 // | for | | |
2600 // | target parallel | for | * |
2601 // | for | | |
2602 // | target parallel | for simd | * |
2603 // | for | | |
2604 // | target parallel | master | * |
2605 // | for | | |
2606 // | target parallel | critical | * |
2607 // | for | | |
2608 // | target parallel | simd | * |
2609 // | for | | |
2610 // | target parallel | sections | * |
2611 // | for | | |
2612 // | target parallel | section | * |
2613 // | for | | |
2614 // | target parallel | single | * |
2615 // | for | | |
2616 // | target parallel | parallel for | * |
2617 // | for | | |
2618 // | target parallel |parallel for simd| * |
2619 // | for | | |
2620 // | target parallel |parallel sections| * |
2621 // | for | | |
2622 // | target parallel | task | * |
2623 // | for | | |
2624 // | target parallel | taskyield | * |
2625 // | for | | |
2626 // | target parallel | barrier | * |
2627 // | for | | |
2628 // | target parallel | taskwait | * |
2629 // | for | | |
2630 // | target parallel | taskgroup | * |
2631 // | for | | |
2632 // | target parallel | flush | * |
2633 // | for | | |
2634 // | target parallel | ordered | * |
2635 // | for | | |
2636 // | target parallel | atomic | * |
2637 // | for | | |
2638 // | target parallel | target | |
2639 // | for | | |
2640 // | target parallel | target parallel | |
2641 // | for | | |
2642 // | target parallel | target parallel | |
2643 // | for | for | |
2644 // | target parallel | target enter | |
2645 // | for | data | |
2646 // | target parallel | target exit | |
2647 // | for | data | |
2648 // | target parallel | teams | |
2649 // | for | | |
2650 // | target parallel | cancellation | |
2651 // | for | point | ! |
2652 // | target parallel | cancel | ! |
2653 // | for | | |
2654 // | target parallel | taskloop | * |
2655 // | for | | |
2656 // | target parallel | taskloop simd | * |
2657 // | for | | |
2658 // | target parallel | distribute | |
2659 // | for | | |
2660 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002661 // | teams | parallel | * |
2662 // | teams | for | + |
2663 // | teams | for simd | + |
2664 // | teams | master | + |
2665 // | teams | critical | + |
2666 // | teams | simd | + |
2667 // | teams | sections | + |
2668 // | teams | section | + |
2669 // | teams | single | + |
2670 // | teams | parallel for | * |
2671 // | teams |parallel for simd| * |
2672 // | teams |parallel sections| * |
2673 // | teams | task | + |
2674 // | teams | taskyield | + |
2675 // | teams | barrier | + |
2676 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002677 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002678 // | teams | flush | + |
2679 // | teams | ordered | + |
2680 // | teams | atomic | + |
2681 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002682 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002683 // | teams | target parallel | + |
2684 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002685 // | teams | target enter | + |
2686 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002687 // | teams | target exit | + |
2688 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002689 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002690 // | teams | cancellation | |
2691 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002692 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002693 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002694 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002695 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002696 // +------------------+-----------------+------------------------------------+
2697 // | taskloop | parallel | * |
2698 // | taskloop | for | + |
2699 // | taskloop | for simd | + |
2700 // | taskloop | master | + |
2701 // | taskloop | critical | * |
2702 // | taskloop | simd | * |
2703 // | taskloop | sections | + |
2704 // | taskloop | section | + |
2705 // | taskloop | single | + |
2706 // | taskloop | parallel for | * |
2707 // | taskloop |parallel for simd| * |
2708 // | taskloop |parallel sections| * |
2709 // | taskloop | task | * |
2710 // | taskloop | taskyield | * |
2711 // | taskloop | barrier | + |
2712 // | taskloop | taskwait | * |
2713 // | taskloop | taskgroup | * |
2714 // | taskloop | flush | * |
2715 // | taskloop | ordered | + |
2716 // | taskloop | atomic | * |
2717 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002718 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002719 // | taskloop | target parallel | * |
2720 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002721 // | taskloop | target enter | * |
2722 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002723 // | taskloop | target exit | * |
2724 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002725 // | taskloop | teams | + |
2726 // | taskloop | cancellation | |
2727 // | | point | |
2728 // | taskloop | cancel | |
2729 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002730 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002731 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002732 // | taskloop simd | parallel | |
2733 // | taskloop simd | for | |
2734 // | taskloop simd | for simd | |
2735 // | taskloop simd | master | |
2736 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002737 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002738 // | taskloop simd | sections | |
2739 // | taskloop simd | section | |
2740 // | taskloop simd | single | |
2741 // | taskloop simd | parallel for | |
2742 // | taskloop simd |parallel for simd| |
2743 // | taskloop simd |parallel sections| |
2744 // | taskloop simd | task | |
2745 // | taskloop simd | taskyield | |
2746 // | taskloop simd | barrier | |
2747 // | taskloop simd | taskwait | |
2748 // | taskloop simd | taskgroup | |
2749 // | taskloop simd | flush | |
2750 // | taskloop simd | ordered | + (with simd clause) |
2751 // | taskloop simd | atomic | |
2752 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002753 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002754 // | taskloop simd | target parallel | |
2755 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002756 // | taskloop simd | target enter | |
2757 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002758 // | taskloop simd | target exit | |
2759 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002760 // | taskloop simd | teams | |
2761 // | taskloop simd | cancellation | |
2762 // | | point | |
2763 // | taskloop simd | cancel | |
2764 // | taskloop simd | taskloop | |
2765 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002766 // | taskloop simd | distribute | |
2767 // +------------------+-----------------+------------------------------------+
2768 // | distribute | parallel | * |
2769 // | distribute | for | * |
2770 // | distribute | for simd | * |
2771 // | distribute | master | * |
2772 // | distribute | critical | * |
2773 // | distribute | simd | * |
2774 // | distribute | sections | * |
2775 // | distribute | section | * |
2776 // | distribute | single | * |
2777 // | distribute | parallel for | * |
2778 // | distribute |parallel for simd| * |
2779 // | distribute |parallel sections| * |
2780 // | distribute | task | * |
2781 // | distribute | taskyield | * |
2782 // | distribute | barrier | * |
2783 // | distribute | taskwait | * |
2784 // | distribute | taskgroup | * |
2785 // | distribute | flush | * |
2786 // | distribute | ordered | + |
2787 // | distribute | atomic | * |
2788 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002789 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002790 // | distribute | target parallel | |
2791 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002792 // | distribute | target enter | |
2793 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002794 // | distribute | target exit | |
2795 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002796 // | distribute | teams | |
2797 // | distribute | cancellation | + |
2798 // | | point | |
2799 // | distribute | cancel | + |
2800 // | distribute | taskloop | * |
2801 // | distribute | taskloop simd | * |
2802 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002803 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002804 if (Stack->getCurScope()) {
2805 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002806 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002807 bool NestingProhibited = false;
2808 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002809 enum {
2810 NoRecommend,
2811 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002812 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002813 ShouldBeInTargetRegion,
2814 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002815 } Recommend = NoRecommend;
Alexey Bataev1f092212016-02-02 04:59:52 +00002816 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered &&
2817 CurrentRegion != OMPD_simd) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002818 // OpenMP [2.16, Nesting of Regions]
2819 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002820 // OpenMP [2.8.1,simd Construct, Restrictions]
2821 // An ordered construct with the simd clause is the only OpenMP construct
2822 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002823 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2824 return true;
2825 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002826 if (ParentRegion == OMPD_atomic) {
2827 // OpenMP [2.16, Nesting of Regions]
2828 // OpenMP constructs may not be nested inside an atomic region.
2829 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2830 return true;
2831 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002832 if (CurrentRegion == OMPD_section) {
2833 // OpenMP [2.7.2, sections Construct, Restrictions]
2834 // Orphaned section directives are prohibited. That is, the section
2835 // directives must appear within the sections construct and must not be
2836 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002837 if (ParentRegion != OMPD_sections &&
2838 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002839 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2840 << (ParentRegion != OMPD_unknown)
2841 << getOpenMPDirectiveName(ParentRegion);
2842 return true;
2843 }
2844 return false;
2845 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002846 // Allow some constructs to be orphaned (they could be used in functions,
2847 // called from OpenMP regions with the required preconditions).
2848 if (ParentRegion == OMPD_unknown)
2849 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002850 if (CurrentRegion == OMPD_cancellation_point ||
2851 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002852 // OpenMP [2.16, Nesting of Regions]
2853 // A cancellation point construct for which construct-type-clause is
2854 // taskgroup must be nested inside a task construct. A cancellation
2855 // point construct for which construct-type-clause is not taskgroup must
2856 // be closely nested inside an OpenMP construct that matches the type
2857 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002858 // A cancel construct for which construct-type-clause is taskgroup must be
2859 // nested inside a task construct. A cancel construct for which
2860 // construct-type-clause is not taskgroup must be closely nested inside an
2861 // OpenMP construct that matches the type specified in
2862 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002863 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002864 !((CancelRegion == OMPD_parallel &&
2865 (ParentRegion == OMPD_parallel ||
2866 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002867 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002868 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2869 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002870 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2871 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002872 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2873 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002874 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002875 // OpenMP [2.16, Nesting of Regions]
2876 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002877 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002878 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002879 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002880 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2881 // OpenMP [2.16, Nesting of Regions]
2882 // A critical region may not be nested (closely or otherwise) inside a
2883 // critical region with the same name. Note that this restriction is not
2884 // sufficient to prevent deadlock.
2885 SourceLocation PreviousCriticalLoc;
2886 bool DeadLock =
2887 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2888 OpenMPDirectiveKind K,
2889 const DeclarationNameInfo &DNI,
2890 SourceLocation Loc)
2891 ->bool {
2892 if (K == OMPD_critical &&
2893 DNI.getName() == CurrentName.getName()) {
2894 PreviousCriticalLoc = Loc;
2895 return true;
2896 } else
2897 return false;
2898 },
2899 false /* skip top directive */);
2900 if (DeadLock) {
2901 SemaRef.Diag(StartLoc,
2902 diag::err_omp_prohibited_region_critical_same_name)
2903 << CurrentName.getName();
2904 if (PreviousCriticalLoc.isValid())
2905 SemaRef.Diag(PreviousCriticalLoc,
2906 diag::note_omp_previous_critical_region);
2907 return true;
2908 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002909 } else if (CurrentRegion == OMPD_barrier) {
2910 // OpenMP [2.16, Nesting of Regions]
2911 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002912 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002913 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2914 isOpenMPTaskingDirective(ParentRegion) ||
2915 ParentRegion == OMPD_master ||
2916 ParentRegion == OMPD_critical ||
2917 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00002918 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002919 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002920 // OpenMP [2.16, Nesting of Regions]
2921 // A worksharing region may not be closely nested inside a worksharing,
2922 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002923 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2924 isOpenMPTaskingDirective(ParentRegion) ||
2925 ParentRegion == OMPD_master ||
2926 ParentRegion == OMPD_critical ||
2927 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002928 Recommend = ShouldBeInParallelRegion;
2929 } else if (CurrentRegion == OMPD_ordered) {
2930 // OpenMP [2.16, Nesting of Regions]
2931 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002932 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002933 // An ordered region must be closely nested inside a loop region (or
2934 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002935 // OpenMP [2.8.1,simd Construct, Restrictions]
2936 // An ordered construct with the simd clause is the only OpenMP construct
2937 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002938 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002939 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002940 !(isOpenMPSimdDirective(ParentRegion) ||
2941 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002942 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002943 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2944 // OpenMP [2.16, Nesting of Regions]
2945 // If specified, a teams construct must be contained within a target
2946 // construct.
2947 NestingProhibited = ParentRegion != OMPD_target;
2948 Recommend = ShouldBeInTargetRegion;
2949 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2950 }
2951 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2952 // OpenMP [2.16, Nesting of Regions]
2953 // distribute, parallel, parallel sections, parallel workshare, and the
2954 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2955 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002956 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2957 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002958 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002959 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002960 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2961 // OpenMP 4.5 [2.17 Nesting of Regions]
2962 // The region associated with the distribute construct must be strictly
2963 // nested inside a teams region
2964 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2965 Recommend = ShouldBeInTeamsRegion;
2966 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002967 if (!NestingProhibited &&
2968 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2969 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2970 // OpenMP 4.5 [2.17 Nesting of Regions]
2971 // If a target, target update, target data, target enter data, or
2972 // target exit data construct is encountered during execution of a
2973 // target region, the behavior is unspecified.
2974 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002975 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
2976 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002977 if (isOpenMPTargetExecutionDirective(K)) {
2978 OffendingRegion = K;
2979 return true;
2980 } else
2981 return false;
2982 },
2983 false /* don't skip top directive */);
2984 CloseNesting = false;
2985 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002986 if (NestingProhibited) {
2987 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002988 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2989 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002990 return true;
2991 }
2992 }
2993 return false;
2994}
2995
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002996static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2997 ArrayRef<OMPClause *> Clauses,
2998 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2999 bool ErrorFound = false;
3000 unsigned NamedModifiersNumber = 0;
3001 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3002 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003003 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003004 for (const auto *C : Clauses) {
3005 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3006 // At most one if clause without a directive-name-modifier can appear on
3007 // the directive.
3008 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3009 if (FoundNameModifiers[CurNM]) {
3010 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
3011 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3012 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3013 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003014 } else if (CurNM != OMPD_unknown) {
3015 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003016 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003017 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003018 FoundNameModifiers[CurNM] = IC;
3019 if (CurNM == OMPD_unknown)
3020 continue;
3021 // Check if the specified name modifier is allowed for the current
3022 // directive.
3023 // At most one if clause with the particular directive-name-modifier can
3024 // appear on the directive.
3025 bool MatchFound = false;
3026 for (auto NM : AllowedNameModifiers) {
3027 if (CurNM == NM) {
3028 MatchFound = true;
3029 break;
3030 }
3031 }
3032 if (!MatchFound) {
3033 S.Diag(IC->getNameModifierLoc(),
3034 diag::err_omp_wrong_if_directive_name_modifier)
3035 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3036 ErrorFound = true;
3037 }
3038 }
3039 }
3040 // If any if clause on the directive includes a directive-name-modifier then
3041 // all if clauses on the directive must include a directive-name-modifier.
3042 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3043 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3044 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
3045 diag::err_omp_no_more_if_clause);
3046 } else {
3047 std::string Values;
3048 std::string Sep(", ");
3049 unsigned AllowedCnt = 0;
3050 unsigned TotalAllowedNum =
3051 AllowedNameModifiers.size() - NamedModifiersNumber;
3052 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3053 ++Cnt) {
3054 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3055 if (!FoundNameModifiers[NM]) {
3056 Values += "'";
3057 Values += getOpenMPDirectiveName(NM);
3058 Values += "'";
3059 if (AllowedCnt + 2 == TotalAllowedNum)
3060 Values += " or ";
3061 else if (AllowedCnt + 1 != TotalAllowedNum)
3062 Values += Sep;
3063 ++AllowedCnt;
3064 }
3065 }
3066 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
3067 diag::err_omp_unnamed_if_clause)
3068 << (TotalAllowedNum > 1) << Values;
3069 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00003070 for (auto Loc : NameModifierLoc) {
3071 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3072 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003073 ErrorFound = true;
3074 }
3075 return ErrorFound;
3076}
3077
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003078StmtResult Sema::ActOnOpenMPExecutableDirective(
3079 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3080 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3081 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003082 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003083 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3084 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003085 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003086
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003087 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003088 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003089 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003090 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00003091 if (AStmt) {
3092 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3093
3094 // Check default data sharing attributes for referenced variables.
3095 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3096 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
3097 if (DSAChecker.isErrorFound())
3098 return StmtError();
3099 // Generate list of implicitly defined firstprivate variables.
3100 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003101
3102 if (!DSAChecker.getImplicitFirstprivate().empty()) {
3103 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3104 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
3105 SourceLocation(), SourceLocation())) {
3106 ClausesWithImplicit.push_back(Implicit);
3107 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3108 DSAChecker.getImplicitFirstprivate().size();
3109 } else
3110 ErrorFound = true;
3111 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003112 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003113
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003114 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003115 switch (Kind) {
3116 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003117 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3118 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003119 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003120 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003121 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003122 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3123 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003124 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003125 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003126 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3127 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003128 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003129 case OMPD_for_simd:
3130 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3131 EndLoc, VarsWithInheritedDSA);
3132 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003133 case OMPD_sections:
3134 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3135 EndLoc);
3136 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003137 case OMPD_section:
3138 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003139 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003140 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3141 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003142 case OMPD_single:
3143 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3144 EndLoc);
3145 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003146 case OMPD_master:
3147 assert(ClausesWithImplicit.empty() &&
3148 "No clauses are allowed for 'omp master' directive");
3149 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3150 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003151 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003152 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3153 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003154 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003155 case OMPD_parallel_for:
3156 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3157 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003158 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003159 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003160 case OMPD_parallel_for_simd:
3161 Res = ActOnOpenMPParallelForSimdDirective(
3162 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003163 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003164 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003165 case OMPD_parallel_sections:
3166 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3167 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003168 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003169 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003170 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003171 Res =
3172 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003173 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003174 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003175 case OMPD_taskyield:
3176 assert(ClausesWithImplicit.empty() &&
3177 "No clauses are allowed for 'omp taskyield' directive");
3178 assert(AStmt == nullptr &&
3179 "No associated statement allowed for 'omp taskyield' directive");
3180 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3181 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003182 case OMPD_barrier:
3183 assert(ClausesWithImplicit.empty() &&
3184 "No clauses are allowed for 'omp barrier' directive");
3185 assert(AStmt == nullptr &&
3186 "No associated statement allowed for 'omp barrier' directive");
3187 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3188 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003189 case OMPD_taskwait:
3190 assert(ClausesWithImplicit.empty() &&
3191 "No clauses are allowed for 'omp taskwait' directive");
3192 assert(AStmt == nullptr &&
3193 "No associated statement allowed for 'omp taskwait' directive");
3194 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3195 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003196 case OMPD_taskgroup:
3197 assert(ClausesWithImplicit.empty() &&
3198 "No clauses are allowed for 'omp taskgroup' directive");
3199 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3200 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003201 case OMPD_flush:
3202 assert(AStmt == nullptr &&
3203 "No associated statement allowed for 'omp flush' directive");
3204 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3205 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003206 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003207 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3208 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003209 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003210 case OMPD_atomic:
3211 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3212 EndLoc);
3213 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003214 case OMPD_teams:
3215 Res =
3216 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3217 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003218 case OMPD_target:
3219 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3220 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003221 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003222 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003223 case OMPD_target_parallel:
3224 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3225 StartLoc, EndLoc);
3226 AllowedNameModifiers.push_back(OMPD_target);
3227 AllowedNameModifiers.push_back(OMPD_parallel);
3228 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003229 case OMPD_target_parallel_for:
3230 Res = ActOnOpenMPTargetParallelForDirective(
3231 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3232 AllowedNameModifiers.push_back(OMPD_target);
3233 AllowedNameModifiers.push_back(OMPD_parallel);
3234 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003235 case OMPD_cancellation_point:
3236 assert(ClausesWithImplicit.empty() &&
3237 "No clauses are allowed for 'omp cancellation point' directive");
3238 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3239 "cancellation point' directive");
3240 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3241 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003242 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003243 assert(AStmt == nullptr &&
3244 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003245 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3246 CancelRegion);
3247 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003248 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003249 case OMPD_target_data:
3250 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3251 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003252 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003253 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003254 case OMPD_target_enter_data:
3255 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3256 EndLoc);
3257 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3258 break;
Samuel Antao72590762016-01-19 20:04:50 +00003259 case OMPD_target_exit_data:
3260 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3261 EndLoc);
3262 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3263 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003264 case OMPD_taskloop:
3265 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3266 EndLoc, VarsWithInheritedDSA);
3267 AllowedNameModifiers.push_back(OMPD_taskloop);
3268 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003269 case OMPD_taskloop_simd:
3270 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3271 EndLoc, VarsWithInheritedDSA);
3272 AllowedNameModifiers.push_back(OMPD_taskloop);
3273 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003274 case OMPD_distribute:
3275 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3276 EndLoc, VarsWithInheritedDSA);
3277 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003278 case OMPD_target_update:
3279 assert(!AStmt && "Statement is not allowed for target update");
3280 Res =
3281 ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, EndLoc);
3282 AllowedNameModifiers.push_back(OMPD_target_update);
3283 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003284 case OMPD_declare_target:
3285 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003286 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003287 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003288 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003289 llvm_unreachable("OpenMP Directive is not allowed");
3290 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003291 llvm_unreachable("Unknown OpenMP directive");
3292 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003293
Alexey Bataev4acb8592014-07-07 13:01:15 +00003294 for (auto P : VarsWithInheritedDSA) {
3295 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3296 << P.first << P.second->getSourceRange();
3297 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003298 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3299
3300 if (!AllowedNameModifiers.empty())
3301 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3302 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003303
Alexey Bataeved09d242014-05-28 05:53:51 +00003304 if (ErrorFound)
3305 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003306 return Res;
3307}
3308
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003309Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3310 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003311 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003312 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3313 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003314 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003315 assert(Linears.size() == LinModifiers.size());
3316 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003317 if (!DG || DG.get().isNull())
3318 return DeclGroupPtrTy();
3319
3320 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003321 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003322 return DG;
3323 }
3324 auto *ADecl = DG.get().getSingleDecl();
3325 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3326 ADecl = FTD->getTemplatedDecl();
3327
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003328 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3329 if (!FD) {
3330 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003331 return DeclGroupPtrTy();
3332 }
3333
Alexey Bataev2af33e32016-04-07 12:45:37 +00003334 // OpenMP [2.8.2, declare simd construct, Description]
3335 // The parameter of the simdlen clause must be a constant positive integer
3336 // expression.
3337 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003338 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003339 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003340 // OpenMP [2.8.2, declare simd construct, Description]
3341 // The special this pointer can be used as if was one of the arguments to the
3342 // function in any of the linear, aligned, or uniform clauses.
3343 // The uniform clause declares one or more arguments to have an invariant
3344 // value for all concurrent invocations of the function in the execution of a
3345 // single SIMD loop.
Alexey Bataevecba70f2016-04-12 11:02:11 +00003346 llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3347 Expr *UniformedLinearThis = nullptr;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003348 for (auto *E : Uniforms) {
3349 E = E->IgnoreParenImpCasts();
3350 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3351 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3352 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3353 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003354 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3355 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003356 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003357 }
3358 if (isa<CXXThisExpr>(E)) {
3359 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003360 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003361 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003362 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3363 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003364 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003365 // OpenMP [2.8.2, declare simd construct, Description]
3366 // The aligned clause declares that the object to which each list item points
3367 // is aligned to the number of bytes expressed in the optional parameter of
3368 // the aligned clause.
3369 // The special this pointer can be used as if was one of the arguments to the
3370 // function in any of the linear, aligned, or uniform clauses.
3371 // The type of list items appearing in the aligned clause must be array,
3372 // pointer, reference to array, or reference to pointer.
3373 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3374 Expr *AlignedThis = nullptr;
3375 for (auto *E : Aligneds) {
3376 E = E->IgnoreParenImpCasts();
3377 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3378 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3379 auto *CanonPVD = PVD->getCanonicalDecl();
3380 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3381 FD->getParamDecl(PVD->getFunctionScopeIndex())
3382 ->getCanonicalDecl() == CanonPVD) {
3383 // OpenMP [2.8.1, simd construct, Restrictions]
3384 // A list-item cannot appear in more than one aligned clause.
3385 if (AlignedArgs.count(CanonPVD) > 0) {
3386 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3387 << 1 << E->getSourceRange();
3388 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3389 diag::note_omp_explicit_dsa)
3390 << getOpenMPClauseName(OMPC_aligned);
3391 continue;
3392 }
3393 AlignedArgs[CanonPVD] = E;
3394 QualType QTy = PVD->getType()
3395 .getNonReferenceType()
3396 .getUnqualifiedType()
3397 .getCanonicalType();
3398 const Type *Ty = QTy.getTypePtrOrNull();
3399 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3400 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3401 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3402 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3403 }
3404 continue;
3405 }
3406 }
3407 if (isa<CXXThisExpr>(E)) {
3408 if (AlignedThis) {
3409 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3410 << 2 << E->getSourceRange();
3411 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3412 << getOpenMPClauseName(OMPC_aligned);
3413 }
3414 AlignedThis = E;
3415 continue;
3416 }
3417 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3418 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3419 }
3420 // The optional parameter of the aligned clause, alignment, must be a constant
3421 // positive integer expression. If no optional parameter is specified,
3422 // implementation-defined default alignments for SIMD instructions on the
3423 // target platforms are assumed.
3424 SmallVector<Expr *, 4> NewAligns;
3425 for (auto *E : Alignments) {
3426 ExprResult Align;
3427 if (E)
3428 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3429 NewAligns.push_back(Align.get());
3430 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003431 // OpenMP [2.8.2, declare simd construct, Description]
3432 // The linear clause declares one or more list items to be private to a SIMD
3433 // lane and to have a linear relationship with respect to the iteration space
3434 // of a loop.
3435 // The special this pointer can be used as if was one of the arguments to the
3436 // function in any of the linear, aligned, or uniform clauses.
3437 // When a linear-step expression is specified in a linear clause it must be
3438 // either a constant integer expression or an integer-typed parameter that is
3439 // specified in a uniform clause on the directive.
3440 llvm::DenseMap<Decl *, Expr *> LinearArgs;
3441 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3442 auto MI = LinModifiers.begin();
3443 for (auto *E : Linears) {
3444 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3445 ++MI;
3446 E = E->IgnoreParenImpCasts();
3447 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3448 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3449 auto *CanonPVD = PVD->getCanonicalDecl();
3450 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3451 FD->getParamDecl(PVD->getFunctionScopeIndex())
3452 ->getCanonicalDecl() == CanonPVD) {
3453 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3454 // A list-item cannot appear in more than one linear clause.
3455 if (LinearArgs.count(CanonPVD) > 0) {
3456 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3457 << getOpenMPClauseName(OMPC_linear)
3458 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3459 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3460 diag::note_omp_explicit_dsa)
3461 << getOpenMPClauseName(OMPC_linear);
3462 continue;
3463 }
3464 // Each argument can appear in at most one uniform or linear clause.
3465 if (UniformedArgs.count(CanonPVD) > 0) {
3466 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3467 << getOpenMPClauseName(OMPC_linear)
3468 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3469 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3470 diag::note_omp_explicit_dsa)
3471 << getOpenMPClauseName(OMPC_uniform);
3472 continue;
3473 }
3474 LinearArgs[CanonPVD] = E;
3475 if (E->isValueDependent() || E->isTypeDependent() ||
3476 E->isInstantiationDependent() ||
3477 E->containsUnexpandedParameterPack())
3478 continue;
3479 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3480 PVD->getOriginalType());
3481 continue;
3482 }
3483 }
3484 if (isa<CXXThisExpr>(E)) {
3485 if (UniformedLinearThis) {
3486 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3487 << getOpenMPClauseName(OMPC_linear)
3488 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3489 << E->getSourceRange();
3490 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3491 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3492 : OMPC_linear);
3493 continue;
3494 }
3495 UniformedLinearThis = E;
3496 if (E->isValueDependent() || E->isTypeDependent() ||
3497 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3498 continue;
3499 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3500 E->getType());
3501 continue;
3502 }
3503 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3504 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3505 }
3506 Expr *Step = nullptr;
3507 Expr *NewStep = nullptr;
3508 SmallVector<Expr *, 4> NewSteps;
3509 for (auto *E : Steps) {
3510 // Skip the same step expression, it was checked already.
3511 if (Step == E || !E) {
3512 NewSteps.push_back(E ? NewStep : nullptr);
3513 continue;
3514 }
3515 Step = E;
3516 if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
3517 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3518 auto *CanonPVD = PVD->getCanonicalDecl();
3519 if (UniformedArgs.count(CanonPVD) == 0) {
3520 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3521 << Step->getSourceRange();
3522 } else if (E->isValueDependent() || E->isTypeDependent() ||
3523 E->isInstantiationDependent() ||
3524 E->containsUnexpandedParameterPack() ||
3525 CanonPVD->getType()->hasIntegerRepresentation())
3526 NewSteps.push_back(Step);
3527 else {
3528 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3529 << Step->getSourceRange();
3530 }
3531 continue;
3532 }
3533 NewStep = Step;
3534 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3535 !Step->isInstantiationDependent() &&
3536 !Step->containsUnexpandedParameterPack()) {
3537 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3538 .get();
3539 if (NewStep)
3540 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3541 }
3542 NewSteps.push_back(NewStep);
3543 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003544 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3545 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003546 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003547 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3548 const_cast<Expr **>(Linears.data()), Linears.size(),
3549 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3550 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003551 ADecl->addAttr(NewAttr);
3552 return ConvertDeclToDeclGroup(ADecl);
3553}
3554
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003555StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3556 Stmt *AStmt,
3557 SourceLocation StartLoc,
3558 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003559 if (!AStmt)
3560 return StmtError();
3561
Alexey Bataev9959db52014-05-06 10:08:46 +00003562 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3563 // 1.2.2 OpenMP Language Terminology
3564 // Structured block - An executable statement with a single entry at the
3565 // top and a single exit at the bottom.
3566 // The point of exit cannot be a branch out of the structured block.
3567 // longjmp() and throw() must not violate the entry/exit criteria.
3568 CS->getCapturedDecl()->setNothrow();
3569
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003570 getCurFunction()->setHasBranchProtectedScope();
3571
Alexey Bataev25e5b442015-09-15 12:52:43 +00003572 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3573 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003574}
3575
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003576namespace {
3577/// \brief Helper class for checking canonical form of the OpenMP loops and
3578/// extracting iteration space of each loop in the loop nest, that will be used
3579/// for IR generation.
3580class OpenMPIterationSpaceChecker {
3581 /// \brief Reference to Sema.
3582 Sema &SemaRef;
3583 /// \brief A location for diagnostics (when there is no some better location).
3584 SourceLocation DefaultLoc;
3585 /// \brief A location for diagnostics (when increment is not compatible).
3586 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003587 /// \brief A source location for referring to loop init later.
3588 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003589 /// \brief A source location for referring to condition later.
3590 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003591 /// \brief A source location for referring to increment later.
3592 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003593 /// \brief Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003594 ValueDecl *LCDecl = nullptr;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003595 /// \brief Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003596 Expr *LCRef = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003597 /// \brief Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003598 Expr *LB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003599 /// \brief Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003600 Expr *UB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003601 /// \brief Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003602 Expr *Step = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003603 /// \brief This flag is true when condition is one of:
3604 /// Var < UB
3605 /// Var <= UB
3606 /// UB > Var
3607 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003608 bool TestIsLessOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003609 /// \brief This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003610 bool TestIsStrictOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003611 /// \brief This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003612 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003613
3614public:
3615 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003616 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003617 /// \brief Check init-expr for canonical loop form and save loop counter
3618 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003619 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003620 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3621 /// for less/greater and for strict/non-strict comparison.
3622 bool CheckCond(Expr *S);
3623 /// \brief Check incr-expr for canonical loop form and return true if it
3624 /// does not conform, otherwise save loop step (#Step).
3625 bool CheckInc(Expr *S);
3626 /// \brief Return the loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003627 ValueDecl *GetLoopDecl() const { return LCDecl; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003628 /// \brief Return the reference expression to loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003629 Expr *GetLoopDeclRefExpr() const { return LCRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003630 /// \brief Source range of the loop init.
3631 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3632 /// \brief Source range of the loop condition.
3633 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3634 /// \brief Source range of the loop increment.
3635 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3636 /// \brief True if the step should be subtracted.
3637 bool ShouldSubtractStep() const { return SubtractStep; }
3638 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003639 Expr *
3640 BuildNumIterations(Scope *S, const bool LimitedType,
3641 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003642 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003643 Expr *BuildPreCond(Scope *S, Expr *Cond,
3644 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003645 /// \brief Build reference expression to the counter be used for codegen.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00003646 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
3647 DSAStackTy &DSA) const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003648 /// \brief Build reference expression to the private counter be used for
3649 /// codegen.
3650 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003651 /// \brief Build initization of the counter be used for codegen.
3652 Expr *BuildCounterInit() const;
3653 /// \brief Build step of the counter be used for codegen.
3654 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003655 /// \brief Return true if any expression is dependent.
3656 bool Dependent() const;
3657
3658private:
3659 /// \brief Check the right-hand side of an assignment in the increment
3660 /// expression.
3661 bool CheckIncRHS(Expr *RHS);
3662 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003663 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003664 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003665 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003666 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003667 /// \brief Helper to set loop increment.
3668 bool SetStep(Expr *NewStep, bool Subtract);
3669};
3670
3671bool OpenMPIterationSpaceChecker::Dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003672 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003673 assert(!LB && !UB && !Step);
3674 return false;
3675 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003676 return LCDecl->getType()->isDependentType() ||
3677 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3678 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003679}
3680
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003681static Expr *getExprAsWritten(Expr *E) {
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003682 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3683 E = ExprTemp->getSubExpr();
3684
3685 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3686 E = MTE->GetTemporaryExpr();
3687
3688 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3689 E = Binder->getSubExpr();
3690
3691 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3692 E = ICE->getSubExprAsWritten();
3693 return E->IgnoreParens();
3694}
3695
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003696bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
3697 Expr *NewLCRefExpr,
3698 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003699 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003700 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003701 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003702 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003703 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003704 LCDecl = getCanonicalDecl(NewLCDecl);
3705 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003706 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3707 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003708 if ((Ctor->isCopyOrMoveConstructor() ||
3709 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3710 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003711 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003712 LB = NewLB;
3713 return false;
3714}
3715
3716bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003717 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003718 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003719 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
3720 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003721 if (!NewUB)
3722 return true;
3723 UB = NewUB;
3724 TestIsLessOp = LessOp;
3725 TestIsStrictOp = StrictOp;
3726 ConditionSrcRange = SR;
3727 ConditionLoc = SL;
3728 return false;
3729}
3730
3731bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3732 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003733 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003734 if (!NewStep)
3735 return true;
3736 if (!NewStep->isValueDependent()) {
3737 // Check that the step is integer expression.
3738 SourceLocation StepLoc = NewStep->getLocStart();
3739 ExprResult Val =
3740 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3741 if (Val.isInvalid())
3742 return true;
3743 NewStep = Val.get();
3744
3745 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3746 // If test-expr is of form var relational-op b and relational-op is < or
3747 // <= then incr-expr must cause var to increase on each iteration of the
3748 // loop. If test-expr is of form var relational-op b and relational-op is
3749 // > or >= then incr-expr must cause var to decrease on each iteration of
3750 // the loop.
3751 // If test-expr is of form b relational-op var and relational-op is < or
3752 // <= then incr-expr must cause var to decrease on each iteration of the
3753 // loop. If test-expr is of form b relational-op var and relational-op is
3754 // > or >= then incr-expr must cause var to increase on each iteration of
3755 // the loop.
3756 llvm::APSInt Result;
3757 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3758 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3759 bool IsConstNeg =
3760 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003761 bool IsConstPos =
3762 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003763 bool IsConstZero = IsConstant && !Result.getBoolValue();
3764 if (UB && (IsConstZero ||
3765 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003766 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003767 SemaRef.Diag(NewStep->getExprLoc(),
3768 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003769 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003770 SemaRef.Diag(ConditionLoc,
3771 diag::note_omp_loop_cond_requres_compatible_incr)
3772 << TestIsLessOp << ConditionSrcRange;
3773 return true;
3774 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003775 if (TestIsLessOp == Subtract) {
3776 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3777 NewStep).get();
3778 Subtract = !Subtract;
3779 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003780 }
3781
3782 Step = NewStep;
3783 SubtractStep = Subtract;
3784 return false;
3785}
3786
Alexey Bataev9c821032015-04-30 04:23:23 +00003787bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003788 // Check init-expr for canonical loop form and save loop counter
3789 // variable - #Var and its initialization value - #LB.
3790 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3791 // var = lb
3792 // integer-type var = lb
3793 // random-access-iterator-type var = lb
3794 // pointer-type var = lb
3795 //
3796 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003797 if (EmitDiags) {
3798 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3799 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003800 return true;
3801 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003802 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003803 if (Expr *E = dyn_cast<Expr>(S))
3804 S = E->IgnoreParens();
3805 if (auto BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003806 if (BO->getOpcode() == BO_Assign) {
3807 auto *LHS = BO->getLHS()->IgnoreParens();
3808 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3809 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3810 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3811 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3812 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
3813 }
3814 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3815 if (ME->isArrow() &&
3816 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3817 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3818 }
3819 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003820 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3821 if (DS->isSingleDecl()) {
3822 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003823 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003824 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003825 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003826 SemaRef.Diag(S->getLocStart(),
3827 diag::ext_omp_loop_not_canonical_init)
3828 << S->getSourceRange();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003829 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003830 }
3831 }
3832 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003833 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3834 if (CE->getOperator() == OO_Equal) {
3835 auto *LHS = CE->getArg(0);
3836 if (auto DRE = dyn_cast<DeclRefExpr>(LHS)) {
3837 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3838 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3839 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3840 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
3841 }
3842 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3843 if (ME->isArrow() &&
3844 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3845 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3846 }
3847 }
3848 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003849
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003850 if (Dependent() || SemaRef.CurContext->isDependentContext())
3851 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00003852 if (EmitDiags) {
3853 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3854 << S->getSourceRange();
3855 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003856 return true;
3857}
3858
Alexey Bataev23b69422014-06-18 07:08:49 +00003859/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003860/// variable (which may be the loop variable) if possible.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003861static const ValueDecl *GetInitLCDecl(Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003862 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003863 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003864 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003865 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3866 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003867 if ((Ctor->isCopyOrMoveConstructor() ||
3868 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3869 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003870 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003871 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
3872 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
3873 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
3874 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3875 return getCanonicalDecl(ME->getMemberDecl());
3876 return getCanonicalDecl(VD);
3877 }
3878 }
3879 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
3880 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3881 return getCanonicalDecl(ME->getMemberDecl());
3882 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003883}
3884
3885bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3886 // Check test-expr for canonical form, save upper-bound UB, flags for
3887 // less/greater and for strict/non-strict comparison.
3888 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3889 // var relational-op b
3890 // b relational-op var
3891 //
3892 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003893 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003894 return true;
3895 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003896 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003897 SourceLocation CondLoc = S->getLocStart();
3898 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3899 if (BO->isRelationalOp()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003900 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003901 return SetUB(BO->getRHS(),
3902 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3903 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3904 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003905 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003906 return SetUB(BO->getLHS(),
3907 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3908 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3909 BO->getSourceRange(), BO->getOperatorLoc());
3910 }
3911 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3912 if (CE->getNumArgs() == 2) {
3913 auto Op = CE->getOperator();
3914 switch (Op) {
3915 case OO_Greater:
3916 case OO_GreaterEqual:
3917 case OO_Less:
3918 case OO_LessEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003919 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003920 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3921 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3922 CE->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003923 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003924 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3925 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3926 CE->getOperatorLoc());
3927 break;
3928 default:
3929 break;
3930 }
3931 }
3932 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003933 if (Dependent() || SemaRef.CurContext->isDependentContext())
3934 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003935 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003936 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003937 return true;
3938}
3939
3940bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3941 // RHS of canonical loop form increment can be:
3942 // var + incr
3943 // incr + var
3944 // var - incr
3945 //
3946 RHS = RHS->IgnoreParenImpCasts();
3947 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3948 if (BO->isAdditiveOp()) {
3949 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003950 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003951 return SetStep(BO->getRHS(), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003952 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003953 return SetStep(BO->getLHS(), false);
3954 }
3955 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3956 bool IsAdd = CE->getOperator() == OO_Plus;
3957 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003958 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003959 return SetStep(CE->getArg(1), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003960 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003961 return SetStep(CE->getArg(0), false);
3962 }
3963 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003964 if (Dependent() || SemaRef.CurContext->isDependentContext())
3965 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003966 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003967 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003968 return true;
3969}
3970
3971bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3972 // Check incr-expr for canonical loop form and return true if it
3973 // does not conform.
3974 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3975 // ++var
3976 // var++
3977 // --var
3978 // var--
3979 // var += incr
3980 // var -= incr
3981 // var = var + incr
3982 // var = incr + var
3983 // var = var - incr
3984 //
3985 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003986 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003987 return true;
3988 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003989 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003990 S = S->IgnoreParens();
3991 if (auto UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003992 if (UO->isIncrementDecrementOp() &&
3993 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003994 return SetStep(
3995 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3996 (UO->isDecrementOp() ? -1 : 1)).get(),
3997 false);
3998 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3999 switch (BO->getOpcode()) {
4000 case BO_AddAssign:
4001 case BO_SubAssign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004002 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004003 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
4004 break;
4005 case BO_Assign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004006 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004007 return CheckIncRHS(BO->getRHS());
4008 break;
4009 default:
4010 break;
4011 }
4012 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4013 switch (CE->getOperator()) {
4014 case OO_PlusPlus:
4015 case OO_MinusMinus:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004016 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004017 return SetStep(
4018 SemaRef.ActOnIntegerConstant(
4019 CE->getLocStart(),
4020 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
4021 false);
4022 break;
4023 case OO_PlusEqual:
4024 case OO_MinusEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004025 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004026 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
4027 break;
4028 case OO_Equal:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004029 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004030 return CheckIncRHS(CE->getArg(1));
4031 break;
4032 default:
4033 break;
4034 }
4035 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004036 if (Dependent() || SemaRef.CurContext->isDependentContext())
4037 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004038 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004039 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004040 return true;
4041}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004042
Alexey Bataev5a3af132016-03-29 08:58:54 +00004043static ExprResult
4044tryBuildCapture(Sema &SemaRef, Expr *Capture,
4045 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4046 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4047 return SemaRef.PerformImplicitConversion(
4048 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4049 /*AllowExplicit=*/true);
4050 auto I = Captures.find(Capture);
4051 if (I != Captures.end())
4052 return buildCapture(SemaRef, Capture, I->second);
4053 DeclRefExpr *Ref = nullptr;
4054 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4055 Captures[Capture] = Ref;
4056 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004057}
4058
Alexander Musmana5f070a2014-10-01 06:03:56 +00004059/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004060Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
4061 Scope *S, const bool LimitedType,
4062 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004063 ExprResult Diff;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004064 auto VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004065 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004066 SemaRef.getLangOpts().CPlusPlus) {
4067 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004068 auto *UBExpr = TestIsLessOp ? UB : LB;
4069 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004070 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4071 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004072 if (!Upper || !Lower)
4073 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004074
4075 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4076
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004077 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004078 // BuildBinOp already emitted error, this one is to point user to upper
4079 // and lower bound, and to tell what is passed to 'operator-'.
4080 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
4081 << Upper->getSourceRange() << Lower->getSourceRange();
4082 return nullptr;
4083 }
4084 }
4085
4086 if (!Diff.isUsable())
4087 return nullptr;
4088
4089 // Upper - Lower [- 1]
4090 if (TestIsStrictOp)
4091 Diff = SemaRef.BuildBinOp(
4092 S, DefaultLoc, BO_Sub, Diff.get(),
4093 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4094 if (!Diff.isUsable())
4095 return nullptr;
4096
4097 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00004098 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
4099 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004100 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004101 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004102 if (!Diff.isUsable())
4103 return nullptr;
4104
4105 // Parentheses (for dumping/debugging purposes only).
4106 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4107 if (!Diff.isUsable())
4108 return nullptr;
4109
4110 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004111 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004112 if (!Diff.isUsable())
4113 return nullptr;
4114
Alexander Musman174b3ca2014-10-06 11:16:29 +00004115 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004116 QualType Type = Diff.get()->getType();
4117 auto &C = SemaRef.Context;
4118 bool UseVarType = VarType->hasIntegerRepresentation() &&
4119 C.getTypeSize(Type) > C.getTypeSize(VarType);
4120 if (!Type->isIntegerType() || UseVarType) {
4121 unsigned NewSize =
4122 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4123 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4124 : Type->hasSignedIntegerRepresentation();
4125 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004126 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4127 Diff = SemaRef.PerformImplicitConversion(
4128 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4129 if (!Diff.isUsable())
4130 return nullptr;
4131 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004132 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004133 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004134 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4135 if (NewSize != C.getTypeSize(Type)) {
4136 if (NewSize < C.getTypeSize(Type)) {
4137 assert(NewSize == 64 && "incorrect loop var size");
4138 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4139 << InitSrcRange << ConditionSrcRange;
4140 }
4141 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004142 NewSize, Type->hasSignedIntegerRepresentation() ||
4143 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004144 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4145 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4146 Sema::AA_Converting, true);
4147 if (!Diff.isUsable())
4148 return nullptr;
4149 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004150 }
4151 }
4152
Alexander Musmana5f070a2014-10-01 06:03:56 +00004153 return Diff.get();
4154}
4155
Alexey Bataev5a3af132016-03-29 08:58:54 +00004156Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4157 Scope *S, Expr *Cond,
4158 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004159 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4160 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4161 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004162
Alexey Bataev5a3af132016-03-29 08:58:54 +00004163 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4164 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4165 if (!NewLB.isUsable() || !NewUB.isUsable())
4166 return nullptr;
4167
Alexey Bataev62dbb972015-04-22 11:59:37 +00004168 auto CondExpr = SemaRef.BuildBinOp(
4169 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4170 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004171 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004172 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004173 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4174 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004175 CondExpr = SemaRef.PerformImplicitConversion(
4176 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4177 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004178 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004179 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4180 // Otherwise use original loop conditon and evaluate it in runtime.
4181 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4182}
4183
Alexander Musmana5f070a2014-10-01 06:03:56 +00004184/// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004185DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004186 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004187 auto *VD = dyn_cast<VarDecl>(LCDecl);
4188 if (!VD) {
4189 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4190 auto *Ref = buildDeclRefExpr(
4191 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004192 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4193 // If the loop control decl is explicitly marked as private, do not mark it
4194 // as captured again.
4195 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4196 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004197 return Ref;
4198 }
4199 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004200 DefaultLoc);
4201}
4202
4203Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004204 if (LCDecl && !LCDecl->isInvalidDecl()) {
4205 auto Type = LCDecl->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00004206 auto *PrivateVar =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004207 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4208 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004209 if (PrivateVar->isInvalidDecl())
4210 return nullptr;
4211 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4212 }
4213 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004214}
4215
4216/// \brief Build initization of the counter be used for codegen.
4217Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4218
4219/// \brief Build step of the counter be used for codegen.
4220Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4221
4222/// \brief Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004223struct LoopIterationSpace final {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004224 /// \brief Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004225 Expr *PreCond = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004226 /// \brief This expression calculates the number of iterations in the loop.
4227 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004228 Expr *NumIterations = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004229 /// \brief The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004230 Expr *CounterVar = nullptr;
Alexey Bataeva8899172015-08-06 12:30:57 +00004231 /// \brief Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004232 Expr *PrivateCounterVar = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004233 /// \brief This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004234 Expr *CounterInit = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004235 /// \brief This is step for the #CounterVar used to generate its update:
4236 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004237 Expr *CounterStep = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004238 /// \brief Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004239 bool Subtract = false;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004240 /// \brief Source range of the loop init.
4241 SourceRange InitSrcRange;
4242 /// \brief Source range of the loop condition.
4243 SourceRange CondSrcRange;
4244 /// \brief Source range of the loop increment.
4245 SourceRange IncSrcRange;
4246};
4247
Alexey Bataev23b69422014-06-18 07:08:49 +00004248} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004249
Alexey Bataev9c821032015-04-30 04:23:23 +00004250void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4251 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4252 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004253 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4254 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004255 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4256 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004257 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4258 if (auto *D = ISC.GetLoopDecl()) {
4259 auto *VD = dyn_cast<VarDecl>(D);
4260 if (!VD) {
4261 if (auto *Private = IsOpenMPCapturedDecl(D))
4262 VD = Private;
4263 else {
4264 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4265 /*WithInit=*/false);
4266 VD = cast<VarDecl>(Ref->getDecl());
4267 }
4268 }
4269 DSAStack->addLoopControlVariable(D, VD);
4270 }
4271 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004272 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004273 }
4274}
4275
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004276/// \brief Called on a for stmt to check and extract its iteration space
4277/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00004278static bool CheckOpenMPIterationSpace(
4279 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4280 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004281 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004282 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004283 LoopIterationSpace &ResultIterSpace,
4284 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004285 // OpenMP [2.6, Canonical Loop Form]
4286 // for (init-expr; test-expr; incr-expr) structured-block
4287 auto For = dyn_cast_or_null<ForStmt>(S);
4288 if (!For) {
4289 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004290 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4291 << getOpenMPDirectiveName(DKind) << NestedLoopCount
4292 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4293 if (NestedLoopCount > 1) {
4294 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4295 SemaRef.Diag(DSA.getConstructLoc(),
4296 diag::note_omp_collapse_ordered_expr)
4297 << 2 << CollapseLoopCountExpr->getSourceRange()
4298 << OrderedLoopCountExpr->getSourceRange();
4299 else if (CollapseLoopCountExpr)
4300 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4301 diag::note_omp_collapse_ordered_expr)
4302 << 0 << CollapseLoopCountExpr->getSourceRange();
4303 else
4304 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4305 diag::note_omp_collapse_ordered_expr)
4306 << 1 << OrderedLoopCountExpr->getSourceRange();
4307 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004308 return true;
4309 }
4310 assert(For->getBody());
4311
4312 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4313
4314 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00004315 auto Init = For->getInit();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004316 if (ISC.CheckInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004317 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004318
4319 bool HasErrors = false;
4320
4321 // Check loop variable's type.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004322 if (auto *LCDecl = ISC.GetLoopDecl()) {
4323 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004324
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004325 // OpenMP [2.6, Canonical Loop Form]
4326 // Var is one of the following:
4327 // A variable of signed or unsigned integer type.
4328 // For C++, a variable of a random access iterator type.
4329 // For C, a variable of a pointer type.
4330 auto VarType = LCDecl->getType().getNonReferenceType();
4331 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4332 !VarType->isPointerType() &&
4333 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4334 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4335 << SemaRef.getLangOpts().CPlusPlus;
4336 HasErrors = true;
4337 }
4338
4339 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4340 // a Construct
4341 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4342 // parallel for construct is (are) private.
4343 // The loop iteration variable in the associated for-loop of a simd
4344 // construct with just one associated for-loop is linear with a
4345 // constant-linear-step that is the increment of the associated for-loop.
4346 // Exclude loop var from the list of variables with implicitly defined data
4347 // sharing attributes.
4348 VarsWithImplicitDSA.erase(LCDecl);
4349
4350 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4351 // in a Construct, C/C++].
4352 // The loop iteration variable in the associated for-loop of a simd
4353 // construct with just one associated for-loop may be listed in a linear
4354 // clause with a constant-linear-step that is the increment of the
4355 // associated for-loop.
4356 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4357 // parallel for construct may be listed in a private or lastprivate clause.
4358 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4359 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4360 // declared in the loop and it is predetermined as a private.
4361 auto PredeterminedCKind =
4362 isOpenMPSimdDirective(DKind)
4363 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4364 : OMPC_private;
4365 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4366 DVar.CKind != PredeterminedCKind) ||
4367 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4368 isOpenMPDistributeDirective(DKind)) &&
4369 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4370 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4371 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4372 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4373 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4374 << getOpenMPClauseName(PredeterminedCKind);
4375 if (DVar.RefExpr == nullptr)
4376 DVar.CKind = PredeterminedCKind;
4377 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4378 HasErrors = true;
4379 } else if (LoopDeclRefExpr != nullptr) {
4380 // Make the loop iteration variable private (for worksharing constructs),
4381 // linear (for simd directives with the only one associated loop) or
4382 // lastprivate (for simd directives with several collapsed or ordered
4383 // loops).
4384 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004385 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4386 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004387 /*FromParent=*/false);
4388 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4389 }
4390
4391 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4392
4393 // Check test-expr.
4394 HasErrors |= ISC.CheckCond(For->getCond());
4395
4396 // Check incr-expr.
4397 HasErrors |= ISC.CheckInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004398 }
4399
Alexander Musmana5f070a2014-10-01 06:03:56 +00004400 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004401 return HasErrors;
4402
Alexander Musmana5f070a2014-10-01 06:03:56 +00004403 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004404 ResultIterSpace.PreCond =
4405 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00004406 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004407 DSA.getCurScope(),
4408 (isOpenMPWorksharingDirective(DKind) ||
4409 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4410 Captures);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004411 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
Alexey Bataeva8899172015-08-06 12:30:57 +00004412 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004413 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4414 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4415 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4416 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4417 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4418 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4419
Alexey Bataev62dbb972015-04-22 11:59:37 +00004420 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4421 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004422 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004423 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004424 ResultIterSpace.CounterInit == nullptr ||
4425 ResultIterSpace.CounterStep == nullptr);
4426
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004427 return HasErrors;
4428}
4429
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004430/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004431static ExprResult
4432BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4433 ExprResult Start,
4434 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004435 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004436 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4437 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004438 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004439 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004440 VarRef.get()->getType())) {
4441 NewStart = SemaRef.PerformImplicitConversion(
4442 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4443 /*AllowExplicit=*/true);
4444 if (!NewStart.isUsable())
4445 return ExprError();
4446 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004447
4448 auto Init =
4449 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4450 return Init;
4451}
4452
Alexander Musmana5f070a2014-10-01 06:03:56 +00004453/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004454static ExprResult
4455BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4456 ExprResult VarRef, ExprResult Start, ExprResult Iter,
4457 ExprResult Step, bool Subtract,
4458 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004459 // Add parentheses (for debugging purposes only).
4460 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4461 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4462 !Step.isUsable())
4463 return ExprError();
4464
Alexey Bataev5a3af132016-03-29 08:58:54 +00004465 ExprResult NewStep = Step;
4466 if (Captures)
4467 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004468 if (NewStep.isInvalid())
4469 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004470 ExprResult Update =
4471 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004472 if (!Update.isUsable())
4473 return ExprError();
4474
Alexey Bataevc0214e02016-02-16 12:13:49 +00004475 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4476 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004477 ExprResult NewStart = Start;
4478 if (Captures)
4479 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004480 if (NewStart.isInvalid())
4481 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004482
Alexey Bataevc0214e02016-02-16 12:13:49 +00004483 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4484 ExprResult SavedUpdate = Update;
4485 ExprResult UpdateVal;
4486 if (VarRef.get()->getType()->isOverloadableType() ||
4487 NewStart.get()->getType()->isOverloadableType() ||
4488 Update.get()->getType()->isOverloadableType()) {
4489 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4490 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4491 Update =
4492 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4493 if (Update.isUsable()) {
4494 UpdateVal =
4495 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4496 VarRef.get(), SavedUpdate.get());
4497 if (UpdateVal.isUsable()) {
4498 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4499 UpdateVal.get());
4500 }
4501 }
4502 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4503 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004504
Alexey Bataevc0214e02016-02-16 12:13:49 +00004505 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4506 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4507 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4508 NewStart.get(), SavedUpdate.get());
4509 if (!Update.isUsable())
4510 return ExprError();
4511
Alexey Bataev11481f52016-02-17 10:29:05 +00004512 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4513 VarRef.get()->getType())) {
4514 Update = SemaRef.PerformImplicitConversion(
4515 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4516 if (!Update.isUsable())
4517 return ExprError();
4518 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004519
4520 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4521 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004522 return Update;
4523}
4524
4525/// \brief Convert integer expression \a E to make it have at least \a Bits
4526/// bits.
4527static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4528 Sema &SemaRef) {
4529 if (E == nullptr)
4530 return ExprError();
4531 auto &C = SemaRef.Context;
4532 QualType OldType = E->getType();
4533 unsigned HasBits = C.getTypeSize(OldType);
4534 if (HasBits >= Bits)
4535 return ExprResult(E);
4536 // OK to convert to signed, because new type has more bits than old.
4537 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4538 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4539 true);
4540}
4541
4542/// \brief Check if the given expression \a E is a constant integer that fits
4543/// into \a Bits bits.
4544static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4545 if (E == nullptr)
4546 return false;
4547 llvm::APSInt Result;
4548 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4549 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4550 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004551}
4552
Alexey Bataev5a3af132016-03-29 08:58:54 +00004553/// Build preinits statement for the given declarations.
4554static Stmt *buildPreInits(ASTContext &Context,
4555 SmallVectorImpl<Decl *> &PreInits) {
4556 if (!PreInits.empty()) {
4557 return new (Context) DeclStmt(
4558 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4559 SourceLocation(), SourceLocation());
4560 }
4561 return nullptr;
4562}
4563
4564/// Build preinits statement for the given declarations.
4565static Stmt *buildPreInits(ASTContext &Context,
4566 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4567 if (!Captures.empty()) {
4568 SmallVector<Decl *, 16> PreInits;
4569 for (auto &Pair : Captures)
4570 PreInits.push_back(Pair.second->getDecl());
4571 return buildPreInits(Context, PreInits);
4572 }
4573 return nullptr;
4574}
4575
4576/// Build postupdate expression for the given list of postupdates expressions.
4577static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4578 Expr *PostUpdate = nullptr;
4579 if (!PostUpdates.empty()) {
4580 for (auto *E : PostUpdates) {
4581 Expr *ConvE = S.BuildCStyleCastExpr(
4582 E->getExprLoc(),
4583 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4584 E->getExprLoc(), E)
4585 .get();
4586 PostUpdate = PostUpdate
4587 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4588 PostUpdate, ConvE)
4589 .get()
4590 : ConvE;
4591 }
4592 }
4593 return PostUpdate;
4594}
4595
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004596/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004597/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4598/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004599static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004600CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4601 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4602 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004603 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004604 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004605 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004606 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004607 // Found 'collapse' clause - calculate collapse number.
4608 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004609 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004610 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004611 }
4612 if (OrderedLoopCountExpr) {
4613 // Found 'ordered' clause - calculate collapse number.
4614 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004615 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4616 if (Result.getLimitedValue() < NestedLoopCount) {
4617 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4618 diag::err_omp_wrong_ordered_loop_count)
4619 << OrderedLoopCountExpr->getSourceRange();
4620 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4621 diag::note_collapse_loop_count)
4622 << CollapseLoopCountExpr->getSourceRange();
4623 }
4624 NestedLoopCount = Result.getLimitedValue();
4625 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004626 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004627 // This is helper routine for loop directives (e.g., 'for', 'simd',
4628 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00004629 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004630 SmallVector<LoopIterationSpace, 4> IterSpaces;
4631 IterSpaces.resize(NestedLoopCount);
4632 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004633 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004634 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004635 NestedLoopCount, CollapseLoopCountExpr,
4636 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004637 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004638 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004639 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004640 // OpenMP [2.8.1, simd construct, Restrictions]
4641 // All loops associated with the construct must be perfectly nested; that
4642 // is, there must be no intervening code nor any OpenMP directive between
4643 // any two loops.
4644 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004645 }
4646
Alexander Musmana5f070a2014-10-01 06:03:56 +00004647 Built.clear(/* size */ NestedLoopCount);
4648
4649 if (SemaRef.CurContext->isDependentContext())
4650 return NestedLoopCount;
4651
4652 // An example of what is generated for the following code:
4653 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004654 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004655 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004656 // for (k = 0; k < NK; ++k)
4657 // for (j = J0; j < NJ; j+=2) {
4658 // <loop body>
4659 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004660 //
4661 // We generate the code below.
4662 // Note: the loop body may be outlined in CodeGen.
4663 // Note: some counters may be C++ classes, operator- is used to find number of
4664 // iterations and operator+= to calculate counter value.
4665 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4666 // or i64 is currently supported).
4667 //
4668 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4669 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4670 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4671 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4672 // // similar updates for vars in clauses (e.g. 'linear')
4673 // <loop body (using local i and j)>
4674 // }
4675 // i = NI; // assign final values of counters
4676 // j = NJ;
4677 //
4678
4679 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4680 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004681 // Precondition tests if there is at least one iteration (all conditions are
4682 // true).
4683 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004684 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004685 ExprResult LastIteration32 = WidenIterationCount(
4686 32 /* Bits */, SemaRef.PerformImplicitConversion(
4687 N0->IgnoreImpCasts(), N0->getType(),
4688 Sema::AA_Converting, /*AllowExplicit=*/true)
4689 .get(),
4690 SemaRef);
4691 ExprResult LastIteration64 = WidenIterationCount(
4692 64 /* Bits */, SemaRef.PerformImplicitConversion(
4693 N0->IgnoreImpCasts(), N0->getType(),
4694 Sema::AA_Converting, /*AllowExplicit=*/true)
4695 .get(),
4696 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004697
4698 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4699 return NestedLoopCount;
4700
4701 auto &C = SemaRef.Context;
4702 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4703
4704 Scope *CurScope = DSA.getCurScope();
4705 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004706 if (PreCond.isUsable()) {
4707 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4708 PreCond.get(), IterSpaces[Cnt].PreCond);
4709 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004710 auto N = IterSpaces[Cnt].NumIterations;
4711 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4712 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004713 LastIteration32 = SemaRef.BuildBinOp(
4714 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4715 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4716 Sema::AA_Converting,
4717 /*AllowExplicit=*/true)
4718 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004719 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004720 LastIteration64 = SemaRef.BuildBinOp(
4721 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4722 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4723 Sema::AA_Converting,
4724 /*AllowExplicit=*/true)
4725 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004726 }
4727
4728 // Choose either the 32-bit or 64-bit version.
4729 ExprResult LastIteration = LastIteration64;
4730 if (LastIteration32.isUsable() &&
4731 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4732 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4733 FitsInto(
4734 32 /* Bits */,
4735 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4736 LastIteration64.get(), SemaRef)))
4737 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00004738 QualType VType = LastIteration.get()->getType();
4739 QualType RealVType = VType;
4740 QualType StrideVType = VType;
4741 if (isOpenMPTaskLoopDirective(DKind)) {
4742 VType =
4743 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
4744 StrideVType =
4745 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
4746 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004747
4748 if (!LastIteration.isUsable())
4749 return 0;
4750
4751 // Save the number of iterations.
4752 ExprResult NumIterations = LastIteration;
4753 {
4754 LastIteration = SemaRef.BuildBinOp(
4755 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4756 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4757 if (!LastIteration.isUsable())
4758 return 0;
4759 }
4760
4761 // Calculate the last iteration number beforehand instead of doing this on
4762 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4763 llvm::APSInt Result;
4764 bool IsConstant =
4765 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4766 ExprResult CalcLastIteration;
4767 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004768 ExprResult SaveRef =
4769 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004770 LastIteration = SaveRef;
4771
4772 // Prepare SaveRef + 1.
4773 NumIterations = SemaRef.BuildBinOp(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004774 CurScope, SourceLocation(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004775 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4776 if (!NumIterations.isUsable())
4777 return 0;
4778 }
4779
4780 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4781
Alexander Musmanc6388682014-12-15 07:07:06 +00004782 // Build variables passed into runtime, nesessary for worksharing directives.
4783 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004784 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4785 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004786 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004787 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4788 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004789 SemaRef.AddInitializerToDecl(
4790 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4791 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4792
4793 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004794 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4795 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004796 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4797 /*DirectInit*/ false,
4798 /*TypeMayContainAuto*/ false);
4799
4800 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4801 // This will be used to implement clause 'lastprivate'.
4802 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004803 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4804 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004805 SemaRef.AddInitializerToDecl(
4806 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4807 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4808
4809 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00004810 VarDecl *STDecl =
4811 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
4812 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004813 SemaRef.AddInitializerToDecl(
4814 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4815 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4816
4817 // Build expression: UB = min(UB, LastIteration)
4818 // It is nesessary for CodeGen of directives with static scheduling.
4819 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4820 UB.get(), LastIteration.get());
4821 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4822 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4823 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4824 CondOp.get());
4825 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4826 }
4827
4828 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004829 ExprResult IV;
4830 ExprResult Init;
4831 {
Alexey Bataev7292c292016-04-25 12:22:29 +00004832 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
4833 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004834 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004835 isOpenMPTaskLoopDirective(DKind) ||
4836 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004837 ? LB.get()
4838 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4839 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4840 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004841 }
4842
Alexander Musmanc6388682014-12-15 07:07:06 +00004843 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004844 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004845 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004846 (isOpenMPWorksharingDirective(DKind) ||
4847 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004848 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4849 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4850 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004851
4852 // Loop increment (IV = IV + 1)
4853 SourceLocation IncLoc;
4854 ExprResult Inc =
4855 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4856 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4857 if (!Inc.isUsable())
4858 return 0;
4859 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004860 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4861 if (!Inc.isUsable())
4862 return 0;
4863
4864 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4865 // Used for directives with static scheduling.
4866 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004867 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4868 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004869 // LB + ST
4870 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4871 if (!NextLB.isUsable())
4872 return 0;
4873 // LB = LB + ST
4874 NextLB =
4875 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4876 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4877 if (!NextLB.isUsable())
4878 return 0;
4879 // UB + ST
4880 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4881 if (!NextUB.isUsable())
4882 return 0;
4883 // UB = UB + ST
4884 NextUB =
4885 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4886 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4887 if (!NextUB.isUsable())
4888 return 0;
4889 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004890
4891 // Build updates and final values of the loop counters.
4892 bool HasErrors = false;
4893 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004894 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004895 Built.Updates.resize(NestedLoopCount);
4896 Built.Finals.resize(NestedLoopCount);
Alexey Bataev8b427062016-05-25 12:36:08 +00004897 SmallVector<Expr *, 4> LoopMultipliers;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004898 {
4899 ExprResult Div;
4900 // Go from inner nested loop to outer.
4901 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4902 LoopIterationSpace &IS = IterSpaces[Cnt];
4903 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4904 // Build: Iter = (IV / Div) % IS.NumIters
4905 // where Div is product of previous iterations' IS.NumIters.
4906 ExprResult Iter;
4907 if (Div.isUsable()) {
4908 Iter =
4909 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4910 } else {
4911 Iter = IV;
4912 assert((Cnt == (int)NestedLoopCount - 1) &&
4913 "unusable div expected on first iteration only");
4914 }
4915
4916 if (Cnt != 0 && Iter.isUsable())
4917 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4918 IS.NumIterations);
4919 if (!Iter.isUsable()) {
4920 HasErrors = true;
4921 break;
4922 }
4923
Alexey Bataev39f915b82015-05-08 10:41:21 +00004924 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004925 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
4926 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
4927 IS.CounterVar->getExprLoc(),
4928 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004929 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004930 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004931 if (!Init.isUsable()) {
4932 HasErrors = true;
4933 break;
4934 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00004935 ExprResult Update = BuildCounterUpdate(
4936 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
4937 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004938 if (!Update.isUsable()) {
4939 HasErrors = true;
4940 break;
4941 }
4942
4943 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4944 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004945 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004946 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004947 if (!Final.isUsable()) {
4948 HasErrors = true;
4949 break;
4950 }
4951
4952 // Build Div for the next iteration: Div <- Div * IS.NumIters
4953 if (Cnt != 0) {
4954 if (Div.isUnset())
4955 Div = IS.NumIterations;
4956 else
4957 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4958 IS.NumIterations);
4959
4960 // Add parentheses (for debugging purposes only).
4961 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00004962 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004963 if (!Div.isUsable()) {
4964 HasErrors = true;
4965 break;
4966 }
Alexey Bataev8b427062016-05-25 12:36:08 +00004967 LoopMultipliers.push_back(Div.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004968 }
4969 if (!Update.isUsable() || !Final.isUsable()) {
4970 HasErrors = true;
4971 break;
4972 }
4973 // Save results
4974 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004975 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004976 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004977 Built.Updates[Cnt] = Update.get();
4978 Built.Finals[Cnt] = Final.get();
4979 }
4980 }
4981
4982 if (HasErrors)
4983 return 0;
4984
4985 // Save results
4986 Built.IterationVarRef = IV.get();
4987 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004988 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004989 Built.CalcLastIteration =
4990 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004991 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00004992 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004993 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004994 Built.Init = Init.get();
4995 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004996 Built.LB = LB.get();
4997 Built.UB = UB.get();
4998 Built.IL = IL.get();
4999 Built.ST = ST.get();
5000 Built.EUB = EUB.get();
5001 Built.NLB = NextLB.get();
5002 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005003
Alexey Bataev8b427062016-05-25 12:36:08 +00005004 Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get();
5005 // Fill data for doacross depend clauses.
5006 for (auto Pair : DSA.getDoacrossDependClauses()) {
5007 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
5008 Pair.first->setCounterValue(CounterVal);
5009 else {
5010 if (NestedLoopCount != Pair.second.size() ||
5011 NestedLoopCount != LoopMultipliers.size() + 1) {
5012 // Erroneous case - clause has some problems.
5013 Pair.first->setCounterValue(CounterVal);
5014 continue;
5015 }
5016 assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink);
5017 auto I = Pair.second.rbegin();
5018 auto IS = IterSpaces.rbegin();
5019 auto ILM = LoopMultipliers.rbegin();
5020 Expr *UpCounterVal = CounterVal;
5021 Expr *Multiplier = nullptr;
5022 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5023 if (I->first) {
5024 assert(IS->CounterStep);
5025 Expr *NormalizedOffset =
5026 SemaRef
5027 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div,
5028 I->first, IS->CounterStep)
5029 .get();
5030 if (Multiplier) {
5031 NormalizedOffset =
5032 SemaRef
5033 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul,
5034 NormalizedOffset, Multiplier)
5035 .get();
5036 }
5037 assert(I->second == OO_Plus || I->second == OO_Minus);
5038 BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub;
5039 UpCounterVal =
5040 SemaRef.BuildBinOp(CurScope, I->first->getExprLoc(), BOK,
5041 UpCounterVal, NormalizedOffset).get();
5042 }
5043 Multiplier = *ILM;
5044 ++I;
5045 ++IS;
5046 ++ILM;
5047 }
5048 Pair.first->setCounterValue(UpCounterVal);
5049 }
5050 }
5051
Alexey Bataevabfc0692014-06-25 06:52:00 +00005052 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005053}
5054
Alexey Bataev10e775f2015-07-30 11:36:16 +00005055static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005056 auto CollapseClauses =
5057 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5058 if (CollapseClauses.begin() != CollapseClauses.end())
5059 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005060 return nullptr;
5061}
5062
Alexey Bataev10e775f2015-07-30 11:36:16 +00005063static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005064 auto OrderedClauses =
5065 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5066 if (OrderedClauses.begin() != OrderedClauses.end())
5067 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005068 return nullptr;
5069}
5070
Alexey Bataev66b15b52015-08-21 11:14:16 +00005071static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
5072 const Expr *Safelen) {
5073 llvm::APSInt SimdlenRes, SafelenRes;
5074 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
5075 Simdlen->isInstantiationDependent() ||
5076 Simdlen->containsUnexpandedParameterPack())
5077 return false;
5078 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
5079 Safelen->isInstantiationDependent() ||
5080 Safelen->containsUnexpandedParameterPack())
5081 return false;
5082 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
5083 Safelen->EvaluateAsInt(SafelenRes, S.Context);
5084 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5085 // If both simdlen and safelen clauses are specified, the value of the simdlen
5086 // parameter must be less than or equal to the value of the safelen parameter.
5087 if (SimdlenRes > SafelenRes) {
5088 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
5089 << Simdlen->getSourceRange() << Safelen->getSourceRange();
5090 return true;
5091 }
5092 return false;
5093}
5094
Alexey Bataev4acb8592014-07-07 13:01:15 +00005095StmtResult Sema::ActOnOpenMPSimdDirective(
5096 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5097 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005098 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005099 if (!AStmt)
5100 return StmtError();
5101
5102 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005103 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005104 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5105 // define the nested loops number.
5106 unsigned NestedLoopCount = CheckOpenMPLoop(
5107 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5108 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005109 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005110 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005111
Alexander Musmana5f070a2014-10-01 06:03:56 +00005112 assert((CurContext->isDependentContext() || B.builtAll()) &&
5113 "omp simd loop exprs were not built");
5114
Alexander Musman3276a272015-03-21 10:12:56 +00005115 if (!CurContext->isDependentContext()) {
5116 // Finalize the clauses that need pre-built expressions for CodeGen.
5117 for (auto C : Clauses) {
5118 if (auto LC = dyn_cast<OMPLinearClause>(C))
5119 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005120 B.NumIterations, *this, CurScope,
5121 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005122 return StmtError();
5123 }
5124 }
5125
Alexey Bataev66b15b52015-08-21 11:14:16 +00005126 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5127 // If both simdlen and safelen clauses are specified, the value of the simdlen
5128 // parameter must be less than or equal to the value of the safelen parameter.
5129 OMPSafelenClause *Safelen = nullptr;
5130 OMPSimdlenClause *Simdlen = nullptr;
5131 for (auto *Clause : Clauses) {
5132 if (Clause->getClauseKind() == OMPC_safelen)
5133 Safelen = cast<OMPSafelenClause>(Clause);
5134 else if (Clause->getClauseKind() == OMPC_simdlen)
5135 Simdlen = cast<OMPSimdlenClause>(Clause);
5136 if (Safelen && Simdlen)
5137 break;
5138 }
5139 if (Simdlen && Safelen &&
5140 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5141 Safelen->getSafelen()))
5142 return StmtError();
5143
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005144 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005145 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5146 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005147}
5148
Alexey Bataev4acb8592014-07-07 13:01:15 +00005149StmtResult Sema::ActOnOpenMPForDirective(
5150 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5151 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005152 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005153 if (!AStmt)
5154 return StmtError();
5155
5156 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005157 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005158 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5159 // define the nested loops number.
5160 unsigned NestedLoopCount = CheckOpenMPLoop(
5161 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5162 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005163 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005164 return StmtError();
5165
Alexander Musmana5f070a2014-10-01 06:03:56 +00005166 assert((CurContext->isDependentContext() || B.builtAll()) &&
5167 "omp for loop exprs were not built");
5168
Alexey Bataev54acd402015-08-04 11:18:19 +00005169 if (!CurContext->isDependentContext()) {
5170 // Finalize the clauses that need pre-built expressions for CodeGen.
5171 for (auto C : Clauses) {
5172 if (auto LC = dyn_cast<OMPLinearClause>(C))
5173 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005174 B.NumIterations, *this, CurScope,
5175 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005176 return StmtError();
5177 }
5178 }
5179
Alexey Bataevf29276e2014-06-18 04:14:57 +00005180 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005181 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005182 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005183}
5184
Alexander Musmanf82886e2014-09-18 05:12:34 +00005185StmtResult Sema::ActOnOpenMPForSimdDirective(
5186 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5187 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005188 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005189 if (!AStmt)
5190 return StmtError();
5191
5192 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005193 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005194 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5195 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005196 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005197 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5198 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5199 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005200 if (NestedLoopCount == 0)
5201 return StmtError();
5202
Alexander Musmanc6388682014-12-15 07:07:06 +00005203 assert((CurContext->isDependentContext() || B.builtAll()) &&
5204 "omp for simd loop exprs were not built");
5205
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005206 if (!CurContext->isDependentContext()) {
5207 // Finalize the clauses that need pre-built expressions for CodeGen.
5208 for (auto C : Clauses) {
5209 if (auto LC = dyn_cast<OMPLinearClause>(C))
5210 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005211 B.NumIterations, *this, CurScope,
5212 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005213 return StmtError();
5214 }
5215 }
5216
Alexey Bataev66b15b52015-08-21 11:14:16 +00005217 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5218 // If both simdlen and safelen clauses are specified, the value of the simdlen
5219 // parameter must be less than or equal to the value of the safelen parameter.
5220 OMPSafelenClause *Safelen = nullptr;
5221 OMPSimdlenClause *Simdlen = nullptr;
5222 for (auto *Clause : Clauses) {
5223 if (Clause->getClauseKind() == OMPC_safelen)
5224 Safelen = cast<OMPSafelenClause>(Clause);
5225 else if (Clause->getClauseKind() == OMPC_simdlen)
5226 Simdlen = cast<OMPSimdlenClause>(Clause);
5227 if (Safelen && Simdlen)
5228 break;
5229 }
5230 if (Simdlen && Safelen &&
5231 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5232 Safelen->getSafelen()))
5233 return StmtError();
5234
Alexander Musmanf82886e2014-09-18 05:12:34 +00005235 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005236 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5237 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005238}
5239
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005240StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5241 Stmt *AStmt,
5242 SourceLocation StartLoc,
5243 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005244 if (!AStmt)
5245 return StmtError();
5246
5247 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005248 auto BaseStmt = AStmt;
5249 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5250 BaseStmt = CS->getCapturedStmt();
5251 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5252 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005253 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005254 return StmtError();
5255 // All associated statements must be '#pragma omp section' except for
5256 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005257 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005258 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5259 if (SectionStmt)
5260 Diag(SectionStmt->getLocStart(),
5261 diag::err_omp_sections_substmt_not_section);
5262 return StmtError();
5263 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005264 cast<OMPSectionDirective>(SectionStmt)
5265 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005266 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005267 } else {
5268 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5269 return StmtError();
5270 }
5271
5272 getCurFunction()->setHasBranchProtectedScope();
5273
Alexey Bataev25e5b442015-09-15 12:52:43 +00005274 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5275 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005276}
5277
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005278StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5279 SourceLocation StartLoc,
5280 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005281 if (!AStmt)
5282 return StmtError();
5283
5284 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005285
5286 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005287 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005288
Alexey Bataev25e5b442015-09-15 12:52:43 +00005289 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5290 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005291}
5292
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005293StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5294 Stmt *AStmt,
5295 SourceLocation StartLoc,
5296 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005297 if (!AStmt)
5298 return StmtError();
5299
5300 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005301
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005302 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005303
Alexey Bataev3255bf32015-01-19 05:20:46 +00005304 // OpenMP [2.7.3, single Construct, Restrictions]
5305 // The copyprivate clause must not be used with the nowait clause.
5306 OMPClause *Nowait = nullptr;
5307 OMPClause *Copyprivate = nullptr;
5308 for (auto *Clause : Clauses) {
5309 if (Clause->getClauseKind() == OMPC_nowait)
5310 Nowait = Clause;
5311 else if (Clause->getClauseKind() == OMPC_copyprivate)
5312 Copyprivate = Clause;
5313 if (Copyprivate && Nowait) {
5314 Diag(Copyprivate->getLocStart(),
5315 diag::err_omp_single_copyprivate_with_nowait);
5316 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5317 return StmtError();
5318 }
5319 }
5320
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005321 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5322}
5323
Alexander Musman80c22892014-07-17 08:54:58 +00005324StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5325 SourceLocation StartLoc,
5326 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005327 if (!AStmt)
5328 return StmtError();
5329
5330 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005331
5332 getCurFunction()->setHasBranchProtectedScope();
5333
5334 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5335}
5336
Alexey Bataev28c75412015-12-15 08:19:24 +00005337StmtResult Sema::ActOnOpenMPCriticalDirective(
5338 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5339 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005340 if (!AStmt)
5341 return StmtError();
5342
5343 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005344
Alexey Bataev28c75412015-12-15 08:19:24 +00005345 bool ErrorFound = false;
5346 llvm::APSInt Hint;
5347 SourceLocation HintLoc;
5348 bool DependentHint = false;
5349 for (auto *C : Clauses) {
5350 if (C->getClauseKind() == OMPC_hint) {
5351 if (!DirName.getName()) {
5352 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5353 ErrorFound = true;
5354 }
5355 Expr *E = cast<OMPHintClause>(C)->getHint();
5356 if (E->isTypeDependent() || E->isValueDependent() ||
5357 E->isInstantiationDependent())
5358 DependentHint = true;
5359 else {
5360 Hint = E->EvaluateKnownConstInt(Context);
5361 HintLoc = C->getLocStart();
5362 }
5363 }
5364 }
5365 if (ErrorFound)
5366 return StmtError();
5367 auto Pair = DSAStack->getCriticalWithHint(DirName);
5368 if (Pair.first && DirName.getName() && !DependentHint) {
5369 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5370 Diag(StartLoc, diag::err_omp_critical_with_hint);
5371 if (HintLoc.isValid()) {
5372 Diag(HintLoc, diag::note_omp_critical_hint_here)
5373 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5374 } else
5375 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5376 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5377 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5378 << 1
5379 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5380 /*Radix=*/10, /*Signed=*/false);
5381 } else
5382 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5383 }
5384 }
5385
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005386 getCurFunction()->setHasBranchProtectedScope();
5387
Alexey Bataev28c75412015-12-15 08:19:24 +00005388 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5389 Clauses, AStmt);
5390 if (!Pair.first && DirName.getName() && !DependentHint)
5391 DSAStack->addCriticalWithHint(Dir, Hint);
5392 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005393}
5394
Alexey Bataev4acb8592014-07-07 13:01:15 +00005395StmtResult Sema::ActOnOpenMPParallelForDirective(
5396 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5397 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005398 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005399 if (!AStmt)
5400 return StmtError();
5401
Alexey Bataev4acb8592014-07-07 13:01:15 +00005402 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5403 // 1.2.2 OpenMP Language Terminology
5404 // Structured block - An executable statement with a single entry at the
5405 // top and a single exit at the bottom.
5406 // The point of exit cannot be a branch out of the structured block.
5407 // longjmp() and throw() must not violate the entry/exit criteria.
5408 CS->getCapturedDecl()->setNothrow();
5409
Alexander Musmanc6388682014-12-15 07:07:06 +00005410 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005411 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5412 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005413 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005414 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5415 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5416 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005417 if (NestedLoopCount == 0)
5418 return StmtError();
5419
Alexander Musmana5f070a2014-10-01 06:03:56 +00005420 assert((CurContext->isDependentContext() || B.builtAll()) &&
5421 "omp parallel for loop exprs were not built");
5422
Alexey Bataev54acd402015-08-04 11:18:19 +00005423 if (!CurContext->isDependentContext()) {
5424 // Finalize the clauses that need pre-built expressions for CodeGen.
5425 for (auto C : Clauses) {
5426 if (auto LC = dyn_cast<OMPLinearClause>(C))
5427 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005428 B.NumIterations, *this, CurScope,
5429 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005430 return StmtError();
5431 }
5432 }
5433
Alexey Bataev4acb8592014-07-07 13:01:15 +00005434 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005435 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005436 NestedLoopCount, Clauses, AStmt, B,
5437 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005438}
5439
Alexander Musmane4e893b2014-09-23 09:33:00 +00005440StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5441 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5442 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005443 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005444 if (!AStmt)
5445 return StmtError();
5446
Alexander Musmane4e893b2014-09-23 09:33:00 +00005447 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5448 // 1.2.2 OpenMP Language Terminology
5449 // Structured block - An executable statement with a single entry at the
5450 // top and a single exit at the bottom.
5451 // The point of exit cannot be a branch out of the structured block.
5452 // longjmp() and throw() must not violate the entry/exit criteria.
5453 CS->getCapturedDecl()->setNothrow();
5454
Alexander Musmanc6388682014-12-15 07:07:06 +00005455 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005456 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5457 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005458 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005459 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5460 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5461 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005462 if (NestedLoopCount == 0)
5463 return StmtError();
5464
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005465 if (!CurContext->isDependentContext()) {
5466 // Finalize the clauses that need pre-built expressions for CodeGen.
5467 for (auto C : Clauses) {
5468 if (auto LC = dyn_cast<OMPLinearClause>(C))
5469 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005470 B.NumIterations, *this, CurScope,
5471 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005472 return StmtError();
5473 }
5474 }
5475
Alexey Bataev66b15b52015-08-21 11:14:16 +00005476 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5477 // If both simdlen and safelen clauses are specified, the value of the simdlen
5478 // parameter must be less than or equal to the value of the safelen parameter.
5479 OMPSafelenClause *Safelen = nullptr;
5480 OMPSimdlenClause *Simdlen = nullptr;
5481 for (auto *Clause : Clauses) {
5482 if (Clause->getClauseKind() == OMPC_safelen)
5483 Safelen = cast<OMPSafelenClause>(Clause);
5484 else if (Clause->getClauseKind() == OMPC_simdlen)
5485 Simdlen = cast<OMPSimdlenClause>(Clause);
5486 if (Safelen && Simdlen)
5487 break;
5488 }
5489 if (Simdlen && Safelen &&
5490 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5491 Safelen->getSafelen()))
5492 return StmtError();
5493
Alexander Musmane4e893b2014-09-23 09:33:00 +00005494 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005495 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005496 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005497}
5498
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005499StmtResult
5500Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5501 Stmt *AStmt, SourceLocation StartLoc,
5502 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005503 if (!AStmt)
5504 return StmtError();
5505
5506 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005507 auto BaseStmt = AStmt;
5508 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5509 BaseStmt = CS->getCapturedStmt();
5510 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5511 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005512 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005513 return StmtError();
5514 // All associated statements must be '#pragma omp section' except for
5515 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005516 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005517 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5518 if (SectionStmt)
5519 Diag(SectionStmt->getLocStart(),
5520 diag::err_omp_parallel_sections_substmt_not_section);
5521 return StmtError();
5522 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005523 cast<OMPSectionDirective>(SectionStmt)
5524 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005525 }
5526 } else {
5527 Diag(AStmt->getLocStart(),
5528 diag::err_omp_parallel_sections_not_compound_stmt);
5529 return StmtError();
5530 }
5531
5532 getCurFunction()->setHasBranchProtectedScope();
5533
Alexey Bataev25e5b442015-09-15 12:52:43 +00005534 return OMPParallelSectionsDirective::Create(
5535 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005536}
5537
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005538StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5539 Stmt *AStmt, SourceLocation StartLoc,
5540 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005541 if (!AStmt)
5542 return StmtError();
5543
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005544 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5545 // 1.2.2 OpenMP Language Terminology
5546 // Structured block - An executable statement with a single entry at the
5547 // top and a single exit at the bottom.
5548 // The point of exit cannot be a branch out of the structured block.
5549 // longjmp() and throw() must not violate the entry/exit criteria.
5550 CS->getCapturedDecl()->setNothrow();
5551
5552 getCurFunction()->setHasBranchProtectedScope();
5553
Alexey Bataev25e5b442015-09-15 12:52:43 +00005554 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5555 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005556}
5557
Alexey Bataev68446b72014-07-18 07:47:19 +00005558StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5559 SourceLocation EndLoc) {
5560 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5561}
5562
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005563StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5564 SourceLocation EndLoc) {
5565 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5566}
5567
Alexey Bataev2df347a2014-07-18 10:17:07 +00005568StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5569 SourceLocation EndLoc) {
5570 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5571}
5572
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005573StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5574 SourceLocation StartLoc,
5575 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005576 if (!AStmt)
5577 return StmtError();
5578
5579 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005580
5581 getCurFunction()->setHasBranchProtectedScope();
5582
5583 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5584}
5585
Alexey Bataev6125da92014-07-21 11:26:11 +00005586StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5587 SourceLocation StartLoc,
5588 SourceLocation EndLoc) {
5589 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5590 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5591}
5592
Alexey Bataev346265e2015-09-25 10:37:12 +00005593StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5594 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005595 SourceLocation StartLoc,
5596 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005597 OMPClause *DependFound = nullptr;
5598 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005599 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005600 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005601 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005602 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005603 for (auto *C : Clauses) {
5604 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5605 DependFound = C;
5606 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5607 if (DependSourceClause) {
5608 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5609 << getOpenMPDirectiveName(OMPD_ordered)
5610 << getOpenMPClauseName(OMPC_depend) << 2;
5611 ErrorFound = true;
5612 } else
5613 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005614 if (DependSinkClause) {
5615 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5616 << 0;
5617 ErrorFound = true;
5618 }
5619 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5620 if (DependSourceClause) {
5621 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5622 << 1;
5623 ErrorFound = true;
5624 }
5625 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005626 }
5627 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005628 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005629 else if (C->getClauseKind() == OMPC_simd)
5630 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005631 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005632 if (!ErrorFound && !SC &&
5633 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005634 // OpenMP [2.8.1,simd Construct, Restrictions]
5635 // An ordered construct with the simd clause is the only OpenMP construct
5636 // that can appear in the simd region.
5637 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005638 ErrorFound = true;
5639 } else if (DependFound && (TC || SC)) {
5640 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5641 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5642 ErrorFound = true;
5643 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5644 Diag(DependFound->getLocStart(),
5645 diag::err_omp_ordered_directive_without_param);
5646 ErrorFound = true;
5647 } else if (TC || Clauses.empty()) {
5648 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5649 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5650 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5651 << (TC != nullptr);
5652 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5653 ErrorFound = true;
5654 }
5655 }
5656 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005657 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005658
5659 if (AStmt) {
5660 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5661
5662 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005663 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005664
5665 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005666}
5667
Alexey Bataev1d160b12015-03-13 12:27:31 +00005668namespace {
5669/// \brief Helper class for checking expression in 'omp atomic [update]'
5670/// construct.
5671class OpenMPAtomicUpdateChecker {
5672 /// \brief Error results for atomic update expressions.
5673 enum ExprAnalysisErrorCode {
5674 /// \brief A statement is not an expression statement.
5675 NotAnExpression,
5676 /// \brief Expression is not builtin binary or unary operation.
5677 NotABinaryOrUnaryExpression,
5678 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5679 NotAnUnaryIncDecExpression,
5680 /// \brief An expression is not of scalar type.
5681 NotAScalarType,
5682 /// \brief A binary operation is not an assignment operation.
5683 NotAnAssignmentOp,
5684 /// \brief RHS part of the binary operation is not a binary expression.
5685 NotABinaryExpression,
5686 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5687 /// expression.
5688 NotABinaryOperator,
5689 /// \brief RHS binary operation does not have reference to the updated LHS
5690 /// part.
5691 NotAnUpdateExpression,
5692 /// \brief No errors is found.
5693 NoError
5694 };
5695 /// \brief Reference to Sema.
5696 Sema &SemaRef;
5697 /// \brief A location for note diagnostics (when error is found).
5698 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005699 /// \brief 'x' lvalue part of the source atomic expression.
5700 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005701 /// \brief 'expr' rvalue part of the source atomic expression.
5702 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005703 /// \brief Helper expression of the form
5704 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5705 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5706 Expr *UpdateExpr;
5707 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5708 /// important for non-associative operations.
5709 bool IsXLHSInRHSPart;
5710 BinaryOperatorKind Op;
5711 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005712 /// \brief true if the source expression is a postfix unary operation, false
5713 /// if it is a prefix unary operation.
5714 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005715
5716public:
5717 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005718 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005719 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005720 /// \brief Check specified statement that it is suitable for 'atomic update'
5721 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005722 /// expression. If DiagId and NoteId == 0, then only check is performed
5723 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005724 /// \param DiagId Diagnostic which should be emitted if error is found.
5725 /// \param NoteId Diagnostic note for the main error message.
5726 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005727 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005728 /// \brief Return the 'x' lvalue part of the source atomic expression.
5729 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005730 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5731 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005732 /// \brief Return the update expression used in calculation of the updated
5733 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5734 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5735 Expr *getUpdateExpr() const { return UpdateExpr; }
5736 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5737 /// false otherwise.
5738 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5739
Alexey Bataevb78ca832015-04-01 03:33:17 +00005740 /// \brief true if the source expression is a postfix unary operation, false
5741 /// if it is a prefix unary operation.
5742 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5743
Alexey Bataev1d160b12015-03-13 12:27:31 +00005744private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005745 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5746 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005747};
5748} // namespace
5749
5750bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5751 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5752 ExprAnalysisErrorCode ErrorFound = NoError;
5753 SourceLocation ErrorLoc, NoteLoc;
5754 SourceRange ErrorRange, NoteRange;
5755 // Allowed constructs are:
5756 // x = x binop expr;
5757 // x = expr binop x;
5758 if (AtomicBinOp->getOpcode() == BO_Assign) {
5759 X = AtomicBinOp->getLHS();
5760 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5761 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5762 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5763 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5764 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005765 Op = AtomicInnerBinOp->getOpcode();
5766 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005767 auto *LHS = AtomicInnerBinOp->getLHS();
5768 auto *RHS = AtomicInnerBinOp->getRHS();
5769 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5770 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5771 /*Canonical=*/true);
5772 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5773 /*Canonical=*/true);
5774 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5775 /*Canonical=*/true);
5776 if (XId == LHSId) {
5777 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005778 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005779 } else if (XId == RHSId) {
5780 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005781 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005782 } else {
5783 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5784 ErrorRange = AtomicInnerBinOp->getSourceRange();
5785 NoteLoc = X->getExprLoc();
5786 NoteRange = X->getSourceRange();
5787 ErrorFound = NotAnUpdateExpression;
5788 }
5789 } else {
5790 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5791 ErrorRange = AtomicInnerBinOp->getSourceRange();
5792 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5793 NoteRange = SourceRange(NoteLoc, NoteLoc);
5794 ErrorFound = NotABinaryOperator;
5795 }
5796 } else {
5797 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5798 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5799 ErrorFound = NotABinaryExpression;
5800 }
5801 } else {
5802 ErrorLoc = AtomicBinOp->getExprLoc();
5803 ErrorRange = AtomicBinOp->getSourceRange();
5804 NoteLoc = AtomicBinOp->getOperatorLoc();
5805 NoteRange = SourceRange(NoteLoc, NoteLoc);
5806 ErrorFound = NotAnAssignmentOp;
5807 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005808 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005809 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5810 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5811 return true;
5812 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005813 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005814 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005815}
5816
5817bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5818 unsigned NoteId) {
5819 ExprAnalysisErrorCode ErrorFound = NoError;
5820 SourceLocation ErrorLoc, NoteLoc;
5821 SourceRange ErrorRange, NoteRange;
5822 // Allowed constructs are:
5823 // x++;
5824 // x--;
5825 // ++x;
5826 // --x;
5827 // x binop= expr;
5828 // x = x binop expr;
5829 // x = expr binop x;
5830 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5831 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5832 if (AtomicBody->getType()->isScalarType() ||
5833 AtomicBody->isInstantiationDependent()) {
5834 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5835 AtomicBody->IgnoreParenImpCasts())) {
5836 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005837 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005838 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005839 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005840 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005841 X = AtomicCompAssignOp->getLHS();
5842 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005843 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5844 AtomicBody->IgnoreParenImpCasts())) {
5845 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005846 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5847 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005848 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005849 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5850 // Check for Unary Operation
5851 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005852 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005853 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5854 OpLoc = AtomicUnaryOp->getOperatorLoc();
5855 X = AtomicUnaryOp->getSubExpr();
5856 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5857 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005858 } else {
5859 ErrorFound = NotAnUnaryIncDecExpression;
5860 ErrorLoc = AtomicUnaryOp->getExprLoc();
5861 ErrorRange = AtomicUnaryOp->getSourceRange();
5862 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5863 NoteRange = SourceRange(NoteLoc, NoteLoc);
5864 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005865 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005866 ErrorFound = NotABinaryOrUnaryExpression;
5867 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5868 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5869 }
5870 } else {
5871 ErrorFound = NotAScalarType;
5872 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5873 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5874 }
5875 } else {
5876 ErrorFound = NotAnExpression;
5877 NoteLoc = ErrorLoc = S->getLocStart();
5878 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5879 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005880 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005881 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5882 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5883 return true;
5884 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005885 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005886 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005887 // Build an update expression of form 'OpaqueValueExpr(x) binop
5888 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5889 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5890 auto *OVEX = new (SemaRef.getASTContext())
5891 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5892 auto *OVEExpr = new (SemaRef.getASTContext())
5893 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5894 auto Update =
5895 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5896 IsXLHSInRHSPart ? OVEExpr : OVEX);
5897 if (Update.isInvalid())
5898 return true;
5899 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5900 Sema::AA_Casting);
5901 if (Update.isInvalid())
5902 return true;
5903 UpdateExpr = Update.get();
5904 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005905 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005906}
5907
Alexey Bataev0162e452014-07-22 10:10:35 +00005908StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5909 Stmt *AStmt,
5910 SourceLocation StartLoc,
5911 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005912 if (!AStmt)
5913 return StmtError();
5914
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005915 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005916 // 1.2.2 OpenMP Language Terminology
5917 // Structured block - An executable statement with a single entry at the
5918 // top and a single exit at the bottom.
5919 // The point of exit cannot be a branch out of the structured block.
5920 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005921 OpenMPClauseKind AtomicKind = OMPC_unknown;
5922 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005923 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005924 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005925 C->getClauseKind() == OMPC_update ||
5926 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005927 if (AtomicKind != OMPC_unknown) {
5928 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5929 << SourceRange(C->getLocStart(), C->getLocEnd());
5930 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5931 << getOpenMPClauseName(AtomicKind);
5932 } else {
5933 AtomicKind = C->getClauseKind();
5934 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005935 }
5936 }
5937 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005938
Alexey Bataev459dec02014-07-24 06:46:57 +00005939 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005940 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5941 Body = EWC->getSubExpr();
5942
Alexey Bataev62cec442014-11-18 10:14:22 +00005943 Expr *X = nullptr;
5944 Expr *V = nullptr;
5945 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005946 Expr *UE = nullptr;
5947 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005948 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005949 // OpenMP [2.12.6, atomic Construct]
5950 // In the next expressions:
5951 // * x and v (as applicable) are both l-value expressions with scalar type.
5952 // * During the execution of an atomic region, multiple syntactic
5953 // occurrences of x must designate the same storage location.
5954 // * Neither of v and expr (as applicable) may access the storage location
5955 // designated by x.
5956 // * Neither of x and expr (as applicable) may access the storage location
5957 // designated by v.
5958 // * expr is an expression with scalar type.
5959 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5960 // * binop, binop=, ++, and -- are not overloaded operators.
5961 // * The expression x binop expr must be numerically equivalent to x binop
5962 // (expr). This requirement is satisfied if the operators in expr have
5963 // precedence greater than binop, or by using parentheses around expr or
5964 // subexpressions of expr.
5965 // * The expression expr binop x must be numerically equivalent to (expr)
5966 // binop x. This requirement is satisfied if the operators in expr have
5967 // precedence equal to or greater than binop, or by using parentheses around
5968 // expr or subexpressions of expr.
5969 // * For forms that allow multiple occurrences of x, the number of times
5970 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005971 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005972 enum {
5973 NotAnExpression,
5974 NotAnAssignmentOp,
5975 NotAScalarType,
5976 NotAnLValue,
5977 NoError
5978 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005979 SourceLocation ErrorLoc, NoteLoc;
5980 SourceRange ErrorRange, NoteRange;
5981 // If clause is read:
5982 // v = x;
5983 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5984 auto AtomicBinOp =
5985 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5986 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5987 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5988 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5989 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5990 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5991 if (!X->isLValue() || !V->isLValue()) {
5992 auto NotLValueExpr = X->isLValue() ? V : X;
5993 ErrorFound = NotAnLValue;
5994 ErrorLoc = AtomicBinOp->getExprLoc();
5995 ErrorRange = AtomicBinOp->getSourceRange();
5996 NoteLoc = NotLValueExpr->getExprLoc();
5997 NoteRange = NotLValueExpr->getSourceRange();
5998 }
5999 } else if (!X->isInstantiationDependent() ||
6000 !V->isInstantiationDependent()) {
6001 auto NotScalarExpr =
6002 (X->isInstantiationDependent() || X->getType()->isScalarType())
6003 ? V
6004 : X;
6005 ErrorFound = NotAScalarType;
6006 ErrorLoc = AtomicBinOp->getExprLoc();
6007 ErrorRange = AtomicBinOp->getSourceRange();
6008 NoteLoc = NotScalarExpr->getExprLoc();
6009 NoteRange = NotScalarExpr->getSourceRange();
6010 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006011 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006012 ErrorFound = NotAnAssignmentOp;
6013 ErrorLoc = AtomicBody->getExprLoc();
6014 ErrorRange = AtomicBody->getSourceRange();
6015 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6016 : AtomicBody->getExprLoc();
6017 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6018 : AtomicBody->getSourceRange();
6019 }
6020 } else {
6021 ErrorFound = NotAnExpression;
6022 NoteLoc = ErrorLoc = Body->getLocStart();
6023 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006024 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006025 if (ErrorFound != NoError) {
6026 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6027 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006028 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6029 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006030 return StmtError();
6031 } else if (CurContext->isDependentContext())
6032 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006033 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006034 enum {
6035 NotAnExpression,
6036 NotAnAssignmentOp,
6037 NotAScalarType,
6038 NotAnLValue,
6039 NoError
6040 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006041 SourceLocation ErrorLoc, NoteLoc;
6042 SourceRange ErrorRange, NoteRange;
6043 // If clause is write:
6044 // x = expr;
6045 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
6046 auto AtomicBinOp =
6047 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6048 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006049 X = AtomicBinOp->getLHS();
6050 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006051 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6052 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6053 if (!X->isLValue()) {
6054 ErrorFound = NotAnLValue;
6055 ErrorLoc = AtomicBinOp->getExprLoc();
6056 ErrorRange = AtomicBinOp->getSourceRange();
6057 NoteLoc = X->getExprLoc();
6058 NoteRange = X->getSourceRange();
6059 }
6060 } else if (!X->isInstantiationDependent() ||
6061 !E->isInstantiationDependent()) {
6062 auto NotScalarExpr =
6063 (X->isInstantiationDependent() || X->getType()->isScalarType())
6064 ? E
6065 : X;
6066 ErrorFound = NotAScalarType;
6067 ErrorLoc = AtomicBinOp->getExprLoc();
6068 ErrorRange = AtomicBinOp->getSourceRange();
6069 NoteLoc = NotScalarExpr->getExprLoc();
6070 NoteRange = NotScalarExpr->getSourceRange();
6071 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006072 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006073 ErrorFound = NotAnAssignmentOp;
6074 ErrorLoc = AtomicBody->getExprLoc();
6075 ErrorRange = AtomicBody->getSourceRange();
6076 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6077 : AtomicBody->getExprLoc();
6078 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6079 : AtomicBody->getSourceRange();
6080 }
6081 } else {
6082 ErrorFound = NotAnExpression;
6083 NoteLoc = ErrorLoc = Body->getLocStart();
6084 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006085 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006086 if (ErrorFound != NoError) {
6087 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6088 << ErrorRange;
6089 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6090 << NoteRange;
6091 return StmtError();
6092 } else if (CurContext->isDependentContext())
6093 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006094 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006095 // If clause is update:
6096 // x++;
6097 // x--;
6098 // ++x;
6099 // --x;
6100 // x binop= expr;
6101 // x = x binop expr;
6102 // x = expr binop x;
6103 OpenMPAtomicUpdateChecker Checker(*this);
6104 if (Checker.checkStatement(
6105 Body, (AtomicKind == OMPC_update)
6106 ? diag::err_omp_atomic_update_not_expression_statement
6107 : diag::err_omp_atomic_not_expression_statement,
6108 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006109 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006110 if (!CurContext->isDependentContext()) {
6111 E = Checker.getExpr();
6112 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006113 UE = Checker.getUpdateExpr();
6114 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006115 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006116 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006117 enum {
6118 NotAnAssignmentOp,
6119 NotACompoundStatement,
6120 NotTwoSubstatements,
6121 NotASpecificExpression,
6122 NoError
6123 } ErrorFound = NoError;
6124 SourceLocation ErrorLoc, NoteLoc;
6125 SourceRange ErrorRange, NoteRange;
6126 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6127 // If clause is a capture:
6128 // v = x++;
6129 // v = x--;
6130 // v = ++x;
6131 // v = --x;
6132 // v = x binop= expr;
6133 // v = x = x binop expr;
6134 // v = x = expr binop x;
6135 auto *AtomicBinOp =
6136 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6137 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6138 V = AtomicBinOp->getLHS();
6139 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6140 OpenMPAtomicUpdateChecker Checker(*this);
6141 if (Checker.checkStatement(
6142 Body, diag::err_omp_atomic_capture_not_expression_statement,
6143 diag::note_omp_atomic_update))
6144 return StmtError();
6145 E = Checker.getExpr();
6146 X = Checker.getX();
6147 UE = Checker.getUpdateExpr();
6148 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6149 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006150 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006151 ErrorLoc = AtomicBody->getExprLoc();
6152 ErrorRange = AtomicBody->getSourceRange();
6153 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6154 : AtomicBody->getExprLoc();
6155 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6156 : AtomicBody->getSourceRange();
6157 ErrorFound = NotAnAssignmentOp;
6158 }
6159 if (ErrorFound != NoError) {
6160 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6161 << ErrorRange;
6162 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6163 return StmtError();
6164 } else if (CurContext->isDependentContext()) {
6165 UE = V = E = X = nullptr;
6166 }
6167 } else {
6168 // If clause is a capture:
6169 // { v = x; x = expr; }
6170 // { v = x; x++; }
6171 // { v = x; x--; }
6172 // { v = x; ++x; }
6173 // { v = x; --x; }
6174 // { v = x; x binop= expr; }
6175 // { v = x; x = x binop expr; }
6176 // { v = x; x = expr binop x; }
6177 // { x++; v = x; }
6178 // { x--; v = x; }
6179 // { ++x; v = x; }
6180 // { --x; v = x; }
6181 // { x binop= expr; v = x; }
6182 // { x = x binop expr; v = x; }
6183 // { x = expr binop x; v = x; }
6184 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6185 // Check that this is { expr1; expr2; }
6186 if (CS->size() == 2) {
6187 auto *First = CS->body_front();
6188 auto *Second = CS->body_back();
6189 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6190 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6191 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6192 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6193 // Need to find what subexpression is 'v' and what is 'x'.
6194 OpenMPAtomicUpdateChecker Checker(*this);
6195 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6196 BinaryOperator *BinOp = nullptr;
6197 if (IsUpdateExprFound) {
6198 BinOp = dyn_cast<BinaryOperator>(First);
6199 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6200 }
6201 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6202 // { v = x; x++; }
6203 // { v = x; x--; }
6204 // { v = x; ++x; }
6205 // { v = x; --x; }
6206 // { v = x; x binop= expr; }
6207 // { v = x; x = x binop expr; }
6208 // { v = x; x = expr binop x; }
6209 // Check that the first expression has form v = x.
6210 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6211 llvm::FoldingSetNodeID XId, PossibleXId;
6212 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6213 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6214 IsUpdateExprFound = XId == PossibleXId;
6215 if (IsUpdateExprFound) {
6216 V = BinOp->getLHS();
6217 X = Checker.getX();
6218 E = Checker.getExpr();
6219 UE = Checker.getUpdateExpr();
6220 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006221 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006222 }
6223 }
6224 if (!IsUpdateExprFound) {
6225 IsUpdateExprFound = !Checker.checkStatement(First);
6226 BinOp = nullptr;
6227 if (IsUpdateExprFound) {
6228 BinOp = dyn_cast<BinaryOperator>(Second);
6229 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6230 }
6231 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6232 // { x++; v = x; }
6233 // { x--; v = x; }
6234 // { ++x; v = x; }
6235 // { --x; v = x; }
6236 // { x binop= expr; v = x; }
6237 // { x = x binop expr; v = x; }
6238 // { x = expr binop x; v = x; }
6239 // Check that the second expression has form v = x.
6240 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6241 llvm::FoldingSetNodeID XId, PossibleXId;
6242 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6243 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6244 IsUpdateExprFound = XId == PossibleXId;
6245 if (IsUpdateExprFound) {
6246 V = BinOp->getLHS();
6247 X = Checker.getX();
6248 E = Checker.getExpr();
6249 UE = Checker.getUpdateExpr();
6250 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006251 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006252 }
6253 }
6254 }
6255 if (!IsUpdateExprFound) {
6256 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006257 auto *FirstExpr = dyn_cast<Expr>(First);
6258 auto *SecondExpr = dyn_cast<Expr>(Second);
6259 if (!FirstExpr || !SecondExpr ||
6260 !(FirstExpr->isInstantiationDependent() ||
6261 SecondExpr->isInstantiationDependent())) {
6262 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6263 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006264 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006265 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6266 : First->getLocStart();
6267 NoteRange = ErrorRange = FirstBinOp
6268 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006269 : SourceRange(ErrorLoc, ErrorLoc);
6270 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006271 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6272 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6273 ErrorFound = NotAnAssignmentOp;
6274 NoteLoc = ErrorLoc = SecondBinOp
6275 ? SecondBinOp->getOperatorLoc()
6276 : Second->getLocStart();
6277 NoteRange = ErrorRange =
6278 SecondBinOp ? SecondBinOp->getSourceRange()
6279 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006280 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006281 auto *PossibleXRHSInFirst =
6282 FirstBinOp->getRHS()->IgnoreParenImpCasts();
6283 auto *PossibleXLHSInSecond =
6284 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6285 llvm::FoldingSetNodeID X1Id, X2Id;
6286 PossibleXRHSInFirst->Profile(X1Id, Context,
6287 /*Canonical=*/true);
6288 PossibleXLHSInSecond->Profile(X2Id, Context,
6289 /*Canonical=*/true);
6290 IsUpdateExprFound = X1Id == X2Id;
6291 if (IsUpdateExprFound) {
6292 V = FirstBinOp->getLHS();
6293 X = SecondBinOp->getLHS();
6294 E = SecondBinOp->getRHS();
6295 UE = nullptr;
6296 IsXLHSInRHSPart = false;
6297 IsPostfixUpdate = true;
6298 } else {
6299 ErrorFound = NotASpecificExpression;
6300 ErrorLoc = FirstBinOp->getExprLoc();
6301 ErrorRange = FirstBinOp->getSourceRange();
6302 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6303 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6304 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006305 }
6306 }
6307 }
6308 }
6309 } else {
6310 NoteLoc = ErrorLoc = Body->getLocStart();
6311 NoteRange = ErrorRange =
6312 SourceRange(Body->getLocStart(), Body->getLocStart());
6313 ErrorFound = NotTwoSubstatements;
6314 }
6315 } else {
6316 NoteLoc = ErrorLoc = Body->getLocStart();
6317 NoteRange = ErrorRange =
6318 SourceRange(Body->getLocStart(), Body->getLocStart());
6319 ErrorFound = NotACompoundStatement;
6320 }
6321 if (ErrorFound != NoError) {
6322 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6323 << ErrorRange;
6324 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6325 return StmtError();
6326 } else if (CurContext->isDependentContext()) {
6327 UE = V = E = X = nullptr;
6328 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006329 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006330 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006331
6332 getCurFunction()->setHasBranchProtectedScope();
6333
Alexey Bataev62cec442014-11-18 10:14:22 +00006334 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006335 X, V, E, UE, IsXLHSInRHSPart,
6336 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006337}
6338
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006339StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6340 Stmt *AStmt,
6341 SourceLocation StartLoc,
6342 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006343 if (!AStmt)
6344 return StmtError();
6345
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006346 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6347 // 1.2.2 OpenMP Language Terminology
6348 // Structured block - An executable statement with a single entry at the
6349 // top and a single exit at the bottom.
6350 // The point of exit cannot be a branch out of the structured block.
6351 // longjmp() and throw() must not violate the entry/exit criteria.
6352 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006353
Alexey Bataev13314bf2014-10-09 04:18:56 +00006354 // OpenMP [2.16, Nesting of Regions]
6355 // If specified, a teams construct must be contained within a target
6356 // construct. That target construct must contain no statements or directives
6357 // outside of the teams construct.
6358 if (DSAStack->hasInnerTeamsRegion()) {
6359 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6360 bool OMPTeamsFound = true;
6361 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6362 auto I = CS->body_begin();
6363 while (I != CS->body_end()) {
6364 auto OED = dyn_cast<OMPExecutableDirective>(*I);
6365 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6366 OMPTeamsFound = false;
6367 break;
6368 }
6369 ++I;
6370 }
6371 assert(I != CS->body_end() && "Not found statement");
6372 S = *I;
6373 }
6374 if (!OMPTeamsFound) {
6375 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6376 Diag(DSAStack->getInnerTeamsRegionLoc(),
6377 diag::note_omp_nested_teams_construct_here);
6378 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6379 << isa<OMPExecutableDirective>(S);
6380 return StmtError();
6381 }
6382 }
6383
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006384 getCurFunction()->setHasBranchProtectedScope();
6385
6386 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6387}
6388
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006389StmtResult
6390Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6391 Stmt *AStmt, SourceLocation StartLoc,
6392 SourceLocation EndLoc) {
6393 if (!AStmt)
6394 return StmtError();
6395
6396 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6397 // 1.2.2 OpenMP Language Terminology
6398 // Structured block - An executable statement with a single entry at the
6399 // top and a single exit at the bottom.
6400 // The point of exit cannot be a branch out of the structured block.
6401 // longjmp() and throw() must not violate the entry/exit criteria.
6402 CS->getCapturedDecl()->setNothrow();
6403
6404 getCurFunction()->setHasBranchProtectedScope();
6405
6406 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6407 AStmt);
6408}
6409
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006410StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6411 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6412 SourceLocation EndLoc,
6413 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6414 if (!AStmt)
6415 return StmtError();
6416
6417 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6418 // 1.2.2 OpenMP Language Terminology
6419 // Structured block - An executable statement with a single entry at the
6420 // top and a single exit at the bottom.
6421 // The point of exit cannot be a branch out of the structured block.
6422 // longjmp() and throw() must not violate the entry/exit criteria.
6423 CS->getCapturedDecl()->setNothrow();
6424
6425 OMPLoopDirective::HelperExprs B;
6426 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6427 // define the nested loops number.
6428 unsigned NestedLoopCount =
6429 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6430 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6431 VarsWithImplicitDSA, B);
6432 if (NestedLoopCount == 0)
6433 return StmtError();
6434
6435 assert((CurContext->isDependentContext() || B.builtAll()) &&
6436 "omp target parallel for loop exprs were not built");
6437
6438 if (!CurContext->isDependentContext()) {
6439 // Finalize the clauses that need pre-built expressions for CodeGen.
6440 for (auto C : Clauses) {
6441 if (auto LC = dyn_cast<OMPLinearClause>(C))
6442 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006443 B.NumIterations, *this, CurScope,
6444 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006445 return StmtError();
6446 }
6447 }
6448
6449 getCurFunction()->setHasBranchProtectedScope();
6450 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6451 NestedLoopCount, Clauses, AStmt,
6452 B, DSAStack->isCancelRegion());
6453}
6454
Samuel Antaodf67fc42016-01-19 19:15:56 +00006455/// \brief Check for existence of a map clause in the list of clauses.
6456static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
6457 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6458 I != E; ++I) {
6459 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6460 return true;
6461 }
6462 }
6463
6464 return false;
6465}
6466
Michael Wong65f367f2015-07-21 13:44:28 +00006467StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6468 Stmt *AStmt,
6469 SourceLocation StartLoc,
6470 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006471 if (!AStmt)
6472 return StmtError();
6473
6474 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6475
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006476 // OpenMP [2.10.1, Restrictions, p. 97]
6477 // At least one map clause must appear on the directive.
6478 if (!HasMapClause(Clauses)) {
6479 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6480 getOpenMPDirectiveName(OMPD_target_data);
6481 return StmtError();
6482 }
6483
Michael Wong65f367f2015-07-21 13:44:28 +00006484 getCurFunction()->setHasBranchProtectedScope();
6485
6486 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6487 AStmt);
6488}
6489
Samuel Antaodf67fc42016-01-19 19:15:56 +00006490StmtResult
6491Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6492 SourceLocation StartLoc,
6493 SourceLocation EndLoc) {
6494 // OpenMP [2.10.2, Restrictions, p. 99]
6495 // At least one map clause must appear on the directive.
6496 if (!HasMapClause(Clauses)) {
6497 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6498 << getOpenMPDirectiveName(OMPD_target_enter_data);
6499 return StmtError();
6500 }
6501
6502 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6503 Clauses);
6504}
6505
Samuel Antao72590762016-01-19 20:04:50 +00006506StmtResult
6507Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6508 SourceLocation StartLoc,
6509 SourceLocation EndLoc) {
6510 // OpenMP [2.10.3, Restrictions, p. 102]
6511 // At least one map clause must appear on the directive.
6512 if (!HasMapClause(Clauses)) {
6513 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6514 << getOpenMPDirectiveName(OMPD_target_exit_data);
6515 return StmtError();
6516 }
6517
6518 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6519}
6520
Samuel Antao686c70c2016-05-26 17:30:50 +00006521StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
6522 SourceLocation StartLoc,
6523 SourceLocation EndLoc) {
6524 // TODO: Set this flag accordingly when we add support for the 'to' and 'from'
6525 // clauses.
6526 bool seenMotionClause = false;
6527
6528 if (!seenMotionClause) {
6529 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
6530 return StmtError();
6531 }
6532 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses);
6533}
6534
Alexey Bataev13314bf2014-10-09 04:18:56 +00006535StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6536 Stmt *AStmt, SourceLocation StartLoc,
6537 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006538 if (!AStmt)
6539 return StmtError();
6540
Alexey Bataev13314bf2014-10-09 04:18:56 +00006541 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6542 // 1.2.2 OpenMP Language Terminology
6543 // Structured block - An executable statement with a single entry at the
6544 // top and a single exit at the bottom.
6545 // The point of exit cannot be a branch out of the structured block.
6546 // longjmp() and throw() must not violate the entry/exit criteria.
6547 CS->getCapturedDecl()->setNothrow();
6548
6549 getCurFunction()->setHasBranchProtectedScope();
6550
6551 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6552}
6553
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006554StmtResult
6555Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6556 SourceLocation EndLoc,
6557 OpenMPDirectiveKind CancelRegion) {
6558 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6559 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6560 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6561 << getOpenMPDirectiveName(CancelRegion);
6562 return StmtError();
6563 }
6564 if (DSAStack->isParentNowaitRegion()) {
6565 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6566 return StmtError();
6567 }
6568 if (DSAStack->isParentOrderedRegion()) {
6569 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6570 return StmtError();
6571 }
6572 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6573 CancelRegion);
6574}
6575
Alexey Bataev87933c72015-09-18 08:07:34 +00006576StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6577 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006578 SourceLocation EndLoc,
6579 OpenMPDirectiveKind CancelRegion) {
6580 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6581 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6582 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6583 << getOpenMPDirectiveName(CancelRegion);
6584 return StmtError();
6585 }
6586 if (DSAStack->isParentNowaitRegion()) {
6587 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6588 return StmtError();
6589 }
6590 if (DSAStack->isParentOrderedRegion()) {
6591 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6592 return StmtError();
6593 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006594 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006595 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6596 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006597}
6598
Alexey Bataev382967a2015-12-08 12:06:20 +00006599static bool checkGrainsizeNumTasksClauses(Sema &S,
6600 ArrayRef<OMPClause *> Clauses) {
6601 OMPClause *PrevClause = nullptr;
6602 bool ErrorFound = false;
6603 for (auto *C : Clauses) {
6604 if (C->getClauseKind() == OMPC_grainsize ||
6605 C->getClauseKind() == OMPC_num_tasks) {
6606 if (!PrevClause)
6607 PrevClause = C;
6608 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6609 S.Diag(C->getLocStart(),
6610 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6611 << getOpenMPClauseName(C->getClauseKind())
6612 << getOpenMPClauseName(PrevClause->getClauseKind());
6613 S.Diag(PrevClause->getLocStart(),
6614 diag::note_omp_previous_grainsize_num_tasks)
6615 << getOpenMPClauseName(PrevClause->getClauseKind());
6616 ErrorFound = true;
6617 }
6618 }
6619 }
6620 return ErrorFound;
6621}
6622
Alexey Bataev49f6e782015-12-01 04:18:41 +00006623StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6624 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6625 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006626 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006627 if (!AStmt)
6628 return StmtError();
6629
6630 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6631 OMPLoopDirective::HelperExprs B;
6632 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6633 // define the nested loops number.
6634 unsigned NestedLoopCount =
6635 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006636 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006637 VarsWithImplicitDSA, B);
6638 if (NestedLoopCount == 0)
6639 return StmtError();
6640
6641 assert((CurContext->isDependentContext() || B.builtAll()) &&
6642 "omp for loop exprs were not built");
6643
Alexey Bataev382967a2015-12-08 12:06:20 +00006644 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6645 // The grainsize clause and num_tasks clause are mutually exclusive and may
6646 // not appear on the same taskloop directive.
6647 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6648 return StmtError();
6649
Alexey Bataev49f6e782015-12-01 04:18:41 +00006650 getCurFunction()->setHasBranchProtectedScope();
6651 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6652 NestedLoopCount, Clauses, AStmt, B);
6653}
6654
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006655StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6656 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6657 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006658 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006659 if (!AStmt)
6660 return StmtError();
6661
6662 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6663 OMPLoopDirective::HelperExprs B;
6664 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6665 // define the nested loops number.
6666 unsigned NestedLoopCount =
6667 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6668 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6669 VarsWithImplicitDSA, B);
6670 if (NestedLoopCount == 0)
6671 return StmtError();
6672
6673 assert((CurContext->isDependentContext() || B.builtAll()) &&
6674 "omp for loop exprs were not built");
6675
Alexey Bataev5a3af132016-03-29 08:58:54 +00006676 if (!CurContext->isDependentContext()) {
6677 // Finalize the clauses that need pre-built expressions for CodeGen.
6678 for (auto C : Clauses) {
6679 if (auto LC = dyn_cast<OMPLinearClause>(C))
6680 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006681 B.NumIterations, *this, CurScope,
6682 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00006683 return StmtError();
6684 }
6685 }
6686
Alexey Bataev382967a2015-12-08 12:06:20 +00006687 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6688 // The grainsize clause and num_tasks clause are mutually exclusive and may
6689 // not appear on the same taskloop directive.
6690 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6691 return StmtError();
6692
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006693 getCurFunction()->setHasBranchProtectedScope();
6694 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6695 NestedLoopCount, Clauses, AStmt, B);
6696}
6697
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006698StmtResult Sema::ActOnOpenMPDistributeDirective(
6699 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6700 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006701 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006702 if (!AStmt)
6703 return StmtError();
6704
6705 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6706 OMPLoopDirective::HelperExprs B;
6707 // In presence of clause 'collapse' with number of loops, it will
6708 // define the nested loops number.
6709 unsigned NestedLoopCount =
6710 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6711 nullptr /*ordered not a clause on distribute*/, AStmt,
6712 *this, *DSAStack, VarsWithImplicitDSA, B);
6713 if (NestedLoopCount == 0)
6714 return StmtError();
6715
6716 assert((CurContext->isDependentContext() || B.builtAll()) &&
6717 "omp for loop exprs were not built");
6718
6719 getCurFunction()->setHasBranchProtectedScope();
6720 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6721 NestedLoopCount, Clauses, AStmt, B);
6722}
6723
Alexey Bataeved09d242014-05-28 05:53:51 +00006724OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006725 SourceLocation StartLoc,
6726 SourceLocation LParenLoc,
6727 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006728 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006729 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00006730 case OMPC_final:
6731 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6732 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00006733 case OMPC_num_threads:
6734 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6735 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006736 case OMPC_safelen:
6737 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6738 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00006739 case OMPC_simdlen:
6740 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6741 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00006742 case OMPC_collapse:
6743 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6744 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006745 case OMPC_ordered:
6746 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6747 break;
Michael Wonge710d542015-08-07 16:16:36 +00006748 case OMPC_device:
6749 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6750 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00006751 case OMPC_num_teams:
6752 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6753 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006754 case OMPC_thread_limit:
6755 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6756 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00006757 case OMPC_priority:
6758 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6759 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006760 case OMPC_grainsize:
6761 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6762 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00006763 case OMPC_num_tasks:
6764 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6765 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00006766 case OMPC_hint:
6767 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6768 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006769 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006770 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006771 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006772 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006773 case OMPC_private:
6774 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006775 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006776 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006777 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006778 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006779 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006780 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006781 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00006782 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006783 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006784 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006785 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006786 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006787 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006788 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006789 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006790 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006791 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006792 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006793 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006794 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006795 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006796 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006797 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006798 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006799 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00006800 case OMPC_uniform:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006801 llvm_unreachable("Clause is not allowed.");
6802 }
6803 return Res;
6804}
6805
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006806OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6807 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006808 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006809 SourceLocation NameModifierLoc,
6810 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006811 SourceLocation EndLoc) {
6812 Expr *ValExpr = Condition;
6813 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6814 !Condition->isInstantiationDependent() &&
6815 !Condition->containsUnexpandedParameterPack()) {
6816 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006817 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006818 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006819 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006820
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006821 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006822 }
6823
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006824 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6825 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006826}
6827
Alexey Bataev3778b602014-07-17 07:32:53 +00006828OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6829 SourceLocation StartLoc,
6830 SourceLocation LParenLoc,
6831 SourceLocation EndLoc) {
6832 Expr *ValExpr = Condition;
6833 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6834 !Condition->isInstantiationDependent() &&
6835 !Condition->containsUnexpandedParameterPack()) {
6836 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6837 Condition->getExprLoc(), Condition);
6838 if (Val.isInvalid())
6839 return nullptr;
6840
6841 ValExpr = Val.get();
6842 }
6843
6844 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6845}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006846ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6847 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006848 if (!Op)
6849 return ExprError();
6850
6851 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6852 public:
6853 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006854 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006855 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6856 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006857 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6858 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006859 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6860 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006861 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6862 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006863 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6864 QualType T,
6865 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006866 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6867 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006868 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6869 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006870 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006871 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006872 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006873 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6874 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006875 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6876 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006877 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6878 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006879 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006880 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006881 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006882 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6883 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006884 llvm_unreachable("conversion functions are permitted");
6885 }
6886 } ConvertDiagnoser;
6887 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6888}
6889
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006890static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006891 OpenMPClauseKind CKind,
6892 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006893 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6894 !ValExpr->isInstantiationDependent()) {
6895 SourceLocation Loc = ValExpr->getExprLoc();
6896 ExprResult Value =
6897 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6898 if (Value.isInvalid())
6899 return false;
6900
6901 ValExpr = Value.get();
6902 // The expression must evaluate to a non-negative integer value.
6903 llvm::APSInt Result;
6904 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006905 Result.isSigned() &&
6906 !((!StrictlyPositive && Result.isNonNegative()) ||
6907 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006908 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006909 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6910 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006911 return false;
6912 }
6913 }
6914 return true;
6915}
6916
Alexey Bataev568a8332014-03-06 06:15:19 +00006917OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6918 SourceLocation StartLoc,
6919 SourceLocation LParenLoc,
6920 SourceLocation EndLoc) {
6921 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006922
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006923 // OpenMP [2.5, Restrictions]
6924 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006925 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6926 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006927 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006928
Alexey Bataeved09d242014-05-28 05:53:51 +00006929 return new (Context)
6930 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006931}
6932
Alexey Bataev62c87d22014-03-21 04:51:18 +00006933ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006934 OpenMPClauseKind CKind,
6935 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006936 if (!E)
6937 return ExprError();
6938 if (E->isValueDependent() || E->isTypeDependent() ||
6939 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006940 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006941 llvm::APSInt Result;
6942 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6943 if (ICE.isInvalid())
6944 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006945 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6946 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006947 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006948 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6949 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006950 return ExprError();
6951 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006952 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6953 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6954 << E->getSourceRange();
6955 return ExprError();
6956 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006957 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6958 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006959 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006960 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006961 return ICE;
6962}
6963
6964OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6965 SourceLocation LParenLoc,
6966 SourceLocation EndLoc) {
6967 // OpenMP [2.8.1, simd construct, Description]
6968 // The parameter of the safelen clause must be a constant
6969 // positive integer expression.
6970 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6971 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006972 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006973 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006974 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006975}
6976
Alexey Bataev66b15b52015-08-21 11:14:16 +00006977OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6978 SourceLocation LParenLoc,
6979 SourceLocation EndLoc) {
6980 // OpenMP [2.8.1, simd construct, Description]
6981 // The parameter of the simdlen clause must be a constant
6982 // positive integer expression.
6983 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6984 if (Simdlen.isInvalid())
6985 return nullptr;
6986 return new (Context)
6987 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6988}
6989
Alexander Musman64d33f12014-06-04 07:53:32 +00006990OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6991 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006992 SourceLocation LParenLoc,
6993 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006994 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006995 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006996 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006997 // The parameter of the collapse clause must be a constant
6998 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006999 ExprResult NumForLoopsResult =
7000 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
7001 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00007002 return nullptr;
7003 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00007004 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00007005}
7006
Alexey Bataev10e775f2015-07-30 11:36:16 +00007007OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
7008 SourceLocation EndLoc,
7009 SourceLocation LParenLoc,
7010 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00007011 // OpenMP [2.7.1, loop construct, Description]
7012 // OpenMP [2.8.1, simd construct, Description]
7013 // OpenMP [2.9.6, distribute construct, Description]
7014 // The parameter of the ordered clause must be a constant
7015 // positive integer expression if any.
7016 if (NumForLoops && LParenLoc.isValid()) {
7017 ExprResult NumForLoopsResult =
7018 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
7019 if (NumForLoopsResult.isInvalid())
7020 return nullptr;
7021 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00007022 } else
7023 NumForLoops = nullptr;
7024 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00007025 return new (Context)
7026 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
7027}
7028
Alexey Bataeved09d242014-05-28 05:53:51 +00007029OMPClause *Sema::ActOnOpenMPSimpleClause(
7030 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
7031 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007032 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007033 switch (Kind) {
7034 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007035 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00007036 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
7037 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007038 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007039 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00007040 Res = ActOnOpenMPProcBindClause(
7041 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
7042 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007043 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007044 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007045 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007046 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007047 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007048 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007049 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007050 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007051 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007052 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007053 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007054 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007055 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007056 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007057 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007058 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007059 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007060 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007061 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007062 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007063 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007064 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007065 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007066 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007067 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007068 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007069 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007070 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007071 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007072 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007073 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007074 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007075 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007076 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007077 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007078 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007079 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007080 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007081 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007082 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007083 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007084 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007085 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007086 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007087 llvm_unreachable("Clause is not allowed.");
7088 }
7089 return Res;
7090}
7091
Alexey Bataev6402bca2015-12-28 07:25:51 +00007092static std::string
7093getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
7094 ArrayRef<unsigned> Exclude = llvm::None) {
7095 std::string Values;
7096 unsigned Bound = Last >= 2 ? Last - 2 : 0;
7097 unsigned Skipped = Exclude.size();
7098 auto S = Exclude.begin(), E = Exclude.end();
7099 for (unsigned i = First; i < Last; ++i) {
7100 if (std::find(S, E, i) != E) {
7101 --Skipped;
7102 continue;
7103 }
7104 Values += "'";
7105 Values += getOpenMPSimpleClauseTypeName(K, i);
7106 Values += "'";
7107 if (i == Bound - Skipped)
7108 Values += " or ";
7109 else if (i != Bound + 1 - Skipped)
7110 Values += ", ";
7111 }
7112 return Values;
7113}
7114
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007115OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
7116 SourceLocation KindKwLoc,
7117 SourceLocation StartLoc,
7118 SourceLocation LParenLoc,
7119 SourceLocation EndLoc) {
7120 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00007121 static_assert(OMPC_DEFAULT_unknown > 0,
7122 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007123 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007124 << getListOfPossibleValues(OMPC_default, /*First=*/0,
7125 /*Last=*/OMPC_DEFAULT_unknown)
7126 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007127 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007128 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00007129 switch (Kind) {
7130 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007131 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007132 break;
7133 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007134 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007135 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007136 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007137 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00007138 break;
7139 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007140 return new (Context)
7141 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007142}
7143
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007144OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
7145 SourceLocation KindKwLoc,
7146 SourceLocation StartLoc,
7147 SourceLocation LParenLoc,
7148 SourceLocation EndLoc) {
7149 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007150 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007151 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
7152 /*Last=*/OMPC_PROC_BIND_unknown)
7153 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007154 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007155 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007156 return new (Context)
7157 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007158}
7159
Alexey Bataev56dafe82014-06-20 07:16:17 +00007160OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007161 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007162 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007163 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007164 SourceLocation EndLoc) {
7165 OMPClause *Res = nullptr;
7166 switch (Kind) {
7167 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007168 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
7169 assert(Argument.size() == NumberOfElements &&
7170 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007171 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007172 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
7173 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
7174 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
7175 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
7176 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007177 break;
7178 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007179 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
7180 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
7181 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
7182 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007183 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00007184 case OMPC_dist_schedule:
7185 Res = ActOnOpenMPDistScheduleClause(
7186 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
7187 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
7188 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007189 case OMPC_defaultmap:
7190 enum { Modifier, DefaultmapKind };
7191 Res = ActOnOpenMPDefaultmapClause(
7192 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
7193 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
7194 StartLoc, LParenLoc, ArgumentLoc[Modifier],
7195 ArgumentLoc[DefaultmapKind], EndLoc);
7196 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00007197 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007198 case OMPC_num_threads:
7199 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007200 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007201 case OMPC_collapse:
7202 case OMPC_default:
7203 case OMPC_proc_bind:
7204 case OMPC_private:
7205 case OMPC_firstprivate:
7206 case OMPC_lastprivate:
7207 case OMPC_shared:
7208 case OMPC_reduction:
7209 case OMPC_linear:
7210 case OMPC_aligned:
7211 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007212 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007213 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007214 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007215 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007216 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007217 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007218 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007219 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007220 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007221 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007222 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007223 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007224 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007225 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007226 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007227 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007228 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007229 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007230 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007231 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007232 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007233 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007234 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007235 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007236 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007237 case OMPC_uniform:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007238 llvm_unreachable("Clause is not allowed.");
7239 }
7240 return Res;
7241}
7242
Alexey Bataev6402bca2015-12-28 07:25:51 +00007243static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
7244 OpenMPScheduleClauseModifier M2,
7245 SourceLocation M1Loc, SourceLocation M2Loc) {
7246 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
7247 SmallVector<unsigned, 2> Excluded;
7248 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
7249 Excluded.push_back(M2);
7250 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
7251 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
7252 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
7253 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
7254 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
7255 << getListOfPossibleValues(OMPC_schedule,
7256 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
7257 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7258 Excluded)
7259 << getOpenMPClauseName(OMPC_schedule);
7260 return true;
7261 }
7262 return false;
7263}
7264
Alexey Bataev56dafe82014-06-20 07:16:17 +00007265OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007266 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007267 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007268 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
7269 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
7270 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
7271 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
7272 return nullptr;
7273 // OpenMP, 2.7.1, Loop Construct, Restrictions
7274 // Either the monotonic modifier or the nonmonotonic modifier can be specified
7275 // but not both.
7276 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
7277 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
7278 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
7279 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
7280 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
7281 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
7282 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
7283 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
7284 return nullptr;
7285 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007286 if (Kind == OMPC_SCHEDULE_unknown) {
7287 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00007288 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
7289 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
7290 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7291 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7292 Exclude);
7293 } else {
7294 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7295 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007296 }
7297 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
7298 << Values << getOpenMPClauseName(OMPC_schedule);
7299 return nullptr;
7300 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00007301 // OpenMP, 2.7.1, Loop Construct, Restrictions
7302 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
7303 // schedule(guided).
7304 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
7305 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
7306 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
7307 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
7308 diag::err_omp_schedule_nonmonotonic_static);
7309 return nullptr;
7310 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007311 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00007312 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00007313 if (ChunkSize) {
7314 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
7315 !ChunkSize->isInstantiationDependent() &&
7316 !ChunkSize->containsUnexpandedParameterPack()) {
7317 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
7318 ExprResult Val =
7319 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
7320 if (Val.isInvalid())
7321 return nullptr;
7322
7323 ValExpr = Val.get();
7324
7325 // OpenMP [2.7.1, Restrictions]
7326 // chunk_size must be a loop invariant integer expression with a positive
7327 // value.
7328 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00007329 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
7330 if (Result.isSigned() && !Result.isStrictlyPositive()) {
7331 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007332 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00007333 return nullptr;
7334 }
7335 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00007336 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7337 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7338 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007339 }
7340 }
7341 }
7342
Alexey Bataev6402bca2015-12-28 07:25:51 +00007343 return new (Context)
7344 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00007345 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007346}
7347
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007348OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
7349 SourceLocation StartLoc,
7350 SourceLocation EndLoc) {
7351 OMPClause *Res = nullptr;
7352 switch (Kind) {
7353 case OMPC_ordered:
7354 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
7355 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00007356 case OMPC_nowait:
7357 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
7358 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007359 case OMPC_untied:
7360 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
7361 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007362 case OMPC_mergeable:
7363 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
7364 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007365 case OMPC_read:
7366 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
7367 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00007368 case OMPC_write:
7369 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
7370 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00007371 case OMPC_update:
7372 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
7373 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00007374 case OMPC_capture:
7375 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
7376 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007377 case OMPC_seq_cst:
7378 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
7379 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00007380 case OMPC_threads:
7381 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
7382 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007383 case OMPC_simd:
7384 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
7385 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00007386 case OMPC_nogroup:
7387 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
7388 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007389 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007390 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007391 case OMPC_num_threads:
7392 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007393 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007394 case OMPC_collapse:
7395 case OMPC_schedule:
7396 case OMPC_private:
7397 case OMPC_firstprivate:
7398 case OMPC_lastprivate:
7399 case OMPC_shared:
7400 case OMPC_reduction:
7401 case OMPC_linear:
7402 case OMPC_aligned:
7403 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007404 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007405 case OMPC_default:
7406 case OMPC_proc_bind:
7407 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007408 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007409 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007410 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007411 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007412 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007413 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007414 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007415 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00007416 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007417 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007418 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007419 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007420 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007421 case OMPC_uniform:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007422 llvm_unreachable("Clause is not allowed.");
7423 }
7424 return Res;
7425}
7426
Alexey Bataev236070f2014-06-20 11:19:47 +00007427OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
7428 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007429 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00007430 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
7431}
7432
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007433OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
7434 SourceLocation EndLoc) {
7435 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
7436}
7437
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007438OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
7439 SourceLocation EndLoc) {
7440 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
7441}
7442
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007443OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
7444 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007445 return new (Context) OMPReadClause(StartLoc, EndLoc);
7446}
7447
Alexey Bataevdea47612014-07-23 07:46:59 +00007448OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
7449 SourceLocation EndLoc) {
7450 return new (Context) OMPWriteClause(StartLoc, EndLoc);
7451}
7452
Alexey Bataev67a4f222014-07-23 10:25:33 +00007453OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
7454 SourceLocation EndLoc) {
7455 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
7456}
7457
Alexey Bataev459dec02014-07-24 06:46:57 +00007458OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
7459 SourceLocation EndLoc) {
7460 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
7461}
7462
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007463OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
7464 SourceLocation EndLoc) {
7465 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
7466}
7467
Alexey Bataev346265e2015-09-25 10:37:12 +00007468OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
7469 SourceLocation EndLoc) {
7470 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
7471}
7472
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007473OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
7474 SourceLocation EndLoc) {
7475 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
7476}
7477
Alexey Bataevb825de12015-12-07 10:51:44 +00007478OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
7479 SourceLocation EndLoc) {
7480 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
7481}
7482
Alexey Bataevc5e02582014-06-16 07:08:35 +00007483OMPClause *Sema::ActOnOpenMPVarListClause(
7484 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
7485 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
7486 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007487 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00007488 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
7489 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
7490 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007491 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007492 switch (Kind) {
7493 case OMPC_private:
7494 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7495 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007496 case OMPC_firstprivate:
7497 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7498 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007499 case OMPC_lastprivate:
7500 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7501 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007502 case OMPC_shared:
7503 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
7504 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007505 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00007506 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
7507 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007508 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00007509 case OMPC_linear:
7510 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00007511 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00007512 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007513 case OMPC_aligned:
7514 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
7515 ColonLoc, EndLoc);
7516 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007517 case OMPC_copyin:
7518 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
7519 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007520 case OMPC_copyprivate:
7521 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7522 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00007523 case OMPC_flush:
7524 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
7525 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007526 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007527 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
7528 StartLoc, LParenLoc, EndLoc);
7529 break;
7530 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00007531 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
7532 DepLinMapLoc, ColonLoc, VarList, StartLoc,
7533 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007534 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007535 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007536 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007537 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007538 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007539 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007540 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007541 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007542 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007543 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007544 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007545 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007546 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007547 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007548 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007549 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007550 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007551 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007552 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007553 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00007554 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007555 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007556 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007557 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007558 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007559 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007560 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007561 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007562 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007563 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007564 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007565 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007566 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007567 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007568 llvm_unreachable("Clause is not allowed.");
7569 }
7570 return Res;
7571}
7572
Alexey Bataev90c228f2016-02-08 09:29:13 +00007573ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00007574 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00007575 ExprResult Res = BuildDeclRefExpr(
7576 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
7577 if (!Res.isUsable())
7578 return ExprError();
7579 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
7580 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
7581 if (!Res.isUsable())
7582 return ExprError();
7583 }
7584 if (VK != VK_LValue && Res.get()->isGLValue()) {
7585 Res = DefaultLvalueConversion(Res.get());
7586 if (!Res.isUsable())
7587 return ExprError();
7588 }
7589 return Res;
7590}
7591
Alexey Bataev60da77e2016-02-29 05:54:20 +00007592static std::pair<ValueDecl *, bool>
7593getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
7594 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007595 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7596 RefExpr->containsUnexpandedParameterPack())
7597 return std::make_pair(nullptr, true);
7598
Alexey Bataevd985eda2016-02-10 11:29:16 +00007599 // OpenMP [3.1, C/C++]
7600 // A list item is a variable name.
7601 // OpenMP [2.9.3.3, Restrictions, p.1]
7602 // A variable that is part of another variable (as an array or
7603 // structure element) cannot appear in a private clause.
7604 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007605 enum {
7606 NoArrayExpr = -1,
7607 ArraySubscript = 0,
7608 OMPArraySection = 1
7609 } IsArrayExpr = NoArrayExpr;
7610 if (AllowArraySection) {
7611 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
7612 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7613 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7614 Base = TempASE->getBase()->IgnoreParenImpCasts();
7615 RefExpr = Base;
7616 IsArrayExpr = ArraySubscript;
7617 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
7618 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7619 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7620 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7621 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7622 Base = TempASE->getBase()->IgnoreParenImpCasts();
7623 RefExpr = Base;
7624 IsArrayExpr = OMPArraySection;
7625 }
7626 }
7627 ELoc = RefExpr->getExprLoc();
7628 ERange = RefExpr->getSourceRange();
7629 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007630 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7631 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
7632 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
7633 (S.getCurrentThisType().isNull() || !ME ||
7634 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
7635 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00007636 if (IsArrayExpr != NoArrayExpr)
7637 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
7638 << ERange;
7639 else {
7640 S.Diag(ELoc,
7641 AllowArraySection
7642 ? diag::err_omp_expected_var_name_member_expr_or_array_item
7643 : diag::err_omp_expected_var_name_member_expr)
7644 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
7645 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007646 return std::make_pair(nullptr, false);
7647 }
7648 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
7649}
7650
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007651OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
7652 SourceLocation StartLoc,
7653 SourceLocation LParenLoc,
7654 SourceLocation EndLoc) {
7655 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00007656 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00007657 for (auto &RefExpr : VarList) {
7658 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007659 SourceLocation ELoc;
7660 SourceRange ERange;
7661 Expr *SimpleRefExpr = RefExpr;
7662 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007663 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007664 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007665 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007666 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007667 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007668 ValueDecl *D = Res.first;
7669 if (!D)
7670 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007671
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007672 QualType Type = D->getType();
7673 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007674
7675 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7676 // A variable that appears in a private clause must not have an incomplete
7677 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007678 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007679 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007680 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007681
Alexey Bataev758e55e2013-09-06 18:03:48 +00007682 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7683 // in a Construct]
7684 // Variables with the predetermined data-sharing attributes may not be
7685 // listed in data-sharing attributes clauses, except for the cases
7686 // listed below. For these exceptions only, listing a predetermined
7687 // variable in a data-sharing attribute clause is allowed and overrides
7688 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007689 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007690 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007691 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7692 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007693 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007694 continue;
7695 }
7696
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007697 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007698 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00007699 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007700 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7701 << getOpenMPClauseName(OMPC_private) << Type
7702 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7703 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007704 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007705 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007706 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007707 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007708 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007709 continue;
7710 }
7711
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007712 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7713 // A list item cannot appear in both a map clause and a data-sharing
7714 // attribute clause on the same construct
7715 if (DSAStack->getCurrentDirective() == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00007716 if (DSAStack->checkMappableExprComponentListsForDecl(
7717 VD, /* CurrentRegionOnly = */ true,
7718 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
7719 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007720 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7721 << getOpenMPClauseName(OMPC_private)
7722 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7723 ReportOriginalDSA(*this, DSAStack, D, DVar);
7724 continue;
7725 }
7726 }
7727
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007728 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
7729 // A variable of class type (or array thereof) that appears in a private
7730 // clause requires an accessible, unambiguous default constructor for the
7731 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00007732 // Generate helper private variable and initialize it with the default
7733 // value. The address of the original variable is replaced by the address of
7734 // the new private variable in CodeGen. This new variable is not added to
7735 // IdResolver, so the code in the OpenMP region uses original variable for
7736 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007737 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007738 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7739 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007740 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007741 if (VDPrivate->isInvalidDecl())
7742 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007743 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007744 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007745
Alexey Bataev90c228f2016-02-08 09:29:13 +00007746 DeclRefExpr *Ref = nullptr;
7747 if (!VD)
Alexey Bataev61205072016-03-02 04:57:40 +00007748 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00007749 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
7750 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007751 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007752 }
7753
Alexey Bataeved09d242014-05-28 05:53:51 +00007754 if (Vars.empty())
7755 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007756
Alexey Bataev03b340a2014-10-21 03:16:40 +00007757 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7758 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007759}
7760
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007761namespace {
7762class DiagsUninitializedSeveretyRAII {
7763private:
7764 DiagnosticsEngine &Diags;
7765 SourceLocation SavedLoc;
7766 bool IsIgnored;
7767
7768public:
7769 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
7770 bool IsIgnored)
7771 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
7772 if (!IsIgnored) {
7773 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
7774 /*Map*/ diag::Severity::Ignored, Loc);
7775 }
7776 }
7777 ~DiagsUninitializedSeveretyRAII() {
7778 if (!IsIgnored)
7779 Diags.popMappings(SavedLoc);
7780 }
7781};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007782}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007783
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007784OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
7785 SourceLocation StartLoc,
7786 SourceLocation LParenLoc,
7787 SourceLocation EndLoc) {
7788 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007789 SmallVector<Expr *, 8> PrivateCopies;
7790 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00007791 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007792 bool IsImplicitClause =
7793 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
7794 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
7795
Alexey Bataeved09d242014-05-28 05:53:51 +00007796 for (auto &RefExpr : VarList) {
7797 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007798 SourceLocation ELoc;
7799 SourceRange ERange;
7800 Expr *SimpleRefExpr = RefExpr;
7801 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007802 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007803 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007804 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007805 PrivateCopies.push_back(nullptr);
7806 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007807 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007808 ValueDecl *D = Res.first;
7809 if (!D)
7810 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007811
Alexey Bataev60da77e2016-02-29 05:54:20 +00007812 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007813 QualType Type = D->getType();
7814 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007815
7816 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7817 // A variable that appears in a private clause must not have an incomplete
7818 // type or a reference type.
7819 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00007820 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007821 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007822 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007823
7824 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
7825 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00007826 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007827 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007828 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007829
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007830 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00007831 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007832 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007833 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00007834 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007835 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007836 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
7837 // A list item that specifies a given variable may not appear in more
7838 // than one clause on the same directive, except that a variable may be
7839 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007840 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007841 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007842 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007843 << getOpenMPClauseName(DVar.CKind)
7844 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007845 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007846 continue;
7847 }
7848
7849 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7850 // in a Construct]
7851 // Variables with the predetermined data-sharing attributes may not be
7852 // listed in data-sharing attributes clauses, except for the cases
7853 // listed below. For these exceptions only, listing a predetermined
7854 // variable in a data-sharing attribute clause is allowed and overrides
7855 // the variable's predetermined data-sharing attributes.
7856 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7857 // in a Construct, C/C++, p.2]
7858 // Variables with const-qualified type having no mutable member may be
7859 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00007860 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007861 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7862 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007863 << getOpenMPClauseName(DVar.CKind)
7864 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007865 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007866 continue;
7867 }
7868
Alexey Bataevf29276e2014-06-18 04:14:57 +00007869 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007870 // OpenMP [2.9.3.4, Restrictions, p.2]
7871 // A list item that is private within a parallel region must not appear
7872 // in a firstprivate clause on a worksharing construct if any of the
7873 // worksharing regions arising from the worksharing construct ever bind
7874 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007875 if (isOpenMPWorksharingDirective(CurrDir) &&
7876 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007877 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007878 if (DVar.CKind != OMPC_shared &&
7879 (isOpenMPParallelDirective(DVar.DKind) ||
7880 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007881 Diag(ELoc, diag::err_omp_required_access)
7882 << getOpenMPClauseName(OMPC_firstprivate)
7883 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007884 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007885 continue;
7886 }
7887 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007888 // OpenMP [2.9.3.4, Restrictions, p.3]
7889 // A list item that appears in a reduction clause of a parallel construct
7890 // must not appear in a firstprivate clause on a worksharing or task
7891 // construct if any of the worksharing or task regions arising from the
7892 // worksharing or task construct ever bind to any of the parallel regions
7893 // arising from the parallel construct.
7894 // OpenMP [2.9.3.4, Restrictions, p.4]
7895 // A list item that appears in a reduction clause in worksharing
7896 // construct must not appear in a firstprivate clause in a task construct
7897 // encountered during execution of any of the worksharing regions arising
7898 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00007899 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00007900 DVar = DSAStack->hasInnermostDSA(
7901 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
7902 [](OpenMPDirectiveKind K) -> bool {
7903 return isOpenMPParallelDirective(K) ||
7904 isOpenMPWorksharingDirective(K);
7905 },
7906 false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007907 if (DVar.CKind == OMPC_reduction &&
7908 (isOpenMPParallelDirective(DVar.DKind) ||
7909 isOpenMPWorksharingDirective(DVar.DKind))) {
7910 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7911 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007912 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007913 continue;
7914 }
7915 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007916
7917 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7918 // A list item that is private within a teams region must not appear in a
7919 // firstprivate clause on a distribute construct if any of the distribute
7920 // regions arising from the distribute construct ever bind to any of the
7921 // teams regions arising from the teams construct.
7922 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7923 // A list item that appears in a reduction clause of a teams construct
7924 // must not appear in a firstprivate clause on a distribute construct if
7925 // any of the distribute regions arising from the distribute construct
7926 // ever bind to any of the teams regions arising from the teams construct.
7927 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7928 // A list item may appear in a firstprivate or lastprivate clause but not
7929 // both.
7930 if (CurrDir == OMPD_distribute) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00007931 DVar = DSAStack->hasInnermostDSA(
7932 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_private; },
7933 [](OpenMPDirectiveKind K) -> bool {
7934 return isOpenMPTeamsDirective(K);
7935 },
7936 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007937 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7938 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007939 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007940 continue;
7941 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00007942 DVar = DSAStack->hasInnermostDSA(
7943 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
7944 [](OpenMPDirectiveKind K) -> bool {
7945 return isOpenMPTeamsDirective(K);
7946 },
7947 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007948 if (DVar.CKind == OMPC_reduction &&
7949 isOpenMPTeamsDirective(DVar.DKind)) {
7950 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007951 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007952 continue;
7953 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007954 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007955 if (DVar.CKind == OMPC_lastprivate) {
7956 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007957 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007958 continue;
7959 }
7960 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007961 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7962 // A list item cannot appear in both a map clause and a data-sharing
7963 // attribute clause on the same construct
7964 if (CurrDir == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00007965 if (DSAStack->checkMappableExprComponentListsForDecl(
7966 VD, /* CurrentRegionOnly = */ true,
7967 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
7968 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007969 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7970 << getOpenMPClauseName(OMPC_firstprivate)
7971 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7972 ReportOriginalDSA(*this, DSAStack, D, DVar);
7973 continue;
7974 }
7975 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007976 }
7977
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007978 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007979 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00007980 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007981 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7982 << getOpenMPClauseName(OMPC_firstprivate) << Type
7983 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7984 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007985 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007986 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007987 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007988 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00007989 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007990 continue;
7991 }
7992
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007993 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007994 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7995 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007996 // Generate helper private variable and initialize it with the value of the
7997 // original variable. The address of the original variable is replaced by
7998 // the address of the new private variable in the CodeGen. This new variable
7999 // is not added to IdResolver, so the code in the OpenMP region uses
8000 // original variable for proper diagnostics and variable capturing.
8001 Expr *VDInitRefExpr = nullptr;
8002 // For arrays generate initializer for single element and replace it by the
8003 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008004 if (Type->isArrayType()) {
8005 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008006 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008007 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008008 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008009 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008010 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008011 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00008012 InitializedEntity Entity =
8013 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008014 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
8015
8016 InitializationSequence InitSeq(*this, Entity, Kind, Init);
8017 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
8018 if (Result.isInvalid())
8019 VDPrivate->setInvalidDecl();
8020 else
8021 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008022 // Remove temp variable declaration.
8023 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008024 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008025 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
8026 ".firstprivate.temp");
8027 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
8028 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00008029 AddInitializerToDecl(VDPrivate,
8030 DefaultLvalueConversion(VDInitRefExpr).get(),
8031 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008032 }
8033 if (VDPrivate->isInvalidDecl()) {
8034 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008035 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008036 diag::note_omp_task_predetermined_firstprivate_here);
8037 }
8038 continue;
8039 }
8040 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008041 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00008042 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
8043 RefExpr->getExprLoc());
8044 DeclRefExpr *Ref = nullptr;
Alexey Bataev417089f2016-02-17 13:19:37 +00008045 if (!VD) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008046 if (TopDVar.CKind == OMPC_lastprivate)
8047 Ref = TopDVar.PrivateCopy;
8048 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008049 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00008050 if (!IsOpenMPCapturedDecl(D))
8051 ExprCaptures.push_back(Ref->getDecl());
8052 }
Alexey Bataev417089f2016-02-17 13:19:37 +00008053 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008054 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
8055 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008056 PrivateCopies.push_back(VDPrivateRefExpr);
8057 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008058 }
8059
Alexey Bataeved09d242014-05-28 05:53:51 +00008060 if (Vars.empty())
8061 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008062
8063 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008064 Vars, PrivateCopies, Inits,
8065 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008066}
8067
Alexander Musman1bb328c2014-06-04 13:06:39 +00008068OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
8069 SourceLocation StartLoc,
8070 SourceLocation LParenLoc,
8071 SourceLocation EndLoc) {
8072 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00008073 SmallVector<Expr *, 8> SrcExprs;
8074 SmallVector<Expr *, 8> DstExprs;
8075 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00008076 SmallVector<Decl *, 4> ExprCaptures;
8077 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008078 for (auto &RefExpr : VarList) {
8079 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008080 SourceLocation ELoc;
8081 SourceRange ERange;
8082 Expr *SimpleRefExpr = RefExpr;
8083 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008084 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00008085 // It will be analyzed later.
8086 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00008087 SrcExprs.push_back(nullptr);
8088 DstExprs.push_back(nullptr);
8089 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008090 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008091 ValueDecl *D = Res.first;
8092 if (!D)
8093 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008094
Alexey Bataev74caaf22016-02-20 04:09:36 +00008095 QualType Type = D->getType();
8096 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008097
8098 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
8099 // A variable that appears in a lastprivate clause must not have an
8100 // incomplete type or a reference type.
8101 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00008102 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00008103 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008104 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00008105
8106 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8107 // in a Construct]
8108 // Variables with the predetermined data-sharing attributes may not be
8109 // listed in data-sharing attributes clauses, except for the cases
8110 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008111 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008112 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
8113 DVar.CKind != OMPC_firstprivate &&
8114 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
8115 Diag(ELoc, diag::err_omp_wrong_dsa)
8116 << getOpenMPClauseName(DVar.CKind)
8117 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008118 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008119 continue;
8120 }
8121
Alexey Bataevf29276e2014-06-18 04:14:57 +00008122 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8123 // OpenMP [2.14.3.5, Restrictions, p.2]
8124 // A list item that is private within a parallel region, or that appears in
8125 // the reduction clause of a parallel construct, must not appear in a
8126 // lastprivate clause on a worksharing construct if any of the corresponding
8127 // worksharing regions ever binds to any of the corresponding parallel
8128 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00008129 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00008130 if (isOpenMPWorksharingDirective(CurrDir) &&
8131 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00008132 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008133 if (DVar.CKind != OMPC_shared) {
8134 Diag(ELoc, diag::err_omp_required_access)
8135 << getOpenMPClauseName(OMPC_lastprivate)
8136 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008137 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008138 continue;
8139 }
8140 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008141
8142 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8143 // A list item may appear in a firstprivate or lastprivate clause but not
8144 // both.
8145 if (CurrDir == OMPD_distribute) {
8146 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8147 if (DVar.CKind == OMPC_firstprivate) {
8148 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
8149 ReportOriginalDSA(*this, DSAStack, D, DVar);
8150 continue;
8151 }
8152 }
8153
Alexander Musman1bb328c2014-06-04 13:06:39 +00008154 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00008155 // A variable of class type (or array thereof) that appears in a
8156 // lastprivate clause requires an accessible, unambiguous default
8157 // constructor for the class type, unless the list item is also specified
8158 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00008159 // A variable of class type (or array thereof) that appears in a
8160 // lastprivate clause requires an accessible, unambiguous copy assignment
8161 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00008162 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008163 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008164 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008165 D->hasAttrs() ? &D->getAttrs() : nullptr);
8166 auto *PseudoSrcExpr =
8167 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008168 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008169 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008170 D->hasAttrs() ? &D->getAttrs() : nullptr);
8171 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008172 // For arrays generate assignment operation for single element and replace
8173 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008174 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00008175 PseudoDstExpr, PseudoSrcExpr);
8176 if (AssignmentOp.isInvalid())
8177 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00008178 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00008179 /*DiscardedValue=*/true);
8180 if (AssignmentOp.isInvalid())
8181 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008182
Alexey Bataev74caaf22016-02-20 04:09:36 +00008183 DeclRefExpr *Ref = nullptr;
Alexey Bataev005248a2016-02-25 05:25:57 +00008184 if (!VD) {
8185 if (TopDVar.CKind == OMPC_firstprivate)
8186 Ref = TopDVar.PrivateCopy;
8187 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008188 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008189 if (!IsOpenMPCapturedDecl(D))
8190 ExprCaptures.push_back(Ref->getDecl());
8191 }
8192 if (TopDVar.CKind == OMPC_firstprivate ||
8193 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008194 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008195 ExprResult RefRes = DefaultLvalueConversion(Ref);
8196 if (!RefRes.isUsable())
8197 continue;
8198 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008199 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
8200 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008201 if (!PostUpdateRes.isUsable())
8202 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008203 ExprPostUpdates.push_back(
8204 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008205 }
8206 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008207 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008208 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00008209 SrcExprs.push_back(PseudoSrcExpr);
8210 DstExprs.push_back(PseudoDstExpr);
8211 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00008212 }
8213
8214 if (Vars.empty())
8215 return nullptr;
8216
8217 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00008218 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008219 buildPreInits(Context, ExprCaptures),
8220 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00008221}
8222
Alexey Bataev758e55e2013-09-06 18:03:48 +00008223OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
8224 SourceLocation StartLoc,
8225 SourceLocation LParenLoc,
8226 SourceLocation EndLoc) {
8227 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00008228 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008229 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008230 SourceLocation ELoc;
8231 SourceRange ERange;
8232 Expr *SimpleRefExpr = RefExpr;
8233 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008234 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00008235 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008236 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008237 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008238 ValueDecl *D = Res.first;
8239 if (!D)
8240 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008241
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008242 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008243 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8244 // in a Construct]
8245 // Variables with the predetermined data-sharing attributes may not be
8246 // listed in data-sharing attributes clauses, except for the cases
8247 // listed below. For these exceptions only, listing a predetermined
8248 // variable in a data-sharing attribute clause is allowed and overrides
8249 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008250 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00008251 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
8252 DVar.RefExpr) {
8253 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8254 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008255 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008256 continue;
8257 }
8258
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008259 DeclRefExpr *Ref = nullptr;
Alexey Bataev1efd1662016-03-29 10:59:56 +00008260 if (!VD && IsOpenMPCapturedDecl(D))
Alexey Bataev61205072016-03-02 04:57:40 +00008261 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008262 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataev1efd1662016-03-29 10:59:56 +00008263 Vars.push_back((VD || !Ref) ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008264 }
8265
Alexey Bataeved09d242014-05-28 05:53:51 +00008266 if (Vars.empty())
8267 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008268
8269 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
8270}
8271
Alexey Bataevc5e02582014-06-16 07:08:35 +00008272namespace {
8273class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
8274 DSAStackTy *Stack;
8275
8276public:
8277 bool VisitDeclRefExpr(DeclRefExpr *E) {
8278 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008279 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008280 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
8281 return false;
8282 if (DVar.CKind != OMPC_unknown)
8283 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008284 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
8285 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
8286 false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008287 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008288 return true;
8289 return false;
8290 }
8291 return false;
8292 }
8293 bool VisitStmt(Stmt *S) {
8294 for (auto Child : S->children()) {
8295 if (Child && Visit(Child))
8296 return true;
8297 }
8298 return false;
8299 }
Alexey Bataev23b69422014-06-18 07:08:49 +00008300 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00008301};
Alexey Bataev23b69422014-06-18 07:08:49 +00008302} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00008303
Alexey Bataev60da77e2016-02-29 05:54:20 +00008304namespace {
8305// Transform MemberExpression for specified FieldDecl of current class to
8306// DeclRefExpr to specified OMPCapturedExprDecl.
8307class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
8308 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
8309 ValueDecl *Field;
8310 DeclRefExpr *CapturedExpr;
8311
8312public:
8313 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
8314 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
8315
8316 ExprResult TransformMemberExpr(MemberExpr *E) {
8317 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
8318 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00008319 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008320 return CapturedExpr;
8321 }
8322 return BaseTransform::TransformMemberExpr(E);
8323 }
8324 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
8325};
8326} // namespace
8327
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008328template <typename T>
8329static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
8330 const llvm::function_ref<T(ValueDecl *)> &Gen) {
8331 for (auto &Set : Lookups) {
8332 for (auto *D : Set) {
8333 if (auto Res = Gen(cast<ValueDecl>(D)))
8334 return Res;
8335 }
8336 }
8337 return T();
8338}
8339
8340static ExprResult
8341buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
8342 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
8343 const DeclarationNameInfo &ReductionId, QualType Ty,
8344 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
8345 if (ReductionIdScopeSpec.isInvalid())
8346 return ExprError();
8347 SmallVector<UnresolvedSet<8>, 4> Lookups;
8348 if (S) {
8349 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
8350 Lookup.suppressDiagnostics();
8351 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
8352 auto *D = Lookup.getRepresentativeDecl();
8353 do {
8354 S = S->getParent();
8355 } while (S && !S->isDeclScope(D));
8356 if (S)
8357 S = S->getParent();
8358 Lookups.push_back(UnresolvedSet<8>());
8359 Lookups.back().append(Lookup.begin(), Lookup.end());
8360 Lookup.clear();
8361 }
8362 } else if (auto *ULE =
8363 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
8364 Lookups.push_back(UnresolvedSet<8>());
8365 Decl *PrevD = nullptr;
8366 for(auto *D : ULE->decls()) {
8367 if (D == PrevD)
8368 Lookups.push_back(UnresolvedSet<8>());
8369 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
8370 Lookups.back().addDecl(DRD);
8371 PrevD = D;
8372 }
8373 }
8374 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
8375 Ty->containsUnexpandedParameterPack() ||
8376 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
8377 return !D->isInvalidDecl() &&
8378 (D->getType()->isDependentType() ||
8379 D->getType()->isInstantiationDependentType() ||
8380 D->getType()->containsUnexpandedParameterPack());
8381 })) {
8382 UnresolvedSet<8> ResSet;
8383 for (auto &Set : Lookups) {
8384 ResSet.append(Set.begin(), Set.end());
8385 // The last item marks the end of all declarations at the specified scope.
8386 ResSet.addDecl(Set[Set.size() - 1]);
8387 }
8388 return UnresolvedLookupExpr::Create(
8389 SemaRef.Context, /*NamingClass=*/nullptr,
8390 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
8391 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
8392 }
8393 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8394 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
8395 if (!D->isInvalidDecl() &&
8396 SemaRef.Context.hasSameType(D->getType(), Ty))
8397 return D;
8398 return nullptr;
8399 }))
8400 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8401 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8402 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
8403 if (!D->isInvalidDecl() &&
8404 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
8405 !Ty.isMoreQualifiedThan(D->getType()))
8406 return D;
8407 return nullptr;
8408 })) {
8409 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
8410 /*DetectVirtual=*/false);
8411 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
8412 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
8413 VD->getType().getUnqualifiedType()))) {
8414 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
8415 /*DiagID=*/0) !=
8416 Sema::AR_inaccessible) {
8417 SemaRef.BuildBasePathArray(Paths, BasePath);
8418 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8419 }
8420 }
8421 }
8422 }
8423 if (ReductionIdScopeSpec.isSet()) {
8424 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
8425 return ExprError();
8426 }
8427 return ExprEmpty();
8428}
8429
Alexey Bataevc5e02582014-06-16 07:08:35 +00008430OMPClause *Sema::ActOnOpenMPReductionClause(
8431 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
8432 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008433 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
8434 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00008435 auto DN = ReductionId.getName();
8436 auto OOK = DN.getCXXOverloadedOperator();
8437 BinaryOperatorKind BOK = BO_Comma;
8438
8439 // OpenMP [2.14.3.6, reduction clause]
8440 // C
8441 // reduction-identifier is either an identifier or one of the following
8442 // operators: +, -, *, &, |, ^, && and ||
8443 // C++
8444 // reduction-identifier is either an id-expression or one of the following
8445 // operators: +, -, *, &, |, ^, && and ||
8446 // FIXME: Only 'min' and 'max' identifiers are supported for now.
8447 switch (OOK) {
8448 case OO_Plus:
8449 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008450 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008451 break;
8452 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008453 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008454 break;
8455 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008456 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008457 break;
8458 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008459 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008460 break;
8461 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008462 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008463 break;
8464 case OO_AmpAmp:
8465 BOK = BO_LAnd;
8466 break;
8467 case OO_PipePipe:
8468 BOK = BO_LOr;
8469 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008470 case OO_New:
8471 case OO_Delete:
8472 case OO_Array_New:
8473 case OO_Array_Delete:
8474 case OO_Slash:
8475 case OO_Percent:
8476 case OO_Tilde:
8477 case OO_Exclaim:
8478 case OO_Equal:
8479 case OO_Less:
8480 case OO_Greater:
8481 case OO_LessEqual:
8482 case OO_GreaterEqual:
8483 case OO_PlusEqual:
8484 case OO_MinusEqual:
8485 case OO_StarEqual:
8486 case OO_SlashEqual:
8487 case OO_PercentEqual:
8488 case OO_CaretEqual:
8489 case OO_AmpEqual:
8490 case OO_PipeEqual:
8491 case OO_LessLess:
8492 case OO_GreaterGreater:
8493 case OO_LessLessEqual:
8494 case OO_GreaterGreaterEqual:
8495 case OO_EqualEqual:
8496 case OO_ExclaimEqual:
8497 case OO_PlusPlus:
8498 case OO_MinusMinus:
8499 case OO_Comma:
8500 case OO_ArrowStar:
8501 case OO_Arrow:
8502 case OO_Call:
8503 case OO_Subscript:
8504 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00008505 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008506 case NUM_OVERLOADED_OPERATORS:
8507 llvm_unreachable("Unexpected reduction identifier");
8508 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008509 if (auto II = DN.getAsIdentifierInfo()) {
8510 if (II->isStr("max"))
8511 BOK = BO_GT;
8512 else if (II->isStr("min"))
8513 BOK = BO_LT;
8514 }
8515 break;
8516 }
8517 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008518 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00008519 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008520 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008521
8522 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008523 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008524 SmallVector<Expr *, 8> LHSs;
8525 SmallVector<Expr *, 8> RHSs;
8526 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00008527 SmallVector<Decl *, 4> ExprCaptures;
8528 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008529 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
8530 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008531 for (auto RefExpr : VarList) {
8532 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00008533 // OpenMP [2.1, C/C++]
8534 // A list item is a variable or array section, subject to the restrictions
8535 // specified in Section 2.4 on page 42 and in each of the sections
8536 // describing clauses and directives for which a list appears.
8537 // OpenMP [2.14.3.3, Restrictions, p.1]
8538 // A variable that is part of another variable (as an array or
8539 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008540 if (!FirstIter && IR != ER)
8541 ++IR;
8542 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008543 SourceLocation ELoc;
8544 SourceRange ERange;
8545 Expr *SimpleRefExpr = RefExpr;
8546 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8547 /*AllowArraySection=*/true);
8548 if (Res.second) {
8549 // It will be analyzed later.
8550 Vars.push_back(RefExpr);
8551 Privates.push_back(nullptr);
8552 LHSs.push_back(nullptr);
8553 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008554 // Try to find 'declare reduction' corresponding construct before using
8555 // builtin/overloaded operators.
8556 QualType Type = Context.DependentTy;
8557 CXXCastPath BasePath;
8558 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8559 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8560 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8561 if (CurContext->isDependentContext() &&
8562 (DeclareReductionRef.isUnset() ||
8563 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
8564 ReductionOps.push_back(DeclareReductionRef.get());
8565 else
8566 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008567 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008568 ValueDecl *D = Res.first;
8569 if (!D)
8570 continue;
8571
Alexey Bataeva1764212015-09-30 09:22:36 +00008572 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008573 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
8574 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
8575 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00008576 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008577 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008578 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
8579 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
8580 Type = ATy->getElementType();
8581 else
8582 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008583 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008584 } else
8585 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
8586 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00008587
Alexey Bataevc5e02582014-06-16 07:08:35 +00008588 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8589 // A variable that appears in a private clause must not have an incomplete
8590 // type or a reference type.
8591 if (RequireCompleteType(ELoc, Type,
8592 diag::err_omp_reduction_incomplete_type))
8593 continue;
8594 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00008595 // A list item that appears in a reduction clause must not be
8596 // const-qualified.
8597 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008598 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008599 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008600 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008601 bool IsDecl = !VD ||
8602 VD->isThisDeclarationADefinition(Context) ==
8603 VarDecl::DeclarationOnly;
8604 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00008605 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00008606 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00008607 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008608 continue;
8609 }
8610 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
8611 // If a list-item is a reference type then it must bind to the same object
8612 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008613 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008614 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008615 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008616 DSARefChecker Check(DSAStack);
8617 if (Check.Visit(VDDef->getInit())) {
8618 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
8619 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
8620 continue;
8621 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008622 }
8623 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008624
Alexey Bataevc5e02582014-06-16 07:08:35 +00008625 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8626 // in a Construct]
8627 // Variables with the predetermined data-sharing attributes may not be
8628 // listed in data-sharing attributes clauses, except for the cases
8629 // listed below. For these exceptions only, listing a predetermined
8630 // variable in a data-sharing attribute clause is allowed and overrides
8631 // the variable's predetermined data-sharing attributes.
8632 // OpenMP [2.14.3.6, Restrictions, p.3]
8633 // Any number of reduction clauses can be specified on the directive,
8634 // but a list item can appear only once in the reduction clauses for that
8635 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00008636 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008637 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008638 if (DVar.CKind == OMPC_reduction) {
8639 Diag(ELoc, diag::err_omp_once_referenced)
8640 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008641 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008642 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008643 } else if (DVar.CKind != OMPC_unknown) {
8644 Diag(ELoc, diag::err_omp_wrong_dsa)
8645 << getOpenMPClauseName(DVar.CKind)
8646 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008647 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008648 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008649 }
8650
8651 // OpenMP [2.14.3.6, Restrictions, p.1]
8652 // A list item that appears in a reduction clause of a worksharing
8653 // construct must be shared in the parallel regions to which any of the
8654 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008655 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8656 if (isOpenMPWorksharingDirective(CurrDir) &&
8657 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008658 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008659 if (DVar.CKind != OMPC_shared) {
8660 Diag(ELoc, diag::err_omp_required_access)
8661 << getOpenMPClauseName(OMPC_reduction)
8662 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008663 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008664 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00008665 }
8666 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008667
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008668 // Try to find 'declare reduction' corresponding construct before using
8669 // builtin/overloaded operators.
8670 CXXCastPath BasePath;
8671 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8672 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8673 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8674 if (DeclareReductionRef.isInvalid())
8675 continue;
8676 if (CurContext->isDependentContext() &&
8677 (DeclareReductionRef.isUnset() ||
8678 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
8679 Vars.push_back(RefExpr);
8680 Privates.push_back(nullptr);
8681 LHSs.push_back(nullptr);
8682 RHSs.push_back(nullptr);
8683 ReductionOps.push_back(DeclareReductionRef.get());
8684 continue;
8685 }
8686 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
8687 // Not allowed reduction identifier is found.
8688 Diag(ReductionId.getLocStart(),
8689 diag::err_omp_unknown_reduction_identifier)
8690 << Type << ReductionIdRange;
8691 continue;
8692 }
8693
8694 // OpenMP [2.14.3.6, reduction clause, Restrictions]
8695 // The type of a list item that appears in a reduction clause must be valid
8696 // for the reduction-identifier. For a max or min reduction in C, the type
8697 // of the list item must be an allowed arithmetic data type: char, int,
8698 // float, double, or _Bool, possibly modified with long, short, signed, or
8699 // unsigned. For a max or min reduction in C++, the type of the list item
8700 // must be an allowed arithmetic data type: char, wchar_t, int, float,
8701 // double, or bool, possibly modified with long, short, signed, or unsigned.
8702 if (DeclareReductionRef.isUnset()) {
8703 if ((BOK == BO_GT || BOK == BO_LT) &&
8704 !(Type->isScalarType() ||
8705 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
8706 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
8707 << getLangOpts().CPlusPlus;
8708 if (!ASE && !OASE) {
8709 bool IsDecl = !VD ||
8710 VD->isThisDeclarationADefinition(Context) ==
8711 VarDecl::DeclarationOnly;
8712 Diag(D->getLocation(),
8713 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8714 << D;
8715 }
8716 continue;
8717 }
8718 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
8719 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
8720 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
8721 if (!ASE && !OASE) {
8722 bool IsDecl = !VD ||
8723 VD->isThisDeclarationADefinition(Context) ==
8724 VarDecl::DeclarationOnly;
8725 Diag(D->getLocation(),
8726 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8727 << D;
8728 }
8729 continue;
8730 }
8731 }
8732
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008733 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008734 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00008735 D->hasAttrs() ? &D->getAttrs() : nullptr);
8736 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
8737 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008738 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00008739 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00008740 (!ASE &&
8741 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00008742 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008743 // Create pseudo array type for private copy. The size for this array will
8744 // be generated during codegen.
8745 // For array subscripts or single variables Private Ty is the same as Type
8746 // (type of the variable or single array element).
8747 PrivateTy = Context.getVariableArrayType(
8748 Type, new (Context) OpaqueValueExpr(SourceLocation(),
8749 Context.getSizeType(), VK_RValue),
8750 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00008751 } else if (!ASE && !OASE &&
8752 Context.getAsArrayType(D->getType().getNonReferenceType()))
8753 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008754 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008755 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
8756 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008757 // Add initializer for private variable.
8758 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008759 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
8760 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
8761 if (DeclareReductionRef.isUsable()) {
8762 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
8763 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
8764 if (DRD->getInitializer()) {
8765 Init = DRDRef;
8766 RHSVD->setInit(DRDRef);
8767 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008768 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008769 } else {
8770 switch (BOK) {
8771 case BO_Add:
8772 case BO_Xor:
8773 case BO_Or:
8774 case BO_LOr:
8775 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
8776 if (Type->isScalarType() || Type->isAnyComplexType())
8777 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
8778 break;
8779 case BO_Mul:
8780 case BO_LAnd:
8781 if (Type->isScalarType() || Type->isAnyComplexType()) {
8782 // '*' and '&&' reduction ops - initializer is '1'.
8783 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00008784 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008785 break;
8786 case BO_And: {
8787 // '&' reduction op - initializer is '~0'.
8788 QualType OrigType = Type;
8789 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
8790 Type = ComplexTy->getElementType();
8791 if (Type->isRealFloatingType()) {
8792 llvm::APFloat InitValue =
8793 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
8794 /*isIEEE=*/true);
8795 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8796 Type, ELoc);
8797 } else if (Type->isScalarType()) {
8798 auto Size = Context.getTypeSize(Type);
8799 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
8800 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
8801 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8802 }
8803 if (Init && OrigType->isAnyComplexType()) {
8804 // Init = 0xFFFF + 0xFFFFi;
8805 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
8806 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
8807 }
8808 Type = OrigType;
8809 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008810 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008811 case BO_LT:
8812 case BO_GT: {
8813 // 'min' reduction op - initializer is 'Largest representable number in
8814 // the reduction list item type'.
8815 // 'max' reduction op - initializer is 'Least representable number in
8816 // the reduction list item type'.
8817 if (Type->isIntegerType() || Type->isPointerType()) {
8818 bool IsSigned = Type->hasSignedIntegerRepresentation();
8819 auto Size = Context.getTypeSize(Type);
8820 QualType IntTy =
8821 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
8822 llvm::APInt InitValue =
8823 (BOK != BO_LT)
8824 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
8825 : llvm::APInt::getMinValue(Size)
8826 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
8827 : llvm::APInt::getMaxValue(Size);
8828 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8829 if (Type->isPointerType()) {
8830 // Cast to pointer type.
8831 auto CastExpr = BuildCStyleCastExpr(
8832 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
8833 SourceLocation(), Init);
8834 if (CastExpr.isInvalid())
8835 continue;
8836 Init = CastExpr.get();
8837 }
8838 } else if (Type->isRealFloatingType()) {
8839 llvm::APFloat InitValue = llvm::APFloat::getLargest(
8840 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
8841 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8842 Type, ELoc);
8843 }
8844 break;
8845 }
8846 case BO_PtrMemD:
8847 case BO_PtrMemI:
8848 case BO_MulAssign:
8849 case BO_Div:
8850 case BO_Rem:
8851 case BO_Sub:
8852 case BO_Shl:
8853 case BO_Shr:
8854 case BO_LE:
8855 case BO_GE:
8856 case BO_EQ:
8857 case BO_NE:
8858 case BO_AndAssign:
8859 case BO_XorAssign:
8860 case BO_OrAssign:
8861 case BO_Assign:
8862 case BO_AddAssign:
8863 case BO_SubAssign:
8864 case BO_DivAssign:
8865 case BO_RemAssign:
8866 case BO_ShlAssign:
8867 case BO_ShrAssign:
8868 case BO_Comma:
8869 llvm_unreachable("Unexpected reduction operation");
8870 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008871 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008872 if (Init && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008873 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
8874 /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008875 } else if (!Init)
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008876 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008877 if (RHSVD->isInvalidDecl())
8878 continue;
8879 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008880 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
8881 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008882 bool IsDecl =
8883 !VD ||
8884 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8885 Diag(D->getLocation(),
8886 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8887 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008888 continue;
8889 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008890 // Store initializer for single element in private copy. Will be used during
8891 // codegen.
8892 PrivateVD->setInit(RHSVD->getInit());
8893 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008894 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008895 ExprResult ReductionOp;
8896 if (DeclareReductionRef.isUsable()) {
8897 QualType RedTy = DeclareReductionRef.get()->getType();
8898 QualType PtrRedTy = Context.getPointerType(RedTy);
8899 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
8900 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
8901 if (!BasePath.empty()) {
8902 LHS = DefaultLvalueConversion(LHS.get());
8903 RHS = DefaultLvalueConversion(RHS.get());
8904 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8905 CK_UncheckedDerivedToBase, LHS.get(),
8906 &BasePath, LHS.get()->getValueKind());
8907 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8908 CK_UncheckedDerivedToBase, RHS.get(),
8909 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008910 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008911 FunctionProtoType::ExtProtoInfo EPI;
8912 QualType Params[] = {PtrRedTy, PtrRedTy};
8913 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
8914 auto *OVE = new (Context) OpaqueValueExpr(
8915 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
8916 DefaultLvalueConversion(DeclareReductionRef.get()).get());
8917 Expr *Args[] = {LHS.get(), RHS.get()};
8918 ReductionOp = new (Context)
8919 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
8920 } else {
8921 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
8922 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
8923 if (ReductionOp.isUsable()) {
8924 if (BOK != BO_LT && BOK != BO_GT) {
8925 ReductionOp =
8926 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8927 BO_Assign, LHSDRE, ReductionOp.get());
8928 } else {
8929 auto *ConditionalOp = new (Context) ConditionalOperator(
8930 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
8931 RHSDRE, Type, VK_LValue, OK_Ordinary);
8932 ReductionOp =
8933 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8934 BO_Assign, LHSDRE, ConditionalOp);
8935 }
8936 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
8937 }
8938 if (ReductionOp.isInvalid())
8939 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008940 }
8941
Alexey Bataev60da77e2016-02-29 05:54:20 +00008942 DeclRefExpr *Ref = nullptr;
8943 Expr *VarsExpr = RefExpr->IgnoreParens();
8944 if (!VD) {
8945 if (ASE || OASE) {
8946 TransformExprToCaptures RebuildToCapture(*this, D);
8947 VarsExpr =
8948 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
8949 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00008950 } else {
8951 VarsExpr = Ref =
8952 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +00008953 }
8954 if (!IsOpenMPCapturedDecl(D)) {
8955 ExprCaptures.push_back(Ref->getDecl());
8956 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8957 ExprResult RefRes = DefaultLvalueConversion(Ref);
8958 if (!RefRes.isUsable())
8959 continue;
8960 ExprResult PostUpdateRes =
8961 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8962 SimpleRefExpr, RefRes.get());
8963 if (!PostUpdateRes.isUsable())
8964 continue;
8965 ExprPostUpdates.push_back(
8966 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00008967 }
8968 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008969 }
8970 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
8971 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008972 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008973 LHSs.push_back(LHSDRE);
8974 RHSs.push_back(RHSDRE);
8975 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008976 }
8977
8978 if (Vars.empty())
8979 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00008980
Alexey Bataevc5e02582014-06-16 07:08:35 +00008981 return OMPReductionClause::Create(
8982 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008983 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008984 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
8985 buildPostUpdate(*this, ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +00008986}
8987
Alexey Bataevecba70f2016-04-12 11:02:11 +00008988bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
8989 SourceLocation LinLoc) {
8990 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
8991 LinKind == OMPC_LINEAR_unknown) {
8992 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
8993 return true;
8994 }
8995 return false;
8996}
8997
8998bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
8999 OpenMPLinearClauseKind LinKind,
9000 QualType Type) {
9001 auto *VD = dyn_cast_or_null<VarDecl>(D);
9002 // A variable must not have an incomplete type or a reference type.
9003 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
9004 return true;
9005 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
9006 !Type->isReferenceType()) {
9007 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
9008 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
9009 return true;
9010 }
9011 Type = Type.getNonReferenceType();
9012
9013 // A list item must not be const-qualified.
9014 if (Type.isConstant(Context)) {
9015 Diag(ELoc, diag::err_omp_const_variable)
9016 << getOpenMPClauseName(OMPC_linear);
9017 if (D) {
9018 bool IsDecl =
9019 !VD ||
9020 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9021 Diag(D->getLocation(),
9022 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9023 << D;
9024 }
9025 return true;
9026 }
9027
9028 // A list item must be of integral or pointer type.
9029 Type = Type.getUnqualifiedType().getCanonicalType();
9030 const auto *Ty = Type.getTypePtrOrNull();
9031 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
9032 !Ty->isPointerType())) {
9033 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
9034 if (D) {
9035 bool IsDecl =
9036 !VD ||
9037 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9038 Diag(D->getLocation(),
9039 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9040 << D;
9041 }
9042 return true;
9043 }
9044 return false;
9045}
9046
Alexey Bataev182227b2015-08-20 10:54:39 +00009047OMPClause *Sema::ActOnOpenMPLinearClause(
9048 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
9049 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
9050 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009051 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009052 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00009053 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00009054 SmallVector<Decl *, 4> ExprCaptures;
9055 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009056 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +00009057 LinKind = OMPC_LINEAR_val;
Alexey Bataeved09d242014-05-28 05:53:51 +00009058 for (auto &RefExpr : VarList) {
9059 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009060 SourceLocation ELoc;
9061 SourceRange ERange;
9062 Expr *SimpleRefExpr = RefExpr;
9063 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9064 /*AllowArraySection=*/false);
9065 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009066 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009067 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009068 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00009069 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00009070 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009071 ValueDecl *D = Res.first;
9072 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00009073 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00009074
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009075 QualType Type = D->getType();
9076 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00009077
9078 // OpenMP [2.14.3.7, linear clause]
9079 // A list-item cannot appear in more than one linear clause.
9080 // A list-item that appears in a linear clause cannot appear in any
9081 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009082 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00009083 if (DVar.RefExpr) {
9084 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9085 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009086 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00009087 continue;
9088 }
9089
Alexey Bataevecba70f2016-04-12 11:02:11 +00009090 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +00009091 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009092 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +00009093
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009094 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009095 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
9096 D->hasAttrs() ? &D->getAttrs() : nullptr);
9097 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009098 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009099 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009100 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009101 DeclRefExpr *Ref = nullptr;
Alexey Bataev78849fb2016-03-09 09:49:00 +00009102 if (!VD) {
9103 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
9104 if (!IsOpenMPCapturedDecl(D)) {
9105 ExprCaptures.push_back(Ref->getDecl());
9106 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9107 ExprResult RefRes = DefaultLvalueConversion(Ref);
9108 if (!RefRes.isUsable())
9109 continue;
9110 ExprResult PostUpdateRes =
9111 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9112 SimpleRefExpr, RefRes.get());
9113 if (!PostUpdateRes.isUsable())
9114 continue;
9115 ExprPostUpdates.push_back(
9116 IgnoredValueConversions(PostUpdateRes.get()).get());
9117 }
9118 }
9119 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009120 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009121 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009122 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009123 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009124 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009125 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
9126 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
9127
9128 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
9129 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009130 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00009131 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00009132 }
9133
9134 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009135 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009136
9137 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00009138 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009139 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
9140 !Step->isInstantiationDependent() &&
9141 !Step->containsUnexpandedParameterPack()) {
9142 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00009143 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00009144 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009145 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00009146 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00009147
Alexander Musman3276a272015-03-21 10:12:56 +00009148 // Build var to save the step value.
9149 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009150 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00009151 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009152 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009153 ExprResult CalcStep =
9154 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009155 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00009156
Alexander Musman8dba6642014-04-22 13:09:42 +00009157 // Warn about zero linear step (it would be probably better specified as
9158 // making corresponding variables 'const').
9159 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00009160 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
9161 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00009162 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
9163 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00009164 if (!IsConstant && CalcStep.isUsable()) {
9165 // Calculate the step beforehand instead of doing this on each iteration.
9166 // (This is not used if the number of iterations may be kfold-ed).
9167 CalcStepExpr = CalcStep.get();
9168 }
Alexander Musman8dba6642014-04-22 13:09:42 +00009169 }
9170
Alexey Bataev182227b2015-08-20 10:54:39 +00009171 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
9172 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009173 StepExpr, CalcStepExpr,
9174 buildPreInits(Context, ExprCaptures),
9175 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +00009176}
9177
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009178static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
9179 Expr *NumIterations, Sema &SemaRef,
9180 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +00009181 // Walk the vars and build update/final expressions for the CodeGen.
9182 SmallVector<Expr *, 8> Updates;
9183 SmallVector<Expr *, 8> Finals;
9184 Expr *Step = Clause.getStep();
9185 Expr *CalcStep = Clause.getCalcStep();
9186 // OpenMP [2.14.3.7, linear clause]
9187 // If linear-step is not specified it is assumed to be 1.
9188 if (Step == nullptr)
9189 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009190 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +00009191 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009192 }
Alexander Musman3276a272015-03-21 10:12:56 +00009193 bool HasErrors = false;
9194 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009195 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009196 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00009197 for (auto &RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009198 SourceLocation ELoc;
9199 SourceRange ERange;
9200 Expr *SimpleRefExpr = RefExpr;
9201 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
9202 /*AllowArraySection=*/false);
9203 ValueDecl *D = Res.first;
9204 if (Res.second || !D) {
9205 Updates.push_back(nullptr);
9206 Finals.push_back(nullptr);
9207 HasErrors = true;
9208 continue;
9209 }
9210 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
9211 D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
9212 ->getMemberDecl();
9213 }
9214 auto &&Info = Stack->isLoopControlVariable(D);
Alexander Musman3276a272015-03-21 10:12:56 +00009215 Expr *InitExpr = *CurInit;
9216
9217 // Build privatized reference to the current linear var.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009218 auto DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009219 Expr *CapturedRef;
9220 if (LinKind == OMPC_LINEAR_uval)
9221 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
9222 else
9223 CapturedRef =
9224 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
9225 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
9226 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009227
9228 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009229 ExprResult Update;
9230 if (!Info.first) {
9231 Update =
9232 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
9233 InitExpr, IV, Step, /* Subtract */ false);
9234 } else
9235 Update = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009236 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
9237 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009238
9239 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009240 ExprResult Final;
9241 if (!Info.first) {
9242 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
9243 InitExpr, NumIterations, Step,
9244 /* Subtract */ false);
9245 } else
9246 Final = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009247 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
9248 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009249
Alexander Musman3276a272015-03-21 10:12:56 +00009250 if (!Update.isUsable() || !Final.isUsable()) {
9251 Updates.push_back(nullptr);
9252 Finals.push_back(nullptr);
9253 HasErrors = true;
9254 } else {
9255 Updates.push_back(Update.get());
9256 Finals.push_back(Final.get());
9257 }
Richard Trieucc3949d2016-02-18 22:34:54 +00009258 ++CurInit;
9259 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00009260 }
9261 Clause.setUpdates(Updates);
9262 Clause.setFinals(Finals);
9263 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00009264}
9265
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009266OMPClause *Sema::ActOnOpenMPAlignedClause(
9267 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
9268 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9269
9270 SmallVector<Expr *, 8> Vars;
9271 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +00009272 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9273 SourceLocation ELoc;
9274 SourceRange ERange;
9275 Expr *SimpleRefExpr = RefExpr;
9276 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9277 /*AllowArraySection=*/false);
9278 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009279 // It will be analyzed later.
9280 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009281 }
Alexey Bataev1efd1662016-03-29 10:59:56 +00009282 ValueDecl *D = Res.first;
9283 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009284 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009285
Alexey Bataev1efd1662016-03-29 10:59:56 +00009286 QualType QType = D->getType();
9287 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009288
9289 // OpenMP [2.8.1, simd construct, Restrictions]
9290 // The type of list items appearing in the aligned clause must be
9291 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009292 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009293 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +00009294 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009295 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009296 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009297 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +00009298 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009299 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +00009300 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009301 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009302 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009303 continue;
9304 }
9305
9306 // OpenMP [2.8.1, simd construct, Restrictions]
9307 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +00009308 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00009309 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009310 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
9311 << getOpenMPClauseName(OMPC_aligned);
9312 continue;
9313 }
9314
Alexey Bataev1efd1662016-03-29 10:59:56 +00009315 DeclRefExpr *Ref = nullptr;
9316 if (!VD && IsOpenMPCapturedDecl(D))
9317 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9318 Vars.push_back(DefaultFunctionArrayConversion(
9319 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
9320 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009321 }
9322
9323 // OpenMP [2.8.1, simd construct, Description]
9324 // The parameter of the aligned clause, alignment, must be a constant
9325 // positive integer expression.
9326 // If no optional parameter is specified, implementation-defined default
9327 // alignments for SIMD instructions on the target platforms are assumed.
9328 if (Alignment != nullptr) {
9329 ExprResult AlignResult =
9330 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
9331 if (AlignResult.isInvalid())
9332 return nullptr;
9333 Alignment = AlignResult.get();
9334 }
9335 if (Vars.empty())
9336 return nullptr;
9337
9338 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
9339 EndLoc, Vars, Alignment);
9340}
9341
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009342OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
9343 SourceLocation StartLoc,
9344 SourceLocation LParenLoc,
9345 SourceLocation EndLoc) {
9346 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009347 SmallVector<Expr *, 8> SrcExprs;
9348 SmallVector<Expr *, 8> DstExprs;
9349 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00009350 for (auto &RefExpr : VarList) {
9351 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
9352 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009353 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009354 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009355 SrcExprs.push_back(nullptr);
9356 DstExprs.push_back(nullptr);
9357 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009358 continue;
9359 }
9360
Alexey Bataeved09d242014-05-28 05:53:51 +00009361 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009362 // OpenMP [2.1, C/C++]
9363 // A list item is a variable name.
9364 // OpenMP [2.14.4.1, Restrictions, p.1]
9365 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00009366 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009367 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009368 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
9369 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009370 continue;
9371 }
9372
9373 Decl *D = DE->getDecl();
9374 VarDecl *VD = cast<VarDecl>(D);
9375
9376 QualType Type = VD->getType();
9377 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
9378 // It will be analyzed later.
9379 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009380 SrcExprs.push_back(nullptr);
9381 DstExprs.push_back(nullptr);
9382 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009383 continue;
9384 }
9385
9386 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
9387 // A list item that appears in a copyin clause must be threadprivate.
9388 if (!DSAStack->isThreadPrivate(VD)) {
9389 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00009390 << getOpenMPClauseName(OMPC_copyin)
9391 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009392 continue;
9393 }
9394
9395 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9396 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00009397 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009398 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009399 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009400 auto *SrcVD =
9401 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
9402 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00009403 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009404 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
9405 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009406 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
9407 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009408 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009409 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009410 // For arrays generate assignment operation for single element and replace
9411 // it by the original array element in CodeGen.
9412 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
9413 PseudoDstExpr, PseudoSrcExpr);
9414 if (AssignmentOp.isInvalid())
9415 continue;
9416 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
9417 /*DiscardedValue=*/true);
9418 if (AssignmentOp.isInvalid())
9419 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009420
9421 DSAStack->addDSA(VD, DE, OMPC_copyin);
9422 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009423 SrcExprs.push_back(PseudoSrcExpr);
9424 DstExprs.push_back(PseudoDstExpr);
9425 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009426 }
9427
Alexey Bataeved09d242014-05-28 05:53:51 +00009428 if (Vars.empty())
9429 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009430
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009431 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9432 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009433}
9434
Alexey Bataevbae9a792014-06-27 10:37:06 +00009435OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
9436 SourceLocation StartLoc,
9437 SourceLocation LParenLoc,
9438 SourceLocation EndLoc) {
9439 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00009440 SmallVector<Expr *, 8> SrcExprs;
9441 SmallVector<Expr *, 8> DstExprs;
9442 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009443 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009444 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9445 SourceLocation ELoc;
9446 SourceRange ERange;
9447 Expr *SimpleRefExpr = RefExpr;
9448 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9449 /*AllowArraySection=*/false);
9450 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009451 // It will be analyzed later.
9452 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009453 SrcExprs.push_back(nullptr);
9454 DstExprs.push_back(nullptr);
9455 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009456 }
Alexey Bataeve122da12016-03-17 10:50:17 +00009457 ValueDecl *D = Res.first;
9458 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +00009459 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009460
Alexey Bataeve122da12016-03-17 10:50:17 +00009461 QualType Type = D->getType();
9462 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009463
9464 // OpenMP [2.14.4.2, Restrictions, p.2]
9465 // A list item that appears in a copyprivate clause may not appear in a
9466 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +00009467 if (!VD || !DSAStack->isThreadPrivate(VD)) {
9468 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009469 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
9470 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009471 Diag(ELoc, diag::err_omp_wrong_dsa)
9472 << getOpenMPClauseName(DVar.CKind)
9473 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +00009474 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009475 continue;
9476 }
9477
9478 // OpenMP [2.11.4.2, Restrictions, p.1]
9479 // All list items that appear in a copyprivate clause must be either
9480 // threadprivate or private in the enclosing context.
9481 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009482 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009483 if (DVar.CKind == OMPC_shared) {
9484 Diag(ELoc, diag::err_omp_required_access)
9485 << getOpenMPClauseName(OMPC_copyprivate)
9486 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +00009487 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009488 continue;
9489 }
9490 }
9491 }
9492
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009493 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009494 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009495 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009496 << getOpenMPClauseName(OMPC_copyprivate) << Type
9497 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009498 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +00009499 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009500 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +00009501 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009502 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +00009503 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009504 continue;
9505 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009506
Alexey Bataevbae9a792014-06-27 10:37:06 +00009507 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9508 // A variable of class type (or array thereof) that appears in a
9509 // copyin clause requires an accessible, unambiguous copy assignment
9510 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009511 Type = Context.getBaseElementType(Type.getNonReferenceType())
9512 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00009513 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009514 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
9515 D->hasAttrs() ? &D->getAttrs() : nullptr);
9516 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009517 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009518 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
9519 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009520 auto *PseudoDstExpr =
Alexey Bataeve122da12016-03-17 10:50:17 +00009521 buildDeclRefExpr(*this, DstVD, Type, ELoc);
9522 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009523 PseudoDstExpr, PseudoSrcExpr);
9524 if (AssignmentOp.isInvalid())
9525 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +00009526 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009527 /*DiscardedValue=*/true);
9528 if (AssignmentOp.isInvalid())
9529 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009530
9531 // No need to mark vars as copyprivate, they are already threadprivate or
9532 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +00009533 assert(VD || IsOpenMPCapturedDecl(D));
9534 Vars.push_back(
9535 VD ? RefExpr->IgnoreParens()
9536 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +00009537 SrcExprs.push_back(PseudoSrcExpr);
9538 DstExprs.push_back(PseudoDstExpr);
9539 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00009540 }
9541
9542 if (Vars.empty())
9543 return nullptr;
9544
Alexey Bataeva63048e2015-03-23 06:18:07 +00009545 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
9546 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009547}
9548
Alexey Bataev6125da92014-07-21 11:26:11 +00009549OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
9550 SourceLocation StartLoc,
9551 SourceLocation LParenLoc,
9552 SourceLocation EndLoc) {
9553 if (VarList.empty())
9554 return nullptr;
9555
9556 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
9557}
Alexey Bataevdea47612014-07-23 07:46:59 +00009558
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009559OMPClause *
9560Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
9561 SourceLocation DepLoc, SourceLocation ColonLoc,
9562 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9563 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009564 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009565 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009566 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009567 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00009568 return nullptr;
9569 }
9570 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009571 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
9572 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00009573 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009574 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009575 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
9576 /*Last=*/OMPC_DEPEND_unknown, Except)
9577 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009578 return nullptr;
9579 }
9580 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +00009581 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009582 llvm::APSInt DepCounter(/*BitWidth=*/32);
9583 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
9584 if (DepKind == OMPC_DEPEND_sink) {
9585 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
9586 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
9587 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009588 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009589 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009590 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
9591 DSAStack->getParentOrderedRegionParam()) {
9592 for (auto &RefExpr : VarList) {
9593 assert(RefExpr && "NULL expr in OpenMP shared clause.");
Alexey Bataev8b427062016-05-25 12:36:08 +00009594 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009595 // It will be analyzed later.
9596 Vars.push_back(RefExpr);
9597 continue;
9598 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009599
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009600 SourceLocation ELoc = RefExpr->getExprLoc();
9601 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
9602 if (DepKind == OMPC_DEPEND_sink) {
9603 if (DepCounter >= TotalDepCount) {
9604 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
9605 continue;
9606 }
9607 ++DepCounter;
9608 // OpenMP [2.13.9, Summary]
9609 // depend(dependence-type : vec), where dependence-type is:
9610 // 'sink' and where vec is the iteration vector, which has the form:
9611 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
9612 // where n is the value specified by the ordered clause in the loop
9613 // directive, xi denotes the loop iteration variable of the i-th nested
9614 // loop associated with the loop directive, and di is a constant
9615 // non-negative integer.
Alexey Bataev8b427062016-05-25 12:36:08 +00009616 if (CurContext->isDependentContext()) {
9617 // It will be analyzed later.
9618 Vars.push_back(RefExpr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009619 continue;
9620 }
Alexey Bataev8b427062016-05-25 12:36:08 +00009621 SimpleExpr = SimpleExpr->IgnoreImplicit();
9622 OverloadedOperatorKind OOK = OO_None;
9623 SourceLocation OOLoc;
9624 Expr *LHS = SimpleExpr;
9625 Expr *RHS = nullptr;
9626 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
9627 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
9628 OOLoc = BO->getOperatorLoc();
9629 LHS = BO->getLHS()->IgnoreParenImpCasts();
9630 RHS = BO->getRHS()->IgnoreParenImpCasts();
9631 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
9632 OOK = OCE->getOperator();
9633 OOLoc = OCE->getOperatorLoc();
9634 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9635 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
9636 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
9637 OOK = MCE->getMethodDecl()
9638 ->getNameInfo()
9639 .getName()
9640 .getCXXOverloadedOperator();
9641 OOLoc = MCE->getCallee()->getExprLoc();
9642 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
9643 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9644 }
9645 SourceLocation ELoc;
9646 SourceRange ERange;
9647 auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
9648 /*AllowArraySection=*/false);
9649 if (Res.second) {
9650 // It will be analyzed later.
9651 Vars.push_back(RefExpr);
9652 }
9653 ValueDecl *D = Res.first;
9654 if (!D)
9655 continue;
9656
9657 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
9658 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
9659 continue;
9660 }
9661 if (RHS) {
9662 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
9663 RHS, OMPC_depend, /*StrictlyPositive=*/false);
9664 if (RHSRes.isInvalid())
9665 continue;
9666 }
9667 if (!CurContext->isDependentContext() &&
9668 DSAStack->getParentOrderedRegionParam() &&
9669 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
9670 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
9671 << DSAStack->getParentLoopControlVariable(
9672 DepCounter.getZExtValue());
9673 continue;
9674 }
9675 OpsOffs.push_back({RHS, OOK});
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009676 } else {
9677 // OpenMP [2.11.1.1, Restrictions, p.3]
9678 // A variable that is part of another variable (such as a field of a
9679 // structure) but is not an array element or an array section cannot
9680 // appear in a depend clause.
9681 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9682 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
9683 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
9684 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
9685 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +00009686 (ASE &&
9687 !ASE->getBase()
9688 ->getType()
9689 .getNonReferenceType()
9690 ->isPointerType() &&
9691 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009692 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
9693 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009694 continue;
9695 }
9696 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009697 Vars.push_back(RefExpr->IgnoreParenImpCasts());
9698 }
9699
9700 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
9701 TotalDepCount > VarList.size() &&
9702 DSAStack->getParentOrderedRegionParam()) {
9703 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
9704 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
9705 }
9706 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
9707 Vars.empty())
9708 return nullptr;
9709 }
Alexey Bataev8b427062016-05-25 12:36:08 +00009710 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
9711 DepKind, DepLoc, ColonLoc, Vars);
9712 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source)
9713 DSAStack->addDoacrossDependClause(C, OpsOffs);
9714 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009715}
Michael Wonge710d542015-08-07 16:16:36 +00009716
9717OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
9718 SourceLocation LParenLoc,
9719 SourceLocation EndLoc) {
9720 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00009721
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009722 // OpenMP [2.9.1, Restrictions]
9723 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009724 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
9725 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009726 return nullptr;
9727
Michael Wonge710d542015-08-07 16:16:36 +00009728 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9729}
Kelvin Li0bff7af2015-11-23 05:32:03 +00009730
9731static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
9732 DSAStackTy *Stack, CXXRecordDecl *RD) {
9733 if (!RD || RD->isInvalidDecl())
9734 return true;
9735
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00009736 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
9737 if (auto *CTD = CTSD->getSpecializedTemplate())
9738 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009739 auto QTy = SemaRef.Context.getRecordType(RD);
9740 if (RD->isDynamicClass()) {
9741 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9742 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
9743 return false;
9744 }
9745 auto *DC = RD;
9746 bool IsCorrect = true;
9747 for (auto *I : DC->decls()) {
9748 if (I) {
9749 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
9750 if (MD->isStatic()) {
9751 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9752 SemaRef.Diag(MD->getLocation(),
9753 diag::note_omp_static_member_in_target);
9754 IsCorrect = false;
9755 }
9756 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
9757 if (VD->isStaticDataMember()) {
9758 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9759 SemaRef.Diag(VD->getLocation(),
9760 diag::note_omp_static_member_in_target);
9761 IsCorrect = false;
9762 }
9763 }
9764 }
9765 }
9766
9767 for (auto &I : RD->bases()) {
9768 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
9769 I.getType()->getAsCXXRecordDecl()))
9770 IsCorrect = false;
9771 }
9772 return IsCorrect;
9773}
9774
9775static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
9776 DSAStackTy *Stack, QualType QTy) {
9777 NamedDecl *ND;
9778 if (QTy->isIncompleteType(&ND)) {
9779 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
9780 return false;
9781 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
9782 if (!RD->isInvalidDecl() &&
9783 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
9784 return false;
9785 }
9786 return true;
9787}
9788
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009789/// \brief Return true if it can be proven that the provided array expression
9790/// (array section or array subscript) does NOT specify the whole size of the
9791/// array whose base type is \a BaseQTy.
9792static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
9793 const Expr *E,
9794 QualType BaseQTy) {
9795 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9796
9797 // If this is an array subscript, it refers to the whole size if the size of
9798 // the dimension is constant and equals 1. Also, an array section assumes the
9799 // format of an array subscript if no colon is used.
9800 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
9801 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9802 return ATy->getSize().getSExtValue() != 1;
9803 // Size can't be evaluated statically.
9804 return false;
9805 }
9806
9807 assert(OASE && "Expecting array section if not an array subscript.");
9808 auto *LowerBound = OASE->getLowerBound();
9809 auto *Length = OASE->getLength();
9810
9811 // If there is a lower bound that does not evaluates to zero, we are not
9812 // convering the whole dimension.
9813 if (LowerBound) {
9814 llvm::APSInt ConstLowerBound;
9815 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
9816 return false; // Can't get the integer value as a constant.
9817 if (ConstLowerBound.getSExtValue())
9818 return true;
9819 }
9820
9821 // If we don't have a length we covering the whole dimension.
9822 if (!Length)
9823 return false;
9824
9825 // If the base is a pointer, we don't have a way to get the size of the
9826 // pointee.
9827 if (BaseQTy->isPointerType())
9828 return false;
9829
9830 // We can only check if the length is the same as the size of the dimension
9831 // if we have a constant array.
9832 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
9833 if (!CATy)
9834 return false;
9835
9836 llvm::APSInt ConstLength;
9837 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9838 return false; // Can't get the integer value as a constant.
9839
9840 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
9841}
9842
9843// Return true if it can be proven that the provided array expression (array
9844// section or array subscript) does NOT specify a single element of the array
9845// whose base type is \a BaseQTy.
9846static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
9847 const Expr *E,
9848 QualType BaseQTy) {
9849 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9850
9851 // An array subscript always refer to a single element. Also, an array section
9852 // assumes the format of an array subscript if no colon is used.
9853 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
9854 return false;
9855
9856 assert(OASE && "Expecting array section if not an array subscript.");
9857 auto *Length = OASE->getLength();
9858
9859 // If we don't have a length we have to check if the array has unitary size
9860 // for this dimension. Also, we should always expect a length if the base type
9861 // is pointer.
9862 if (!Length) {
9863 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9864 return ATy->getSize().getSExtValue() != 1;
9865 // We cannot assume anything.
9866 return false;
9867 }
9868
9869 // Check if the length evaluates to 1.
9870 llvm::APSInt ConstLength;
9871 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9872 return false; // Can't get the integer value as a constant.
9873
9874 return ConstLength.getSExtValue() != 1;
9875}
9876
Samuel Antao5de996e2016-01-22 20:21:36 +00009877// Return the expression of the base of the map clause or null if it cannot
9878// be determined and do all the necessary checks to see if the expression is
Samuel Antao90927002016-04-26 14:54:23 +00009879// valid as a standalone map clause expression. In the process, record all the
9880// components of the expression.
9881static Expr *CheckMapClauseExpressionBase(
9882 Sema &SemaRef, Expr *E,
9883 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009884 SourceLocation ELoc = E->getExprLoc();
9885 SourceRange ERange = E->getSourceRange();
9886
9887 // The base of elements of list in a map clause have to be either:
9888 // - a reference to variable or field.
9889 // - a member expression.
9890 // - an array expression.
9891 //
9892 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
9893 // reference to 'r'.
9894 //
9895 // If we have:
9896 //
9897 // struct SS {
9898 // Bla S;
9899 // foo() {
9900 // #pragma omp target map (S.Arr[:12]);
9901 // }
9902 // }
9903 //
9904 // We want to retrieve the member expression 'this->S';
9905
9906 Expr *RelevantExpr = nullptr;
9907
Samuel Antao5de996e2016-01-22 20:21:36 +00009908 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
9909 // If a list item is an array section, it must specify contiguous storage.
9910 //
9911 // For this restriction it is sufficient that we make sure only references
9912 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009913 // exist except in the rightmost expression (unless they cover the whole
9914 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +00009915 //
9916 // r.ArrS[3:5].Arr[6:7]
9917 //
9918 // r.ArrS[3:5].x
9919 //
9920 // but these would be valid:
9921 // r.ArrS[3].Arr[6:7]
9922 //
9923 // r.ArrS[3].x
9924
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009925 bool AllowUnitySizeArraySection = true;
9926 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +00009927
Dmitry Polukhin644a9252016-03-11 07:58:34 +00009928 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009929 E = E->IgnoreParenImpCasts();
9930
9931 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
9932 if (!isa<VarDecl>(CurE->getDecl()))
9933 break;
9934
9935 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009936
9937 // If we got a reference to a declaration, we should not expect any array
9938 // section before that.
9939 AllowUnitySizeArraySection = false;
9940 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +00009941
9942 // Record the component.
9943 CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent(
9944 CurE, CurE->getDecl()));
Samuel Antao5de996e2016-01-22 20:21:36 +00009945 continue;
9946 }
9947
9948 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
9949 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9950
9951 if (isa<CXXThisExpr>(BaseE))
9952 // We found a base expression: this->Val.
9953 RelevantExpr = CurE;
9954 else
9955 E = BaseE;
9956
9957 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
9958 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
9959 << CurE->getSourceRange();
9960 break;
9961 }
9962
9963 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
9964
9965 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
9966 // A bit-field cannot appear in a map clause.
9967 //
9968 if (FD->isBitField()) {
9969 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
9970 << CurE->getSourceRange();
9971 break;
9972 }
9973
9974 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9975 // If the type of a list item is a reference to a type T then the type
9976 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009977 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +00009978
9979 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
9980 // A list item cannot be a variable that is a member of a structure with
9981 // a union type.
9982 //
9983 if (auto *RT = CurType->getAs<RecordType>())
9984 if (RT->isUnionType()) {
9985 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
9986 << CurE->getSourceRange();
9987 break;
9988 }
9989
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009990 // If we got a member expression, we should not expect any array section
9991 // before that:
9992 //
9993 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
9994 // If a list item is an element of a structure, only the rightmost symbol
9995 // of the variable reference can be an array section.
9996 //
9997 AllowUnitySizeArraySection = false;
9998 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +00009999
10000 // Record the component.
10001 CurComponents.push_back(
10002 OMPClauseMappableExprCommon::MappableComponent(CurE, FD));
Samuel Antao5de996e2016-01-22 20:21:36 +000010003 continue;
10004 }
10005
10006 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
10007 E = CurE->getBase()->IgnoreParenImpCasts();
10008
10009 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
10010 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10011 << 0 << CurE->getSourceRange();
10012 break;
10013 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010014
10015 // If we got an array subscript that express the whole dimension we
10016 // can have any array expressions before. If it only expressing part of
10017 // the dimension, we can only have unitary-size array expressions.
10018 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
10019 E->getType()))
10020 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010021
10022 // Record the component - we don't have any declaration associated.
10023 CurComponents.push_back(
10024 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010025 continue;
10026 }
10027
10028 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010029 E = CurE->getBase()->IgnoreParenImpCasts();
10030
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010031 auto CurType =
10032 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
10033
Samuel Antao5de996e2016-01-22 20:21:36 +000010034 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10035 // If the type of a list item is a reference to a type T then the type
10036 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000010037 if (CurType->isReferenceType())
10038 CurType = CurType->getPointeeType();
10039
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010040 bool IsPointer = CurType->isAnyPointerType();
10041
10042 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010043 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10044 << 0 << CurE->getSourceRange();
10045 break;
10046 }
10047
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010048 bool NotWhole =
10049 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
10050 bool NotUnity =
10051 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
10052
10053 if (AllowWholeSizeArraySection && AllowUnitySizeArraySection) {
10054 // Any array section is currently allowed.
10055 //
10056 // If this array section refers to the whole dimension we can still
10057 // accept other array sections before this one, except if the base is a
10058 // pointer. Otherwise, only unitary sections are accepted.
10059 if (NotWhole || IsPointer)
10060 AllowWholeSizeArraySection = false;
10061 } else if ((AllowUnitySizeArraySection && NotUnity) ||
10062 (AllowWholeSizeArraySection && NotWhole)) {
10063 // A unity or whole array section is not allowed and that is not
10064 // compatible with the properties of the current array section.
10065 SemaRef.Diag(
10066 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
10067 << CurE->getSourceRange();
10068 break;
10069 }
Samuel Antao90927002016-04-26 14:54:23 +000010070
10071 // Record the component - we don't have any declaration associated.
10072 CurComponents.push_back(
10073 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010074 continue;
10075 }
10076
10077 // If nothing else worked, this is not a valid map clause expression.
10078 SemaRef.Diag(ELoc,
10079 diag::err_omp_expected_named_var_member_or_array_expression)
10080 << ERange;
10081 break;
10082 }
10083
10084 return RelevantExpr;
10085}
10086
10087// Return true if expression E associated with value VD has conflicts with other
10088// map information.
Samuel Antao90927002016-04-26 14:54:23 +000010089static bool CheckMapConflicts(
10090 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
10091 bool CurrentRegionOnly,
10092 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010093 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000010094 SourceLocation ELoc = E->getExprLoc();
10095 SourceRange ERange = E->getSourceRange();
10096
10097 // In order to easily check the conflicts we need to match each component of
10098 // the expression under test with the components of the expressions that are
10099 // already in the stack.
10100
Samuel Antao5de996e2016-01-22 20:21:36 +000010101 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010102 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010103 "Map clause expression with unexpected base!");
10104
10105 // Variables to help detecting enclosing problems in data environment nests.
10106 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000010107 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000010108
Samuel Antao90927002016-04-26 14:54:23 +000010109 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
10110 VD, CurrentRegionOnly,
10111 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
10112 StackComponents) -> bool {
10113
Samuel Antao5de996e2016-01-22 20:21:36 +000010114 assert(!StackComponents.empty() &&
10115 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010116 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010117 "Map clause expression with unexpected base!");
10118
Samuel Antao90927002016-04-26 14:54:23 +000010119 // The whole expression in the stack.
10120 auto *RE = StackComponents.front().getAssociatedExpression();
10121
Samuel Antao5de996e2016-01-22 20:21:36 +000010122 // Expressions must start from the same base. Here we detect at which
10123 // point both expressions diverge from each other and see if we can
10124 // detect if the memory referred to both expressions is contiguous and
10125 // do not overlap.
10126 auto CI = CurComponents.rbegin();
10127 auto CE = CurComponents.rend();
10128 auto SI = StackComponents.rbegin();
10129 auto SE = StackComponents.rend();
10130 for (; CI != CE && SI != SE; ++CI, ++SI) {
10131
10132 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
10133 // At most one list item can be an array item derived from a given
10134 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000010135 if (CurrentRegionOnly &&
10136 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
10137 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
10138 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
10139 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
10140 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000010141 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000010142 << CI->getAssociatedExpression()->getSourceRange();
10143 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
10144 diag::note_used_here)
10145 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000010146 return true;
10147 }
10148
10149 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000010150 if (CI->getAssociatedExpression()->getStmtClass() !=
10151 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000010152 break;
10153
10154 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000010155 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000010156 break;
10157 }
10158
10159 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10160 // List items of map clauses in the same construct must not share
10161 // original storage.
10162 //
10163 // If the expressions are exactly the same or one is a subset of the
10164 // other, it means they are sharing storage.
10165 if (CI == CE && SI == SE) {
10166 if (CurrentRegionOnly) {
10167 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10168 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10169 << RE->getSourceRange();
10170 return true;
10171 } else {
10172 // If we find the same expression in the enclosing data environment,
10173 // that is legal.
10174 IsEnclosedByDataEnvironmentExpr = true;
10175 return false;
10176 }
10177 }
10178
Samuel Antao90927002016-04-26 14:54:23 +000010179 QualType DerivedType =
10180 std::prev(CI)->getAssociatedDeclaration()->getType();
10181 SourceLocation DerivedLoc =
10182 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000010183
10184 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10185 // If the type of a list item is a reference to a type T then the type
10186 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010187 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010188
10189 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
10190 // A variable for which the type is pointer and an array section
10191 // derived from that variable must not appear as list items of map
10192 // clauses of the same construct.
10193 //
10194 // Also, cover one of the cases in:
10195 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10196 // If any part of the original storage of a list item has corresponding
10197 // storage in the device data environment, all of the original storage
10198 // must have corresponding storage in the device data environment.
10199 //
10200 if (DerivedType->isAnyPointerType()) {
10201 if (CI == CE || SI == SE) {
10202 SemaRef.Diag(
10203 DerivedLoc,
10204 diag::err_omp_pointer_mapped_along_with_derived_section)
10205 << DerivedLoc;
10206 } else {
10207 assert(CI != CE && SI != SE);
10208 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
10209 << DerivedLoc;
10210 }
10211 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10212 << RE->getSourceRange();
10213 return true;
10214 }
10215
10216 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10217 // List items of map clauses in the same construct must not share
10218 // original storage.
10219 //
10220 // An expression is a subset of the other.
10221 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
10222 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10223 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10224 << RE->getSourceRange();
10225 return true;
10226 }
10227
10228 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000010229 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000010230 if (!CurrentRegionOnly && SI != SE)
10231 EnclosingExpr = RE;
10232
10233 // The current expression is a subset of the expression in the data
10234 // environment.
10235 IsEnclosedByDataEnvironmentExpr |=
10236 (!CurrentRegionOnly && CI != CE && SI == SE);
10237
10238 return false;
10239 });
10240
10241 if (CurrentRegionOnly)
10242 return FoundError;
10243
10244 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10245 // If any part of the original storage of a list item has corresponding
10246 // storage in the device data environment, all of the original storage must
10247 // have corresponding storage in the device data environment.
10248 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
10249 // If a list item is an element of a structure, and a different element of
10250 // the structure has a corresponding list item in the device data environment
10251 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000010252 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000010253 // data environment prior to the task encountering the construct.
10254 //
10255 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
10256 SemaRef.Diag(ELoc,
10257 diag::err_omp_original_storage_is_shared_and_does_not_contain)
10258 << ERange;
10259 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
10260 << EnclosingExpr->getSourceRange();
10261 return true;
10262 }
10263
10264 return FoundError;
10265}
10266
Samuel Antao23abd722016-01-19 20:40:49 +000010267OMPClause *
10268Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
10269 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
10270 SourceLocation MapLoc, SourceLocation ColonLoc,
10271 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10272 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000010273 SmallVector<Expr *, 4> Vars;
10274
Samuel Antao90927002016-04-26 14:54:23 +000010275 // Keep track of the mappable components and base declarations in this clause.
10276 // Each entry in the list is going to have a list of components associated. We
10277 // record each set of the components so that we can build the clause later on.
10278 // In the end we should have the same amount of declarations and component
10279 // lists.
10280 OMPClauseMappableExprCommon::MappableExprComponentLists ClauseComponents;
10281 SmallVector<ValueDecl *, 16> ClauseBaseDeclarations;
10282
10283 ClauseComponents.reserve(VarList.size());
10284 ClauseBaseDeclarations.reserve(VarList.size());
10285
Kelvin Li0bff7af2015-11-23 05:32:03 +000010286 for (auto &RE : VarList) {
10287 assert(RE && "Null expr in omp map");
10288 if (isa<DependentScopeDeclRefExpr>(RE)) {
10289 // It will be analyzed later.
10290 Vars.push_back(RE);
10291 continue;
10292 }
10293 SourceLocation ELoc = RE->getExprLoc();
10294
Kelvin Li0bff7af2015-11-23 05:32:03 +000010295 auto *VE = RE->IgnoreParenLValueCasts();
10296
10297 if (VE->isValueDependent() || VE->isTypeDependent() ||
10298 VE->isInstantiationDependent() ||
10299 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010300 // We can only analyze this information once the missing information is
10301 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010302 Vars.push_back(RE);
10303 continue;
10304 }
10305
10306 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010307
Samuel Antao5de996e2016-01-22 20:21:36 +000010308 if (!RE->IgnoreParenImpCasts()->isLValue()) {
10309 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
10310 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010311 continue;
10312 }
10313
Samuel Antao90927002016-04-26 14:54:23 +000010314 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
10315 ValueDecl *CurDeclaration = nullptr;
10316
10317 // Obtain the array or member expression bases if required. Also, fill the
10318 // components array with all the components identified in the process.
10319 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr, CurComponents);
Samuel Antao5de996e2016-01-22 20:21:36 +000010320 if (!BE)
10321 continue;
10322
Samuel Antao90927002016-04-26 14:54:23 +000010323 assert(!CurComponents.empty() &&
10324 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010325
Samuel Antao90927002016-04-26 14:54:23 +000010326 // For the following checks, we rely on the base declaration which is
10327 // expected to be associated with the last component. The declaration is
10328 // expected to be a variable or a field (if 'this' is being mapped).
10329 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
10330 assert(CurDeclaration && "Null decl on map clause.");
10331 assert(
10332 CurDeclaration->isCanonicalDecl() &&
10333 "Expecting components to have associated only canonical declarations.");
10334
10335 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
10336 auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000010337
10338 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000010339 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000010340
10341 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
10342 // threadprivate variables cannot appear in a map clause.
10343 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000010344 auto DVar = DSAStack->getTopDSA(VD, false);
10345 Diag(ELoc, diag::err_omp_threadprivate_in_map);
10346 ReportOriginalDSA(*this, DSAStack, VD, DVar);
10347 continue;
10348 }
10349
Samuel Antao5de996e2016-01-22 20:21:36 +000010350 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
10351 // A list item cannot appear in both a map clause and a data-sharing
10352 // attribute clause on the same construct.
10353 //
10354 // TODO: Implement this check - it cannot currently be tested because of
10355 // missing implementation of the other data sharing clauses in target
10356 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010357
Samuel Antao5de996e2016-01-22 20:21:36 +000010358 // Check conflicts with other map clause expressions. We check the conflicts
10359 // with the current construct separately from the enclosing data
10360 // environment, because the restrictions are different.
Samuel Antao90927002016-04-26 14:54:23 +000010361 if (CheckMapConflicts(*this, DSAStack, CurDeclaration, SimpleExpr,
10362 /*CurrentRegionOnly=*/true, CurComponents))
Samuel Antao5de996e2016-01-22 20:21:36 +000010363 break;
Samuel Antao90927002016-04-26 14:54:23 +000010364 if (CheckMapConflicts(*this, DSAStack, CurDeclaration, SimpleExpr,
10365 /*CurrentRegionOnly=*/false, CurComponents))
Samuel Antao5de996e2016-01-22 20:21:36 +000010366 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000010367
Samuel Antao5de996e2016-01-22 20:21:36 +000010368 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10369 // If the type of a list item is a reference to a type T then the type will
10370 // be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010371 QualType Type = CurDeclaration->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010372
10373 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000010374 // A list item must have a mappable type.
10375 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
10376 DSAStack, Type))
10377 continue;
10378
Samuel Antaodf67fc42016-01-19 19:15:56 +000010379 // target enter data
10380 // OpenMP [2.10.2, Restrictions, p. 99]
10381 // A map-type must be specified in all map clauses and must be either
10382 // to or alloc.
10383 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
10384 if (DKind == OMPD_target_enter_data &&
10385 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
10386 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +000010387 << (IsMapTypeImplicit ? 1 : 0)
10388 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +000010389 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010390 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +000010391 }
10392
Samuel Antao72590762016-01-19 20:04:50 +000010393 // target exit_data
10394 // OpenMP [2.10.3, Restrictions, p. 102]
10395 // A map-type must be specified in all map clauses and must be either
10396 // from, release, or delete.
10397 DKind = DSAStack->getCurrentDirective();
10398 if (DKind == OMPD_target_exit_data &&
10399 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
10400 MapType == OMPC_MAP_delete)) {
10401 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +000010402 << (IsMapTypeImplicit ? 1 : 0)
10403 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +000010404 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010405 continue;
Samuel Antao72590762016-01-19 20:04:50 +000010406 }
10407
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010408 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10409 // A list item cannot appear in both a map clause and a data-sharing
10410 // attribute clause on the same construct
10411 if (DKind == OMPD_target && VD) {
10412 auto DVar = DSAStack->getTopDSA(VD, false);
10413 if (isOpenMPPrivate(DVar.CKind)) {
10414 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
10415 << getOpenMPClauseName(DVar.CKind)
10416 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Samuel Antao90927002016-04-26 14:54:23 +000010417 ReportOriginalDSA(*this, DSAStack, CurDeclaration, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010418 continue;
10419 }
10420 }
10421
Samuel Antao90927002016-04-26 14:54:23 +000010422 // Save the current expression.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010423 Vars.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000010424
10425 // Store the components in the stack so that they can be used to check
10426 // against other clauses later on.
10427 DSAStack->addMappableExpressionComponents(CurDeclaration, CurComponents);
10428
10429 // Save the components and declaration to create the clause. For purposes of
10430 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000010431 // null as base declaration.
Samuel Antao90927002016-04-26 14:54:23 +000010432 ClauseComponents.resize(ClauseComponents.size() + 1);
10433 ClauseComponents.back().append(CurComponents.begin(), CurComponents.end());
10434 ClauseBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
10435 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010436 }
Kelvin Li0bff7af2015-11-23 05:32:03 +000010437
Samuel Antao5de996e2016-01-22 20:21:36 +000010438 // We need to produce a map clause even if we don't have variables so that
10439 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao90927002016-04-26 14:54:23 +000010440 return OMPMapClause::Create(
10441 Context, StartLoc, LParenLoc, EndLoc, Vars, ClauseBaseDeclarations,
10442 ClauseComponents, MapTypeModifier, MapType, IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010443}
Kelvin Li099bb8c2015-11-24 20:50:12 +000010444
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010445QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
10446 TypeResult ParsedType) {
10447 assert(ParsedType.isUsable());
10448
10449 QualType ReductionType = GetTypeFromParser(ParsedType.get());
10450 if (ReductionType.isNull())
10451 return QualType();
10452
10453 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
10454 // A type name in a declare reduction directive cannot be a function type, an
10455 // array type, a reference type, or a type qualified with const, volatile or
10456 // restrict.
10457 if (ReductionType.hasQualifiers()) {
10458 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
10459 return QualType();
10460 }
10461
10462 if (ReductionType->isFunctionType()) {
10463 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
10464 return QualType();
10465 }
10466 if (ReductionType->isReferenceType()) {
10467 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
10468 return QualType();
10469 }
10470 if (ReductionType->isArrayType()) {
10471 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
10472 return QualType();
10473 }
10474 return ReductionType;
10475}
10476
10477Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
10478 Scope *S, DeclContext *DC, DeclarationName Name,
10479 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
10480 AccessSpecifier AS, Decl *PrevDeclInScope) {
10481 SmallVector<Decl *, 8> Decls;
10482 Decls.reserve(ReductionTypes.size());
10483
10484 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
10485 ForRedeclaration);
10486 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
10487 // A reduction-identifier may not be re-declared in the current scope for the
10488 // same type or for a type that is compatible according to the base language
10489 // rules.
10490 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
10491 OMPDeclareReductionDecl *PrevDRD = nullptr;
10492 bool InCompoundScope = true;
10493 if (S != nullptr) {
10494 // Find previous declaration with the same name not referenced in other
10495 // declarations.
10496 FunctionScopeInfo *ParentFn = getEnclosingFunction();
10497 InCompoundScope =
10498 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
10499 LookupName(Lookup, S);
10500 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
10501 /*AllowInlineNamespace=*/false);
10502 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
10503 auto Filter = Lookup.makeFilter();
10504 while (Filter.hasNext()) {
10505 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
10506 if (InCompoundScope) {
10507 auto I = UsedAsPrevious.find(PrevDecl);
10508 if (I == UsedAsPrevious.end())
10509 UsedAsPrevious[PrevDecl] = false;
10510 if (auto *D = PrevDecl->getPrevDeclInScope())
10511 UsedAsPrevious[D] = true;
10512 }
10513 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
10514 PrevDecl->getLocation();
10515 }
10516 Filter.done();
10517 if (InCompoundScope) {
10518 for (auto &PrevData : UsedAsPrevious) {
10519 if (!PrevData.second) {
10520 PrevDRD = PrevData.first;
10521 break;
10522 }
10523 }
10524 }
10525 } else if (PrevDeclInScope != nullptr) {
10526 auto *PrevDRDInScope = PrevDRD =
10527 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
10528 do {
10529 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
10530 PrevDRDInScope->getLocation();
10531 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
10532 } while (PrevDRDInScope != nullptr);
10533 }
10534 for (auto &TyData : ReductionTypes) {
10535 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
10536 bool Invalid = false;
10537 if (I != PreviousRedeclTypes.end()) {
10538 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
10539 << TyData.first;
10540 Diag(I->second, diag::note_previous_definition);
10541 Invalid = true;
10542 }
10543 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
10544 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
10545 Name, TyData.first, PrevDRD);
10546 DC->addDecl(DRD);
10547 DRD->setAccess(AS);
10548 Decls.push_back(DRD);
10549 if (Invalid)
10550 DRD->setInvalidDecl();
10551 else
10552 PrevDRD = DRD;
10553 }
10554
10555 return DeclGroupPtrTy::make(
10556 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
10557}
10558
10559void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
10560 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10561
10562 // Enter new function scope.
10563 PushFunctionScope();
10564 getCurFunction()->setHasBranchProtectedScope();
10565 getCurFunction()->setHasOMPDeclareReductionCombiner();
10566
10567 if (S != nullptr)
10568 PushDeclContext(S, DRD);
10569 else
10570 CurContext = DRD;
10571
10572 PushExpressionEvaluationContext(PotentiallyEvaluated);
10573
10574 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010575 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
10576 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
10577 // uses semantics of argument handles by value, but it should be passed by
10578 // reference. C lang does not support references, so pass all parameters as
10579 // pointers.
10580 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010581 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010582 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010583 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
10584 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
10585 // uses semantics of argument handles by value, but it should be passed by
10586 // reference. C lang does not support references, so pass all parameters as
10587 // pointers.
10588 // Create 'T omp_out;' variable.
10589 auto *OmpOutParm =
10590 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
10591 if (S != nullptr) {
10592 PushOnScopeChains(OmpInParm, S);
10593 PushOnScopeChains(OmpOutParm, S);
10594 } else {
10595 DRD->addDecl(OmpInParm);
10596 DRD->addDecl(OmpOutParm);
10597 }
10598}
10599
10600void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
10601 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10602 DiscardCleanupsInEvaluationContext();
10603 PopExpressionEvaluationContext();
10604
10605 PopDeclContext();
10606 PopFunctionScopeInfo();
10607
10608 if (Combiner != nullptr)
10609 DRD->setCombiner(Combiner);
10610 else
10611 DRD->setInvalidDecl();
10612}
10613
10614void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
10615 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10616
10617 // Enter new function scope.
10618 PushFunctionScope();
10619 getCurFunction()->setHasBranchProtectedScope();
10620
10621 if (S != nullptr)
10622 PushDeclContext(S, DRD);
10623 else
10624 CurContext = DRD;
10625
10626 PushExpressionEvaluationContext(PotentiallyEvaluated);
10627
10628 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010629 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
10630 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
10631 // uses semantics of argument handles by value, but it should be passed by
10632 // reference. C lang does not support references, so pass all parameters as
10633 // pointers.
10634 // Create 'T omp_priv;' variable.
10635 auto *OmpPrivParm =
10636 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010637 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
10638 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
10639 // uses semantics of argument handles by value, but it should be passed by
10640 // reference. C lang does not support references, so pass all parameters as
10641 // pointers.
10642 // Create 'T omp_orig;' variable.
10643 auto *OmpOrigParm =
10644 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010645 if (S != nullptr) {
10646 PushOnScopeChains(OmpPrivParm, S);
10647 PushOnScopeChains(OmpOrigParm, S);
10648 } else {
10649 DRD->addDecl(OmpPrivParm);
10650 DRD->addDecl(OmpOrigParm);
10651 }
10652}
10653
10654void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
10655 Expr *Initializer) {
10656 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10657 DiscardCleanupsInEvaluationContext();
10658 PopExpressionEvaluationContext();
10659
10660 PopDeclContext();
10661 PopFunctionScopeInfo();
10662
10663 if (Initializer != nullptr)
10664 DRD->setInitializer(Initializer);
10665 else
10666 DRD->setInvalidDecl();
10667}
10668
10669Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
10670 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
10671 for (auto *D : DeclReductions.get()) {
10672 if (IsValid) {
10673 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10674 if (S != nullptr)
10675 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
10676 } else
10677 D->setInvalidDecl();
10678 }
10679 return DeclReductions;
10680}
10681
Kelvin Li099bb8c2015-11-24 20:50:12 +000010682OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
10683 SourceLocation StartLoc,
10684 SourceLocation LParenLoc,
10685 SourceLocation EndLoc) {
10686 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010687
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010688 // OpenMP [teams Constrcut, Restrictions]
10689 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010690 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
10691 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010692 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010693
10694 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10695}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010696
10697OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
10698 SourceLocation StartLoc,
10699 SourceLocation LParenLoc,
10700 SourceLocation EndLoc) {
10701 Expr *ValExpr = ThreadLimit;
10702
10703 // OpenMP [teams Constrcut, Restrictions]
10704 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010705 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
10706 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010707 return nullptr;
10708
10709 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
10710 EndLoc);
10711}
Alexey Bataeva0569352015-12-01 10:17:31 +000010712
10713OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
10714 SourceLocation StartLoc,
10715 SourceLocation LParenLoc,
10716 SourceLocation EndLoc) {
10717 Expr *ValExpr = Priority;
10718
10719 // OpenMP [2.9.1, task Constrcut]
10720 // The priority-value is a non-negative numerical scalar expression.
10721 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
10722 /*StrictlyPositive=*/false))
10723 return nullptr;
10724
10725 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10726}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000010727
10728OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
10729 SourceLocation StartLoc,
10730 SourceLocation LParenLoc,
10731 SourceLocation EndLoc) {
10732 Expr *ValExpr = Grainsize;
10733
10734 // OpenMP [2.9.2, taskloop Constrcut]
10735 // The parameter of the grainsize clause must be a positive integer
10736 // expression.
10737 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
10738 /*StrictlyPositive=*/true))
10739 return nullptr;
10740
10741 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10742}
Alexey Bataev382967a2015-12-08 12:06:20 +000010743
10744OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
10745 SourceLocation StartLoc,
10746 SourceLocation LParenLoc,
10747 SourceLocation EndLoc) {
10748 Expr *ValExpr = NumTasks;
10749
10750 // OpenMP [2.9.2, taskloop Constrcut]
10751 // The parameter of the num_tasks clause must be a positive integer
10752 // expression.
10753 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
10754 /*StrictlyPositive=*/true))
10755 return nullptr;
10756
10757 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10758}
10759
Alexey Bataev28c75412015-12-15 08:19:24 +000010760OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
10761 SourceLocation LParenLoc,
10762 SourceLocation EndLoc) {
10763 // OpenMP [2.13.2, critical construct, Description]
10764 // ... where hint-expression is an integer constant expression that evaluates
10765 // to a valid lock hint.
10766 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
10767 if (HintExpr.isInvalid())
10768 return nullptr;
10769 return new (Context)
10770 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
10771}
10772
Carlo Bertollib4adf552016-01-15 18:50:31 +000010773OMPClause *Sema::ActOnOpenMPDistScheduleClause(
10774 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
10775 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
10776 SourceLocation EndLoc) {
10777 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
10778 std::string Values;
10779 Values += "'";
10780 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
10781 Values += "'";
10782 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
10783 << Values << getOpenMPClauseName(OMPC_dist_schedule);
10784 return nullptr;
10785 }
10786 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000010787 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000010788 if (ChunkSize) {
10789 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
10790 !ChunkSize->isInstantiationDependent() &&
10791 !ChunkSize->containsUnexpandedParameterPack()) {
10792 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
10793 ExprResult Val =
10794 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
10795 if (Val.isInvalid())
10796 return nullptr;
10797
10798 ValExpr = Val.get();
10799
10800 // OpenMP [2.7.1, Restrictions]
10801 // chunk_size must be a loop invariant integer expression with a positive
10802 // value.
10803 llvm::APSInt Result;
10804 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
10805 if (Result.isSigned() && !Result.isStrictlyPositive()) {
10806 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
10807 << "dist_schedule" << ChunkSize->getSourceRange();
10808 return nullptr;
10809 }
10810 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev5a3af132016-03-29 08:58:54 +000010811 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
10812 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
10813 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010814 }
10815 }
10816 }
10817
10818 return new (Context)
10819 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000010820 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010821}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000010822
10823OMPClause *Sema::ActOnOpenMPDefaultmapClause(
10824 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
10825 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
10826 SourceLocation KindLoc, SourceLocation EndLoc) {
10827 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
10828 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
10829 Kind != OMPC_DEFAULTMAP_scalar) {
10830 std::string Value;
10831 SourceLocation Loc;
10832 Value += "'";
10833 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
10834 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10835 OMPC_DEFAULTMAP_MODIFIER_tofrom);
10836 Loc = MLoc;
10837 } else {
10838 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10839 OMPC_DEFAULTMAP_scalar);
10840 Loc = KindLoc;
10841 }
10842 Value += "'";
10843 Diag(Loc, diag::err_omp_unexpected_clause_value)
10844 << Value << getOpenMPClauseName(OMPC_defaultmap);
10845 return nullptr;
10846 }
10847
10848 return new (Context)
10849 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
10850}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010851
10852bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
10853 DeclContext *CurLexicalContext = getCurLexicalContext();
10854 if (!CurLexicalContext->isFileContext() &&
10855 !CurLexicalContext->isExternCContext() &&
10856 !CurLexicalContext->isExternCXXContext()) {
10857 Diag(Loc, diag::err_omp_region_not_file_context);
10858 return false;
10859 }
10860 if (IsInOpenMPDeclareTargetContext) {
10861 Diag(Loc, diag::err_omp_enclosed_declare_target);
10862 return false;
10863 }
10864
10865 IsInOpenMPDeclareTargetContext = true;
10866 return true;
10867}
10868
10869void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
10870 assert(IsInOpenMPDeclareTargetContext &&
10871 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
10872
10873 IsInOpenMPDeclareTargetContext = false;
10874}
10875
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010876void
10877Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec,
10878 const DeclarationNameInfo &Id,
10879 OMPDeclareTargetDeclAttr::MapTypeTy MT,
10880 NamedDeclSetType &SameDirectiveDecls) {
10881 LookupResult Lookup(*this, Id, LookupOrdinaryName);
10882 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
10883
10884 if (Lookup.isAmbiguous())
10885 return;
10886 Lookup.suppressDiagnostics();
10887
10888 if (!Lookup.isSingleResult()) {
10889 if (TypoCorrection Corrected =
10890 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
10891 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
10892 CTK_ErrorRecovery)) {
10893 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
10894 << Id.getName());
10895 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
10896 return;
10897 }
10898
10899 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
10900 return;
10901 }
10902
10903 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
10904 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
10905 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
10906 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
10907
10908 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
10909 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
10910 ND->addAttr(A);
10911 if (ASTMutationListener *ML = Context.getASTMutationListener())
10912 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
10913 checkDeclIsAllowedInOpenMPTarget(nullptr, ND);
10914 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
10915 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
10916 << Id.getName();
10917 }
10918 } else
10919 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
10920}
10921
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010922static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
10923 Sema &SemaRef, Decl *D) {
10924 if (!D)
10925 return;
10926 Decl *LD = nullptr;
10927 if (isa<TagDecl>(D)) {
10928 LD = cast<TagDecl>(D)->getDefinition();
10929 } else if (isa<VarDecl>(D)) {
10930 LD = cast<VarDecl>(D)->getDefinition();
10931
10932 // If this is an implicit variable that is legal and we do not need to do
10933 // anything.
10934 if (cast<VarDecl>(D)->isImplicit()) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010935 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
10936 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
10937 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010938 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010939 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010940 return;
10941 }
10942
10943 } else if (isa<FunctionDecl>(D)) {
10944 const FunctionDecl *FD = nullptr;
10945 if (cast<FunctionDecl>(D)->hasBody(FD))
10946 LD = const_cast<FunctionDecl *>(FD);
10947
10948 // If the definition is associated with the current declaration in the
10949 // target region (it can be e.g. a lambda) that is legal and we do not need
10950 // to do anything else.
10951 if (LD == D) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010952 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
10953 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
10954 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010955 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010956 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010957 return;
10958 }
10959 }
10960 if (!LD)
10961 LD = D;
10962 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
10963 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
10964 // Outlined declaration is not declared target.
10965 if (LD->isOutOfLine()) {
10966 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
10967 SemaRef.Diag(SL, diag::note_used_here) << SR;
10968 } else {
10969 DeclContext *DC = LD->getDeclContext();
10970 while (DC) {
10971 if (isa<FunctionDecl>(DC) &&
10972 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
10973 break;
10974 DC = DC->getParent();
10975 }
10976 if (DC)
10977 return;
10978
10979 // Is not declared in target context.
10980 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
10981 SemaRef.Diag(SL, diag::note_used_here) << SR;
10982 }
10983 // Mark decl as declared target to prevent further diagnostic.
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010984 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
10985 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
10986 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010987 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010988 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010989 }
10990}
10991
10992static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
10993 Sema &SemaRef, DSAStackTy *Stack,
10994 ValueDecl *VD) {
10995 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
10996 return true;
10997 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
10998 return false;
10999 return true;
11000}
11001
11002void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
11003 if (!D || D->isInvalidDecl())
11004 return;
11005 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
11006 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
11007 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
11008 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
11009 if (DSAStack->isThreadPrivate(VD)) {
11010 Diag(SL, diag::err_omp_threadprivate_in_target);
11011 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
11012 return;
11013 }
11014 }
11015 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
11016 // Problem if any with var declared with incomplete type will be reported
11017 // as normal, so no need to check it here.
11018 if ((E || !VD->getType()->isIncompleteType()) &&
11019 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
11020 // Mark decl as declared target to prevent further diagnostic.
11021 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011022 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11023 Context, OMPDeclareTargetDeclAttr::MT_To);
11024 VD->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011025 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011026 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011027 }
11028 return;
11029 }
11030 }
11031 if (!E) {
11032 // Checking declaration inside declare target region.
11033 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
11034 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011035 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11036 Context, OMPDeclareTargetDeclAttr::MT_To);
11037 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011038 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011039 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011040 }
11041 return;
11042 }
11043 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
11044}