blob: 57759bcb63bf57c8629a10544a717a41cf612383 [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:
Alexey Bataev9959db52014-05-06 10:08:46 +00001828 llvm_unreachable("OpenMP Directive is not allowed");
1829 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001830 llvm_unreachable("Unknown OpenMP directive");
1831 }
1832}
1833
Alexey Bataev3392d762016-02-16 11:18:12 +00001834static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00001835 Expr *CaptureExpr, bool WithInit,
1836 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001837 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001838 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00001839 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00001840 QualType Ty = Init->getType();
1841 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1842 if (S.getLangOpts().CPlusPlus)
1843 Ty = C.getLValueReferenceType(Ty);
1844 else {
1845 Ty = C.getPointerType(Ty);
1846 ExprResult Res =
1847 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1848 if (!Res.isUsable())
1849 return nullptr;
1850 Init = Res.get();
1851 }
Alexey Bataev61205072016-03-02 04:57:40 +00001852 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001853 }
1854 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001855 if (!WithInit)
1856 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001857 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001858 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1859 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001860 return CED;
1861}
1862
Alexey Bataev61205072016-03-02 04:57:40 +00001863static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1864 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001865 OMPCapturedExprDecl *CD;
1866 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1867 CD = cast<OMPCapturedExprDecl>(VD);
1868 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00001869 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1870 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001871 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00001872 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00001873}
1874
Alexey Bataev5a3af132016-03-29 08:58:54 +00001875static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1876 if (!Ref) {
1877 auto *CD =
1878 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1879 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1880 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1881 CaptureExpr->getExprLoc());
1882 }
1883 ExprResult Res = Ref;
1884 if (!S.getLangOpts().CPlusPlus &&
1885 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1886 Ref->getType()->isPointerType())
1887 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1888 if (!Res.isUsable())
1889 return ExprError();
1890 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00001891}
1892
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001893StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1894 ArrayRef<OMPClause *> Clauses) {
1895 if (!S.isUsable()) {
1896 ActOnCapturedRegionError();
1897 return StmtError();
1898 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001899
1900 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001901 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001902 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001903 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001904 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001905 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001906 Clause->getClauseKind() == OMPC_copyprivate ||
1907 (getLangOpts().OpenMPUseTLS &&
1908 getASTContext().getTargetInfo().isTLSSupported() &&
1909 Clause->getClauseKind() == OMPC_copyin)) {
1910 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001911 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001912 for (auto *VarRef : Clause->children()) {
1913 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001914 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001915 }
1916 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001917 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001918 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001919 // Mark all variables in private list clauses as used in inner region.
1920 // Required for proper codegen of combined directives.
1921 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001922 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001923 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1924 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001925 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1926 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001927 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001928 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1929 if (auto *E = C->getPostUpdateExpr())
1930 MarkDeclarationsReferencedInExpr(E);
1931 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001932 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001933 if (Clause->getClauseKind() == OMPC_schedule)
1934 SC = cast<OMPScheduleClause>(Clause);
1935 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001936 OC = cast<OMPOrderedClause>(Clause);
1937 else if (Clause->getClauseKind() == OMPC_linear)
1938 LCs.push_back(cast<OMPLinearClause>(Clause));
1939 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001940 bool ErrorFound = false;
1941 // OpenMP, 2.7.1 Loop Construct, Restrictions
1942 // The nonmonotonic modifier cannot be specified if an ordered clause is
1943 // specified.
1944 if (SC &&
1945 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1946 SC->getSecondScheduleModifier() ==
1947 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1948 OC) {
1949 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1950 ? SC->getFirstScheduleModifierLoc()
1951 : SC->getSecondScheduleModifierLoc(),
1952 diag::err_omp_schedule_nonmonotonic_ordered)
1953 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1954 ErrorFound = true;
1955 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001956 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1957 for (auto *C : LCs) {
1958 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1959 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1960 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001961 ErrorFound = true;
1962 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001963 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1964 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1965 OC->getNumForLoops()) {
1966 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1967 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1968 ErrorFound = true;
1969 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001970 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001971 ActOnCapturedRegionError();
1972 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001973 }
1974 return ActOnCapturedRegionEnd(S.get());
1975}
1976
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001977static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1978 OpenMPDirectiveKind CurrentRegion,
1979 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001980 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001981 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001982 // Allowed nesting of constructs
1983 // +------------------+-----------------+------------------------------------+
1984 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1985 // +------------------+-----------------+------------------------------------+
1986 // | parallel | parallel | * |
1987 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001988 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001989 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001990 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001991 // | parallel | simd | * |
1992 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001993 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001994 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001995 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001996 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001997 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001998 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001999 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002000 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002001 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002002 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002003 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002004 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002005 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002006 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002007 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002008 // | parallel | target parallel | * |
2009 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002010 // | parallel | target enter | * |
2011 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002012 // | parallel | target exit | * |
2013 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002014 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002015 // | parallel | cancellation | |
2016 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002017 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002018 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002019 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002020 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002021 // +------------------+-----------------+------------------------------------+
2022 // | for | parallel | * |
2023 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002024 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002025 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002026 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002027 // | for | simd | * |
2028 // | for | sections | + |
2029 // | for | section | + |
2030 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002031 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002032 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002033 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002034 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002035 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002036 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002037 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002038 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002039 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002040 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002041 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002042 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002043 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002044 // | for | target parallel | * |
2045 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002046 // | for | target enter | * |
2047 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002048 // | for | target exit | * |
2049 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002050 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002051 // | for | cancellation | |
2052 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002053 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002054 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002055 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002056 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002057 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00002058 // | master | parallel | * |
2059 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002060 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002061 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002062 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00002063 // | master | simd | * |
2064 // | master | sections | + |
2065 // | master | section | + |
2066 // | master | single | + |
2067 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002068 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00002069 // | master |parallel sections| * |
2070 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002071 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002072 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002073 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002074 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002075 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002076 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002077 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002078 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002079 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002080 // | master | target parallel | * |
2081 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002082 // | master | target enter | * |
2083 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002084 // | master | target exit | * |
2085 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002086 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002087 // | master | cancellation | |
2088 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002089 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002090 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002091 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002092 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00002093 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002094 // | critical | parallel | * |
2095 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002096 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002097 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002098 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002099 // | critical | simd | * |
2100 // | critical | sections | + |
2101 // | critical | section | + |
2102 // | critical | single | + |
2103 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002104 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002105 // | critical |parallel sections| * |
2106 // | critical | task | * |
2107 // | critical | taskyield | * |
2108 // | critical | barrier | + |
2109 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002110 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002111 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002112 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002113 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002114 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002115 // | critical | target parallel | * |
2116 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002117 // | critical | target enter | * |
2118 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002119 // | critical | target exit | * |
2120 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002121 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002122 // | critical | cancellation | |
2123 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002124 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002125 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002126 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002127 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002128 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002129 // | simd | parallel | |
2130 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002131 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00002132 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002133 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002134 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002135 // | simd | sections | |
2136 // | simd | section | |
2137 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002138 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002139 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002140 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002141 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002142 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002143 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002144 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002145 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002146 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002147 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002148 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002149 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002150 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002151 // | simd | target parallel | |
2152 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002153 // | simd | target enter | |
2154 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002155 // | simd | target exit | |
2156 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002157 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002158 // | simd | cancellation | |
2159 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002160 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002161 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002162 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002163 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002164 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002165 // | for simd | parallel | |
2166 // | for simd | for | |
2167 // | for simd | for simd | |
2168 // | for simd | master | |
2169 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002170 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002171 // | for simd | sections | |
2172 // | for simd | section | |
2173 // | for simd | single | |
2174 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002175 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002176 // | for simd |parallel sections| |
2177 // | for simd | task | |
2178 // | for simd | taskyield | |
2179 // | for simd | barrier | |
2180 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002181 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002182 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002183 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002184 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002185 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002186 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002187 // | for simd | target parallel | |
2188 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002189 // | for simd | target enter | |
2190 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002191 // | for simd | target exit | |
2192 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002193 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002194 // | for simd | cancellation | |
2195 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002196 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002197 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002198 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002199 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002200 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002201 // | parallel for simd| parallel | |
2202 // | parallel for simd| for | |
2203 // | parallel for simd| for simd | |
2204 // | parallel for simd| master | |
2205 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002206 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002207 // | parallel for simd| sections | |
2208 // | parallel for simd| section | |
2209 // | parallel for simd| single | |
2210 // | parallel for simd| parallel for | |
2211 // | parallel for simd|parallel for simd| |
2212 // | parallel for simd|parallel sections| |
2213 // | parallel for simd| task | |
2214 // | parallel for simd| taskyield | |
2215 // | parallel for simd| barrier | |
2216 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002217 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002218 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002219 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002220 // | parallel for simd| atomic | |
2221 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002222 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002223 // | parallel for simd| target parallel | |
2224 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002225 // | parallel for simd| target enter | |
2226 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002227 // | parallel for simd| target exit | |
2228 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002229 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002230 // | parallel for simd| cancellation | |
2231 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002232 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002233 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002234 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002235 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002236 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002237 // | sections | parallel | * |
2238 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002239 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002240 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002241 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002242 // | sections | simd | * |
2243 // | sections | sections | + |
2244 // | sections | section | * |
2245 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002246 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002247 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002248 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002249 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002250 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002251 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002252 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002253 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002254 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002255 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002256 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002257 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002258 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002259 // | sections | target parallel | * |
2260 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002261 // | sections | target enter | * |
2262 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002263 // | sections | target exit | * |
2264 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002265 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002266 // | sections | cancellation | |
2267 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002268 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002269 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002270 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002271 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002272 // +------------------+-----------------+------------------------------------+
2273 // | section | parallel | * |
2274 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002275 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002276 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002277 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002278 // | section | simd | * |
2279 // | section | sections | + |
2280 // | section | section | + |
2281 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002282 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002283 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002284 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002285 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002286 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002287 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002288 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002289 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002290 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002291 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002292 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002293 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002294 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002295 // | section | target parallel | * |
2296 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002297 // | section | target enter | * |
2298 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002299 // | section | target exit | * |
2300 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002301 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002302 // | section | cancellation | |
2303 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002304 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002305 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002306 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002307 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002308 // +------------------+-----------------+------------------------------------+
2309 // | single | parallel | * |
2310 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002311 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002312 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002313 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002314 // | single | simd | * |
2315 // | single | sections | + |
2316 // | single | section | + |
2317 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002318 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002319 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002320 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002321 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002322 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002323 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002324 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002325 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002326 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002327 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002328 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002329 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002330 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002331 // | single | target parallel | * |
2332 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002333 // | single | target enter | * |
2334 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002335 // | single | target exit | * |
2336 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002337 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002338 // | single | cancellation | |
2339 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002340 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002341 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002342 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002343 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002344 // +------------------+-----------------+------------------------------------+
2345 // | parallel for | parallel | * |
2346 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002347 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002348 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002349 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002350 // | parallel for | simd | * |
2351 // | parallel for | sections | + |
2352 // | parallel for | section | + |
2353 // | parallel for | single | + |
2354 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002355 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002356 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002357 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002358 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002359 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002360 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002361 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002362 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002363 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002364 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002365 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002366 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002367 // | parallel for | target parallel | * |
2368 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002369 // | parallel for | target enter | * |
2370 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002371 // | parallel for | target exit | * |
2372 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002373 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002374 // | parallel for | cancellation | |
2375 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002376 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002377 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002378 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002379 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002380 // +------------------+-----------------+------------------------------------+
2381 // | parallel sections| parallel | * |
2382 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002383 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002384 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002385 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002386 // | parallel sections| simd | * |
2387 // | parallel sections| sections | + |
2388 // | parallel sections| section | * |
2389 // | parallel sections| single | + |
2390 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002391 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002392 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002393 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002394 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002395 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002396 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002397 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002398 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002399 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002400 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002401 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002402 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002403 // | parallel sections| target parallel | * |
2404 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002405 // | parallel sections| target enter | * |
2406 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002407 // | parallel sections| target exit | * |
2408 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002409 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002410 // | parallel sections| cancellation | |
2411 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002412 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002413 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002414 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002415 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002416 // +------------------+-----------------+------------------------------------+
2417 // | task | parallel | * |
2418 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002419 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002420 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002421 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002422 // | task | simd | * |
2423 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002424 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002425 // | task | single | + |
2426 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002427 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002428 // | task |parallel sections| * |
2429 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002430 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002431 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002432 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002433 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002434 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002435 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002436 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002437 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002438 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002439 // | task | target parallel | * |
2440 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002441 // | task | target enter | * |
2442 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002443 // | task | target exit | * |
2444 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002445 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002446 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002447 // | | point | ! |
2448 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002449 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002450 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002451 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002452 // +------------------+-----------------+------------------------------------+
2453 // | ordered | parallel | * |
2454 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002455 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002456 // | ordered | master | * |
2457 // | ordered | critical | * |
2458 // | ordered | simd | * |
2459 // | ordered | sections | + |
2460 // | ordered | section | + |
2461 // | ordered | single | + |
2462 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002463 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002464 // | ordered |parallel sections| * |
2465 // | ordered | task | * |
2466 // | ordered | taskyield | * |
2467 // | ordered | barrier | + |
2468 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002469 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002470 // | ordered | flush | * |
2471 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002472 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002473 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002474 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002475 // | ordered | target parallel | * |
2476 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002477 // | ordered | target enter | * |
2478 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002479 // | ordered | target exit | * |
2480 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002481 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002482 // | ordered | cancellation | |
2483 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002484 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002485 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002486 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002487 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002488 // +------------------+-----------------+------------------------------------+
2489 // | atomic | parallel | |
2490 // | atomic | for | |
2491 // | atomic | for simd | |
2492 // | atomic | master | |
2493 // | atomic | critical | |
2494 // | atomic | simd | |
2495 // | atomic | sections | |
2496 // | atomic | section | |
2497 // | atomic | single | |
2498 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002499 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002500 // | atomic |parallel sections| |
2501 // | atomic | task | |
2502 // | atomic | taskyield | |
2503 // | atomic | barrier | |
2504 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002505 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002506 // | atomic | flush | |
2507 // | atomic | ordered | |
2508 // | atomic | atomic | |
2509 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002510 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002511 // | atomic | target parallel | |
2512 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002513 // | atomic | target enter | |
2514 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002515 // | atomic | target exit | |
2516 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002517 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002518 // | atomic | cancellation | |
2519 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002520 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002521 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002522 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002523 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002524 // +------------------+-----------------+------------------------------------+
2525 // | target | parallel | * |
2526 // | target | for | * |
2527 // | target | for simd | * |
2528 // | target | master | * |
2529 // | target | critical | * |
2530 // | target | simd | * |
2531 // | target | sections | * |
2532 // | target | section | * |
2533 // | target | single | * |
2534 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002535 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002536 // | target |parallel sections| * |
2537 // | target | task | * |
2538 // | target | taskyield | * |
2539 // | target | barrier | * |
2540 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002541 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002542 // | target | flush | * |
2543 // | target | ordered | * |
2544 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002545 // | target | target | |
2546 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002547 // | target | target parallel | |
2548 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002549 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002550 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002551 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002552 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002553 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002554 // | target | cancellation | |
2555 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002556 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002557 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002558 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002559 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002560 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002561 // | target parallel | parallel | * |
2562 // | target parallel | for | * |
2563 // | target parallel | for simd | * |
2564 // | target parallel | master | * |
2565 // | target parallel | critical | * |
2566 // | target parallel | simd | * |
2567 // | target parallel | sections | * |
2568 // | target parallel | section | * |
2569 // | target parallel | single | * |
2570 // | target parallel | parallel for | * |
2571 // | target parallel |parallel for simd| * |
2572 // | target parallel |parallel sections| * |
2573 // | target parallel | task | * |
2574 // | target parallel | taskyield | * |
2575 // | target parallel | barrier | * |
2576 // | target parallel | taskwait | * |
2577 // | target parallel | taskgroup | * |
2578 // | target parallel | flush | * |
2579 // | target parallel | ordered | * |
2580 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002581 // | target parallel | target | |
2582 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002583 // | target parallel | target parallel | |
2584 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002585 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002586 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002587 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002588 // | | data | |
2589 // | target parallel | teams | |
2590 // | target parallel | cancellation | |
2591 // | | point | ! |
2592 // | target parallel | cancel | ! |
2593 // | target parallel | taskloop | * |
2594 // | target parallel | taskloop simd | * |
2595 // | target parallel | distribute | |
2596 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002597 // | target parallel | parallel | * |
2598 // | for | | |
2599 // | target parallel | for | * |
2600 // | for | | |
2601 // | target parallel | for simd | * |
2602 // | for | | |
2603 // | target parallel | master | * |
2604 // | for | | |
2605 // | target parallel | critical | * |
2606 // | for | | |
2607 // | target parallel | simd | * |
2608 // | for | | |
2609 // | target parallel | sections | * |
2610 // | for | | |
2611 // | target parallel | section | * |
2612 // | for | | |
2613 // | target parallel | single | * |
2614 // | for | | |
2615 // | target parallel | parallel for | * |
2616 // | for | | |
2617 // | target parallel |parallel for simd| * |
2618 // | for | | |
2619 // | target parallel |parallel sections| * |
2620 // | for | | |
2621 // | target parallel | task | * |
2622 // | for | | |
2623 // | target parallel | taskyield | * |
2624 // | for | | |
2625 // | target parallel | barrier | * |
2626 // | for | | |
2627 // | target parallel | taskwait | * |
2628 // | for | | |
2629 // | target parallel | taskgroup | * |
2630 // | for | | |
2631 // | target parallel | flush | * |
2632 // | for | | |
2633 // | target parallel | ordered | * |
2634 // | for | | |
2635 // | target parallel | atomic | * |
2636 // | for | | |
2637 // | target parallel | target | |
2638 // | for | | |
2639 // | target parallel | target parallel | |
2640 // | for | | |
2641 // | target parallel | target parallel | |
2642 // | for | for | |
2643 // | target parallel | target enter | |
2644 // | for | data | |
2645 // | target parallel | target exit | |
2646 // | for | data | |
2647 // | target parallel | teams | |
2648 // | for | | |
2649 // | target parallel | cancellation | |
2650 // | for | point | ! |
2651 // | target parallel | cancel | ! |
2652 // | for | | |
2653 // | target parallel | taskloop | * |
2654 // | for | | |
2655 // | target parallel | taskloop simd | * |
2656 // | for | | |
2657 // | target parallel | distribute | |
2658 // | for | | |
2659 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002660 // | teams | parallel | * |
2661 // | teams | for | + |
2662 // | teams | for simd | + |
2663 // | teams | master | + |
2664 // | teams | critical | + |
2665 // | teams | simd | + |
2666 // | teams | sections | + |
2667 // | teams | section | + |
2668 // | teams | single | + |
2669 // | teams | parallel for | * |
2670 // | teams |parallel for simd| * |
2671 // | teams |parallel sections| * |
2672 // | teams | task | + |
2673 // | teams | taskyield | + |
2674 // | teams | barrier | + |
2675 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002676 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002677 // | teams | flush | + |
2678 // | teams | ordered | + |
2679 // | teams | atomic | + |
2680 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002681 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002682 // | teams | target parallel | + |
2683 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002684 // | teams | target enter | + |
2685 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002686 // | teams | target exit | + |
2687 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002688 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002689 // | teams | cancellation | |
2690 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002691 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002692 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002693 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002694 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002695 // +------------------+-----------------+------------------------------------+
2696 // | taskloop | parallel | * |
2697 // | taskloop | for | + |
2698 // | taskloop | for simd | + |
2699 // | taskloop | master | + |
2700 // | taskloop | critical | * |
2701 // | taskloop | simd | * |
2702 // | taskloop | sections | + |
2703 // | taskloop | section | + |
2704 // | taskloop | single | + |
2705 // | taskloop | parallel for | * |
2706 // | taskloop |parallel for simd| * |
2707 // | taskloop |parallel sections| * |
2708 // | taskloop | task | * |
2709 // | taskloop | taskyield | * |
2710 // | taskloop | barrier | + |
2711 // | taskloop | taskwait | * |
2712 // | taskloop | taskgroup | * |
2713 // | taskloop | flush | * |
2714 // | taskloop | ordered | + |
2715 // | taskloop | atomic | * |
2716 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002717 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002718 // | taskloop | target parallel | * |
2719 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002720 // | taskloop | target enter | * |
2721 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002722 // | taskloop | target exit | * |
2723 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002724 // | taskloop | teams | + |
2725 // | taskloop | cancellation | |
2726 // | | point | |
2727 // | taskloop | cancel | |
2728 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002729 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002730 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002731 // | taskloop simd | parallel | |
2732 // | taskloop simd | for | |
2733 // | taskloop simd | for simd | |
2734 // | taskloop simd | master | |
2735 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002736 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002737 // | taskloop simd | sections | |
2738 // | taskloop simd | section | |
2739 // | taskloop simd | single | |
2740 // | taskloop simd | parallel for | |
2741 // | taskloop simd |parallel for simd| |
2742 // | taskloop simd |parallel sections| |
2743 // | taskloop simd | task | |
2744 // | taskloop simd | taskyield | |
2745 // | taskloop simd | barrier | |
2746 // | taskloop simd | taskwait | |
2747 // | taskloop simd | taskgroup | |
2748 // | taskloop simd | flush | |
2749 // | taskloop simd | ordered | + (with simd clause) |
2750 // | taskloop simd | atomic | |
2751 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002752 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002753 // | taskloop simd | target parallel | |
2754 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002755 // | taskloop simd | target enter | |
2756 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002757 // | taskloop simd | target exit | |
2758 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002759 // | taskloop simd | teams | |
2760 // | taskloop simd | cancellation | |
2761 // | | point | |
2762 // | taskloop simd | cancel | |
2763 // | taskloop simd | taskloop | |
2764 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002765 // | taskloop simd | distribute | |
2766 // +------------------+-----------------+------------------------------------+
2767 // | distribute | parallel | * |
2768 // | distribute | for | * |
2769 // | distribute | for simd | * |
2770 // | distribute | master | * |
2771 // | distribute | critical | * |
2772 // | distribute | simd | * |
2773 // | distribute | sections | * |
2774 // | distribute | section | * |
2775 // | distribute | single | * |
2776 // | distribute | parallel for | * |
2777 // | distribute |parallel for simd| * |
2778 // | distribute |parallel sections| * |
2779 // | distribute | task | * |
2780 // | distribute | taskyield | * |
2781 // | distribute | barrier | * |
2782 // | distribute | taskwait | * |
2783 // | distribute | taskgroup | * |
2784 // | distribute | flush | * |
2785 // | distribute | ordered | + |
2786 // | distribute | atomic | * |
2787 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002788 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002789 // | distribute | target parallel | |
2790 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002791 // | distribute | target enter | |
2792 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002793 // | distribute | target exit | |
2794 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002795 // | distribute | teams | |
2796 // | distribute | cancellation | + |
2797 // | | point | |
2798 // | distribute | cancel | + |
2799 // | distribute | taskloop | * |
2800 // | distribute | taskloop simd | * |
2801 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002802 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002803 if (Stack->getCurScope()) {
2804 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002805 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002806 bool NestingProhibited = false;
2807 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002808 enum {
2809 NoRecommend,
2810 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002811 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002812 ShouldBeInTargetRegion,
2813 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002814 } Recommend = NoRecommend;
Alexey Bataev1f092212016-02-02 04:59:52 +00002815 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered &&
2816 CurrentRegion != OMPD_simd) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002817 // OpenMP [2.16, Nesting of Regions]
2818 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002819 // OpenMP [2.8.1,simd Construct, Restrictions]
2820 // An ordered construct with the simd clause is the only OpenMP construct
2821 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002822 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2823 return true;
2824 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002825 if (ParentRegion == OMPD_atomic) {
2826 // OpenMP [2.16, Nesting of Regions]
2827 // OpenMP constructs may not be nested inside an atomic region.
2828 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2829 return true;
2830 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002831 if (CurrentRegion == OMPD_section) {
2832 // OpenMP [2.7.2, sections Construct, Restrictions]
2833 // Orphaned section directives are prohibited. That is, the section
2834 // directives must appear within the sections construct and must not be
2835 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002836 if (ParentRegion != OMPD_sections &&
2837 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002838 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2839 << (ParentRegion != OMPD_unknown)
2840 << getOpenMPDirectiveName(ParentRegion);
2841 return true;
2842 }
2843 return false;
2844 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002845 // Allow some constructs to be orphaned (they could be used in functions,
2846 // called from OpenMP regions with the required preconditions).
2847 if (ParentRegion == OMPD_unknown)
2848 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002849 if (CurrentRegion == OMPD_cancellation_point ||
2850 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002851 // OpenMP [2.16, Nesting of Regions]
2852 // A cancellation point construct for which construct-type-clause is
2853 // taskgroup must be nested inside a task construct. A cancellation
2854 // point construct for which construct-type-clause is not taskgroup must
2855 // be closely nested inside an OpenMP construct that matches the type
2856 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002857 // A cancel construct for which construct-type-clause is taskgroup must be
2858 // nested inside a task construct. A cancel construct for which
2859 // construct-type-clause is not taskgroup must be closely nested inside an
2860 // OpenMP construct that matches the type specified in
2861 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002862 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002863 !((CancelRegion == OMPD_parallel &&
2864 (ParentRegion == OMPD_parallel ||
2865 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002866 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002867 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2868 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002869 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2870 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002871 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2872 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002873 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002874 // OpenMP [2.16, Nesting of Regions]
2875 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002876 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002877 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002878 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002879 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2880 // OpenMP [2.16, Nesting of Regions]
2881 // A critical region may not be nested (closely or otherwise) inside a
2882 // critical region with the same name. Note that this restriction is not
2883 // sufficient to prevent deadlock.
2884 SourceLocation PreviousCriticalLoc;
2885 bool DeadLock =
2886 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2887 OpenMPDirectiveKind K,
2888 const DeclarationNameInfo &DNI,
2889 SourceLocation Loc)
2890 ->bool {
2891 if (K == OMPD_critical &&
2892 DNI.getName() == CurrentName.getName()) {
2893 PreviousCriticalLoc = Loc;
2894 return true;
2895 } else
2896 return false;
2897 },
2898 false /* skip top directive */);
2899 if (DeadLock) {
2900 SemaRef.Diag(StartLoc,
2901 diag::err_omp_prohibited_region_critical_same_name)
2902 << CurrentName.getName();
2903 if (PreviousCriticalLoc.isValid())
2904 SemaRef.Diag(PreviousCriticalLoc,
2905 diag::note_omp_previous_critical_region);
2906 return true;
2907 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002908 } else if (CurrentRegion == OMPD_barrier) {
2909 // OpenMP [2.16, Nesting of Regions]
2910 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002911 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002912 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2913 isOpenMPTaskingDirective(ParentRegion) ||
2914 ParentRegion == OMPD_master ||
2915 ParentRegion == OMPD_critical ||
2916 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00002917 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002918 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002919 // OpenMP [2.16, Nesting of Regions]
2920 // A worksharing region may not be closely nested inside a worksharing,
2921 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002922 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2923 isOpenMPTaskingDirective(ParentRegion) ||
2924 ParentRegion == OMPD_master ||
2925 ParentRegion == OMPD_critical ||
2926 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002927 Recommend = ShouldBeInParallelRegion;
2928 } else if (CurrentRegion == OMPD_ordered) {
2929 // OpenMP [2.16, Nesting of Regions]
2930 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002931 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002932 // An ordered region must be closely nested inside a loop region (or
2933 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002934 // OpenMP [2.8.1,simd Construct, Restrictions]
2935 // An ordered construct with the simd clause is the only OpenMP construct
2936 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002937 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002938 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002939 !(isOpenMPSimdDirective(ParentRegion) ||
2940 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002941 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002942 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2943 // OpenMP [2.16, Nesting of Regions]
2944 // If specified, a teams construct must be contained within a target
2945 // construct.
2946 NestingProhibited = ParentRegion != OMPD_target;
2947 Recommend = ShouldBeInTargetRegion;
2948 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2949 }
2950 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2951 // OpenMP [2.16, Nesting of Regions]
2952 // distribute, parallel, parallel sections, parallel workshare, and the
2953 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2954 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002955 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2956 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002957 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002958 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002959 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2960 // OpenMP 4.5 [2.17 Nesting of Regions]
2961 // The region associated with the distribute construct must be strictly
2962 // nested inside a teams region
2963 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2964 Recommend = ShouldBeInTeamsRegion;
2965 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002966 if (!NestingProhibited &&
2967 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2968 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2969 // OpenMP 4.5 [2.17 Nesting of Regions]
2970 // If a target, target update, target data, target enter data, or
2971 // target exit data construct is encountered during execution of a
2972 // target region, the behavior is unspecified.
2973 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002974 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
2975 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002976 if (isOpenMPTargetExecutionDirective(K)) {
2977 OffendingRegion = K;
2978 return true;
2979 } else
2980 return false;
2981 },
2982 false /* don't skip top directive */);
2983 CloseNesting = false;
2984 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002985 if (NestingProhibited) {
2986 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002987 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2988 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002989 return true;
2990 }
2991 }
2992 return false;
2993}
2994
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002995static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2996 ArrayRef<OMPClause *> Clauses,
2997 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2998 bool ErrorFound = false;
2999 unsigned NamedModifiersNumber = 0;
3000 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3001 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003002 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003003 for (const auto *C : Clauses) {
3004 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3005 // At most one if clause without a directive-name-modifier can appear on
3006 // the directive.
3007 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3008 if (FoundNameModifiers[CurNM]) {
3009 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
3010 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3011 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3012 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003013 } else if (CurNM != OMPD_unknown) {
3014 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003015 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003016 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003017 FoundNameModifiers[CurNM] = IC;
3018 if (CurNM == OMPD_unknown)
3019 continue;
3020 // Check if the specified name modifier is allowed for the current
3021 // directive.
3022 // At most one if clause with the particular directive-name-modifier can
3023 // appear on the directive.
3024 bool MatchFound = false;
3025 for (auto NM : AllowedNameModifiers) {
3026 if (CurNM == NM) {
3027 MatchFound = true;
3028 break;
3029 }
3030 }
3031 if (!MatchFound) {
3032 S.Diag(IC->getNameModifierLoc(),
3033 diag::err_omp_wrong_if_directive_name_modifier)
3034 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3035 ErrorFound = true;
3036 }
3037 }
3038 }
3039 // If any if clause on the directive includes a directive-name-modifier then
3040 // all if clauses on the directive must include a directive-name-modifier.
3041 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3042 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3043 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
3044 diag::err_omp_no_more_if_clause);
3045 } else {
3046 std::string Values;
3047 std::string Sep(", ");
3048 unsigned AllowedCnt = 0;
3049 unsigned TotalAllowedNum =
3050 AllowedNameModifiers.size() - NamedModifiersNumber;
3051 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3052 ++Cnt) {
3053 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3054 if (!FoundNameModifiers[NM]) {
3055 Values += "'";
3056 Values += getOpenMPDirectiveName(NM);
3057 Values += "'";
3058 if (AllowedCnt + 2 == TotalAllowedNum)
3059 Values += " or ";
3060 else if (AllowedCnt + 1 != TotalAllowedNum)
3061 Values += Sep;
3062 ++AllowedCnt;
3063 }
3064 }
3065 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
3066 diag::err_omp_unnamed_if_clause)
3067 << (TotalAllowedNum > 1) << Values;
3068 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00003069 for (auto Loc : NameModifierLoc) {
3070 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3071 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003072 ErrorFound = true;
3073 }
3074 return ErrorFound;
3075}
3076
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003077StmtResult Sema::ActOnOpenMPExecutableDirective(
3078 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3079 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3080 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003081 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003082 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3083 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003084 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003085
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003086 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003087 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003088 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003089 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00003090 if (AStmt) {
3091 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3092
3093 // Check default data sharing attributes for referenced variables.
3094 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3095 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
3096 if (DSAChecker.isErrorFound())
3097 return StmtError();
3098 // Generate list of implicitly defined firstprivate variables.
3099 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003100
3101 if (!DSAChecker.getImplicitFirstprivate().empty()) {
3102 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3103 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
3104 SourceLocation(), SourceLocation())) {
3105 ClausesWithImplicit.push_back(Implicit);
3106 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3107 DSAChecker.getImplicitFirstprivate().size();
3108 } else
3109 ErrorFound = true;
3110 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003111 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003112
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003113 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003114 switch (Kind) {
3115 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003116 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3117 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003118 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003119 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003120 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003121 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3122 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003123 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003124 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003125 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3126 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003127 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003128 case OMPD_for_simd:
3129 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3130 EndLoc, VarsWithInheritedDSA);
3131 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003132 case OMPD_sections:
3133 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3134 EndLoc);
3135 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003136 case OMPD_section:
3137 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003138 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003139 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3140 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003141 case OMPD_single:
3142 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3143 EndLoc);
3144 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003145 case OMPD_master:
3146 assert(ClausesWithImplicit.empty() &&
3147 "No clauses are allowed for 'omp master' directive");
3148 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3149 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003150 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003151 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3152 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003153 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003154 case OMPD_parallel_for:
3155 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3156 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003157 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003158 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003159 case OMPD_parallel_for_simd:
3160 Res = ActOnOpenMPParallelForSimdDirective(
3161 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003162 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003163 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003164 case OMPD_parallel_sections:
3165 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3166 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003167 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003168 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003169 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003170 Res =
3171 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003172 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003173 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003174 case OMPD_taskyield:
3175 assert(ClausesWithImplicit.empty() &&
3176 "No clauses are allowed for 'omp taskyield' directive");
3177 assert(AStmt == nullptr &&
3178 "No associated statement allowed for 'omp taskyield' directive");
3179 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3180 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003181 case OMPD_barrier:
3182 assert(ClausesWithImplicit.empty() &&
3183 "No clauses are allowed for 'omp barrier' directive");
3184 assert(AStmt == nullptr &&
3185 "No associated statement allowed for 'omp barrier' directive");
3186 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3187 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003188 case OMPD_taskwait:
3189 assert(ClausesWithImplicit.empty() &&
3190 "No clauses are allowed for 'omp taskwait' directive");
3191 assert(AStmt == nullptr &&
3192 "No associated statement allowed for 'omp taskwait' directive");
3193 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3194 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003195 case OMPD_taskgroup:
3196 assert(ClausesWithImplicit.empty() &&
3197 "No clauses are allowed for 'omp taskgroup' directive");
3198 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3199 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003200 case OMPD_flush:
3201 assert(AStmt == nullptr &&
3202 "No associated statement allowed for 'omp flush' directive");
3203 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3204 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003205 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003206 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3207 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003208 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003209 case OMPD_atomic:
3210 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3211 EndLoc);
3212 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003213 case OMPD_teams:
3214 Res =
3215 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3216 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003217 case OMPD_target:
3218 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3219 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003220 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003221 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003222 case OMPD_target_parallel:
3223 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3224 StartLoc, EndLoc);
3225 AllowedNameModifiers.push_back(OMPD_target);
3226 AllowedNameModifiers.push_back(OMPD_parallel);
3227 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003228 case OMPD_target_parallel_for:
3229 Res = ActOnOpenMPTargetParallelForDirective(
3230 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3231 AllowedNameModifiers.push_back(OMPD_target);
3232 AllowedNameModifiers.push_back(OMPD_parallel);
3233 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003234 case OMPD_cancellation_point:
3235 assert(ClausesWithImplicit.empty() &&
3236 "No clauses are allowed for 'omp cancellation point' directive");
3237 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3238 "cancellation point' directive");
3239 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3240 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003241 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003242 assert(AStmt == nullptr &&
3243 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003244 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3245 CancelRegion);
3246 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003247 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003248 case OMPD_target_data:
3249 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3250 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003251 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003252 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003253 case OMPD_target_enter_data:
3254 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3255 EndLoc);
3256 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3257 break;
Samuel Antao72590762016-01-19 20:04:50 +00003258 case OMPD_target_exit_data:
3259 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3260 EndLoc);
3261 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3262 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003263 case OMPD_taskloop:
3264 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3265 EndLoc, VarsWithInheritedDSA);
3266 AllowedNameModifiers.push_back(OMPD_taskloop);
3267 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003268 case OMPD_taskloop_simd:
3269 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3270 EndLoc, VarsWithInheritedDSA);
3271 AllowedNameModifiers.push_back(OMPD_taskloop);
3272 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003273 case OMPD_distribute:
3274 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3275 EndLoc, VarsWithInheritedDSA);
3276 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003277 case OMPD_declare_target:
3278 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003279 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003280 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003281 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003282 llvm_unreachable("OpenMP Directive is not allowed");
3283 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003284 llvm_unreachable("Unknown OpenMP directive");
3285 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003286
Alexey Bataev4acb8592014-07-07 13:01:15 +00003287 for (auto P : VarsWithInheritedDSA) {
3288 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3289 << P.first << P.second->getSourceRange();
3290 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003291 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3292
3293 if (!AllowedNameModifiers.empty())
3294 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3295 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003296
Alexey Bataeved09d242014-05-28 05:53:51 +00003297 if (ErrorFound)
3298 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003299 return Res;
3300}
3301
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003302Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3303 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003304 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003305 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3306 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003307 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003308 assert(Linears.size() == LinModifiers.size());
3309 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003310 if (!DG || DG.get().isNull())
3311 return DeclGroupPtrTy();
3312
3313 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003314 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003315 return DG;
3316 }
3317 auto *ADecl = DG.get().getSingleDecl();
3318 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3319 ADecl = FTD->getTemplatedDecl();
3320
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003321 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3322 if (!FD) {
3323 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003324 return DeclGroupPtrTy();
3325 }
3326
Alexey Bataev2af33e32016-04-07 12:45:37 +00003327 // OpenMP [2.8.2, declare simd construct, Description]
3328 // The parameter of the simdlen clause must be a constant positive integer
3329 // expression.
3330 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003331 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003332 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003333 // OpenMP [2.8.2, declare simd construct, Description]
3334 // The special this pointer can be used as if was one of the arguments to the
3335 // function in any of the linear, aligned, or uniform clauses.
3336 // The uniform clause declares one or more arguments to have an invariant
3337 // value for all concurrent invocations of the function in the execution of a
3338 // single SIMD loop.
Alexey Bataevecba70f2016-04-12 11:02:11 +00003339 llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3340 Expr *UniformedLinearThis = nullptr;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003341 for (auto *E : Uniforms) {
3342 E = E->IgnoreParenImpCasts();
3343 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3344 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3345 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3346 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003347 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3348 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003349 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003350 }
3351 if (isa<CXXThisExpr>(E)) {
3352 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003353 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003354 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003355 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3356 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003357 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003358 // OpenMP [2.8.2, declare simd construct, Description]
3359 // The aligned clause declares that the object to which each list item points
3360 // is aligned to the number of bytes expressed in the optional parameter of
3361 // the aligned clause.
3362 // The special this pointer can be used as if was one of the arguments to the
3363 // function in any of the linear, aligned, or uniform clauses.
3364 // The type of list items appearing in the aligned clause must be array,
3365 // pointer, reference to array, or reference to pointer.
3366 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3367 Expr *AlignedThis = nullptr;
3368 for (auto *E : Aligneds) {
3369 E = E->IgnoreParenImpCasts();
3370 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3371 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3372 auto *CanonPVD = PVD->getCanonicalDecl();
3373 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3374 FD->getParamDecl(PVD->getFunctionScopeIndex())
3375 ->getCanonicalDecl() == CanonPVD) {
3376 // OpenMP [2.8.1, simd construct, Restrictions]
3377 // A list-item cannot appear in more than one aligned clause.
3378 if (AlignedArgs.count(CanonPVD) > 0) {
3379 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3380 << 1 << E->getSourceRange();
3381 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3382 diag::note_omp_explicit_dsa)
3383 << getOpenMPClauseName(OMPC_aligned);
3384 continue;
3385 }
3386 AlignedArgs[CanonPVD] = E;
3387 QualType QTy = PVD->getType()
3388 .getNonReferenceType()
3389 .getUnqualifiedType()
3390 .getCanonicalType();
3391 const Type *Ty = QTy.getTypePtrOrNull();
3392 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3393 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3394 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3395 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3396 }
3397 continue;
3398 }
3399 }
3400 if (isa<CXXThisExpr>(E)) {
3401 if (AlignedThis) {
3402 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3403 << 2 << E->getSourceRange();
3404 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3405 << getOpenMPClauseName(OMPC_aligned);
3406 }
3407 AlignedThis = E;
3408 continue;
3409 }
3410 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3411 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3412 }
3413 // The optional parameter of the aligned clause, alignment, must be a constant
3414 // positive integer expression. If no optional parameter is specified,
3415 // implementation-defined default alignments for SIMD instructions on the
3416 // target platforms are assumed.
3417 SmallVector<Expr *, 4> NewAligns;
3418 for (auto *E : Alignments) {
3419 ExprResult Align;
3420 if (E)
3421 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3422 NewAligns.push_back(Align.get());
3423 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003424 // OpenMP [2.8.2, declare simd construct, Description]
3425 // The linear clause declares one or more list items to be private to a SIMD
3426 // lane and to have a linear relationship with respect to the iteration space
3427 // of a loop.
3428 // The special this pointer can be used as if was one of the arguments to the
3429 // function in any of the linear, aligned, or uniform clauses.
3430 // When a linear-step expression is specified in a linear clause it must be
3431 // either a constant integer expression or an integer-typed parameter that is
3432 // specified in a uniform clause on the directive.
3433 llvm::DenseMap<Decl *, Expr *> LinearArgs;
3434 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3435 auto MI = LinModifiers.begin();
3436 for (auto *E : Linears) {
3437 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3438 ++MI;
3439 E = E->IgnoreParenImpCasts();
3440 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3441 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3442 auto *CanonPVD = PVD->getCanonicalDecl();
3443 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3444 FD->getParamDecl(PVD->getFunctionScopeIndex())
3445 ->getCanonicalDecl() == CanonPVD) {
3446 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3447 // A list-item cannot appear in more than one linear clause.
3448 if (LinearArgs.count(CanonPVD) > 0) {
3449 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3450 << getOpenMPClauseName(OMPC_linear)
3451 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3452 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3453 diag::note_omp_explicit_dsa)
3454 << getOpenMPClauseName(OMPC_linear);
3455 continue;
3456 }
3457 // Each argument can appear in at most one uniform or linear clause.
3458 if (UniformedArgs.count(CanonPVD) > 0) {
3459 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3460 << getOpenMPClauseName(OMPC_linear)
3461 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3462 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3463 diag::note_omp_explicit_dsa)
3464 << getOpenMPClauseName(OMPC_uniform);
3465 continue;
3466 }
3467 LinearArgs[CanonPVD] = E;
3468 if (E->isValueDependent() || E->isTypeDependent() ||
3469 E->isInstantiationDependent() ||
3470 E->containsUnexpandedParameterPack())
3471 continue;
3472 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3473 PVD->getOriginalType());
3474 continue;
3475 }
3476 }
3477 if (isa<CXXThisExpr>(E)) {
3478 if (UniformedLinearThis) {
3479 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3480 << getOpenMPClauseName(OMPC_linear)
3481 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3482 << E->getSourceRange();
3483 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3484 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3485 : OMPC_linear);
3486 continue;
3487 }
3488 UniformedLinearThis = E;
3489 if (E->isValueDependent() || E->isTypeDependent() ||
3490 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3491 continue;
3492 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3493 E->getType());
3494 continue;
3495 }
3496 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3497 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3498 }
3499 Expr *Step = nullptr;
3500 Expr *NewStep = nullptr;
3501 SmallVector<Expr *, 4> NewSteps;
3502 for (auto *E : Steps) {
3503 // Skip the same step expression, it was checked already.
3504 if (Step == E || !E) {
3505 NewSteps.push_back(E ? NewStep : nullptr);
3506 continue;
3507 }
3508 Step = E;
3509 if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
3510 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3511 auto *CanonPVD = PVD->getCanonicalDecl();
3512 if (UniformedArgs.count(CanonPVD) == 0) {
3513 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3514 << Step->getSourceRange();
3515 } else if (E->isValueDependent() || E->isTypeDependent() ||
3516 E->isInstantiationDependent() ||
3517 E->containsUnexpandedParameterPack() ||
3518 CanonPVD->getType()->hasIntegerRepresentation())
3519 NewSteps.push_back(Step);
3520 else {
3521 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3522 << Step->getSourceRange();
3523 }
3524 continue;
3525 }
3526 NewStep = Step;
3527 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3528 !Step->isInstantiationDependent() &&
3529 !Step->containsUnexpandedParameterPack()) {
3530 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3531 .get();
3532 if (NewStep)
3533 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3534 }
3535 NewSteps.push_back(NewStep);
3536 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003537 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3538 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003539 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003540 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3541 const_cast<Expr **>(Linears.data()), Linears.size(),
3542 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3543 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003544 ADecl->addAttr(NewAttr);
3545 return ConvertDeclToDeclGroup(ADecl);
3546}
3547
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003548StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3549 Stmt *AStmt,
3550 SourceLocation StartLoc,
3551 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003552 if (!AStmt)
3553 return StmtError();
3554
Alexey Bataev9959db52014-05-06 10:08:46 +00003555 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3556 // 1.2.2 OpenMP Language Terminology
3557 // Structured block - An executable statement with a single entry at the
3558 // top and a single exit at the bottom.
3559 // The point of exit cannot be a branch out of the structured block.
3560 // longjmp() and throw() must not violate the entry/exit criteria.
3561 CS->getCapturedDecl()->setNothrow();
3562
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003563 getCurFunction()->setHasBranchProtectedScope();
3564
Alexey Bataev25e5b442015-09-15 12:52:43 +00003565 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3566 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003567}
3568
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003569namespace {
3570/// \brief Helper class for checking canonical form of the OpenMP loops and
3571/// extracting iteration space of each loop in the loop nest, that will be used
3572/// for IR generation.
3573class OpenMPIterationSpaceChecker {
3574 /// \brief Reference to Sema.
3575 Sema &SemaRef;
3576 /// \brief A location for diagnostics (when there is no some better location).
3577 SourceLocation DefaultLoc;
3578 /// \brief A location for diagnostics (when increment is not compatible).
3579 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003580 /// \brief A source location for referring to loop init later.
3581 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003582 /// \brief A source location for referring to condition later.
3583 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003584 /// \brief A source location for referring to increment later.
3585 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003586 /// \brief Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003587 ValueDecl *LCDecl = nullptr;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003588 /// \brief Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003589 Expr *LCRef = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003590 /// \brief Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003591 Expr *LB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003592 /// \brief Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003593 Expr *UB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003594 /// \brief Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003595 Expr *Step = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003596 /// \brief This flag is true when condition is one of:
3597 /// Var < UB
3598 /// Var <= UB
3599 /// UB > Var
3600 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003601 bool TestIsLessOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003602 /// \brief This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003603 bool TestIsStrictOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003604 /// \brief This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003605 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003606
3607public:
3608 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003609 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003610 /// \brief Check init-expr for canonical loop form and save loop counter
3611 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003612 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003613 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3614 /// for less/greater and for strict/non-strict comparison.
3615 bool CheckCond(Expr *S);
3616 /// \brief Check incr-expr for canonical loop form and return true if it
3617 /// does not conform, otherwise save loop step (#Step).
3618 bool CheckInc(Expr *S);
3619 /// \brief Return the loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003620 ValueDecl *GetLoopDecl() const { return LCDecl; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003621 /// \brief Return the reference expression to loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003622 Expr *GetLoopDeclRefExpr() const { return LCRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003623 /// \brief Source range of the loop init.
3624 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3625 /// \brief Source range of the loop condition.
3626 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3627 /// \brief Source range of the loop increment.
3628 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3629 /// \brief True if the step should be subtracted.
3630 bool ShouldSubtractStep() const { return SubtractStep; }
3631 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003632 Expr *
3633 BuildNumIterations(Scope *S, const bool LimitedType,
3634 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003635 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003636 Expr *BuildPreCond(Scope *S, Expr *Cond,
3637 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003638 /// \brief Build reference expression to the counter be used for codegen.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00003639 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
3640 DSAStackTy &DSA) const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003641 /// \brief Build reference expression to the private counter be used for
3642 /// codegen.
3643 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003644 /// \brief Build initization of the counter be used for codegen.
3645 Expr *BuildCounterInit() const;
3646 /// \brief Build step of the counter be used for codegen.
3647 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003648 /// \brief Return true if any expression is dependent.
3649 bool Dependent() const;
3650
3651private:
3652 /// \brief Check the right-hand side of an assignment in the increment
3653 /// expression.
3654 bool CheckIncRHS(Expr *RHS);
3655 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003656 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003657 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003658 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003659 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003660 /// \brief Helper to set loop increment.
3661 bool SetStep(Expr *NewStep, bool Subtract);
3662};
3663
3664bool OpenMPIterationSpaceChecker::Dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003665 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003666 assert(!LB && !UB && !Step);
3667 return false;
3668 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003669 return LCDecl->getType()->isDependentType() ||
3670 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3671 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003672}
3673
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003674static Expr *getExprAsWritten(Expr *E) {
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003675 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3676 E = ExprTemp->getSubExpr();
3677
3678 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3679 E = MTE->GetTemporaryExpr();
3680
3681 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3682 E = Binder->getSubExpr();
3683
3684 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3685 E = ICE->getSubExprAsWritten();
3686 return E->IgnoreParens();
3687}
3688
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003689bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
3690 Expr *NewLCRefExpr,
3691 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003692 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003693 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003694 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003695 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003696 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003697 LCDecl = getCanonicalDecl(NewLCDecl);
3698 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003699 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3700 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003701 if ((Ctor->isCopyOrMoveConstructor() ||
3702 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3703 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003704 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003705 LB = NewLB;
3706 return false;
3707}
3708
3709bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003710 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003711 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003712 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
3713 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003714 if (!NewUB)
3715 return true;
3716 UB = NewUB;
3717 TestIsLessOp = LessOp;
3718 TestIsStrictOp = StrictOp;
3719 ConditionSrcRange = SR;
3720 ConditionLoc = SL;
3721 return false;
3722}
3723
3724bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3725 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003726 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003727 if (!NewStep)
3728 return true;
3729 if (!NewStep->isValueDependent()) {
3730 // Check that the step is integer expression.
3731 SourceLocation StepLoc = NewStep->getLocStart();
3732 ExprResult Val =
3733 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3734 if (Val.isInvalid())
3735 return true;
3736 NewStep = Val.get();
3737
3738 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3739 // If test-expr is of form var relational-op b and relational-op is < or
3740 // <= then incr-expr must cause var to increase on each iteration of the
3741 // loop. If test-expr is of form var relational-op b and relational-op is
3742 // > or >= then incr-expr must cause var to decrease on each iteration of
3743 // the loop.
3744 // If test-expr is of form b relational-op var and relational-op is < or
3745 // <= then incr-expr must cause var to decrease on each iteration of the
3746 // loop. If test-expr is of form b relational-op var and relational-op is
3747 // > or >= then incr-expr must cause var to increase on each iteration of
3748 // the loop.
3749 llvm::APSInt Result;
3750 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3751 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3752 bool IsConstNeg =
3753 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003754 bool IsConstPos =
3755 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003756 bool IsConstZero = IsConstant && !Result.getBoolValue();
3757 if (UB && (IsConstZero ||
3758 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003759 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003760 SemaRef.Diag(NewStep->getExprLoc(),
3761 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003762 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003763 SemaRef.Diag(ConditionLoc,
3764 diag::note_omp_loop_cond_requres_compatible_incr)
3765 << TestIsLessOp << ConditionSrcRange;
3766 return true;
3767 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003768 if (TestIsLessOp == Subtract) {
3769 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3770 NewStep).get();
3771 Subtract = !Subtract;
3772 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003773 }
3774
3775 Step = NewStep;
3776 SubtractStep = Subtract;
3777 return false;
3778}
3779
Alexey Bataev9c821032015-04-30 04:23:23 +00003780bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003781 // Check init-expr for canonical loop form and save loop counter
3782 // variable - #Var and its initialization value - #LB.
3783 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3784 // var = lb
3785 // integer-type var = lb
3786 // random-access-iterator-type var = lb
3787 // pointer-type var = lb
3788 //
3789 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003790 if (EmitDiags) {
3791 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3792 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003793 return true;
3794 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003795 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003796 if (Expr *E = dyn_cast<Expr>(S))
3797 S = E->IgnoreParens();
3798 if (auto BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003799 if (BO->getOpcode() == BO_Assign) {
3800 auto *LHS = BO->getLHS()->IgnoreParens();
3801 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3802 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3803 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3804 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3805 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
3806 }
3807 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3808 if (ME->isArrow() &&
3809 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3810 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3811 }
3812 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003813 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3814 if (DS->isSingleDecl()) {
3815 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003816 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003817 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003818 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003819 SemaRef.Diag(S->getLocStart(),
3820 diag::ext_omp_loop_not_canonical_init)
3821 << S->getSourceRange();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003822 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003823 }
3824 }
3825 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003826 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3827 if (CE->getOperator() == OO_Equal) {
3828 auto *LHS = CE->getArg(0);
3829 if (auto DRE = dyn_cast<DeclRefExpr>(LHS)) {
3830 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3831 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3832 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3833 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
3834 }
3835 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3836 if (ME->isArrow() &&
3837 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3838 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3839 }
3840 }
3841 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003842
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003843 if (Dependent() || SemaRef.CurContext->isDependentContext())
3844 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00003845 if (EmitDiags) {
3846 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3847 << S->getSourceRange();
3848 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003849 return true;
3850}
3851
Alexey Bataev23b69422014-06-18 07:08:49 +00003852/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003853/// variable (which may be the loop variable) if possible.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003854static const ValueDecl *GetInitLCDecl(Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003855 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003856 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003857 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003858 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3859 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003860 if ((Ctor->isCopyOrMoveConstructor() ||
3861 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3862 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003863 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003864 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
3865 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
3866 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
3867 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3868 return getCanonicalDecl(ME->getMemberDecl());
3869 return getCanonicalDecl(VD);
3870 }
3871 }
3872 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
3873 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3874 return getCanonicalDecl(ME->getMemberDecl());
3875 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003876}
3877
3878bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3879 // Check test-expr for canonical form, save upper-bound UB, flags for
3880 // less/greater and for strict/non-strict comparison.
3881 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3882 // var relational-op b
3883 // b relational-op var
3884 //
3885 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003886 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003887 return true;
3888 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003889 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003890 SourceLocation CondLoc = S->getLocStart();
3891 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3892 if (BO->isRelationalOp()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003893 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003894 return SetUB(BO->getRHS(),
3895 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3896 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3897 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003898 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003899 return SetUB(BO->getLHS(),
3900 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3901 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3902 BO->getSourceRange(), BO->getOperatorLoc());
3903 }
3904 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3905 if (CE->getNumArgs() == 2) {
3906 auto Op = CE->getOperator();
3907 switch (Op) {
3908 case OO_Greater:
3909 case OO_GreaterEqual:
3910 case OO_Less:
3911 case OO_LessEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003912 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003913 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3914 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3915 CE->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003916 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003917 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3918 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3919 CE->getOperatorLoc());
3920 break;
3921 default:
3922 break;
3923 }
3924 }
3925 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003926 if (Dependent() || SemaRef.CurContext->isDependentContext())
3927 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003928 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003929 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003930 return true;
3931}
3932
3933bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3934 // RHS of canonical loop form increment can be:
3935 // var + incr
3936 // incr + var
3937 // var - incr
3938 //
3939 RHS = RHS->IgnoreParenImpCasts();
3940 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3941 if (BO->isAdditiveOp()) {
3942 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003943 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003944 return SetStep(BO->getRHS(), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003945 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003946 return SetStep(BO->getLHS(), false);
3947 }
3948 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3949 bool IsAdd = CE->getOperator() == OO_Plus;
3950 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003951 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003952 return SetStep(CE->getArg(1), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003953 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003954 return SetStep(CE->getArg(0), false);
3955 }
3956 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003957 if (Dependent() || SemaRef.CurContext->isDependentContext())
3958 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003959 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003960 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003961 return true;
3962}
3963
3964bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3965 // Check incr-expr for canonical loop form and return true if it
3966 // does not conform.
3967 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3968 // ++var
3969 // var++
3970 // --var
3971 // var--
3972 // var += incr
3973 // var -= incr
3974 // var = var + incr
3975 // var = incr + var
3976 // var = var - incr
3977 //
3978 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003979 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003980 return true;
3981 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003982 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003983 S = S->IgnoreParens();
3984 if (auto UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003985 if (UO->isIncrementDecrementOp() &&
3986 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003987 return SetStep(
3988 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3989 (UO->isDecrementOp() ? -1 : 1)).get(),
3990 false);
3991 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3992 switch (BO->getOpcode()) {
3993 case BO_AddAssign:
3994 case BO_SubAssign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003995 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003996 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3997 break;
3998 case BO_Assign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003999 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004000 return CheckIncRHS(BO->getRHS());
4001 break;
4002 default:
4003 break;
4004 }
4005 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4006 switch (CE->getOperator()) {
4007 case OO_PlusPlus:
4008 case OO_MinusMinus:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004009 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004010 return SetStep(
4011 SemaRef.ActOnIntegerConstant(
4012 CE->getLocStart(),
4013 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
4014 false);
4015 break;
4016 case OO_PlusEqual:
4017 case OO_MinusEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004018 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004019 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
4020 break;
4021 case OO_Equal:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004022 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004023 return CheckIncRHS(CE->getArg(1));
4024 break;
4025 default:
4026 break;
4027 }
4028 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004029 if (Dependent() || SemaRef.CurContext->isDependentContext())
4030 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004031 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004032 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004033 return true;
4034}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004035
Alexey Bataev5a3af132016-03-29 08:58:54 +00004036static ExprResult
4037tryBuildCapture(Sema &SemaRef, Expr *Capture,
4038 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4039 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4040 return SemaRef.PerformImplicitConversion(
4041 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4042 /*AllowExplicit=*/true);
4043 auto I = Captures.find(Capture);
4044 if (I != Captures.end())
4045 return buildCapture(SemaRef, Capture, I->second);
4046 DeclRefExpr *Ref = nullptr;
4047 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4048 Captures[Capture] = Ref;
4049 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004050}
4051
Alexander Musmana5f070a2014-10-01 06:03:56 +00004052/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004053Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
4054 Scope *S, const bool LimitedType,
4055 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004056 ExprResult Diff;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004057 auto VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004058 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004059 SemaRef.getLangOpts().CPlusPlus) {
4060 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004061 auto *UBExpr = TestIsLessOp ? UB : LB;
4062 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004063 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4064 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004065 if (!Upper || !Lower)
4066 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004067
4068 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4069
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004070 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004071 // BuildBinOp already emitted error, this one is to point user to upper
4072 // and lower bound, and to tell what is passed to 'operator-'.
4073 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
4074 << Upper->getSourceRange() << Lower->getSourceRange();
4075 return nullptr;
4076 }
4077 }
4078
4079 if (!Diff.isUsable())
4080 return nullptr;
4081
4082 // Upper - Lower [- 1]
4083 if (TestIsStrictOp)
4084 Diff = SemaRef.BuildBinOp(
4085 S, DefaultLoc, BO_Sub, Diff.get(),
4086 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4087 if (!Diff.isUsable())
4088 return nullptr;
4089
4090 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00004091 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
4092 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004093 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004094 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004095 if (!Diff.isUsable())
4096 return nullptr;
4097
4098 // Parentheses (for dumping/debugging purposes only).
4099 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4100 if (!Diff.isUsable())
4101 return nullptr;
4102
4103 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004104 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004105 if (!Diff.isUsable())
4106 return nullptr;
4107
Alexander Musman174b3ca2014-10-06 11:16:29 +00004108 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004109 QualType Type = Diff.get()->getType();
4110 auto &C = SemaRef.Context;
4111 bool UseVarType = VarType->hasIntegerRepresentation() &&
4112 C.getTypeSize(Type) > C.getTypeSize(VarType);
4113 if (!Type->isIntegerType() || UseVarType) {
4114 unsigned NewSize =
4115 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4116 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4117 : Type->hasSignedIntegerRepresentation();
4118 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004119 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4120 Diff = SemaRef.PerformImplicitConversion(
4121 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4122 if (!Diff.isUsable())
4123 return nullptr;
4124 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004125 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004126 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004127 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4128 if (NewSize != C.getTypeSize(Type)) {
4129 if (NewSize < C.getTypeSize(Type)) {
4130 assert(NewSize == 64 && "incorrect loop var size");
4131 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4132 << InitSrcRange << ConditionSrcRange;
4133 }
4134 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004135 NewSize, Type->hasSignedIntegerRepresentation() ||
4136 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004137 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4138 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4139 Sema::AA_Converting, true);
4140 if (!Diff.isUsable())
4141 return nullptr;
4142 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004143 }
4144 }
4145
Alexander Musmana5f070a2014-10-01 06:03:56 +00004146 return Diff.get();
4147}
4148
Alexey Bataev5a3af132016-03-29 08:58:54 +00004149Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4150 Scope *S, Expr *Cond,
4151 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004152 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4153 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4154 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004155
Alexey Bataev5a3af132016-03-29 08:58:54 +00004156 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4157 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4158 if (!NewLB.isUsable() || !NewUB.isUsable())
4159 return nullptr;
4160
Alexey Bataev62dbb972015-04-22 11:59:37 +00004161 auto CondExpr = SemaRef.BuildBinOp(
4162 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4163 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004164 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004165 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004166 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4167 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004168 CondExpr = SemaRef.PerformImplicitConversion(
4169 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4170 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004171 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004172 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4173 // Otherwise use original loop conditon and evaluate it in runtime.
4174 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4175}
4176
Alexander Musmana5f070a2014-10-01 06:03:56 +00004177/// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004178DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004179 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004180 auto *VD = dyn_cast<VarDecl>(LCDecl);
4181 if (!VD) {
4182 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4183 auto *Ref = buildDeclRefExpr(
4184 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004185 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4186 // If the loop control decl is explicitly marked as private, do not mark it
4187 // as captured again.
4188 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4189 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004190 return Ref;
4191 }
4192 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004193 DefaultLoc);
4194}
4195
4196Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004197 if (LCDecl && !LCDecl->isInvalidDecl()) {
4198 auto Type = LCDecl->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00004199 auto *PrivateVar =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004200 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4201 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004202 if (PrivateVar->isInvalidDecl())
4203 return nullptr;
4204 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4205 }
4206 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004207}
4208
4209/// \brief Build initization of the counter be used for codegen.
4210Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4211
4212/// \brief Build step of the counter be used for codegen.
4213Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4214
4215/// \brief Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004216struct LoopIterationSpace final {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004217 /// \brief Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004218 Expr *PreCond = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004219 /// \brief This expression calculates the number of iterations in the loop.
4220 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004221 Expr *NumIterations = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004222 /// \brief The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004223 Expr *CounterVar = nullptr;
Alexey Bataeva8899172015-08-06 12:30:57 +00004224 /// \brief Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004225 Expr *PrivateCounterVar = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004226 /// \brief This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004227 Expr *CounterInit = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004228 /// \brief This is step for the #CounterVar used to generate its update:
4229 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004230 Expr *CounterStep = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004231 /// \brief Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004232 bool Subtract = false;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004233 /// \brief Source range of the loop init.
4234 SourceRange InitSrcRange;
4235 /// \brief Source range of the loop condition.
4236 SourceRange CondSrcRange;
4237 /// \brief Source range of the loop increment.
4238 SourceRange IncSrcRange;
4239};
4240
Alexey Bataev23b69422014-06-18 07:08:49 +00004241} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004242
Alexey Bataev9c821032015-04-30 04:23:23 +00004243void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4244 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4245 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004246 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4247 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004248 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4249 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004250 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4251 if (auto *D = ISC.GetLoopDecl()) {
4252 auto *VD = dyn_cast<VarDecl>(D);
4253 if (!VD) {
4254 if (auto *Private = IsOpenMPCapturedDecl(D))
4255 VD = Private;
4256 else {
4257 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4258 /*WithInit=*/false);
4259 VD = cast<VarDecl>(Ref->getDecl());
4260 }
4261 }
4262 DSAStack->addLoopControlVariable(D, VD);
4263 }
4264 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004265 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004266 }
4267}
4268
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004269/// \brief Called on a for stmt to check and extract its iteration space
4270/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00004271static bool CheckOpenMPIterationSpace(
4272 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4273 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004274 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004275 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004276 LoopIterationSpace &ResultIterSpace,
4277 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004278 // OpenMP [2.6, Canonical Loop Form]
4279 // for (init-expr; test-expr; incr-expr) structured-block
4280 auto For = dyn_cast_or_null<ForStmt>(S);
4281 if (!For) {
4282 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004283 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4284 << getOpenMPDirectiveName(DKind) << NestedLoopCount
4285 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4286 if (NestedLoopCount > 1) {
4287 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4288 SemaRef.Diag(DSA.getConstructLoc(),
4289 diag::note_omp_collapse_ordered_expr)
4290 << 2 << CollapseLoopCountExpr->getSourceRange()
4291 << OrderedLoopCountExpr->getSourceRange();
4292 else if (CollapseLoopCountExpr)
4293 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4294 diag::note_omp_collapse_ordered_expr)
4295 << 0 << CollapseLoopCountExpr->getSourceRange();
4296 else
4297 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4298 diag::note_omp_collapse_ordered_expr)
4299 << 1 << OrderedLoopCountExpr->getSourceRange();
4300 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004301 return true;
4302 }
4303 assert(For->getBody());
4304
4305 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4306
4307 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00004308 auto Init = For->getInit();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004309 if (ISC.CheckInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004310 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004311
4312 bool HasErrors = false;
4313
4314 // Check loop variable's type.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004315 if (auto *LCDecl = ISC.GetLoopDecl()) {
4316 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004317
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004318 // OpenMP [2.6, Canonical Loop Form]
4319 // Var is one of the following:
4320 // A variable of signed or unsigned integer type.
4321 // For C++, a variable of a random access iterator type.
4322 // For C, a variable of a pointer type.
4323 auto VarType = LCDecl->getType().getNonReferenceType();
4324 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4325 !VarType->isPointerType() &&
4326 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4327 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4328 << SemaRef.getLangOpts().CPlusPlus;
4329 HasErrors = true;
4330 }
4331
4332 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4333 // a Construct
4334 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4335 // parallel for construct is (are) private.
4336 // The loop iteration variable in the associated for-loop of a simd
4337 // construct with just one associated for-loop is linear with a
4338 // constant-linear-step that is the increment of the associated for-loop.
4339 // Exclude loop var from the list of variables with implicitly defined data
4340 // sharing attributes.
4341 VarsWithImplicitDSA.erase(LCDecl);
4342
4343 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4344 // in a Construct, C/C++].
4345 // The loop iteration variable in the associated for-loop of a simd
4346 // construct with just one associated for-loop may be listed in a linear
4347 // clause with a constant-linear-step that is the increment of the
4348 // associated for-loop.
4349 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4350 // parallel for construct may be listed in a private or lastprivate clause.
4351 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4352 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4353 // declared in the loop and it is predetermined as a private.
4354 auto PredeterminedCKind =
4355 isOpenMPSimdDirective(DKind)
4356 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4357 : OMPC_private;
4358 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4359 DVar.CKind != PredeterminedCKind) ||
4360 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4361 isOpenMPDistributeDirective(DKind)) &&
4362 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4363 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4364 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4365 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4366 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4367 << getOpenMPClauseName(PredeterminedCKind);
4368 if (DVar.RefExpr == nullptr)
4369 DVar.CKind = PredeterminedCKind;
4370 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4371 HasErrors = true;
4372 } else if (LoopDeclRefExpr != nullptr) {
4373 // Make the loop iteration variable private (for worksharing constructs),
4374 // linear (for simd directives with the only one associated loop) or
4375 // lastprivate (for simd directives with several collapsed or ordered
4376 // loops).
4377 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004378 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4379 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004380 /*FromParent=*/false);
4381 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4382 }
4383
4384 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4385
4386 // Check test-expr.
4387 HasErrors |= ISC.CheckCond(For->getCond());
4388
4389 // Check incr-expr.
4390 HasErrors |= ISC.CheckInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004391 }
4392
Alexander Musmana5f070a2014-10-01 06:03:56 +00004393 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004394 return HasErrors;
4395
Alexander Musmana5f070a2014-10-01 06:03:56 +00004396 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004397 ResultIterSpace.PreCond =
4398 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00004399 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004400 DSA.getCurScope(),
4401 (isOpenMPWorksharingDirective(DKind) ||
4402 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4403 Captures);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004404 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
Alexey Bataeva8899172015-08-06 12:30:57 +00004405 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004406 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4407 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4408 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4409 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4410 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4411 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4412
Alexey Bataev62dbb972015-04-22 11:59:37 +00004413 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4414 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004415 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004416 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004417 ResultIterSpace.CounterInit == nullptr ||
4418 ResultIterSpace.CounterStep == nullptr);
4419
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004420 return HasErrors;
4421}
4422
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004423/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004424static ExprResult
4425BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4426 ExprResult Start,
4427 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004428 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004429 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4430 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004431 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004432 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004433 VarRef.get()->getType())) {
4434 NewStart = SemaRef.PerformImplicitConversion(
4435 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4436 /*AllowExplicit=*/true);
4437 if (!NewStart.isUsable())
4438 return ExprError();
4439 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004440
4441 auto Init =
4442 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4443 return Init;
4444}
4445
Alexander Musmana5f070a2014-10-01 06:03:56 +00004446/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004447static ExprResult
4448BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4449 ExprResult VarRef, ExprResult Start, ExprResult Iter,
4450 ExprResult Step, bool Subtract,
4451 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004452 // Add parentheses (for debugging purposes only).
4453 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4454 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4455 !Step.isUsable())
4456 return ExprError();
4457
Alexey Bataev5a3af132016-03-29 08:58:54 +00004458 ExprResult NewStep = Step;
4459 if (Captures)
4460 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004461 if (NewStep.isInvalid())
4462 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004463 ExprResult Update =
4464 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004465 if (!Update.isUsable())
4466 return ExprError();
4467
Alexey Bataevc0214e02016-02-16 12:13:49 +00004468 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4469 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004470 ExprResult NewStart = Start;
4471 if (Captures)
4472 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004473 if (NewStart.isInvalid())
4474 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004475
Alexey Bataevc0214e02016-02-16 12:13:49 +00004476 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4477 ExprResult SavedUpdate = Update;
4478 ExprResult UpdateVal;
4479 if (VarRef.get()->getType()->isOverloadableType() ||
4480 NewStart.get()->getType()->isOverloadableType() ||
4481 Update.get()->getType()->isOverloadableType()) {
4482 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4483 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4484 Update =
4485 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4486 if (Update.isUsable()) {
4487 UpdateVal =
4488 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4489 VarRef.get(), SavedUpdate.get());
4490 if (UpdateVal.isUsable()) {
4491 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4492 UpdateVal.get());
4493 }
4494 }
4495 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4496 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004497
Alexey Bataevc0214e02016-02-16 12:13:49 +00004498 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4499 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4500 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4501 NewStart.get(), SavedUpdate.get());
4502 if (!Update.isUsable())
4503 return ExprError();
4504
Alexey Bataev11481f52016-02-17 10:29:05 +00004505 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4506 VarRef.get()->getType())) {
4507 Update = SemaRef.PerformImplicitConversion(
4508 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4509 if (!Update.isUsable())
4510 return ExprError();
4511 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004512
4513 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4514 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004515 return Update;
4516}
4517
4518/// \brief Convert integer expression \a E to make it have at least \a Bits
4519/// bits.
4520static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4521 Sema &SemaRef) {
4522 if (E == nullptr)
4523 return ExprError();
4524 auto &C = SemaRef.Context;
4525 QualType OldType = E->getType();
4526 unsigned HasBits = C.getTypeSize(OldType);
4527 if (HasBits >= Bits)
4528 return ExprResult(E);
4529 // OK to convert to signed, because new type has more bits than old.
4530 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4531 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4532 true);
4533}
4534
4535/// \brief Check if the given expression \a E is a constant integer that fits
4536/// into \a Bits bits.
4537static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4538 if (E == nullptr)
4539 return false;
4540 llvm::APSInt Result;
4541 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4542 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4543 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004544}
4545
Alexey Bataev5a3af132016-03-29 08:58:54 +00004546/// Build preinits statement for the given declarations.
4547static Stmt *buildPreInits(ASTContext &Context,
4548 SmallVectorImpl<Decl *> &PreInits) {
4549 if (!PreInits.empty()) {
4550 return new (Context) DeclStmt(
4551 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4552 SourceLocation(), SourceLocation());
4553 }
4554 return nullptr;
4555}
4556
4557/// Build preinits statement for the given declarations.
4558static Stmt *buildPreInits(ASTContext &Context,
4559 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4560 if (!Captures.empty()) {
4561 SmallVector<Decl *, 16> PreInits;
4562 for (auto &Pair : Captures)
4563 PreInits.push_back(Pair.second->getDecl());
4564 return buildPreInits(Context, PreInits);
4565 }
4566 return nullptr;
4567}
4568
4569/// Build postupdate expression for the given list of postupdates expressions.
4570static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4571 Expr *PostUpdate = nullptr;
4572 if (!PostUpdates.empty()) {
4573 for (auto *E : PostUpdates) {
4574 Expr *ConvE = S.BuildCStyleCastExpr(
4575 E->getExprLoc(),
4576 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4577 E->getExprLoc(), E)
4578 .get();
4579 PostUpdate = PostUpdate
4580 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4581 PostUpdate, ConvE)
4582 .get()
4583 : ConvE;
4584 }
4585 }
4586 return PostUpdate;
4587}
4588
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004589/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004590/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4591/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004592static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004593CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4594 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4595 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004596 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004597 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004598 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004599 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004600 // Found 'collapse' clause - calculate collapse number.
4601 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004602 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004603 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004604 }
4605 if (OrderedLoopCountExpr) {
4606 // Found 'ordered' clause - calculate collapse number.
4607 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004608 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4609 if (Result.getLimitedValue() < NestedLoopCount) {
4610 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4611 diag::err_omp_wrong_ordered_loop_count)
4612 << OrderedLoopCountExpr->getSourceRange();
4613 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4614 diag::note_collapse_loop_count)
4615 << CollapseLoopCountExpr->getSourceRange();
4616 }
4617 NestedLoopCount = Result.getLimitedValue();
4618 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004619 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004620 // This is helper routine for loop directives (e.g., 'for', 'simd',
4621 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00004622 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004623 SmallVector<LoopIterationSpace, 4> IterSpaces;
4624 IterSpaces.resize(NestedLoopCount);
4625 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004626 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004627 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004628 NestedLoopCount, CollapseLoopCountExpr,
4629 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004630 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004631 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004632 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004633 // OpenMP [2.8.1, simd construct, Restrictions]
4634 // All loops associated with the construct must be perfectly nested; that
4635 // is, there must be no intervening code nor any OpenMP directive between
4636 // any two loops.
4637 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004638 }
4639
Alexander Musmana5f070a2014-10-01 06:03:56 +00004640 Built.clear(/* size */ NestedLoopCount);
4641
4642 if (SemaRef.CurContext->isDependentContext())
4643 return NestedLoopCount;
4644
4645 // An example of what is generated for the following code:
4646 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004647 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004648 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004649 // for (k = 0; k < NK; ++k)
4650 // for (j = J0; j < NJ; j+=2) {
4651 // <loop body>
4652 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004653 //
4654 // We generate the code below.
4655 // Note: the loop body may be outlined in CodeGen.
4656 // Note: some counters may be C++ classes, operator- is used to find number of
4657 // iterations and operator+= to calculate counter value.
4658 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4659 // or i64 is currently supported).
4660 //
4661 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4662 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4663 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4664 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4665 // // similar updates for vars in clauses (e.g. 'linear')
4666 // <loop body (using local i and j)>
4667 // }
4668 // i = NI; // assign final values of counters
4669 // j = NJ;
4670 //
4671
4672 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4673 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004674 // Precondition tests if there is at least one iteration (all conditions are
4675 // true).
4676 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004677 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004678 ExprResult LastIteration32 = WidenIterationCount(
4679 32 /* Bits */, SemaRef.PerformImplicitConversion(
4680 N0->IgnoreImpCasts(), N0->getType(),
4681 Sema::AA_Converting, /*AllowExplicit=*/true)
4682 .get(),
4683 SemaRef);
4684 ExprResult LastIteration64 = WidenIterationCount(
4685 64 /* Bits */, SemaRef.PerformImplicitConversion(
4686 N0->IgnoreImpCasts(), N0->getType(),
4687 Sema::AA_Converting, /*AllowExplicit=*/true)
4688 .get(),
4689 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004690
4691 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4692 return NestedLoopCount;
4693
4694 auto &C = SemaRef.Context;
4695 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4696
4697 Scope *CurScope = DSA.getCurScope();
4698 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004699 if (PreCond.isUsable()) {
4700 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4701 PreCond.get(), IterSpaces[Cnt].PreCond);
4702 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004703 auto N = IterSpaces[Cnt].NumIterations;
4704 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4705 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004706 LastIteration32 = SemaRef.BuildBinOp(
4707 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4708 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4709 Sema::AA_Converting,
4710 /*AllowExplicit=*/true)
4711 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004712 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004713 LastIteration64 = SemaRef.BuildBinOp(
4714 CurScope, SourceLocation(), BO_Mul, LastIteration64.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 }
4720
4721 // Choose either the 32-bit or 64-bit version.
4722 ExprResult LastIteration = LastIteration64;
4723 if (LastIteration32.isUsable() &&
4724 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4725 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4726 FitsInto(
4727 32 /* Bits */,
4728 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4729 LastIteration64.get(), SemaRef)))
4730 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00004731 QualType VType = LastIteration.get()->getType();
4732 QualType RealVType = VType;
4733 QualType StrideVType = VType;
4734 if (isOpenMPTaskLoopDirective(DKind)) {
4735 VType =
4736 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
4737 StrideVType =
4738 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
4739 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004740
4741 if (!LastIteration.isUsable())
4742 return 0;
4743
4744 // Save the number of iterations.
4745 ExprResult NumIterations = LastIteration;
4746 {
4747 LastIteration = SemaRef.BuildBinOp(
4748 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4749 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4750 if (!LastIteration.isUsable())
4751 return 0;
4752 }
4753
4754 // Calculate the last iteration number beforehand instead of doing this on
4755 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4756 llvm::APSInt Result;
4757 bool IsConstant =
4758 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4759 ExprResult CalcLastIteration;
4760 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004761 ExprResult SaveRef =
4762 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004763 LastIteration = SaveRef;
4764
4765 // Prepare SaveRef + 1.
4766 NumIterations = SemaRef.BuildBinOp(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004767 CurScope, SourceLocation(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004768 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4769 if (!NumIterations.isUsable())
4770 return 0;
4771 }
4772
4773 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4774
Alexander Musmanc6388682014-12-15 07:07:06 +00004775 // Build variables passed into runtime, nesessary for worksharing directives.
4776 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004777 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4778 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004779 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004780 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4781 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004782 SemaRef.AddInitializerToDecl(
4783 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4784 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4785
4786 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004787 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4788 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004789 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4790 /*DirectInit*/ false,
4791 /*TypeMayContainAuto*/ false);
4792
4793 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4794 // This will be used to implement clause 'lastprivate'.
4795 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004796 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4797 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004798 SemaRef.AddInitializerToDecl(
4799 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4800 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4801
4802 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00004803 VarDecl *STDecl =
4804 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
4805 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004806 SemaRef.AddInitializerToDecl(
4807 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4808 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4809
4810 // Build expression: UB = min(UB, LastIteration)
4811 // It is nesessary for CodeGen of directives with static scheduling.
4812 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4813 UB.get(), LastIteration.get());
4814 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4815 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4816 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4817 CondOp.get());
4818 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4819 }
4820
4821 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004822 ExprResult IV;
4823 ExprResult Init;
4824 {
Alexey Bataev7292c292016-04-25 12:22:29 +00004825 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
4826 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004827 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004828 isOpenMPTaskLoopDirective(DKind) ||
4829 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004830 ? LB.get()
4831 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4832 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4833 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004834 }
4835
Alexander Musmanc6388682014-12-15 07:07:06 +00004836 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004837 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004838 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004839 (isOpenMPWorksharingDirective(DKind) ||
4840 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004841 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4842 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4843 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004844
4845 // Loop increment (IV = IV + 1)
4846 SourceLocation IncLoc;
4847 ExprResult Inc =
4848 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4849 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4850 if (!Inc.isUsable())
4851 return 0;
4852 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004853 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4854 if (!Inc.isUsable())
4855 return 0;
4856
4857 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4858 // Used for directives with static scheduling.
4859 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004860 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4861 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004862 // LB + ST
4863 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4864 if (!NextLB.isUsable())
4865 return 0;
4866 // LB = LB + ST
4867 NextLB =
4868 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4869 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4870 if (!NextLB.isUsable())
4871 return 0;
4872 // UB + ST
4873 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4874 if (!NextUB.isUsable())
4875 return 0;
4876 // UB = UB + ST
4877 NextUB =
4878 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4879 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4880 if (!NextUB.isUsable())
4881 return 0;
4882 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004883
4884 // Build updates and final values of the loop counters.
4885 bool HasErrors = false;
4886 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004887 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004888 Built.Updates.resize(NestedLoopCount);
4889 Built.Finals.resize(NestedLoopCount);
Alexey Bataev8b427062016-05-25 12:36:08 +00004890 SmallVector<Expr *, 4> LoopMultipliers;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004891 {
4892 ExprResult Div;
4893 // Go from inner nested loop to outer.
4894 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4895 LoopIterationSpace &IS = IterSpaces[Cnt];
4896 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4897 // Build: Iter = (IV / Div) % IS.NumIters
4898 // where Div is product of previous iterations' IS.NumIters.
4899 ExprResult Iter;
4900 if (Div.isUsable()) {
4901 Iter =
4902 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4903 } else {
4904 Iter = IV;
4905 assert((Cnt == (int)NestedLoopCount - 1) &&
4906 "unusable div expected on first iteration only");
4907 }
4908
4909 if (Cnt != 0 && Iter.isUsable())
4910 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4911 IS.NumIterations);
4912 if (!Iter.isUsable()) {
4913 HasErrors = true;
4914 break;
4915 }
4916
Alexey Bataev39f915b82015-05-08 10:41:21 +00004917 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004918 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
4919 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
4920 IS.CounterVar->getExprLoc(),
4921 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004922 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004923 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004924 if (!Init.isUsable()) {
4925 HasErrors = true;
4926 break;
4927 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00004928 ExprResult Update = BuildCounterUpdate(
4929 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
4930 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004931 if (!Update.isUsable()) {
4932 HasErrors = true;
4933 break;
4934 }
4935
4936 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4937 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004938 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004939 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004940 if (!Final.isUsable()) {
4941 HasErrors = true;
4942 break;
4943 }
4944
4945 // Build Div for the next iteration: Div <- Div * IS.NumIters
4946 if (Cnt != 0) {
4947 if (Div.isUnset())
4948 Div = IS.NumIterations;
4949 else
4950 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4951 IS.NumIterations);
4952
4953 // Add parentheses (for debugging purposes only).
4954 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00004955 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004956 if (!Div.isUsable()) {
4957 HasErrors = true;
4958 break;
4959 }
Alexey Bataev8b427062016-05-25 12:36:08 +00004960 LoopMultipliers.push_back(Div.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004961 }
4962 if (!Update.isUsable() || !Final.isUsable()) {
4963 HasErrors = true;
4964 break;
4965 }
4966 // Save results
4967 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004968 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004969 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004970 Built.Updates[Cnt] = Update.get();
4971 Built.Finals[Cnt] = Final.get();
4972 }
4973 }
4974
4975 if (HasErrors)
4976 return 0;
4977
4978 // Save results
4979 Built.IterationVarRef = IV.get();
4980 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004981 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004982 Built.CalcLastIteration =
4983 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004984 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00004985 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004986 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004987 Built.Init = Init.get();
4988 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004989 Built.LB = LB.get();
4990 Built.UB = UB.get();
4991 Built.IL = IL.get();
4992 Built.ST = ST.get();
4993 Built.EUB = EUB.get();
4994 Built.NLB = NextLB.get();
4995 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004996
Alexey Bataev8b427062016-05-25 12:36:08 +00004997 Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get();
4998 // Fill data for doacross depend clauses.
4999 for (auto Pair : DSA.getDoacrossDependClauses()) {
5000 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
5001 Pair.first->setCounterValue(CounterVal);
5002 else {
5003 if (NestedLoopCount != Pair.second.size() ||
5004 NestedLoopCount != LoopMultipliers.size() + 1) {
5005 // Erroneous case - clause has some problems.
5006 Pair.first->setCounterValue(CounterVal);
5007 continue;
5008 }
5009 assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink);
5010 auto I = Pair.second.rbegin();
5011 auto IS = IterSpaces.rbegin();
5012 auto ILM = LoopMultipliers.rbegin();
5013 Expr *UpCounterVal = CounterVal;
5014 Expr *Multiplier = nullptr;
5015 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5016 if (I->first) {
5017 assert(IS->CounterStep);
5018 Expr *NormalizedOffset =
5019 SemaRef
5020 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div,
5021 I->first, IS->CounterStep)
5022 .get();
5023 if (Multiplier) {
5024 NormalizedOffset =
5025 SemaRef
5026 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul,
5027 NormalizedOffset, Multiplier)
5028 .get();
5029 }
5030 assert(I->second == OO_Plus || I->second == OO_Minus);
5031 BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub;
5032 UpCounterVal =
5033 SemaRef.BuildBinOp(CurScope, I->first->getExprLoc(), BOK,
5034 UpCounterVal, NormalizedOffset).get();
5035 }
5036 Multiplier = *ILM;
5037 ++I;
5038 ++IS;
5039 ++ILM;
5040 }
5041 Pair.first->setCounterValue(UpCounterVal);
5042 }
5043 }
5044
Alexey Bataevabfc0692014-06-25 06:52:00 +00005045 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005046}
5047
Alexey Bataev10e775f2015-07-30 11:36:16 +00005048static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005049 auto CollapseClauses =
5050 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5051 if (CollapseClauses.begin() != CollapseClauses.end())
5052 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005053 return nullptr;
5054}
5055
Alexey Bataev10e775f2015-07-30 11:36:16 +00005056static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005057 auto OrderedClauses =
5058 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5059 if (OrderedClauses.begin() != OrderedClauses.end())
5060 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005061 return nullptr;
5062}
5063
Alexey Bataev66b15b52015-08-21 11:14:16 +00005064static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
5065 const Expr *Safelen) {
5066 llvm::APSInt SimdlenRes, SafelenRes;
5067 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
5068 Simdlen->isInstantiationDependent() ||
5069 Simdlen->containsUnexpandedParameterPack())
5070 return false;
5071 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
5072 Safelen->isInstantiationDependent() ||
5073 Safelen->containsUnexpandedParameterPack())
5074 return false;
5075 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
5076 Safelen->EvaluateAsInt(SafelenRes, S.Context);
5077 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5078 // If both simdlen and safelen clauses are specified, the value of the simdlen
5079 // parameter must be less than or equal to the value of the safelen parameter.
5080 if (SimdlenRes > SafelenRes) {
5081 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
5082 << Simdlen->getSourceRange() << Safelen->getSourceRange();
5083 return true;
5084 }
5085 return false;
5086}
5087
Alexey Bataev4acb8592014-07-07 13:01:15 +00005088StmtResult Sema::ActOnOpenMPSimdDirective(
5089 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5090 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005091 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005092 if (!AStmt)
5093 return StmtError();
5094
5095 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005096 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005097 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5098 // define the nested loops number.
5099 unsigned NestedLoopCount = CheckOpenMPLoop(
5100 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5101 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005102 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005103 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005104
Alexander Musmana5f070a2014-10-01 06:03:56 +00005105 assert((CurContext->isDependentContext() || B.builtAll()) &&
5106 "omp simd loop exprs were not built");
5107
Alexander Musman3276a272015-03-21 10:12:56 +00005108 if (!CurContext->isDependentContext()) {
5109 // Finalize the clauses that need pre-built expressions for CodeGen.
5110 for (auto C : Clauses) {
5111 if (auto LC = dyn_cast<OMPLinearClause>(C))
5112 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005113 B.NumIterations, *this, CurScope,
5114 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005115 return StmtError();
5116 }
5117 }
5118
Alexey Bataev66b15b52015-08-21 11:14:16 +00005119 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5120 // If both simdlen and safelen clauses are specified, the value of the simdlen
5121 // parameter must be less than or equal to the value of the safelen parameter.
5122 OMPSafelenClause *Safelen = nullptr;
5123 OMPSimdlenClause *Simdlen = nullptr;
5124 for (auto *Clause : Clauses) {
5125 if (Clause->getClauseKind() == OMPC_safelen)
5126 Safelen = cast<OMPSafelenClause>(Clause);
5127 else if (Clause->getClauseKind() == OMPC_simdlen)
5128 Simdlen = cast<OMPSimdlenClause>(Clause);
5129 if (Safelen && Simdlen)
5130 break;
5131 }
5132 if (Simdlen && Safelen &&
5133 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5134 Safelen->getSafelen()))
5135 return StmtError();
5136
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005137 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005138 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5139 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005140}
5141
Alexey Bataev4acb8592014-07-07 13:01:15 +00005142StmtResult Sema::ActOnOpenMPForDirective(
5143 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5144 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005145 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005146 if (!AStmt)
5147 return StmtError();
5148
5149 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005150 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005151 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5152 // define the nested loops number.
5153 unsigned NestedLoopCount = CheckOpenMPLoop(
5154 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5155 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005156 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005157 return StmtError();
5158
Alexander Musmana5f070a2014-10-01 06:03:56 +00005159 assert((CurContext->isDependentContext() || B.builtAll()) &&
5160 "omp for loop exprs were not built");
5161
Alexey Bataev54acd402015-08-04 11:18:19 +00005162 if (!CurContext->isDependentContext()) {
5163 // Finalize the clauses that need pre-built expressions for CodeGen.
5164 for (auto C : Clauses) {
5165 if (auto LC = dyn_cast<OMPLinearClause>(C))
5166 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005167 B.NumIterations, *this, CurScope,
5168 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005169 return StmtError();
5170 }
5171 }
5172
Alexey Bataevf29276e2014-06-18 04:14:57 +00005173 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005174 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005175 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005176}
5177
Alexander Musmanf82886e2014-09-18 05:12:34 +00005178StmtResult Sema::ActOnOpenMPForSimdDirective(
5179 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5180 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005181 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005182 if (!AStmt)
5183 return StmtError();
5184
5185 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005186 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005187 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5188 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005189 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005190 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5191 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5192 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005193 if (NestedLoopCount == 0)
5194 return StmtError();
5195
Alexander Musmanc6388682014-12-15 07:07:06 +00005196 assert((CurContext->isDependentContext() || B.builtAll()) &&
5197 "omp for simd loop exprs were not built");
5198
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005199 if (!CurContext->isDependentContext()) {
5200 // Finalize the clauses that need pre-built expressions for CodeGen.
5201 for (auto C : Clauses) {
5202 if (auto LC = dyn_cast<OMPLinearClause>(C))
5203 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005204 B.NumIterations, *this, CurScope,
5205 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005206 return StmtError();
5207 }
5208 }
5209
Alexey Bataev66b15b52015-08-21 11:14:16 +00005210 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5211 // If both simdlen and safelen clauses are specified, the value of the simdlen
5212 // parameter must be less than or equal to the value of the safelen parameter.
5213 OMPSafelenClause *Safelen = nullptr;
5214 OMPSimdlenClause *Simdlen = nullptr;
5215 for (auto *Clause : Clauses) {
5216 if (Clause->getClauseKind() == OMPC_safelen)
5217 Safelen = cast<OMPSafelenClause>(Clause);
5218 else if (Clause->getClauseKind() == OMPC_simdlen)
5219 Simdlen = cast<OMPSimdlenClause>(Clause);
5220 if (Safelen && Simdlen)
5221 break;
5222 }
5223 if (Simdlen && Safelen &&
5224 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5225 Safelen->getSafelen()))
5226 return StmtError();
5227
Alexander Musmanf82886e2014-09-18 05:12:34 +00005228 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005229 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5230 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005231}
5232
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005233StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5234 Stmt *AStmt,
5235 SourceLocation StartLoc,
5236 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005237 if (!AStmt)
5238 return StmtError();
5239
5240 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005241 auto BaseStmt = AStmt;
5242 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5243 BaseStmt = CS->getCapturedStmt();
5244 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5245 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005246 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005247 return StmtError();
5248 // All associated statements must be '#pragma omp section' except for
5249 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005250 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005251 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5252 if (SectionStmt)
5253 Diag(SectionStmt->getLocStart(),
5254 diag::err_omp_sections_substmt_not_section);
5255 return StmtError();
5256 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005257 cast<OMPSectionDirective>(SectionStmt)
5258 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005259 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005260 } else {
5261 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5262 return StmtError();
5263 }
5264
5265 getCurFunction()->setHasBranchProtectedScope();
5266
Alexey Bataev25e5b442015-09-15 12:52:43 +00005267 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5268 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005269}
5270
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005271StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5272 SourceLocation StartLoc,
5273 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005274 if (!AStmt)
5275 return StmtError();
5276
5277 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005278
5279 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005280 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005281
Alexey Bataev25e5b442015-09-15 12:52:43 +00005282 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5283 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005284}
5285
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005286StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5287 Stmt *AStmt,
5288 SourceLocation StartLoc,
5289 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005290 if (!AStmt)
5291 return StmtError();
5292
5293 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005294
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005295 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005296
Alexey Bataev3255bf32015-01-19 05:20:46 +00005297 // OpenMP [2.7.3, single Construct, Restrictions]
5298 // The copyprivate clause must not be used with the nowait clause.
5299 OMPClause *Nowait = nullptr;
5300 OMPClause *Copyprivate = nullptr;
5301 for (auto *Clause : Clauses) {
5302 if (Clause->getClauseKind() == OMPC_nowait)
5303 Nowait = Clause;
5304 else if (Clause->getClauseKind() == OMPC_copyprivate)
5305 Copyprivate = Clause;
5306 if (Copyprivate && Nowait) {
5307 Diag(Copyprivate->getLocStart(),
5308 diag::err_omp_single_copyprivate_with_nowait);
5309 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5310 return StmtError();
5311 }
5312 }
5313
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005314 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5315}
5316
Alexander Musman80c22892014-07-17 08:54:58 +00005317StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5318 SourceLocation StartLoc,
5319 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005320 if (!AStmt)
5321 return StmtError();
5322
5323 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005324
5325 getCurFunction()->setHasBranchProtectedScope();
5326
5327 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5328}
5329
Alexey Bataev28c75412015-12-15 08:19:24 +00005330StmtResult Sema::ActOnOpenMPCriticalDirective(
5331 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5332 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005333 if (!AStmt)
5334 return StmtError();
5335
5336 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005337
Alexey Bataev28c75412015-12-15 08:19:24 +00005338 bool ErrorFound = false;
5339 llvm::APSInt Hint;
5340 SourceLocation HintLoc;
5341 bool DependentHint = false;
5342 for (auto *C : Clauses) {
5343 if (C->getClauseKind() == OMPC_hint) {
5344 if (!DirName.getName()) {
5345 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5346 ErrorFound = true;
5347 }
5348 Expr *E = cast<OMPHintClause>(C)->getHint();
5349 if (E->isTypeDependent() || E->isValueDependent() ||
5350 E->isInstantiationDependent())
5351 DependentHint = true;
5352 else {
5353 Hint = E->EvaluateKnownConstInt(Context);
5354 HintLoc = C->getLocStart();
5355 }
5356 }
5357 }
5358 if (ErrorFound)
5359 return StmtError();
5360 auto Pair = DSAStack->getCriticalWithHint(DirName);
5361 if (Pair.first && DirName.getName() && !DependentHint) {
5362 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5363 Diag(StartLoc, diag::err_omp_critical_with_hint);
5364 if (HintLoc.isValid()) {
5365 Diag(HintLoc, diag::note_omp_critical_hint_here)
5366 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5367 } else
5368 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5369 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5370 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5371 << 1
5372 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5373 /*Radix=*/10, /*Signed=*/false);
5374 } else
5375 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5376 }
5377 }
5378
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005379 getCurFunction()->setHasBranchProtectedScope();
5380
Alexey Bataev28c75412015-12-15 08:19:24 +00005381 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5382 Clauses, AStmt);
5383 if (!Pair.first && DirName.getName() && !DependentHint)
5384 DSAStack->addCriticalWithHint(Dir, Hint);
5385 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005386}
5387
Alexey Bataev4acb8592014-07-07 13:01:15 +00005388StmtResult Sema::ActOnOpenMPParallelForDirective(
5389 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5390 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005391 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005392 if (!AStmt)
5393 return StmtError();
5394
Alexey Bataev4acb8592014-07-07 13:01:15 +00005395 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5396 // 1.2.2 OpenMP Language Terminology
5397 // Structured block - An executable statement with a single entry at the
5398 // top and a single exit at the bottom.
5399 // The point of exit cannot be a branch out of the structured block.
5400 // longjmp() and throw() must not violate the entry/exit criteria.
5401 CS->getCapturedDecl()->setNothrow();
5402
Alexander Musmanc6388682014-12-15 07:07:06 +00005403 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005404 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5405 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005406 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005407 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5408 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5409 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005410 if (NestedLoopCount == 0)
5411 return StmtError();
5412
Alexander Musmana5f070a2014-10-01 06:03:56 +00005413 assert((CurContext->isDependentContext() || B.builtAll()) &&
5414 "omp parallel for loop exprs were not built");
5415
Alexey Bataev54acd402015-08-04 11:18:19 +00005416 if (!CurContext->isDependentContext()) {
5417 // Finalize the clauses that need pre-built expressions for CodeGen.
5418 for (auto C : Clauses) {
5419 if (auto LC = dyn_cast<OMPLinearClause>(C))
5420 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005421 B.NumIterations, *this, CurScope,
5422 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005423 return StmtError();
5424 }
5425 }
5426
Alexey Bataev4acb8592014-07-07 13:01:15 +00005427 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005428 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005429 NestedLoopCount, Clauses, AStmt, B,
5430 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005431}
5432
Alexander Musmane4e893b2014-09-23 09:33:00 +00005433StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5434 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5435 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005436 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005437 if (!AStmt)
5438 return StmtError();
5439
Alexander Musmane4e893b2014-09-23 09:33:00 +00005440 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5441 // 1.2.2 OpenMP Language Terminology
5442 // Structured block - An executable statement with a single entry at the
5443 // top and a single exit at the bottom.
5444 // The point of exit cannot be a branch out of the structured block.
5445 // longjmp() and throw() must not violate the entry/exit criteria.
5446 CS->getCapturedDecl()->setNothrow();
5447
Alexander Musmanc6388682014-12-15 07:07:06 +00005448 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005449 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5450 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005451 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005452 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5453 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5454 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005455 if (NestedLoopCount == 0)
5456 return StmtError();
5457
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005458 if (!CurContext->isDependentContext()) {
5459 // Finalize the clauses that need pre-built expressions for CodeGen.
5460 for (auto C : Clauses) {
5461 if (auto LC = dyn_cast<OMPLinearClause>(C))
5462 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005463 B.NumIterations, *this, CurScope,
5464 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005465 return StmtError();
5466 }
5467 }
5468
Alexey Bataev66b15b52015-08-21 11:14:16 +00005469 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5470 // If both simdlen and safelen clauses are specified, the value of the simdlen
5471 // parameter must be less than or equal to the value of the safelen parameter.
5472 OMPSafelenClause *Safelen = nullptr;
5473 OMPSimdlenClause *Simdlen = nullptr;
5474 for (auto *Clause : Clauses) {
5475 if (Clause->getClauseKind() == OMPC_safelen)
5476 Safelen = cast<OMPSafelenClause>(Clause);
5477 else if (Clause->getClauseKind() == OMPC_simdlen)
5478 Simdlen = cast<OMPSimdlenClause>(Clause);
5479 if (Safelen && Simdlen)
5480 break;
5481 }
5482 if (Simdlen && Safelen &&
5483 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5484 Safelen->getSafelen()))
5485 return StmtError();
5486
Alexander Musmane4e893b2014-09-23 09:33:00 +00005487 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005488 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005489 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005490}
5491
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005492StmtResult
5493Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5494 Stmt *AStmt, SourceLocation StartLoc,
5495 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005496 if (!AStmt)
5497 return StmtError();
5498
5499 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005500 auto BaseStmt = AStmt;
5501 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5502 BaseStmt = CS->getCapturedStmt();
5503 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5504 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005505 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005506 return StmtError();
5507 // All associated statements must be '#pragma omp section' except for
5508 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005509 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005510 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5511 if (SectionStmt)
5512 Diag(SectionStmt->getLocStart(),
5513 diag::err_omp_parallel_sections_substmt_not_section);
5514 return StmtError();
5515 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005516 cast<OMPSectionDirective>(SectionStmt)
5517 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005518 }
5519 } else {
5520 Diag(AStmt->getLocStart(),
5521 diag::err_omp_parallel_sections_not_compound_stmt);
5522 return StmtError();
5523 }
5524
5525 getCurFunction()->setHasBranchProtectedScope();
5526
Alexey Bataev25e5b442015-09-15 12:52:43 +00005527 return OMPParallelSectionsDirective::Create(
5528 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005529}
5530
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005531StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5532 Stmt *AStmt, SourceLocation StartLoc,
5533 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005534 if (!AStmt)
5535 return StmtError();
5536
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005537 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5538 // 1.2.2 OpenMP Language Terminology
5539 // Structured block - An executable statement with a single entry at the
5540 // top and a single exit at the bottom.
5541 // The point of exit cannot be a branch out of the structured block.
5542 // longjmp() and throw() must not violate the entry/exit criteria.
5543 CS->getCapturedDecl()->setNothrow();
5544
5545 getCurFunction()->setHasBranchProtectedScope();
5546
Alexey Bataev25e5b442015-09-15 12:52:43 +00005547 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5548 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005549}
5550
Alexey Bataev68446b72014-07-18 07:47:19 +00005551StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5552 SourceLocation EndLoc) {
5553 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5554}
5555
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005556StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5557 SourceLocation EndLoc) {
5558 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5559}
5560
Alexey Bataev2df347a2014-07-18 10:17:07 +00005561StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5562 SourceLocation EndLoc) {
5563 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5564}
5565
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005566StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5567 SourceLocation StartLoc,
5568 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005569 if (!AStmt)
5570 return StmtError();
5571
5572 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005573
5574 getCurFunction()->setHasBranchProtectedScope();
5575
5576 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5577}
5578
Alexey Bataev6125da92014-07-21 11:26:11 +00005579StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5580 SourceLocation StartLoc,
5581 SourceLocation EndLoc) {
5582 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5583 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5584}
5585
Alexey Bataev346265e2015-09-25 10:37:12 +00005586StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5587 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005588 SourceLocation StartLoc,
5589 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005590 OMPClause *DependFound = nullptr;
5591 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005592 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005593 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005594 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005595 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005596 for (auto *C : Clauses) {
5597 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5598 DependFound = C;
5599 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5600 if (DependSourceClause) {
5601 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5602 << getOpenMPDirectiveName(OMPD_ordered)
5603 << getOpenMPClauseName(OMPC_depend) << 2;
5604 ErrorFound = true;
5605 } else
5606 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005607 if (DependSinkClause) {
5608 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5609 << 0;
5610 ErrorFound = true;
5611 }
5612 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5613 if (DependSourceClause) {
5614 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5615 << 1;
5616 ErrorFound = true;
5617 }
5618 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005619 }
5620 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005621 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005622 else if (C->getClauseKind() == OMPC_simd)
5623 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005624 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005625 if (!ErrorFound && !SC &&
5626 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005627 // OpenMP [2.8.1,simd Construct, Restrictions]
5628 // An ordered construct with the simd clause is the only OpenMP construct
5629 // that can appear in the simd region.
5630 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005631 ErrorFound = true;
5632 } else if (DependFound && (TC || SC)) {
5633 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5634 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5635 ErrorFound = true;
5636 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5637 Diag(DependFound->getLocStart(),
5638 diag::err_omp_ordered_directive_without_param);
5639 ErrorFound = true;
5640 } else if (TC || Clauses.empty()) {
5641 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5642 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5643 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5644 << (TC != nullptr);
5645 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5646 ErrorFound = true;
5647 }
5648 }
5649 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005650 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005651
5652 if (AStmt) {
5653 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5654
5655 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005656 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005657
5658 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005659}
5660
Alexey Bataev1d160b12015-03-13 12:27:31 +00005661namespace {
5662/// \brief Helper class for checking expression in 'omp atomic [update]'
5663/// construct.
5664class OpenMPAtomicUpdateChecker {
5665 /// \brief Error results for atomic update expressions.
5666 enum ExprAnalysisErrorCode {
5667 /// \brief A statement is not an expression statement.
5668 NotAnExpression,
5669 /// \brief Expression is not builtin binary or unary operation.
5670 NotABinaryOrUnaryExpression,
5671 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5672 NotAnUnaryIncDecExpression,
5673 /// \brief An expression is not of scalar type.
5674 NotAScalarType,
5675 /// \brief A binary operation is not an assignment operation.
5676 NotAnAssignmentOp,
5677 /// \brief RHS part of the binary operation is not a binary expression.
5678 NotABinaryExpression,
5679 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5680 /// expression.
5681 NotABinaryOperator,
5682 /// \brief RHS binary operation does not have reference to the updated LHS
5683 /// part.
5684 NotAnUpdateExpression,
5685 /// \brief No errors is found.
5686 NoError
5687 };
5688 /// \brief Reference to Sema.
5689 Sema &SemaRef;
5690 /// \brief A location for note diagnostics (when error is found).
5691 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005692 /// \brief 'x' lvalue part of the source atomic expression.
5693 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005694 /// \brief 'expr' rvalue part of the source atomic expression.
5695 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005696 /// \brief Helper expression of the form
5697 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5698 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5699 Expr *UpdateExpr;
5700 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5701 /// important for non-associative operations.
5702 bool IsXLHSInRHSPart;
5703 BinaryOperatorKind Op;
5704 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005705 /// \brief true if the source expression is a postfix unary operation, false
5706 /// if it is a prefix unary operation.
5707 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005708
5709public:
5710 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005711 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005712 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005713 /// \brief Check specified statement that it is suitable for 'atomic update'
5714 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005715 /// expression. If DiagId and NoteId == 0, then only check is performed
5716 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005717 /// \param DiagId Diagnostic which should be emitted if error is found.
5718 /// \param NoteId Diagnostic note for the main error message.
5719 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005720 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005721 /// \brief Return the 'x' lvalue part of the source atomic expression.
5722 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005723 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5724 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005725 /// \brief Return the update expression used in calculation of the updated
5726 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5727 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5728 Expr *getUpdateExpr() const { return UpdateExpr; }
5729 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5730 /// false otherwise.
5731 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5732
Alexey Bataevb78ca832015-04-01 03:33:17 +00005733 /// \brief true if the source expression is a postfix unary operation, false
5734 /// if it is a prefix unary operation.
5735 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5736
Alexey Bataev1d160b12015-03-13 12:27:31 +00005737private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005738 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5739 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005740};
5741} // namespace
5742
5743bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5744 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5745 ExprAnalysisErrorCode ErrorFound = NoError;
5746 SourceLocation ErrorLoc, NoteLoc;
5747 SourceRange ErrorRange, NoteRange;
5748 // Allowed constructs are:
5749 // x = x binop expr;
5750 // x = expr binop x;
5751 if (AtomicBinOp->getOpcode() == BO_Assign) {
5752 X = AtomicBinOp->getLHS();
5753 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5754 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5755 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5756 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5757 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005758 Op = AtomicInnerBinOp->getOpcode();
5759 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005760 auto *LHS = AtomicInnerBinOp->getLHS();
5761 auto *RHS = AtomicInnerBinOp->getRHS();
5762 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5763 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5764 /*Canonical=*/true);
5765 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5766 /*Canonical=*/true);
5767 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5768 /*Canonical=*/true);
5769 if (XId == LHSId) {
5770 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005771 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005772 } else if (XId == RHSId) {
5773 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005774 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005775 } else {
5776 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5777 ErrorRange = AtomicInnerBinOp->getSourceRange();
5778 NoteLoc = X->getExprLoc();
5779 NoteRange = X->getSourceRange();
5780 ErrorFound = NotAnUpdateExpression;
5781 }
5782 } else {
5783 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5784 ErrorRange = AtomicInnerBinOp->getSourceRange();
5785 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5786 NoteRange = SourceRange(NoteLoc, NoteLoc);
5787 ErrorFound = NotABinaryOperator;
5788 }
5789 } else {
5790 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5791 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5792 ErrorFound = NotABinaryExpression;
5793 }
5794 } else {
5795 ErrorLoc = AtomicBinOp->getExprLoc();
5796 ErrorRange = AtomicBinOp->getSourceRange();
5797 NoteLoc = AtomicBinOp->getOperatorLoc();
5798 NoteRange = SourceRange(NoteLoc, NoteLoc);
5799 ErrorFound = NotAnAssignmentOp;
5800 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005801 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005802 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5803 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5804 return true;
5805 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005806 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005807 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005808}
5809
5810bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5811 unsigned NoteId) {
5812 ExprAnalysisErrorCode ErrorFound = NoError;
5813 SourceLocation ErrorLoc, NoteLoc;
5814 SourceRange ErrorRange, NoteRange;
5815 // Allowed constructs are:
5816 // x++;
5817 // x--;
5818 // ++x;
5819 // --x;
5820 // x binop= expr;
5821 // x = x binop expr;
5822 // x = expr binop x;
5823 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5824 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5825 if (AtomicBody->getType()->isScalarType() ||
5826 AtomicBody->isInstantiationDependent()) {
5827 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5828 AtomicBody->IgnoreParenImpCasts())) {
5829 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005830 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005831 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005832 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005833 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005834 X = AtomicCompAssignOp->getLHS();
5835 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005836 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5837 AtomicBody->IgnoreParenImpCasts())) {
5838 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005839 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5840 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005841 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005842 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5843 // Check for Unary Operation
5844 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005845 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005846 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5847 OpLoc = AtomicUnaryOp->getOperatorLoc();
5848 X = AtomicUnaryOp->getSubExpr();
5849 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5850 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005851 } else {
5852 ErrorFound = NotAnUnaryIncDecExpression;
5853 ErrorLoc = AtomicUnaryOp->getExprLoc();
5854 ErrorRange = AtomicUnaryOp->getSourceRange();
5855 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5856 NoteRange = SourceRange(NoteLoc, NoteLoc);
5857 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005858 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005859 ErrorFound = NotABinaryOrUnaryExpression;
5860 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5861 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5862 }
5863 } else {
5864 ErrorFound = NotAScalarType;
5865 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5866 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5867 }
5868 } else {
5869 ErrorFound = NotAnExpression;
5870 NoteLoc = ErrorLoc = S->getLocStart();
5871 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5872 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005873 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005874 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5875 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5876 return true;
5877 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005878 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005879 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005880 // Build an update expression of form 'OpaqueValueExpr(x) binop
5881 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5882 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5883 auto *OVEX = new (SemaRef.getASTContext())
5884 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5885 auto *OVEExpr = new (SemaRef.getASTContext())
5886 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5887 auto Update =
5888 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5889 IsXLHSInRHSPart ? OVEExpr : OVEX);
5890 if (Update.isInvalid())
5891 return true;
5892 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5893 Sema::AA_Casting);
5894 if (Update.isInvalid())
5895 return true;
5896 UpdateExpr = Update.get();
5897 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005898 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005899}
5900
Alexey Bataev0162e452014-07-22 10:10:35 +00005901StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5902 Stmt *AStmt,
5903 SourceLocation StartLoc,
5904 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005905 if (!AStmt)
5906 return StmtError();
5907
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005908 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005909 // 1.2.2 OpenMP Language Terminology
5910 // Structured block - An executable statement with a single entry at the
5911 // top and a single exit at the bottom.
5912 // The point of exit cannot be a branch out of the structured block.
5913 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005914 OpenMPClauseKind AtomicKind = OMPC_unknown;
5915 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005916 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005917 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005918 C->getClauseKind() == OMPC_update ||
5919 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005920 if (AtomicKind != OMPC_unknown) {
5921 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5922 << SourceRange(C->getLocStart(), C->getLocEnd());
5923 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5924 << getOpenMPClauseName(AtomicKind);
5925 } else {
5926 AtomicKind = C->getClauseKind();
5927 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005928 }
5929 }
5930 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005931
Alexey Bataev459dec02014-07-24 06:46:57 +00005932 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005933 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5934 Body = EWC->getSubExpr();
5935
Alexey Bataev62cec442014-11-18 10:14:22 +00005936 Expr *X = nullptr;
5937 Expr *V = nullptr;
5938 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005939 Expr *UE = nullptr;
5940 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005941 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005942 // OpenMP [2.12.6, atomic Construct]
5943 // In the next expressions:
5944 // * x and v (as applicable) are both l-value expressions with scalar type.
5945 // * During the execution of an atomic region, multiple syntactic
5946 // occurrences of x must designate the same storage location.
5947 // * Neither of v and expr (as applicable) may access the storage location
5948 // designated by x.
5949 // * Neither of x and expr (as applicable) may access the storage location
5950 // designated by v.
5951 // * expr is an expression with scalar type.
5952 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5953 // * binop, binop=, ++, and -- are not overloaded operators.
5954 // * The expression x binop expr must be numerically equivalent to x binop
5955 // (expr). This requirement is satisfied if the operators in expr have
5956 // precedence greater than binop, or by using parentheses around expr or
5957 // subexpressions of expr.
5958 // * The expression expr binop x must be numerically equivalent to (expr)
5959 // binop x. This requirement is satisfied if the operators in expr have
5960 // precedence equal to or greater than binop, or by using parentheses around
5961 // expr or subexpressions of expr.
5962 // * For forms that allow multiple occurrences of x, the number of times
5963 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005964 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005965 enum {
5966 NotAnExpression,
5967 NotAnAssignmentOp,
5968 NotAScalarType,
5969 NotAnLValue,
5970 NoError
5971 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005972 SourceLocation ErrorLoc, NoteLoc;
5973 SourceRange ErrorRange, NoteRange;
5974 // If clause is read:
5975 // v = x;
5976 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5977 auto AtomicBinOp =
5978 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5979 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5980 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5981 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5982 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5983 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5984 if (!X->isLValue() || !V->isLValue()) {
5985 auto NotLValueExpr = X->isLValue() ? V : X;
5986 ErrorFound = NotAnLValue;
5987 ErrorLoc = AtomicBinOp->getExprLoc();
5988 ErrorRange = AtomicBinOp->getSourceRange();
5989 NoteLoc = NotLValueExpr->getExprLoc();
5990 NoteRange = NotLValueExpr->getSourceRange();
5991 }
5992 } else if (!X->isInstantiationDependent() ||
5993 !V->isInstantiationDependent()) {
5994 auto NotScalarExpr =
5995 (X->isInstantiationDependent() || X->getType()->isScalarType())
5996 ? V
5997 : X;
5998 ErrorFound = NotAScalarType;
5999 ErrorLoc = AtomicBinOp->getExprLoc();
6000 ErrorRange = AtomicBinOp->getSourceRange();
6001 NoteLoc = NotScalarExpr->getExprLoc();
6002 NoteRange = NotScalarExpr->getSourceRange();
6003 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006004 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006005 ErrorFound = NotAnAssignmentOp;
6006 ErrorLoc = AtomicBody->getExprLoc();
6007 ErrorRange = AtomicBody->getSourceRange();
6008 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6009 : AtomicBody->getExprLoc();
6010 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6011 : AtomicBody->getSourceRange();
6012 }
6013 } else {
6014 ErrorFound = NotAnExpression;
6015 NoteLoc = ErrorLoc = Body->getLocStart();
6016 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006017 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006018 if (ErrorFound != NoError) {
6019 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6020 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006021 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6022 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006023 return StmtError();
6024 } else if (CurContext->isDependentContext())
6025 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006026 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006027 enum {
6028 NotAnExpression,
6029 NotAnAssignmentOp,
6030 NotAScalarType,
6031 NotAnLValue,
6032 NoError
6033 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006034 SourceLocation ErrorLoc, NoteLoc;
6035 SourceRange ErrorRange, NoteRange;
6036 // If clause is write:
6037 // x = expr;
6038 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
6039 auto AtomicBinOp =
6040 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6041 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006042 X = AtomicBinOp->getLHS();
6043 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006044 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6045 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6046 if (!X->isLValue()) {
6047 ErrorFound = NotAnLValue;
6048 ErrorLoc = AtomicBinOp->getExprLoc();
6049 ErrorRange = AtomicBinOp->getSourceRange();
6050 NoteLoc = X->getExprLoc();
6051 NoteRange = X->getSourceRange();
6052 }
6053 } else if (!X->isInstantiationDependent() ||
6054 !E->isInstantiationDependent()) {
6055 auto NotScalarExpr =
6056 (X->isInstantiationDependent() || X->getType()->isScalarType())
6057 ? E
6058 : X;
6059 ErrorFound = NotAScalarType;
6060 ErrorLoc = AtomicBinOp->getExprLoc();
6061 ErrorRange = AtomicBinOp->getSourceRange();
6062 NoteLoc = NotScalarExpr->getExprLoc();
6063 NoteRange = NotScalarExpr->getSourceRange();
6064 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006065 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006066 ErrorFound = NotAnAssignmentOp;
6067 ErrorLoc = AtomicBody->getExprLoc();
6068 ErrorRange = AtomicBody->getSourceRange();
6069 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6070 : AtomicBody->getExprLoc();
6071 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6072 : AtomicBody->getSourceRange();
6073 }
6074 } else {
6075 ErrorFound = NotAnExpression;
6076 NoteLoc = ErrorLoc = Body->getLocStart();
6077 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006078 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006079 if (ErrorFound != NoError) {
6080 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6081 << ErrorRange;
6082 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6083 << NoteRange;
6084 return StmtError();
6085 } else if (CurContext->isDependentContext())
6086 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006087 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006088 // If clause is update:
6089 // x++;
6090 // x--;
6091 // ++x;
6092 // --x;
6093 // x binop= expr;
6094 // x = x binop expr;
6095 // x = expr binop x;
6096 OpenMPAtomicUpdateChecker Checker(*this);
6097 if (Checker.checkStatement(
6098 Body, (AtomicKind == OMPC_update)
6099 ? diag::err_omp_atomic_update_not_expression_statement
6100 : diag::err_omp_atomic_not_expression_statement,
6101 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006102 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006103 if (!CurContext->isDependentContext()) {
6104 E = Checker.getExpr();
6105 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006106 UE = Checker.getUpdateExpr();
6107 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006108 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006109 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006110 enum {
6111 NotAnAssignmentOp,
6112 NotACompoundStatement,
6113 NotTwoSubstatements,
6114 NotASpecificExpression,
6115 NoError
6116 } ErrorFound = NoError;
6117 SourceLocation ErrorLoc, NoteLoc;
6118 SourceRange ErrorRange, NoteRange;
6119 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6120 // If clause is a capture:
6121 // v = x++;
6122 // v = x--;
6123 // v = ++x;
6124 // v = --x;
6125 // v = x binop= expr;
6126 // v = x = x binop expr;
6127 // v = x = expr binop x;
6128 auto *AtomicBinOp =
6129 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6130 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6131 V = AtomicBinOp->getLHS();
6132 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6133 OpenMPAtomicUpdateChecker Checker(*this);
6134 if (Checker.checkStatement(
6135 Body, diag::err_omp_atomic_capture_not_expression_statement,
6136 diag::note_omp_atomic_update))
6137 return StmtError();
6138 E = Checker.getExpr();
6139 X = Checker.getX();
6140 UE = Checker.getUpdateExpr();
6141 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6142 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006143 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006144 ErrorLoc = AtomicBody->getExprLoc();
6145 ErrorRange = AtomicBody->getSourceRange();
6146 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6147 : AtomicBody->getExprLoc();
6148 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6149 : AtomicBody->getSourceRange();
6150 ErrorFound = NotAnAssignmentOp;
6151 }
6152 if (ErrorFound != NoError) {
6153 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6154 << ErrorRange;
6155 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6156 return StmtError();
6157 } else if (CurContext->isDependentContext()) {
6158 UE = V = E = X = nullptr;
6159 }
6160 } else {
6161 // If clause is a capture:
6162 // { v = x; x = expr; }
6163 // { v = x; x++; }
6164 // { v = x; x--; }
6165 // { v = x; ++x; }
6166 // { v = x; --x; }
6167 // { v = x; x binop= expr; }
6168 // { v = x; x = x binop expr; }
6169 // { v = x; x = expr binop x; }
6170 // { x++; v = x; }
6171 // { x--; v = x; }
6172 // { ++x; v = x; }
6173 // { --x; v = x; }
6174 // { x binop= expr; v = x; }
6175 // { x = x binop expr; v = x; }
6176 // { x = expr binop x; v = x; }
6177 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6178 // Check that this is { expr1; expr2; }
6179 if (CS->size() == 2) {
6180 auto *First = CS->body_front();
6181 auto *Second = CS->body_back();
6182 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6183 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6184 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6185 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6186 // Need to find what subexpression is 'v' and what is 'x'.
6187 OpenMPAtomicUpdateChecker Checker(*this);
6188 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6189 BinaryOperator *BinOp = nullptr;
6190 if (IsUpdateExprFound) {
6191 BinOp = dyn_cast<BinaryOperator>(First);
6192 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6193 }
6194 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6195 // { v = x; x++; }
6196 // { v = x; x--; }
6197 // { v = x; ++x; }
6198 // { v = x; --x; }
6199 // { v = x; x binop= expr; }
6200 // { v = x; x = x binop expr; }
6201 // { v = x; x = expr binop x; }
6202 // Check that the first expression has form v = x.
6203 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6204 llvm::FoldingSetNodeID XId, PossibleXId;
6205 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6206 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6207 IsUpdateExprFound = XId == PossibleXId;
6208 if (IsUpdateExprFound) {
6209 V = BinOp->getLHS();
6210 X = Checker.getX();
6211 E = Checker.getExpr();
6212 UE = Checker.getUpdateExpr();
6213 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006214 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006215 }
6216 }
6217 if (!IsUpdateExprFound) {
6218 IsUpdateExprFound = !Checker.checkStatement(First);
6219 BinOp = nullptr;
6220 if (IsUpdateExprFound) {
6221 BinOp = dyn_cast<BinaryOperator>(Second);
6222 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6223 }
6224 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6225 // { x++; v = x; }
6226 // { x--; v = x; }
6227 // { ++x; v = x; }
6228 // { --x; v = x; }
6229 // { x binop= expr; v = x; }
6230 // { x = x binop expr; v = x; }
6231 // { x = expr binop x; v = x; }
6232 // Check that the second expression has form v = x.
6233 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6234 llvm::FoldingSetNodeID XId, PossibleXId;
6235 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6236 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6237 IsUpdateExprFound = XId == PossibleXId;
6238 if (IsUpdateExprFound) {
6239 V = BinOp->getLHS();
6240 X = Checker.getX();
6241 E = Checker.getExpr();
6242 UE = Checker.getUpdateExpr();
6243 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006244 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006245 }
6246 }
6247 }
6248 if (!IsUpdateExprFound) {
6249 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006250 auto *FirstExpr = dyn_cast<Expr>(First);
6251 auto *SecondExpr = dyn_cast<Expr>(Second);
6252 if (!FirstExpr || !SecondExpr ||
6253 !(FirstExpr->isInstantiationDependent() ||
6254 SecondExpr->isInstantiationDependent())) {
6255 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6256 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006257 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006258 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6259 : First->getLocStart();
6260 NoteRange = ErrorRange = FirstBinOp
6261 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006262 : SourceRange(ErrorLoc, ErrorLoc);
6263 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006264 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6265 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6266 ErrorFound = NotAnAssignmentOp;
6267 NoteLoc = ErrorLoc = SecondBinOp
6268 ? SecondBinOp->getOperatorLoc()
6269 : Second->getLocStart();
6270 NoteRange = ErrorRange =
6271 SecondBinOp ? SecondBinOp->getSourceRange()
6272 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006273 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006274 auto *PossibleXRHSInFirst =
6275 FirstBinOp->getRHS()->IgnoreParenImpCasts();
6276 auto *PossibleXLHSInSecond =
6277 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6278 llvm::FoldingSetNodeID X1Id, X2Id;
6279 PossibleXRHSInFirst->Profile(X1Id, Context,
6280 /*Canonical=*/true);
6281 PossibleXLHSInSecond->Profile(X2Id, Context,
6282 /*Canonical=*/true);
6283 IsUpdateExprFound = X1Id == X2Id;
6284 if (IsUpdateExprFound) {
6285 V = FirstBinOp->getLHS();
6286 X = SecondBinOp->getLHS();
6287 E = SecondBinOp->getRHS();
6288 UE = nullptr;
6289 IsXLHSInRHSPart = false;
6290 IsPostfixUpdate = true;
6291 } else {
6292 ErrorFound = NotASpecificExpression;
6293 ErrorLoc = FirstBinOp->getExprLoc();
6294 ErrorRange = FirstBinOp->getSourceRange();
6295 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6296 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6297 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006298 }
6299 }
6300 }
6301 }
6302 } else {
6303 NoteLoc = ErrorLoc = Body->getLocStart();
6304 NoteRange = ErrorRange =
6305 SourceRange(Body->getLocStart(), Body->getLocStart());
6306 ErrorFound = NotTwoSubstatements;
6307 }
6308 } else {
6309 NoteLoc = ErrorLoc = Body->getLocStart();
6310 NoteRange = ErrorRange =
6311 SourceRange(Body->getLocStart(), Body->getLocStart());
6312 ErrorFound = NotACompoundStatement;
6313 }
6314 if (ErrorFound != NoError) {
6315 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6316 << ErrorRange;
6317 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6318 return StmtError();
6319 } else if (CurContext->isDependentContext()) {
6320 UE = V = E = X = nullptr;
6321 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006322 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006323 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006324
6325 getCurFunction()->setHasBranchProtectedScope();
6326
Alexey Bataev62cec442014-11-18 10:14:22 +00006327 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006328 X, V, E, UE, IsXLHSInRHSPart,
6329 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006330}
6331
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006332StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6333 Stmt *AStmt,
6334 SourceLocation StartLoc,
6335 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006336 if (!AStmt)
6337 return StmtError();
6338
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006339 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6340 // 1.2.2 OpenMP Language Terminology
6341 // Structured block - An executable statement with a single entry at the
6342 // top and a single exit at the bottom.
6343 // The point of exit cannot be a branch out of the structured block.
6344 // longjmp() and throw() must not violate the entry/exit criteria.
6345 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006346
Alexey Bataev13314bf2014-10-09 04:18:56 +00006347 // OpenMP [2.16, Nesting of Regions]
6348 // If specified, a teams construct must be contained within a target
6349 // construct. That target construct must contain no statements or directives
6350 // outside of the teams construct.
6351 if (DSAStack->hasInnerTeamsRegion()) {
6352 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6353 bool OMPTeamsFound = true;
6354 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6355 auto I = CS->body_begin();
6356 while (I != CS->body_end()) {
6357 auto OED = dyn_cast<OMPExecutableDirective>(*I);
6358 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6359 OMPTeamsFound = false;
6360 break;
6361 }
6362 ++I;
6363 }
6364 assert(I != CS->body_end() && "Not found statement");
6365 S = *I;
6366 }
6367 if (!OMPTeamsFound) {
6368 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6369 Diag(DSAStack->getInnerTeamsRegionLoc(),
6370 diag::note_omp_nested_teams_construct_here);
6371 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6372 << isa<OMPExecutableDirective>(S);
6373 return StmtError();
6374 }
6375 }
6376
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006377 getCurFunction()->setHasBranchProtectedScope();
6378
6379 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6380}
6381
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006382StmtResult
6383Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6384 Stmt *AStmt, SourceLocation StartLoc,
6385 SourceLocation EndLoc) {
6386 if (!AStmt)
6387 return StmtError();
6388
6389 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6390 // 1.2.2 OpenMP Language Terminology
6391 // Structured block - An executable statement with a single entry at the
6392 // top and a single exit at the bottom.
6393 // The point of exit cannot be a branch out of the structured block.
6394 // longjmp() and throw() must not violate the entry/exit criteria.
6395 CS->getCapturedDecl()->setNothrow();
6396
6397 getCurFunction()->setHasBranchProtectedScope();
6398
6399 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6400 AStmt);
6401}
6402
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006403StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6404 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6405 SourceLocation EndLoc,
6406 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6407 if (!AStmt)
6408 return StmtError();
6409
6410 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6411 // 1.2.2 OpenMP Language Terminology
6412 // Structured block - An executable statement with a single entry at the
6413 // top and a single exit at the bottom.
6414 // The point of exit cannot be a branch out of the structured block.
6415 // longjmp() and throw() must not violate the entry/exit criteria.
6416 CS->getCapturedDecl()->setNothrow();
6417
6418 OMPLoopDirective::HelperExprs B;
6419 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6420 // define the nested loops number.
6421 unsigned NestedLoopCount =
6422 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6423 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6424 VarsWithImplicitDSA, B);
6425 if (NestedLoopCount == 0)
6426 return StmtError();
6427
6428 assert((CurContext->isDependentContext() || B.builtAll()) &&
6429 "omp target parallel for loop exprs were not built");
6430
6431 if (!CurContext->isDependentContext()) {
6432 // Finalize the clauses that need pre-built expressions for CodeGen.
6433 for (auto C : Clauses) {
6434 if (auto LC = dyn_cast<OMPLinearClause>(C))
6435 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006436 B.NumIterations, *this, CurScope,
6437 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006438 return StmtError();
6439 }
6440 }
6441
6442 getCurFunction()->setHasBranchProtectedScope();
6443 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6444 NestedLoopCount, Clauses, AStmt,
6445 B, DSAStack->isCancelRegion());
6446}
6447
Samuel Antaodf67fc42016-01-19 19:15:56 +00006448/// \brief Check for existence of a map clause in the list of clauses.
6449static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
6450 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6451 I != E; ++I) {
6452 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6453 return true;
6454 }
6455 }
6456
6457 return false;
6458}
6459
Michael Wong65f367f2015-07-21 13:44:28 +00006460StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6461 Stmt *AStmt,
6462 SourceLocation StartLoc,
6463 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006464 if (!AStmt)
6465 return StmtError();
6466
6467 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6468
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006469 // OpenMP [2.10.1, Restrictions, p. 97]
6470 // At least one map clause must appear on the directive.
6471 if (!HasMapClause(Clauses)) {
6472 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6473 getOpenMPDirectiveName(OMPD_target_data);
6474 return StmtError();
6475 }
6476
Michael Wong65f367f2015-07-21 13:44:28 +00006477 getCurFunction()->setHasBranchProtectedScope();
6478
6479 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6480 AStmt);
6481}
6482
Samuel Antaodf67fc42016-01-19 19:15:56 +00006483StmtResult
6484Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6485 SourceLocation StartLoc,
6486 SourceLocation EndLoc) {
6487 // OpenMP [2.10.2, Restrictions, p. 99]
6488 // At least one map clause must appear on the directive.
6489 if (!HasMapClause(Clauses)) {
6490 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6491 << getOpenMPDirectiveName(OMPD_target_enter_data);
6492 return StmtError();
6493 }
6494
6495 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6496 Clauses);
6497}
6498
Samuel Antao72590762016-01-19 20:04:50 +00006499StmtResult
6500Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6501 SourceLocation StartLoc,
6502 SourceLocation EndLoc) {
6503 // OpenMP [2.10.3, Restrictions, p. 102]
6504 // At least one map clause must appear on the directive.
6505 if (!HasMapClause(Clauses)) {
6506 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6507 << getOpenMPDirectiveName(OMPD_target_exit_data);
6508 return StmtError();
6509 }
6510
6511 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6512}
6513
Alexey Bataev13314bf2014-10-09 04:18:56 +00006514StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6515 Stmt *AStmt, SourceLocation StartLoc,
6516 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006517 if (!AStmt)
6518 return StmtError();
6519
Alexey Bataev13314bf2014-10-09 04:18:56 +00006520 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6521 // 1.2.2 OpenMP Language Terminology
6522 // Structured block - An executable statement with a single entry at the
6523 // top and a single exit at the bottom.
6524 // The point of exit cannot be a branch out of the structured block.
6525 // longjmp() and throw() must not violate the entry/exit criteria.
6526 CS->getCapturedDecl()->setNothrow();
6527
6528 getCurFunction()->setHasBranchProtectedScope();
6529
6530 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6531}
6532
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006533StmtResult
6534Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6535 SourceLocation EndLoc,
6536 OpenMPDirectiveKind CancelRegion) {
6537 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6538 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6539 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6540 << getOpenMPDirectiveName(CancelRegion);
6541 return StmtError();
6542 }
6543 if (DSAStack->isParentNowaitRegion()) {
6544 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6545 return StmtError();
6546 }
6547 if (DSAStack->isParentOrderedRegion()) {
6548 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6549 return StmtError();
6550 }
6551 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6552 CancelRegion);
6553}
6554
Alexey Bataev87933c72015-09-18 08:07:34 +00006555StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6556 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006557 SourceLocation EndLoc,
6558 OpenMPDirectiveKind CancelRegion) {
6559 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6560 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6561 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6562 << getOpenMPDirectiveName(CancelRegion);
6563 return StmtError();
6564 }
6565 if (DSAStack->isParentNowaitRegion()) {
6566 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6567 return StmtError();
6568 }
6569 if (DSAStack->isParentOrderedRegion()) {
6570 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6571 return StmtError();
6572 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006573 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006574 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6575 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006576}
6577
Alexey Bataev382967a2015-12-08 12:06:20 +00006578static bool checkGrainsizeNumTasksClauses(Sema &S,
6579 ArrayRef<OMPClause *> Clauses) {
6580 OMPClause *PrevClause = nullptr;
6581 bool ErrorFound = false;
6582 for (auto *C : Clauses) {
6583 if (C->getClauseKind() == OMPC_grainsize ||
6584 C->getClauseKind() == OMPC_num_tasks) {
6585 if (!PrevClause)
6586 PrevClause = C;
6587 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6588 S.Diag(C->getLocStart(),
6589 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6590 << getOpenMPClauseName(C->getClauseKind())
6591 << getOpenMPClauseName(PrevClause->getClauseKind());
6592 S.Diag(PrevClause->getLocStart(),
6593 diag::note_omp_previous_grainsize_num_tasks)
6594 << getOpenMPClauseName(PrevClause->getClauseKind());
6595 ErrorFound = true;
6596 }
6597 }
6598 }
6599 return ErrorFound;
6600}
6601
Alexey Bataev49f6e782015-12-01 04:18:41 +00006602StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6603 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6604 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006605 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006606 if (!AStmt)
6607 return StmtError();
6608
6609 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6610 OMPLoopDirective::HelperExprs B;
6611 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6612 // define the nested loops number.
6613 unsigned NestedLoopCount =
6614 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006615 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006616 VarsWithImplicitDSA, B);
6617 if (NestedLoopCount == 0)
6618 return StmtError();
6619
6620 assert((CurContext->isDependentContext() || B.builtAll()) &&
6621 "omp for loop exprs were not built");
6622
Alexey Bataev382967a2015-12-08 12:06:20 +00006623 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6624 // The grainsize clause and num_tasks clause are mutually exclusive and may
6625 // not appear on the same taskloop directive.
6626 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6627 return StmtError();
6628
Alexey Bataev49f6e782015-12-01 04:18:41 +00006629 getCurFunction()->setHasBranchProtectedScope();
6630 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6631 NestedLoopCount, Clauses, AStmt, B);
6632}
6633
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006634StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6635 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6636 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006637 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006638 if (!AStmt)
6639 return StmtError();
6640
6641 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6642 OMPLoopDirective::HelperExprs B;
6643 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6644 // define the nested loops number.
6645 unsigned NestedLoopCount =
6646 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6647 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6648 VarsWithImplicitDSA, B);
6649 if (NestedLoopCount == 0)
6650 return StmtError();
6651
6652 assert((CurContext->isDependentContext() || B.builtAll()) &&
6653 "omp for loop exprs were not built");
6654
Alexey Bataev5a3af132016-03-29 08:58:54 +00006655 if (!CurContext->isDependentContext()) {
6656 // Finalize the clauses that need pre-built expressions for CodeGen.
6657 for (auto C : Clauses) {
6658 if (auto LC = dyn_cast<OMPLinearClause>(C))
6659 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006660 B.NumIterations, *this, CurScope,
6661 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00006662 return StmtError();
6663 }
6664 }
6665
Alexey Bataev382967a2015-12-08 12:06:20 +00006666 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6667 // The grainsize clause and num_tasks clause are mutually exclusive and may
6668 // not appear on the same taskloop directive.
6669 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6670 return StmtError();
6671
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006672 getCurFunction()->setHasBranchProtectedScope();
6673 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6674 NestedLoopCount, Clauses, AStmt, B);
6675}
6676
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006677StmtResult Sema::ActOnOpenMPDistributeDirective(
6678 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6679 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006680 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006681 if (!AStmt)
6682 return StmtError();
6683
6684 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6685 OMPLoopDirective::HelperExprs B;
6686 // In presence of clause 'collapse' with number of loops, it will
6687 // define the nested loops number.
6688 unsigned NestedLoopCount =
6689 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6690 nullptr /*ordered not a clause on distribute*/, AStmt,
6691 *this, *DSAStack, VarsWithImplicitDSA, B);
6692 if (NestedLoopCount == 0)
6693 return StmtError();
6694
6695 assert((CurContext->isDependentContext() || B.builtAll()) &&
6696 "omp for loop exprs were not built");
6697
6698 getCurFunction()->setHasBranchProtectedScope();
6699 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6700 NestedLoopCount, Clauses, AStmt, B);
6701}
6702
Alexey Bataeved09d242014-05-28 05:53:51 +00006703OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006704 SourceLocation StartLoc,
6705 SourceLocation LParenLoc,
6706 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006707 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006708 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00006709 case OMPC_final:
6710 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6711 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00006712 case OMPC_num_threads:
6713 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6714 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006715 case OMPC_safelen:
6716 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6717 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00006718 case OMPC_simdlen:
6719 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6720 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00006721 case OMPC_collapse:
6722 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6723 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006724 case OMPC_ordered:
6725 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6726 break;
Michael Wonge710d542015-08-07 16:16:36 +00006727 case OMPC_device:
6728 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6729 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00006730 case OMPC_num_teams:
6731 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6732 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006733 case OMPC_thread_limit:
6734 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6735 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00006736 case OMPC_priority:
6737 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6738 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006739 case OMPC_grainsize:
6740 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6741 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00006742 case OMPC_num_tasks:
6743 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6744 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00006745 case OMPC_hint:
6746 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6747 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006748 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006749 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006750 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006751 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006752 case OMPC_private:
6753 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006754 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006755 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006756 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006757 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006758 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006759 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006760 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00006761 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006762 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006763 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006764 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006765 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006766 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006767 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006768 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006769 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006770 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006771 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006772 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006773 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006774 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006775 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006776 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006777 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006778 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00006779 case OMPC_uniform:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006780 llvm_unreachable("Clause is not allowed.");
6781 }
6782 return Res;
6783}
6784
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006785OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6786 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006787 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006788 SourceLocation NameModifierLoc,
6789 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006790 SourceLocation EndLoc) {
6791 Expr *ValExpr = Condition;
6792 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6793 !Condition->isInstantiationDependent() &&
6794 !Condition->containsUnexpandedParameterPack()) {
6795 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006796 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006797 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006798 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006799
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006800 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006801 }
6802
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006803 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6804 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006805}
6806
Alexey Bataev3778b602014-07-17 07:32:53 +00006807OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6808 SourceLocation StartLoc,
6809 SourceLocation LParenLoc,
6810 SourceLocation EndLoc) {
6811 Expr *ValExpr = Condition;
6812 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6813 !Condition->isInstantiationDependent() &&
6814 !Condition->containsUnexpandedParameterPack()) {
6815 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6816 Condition->getExprLoc(), Condition);
6817 if (Val.isInvalid())
6818 return nullptr;
6819
6820 ValExpr = Val.get();
6821 }
6822
6823 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6824}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006825ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6826 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006827 if (!Op)
6828 return ExprError();
6829
6830 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6831 public:
6832 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006833 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006834 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6835 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006836 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6837 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006838 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6839 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006840 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6841 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006842 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6843 QualType T,
6844 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006845 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6846 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006847 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6848 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006849 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006850 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006851 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006852 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6853 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006854 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6855 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006856 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6857 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006858 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006859 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006860 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006861 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6862 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006863 llvm_unreachable("conversion functions are permitted");
6864 }
6865 } ConvertDiagnoser;
6866 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6867}
6868
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006869static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006870 OpenMPClauseKind CKind,
6871 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006872 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6873 !ValExpr->isInstantiationDependent()) {
6874 SourceLocation Loc = ValExpr->getExprLoc();
6875 ExprResult Value =
6876 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6877 if (Value.isInvalid())
6878 return false;
6879
6880 ValExpr = Value.get();
6881 // The expression must evaluate to a non-negative integer value.
6882 llvm::APSInt Result;
6883 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006884 Result.isSigned() &&
6885 !((!StrictlyPositive && Result.isNonNegative()) ||
6886 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006887 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006888 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6889 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006890 return false;
6891 }
6892 }
6893 return true;
6894}
6895
Alexey Bataev568a8332014-03-06 06:15:19 +00006896OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6897 SourceLocation StartLoc,
6898 SourceLocation LParenLoc,
6899 SourceLocation EndLoc) {
6900 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006901
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006902 // OpenMP [2.5, Restrictions]
6903 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006904 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6905 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006906 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006907
Alexey Bataeved09d242014-05-28 05:53:51 +00006908 return new (Context)
6909 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006910}
6911
Alexey Bataev62c87d22014-03-21 04:51:18 +00006912ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006913 OpenMPClauseKind CKind,
6914 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006915 if (!E)
6916 return ExprError();
6917 if (E->isValueDependent() || E->isTypeDependent() ||
6918 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006919 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006920 llvm::APSInt Result;
6921 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6922 if (ICE.isInvalid())
6923 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006924 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6925 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006926 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006927 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6928 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006929 return ExprError();
6930 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006931 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6932 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6933 << E->getSourceRange();
6934 return ExprError();
6935 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006936 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6937 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006938 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006939 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006940 return ICE;
6941}
6942
6943OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6944 SourceLocation LParenLoc,
6945 SourceLocation EndLoc) {
6946 // OpenMP [2.8.1, simd construct, Description]
6947 // The parameter of the safelen clause must be a constant
6948 // positive integer expression.
6949 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6950 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006951 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006952 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006953 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006954}
6955
Alexey Bataev66b15b52015-08-21 11:14:16 +00006956OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6957 SourceLocation LParenLoc,
6958 SourceLocation EndLoc) {
6959 // OpenMP [2.8.1, simd construct, Description]
6960 // The parameter of the simdlen clause must be a constant
6961 // positive integer expression.
6962 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6963 if (Simdlen.isInvalid())
6964 return nullptr;
6965 return new (Context)
6966 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6967}
6968
Alexander Musman64d33f12014-06-04 07:53:32 +00006969OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6970 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006971 SourceLocation LParenLoc,
6972 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006973 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006974 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006975 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006976 // The parameter of the collapse clause must be a constant
6977 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006978 ExprResult NumForLoopsResult =
6979 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6980 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006981 return nullptr;
6982 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006983 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006984}
6985
Alexey Bataev10e775f2015-07-30 11:36:16 +00006986OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6987 SourceLocation EndLoc,
6988 SourceLocation LParenLoc,
6989 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006990 // OpenMP [2.7.1, loop construct, Description]
6991 // OpenMP [2.8.1, simd construct, Description]
6992 // OpenMP [2.9.6, distribute construct, Description]
6993 // The parameter of the ordered clause must be a constant
6994 // positive integer expression if any.
6995 if (NumForLoops && LParenLoc.isValid()) {
6996 ExprResult NumForLoopsResult =
6997 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6998 if (NumForLoopsResult.isInvalid())
6999 return nullptr;
7000 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00007001 } else
7002 NumForLoops = nullptr;
7003 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00007004 return new (Context)
7005 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
7006}
7007
Alexey Bataeved09d242014-05-28 05:53:51 +00007008OMPClause *Sema::ActOnOpenMPSimpleClause(
7009 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
7010 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007011 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007012 switch (Kind) {
7013 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007014 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00007015 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
7016 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007017 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007018 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00007019 Res = ActOnOpenMPProcBindClause(
7020 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
7021 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007022 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007023 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007024 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007025 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007026 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007027 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007028 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007029 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007030 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007031 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007032 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007033 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007034 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007035 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007036 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007037 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007038 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007039 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007040 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007041 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007042 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007043 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007044 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007045 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007046 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007047 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007048 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007049 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007050 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007051 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007052 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007053 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007054 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007055 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007056 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007057 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007058 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007059 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007060 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007061 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007062 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007063 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007064 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007065 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007066 llvm_unreachable("Clause is not allowed.");
7067 }
7068 return Res;
7069}
7070
Alexey Bataev6402bca2015-12-28 07:25:51 +00007071static std::string
7072getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
7073 ArrayRef<unsigned> Exclude = llvm::None) {
7074 std::string Values;
7075 unsigned Bound = Last >= 2 ? Last - 2 : 0;
7076 unsigned Skipped = Exclude.size();
7077 auto S = Exclude.begin(), E = Exclude.end();
7078 for (unsigned i = First; i < Last; ++i) {
7079 if (std::find(S, E, i) != E) {
7080 --Skipped;
7081 continue;
7082 }
7083 Values += "'";
7084 Values += getOpenMPSimpleClauseTypeName(K, i);
7085 Values += "'";
7086 if (i == Bound - Skipped)
7087 Values += " or ";
7088 else if (i != Bound + 1 - Skipped)
7089 Values += ", ";
7090 }
7091 return Values;
7092}
7093
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007094OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
7095 SourceLocation KindKwLoc,
7096 SourceLocation StartLoc,
7097 SourceLocation LParenLoc,
7098 SourceLocation EndLoc) {
7099 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00007100 static_assert(OMPC_DEFAULT_unknown > 0,
7101 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007102 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007103 << getListOfPossibleValues(OMPC_default, /*First=*/0,
7104 /*Last=*/OMPC_DEFAULT_unknown)
7105 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007106 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007107 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00007108 switch (Kind) {
7109 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007110 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007111 break;
7112 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007113 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007114 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007115 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007116 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00007117 break;
7118 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007119 return new (Context)
7120 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007121}
7122
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007123OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
7124 SourceLocation KindKwLoc,
7125 SourceLocation StartLoc,
7126 SourceLocation LParenLoc,
7127 SourceLocation EndLoc) {
7128 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007129 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007130 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
7131 /*Last=*/OMPC_PROC_BIND_unknown)
7132 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007133 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007134 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007135 return new (Context)
7136 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007137}
7138
Alexey Bataev56dafe82014-06-20 07:16:17 +00007139OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007140 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007141 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007142 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007143 SourceLocation EndLoc) {
7144 OMPClause *Res = nullptr;
7145 switch (Kind) {
7146 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007147 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
7148 assert(Argument.size() == NumberOfElements &&
7149 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007150 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007151 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
7152 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
7153 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
7154 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
7155 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007156 break;
7157 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007158 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
7159 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
7160 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
7161 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007162 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00007163 case OMPC_dist_schedule:
7164 Res = ActOnOpenMPDistScheduleClause(
7165 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
7166 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
7167 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007168 case OMPC_defaultmap:
7169 enum { Modifier, DefaultmapKind };
7170 Res = ActOnOpenMPDefaultmapClause(
7171 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
7172 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
7173 StartLoc, LParenLoc, ArgumentLoc[Modifier],
7174 ArgumentLoc[DefaultmapKind], EndLoc);
7175 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00007176 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007177 case OMPC_num_threads:
7178 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007179 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007180 case OMPC_collapse:
7181 case OMPC_default:
7182 case OMPC_proc_bind:
7183 case OMPC_private:
7184 case OMPC_firstprivate:
7185 case OMPC_lastprivate:
7186 case OMPC_shared:
7187 case OMPC_reduction:
7188 case OMPC_linear:
7189 case OMPC_aligned:
7190 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007191 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007192 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007193 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007194 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007195 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007196 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007197 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007198 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007199 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007200 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007201 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007202 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007203 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007204 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007205 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007206 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007207 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007208 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007209 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007210 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007211 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007212 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007213 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007214 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007215 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007216 case OMPC_uniform:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007217 llvm_unreachable("Clause is not allowed.");
7218 }
7219 return Res;
7220}
7221
Alexey Bataev6402bca2015-12-28 07:25:51 +00007222static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
7223 OpenMPScheduleClauseModifier M2,
7224 SourceLocation M1Loc, SourceLocation M2Loc) {
7225 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
7226 SmallVector<unsigned, 2> Excluded;
7227 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
7228 Excluded.push_back(M2);
7229 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
7230 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
7231 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
7232 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
7233 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
7234 << getListOfPossibleValues(OMPC_schedule,
7235 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
7236 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7237 Excluded)
7238 << getOpenMPClauseName(OMPC_schedule);
7239 return true;
7240 }
7241 return false;
7242}
7243
Alexey Bataev56dafe82014-06-20 07:16:17 +00007244OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007245 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007246 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007247 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
7248 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
7249 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
7250 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
7251 return nullptr;
7252 // OpenMP, 2.7.1, Loop Construct, Restrictions
7253 // Either the monotonic modifier or the nonmonotonic modifier can be specified
7254 // but not both.
7255 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
7256 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
7257 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
7258 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
7259 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
7260 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
7261 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
7262 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
7263 return nullptr;
7264 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007265 if (Kind == OMPC_SCHEDULE_unknown) {
7266 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00007267 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
7268 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
7269 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7270 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7271 Exclude);
7272 } else {
7273 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7274 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007275 }
7276 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
7277 << Values << getOpenMPClauseName(OMPC_schedule);
7278 return nullptr;
7279 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00007280 // OpenMP, 2.7.1, Loop Construct, Restrictions
7281 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
7282 // schedule(guided).
7283 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
7284 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
7285 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
7286 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
7287 diag::err_omp_schedule_nonmonotonic_static);
7288 return nullptr;
7289 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007290 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00007291 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00007292 if (ChunkSize) {
7293 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
7294 !ChunkSize->isInstantiationDependent() &&
7295 !ChunkSize->containsUnexpandedParameterPack()) {
7296 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
7297 ExprResult Val =
7298 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
7299 if (Val.isInvalid())
7300 return nullptr;
7301
7302 ValExpr = Val.get();
7303
7304 // OpenMP [2.7.1, Restrictions]
7305 // chunk_size must be a loop invariant integer expression with a positive
7306 // value.
7307 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00007308 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
7309 if (Result.isSigned() && !Result.isStrictlyPositive()) {
7310 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007311 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00007312 return nullptr;
7313 }
7314 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00007315 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7316 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7317 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007318 }
7319 }
7320 }
7321
Alexey Bataev6402bca2015-12-28 07:25:51 +00007322 return new (Context)
7323 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00007324 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007325}
7326
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007327OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
7328 SourceLocation StartLoc,
7329 SourceLocation EndLoc) {
7330 OMPClause *Res = nullptr;
7331 switch (Kind) {
7332 case OMPC_ordered:
7333 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
7334 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00007335 case OMPC_nowait:
7336 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
7337 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007338 case OMPC_untied:
7339 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
7340 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007341 case OMPC_mergeable:
7342 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
7343 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007344 case OMPC_read:
7345 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
7346 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00007347 case OMPC_write:
7348 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
7349 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00007350 case OMPC_update:
7351 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
7352 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00007353 case OMPC_capture:
7354 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
7355 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007356 case OMPC_seq_cst:
7357 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
7358 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00007359 case OMPC_threads:
7360 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
7361 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007362 case OMPC_simd:
7363 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
7364 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00007365 case OMPC_nogroup:
7366 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
7367 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007368 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007369 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007370 case OMPC_num_threads:
7371 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007372 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007373 case OMPC_collapse:
7374 case OMPC_schedule:
7375 case OMPC_private:
7376 case OMPC_firstprivate:
7377 case OMPC_lastprivate:
7378 case OMPC_shared:
7379 case OMPC_reduction:
7380 case OMPC_linear:
7381 case OMPC_aligned:
7382 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007383 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007384 case OMPC_default:
7385 case OMPC_proc_bind:
7386 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007387 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007388 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007389 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007390 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007391 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007392 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007393 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007394 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00007395 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007396 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007397 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007398 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007399 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007400 case OMPC_uniform:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007401 llvm_unreachable("Clause is not allowed.");
7402 }
7403 return Res;
7404}
7405
Alexey Bataev236070f2014-06-20 11:19:47 +00007406OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
7407 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007408 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00007409 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
7410}
7411
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007412OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
7413 SourceLocation EndLoc) {
7414 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
7415}
7416
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007417OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
7418 SourceLocation EndLoc) {
7419 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
7420}
7421
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007422OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
7423 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007424 return new (Context) OMPReadClause(StartLoc, EndLoc);
7425}
7426
Alexey Bataevdea47612014-07-23 07:46:59 +00007427OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
7428 SourceLocation EndLoc) {
7429 return new (Context) OMPWriteClause(StartLoc, EndLoc);
7430}
7431
Alexey Bataev67a4f222014-07-23 10:25:33 +00007432OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
7433 SourceLocation EndLoc) {
7434 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
7435}
7436
Alexey Bataev459dec02014-07-24 06:46:57 +00007437OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
7438 SourceLocation EndLoc) {
7439 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
7440}
7441
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007442OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
7443 SourceLocation EndLoc) {
7444 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
7445}
7446
Alexey Bataev346265e2015-09-25 10:37:12 +00007447OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
7448 SourceLocation EndLoc) {
7449 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
7450}
7451
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007452OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
7453 SourceLocation EndLoc) {
7454 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
7455}
7456
Alexey Bataevb825de12015-12-07 10:51:44 +00007457OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
7458 SourceLocation EndLoc) {
7459 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
7460}
7461
Alexey Bataevc5e02582014-06-16 07:08:35 +00007462OMPClause *Sema::ActOnOpenMPVarListClause(
7463 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
7464 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
7465 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007466 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00007467 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
7468 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
7469 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007470 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007471 switch (Kind) {
7472 case OMPC_private:
7473 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7474 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007475 case OMPC_firstprivate:
7476 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7477 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007478 case OMPC_lastprivate:
7479 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7480 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007481 case OMPC_shared:
7482 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
7483 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007484 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00007485 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
7486 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007487 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00007488 case OMPC_linear:
7489 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00007490 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00007491 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007492 case OMPC_aligned:
7493 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
7494 ColonLoc, EndLoc);
7495 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007496 case OMPC_copyin:
7497 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
7498 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007499 case OMPC_copyprivate:
7500 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7501 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00007502 case OMPC_flush:
7503 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
7504 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007505 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007506 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
7507 StartLoc, LParenLoc, EndLoc);
7508 break;
7509 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00007510 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
7511 DepLinMapLoc, ColonLoc, VarList, StartLoc,
7512 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007513 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007514 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007515 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007516 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007517 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007518 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007519 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007520 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007521 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007522 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007523 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007524 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007525 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007526 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007527 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007528 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007529 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007530 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007531 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007532 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00007533 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007534 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007535 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007536 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007537 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007538 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007539 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007540 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007541 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007542 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007543 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007544 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007545 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007546 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007547 llvm_unreachable("Clause is not allowed.");
7548 }
7549 return Res;
7550}
7551
Alexey Bataev90c228f2016-02-08 09:29:13 +00007552ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00007553 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00007554 ExprResult Res = BuildDeclRefExpr(
7555 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
7556 if (!Res.isUsable())
7557 return ExprError();
7558 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
7559 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
7560 if (!Res.isUsable())
7561 return ExprError();
7562 }
7563 if (VK != VK_LValue && Res.get()->isGLValue()) {
7564 Res = DefaultLvalueConversion(Res.get());
7565 if (!Res.isUsable())
7566 return ExprError();
7567 }
7568 return Res;
7569}
7570
Alexey Bataev60da77e2016-02-29 05:54:20 +00007571static std::pair<ValueDecl *, bool>
7572getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
7573 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007574 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7575 RefExpr->containsUnexpandedParameterPack())
7576 return std::make_pair(nullptr, true);
7577
Alexey Bataevd985eda2016-02-10 11:29:16 +00007578 // OpenMP [3.1, C/C++]
7579 // A list item is a variable name.
7580 // OpenMP [2.9.3.3, Restrictions, p.1]
7581 // A variable that is part of another variable (as an array or
7582 // structure element) cannot appear in a private clause.
7583 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007584 enum {
7585 NoArrayExpr = -1,
7586 ArraySubscript = 0,
7587 OMPArraySection = 1
7588 } IsArrayExpr = NoArrayExpr;
7589 if (AllowArraySection) {
7590 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
7591 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7592 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7593 Base = TempASE->getBase()->IgnoreParenImpCasts();
7594 RefExpr = Base;
7595 IsArrayExpr = ArraySubscript;
7596 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
7597 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7598 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7599 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7600 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7601 Base = TempASE->getBase()->IgnoreParenImpCasts();
7602 RefExpr = Base;
7603 IsArrayExpr = OMPArraySection;
7604 }
7605 }
7606 ELoc = RefExpr->getExprLoc();
7607 ERange = RefExpr->getSourceRange();
7608 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007609 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7610 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
7611 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
7612 (S.getCurrentThisType().isNull() || !ME ||
7613 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
7614 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00007615 if (IsArrayExpr != NoArrayExpr)
7616 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
7617 << ERange;
7618 else {
7619 S.Diag(ELoc,
7620 AllowArraySection
7621 ? diag::err_omp_expected_var_name_member_expr_or_array_item
7622 : diag::err_omp_expected_var_name_member_expr)
7623 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
7624 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007625 return std::make_pair(nullptr, false);
7626 }
7627 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
7628}
7629
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007630OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
7631 SourceLocation StartLoc,
7632 SourceLocation LParenLoc,
7633 SourceLocation EndLoc) {
7634 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00007635 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00007636 for (auto &RefExpr : VarList) {
7637 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007638 SourceLocation ELoc;
7639 SourceRange ERange;
7640 Expr *SimpleRefExpr = RefExpr;
7641 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007642 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007643 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007644 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007645 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007646 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007647 ValueDecl *D = Res.first;
7648 if (!D)
7649 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007650
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007651 QualType Type = D->getType();
7652 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007653
7654 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7655 // A variable that appears in a private clause must not have an incomplete
7656 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007657 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007658 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007659 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007660
Alexey Bataev758e55e2013-09-06 18:03:48 +00007661 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7662 // in a Construct]
7663 // Variables with the predetermined data-sharing attributes may not be
7664 // listed in data-sharing attributes clauses, except for the cases
7665 // listed below. For these exceptions only, listing a predetermined
7666 // variable in a data-sharing attribute clause is allowed and overrides
7667 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007668 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007669 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007670 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7671 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007672 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007673 continue;
7674 }
7675
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007676 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007677 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00007678 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007679 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7680 << getOpenMPClauseName(OMPC_private) << Type
7681 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7682 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007683 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007684 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007685 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007686 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007687 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007688 continue;
7689 }
7690
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007691 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7692 // A list item cannot appear in both a map clause and a data-sharing
7693 // attribute clause on the same construct
7694 if (DSAStack->getCurrentDirective() == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00007695 if (DSAStack->checkMappableExprComponentListsForDecl(
7696 VD, /* CurrentRegionOnly = */ true,
7697 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
7698 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007699 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7700 << getOpenMPClauseName(OMPC_private)
7701 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7702 ReportOriginalDSA(*this, DSAStack, D, DVar);
7703 continue;
7704 }
7705 }
7706
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007707 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
7708 // A variable of class type (or array thereof) that appears in a private
7709 // clause requires an accessible, unambiguous default constructor for the
7710 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00007711 // Generate helper private variable and initialize it with the default
7712 // value. The address of the original variable is replaced by the address of
7713 // the new private variable in CodeGen. This new variable is not added to
7714 // IdResolver, so the code in the OpenMP region uses original variable for
7715 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007716 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007717 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7718 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007719 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007720 if (VDPrivate->isInvalidDecl())
7721 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007722 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007723 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007724
Alexey Bataev90c228f2016-02-08 09:29:13 +00007725 DeclRefExpr *Ref = nullptr;
7726 if (!VD)
Alexey Bataev61205072016-03-02 04:57:40 +00007727 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00007728 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
7729 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007730 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007731 }
7732
Alexey Bataeved09d242014-05-28 05:53:51 +00007733 if (Vars.empty())
7734 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007735
Alexey Bataev03b340a2014-10-21 03:16:40 +00007736 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7737 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007738}
7739
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007740namespace {
7741class DiagsUninitializedSeveretyRAII {
7742private:
7743 DiagnosticsEngine &Diags;
7744 SourceLocation SavedLoc;
7745 bool IsIgnored;
7746
7747public:
7748 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
7749 bool IsIgnored)
7750 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
7751 if (!IsIgnored) {
7752 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
7753 /*Map*/ diag::Severity::Ignored, Loc);
7754 }
7755 }
7756 ~DiagsUninitializedSeveretyRAII() {
7757 if (!IsIgnored)
7758 Diags.popMappings(SavedLoc);
7759 }
7760};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007761}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007762
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007763OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
7764 SourceLocation StartLoc,
7765 SourceLocation LParenLoc,
7766 SourceLocation EndLoc) {
7767 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007768 SmallVector<Expr *, 8> PrivateCopies;
7769 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00007770 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007771 bool IsImplicitClause =
7772 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
7773 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
7774
Alexey Bataeved09d242014-05-28 05:53:51 +00007775 for (auto &RefExpr : VarList) {
7776 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007777 SourceLocation ELoc;
7778 SourceRange ERange;
7779 Expr *SimpleRefExpr = RefExpr;
7780 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007781 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007782 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007783 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007784 PrivateCopies.push_back(nullptr);
7785 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007786 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007787 ValueDecl *D = Res.first;
7788 if (!D)
7789 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007790
Alexey Bataev60da77e2016-02-29 05:54:20 +00007791 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007792 QualType Type = D->getType();
7793 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007794
7795 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7796 // A variable that appears in a private clause must not have an incomplete
7797 // type or a reference type.
7798 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00007799 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007800 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007801 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007802
7803 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
7804 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00007805 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007806 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007807 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007808
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007809 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00007810 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007811 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007812 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00007813 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007814 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007815 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
7816 // A list item that specifies a given variable may not appear in more
7817 // than one clause on the same directive, except that a variable may be
7818 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007819 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007820 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007821 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007822 << getOpenMPClauseName(DVar.CKind)
7823 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007824 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007825 continue;
7826 }
7827
7828 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7829 // in a Construct]
7830 // Variables with the predetermined data-sharing attributes may not be
7831 // listed in data-sharing attributes clauses, except for the cases
7832 // listed below. For these exceptions only, listing a predetermined
7833 // variable in a data-sharing attribute clause is allowed and overrides
7834 // the variable's predetermined data-sharing attributes.
7835 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7836 // in a Construct, C/C++, p.2]
7837 // Variables with const-qualified type having no mutable member may be
7838 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00007839 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007840 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7841 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007842 << getOpenMPClauseName(DVar.CKind)
7843 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007844 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007845 continue;
7846 }
7847
Alexey Bataevf29276e2014-06-18 04:14:57 +00007848 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007849 // OpenMP [2.9.3.4, Restrictions, p.2]
7850 // A list item that is private within a parallel region must not appear
7851 // in a firstprivate clause on a worksharing construct if any of the
7852 // worksharing regions arising from the worksharing construct ever bind
7853 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007854 if (isOpenMPWorksharingDirective(CurrDir) &&
7855 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007856 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007857 if (DVar.CKind != OMPC_shared &&
7858 (isOpenMPParallelDirective(DVar.DKind) ||
7859 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007860 Diag(ELoc, diag::err_omp_required_access)
7861 << getOpenMPClauseName(OMPC_firstprivate)
7862 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007863 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007864 continue;
7865 }
7866 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007867 // OpenMP [2.9.3.4, Restrictions, p.3]
7868 // A list item that appears in a reduction clause of a parallel construct
7869 // must not appear in a firstprivate clause on a worksharing or task
7870 // construct if any of the worksharing or task regions arising from the
7871 // worksharing or task construct ever bind to any of the parallel regions
7872 // arising from the parallel construct.
7873 // OpenMP [2.9.3.4, Restrictions, p.4]
7874 // A list item that appears in a reduction clause in worksharing
7875 // construct must not appear in a firstprivate clause in a task construct
7876 // encountered during execution of any of the worksharing regions arising
7877 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00007878 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00007879 DVar = DSAStack->hasInnermostDSA(
7880 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
7881 [](OpenMPDirectiveKind K) -> bool {
7882 return isOpenMPParallelDirective(K) ||
7883 isOpenMPWorksharingDirective(K);
7884 },
7885 false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007886 if (DVar.CKind == OMPC_reduction &&
7887 (isOpenMPParallelDirective(DVar.DKind) ||
7888 isOpenMPWorksharingDirective(DVar.DKind))) {
7889 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7890 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007891 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007892 continue;
7893 }
7894 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007895
7896 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7897 // A list item that is private within a teams region must not appear in a
7898 // firstprivate clause on a distribute construct if any of the distribute
7899 // regions arising from the distribute construct ever bind to any of the
7900 // teams regions arising from the teams construct.
7901 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7902 // A list item that appears in a reduction clause of a teams construct
7903 // must not appear in a firstprivate clause on a distribute construct if
7904 // any of the distribute regions arising from the distribute construct
7905 // ever bind to any of the teams regions arising from the teams construct.
7906 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7907 // A list item may appear in a firstprivate or lastprivate clause but not
7908 // both.
7909 if (CurrDir == OMPD_distribute) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00007910 DVar = DSAStack->hasInnermostDSA(
7911 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_private; },
7912 [](OpenMPDirectiveKind K) -> bool {
7913 return isOpenMPTeamsDirective(K);
7914 },
7915 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007916 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7917 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007918 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007919 continue;
7920 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00007921 DVar = DSAStack->hasInnermostDSA(
7922 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
7923 [](OpenMPDirectiveKind K) -> bool {
7924 return isOpenMPTeamsDirective(K);
7925 },
7926 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007927 if (DVar.CKind == OMPC_reduction &&
7928 isOpenMPTeamsDirective(DVar.DKind)) {
7929 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007930 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007931 continue;
7932 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007933 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007934 if (DVar.CKind == OMPC_lastprivate) {
7935 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007936 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007937 continue;
7938 }
7939 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007940 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7941 // A list item cannot appear in both a map clause and a data-sharing
7942 // attribute clause on the same construct
7943 if (CurrDir == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00007944 if (DSAStack->checkMappableExprComponentListsForDecl(
7945 VD, /* CurrentRegionOnly = */ true,
7946 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
7947 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007948 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7949 << getOpenMPClauseName(OMPC_firstprivate)
7950 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7951 ReportOriginalDSA(*this, DSAStack, D, DVar);
7952 continue;
7953 }
7954 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007955 }
7956
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007957 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007958 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00007959 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007960 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7961 << getOpenMPClauseName(OMPC_firstprivate) << Type
7962 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7963 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007964 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007965 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007966 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007967 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00007968 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007969 continue;
7970 }
7971
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007972 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007973 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7974 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007975 // Generate helper private variable and initialize it with the value of the
7976 // original variable. The address of the original variable is replaced by
7977 // the address of the new private variable in the CodeGen. This new variable
7978 // is not added to IdResolver, so the code in the OpenMP region uses
7979 // original variable for proper diagnostics and variable capturing.
7980 Expr *VDInitRefExpr = nullptr;
7981 // For arrays generate initializer for single element and replace it by the
7982 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007983 if (Type->isArrayType()) {
7984 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007985 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007986 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007987 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007988 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007989 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007990 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007991 InitializedEntity Entity =
7992 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007993 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7994
7995 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7996 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7997 if (Result.isInvalid())
7998 VDPrivate->setInvalidDecl();
7999 else
8000 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008001 // Remove temp variable declaration.
8002 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008003 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008004 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
8005 ".firstprivate.temp");
8006 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
8007 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00008008 AddInitializerToDecl(VDPrivate,
8009 DefaultLvalueConversion(VDInitRefExpr).get(),
8010 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008011 }
8012 if (VDPrivate->isInvalidDecl()) {
8013 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008014 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008015 diag::note_omp_task_predetermined_firstprivate_here);
8016 }
8017 continue;
8018 }
8019 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008020 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00008021 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
8022 RefExpr->getExprLoc());
8023 DeclRefExpr *Ref = nullptr;
Alexey Bataev417089f2016-02-17 13:19:37 +00008024 if (!VD) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008025 if (TopDVar.CKind == OMPC_lastprivate)
8026 Ref = TopDVar.PrivateCopy;
8027 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008028 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00008029 if (!IsOpenMPCapturedDecl(D))
8030 ExprCaptures.push_back(Ref->getDecl());
8031 }
Alexey Bataev417089f2016-02-17 13:19:37 +00008032 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008033 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
8034 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008035 PrivateCopies.push_back(VDPrivateRefExpr);
8036 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008037 }
8038
Alexey Bataeved09d242014-05-28 05:53:51 +00008039 if (Vars.empty())
8040 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008041
8042 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008043 Vars, PrivateCopies, Inits,
8044 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008045}
8046
Alexander Musman1bb328c2014-06-04 13:06:39 +00008047OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
8048 SourceLocation StartLoc,
8049 SourceLocation LParenLoc,
8050 SourceLocation EndLoc) {
8051 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00008052 SmallVector<Expr *, 8> SrcExprs;
8053 SmallVector<Expr *, 8> DstExprs;
8054 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00008055 SmallVector<Decl *, 4> ExprCaptures;
8056 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008057 for (auto &RefExpr : VarList) {
8058 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008059 SourceLocation ELoc;
8060 SourceRange ERange;
8061 Expr *SimpleRefExpr = RefExpr;
8062 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008063 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00008064 // It will be analyzed later.
8065 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00008066 SrcExprs.push_back(nullptr);
8067 DstExprs.push_back(nullptr);
8068 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008069 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008070 ValueDecl *D = Res.first;
8071 if (!D)
8072 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008073
Alexey Bataev74caaf22016-02-20 04:09:36 +00008074 QualType Type = D->getType();
8075 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008076
8077 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
8078 // A variable that appears in a lastprivate clause must not have an
8079 // incomplete type or a reference type.
8080 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00008081 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00008082 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008083 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00008084
8085 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8086 // in a Construct]
8087 // Variables with the predetermined data-sharing attributes may not be
8088 // listed in data-sharing attributes clauses, except for the cases
8089 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008090 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008091 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
8092 DVar.CKind != OMPC_firstprivate &&
8093 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
8094 Diag(ELoc, diag::err_omp_wrong_dsa)
8095 << getOpenMPClauseName(DVar.CKind)
8096 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008097 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008098 continue;
8099 }
8100
Alexey Bataevf29276e2014-06-18 04:14:57 +00008101 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8102 // OpenMP [2.14.3.5, Restrictions, p.2]
8103 // A list item that is private within a parallel region, or that appears in
8104 // the reduction clause of a parallel construct, must not appear in a
8105 // lastprivate clause on a worksharing construct if any of the corresponding
8106 // worksharing regions ever binds to any of the corresponding parallel
8107 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00008108 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00008109 if (isOpenMPWorksharingDirective(CurrDir) &&
8110 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00008111 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008112 if (DVar.CKind != OMPC_shared) {
8113 Diag(ELoc, diag::err_omp_required_access)
8114 << getOpenMPClauseName(OMPC_lastprivate)
8115 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008116 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008117 continue;
8118 }
8119 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008120
8121 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8122 // A list item may appear in a firstprivate or lastprivate clause but not
8123 // both.
8124 if (CurrDir == OMPD_distribute) {
8125 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8126 if (DVar.CKind == OMPC_firstprivate) {
8127 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
8128 ReportOriginalDSA(*this, DSAStack, D, DVar);
8129 continue;
8130 }
8131 }
8132
Alexander Musman1bb328c2014-06-04 13:06:39 +00008133 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00008134 // A variable of class type (or array thereof) that appears in a
8135 // lastprivate clause requires an accessible, unambiguous default
8136 // constructor for the class type, unless the list item is also specified
8137 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00008138 // A variable of class type (or array thereof) that appears in a
8139 // lastprivate clause requires an accessible, unambiguous copy assignment
8140 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00008141 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008142 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008143 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008144 D->hasAttrs() ? &D->getAttrs() : nullptr);
8145 auto *PseudoSrcExpr =
8146 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008147 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008148 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008149 D->hasAttrs() ? &D->getAttrs() : nullptr);
8150 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008151 // For arrays generate assignment operation for single element and replace
8152 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008153 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00008154 PseudoDstExpr, PseudoSrcExpr);
8155 if (AssignmentOp.isInvalid())
8156 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00008157 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00008158 /*DiscardedValue=*/true);
8159 if (AssignmentOp.isInvalid())
8160 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008161
Alexey Bataev74caaf22016-02-20 04:09:36 +00008162 DeclRefExpr *Ref = nullptr;
Alexey Bataev005248a2016-02-25 05:25:57 +00008163 if (!VD) {
8164 if (TopDVar.CKind == OMPC_firstprivate)
8165 Ref = TopDVar.PrivateCopy;
8166 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008167 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008168 if (!IsOpenMPCapturedDecl(D))
8169 ExprCaptures.push_back(Ref->getDecl());
8170 }
8171 if (TopDVar.CKind == OMPC_firstprivate ||
8172 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008173 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008174 ExprResult RefRes = DefaultLvalueConversion(Ref);
8175 if (!RefRes.isUsable())
8176 continue;
8177 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008178 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
8179 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008180 if (!PostUpdateRes.isUsable())
8181 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008182 ExprPostUpdates.push_back(
8183 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008184 }
8185 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008186 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008187 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00008188 SrcExprs.push_back(PseudoSrcExpr);
8189 DstExprs.push_back(PseudoDstExpr);
8190 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00008191 }
8192
8193 if (Vars.empty())
8194 return nullptr;
8195
8196 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00008197 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008198 buildPreInits(Context, ExprCaptures),
8199 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00008200}
8201
Alexey Bataev758e55e2013-09-06 18:03:48 +00008202OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
8203 SourceLocation StartLoc,
8204 SourceLocation LParenLoc,
8205 SourceLocation EndLoc) {
8206 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00008207 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008208 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008209 SourceLocation ELoc;
8210 SourceRange ERange;
8211 Expr *SimpleRefExpr = RefExpr;
8212 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008213 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00008214 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008215 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008216 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008217 ValueDecl *D = Res.first;
8218 if (!D)
8219 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008220
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008221 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008222 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8223 // in a Construct]
8224 // Variables with the predetermined data-sharing attributes may not be
8225 // listed in data-sharing attributes clauses, except for the cases
8226 // listed below. For these exceptions only, listing a predetermined
8227 // variable in a data-sharing attribute clause is allowed and overrides
8228 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008229 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00008230 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
8231 DVar.RefExpr) {
8232 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8233 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008234 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008235 continue;
8236 }
8237
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008238 DeclRefExpr *Ref = nullptr;
Alexey Bataev1efd1662016-03-29 10:59:56 +00008239 if (!VD && IsOpenMPCapturedDecl(D))
Alexey Bataev61205072016-03-02 04:57:40 +00008240 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008241 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataev1efd1662016-03-29 10:59:56 +00008242 Vars.push_back((VD || !Ref) ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008243 }
8244
Alexey Bataeved09d242014-05-28 05:53:51 +00008245 if (Vars.empty())
8246 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008247
8248 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
8249}
8250
Alexey Bataevc5e02582014-06-16 07:08:35 +00008251namespace {
8252class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
8253 DSAStackTy *Stack;
8254
8255public:
8256 bool VisitDeclRefExpr(DeclRefExpr *E) {
8257 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008258 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008259 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
8260 return false;
8261 if (DVar.CKind != OMPC_unknown)
8262 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008263 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
8264 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
8265 false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008266 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008267 return true;
8268 return false;
8269 }
8270 return false;
8271 }
8272 bool VisitStmt(Stmt *S) {
8273 for (auto Child : S->children()) {
8274 if (Child && Visit(Child))
8275 return true;
8276 }
8277 return false;
8278 }
Alexey Bataev23b69422014-06-18 07:08:49 +00008279 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00008280};
Alexey Bataev23b69422014-06-18 07:08:49 +00008281} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00008282
Alexey Bataev60da77e2016-02-29 05:54:20 +00008283namespace {
8284// Transform MemberExpression for specified FieldDecl of current class to
8285// DeclRefExpr to specified OMPCapturedExprDecl.
8286class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
8287 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
8288 ValueDecl *Field;
8289 DeclRefExpr *CapturedExpr;
8290
8291public:
8292 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
8293 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
8294
8295 ExprResult TransformMemberExpr(MemberExpr *E) {
8296 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
8297 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00008298 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008299 return CapturedExpr;
8300 }
8301 return BaseTransform::TransformMemberExpr(E);
8302 }
8303 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
8304};
8305} // namespace
8306
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008307template <typename T>
8308static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
8309 const llvm::function_ref<T(ValueDecl *)> &Gen) {
8310 for (auto &Set : Lookups) {
8311 for (auto *D : Set) {
8312 if (auto Res = Gen(cast<ValueDecl>(D)))
8313 return Res;
8314 }
8315 }
8316 return T();
8317}
8318
8319static ExprResult
8320buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
8321 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
8322 const DeclarationNameInfo &ReductionId, QualType Ty,
8323 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
8324 if (ReductionIdScopeSpec.isInvalid())
8325 return ExprError();
8326 SmallVector<UnresolvedSet<8>, 4> Lookups;
8327 if (S) {
8328 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
8329 Lookup.suppressDiagnostics();
8330 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
8331 auto *D = Lookup.getRepresentativeDecl();
8332 do {
8333 S = S->getParent();
8334 } while (S && !S->isDeclScope(D));
8335 if (S)
8336 S = S->getParent();
8337 Lookups.push_back(UnresolvedSet<8>());
8338 Lookups.back().append(Lookup.begin(), Lookup.end());
8339 Lookup.clear();
8340 }
8341 } else if (auto *ULE =
8342 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
8343 Lookups.push_back(UnresolvedSet<8>());
8344 Decl *PrevD = nullptr;
8345 for(auto *D : ULE->decls()) {
8346 if (D == PrevD)
8347 Lookups.push_back(UnresolvedSet<8>());
8348 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
8349 Lookups.back().addDecl(DRD);
8350 PrevD = D;
8351 }
8352 }
8353 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
8354 Ty->containsUnexpandedParameterPack() ||
8355 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
8356 return !D->isInvalidDecl() &&
8357 (D->getType()->isDependentType() ||
8358 D->getType()->isInstantiationDependentType() ||
8359 D->getType()->containsUnexpandedParameterPack());
8360 })) {
8361 UnresolvedSet<8> ResSet;
8362 for (auto &Set : Lookups) {
8363 ResSet.append(Set.begin(), Set.end());
8364 // The last item marks the end of all declarations at the specified scope.
8365 ResSet.addDecl(Set[Set.size() - 1]);
8366 }
8367 return UnresolvedLookupExpr::Create(
8368 SemaRef.Context, /*NamingClass=*/nullptr,
8369 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
8370 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
8371 }
8372 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8373 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
8374 if (!D->isInvalidDecl() &&
8375 SemaRef.Context.hasSameType(D->getType(), Ty))
8376 return D;
8377 return nullptr;
8378 }))
8379 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8380 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8381 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
8382 if (!D->isInvalidDecl() &&
8383 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
8384 !Ty.isMoreQualifiedThan(D->getType()))
8385 return D;
8386 return nullptr;
8387 })) {
8388 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
8389 /*DetectVirtual=*/false);
8390 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
8391 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
8392 VD->getType().getUnqualifiedType()))) {
8393 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
8394 /*DiagID=*/0) !=
8395 Sema::AR_inaccessible) {
8396 SemaRef.BuildBasePathArray(Paths, BasePath);
8397 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8398 }
8399 }
8400 }
8401 }
8402 if (ReductionIdScopeSpec.isSet()) {
8403 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
8404 return ExprError();
8405 }
8406 return ExprEmpty();
8407}
8408
Alexey Bataevc5e02582014-06-16 07:08:35 +00008409OMPClause *Sema::ActOnOpenMPReductionClause(
8410 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
8411 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008412 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
8413 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00008414 auto DN = ReductionId.getName();
8415 auto OOK = DN.getCXXOverloadedOperator();
8416 BinaryOperatorKind BOK = BO_Comma;
8417
8418 // OpenMP [2.14.3.6, reduction clause]
8419 // C
8420 // reduction-identifier is either an identifier or one of the following
8421 // operators: +, -, *, &, |, ^, && and ||
8422 // C++
8423 // reduction-identifier is either an id-expression or one of the following
8424 // operators: +, -, *, &, |, ^, && and ||
8425 // FIXME: Only 'min' and 'max' identifiers are supported for now.
8426 switch (OOK) {
8427 case OO_Plus:
8428 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008429 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008430 break;
8431 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008432 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008433 break;
8434 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008435 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008436 break;
8437 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008438 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008439 break;
8440 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008441 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008442 break;
8443 case OO_AmpAmp:
8444 BOK = BO_LAnd;
8445 break;
8446 case OO_PipePipe:
8447 BOK = BO_LOr;
8448 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008449 case OO_New:
8450 case OO_Delete:
8451 case OO_Array_New:
8452 case OO_Array_Delete:
8453 case OO_Slash:
8454 case OO_Percent:
8455 case OO_Tilde:
8456 case OO_Exclaim:
8457 case OO_Equal:
8458 case OO_Less:
8459 case OO_Greater:
8460 case OO_LessEqual:
8461 case OO_GreaterEqual:
8462 case OO_PlusEqual:
8463 case OO_MinusEqual:
8464 case OO_StarEqual:
8465 case OO_SlashEqual:
8466 case OO_PercentEqual:
8467 case OO_CaretEqual:
8468 case OO_AmpEqual:
8469 case OO_PipeEqual:
8470 case OO_LessLess:
8471 case OO_GreaterGreater:
8472 case OO_LessLessEqual:
8473 case OO_GreaterGreaterEqual:
8474 case OO_EqualEqual:
8475 case OO_ExclaimEqual:
8476 case OO_PlusPlus:
8477 case OO_MinusMinus:
8478 case OO_Comma:
8479 case OO_ArrowStar:
8480 case OO_Arrow:
8481 case OO_Call:
8482 case OO_Subscript:
8483 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00008484 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008485 case NUM_OVERLOADED_OPERATORS:
8486 llvm_unreachable("Unexpected reduction identifier");
8487 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008488 if (auto II = DN.getAsIdentifierInfo()) {
8489 if (II->isStr("max"))
8490 BOK = BO_GT;
8491 else if (II->isStr("min"))
8492 BOK = BO_LT;
8493 }
8494 break;
8495 }
8496 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008497 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00008498 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008499 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008500
8501 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008502 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008503 SmallVector<Expr *, 8> LHSs;
8504 SmallVector<Expr *, 8> RHSs;
8505 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00008506 SmallVector<Decl *, 4> ExprCaptures;
8507 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008508 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
8509 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008510 for (auto RefExpr : VarList) {
8511 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00008512 // OpenMP [2.1, C/C++]
8513 // A list item is a variable or array section, subject to the restrictions
8514 // specified in Section 2.4 on page 42 and in each of the sections
8515 // describing clauses and directives for which a list appears.
8516 // OpenMP [2.14.3.3, Restrictions, p.1]
8517 // A variable that is part of another variable (as an array or
8518 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008519 if (!FirstIter && IR != ER)
8520 ++IR;
8521 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008522 SourceLocation ELoc;
8523 SourceRange ERange;
8524 Expr *SimpleRefExpr = RefExpr;
8525 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8526 /*AllowArraySection=*/true);
8527 if (Res.second) {
8528 // It will be analyzed later.
8529 Vars.push_back(RefExpr);
8530 Privates.push_back(nullptr);
8531 LHSs.push_back(nullptr);
8532 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008533 // Try to find 'declare reduction' corresponding construct before using
8534 // builtin/overloaded operators.
8535 QualType Type = Context.DependentTy;
8536 CXXCastPath BasePath;
8537 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8538 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8539 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8540 if (CurContext->isDependentContext() &&
8541 (DeclareReductionRef.isUnset() ||
8542 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
8543 ReductionOps.push_back(DeclareReductionRef.get());
8544 else
8545 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008546 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008547 ValueDecl *D = Res.first;
8548 if (!D)
8549 continue;
8550
Alexey Bataeva1764212015-09-30 09:22:36 +00008551 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008552 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
8553 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
8554 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00008555 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008556 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008557 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
8558 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
8559 Type = ATy->getElementType();
8560 else
8561 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008562 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008563 } else
8564 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
8565 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00008566
Alexey Bataevc5e02582014-06-16 07:08:35 +00008567 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8568 // A variable that appears in a private clause must not have an incomplete
8569 // type or a reference type.
8570 if (RequireCompleteType(ELoc, Type,
8571 diag::err_omp_reduction_incomplete_type))
8572 continue;
8573 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00008574 // A list item that appears in a reduction clause must not be
8575 // const-qualified.
8576 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008577 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008578 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008579 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008580 bool IsDecl = !VD ||
8581 VD->isThisDeclarationADefinition(Context) ==
8582 VarDecl::DeclarationOnly;
8583 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00008584 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00008585 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00008586 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008587 continue;
8588 }
8589 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
8590 // If a list-item is a reference type then it must bind to the same object
8591 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008592 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008593 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008594 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008595 DSARefChecker Check(DSAStack);
8596 if (Check.Visit(VDDef->getInit())) {
8597 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
8598 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
8599 continue;
8600 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008601 }
8602 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008603
Alexey Bataevc5e02582014-06-16 07:08:35 +00008604 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8605 // in a Construct]
8606 // Variables with the predetermined data-sharing attributes may not be
8607 // listed in data-sharing attributes clauses, except for the cases
8608 // listed below. For these exceptions only, listing a predetermined
8609 // variable in a data-sharing attribute clause is allowed and overrides
8610 // the variable's predetermined data-sharing attributes.
8611 // OpenMP [2.14.3.6, Restrictions, p.3]
8612 // Any number of reduction clauses can be specified on the directive,
8613 // but a list item can appear only once in the reduction clauses for that
8614 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00008615 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008616 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008617 if (DVar.CKind == OMPC_reduction) {
8618 Diag(ELoc, diag::err_omp_once_referenced)
8619 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008620 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008621 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008622 } else if (DVar.CKind != OMPC_unknown) {
8623 Diag(ELoc, diag::err_omp_wrong_dsa)
8624 << getOpenMPClauseName(DVar.CKind)
8625 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008626 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008627 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008628 }
8629
8630 // OpenMP [2.14.3.6, Restrictions, p.1]
8631 // A list item that appears in a reduction clause of a worksharing
8632 // construct must be shared in the parallel regions to which any of the
8633 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008634 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8635 if (isOpenMPWorksharingDirective(CurrDir) &&
8636 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008637 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008638 if (DVar.CKind != OMPC_shared) {
8639 Diag(ELoc, diag::err_omp_required_access)
8640 << getOpenMPClauseName(OMPC_reduction)
8641 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008642 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008643 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00008644 }
8645 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008646
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008647 // Try to find 'declare reduction' corresponding construct before using
8648 // builtin/overloaded operators.
8649 CXXCastPath BasePath;
8650 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8651 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8652 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8653 if (DeclareReductionRef.isInvalid())
8654 continue;
8655 if (CurContext->isDependentContext() &&
8656 (DeclareReductionRef.isUnset() ||
8657 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
8658 Vars.push_back(RefExpr);
8659 Privates.push_back(nullptr);
8660 LHSs.push_back(nullptr);
8661 RHSs.push_back(nullptr);
8662 ReductionOps.push_back(DeclareReductionRef.get());
8663 continue;
8664 }
8665 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
8666 // Not allowed reduction identifier is found.
8667 Diag(ReductionId.getLocStart(),
8668 diag::err_omp_unknown_reduction_identifier)
8669 << Type << ReductionIdRange;
8670 continue;
8671 }
8672
8673 // OpenMP [2.14.3.6, reduction clause, Restrictions]
8674 // The type of a list item that appears in a reduction clause must be valid
8675 // for the reduction-identifier. For a max or min reduction in C, the type
8676 // of the list item must be an allowed arithmetic data type: char, int,
8677 // float, double, or _Bool, possibly modified with long, short, signed, or
8678 // unsigned. For a max or min reduction in C++, the type of the list item
8679 // must be an allowed arithmetic data type: char, wchar_t, int, float,
8680 // double, or bool, possibly modified with long, short, signed, or unsigned.
8681 if (DeclareReductionRef.isUnset()) {
8682 if ((BOK == BO_GT || BOK == BO_LT) &&
8683 !(Type->isScalarType() ||
8684 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
8685 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
8686 << getLangOpts().CPlusPlus;
8687 if (!ASE && !OASE) {
8688 bool IsDecl = !VD ||
8689 VD->isThisDeclarationADefinition(Context) ==
8690 VarDecl::DeclarationOnly;
8691 Diag(D->getLocation(),
8692 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8693 << D;
8694 }
8695 continue;
8696 }
8697 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
8698 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
8699 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
8700 if (!ASE && !OASE) {
8701 bool IsDecl = !VD ||
8702 VD->isThisDeclarationADefinition(Context) ==
8703 VarDecl::DeclarationOnly;
8704 Diag(D->getLocation(),
8705 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8706 << D;
8707 }
8708 continue;
8709 }
8710 }
8711
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008712 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008713 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00008714 D->hasAttrs() ? &D->getAttrs() : nullptr);
8715 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
8716 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008717 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00008718 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00008719 (!ASE &&
8720 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00008721 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008722 // Create pseudo array type for private copy. The size for this array will
8723 // be generated during codegen.
8724 // For array subscripts or single variables Private Ty is the same as Type
8725 // (type of the variable or single array element).
8726 PrivateTy = Context.getVariableArrayType(
8727 Type, new (Context) OpaqueValueExpr(SourceLocation(),
8728 Context.getSizeType(), VK_RValue),
8729 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00008730 } else if (!ASE && !OASE &&
8731 Context.getAsArrayType(D->getType().getNonReferenceType()))
8732 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008733 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008734 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
8735 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008736 // Add initializer for private variable.
8737 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008738 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
8739 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
8740 if (DeclareReductionRef.isUsable()) {
8741 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
8742 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
8743 if (DRD->getInitializer()) {
8744 Init = DRDRef;
8745 RHSVD->setInit(DRDRef);
8746 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008747 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008748 } else {
8749 switch (BOK) {
8750 case BO_Add:
8751 case BO_Xor:
8752 case BO_Or:
8753 case BO_LOr:
8754 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
8755 if (Type->isScalarType() || Type->isAnyComplexType())
8756 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
8757 break;
8758 case BO_Mul:
8759 case BO_LAnd:
8760 if (Type->isScalarType() || Type->isAnyComplexType()) {
8761 // '*' and '&&' reduction ops - initializer is '1'.
8762 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00008763 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008764 break;
8765 case BO_And: {
8766 // '&' reduction op - initializer is '~0'.
8767 QualType OrigType = Type;
8768 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
8769 Type = ComplexTy->getElementType();
8770 if (Type->isRealFloatingType()) {
8771 llvm::APFloat InitValue =
8772 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
8773 /*isIEEE=*/true);
8774 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8775 Type, ELoc);
8776 } else if (Type->isScalarType()) {
8777 auto Size = Context.getTypeSize(Type);
8778 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
8779 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
8780 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8781 }
8782 if (Init && OrigType->isAnyComplexType()) {
8783 // Init = 0xFFFF + 0xFFFFi;
8784 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
8785 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
8786 }
8787 Type = OrigType;
8788 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008789 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008790 case BO_LT:
8791 case BO_GT: {
8792 // 'min' reduction op - initializer is 'Largest representable number in
8793 // the reduction list item type'.
8794 // 'max' reduction op - initializer is 'Least representable number in
8795 // the reduction list item type'.
8796 if (Type->isIntegerType() || Type->isPointerType()) {
8797 bool IsSigned = Type->hasSignedIntegerRepresentation();
8798 auto Size = Context.getTypeSize(Type);
8799 QualType IntTy =
8800 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
8801 llvm::APInt InitValue =
8802 (BOK != BO_LT)
8803 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
8804 : llvm::APInt::getMinValue(Size)
8805 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
8806 : llvm::APInt::getMaxValue(Size);
8807 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8808 if (Type->isPointerType()) {
8809 // Cast to pointer type.
8810 auto CastExpr = BuildCStyleCastExpr(
8811 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
8812 SourceLocation(), Init);
8813 if (CastExpr.isInvalid())
8814 continue;
8815 Init = CastExpr.get();
8816 }
8817 } else if (Type->isRealFloatingType()) {
8818 llvm::APFloat InitValue = llvm::APFloat::getLargest(
8819 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
8820 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8821 Type, ELoc);
8822 }
8823 break;
8824 }
8825 case BO_PtrMemD:
8826 case BO_PtrMemI:
8827 case BO_MulAssign:
8828 case BO_Div:
8829 case BO_Rem:
8830 case BO_Sub:
8831 case BO_Shl:
8832 case BO_Shr:
8833 case BO_LE:
8834 case BO_GE:
8835 case BO_EQ:
8836 case BO_NE:
8837 case BO_AndAssign:
8838 case BO_XorAssign:
8839 case BO_OrAssign:
8840 case BO_Assign:
8841 case BO_AddAssign:
8842 case BO_SubAssign:
8843 case BO_DivAssign:
8844 case BO_RemAssign:
8845 case BO_ShlAssign:
8846 case BO_ShrAssign:
8847 case BO_Comma:
8848 llvm_unreachable("Unexpected reduction operation");
8849 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008850 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008851 if (Init && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008852 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
8853 /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008854 } else if (!Init)
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008855 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008856 if (RHSVD->isInvalidDecl())
8857 continue;
8858 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008859 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
8860 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008861 bool IsDecl =
8862 !VD ||
8863 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8864 Diag(D->getLocation(),
8865 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8866 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008867 continue;
8868 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008869 // Store initializer for single element in private copy. Will be used during
8870 // codegen.
8871 PrivateVD->setInit(RHSVD->getInit());
8872 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008873 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008874 ExprResult ReductionOp;
8875 if (DeclareReductionRef.isUsable()) {
8876 QualType RedTy = DeclareReductionRef.get()->getType();
8877 QualType PtrRedTy = Context.getPointerType(RedTy);
8878 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
8879 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
8880 if (!BasePath.empty()) {
8881 LHS = DefaultLvalueConversion(LHS.get());
8882 RHS = DefaultLvalueConversion(RHS.get());
8883 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8884 CK_UncheckedDerivedToBase, LHS.get(),
8885 &BasePath, LHS.get()->getValueKind());
8886 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8887 CK_UncheckedDerivedToBase, RHS.get(),
8888 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008889 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008890 FunctionProtoType::ExtProtoInfo EPI;
8891 QualType Params[] = {PtrRedTy, PtrRedTy};
8892 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
8893 auto *OVE = new (Context) OpaqueValueExpr(
8894 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
8895 DefaultLvalueConversion(DeclareReductionRef.get()).get());
8896 Expr *Args[] = {LHS.get(), RHS.get()};
8897 ReductionOp = new (Context)
8898 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
8899 } else {
8900 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
8901 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
8902 if (ReductionOp.isUsable()) {
8903 if (BOK != BO_LT && BOK != BO_GT) {
8904 ReductionOp =
8905 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8906 BO_Assign, LHSDRE, ReductionOp.get());
8907 } else {
8908 auto *ConditionalOp = new (Context) ConditionalOperator(
8909 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
8910 RHSDRE, Type, VK_LValue, OK_Ordinary);
8911 ReductionOp =
8912 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8913 BO_Assign, LHSDRE, ConditionalOp);
8914 }
8915 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
8916 }
8917 if (ReductionOp.isInvalid())
8918 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008919 }
8920
Alexey Bataev60da77e2016-02-29 05:54:20 +00008921 DeclRefExpr *Ref = nullptr;
8922 Expr *VarsExpr = RefExpr->IgnoreParens();
8923 if (!VD) {
8924 if (ASE || OASE) {
8925 TransformExprToCaptures RebuildToCapture(*this, D);
8926 VarsExpr =
8927 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
8928 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00008929 } else {
8930 VarsExpr = Ref =
8931 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +00008932 }
8933 if (!IsOpenMPCapturedDecl(D)) {
8934 ExprCaptures.push_back(Ref->getDecl());
8935 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8936 ExprResult RefRes = DefaultLvalueConversion(Ref);
8937 if (!RefRes.isUsable())
8938 continue;
8939 ExprResult PostUpdateRes =
8940 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8941 SimpleRefExpr, RefRes.get());
8942 if (!PostUpdateRes.isUsable())
8943 continue;
8944 ExprPostUpdates.push_back(
8945 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00008946 }
8947 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008948 }
8949 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
8950 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008951 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008952 LHSs.push_back(LHSDRE);
8953 RHSs.push_back(RHSDRE);
8954 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008955 }
8956
8957 if (Vars.empty())
8958 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00008959
Alexey Bataevc5e02582014-06-16 07:08:35 +00008960 return OMPReductionClause::Create(
8961 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008962 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008963 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
8964 buildPostUpdate(*this, ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +00008965}
8966
Alexey Bataevecba70f2016-04-12 11:02:11 +00008967bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
8968 SourceLocation LinLoc) {
8969 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
8970 LinKind == OMPC_LINEAR_unknown) {
8971 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
8972 return true;
8973 }
8974 return false;
8975}
8976
8977bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
8978 OpenMPLinearClauseKind LinKind,
8979 QualType Type) {
8980 auto *VD = dyn_cast_or_null<VarDecl>(D);
8981 // A variable must not have an incomplete type or a reference type.
8982 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
8983 return true;
8984 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
8985 !Type->isReferenceType()) {
8986 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
8987 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
8988 return true;
8989 }
8990 Type = Type.getNonReferenceType();
8991
8992 // A list item must not be const-qualified.
8993 if (Type.isConstant(Context)) {
8994 Diag(ELoc, diag::err_omp_const_variable)
8995 << getOpenMPClauseName(OMPC_linear);
8996 if (D) {
8997 bool IsDecl =
8998 !VD ||
8999 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9000 Diag(D->getLocation(),
9001 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9002 << D;
9003 }
9004 return true;
9005 }
9006
9007 // A list item must be of integral or pointer type.
9008 Type = Type.getUnqualifiedType().getCanonicalType();
9009 const auto *Ty = Type.getTypePtrOrNull();
9010 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
9011 !Ty->isPointerType())) {
9012 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
9013 if (D) {
9014 bool IsDecl =
9015 !VD ||
9016 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9017 Diag(D->getLocation(),
9018 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9019 << D;
9020 }
9021 return true;
9022 }
9023 return false;
9024}
9025
Alexey Bataev182227b2015-08-20 10:54:39 +00009026OMPClause *Sema::ActOnOpenMPLinearClause(
9027 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
9028 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
9029 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009030 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009031 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00009032 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00009033 SmallVector<Decl *, 4> ExprCaptures;
9034 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009035 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +00009036 LinKind = OMPC_LINEAR_val;
Alexey Bataeved09d242014-05-28 05:53:51 +00009037 for (auto &RefExpr : VarList) {
9038 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009039 SourceLocation ELoc;
9040 SourceRange ERange;
9041 Expr *SimpleRefExpr = RefExpr;
9042 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9043 /*AllowArraySection=*/false);
9044 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009045 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009046 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009047 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00009048 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00009049 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009050 ValueDecl *D = Res.first;
9051 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00009052 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00009053
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009054 QualType Type = D->getType();
9055 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00009056
9057 // OpenMP [2.14.3.7, linear clause]
9058 // A list-item cannot appear in more than one linear clause.
9059 // A list-item that appears in a linear clause cannot appear in any
9060 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009061 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00009062 if (DVar.RefExpr) {
9063 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9064 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009065 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00009066 continue;
9067 }
9068
Alexey Bataevecba70f2016-04-12 11:02:11 +00009069 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +00009070 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009071 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +00009072
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009073 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009074 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
9075 D->hasAttrs() ? &D->getAttrs() : nullptr);
9076 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009077 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009078 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009079 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009080 DeclRefExpr *Ref = nullptr;
Alexey Bataev78849fb2016-03-09 09:49:00 +00009081 if (!VD) {
9082 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
9083 if (!IsOpenMPCapturedDecl(D)) {
9084 ExprCaptures.push_back(Ref->getDecl());
9085 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9086 ExprResult RefRes = DefaultLvalueConversion(Ref);
9087 if (!RefRes.isUsable())
9088 continue;
9089 ExprResult PostUpdateRes =
9090 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9091 SimpleRefExpr, RefRes.get());
9092 if (!PostUpdateRes.isUsable())
9093 continue;
9094 ExprPostUpdates.push_back(
9095 IgnoredValueConversions(PostUpdateRes.get()).get());
9096 }
9097 }
9098 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009099 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009100 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009101 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009102 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009103 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009104 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
9105 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
9106
9107 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
9108 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009109 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00009110 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00009111 }
9112
9113 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009114 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009115
9116 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00009117 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009118 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
9119 !Step->isInstantiationDependent() &&
9120 !Step->containsUnexpandedParameterPack()) {
9121 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00009122 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00009123 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009124 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00009125 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00009126
Alexander Musman3276a272015-03-21 10:12:56 +00009127 // Build var to save the step value.
9128 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009129 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00009130 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009131 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009132 ExprResult CalcStep =
9133 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009134 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00009135
Alexander Musman8dba6642014-04-22 13:09:42 +00009136 // Warn about zero linear step (it would be probably better specified as
9137 // making corresponding variables 'const').
9138 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00009139 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
9140 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00009141 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
9142 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00009143 if (!IsConstant && CalcStep.isUsable()) {
9144 // Calculate the step beforehand instead of doing this on each iteration.
9145 // (This is not used if the number of iterations may be kfold-ed).
9146 CalcStepExpr = CalcStep.get();
9147 }
Alexander Musman8dba6642014-04-22 13:09:42 +00009148 }
9149
Alexey Bataev182227b2015-08-20 10:54:39 +00009150 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
9151 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009152 StepExpr, CalcStepExpr,
9153 buildPreInits(Context, ExprCaptures),
9154 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +00009155}
9156
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009157static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
9158 Expr *NumIterations, Sema &SemaRef,
9159 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +00009160 // Walk the vars and build update/final expressions for the CodeGen.
9161 SmallVector<Expr *, 8> Updates;
9162 SmallVector<Expr *, 8> Finals;
9163 Expr *Step = Clause.getStep();
9164 Expr *CalcStep = Clause.getCalcStep();
9165 // OpenMP [2.14.3.7, linear clause]
9166 // If linear-step is not specified it is assumed to be 1.
9167 if (Step == nullptr)
9168 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009169 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +00009170 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009171 }
Alexander Musman3276a272015-03-21 10:12:56 +00009172 bool HasErrors = false;
9173 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009174 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009175 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00009176 for (auto &RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009177 SourceLocation ELoc;
9178 SourceRange ERange;
9179 Expr *SimpleRefExpr = RefExpr;
9180 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
9181 /*AllowArraySection=*/false);
9182 ValueDecl *D = Res.first;
9183 if (Res.second || !D) {
9184 Updates.push_back(nullptr);
9185 Finals.push_back(nullptr);
9186 HasErrors = true;
9187 continue;
9188 }
9189 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
9190 D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
9191 ->getMemberDecl();
9192 }
9193 auto &&Info = Stack->isLoopControlVariable(D);
Alexander Musman3276a272015-03-21 10:12:56 +00009194 Expr *InitExpr = *CurInit;
9195
9196 // Build privatized reference to the current linear var.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009197 auto DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009198 Expr *CapturedRef;
9199 if (LinKind == OMPC_LINEAR_uval)
9200 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
9201 else
9202 CapturedRef =
9203 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
9204 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
9205 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009206
9207 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009208 ExprResult Update;
9209 if (!Info.first) {
9210 Update =
9211 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
9212 InitExpr, IV, Step, /* Subtract */ false);
9213 } else
9214 Update = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009215 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
9216 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009217
9218 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009219 ExprResult Final;
9220 if (!Info.first) {
9221 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
9222 InitExpr, NumIterations, Step,
9223 /* Subtract */ false);
9224 } else
9225 Final = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009226 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
9227 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009228
Alexander Musman3276a272015-03-21 10:12:56 +00009229 if (!Update.isUsable() || !Final.isUsable()) {
9230 Updates.push_back(nullptr);
9231 Finals.push_back(nullptr);
9232 HasErrors = true;
9233 } else {
9234 Updates.push_back(Update.get());
9235 Finals.push_back(Final.get());
9236 }
Richard Trieucc3949d2016-02-18 22:34:54 +00009237 ++CurInit;
9238 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00009239 }
9240 Clause.setUpdates(Updates);
9241 Clause.setFinals(Finals);
9242 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00009243}
9244
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009245OMPClause *Sema::ActOnOpenMPAlignedClause(
9246 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
9247 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9248
9249 SmallVector<Expr *, 8> Vars;
9250 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +00009251 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9252 SourceLocation ELoc;
9253 SourceRange ERange;
9254 Expr *SimpleRefExpr = RefExpr;
9255 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9256 /*AllowArraySection=*/false);
9257 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009258 // It will be analyzed later.
9259 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009260 }
Alexey Bataev1efd1662016-03-29 10:59:56 +00009261 ValueDecl *D = Res.first;
9262 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009263 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009264
Alexey Bataev1efd1662016-03-29 10:59:56 +00009265 QualType QType = D->getType();
9266 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009267
9268 // OpenMP [2.8.1, simd construct, Restrictions]
9269 // The type of list items appearing in the aligned clause must be
9270 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009271 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009272 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +00009273 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009274 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009275 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009276 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +00009277 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009278 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +00009279 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009280 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009281 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009282 continue;
9283 }
9284
9285 // OpenMP [2.8.1, simd construct, Restrictions]
9286 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +00009287 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00009288 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009289 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
9290 << getOpenMPClauseName(OMPC_aligned);
9291 continue;
9292 }
9293
Alexey Bataev1efd1662016-03-29 10:59:56 +00009294 DeclRefExpr *Ref = nullptr;
9295 if (!VD && IsOpenMPCapturedDecl(D))
9296 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9297 Vars.push_back(DefaultFunctionArrayConversion(
9298 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
9299 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009300 }
9301
9302 // OpenMP [2.8.1, simd construct, Description]
9303 // The parameter of the aligned clause, alignment, must be a constant
9304 // positive integer expression.
9305 // If no optional parameter is specified, implementation-defined default
9306 // alignments for SIMD instructions on the target platforms are assumed.
9307 if (Alignment != nullptr) {
9308 ExprResult AlignResult =
9309 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
9310 if (AlignResult.isInvalid())
9311 return nullptr;
9312 Alignment = AlignResult.get();
9313 }
9314 if (Vars.empty())
9315 return nullptr;
9316
9317 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
9318 EndLoc, Vars, Alignment);
9319}
9320
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009321OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
9322 SourceLocation StartLoc,
9323 SourceLocation LParenLoc,
9324 SourceLocation EndLoc) {
9325 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009326 SmallVector<Expr *, 8> SrcExprs;
9327 SmallVector<Expr *, 8> DstExprs;
9328 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00009329 for (auto &RefExpr : VarList) {
9330 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
9331 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009332 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009333 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009334 SrcExprs.push_back(nullptr);
9335 DstExprs.push_back(nullptr);
9336 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009337 continue;
9338 }
9339
Alexey Bataeved09d242014-05-28 05:53:51 +00009340 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009341 // OpenMP [2.1, C/C++]
9342 // A list item is a variable name.
9343 // OpenMP [2.14.4.1, Restrictions, p.1]
9344 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00009345 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009346 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009347 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
9348 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009349 continue;
9350 }
9351
9352 Decl *D = DE->getDecl();
9353 VarDecl *VD = cast<VarDecl>(D);
9354
9355 QualType Type = VD->getType();
9356 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
9357 // It will be analyzed later.
9358 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009359 SrcExprs.push_back(nullptr);
9360 DstExprs.push_back(nullptr);
9361 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009362 continue;
9363 }
9364
9365 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
9366 // A list item that appears in a copyin clause must be threadprivate.
9367 if (!DSAStack->isThreadPrivate(VD)) {
9368 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00009369 << getOpenMPClauseName(OMPC_copyin)
9370 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009371 continue;
9372 }
9373
9374 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9375 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00009376 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009377 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009378 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009379 auto *SrcVD =
9380 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
9381 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00009382 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009383 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
9384 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009385 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
9386 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009387 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009388 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009389 // For arrays generate assignment operation for single element and replace
9390 // it by the original array element in CodeGen.
9391 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
9392 PseudoDstExpr, PseudoSrcExpr);
9393 if (AssignmentOp.isInvalid())
9394 continue;
9395 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
9396 /*DiscardedValue=*/true);
9397 if (AssignmentOp.isInvalid())
9398 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009399
9400 DSAStack->addDSA(VD, DE, OMPC_copyin);
9401 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009402 SrcExprs.push_back(PseudoSrcExpr);
9403 DstExprs.push_back(PseudoDstExpr);
9404 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009405 }
9406
Alexey Bataeved09d242014-05-28 05:53:51 +00009407 if (Vars.empty())
9408 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009409
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009410 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9411 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009412}
9413
Alexey Bataevbae9a792014-06-27 10:37:06 +00009414OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
9415 SourceLocation StartLoc,
9416 SourceLocation LParenLoc,
9417 SourceLocation EndLoc) {
9418 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00009419 SmallVector<Expr *, 8> SrcExprs;
9420 SmallVector<Expr *, 8> DstExprs;
9421 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009422 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009423 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9424 SourceLocation ELoc;
9425 SourceRange ERange;
9426 Expr *SimpleRefExpr = RefExpr;
9427 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9428 /*AllowArraySection=*/false);
9429 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009430 // It will be analyzed later.
9431 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009432 SrcExprs.push_back(nullptr);
9433 DstExprs.push_back(nullptr);
9434 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009435 }
Alexey Bataeve122da12016-03-17 10:50:17 +00009436 ValueDecl *D = Res.first;
9437 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +00009438 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009439
Alexey Bataeve122da12016-03-17 10:50:17 +00009440 QualType Type = D->getType();
9441 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009442
9443 // OpenMP [2.14.4.2, Restrictions, p.2]
9444 // A list item that appears in a copyprivate clause may not appear in a
9445 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +00009446 if (!VD || !DSAStack->isThreadPrivate(VD)) {
9447 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009448 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
9449 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009450 Diag(ELoc, diag::err_omp_wrong_dsa)
9451 << getOpenMPClauseName(DVar.CKind)
9452 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +00009453 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009454 continue;
9455 }
9456
9457 // OpenMP [2.11.4.2, Restrictions, p.1]
9458 // All list items that appear in a copyprivate clause must be either
9459 // threadprivate or private in the enclosing context.
9460 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009461 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009462 if (DVar.CKind == OMPC_shared) {
9463 Diag(ELoc, diag::err_omp_required_access)
9464 << getOpenMPClauseName(OMPC_copyprivate)
9465 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +00009466 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009467 continue;
9468 }
9469 }
9470 }
9471
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009472 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009473 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009474 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009475 << getOpenMPClauseName(OMPC_copyprivate) << Type
9476 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009477 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +00009478 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009479 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +00009480 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009481 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +00009482 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009483 continue;
9484 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009485
Alexey Bataevbae9a792014-06-27 10:37:06 +00009486 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9487 // A variable of class type (or array thereof) that appears in a
9488 // copyin clause requires an accessible, unambiguous copy assignment
9489 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009490 Type = Context.getBaseElementType(Type.getNonReferenceType())
9491 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00009492 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009493 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
9494 D->hasAttrs() ? &D->getAttrs() : nullptr);
9495 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009496 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009497 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
9498 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009499 auto *PseudoDstExpr =
Alexey Bataeve122da12016-03-17 10:50:17 +00009500 buildDeclRefExpr(*this, DstVD, Type, ELoc);
9501 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009502 PseudoDstExpr, PseudoSrcExpr);
9503 if (AssignmentOp.isInvalid())
9504 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +00009505 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009506 /*DiscardedValue=*/true);
9507 if (AssignmentOp.isInvalid())
9508 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009509
9510 // No need to mark vars as copyprivate, they are already threadprivate or
9511 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +00009512 assert(VD || IsOpenMPCapturedDecl(D));
9513 Vars.push_back(
9514 VD ? RefExpr->IgnoreParens()
9515 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +00009516 SrcExprs.push_back(PseudoSrcExpr);
9517 DstExprs.push_back(PseudoDstExpr);
9518 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00009519 }
9520
9521 if (Vars.empty())
9522 return nullptr;
9523
Alexey Bataeva63048e2015-03-23 06:18:07 +00009524 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
9525 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009526}
9527
Alexey Bataev6125da92014-07-21 11:26:11 +00009528OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
9529 SourceLocation StartLoc,
9530 SourceLocation LParenLoc,
9531 SourceLocation EndLoc) {
9532 if (VarList.empty())
9533 return nullptr;
9534
9535 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
9536}
Alexey Bataevdea47612014-07-23 07:46:59 +00009537
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009538OMPClause *
9539Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
9540 SourceLocation DepLoc, SourceLocation ColonLoc,
9541 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9542 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009543 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009544 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009545 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009546 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00009547 return nullptr;
9548 }
9549 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009550 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
9551 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00009552 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009553 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009554 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
9555 /*Last=*/OMPC_DEPEND_unknown, Except)
9556 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009557 return nullptr;
9558 }
9559 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +00009560 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009561 llvm::APSInt DepCounter(/*BitWidth=*/32);
9562 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
9563 if (DepKind == OMPC_DEPEND_sink) {
9564 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
9565 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
9566 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009567 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009568 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009569 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
9570 DSAStack->getParentOrderedRegionParam()) {
9571 for (auto &RefExpr : VarList) {
9572 assert(RefExpr && "NULL expr in OpenMP shared clause.");
Alexey Bataev8b427062016-05-25 12:36:08 +00009573 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009574 // It will be analyzed later.
9575 Vars.push_back(RefExpr);
9576 continue;
9577 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009578
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009579 SourceLocation ELoc = RefExpr->getExprLoc();
9580 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
9581 if (DepKind == OMPC_DEPEND_sink) {
9582 if (DepCounter >= TotalDepCount) {
9583 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
9584 continue;
9585 }
9586 ++DepCounter;
9587 // OpenMP [2.13.9, Summary]
9588 // depend(dependence-type : vec), where dependence-type is:
9589 // 'sink' and where vec is the iteration vector, which has the form:
9590 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
9591 // where n is the value specified by the ordered clause in the loop
9592 // directive, xi denotes the loop iteration variable of the i-th nested
9593 // loop associated with the loop directive, and di is a constant
9594 // non-negative integer.
Alexey Bataev8b427062016-05-25 12:36:08 +00009595 if (CurContext->isDependentContext()) {
9596 // It will be analyzed later.
9597 Vars.push_back(RefExpr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009598 continue;
9599 }
Alexey Bataev8b427062016-05-25 12:36:08 +00009600 SimpleExpr = SimpleExpr->IgnoreImplicit();
9601 OverloadedOperatorKind OOK = OO_None;
9602 SourceLocation OOLoc;
9603 Expr *LHS = SimpleExpr;
9604 Expr *RHS = nullptr;
9605 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
9606 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
9607 OOLoc = BO->getOperatorLoc();
9608 LHS = BO->getLHS()->IgnoreParenImpCasts();
9609 RHS = BO->getRHS()->IgnoreParenImpCasts();
9610 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
9611 OOK = OCE->getOperator();
9612 OOLoc = OCE->getOperatorLoc();
9613 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9614 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
9615 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
9616 OOK = MCE->getMethodDecl()
9617 ->getNameInfo()
9618 .getName()
9619 .getCXXOverloadedOperator();
9620 OOLoc = MCE->getCallee()->getExprLoc();
9621 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
9622 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9623 }
9624 SourceLocation ELoc;
9625 SourceRange ERange;
9626 auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
9627 /*AllowArraySection=*/false);
9628 if (Res.second) {
9629 // It will be analyzed later.
9630 Vars.push_back(RefExpr);
9631 }
9632 ValueDecl *D = Res.first;
9633 if (!D)
9634 continue;
9635
9636 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
9637 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
9638 continue;
9639 }
9640 if (RHS) {
9641 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
9642 RHS, OMPC_depend, /*StrictlyPositive=*/false);
9643 if (RHSRes.isInvalid())
9644 continue;
9645 }
9646 if (!CurContext->isDependentContext() &&
9647 DSAStack->getParentOrderedRegionParam() &&
9648 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
9649 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
9650 << DSAStack->getParentLoopControlVariable(
9651 DepCounter.getZExtValue());
9652 continue;
9653 }
9654 OpsOffs.push_back({RHS, OOK});
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009655 } else {
9656 // OpenMP [2.11.1.1, Restrictions, p.3]
9657 // A variable that is part of another variable (such as a field of a
9658 // structure) but is not an array element or an array section cannot
9659 // appear in a depend clause.
9660 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9661 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
9662 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
9663 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
9664 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +00009665 (ASE &&
9666 !ASE->getBase()
9667 ->getType()
9668 .getNonReferenceType()
9669 ->isPointerType() &&
9670 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009671 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
9672 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009673 continue;
9674 }
9675 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009676 Vars.push_back(RefExpr->IgnoreParenImpCasts());
9677 }
9678
9679 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
9680 TotalDepCount > VarList.size() &&
9681 DSAStack->getParentOrderedRegionParam()) {
9682 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
9683 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
9684 }
9685 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
9686 Vars.empty())
9687 return nullptr;
9688 }
Alexey Bataev8b427062016-05-25 12:36:08 +00009689 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
9690 DepKind, DepLoc, ColonLoc, Vars);
9691 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source)
9692 DSAStack->addDoacrossDependClause(C, OpsOffs);
9693 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009694}
Michael Wonge710d542015-08-07 16:16:36 +00009695
9696OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
9697 SourceLocation LParenLoc,
9698 SourceLocation EndLoc) {
9699 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00009700
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009701 // OpenMP [2.9.1, Restrictions]
9702 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009703 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
9704 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009705 return nullptr;
9706
Michael Wonge710d542015-08-07 16:16:36 +00009707 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9708}
Kelvin Li0bff7af2015-11-23 05:32:03 +00009709
9710static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
9711 DSAStackTy *Stack, CXXRecordDecl *RD) {
9712 if (!RD || RD->isInvalidDecl())
9713 return true;
9714
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00009715 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
9716 if (auto *CTD = CTSD->getSpecializedTemplate())
9717 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009718 auto QTy = SemaRef.Context.getRecordType(RD);
9719 if (RD->isDynamicClass()) {
9720 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9721 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
9722 return false;
9723 }
9724 auto *DC = RD;
9725 bool IsCorrect = true;
9726 for (auto *I : DC->decls()) {
9727 if (I) {
9728 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
9729 if (MD->isStatic()) {
9730 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9731 SemaRef.Diag(MD->getLocation(),
9732 diag::note_omp_static_member_in_target);
9733 IsCorrect = false;
9734 }
9735 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
9736 if (VD->isStaticDataMember()) {
9737 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9738 SemaRef.Diag(VD->getLocation(),
9739 diag::note_omp_static_member_in_target);
9740 IsCorrect = false;
9741 }
9742 }
9743 }
9744 }
9745
9746 for (auto &I : RD->bases()) {
9747 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
9748 I.getType()->getAsCXXRecordDecl()))
9749 IsCorrect = false;
9750 }
9751 return IsCorrect;
9752}
9753
9754static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
9755 DSAStackTy *Stack, QualType QTy) {
9756 NamedDecl *ND;
9757 if (QTy->isIncompleteType(&ND)) {
9758 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
9759 return false;
9760 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
9761 if (!RD->isInvalidDecl() &&
9762 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
9763 return false;
9764 }
9765 return true;
9766}
9767
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009768/// \brief Return true if it can be proven that the provided array expression
9769/// (array section or array subscript) does NOT specify the whole size of the
9770/// array whose base type is \a BaseQTy.
9771static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
9772 const Expr *E,
9773 QualType BaseQTy) {
9774 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9775
9776 // If this is an array subscript, it refers to the whole size if the size of
9777 // the dimension is constant and equals 1. Also, an array section assumes the
9778 // format of an array subscript if no colon is used.
9779 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
9780 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9781 return ATy->getSize().getSExtValue() != 1;
9782 // Size can't be evaluated statically.
9783 return false;
9784 }
9785
9786 assert(OASE && "Expecting array section if not an array subscript.");
9787 auto *LowerBound = OASE->getLowerBound();
9788 auto *Length = OASE->getLength();
9789
9790 // If there is a lower bound that does not evaluates to zero, we are not
9791 // convering the whole dimension.
9792 if (LowerBound) {
9793 llvm::APSInt ConstLowerBound;
9794 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
9795 return false; // Can't get the integer value as a constant.
9796 if (ConstLowerBound.getSExtValue())
9797 return true;
9798 }
9799
9800 // If we don't have a length we covering the whole dimension.
9801 if (!Length)
9802 return false;
9803
9804 // If the base is a pointer, we don't have a way to get the size of the
9805 // pointee.
9806 if (BaseQTy->isPointerType())
9807 return false;
9808
9809 // We can only check if the length is the same as the size of the dimension
9810 // if we have a constant array.
9811 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
9812 if (!CATy)
9813 return false;
9814
9815 llvm::APSInt ConstLength;
9816 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9817 return false; // Can't get the integer value as a constant.
9818
9819 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
9820}
9821
9822// Return true if it can be proven that the provided array expression (array
9823// section or array subscript) does NOT specify a single element of the array
9824// whose base type is \a BaseQTy.
9825static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
9826 const Expr *E,
9827 QualType BaseQTy) {
9828 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9829
9830 // An array subscript always refer to a single element. Also, an array section
9831 // assumes the format of an array subscript if no colon is used.
9832 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
9833 return false;
9834
9835 assert(OASE && "Expecting array section if not an array subscript.");
9836 auto *Length = OASE->getLength();
9837
9838 // If we don't have a length we have to check if the array has unitary size
9839 // for this dimension. Also, we should always expect a length if the base type
9840 // is pointer.
9841 if (!Length) {
9842 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9843 return ATy->getSize().getSExtValue() != 1;
9844 // We cannot assume anything.
9845 return false;
9846 }
9847
9848 // Check if the length evaluates to 1.
9849 llvm::APSInt ConstLength;
9850 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9851 return false; // Can't get the integer value as a constant.
9852
9853 return ConstLength.getSExtValue() != 1;
9854}
9855
Samuel Antao5de996e2016-01-22 20:21:36 +00009856// Return the expression of the base of the map clause or null if it cannot
9857// be determined and do all the necessary checks to see if the expression is
Samuel Antao90927002016-04-26 14:54:23 +00009858// valid as a standalone map clause expression. In the process, record all the
9859// components of the expression.
9860static Expr *CheckMapClauseExpressionBase(
9861 Sema &SemaRef, Expr *E,
9862 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009863 SourceLocation ELoc = E->getExprLoc();
9864 SourceRange ERange = E->getSourceRange();
9865
9866 // The base of elements of list in a map clause have to be either:
9867 // - a reference to variable or field.
9868 // - a member expression.
9869 // - an array expression.
9870 //
9871 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
9872 // reference to 'r'.
9873 //
9874 // If we have:
9875 //
9876 // struct SS {
9877 // Bla S;
9878 // foo() {
9879 // #pragma omp target map (S.Arr[:12]);
9880 // }
9881 // }
9882 //
9883 // We want to retrieve the member expression 'this->S';
9884
9885 Expr *RelevantExpr = nullptr;
9886
Samuel Antao5de996e2016-01-22 20:21:36 +00009887 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
9888 // If a list item is an array section, it must specify contiguous storage.
9889 //
9890 // For this restriction it is sufficient that we make sure only references
9891 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009892 // exist except in the rightmost expression (unless they cover the whole
9893 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +00009894 //
9895 // r.ArrS[3:5].Arr[6:7]
9896 //
9897 // r.ArrS[3:5].x
9898 //
9899 // but these would be valid:
9900 // r.ArrS[3].Arr[6:7]
9901 //
9902 // r.ArrS[3].x
9903
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009904 bool AllowUnitySizeArraySection = true;
9905 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +00009906
Dmitry Polukhin644a9252016-03-11 07:58:34 +00009907 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009908 E = E->IgnoreParenImpCasts();
9909
9910 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
9911 if (!isa<VarDecl>(CurE->getDecl()))
9912 break;
9913
9914 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009915
9916 // If we got a reference to a declaration, we should not expect any array
9917 // section before that.
9918 AllowUnitySizeArraySection = false;
9919 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +00009920
9921 // Record the component.
9922 CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent(
9923 CurE, CurE->getDecl()));
Samuel Antao5de996e2016-01-22 20:21:36 +00009924 continue;
9925 }
9926
9927 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
9928 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9929
9930 if (isa<CXXThisExpr>(BaseE))
9931 // We found a base expression: this->Val.
9932 RelevantExpr = CurE;
9933 else
9934 E = BaseE;
9935
9936 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
9937 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
9938 << CurE->getSourceRange();
9939 break;
9940 }
9941
9942 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
9943
9944 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
9945 // A bit-field cannot appear in a map clause.
9946 //
9947 if (FD->isBitField()) {
9948 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
9949 << CurE->getSourceRange();
9950 break;
9951 }
9952
9953 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9954 // If the type of a list item is a reference to a type T then the type
9955 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009956 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +00009957
9958 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
9959 // A list item cannot be a variable that is a member of a structure with
9960 // a union type.
9961 //
9962 if (auto *RT = CurType->getAs<RecordType>())
9963 if (RT->isUnionType()) {
9964 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
9965 << CurE->getSourceRange();
9966 break;
9967 }
9968
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009969 // If we got a member expression, we should not expect any array section
9970 // before that:
9971 //
9972 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
9973 // If a list item is an element of a structure, only the rightmost symbol
9974 // of the variable reference can be an array section.
9975 //
9976 AllowUnitySizeArraySection = false;
9977 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +00009978
9979 // Record the component.
9980 CurComponents.push_back(
9981 OMPClauseMappableExprCommon::MappableComponent(CurE, FD));
Samuel Antao5de996e2016-01-22 20:21:36 +00009982 continue;
9983 }
9984
9985 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
9986 E = CurE->getBase()->IgnoreParenImpCasts();
9987
9988 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
9989 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9990 << 0 << CurE->getSourceRange();
9991 break;
9992 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009993
9994 // If we got an array subscript that express the whole dimension we
9995 // can have any array expressions before. If it only expressing part of
9996 // the dimension, we can only have unitary-size array expressions.
9997 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
9998 E->getType()))
9999 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010000
10001 // Record the component - we don't have any declaration associated.
10002 CurComponents.push_back(
10003 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010004 continue;
10005 }
10006
10007 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010008 E = CurE->getBase()->IgnoreParenImpCasts();
10009
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010010 auto CurType =
10011 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
10012
Samuel Antao5de996e2016-01-22 20:21:36 +000010013 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10014 // If the type of a list item is a reference to a type T then the type
10015 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000010016 if (CurType->isReferenceType())
10017 CurType = CurType->getPointeeType();
10018
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010019 bool IsPointer = CurType->isAnyPointerType();
10020
10021 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010022 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10023 << 0 << CurE->getSourceRange();
10024 break;
10025 }
10026
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010027 bool NotWhole =
10028 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
10029 bool NotUnity =
10030 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
10031
10032 if (AllowWholeSizeArraySection && AllowUnitySizeArraySection) {
10033 // Any array section is currently allowed.
10034 //
10035 // If this array section refers to the whole dimension we can still
10036 // accept other array sections before this one, except if the base is a
10037 // pointer. Otherwise, only unitary sections are accepted.
10038 if (NotWhole || IsPointer)
10039 AllowWholeSizeArraySection = false;
10040 } else if ((AllowUnitySizeArraySection && NotUnity) ||
10041 (AllowWholeSizeArraySection && NotWhole)) {
10042 // A unity or whole array section is not allowed and that is not
10043 // compatible with the properties of the current array section.
10044 SemaRef.Diag(
10045 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
10046 << CurE->getSourceRange();
10047 break;
10048 }
Samuel Antao90927002016-04-26 14:54:23 +000010049
10050 // Record the component - we don't have any declaration associated.
10051 CurComponents.push_back(
10052 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010053 continue;
10054 }
10055
10056 // If nothing else worked, this is not a valid map clause expression.
10057 SemaRef.Diag(ELoc,
10058 diag::err_omp_expected_named_var_member_or_array_expression)
10059 << ERange;
10060 break;
10061 }
10062
10063 return RelevantExpr;
10064}
10065
10066// Return true if expression E associated with value VD has conflicts with other
10067// map information.
Samuel Antao90927002016-04-26 14:54:23 +000010068static bool CheckMapConflicts(
10069 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
10070 bool CurrentRegionOnly,
10071 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010072 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000010073 SourceLocation ELoc = E->getExprLoc();
10074 SourceRange ERange = E->getSourceRange();
10075
10076 // In order to easily check the conflicts we need to match each component of
10077 // the expression under test with the components of the expressions that are
10078 // already in the stack.
10079
Samuel Antao5de996e2016-01-22 20:21:36 +000010080 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010081 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010082 "Map clause expression with unexpected base!");
10083
10084 // Variables to help detecting enclosing problems in data environment nests.
10085 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000010086 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000010087
Samuel Antao90927002016-04-26 14:54:23 +000010088 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
10089 VD, CurrentRegionOnly,
10090 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
10091 StackComponents) -> bool {
10092
Samuel Antao5de996e2016-01-22 20:21:36 +000010093 assert(!StackComponents.empty() &&
10094 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010095 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010096 "Map clause expression with unexpected base!");
10097
Samuel Antao90927002016-04-26 14:54:23 +000010098 // The whole expression in the stack.
10099 auto *RE = StackComponents.front().getAssociatedExpression();
10100
Samuel Antao5de996e2016-01-22 20:21:36 +000010101 // Expressions must start from the same base. Here we detect at which
10102 // point both expressions diverge from each other and see if we can
10103 // detect if the memory referred to both expressions is contiguous and
10104 // do not overlap.
10105 auto CI = CurComponents.rbegin();
10106 auto CE = CurComponents.rend();
10107 auto SI = StackComponents.rbegin();
10108 auto SE = StackComponents.rend();
10109 for (; CI != CE && SI != SE; ++CI, ++SI) {
10110
10111 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
10112 // At most one list item can be an array item derived from a given
10113 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000010114 if (CurrentRegionOnly &&
10115 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
10116 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
10117 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
10118 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
10119 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000010120 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000010121 << CI->getAssociatedExpression()->getSourceRange();
10122 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
10123 diag::note_used_here)
10124 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000010125 return true;
10126 }
10127
10128 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000010129 if (CI->getAssociatedExpression()->getStmtClass() !=
10130 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000010131 break;
10132
10133 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000010134 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000010135 break;
10136 }
10137
10138 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10139 // List items of map clauses in the same construct must not share
10140 // original storage.
10141 //
10142 // If the expressions are exactly the same or one is a subset of the
10143 // other, it means they are sharing storage.
10144 if (CI == CE && SI == SE) {
10145 if (CurrentRegionOnly) {
10146 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10147 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10148 << RE->getSourceRange();
10149 return true;
10150 } else {
10151 // If we find the same expression in the enclosing data environment,
10152 // that is legal.
10153 IsEnclosedByDataEnvironmentExpr = true;
10154 return false;
10155 }
10156 }
10157
Samuel Antao90927002016-04-26 14:54:23 +000010158 QualType DerivedType =
10159 std::prev(CI)->getAssociatedDeclaration()->getType();
10160 SourceLocation DerivedLoc =
10161 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000010162
10163 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10164 // If the type of a list item is a reference to a type T then the type
10165 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010166 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010167
10168 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
10169 // A variable for which the type is pointer and an array section
10170 // derived from that variable must not appear as list items of map
10171 // clauses of the same construct.
10172 //
10173 // Also, cover one of the cases in:
10174 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10175 // If any part of the original storage of a list item has corresponding
10176 // storage in the device data environment, all of the original storage
10177 // must have corresponding storage in the device data environment.
10178 //
10179 if (DerivedType->isAnyPointerType()) {
10180 if (CI == CE || SI == SE) {
10181 SemaRef.Diag(
10182 DerivedLoc,
10183 diag::err_omp_pointer_mapped_along_with_derived_section)
10184 << DerivedLoc;
10185 } else {
10186 assert(CI != CE && SI != SE);
10187 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
10188 << DerivedLoc;
10189 }
10190 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10191 << RE->getSourceRange();
10192 return true;
10193 }
10194
10195 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10196 // List items of map clauses in the same construct must not share
10197 // original storage.
10198 //
10199 // An expression is a subset of the other.
10200 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
10201 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10202 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10203 << RE->getSourceRange();
10204 return true;
10205 }
10206
10207 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000010208 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000010209 if (!CurrentRegionOnly && SI != SE)
10210 EnclosingExpr = RE;
10211
10212 // The current expression is a subset of the expression in the data
10213 // environment.
10214 IsEnclosedByDataEnvironmentExpr |=
10215 (!CurrentRegionOnly && CI != CE && SI == SE);
10216
10217 return false;
10218 });
10219
10220 if (CurrentRegionOnly)
10221 return FoundError;
10222
10223 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10224 // If any part of the original storage of a list item has corresponding
10225 // storage in the device data environment, all of the original storage must
10226 // have corresponding storage in the device data environment.
10227 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
10228 // If a list item is an element of a structure, and a different element of
10229 // the structure has a corresponding list item in the device data environment
10230 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000010231 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000010232 // data environment prior to the task encountering the construct.
10233 //
10234 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
10235 SemaRef.Diag(ELoc,
10236 diag::err_omp_original_storage_is_shared_and_does_not_contain)
10237 << ERange;
10238 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
10239 << EnclosingExpr->getSourceRange();
10240 return true;
10241 }
10242
10243 return FoundError;
10244}
10245
Samuel Antao23abd722016-01-19 20:40:49 +000010246OMPClause *
10247Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
10248 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
10249 SourceLocation MapLoc, SourceLocation ColonLoc,
10250 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10251 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000010252 SmallVector<Expr *, 4> Vars;
10253
Samuel Antao90927002016-04-26 14:54:23 +000010254 // Keep track of the mappable components and base declarations in this clause.
10255 // Each entry in the list is going to have a list of components associated. We
10256 // record each set of the components so that we can build the clause later on.
10257 // In the end we should have the same amount of declarations and component
10258 // lists.
10259 OMPClauseMappableExprCommon::MappableExprComponentLists ClauseComponents;
10260 SmallVector<ValueDecl *, 16> ClauseBaseDeclarations;
10261
10262 ClauseComponents.reserve(VarList.size());
10263 ClauseBaseDeclarations.reserve(VarList.size());
10264
Kelvin Li0bff7af2015-11-23 05:32:03 +000010265 for (auto &RE : VarList) {
10266 assert(RE && "Null expr in omp map");
10267 if (isa<DependentScopeDeclRefExpr>(RE)) {
10268 // It will be analyzed later.
10269 Vars.push_back(RE);
10270 continue;
10271 }
10272 SourceLocation ELoc = RE->getExprLoc();
10273
Kelvin Li0bff7af2015-11-23 05:32:03 +000010274 auto *VE = RE->IgnoreParenLValueCasts();
10275
10276 if (VE->isValueDependent() || VE->isTypeDependent() ||
10277 VE->isInstantiationDependent() ||
10278 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010279 // We can only analyze this information once the missing information is
10280 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010281 Vars.push_back(RE);
10282 continue;
10283 }
10284
10285 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010286
Samuel Antao5de996e2016-01-22 20:21:36 +000010287 if (!RE->IgnoreParenImpCasts()->isLValue()) {
10288 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
10289 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010290 continue;
10291 }
10292
Samuel Antao90927002016-04-26 14:54:23 +000010293 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
10294 ValueDecl *CurDeclaration = nullptr;
10295
10296 // Obtain the array or member expression bases if required. Also, fill the
10297 // components array with all the components identified in the process.
10298 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr, CurComponents);
Samuel Antao5de996e2016-01-22 20:21:36 +000010299 if (!BE)
10300 continue;
10301
Samuel Antao90927002016-04-26 14:54:23 +000010302 assert(!CurComponents.empty() &&
10303 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010304
Samuel Antao90927002016-04-26 14:54:23 +000010305 // For the following checks, we rely on the base declaration which is
10306 // expected to be associated with the last component. The declaration is
10307 // expected to be a variable or a field (if 'this' is being mapped).
10308 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
10309 assert(CurDeclaration && "Null decl on map clause.");
10310 assert(
10311 CurDeclaration->isCanonicalDecl() &&
10312 "Expecting components to have associated only canonical declarations.");
10313
10314 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
10315 auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000010316
10317 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000010318 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000010319
10320 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
10321 // threadprivate variables cannot appear in a map clause.
10322 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000010323 auto DVar = DSAStack->getTopDSA(VD, false);
10324 Diag(ELoc, diag::err_omp_threadprivate_in_map);
10325 ReportOriginalDSA(*this, DSAStack, VD, DVar);
10326 continue;
10327 }
10328
Samuel Antao5de996e2016-01-22 20:21:36 +000010329 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
10330 // A list item cannot appear in both a map clause and a data-sharing
10331 // attribute clause on the same construct.
10332 //
10333 // TODO: Implement this check - it cannot currently be tested because of
10334 // missing implementation of the other data sharing clauses in target
10335 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010336
Samuel Antao5de996e2016-01-22 20:21:36 +000010337 // Check conflicts with other map clause expressions. We check the conflicts
10338 // with the current construct separately from the enclosing data
10339 // environment, because the restrictions are different.
Samuel Antao90927002016-04-26 14:54:23 +000010340 if (CheckMapConflicts(*this, DSAStack, CurDeclaration, SimpleExpr,
10341 /*CurrentRegionOnly=*/true, CurComponents))
Samuel Antao5de996e2016-01-22 20:21:36 +000010342 break;
Samuel Antao90927002016-04-26 14:54:23 +000010343 if (CheckMapConflicts(*this, DSAStack, CurDeclaration, SimpleExpr,
10344 /*CurrentRegionOnly=*/false, CurComponents))
Samuel Antao5de996e2016-01-22 20:21:36 +000010345 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000010346
Samuel Antao5de996e2016-01-22 20:21:36 +000010347 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10348 // If the type of a list item is a reference to a type T then the type will
10349 // be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010350 QualType Type = CurDeclaration->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010351
10352 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000010353 // A list item must have a mappable type.
10354 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
10355 DSAStack, Type))
10356 continue;
10357
Samuel Antaodf67fc42016-01-19 19:15:56 +000010358 // target enter data
10359 // OpenMP [2.10.2, Restrictions, p. 99]
10360 // A map-type must be specified in all map clauses and must be either
10361 // to or alloc.
10362 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
10363 if (DKind == OMPD_target_enter_data &&
10364 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
10365 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +000010366 << (IsMapTypeImplicit ? 1 : 0)
10367 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +000010368 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010369 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +000010370 }
10371
Samuel Antao72590762016-01-19 20:04:50 +000010372 // target exit_data
10373 // OpenMP [2.10.3, Restrictions, p. 102]
10374 // A map-type must be specified in all map clauses and must be either
10375 // from, release, or delete.
10376 DKind = DSAStack->getCurrentDirective();
10377 if (DKind == OMPD_target_exit_data &&
10378 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
10379 MapType == OMPC_MAP_delete)) {
10380 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +000010381 << (IsMapTypeImplicit ? 1 : 0)
10382 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +000010383 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010384 continue;
Samuel Antao72590762016-01-19 20:04:50 +000010385 }
10386
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010387 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10388 // A list item cannot appear in both a map clause and a data-sharing
10389 // attribute clause on the same construct
10390 if (DKind == OMPD_target && VD) {
10391 auto DVar = DSAStack->getTopDSA(VD, false);
10392 if (isOpenMPPrivate(DVar.CKind)) {
10393 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
10394 << getOpenMPClauseName(DVar.CKind)
10395 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Samuel Antao90927002016-04-26 14:54:23 +000010396 ReportOriginalDSA(*this, DSAStack, CurDeclaration, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010397 continue;
10398 }
10399 }
10400
Samuel Antao90927002016-04-26 14:54:23 +000010401 // Save the current expression.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010402 Vars.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000010403
10404 // Store the components in the stack so that they can be used to check
10405 // against other clauses later on.
10406 DSAStack->addMappableExpressionComponents(CurDeclaration, CurComponents);
10407
10408 // Save the components and declaration to create the clause. For purposes of
10409 // the clause creation, any component list that has has base 'this' uses
10410 // null has
10411 ClauseComponents.resize(ClauseComponents.size() + 1);
10412 ClauseComponents.back().append(CurComponents.begin(), CurComponents.end());
10413 ClauseBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
10414 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010415 }
Kelvin Li0bff7af2015-11-23 05:32:03 +000010416
Samuel Antao5de996e2016-01-22 20:21:36 +000010417 // We need to produce a map clause even if we don't have variables so that
10418 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao90927002016-04-26 14:54:23 +000010419 return OMPMapClause::Create(
10420 Context, StartLoc, LParenLoc, EndLoc, Vars, ClauseBaseDeclarations,
10421 ClauseComponents, MapTypeModifier, MapType, IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010422}
Kelvin Li099bb8c2015-11-24 20:50:12 +000010423
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010424QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
10425 TypeResult ParsedType) {
10426 assert(ParsedType.isUsable());
10427
10428 QualType ReductionType = GetTypeFromParser(ParsedType.get());
10429 if (ReductionType.isNull())
10430 return QualType();
10431
10432 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
10433 // A type name in a declare reduction directive cannot be a function type, an
10434 // array type, a reference type, or a type qualified with const, volatile or
10435 // restrict.
10436 if (ReductionType.hasQualifiers()) {
10437 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
10438 return QualType();
10439 }
10440
10441 if (ReductionType->isFunctionType()) {
10442 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
10443 return QualType();
10444 }
10445 if (ReductionType->isReferenceType()) {
10446 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
10447 return QualType();
10448 }
10449 if (ReductionType->isArrayType()) {
10450 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
10451 return QualType();
10452 }
10453 return ReductionType;
10454}
10455
10456Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
10457 Scope *S, DeclContext *DC, DeclarationName Name,
10458 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
10459 AccessSpecifier AS, Decl *PrevDeclInScope) {
10460 SmallVector<Decl *, 8> Decls;
10461 Decls.reserve(ReductionTypes.size());
10462
10463 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
10464 ForRedeclaration);
10465 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
10466 // A reduction-identifier may not be re-declared in the current scope for the
10467 // same type or for a type that is compatible according to the base language
10468 // rules.
10469 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
10470 OMPDeclareReductionDecl *PrevDRD = nullptr;
10471 bool InCompoundScope = true;
10472 if (S != nullptr) {
10473 // Find previous declaration with the same name not referenced in other
10474 // declarations.
10475 FunctionScopeInfo *ParentFn = getEnclosingFunction();
10476 InCompoundScope =
10477 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
10478 LookupName(Lookup, S);
10479 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
10480 /*AllowInlineNamespace=*/false);
10481 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
10482 auto Filter = Lookup.makeFilter();
10483 while (Filter.hasNext()) {
10484 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
10485 if (InCompoundScope) {
10486 auto I = UsedAsPrevious.find(PrevDecl);
10487 if (I == UsedAsPrevious.end())
10488 UsedAsPrevious[PrevDecl] = false;
10489 if (auto *D = PrevDecl->getPrevDeclInScope())
10490 UsedAsPrevious[D] = true;
10491 }
10492 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
10493 PrevDecl->getLocation();
10494 }
10495 Filter.done();
10496 if (InCompoundScope) {
10497 for (auto &PrevData : UsedAsPrevious) {
10498 if (!PrevData.second) {
10499 PrevDRD = PrevData.first;
10500 break;
10501 }
10502 }
10503 }
10504 } else if (PrevDeclInScope != nullptr) {
10505 auto *PrevDRDInScope = PrevDRD =
10506 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
10507 do {
10508 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
10509 PrevDRDInScope->getLocation();
10510 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
10511 } while (PrevDRDInScope != nullptr);
10512 }
10513 for (auto &TyData : ReductionTypes) {
10514 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
10515 bool Invalid = false;
10516 if (I != PreviousRedeclTypes.end()) {
10517 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
10518 << TyData.first;
10519 Diag(I->second, diag::note_previous_definition);
10520 Invalid = true;
10521 }
10522 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
10523 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
10524 Name, TyData.first, PrevDRD);
10525 DC->addDecl(DRD);
10526 DRD->setAccess(AS);
10527 Decls.push_back(DRD);
10528 if (Invalid)
10529 DRD->setInvalidDecl();
10530 else
10531 PrevDRD = DRD;
10532 }
10533
10534 return DeclGroupPtrTy::make(
10535 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
10536}
10537
10538void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
10539 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10540
10541 // Enter new function scope.
10542 PushFunctionScope();
10543 getCurFunction()->setHasBranchProtectedScope();
10544 getCurFunction()->setHasOMPDeclareReductionCombiner();
10545
10546 if (S != nullptr)
10547 PushDeclContext(S, DRD);
10548 else
10549 CurContext = DRD;
10550
10551 PushExpressionEvaluationContext(PotentiallyEvaluated);
10552
10553 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010554 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
10555 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
10556 // uses semantics of argument handles by value, but it should be passed by
10557 // reference. C lang does not support references, so pass all parameters as
10558 // pointers.
10559 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010560 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010561 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010562 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
10563 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
10564 // uses semantics of argument handles by value, but it should be passed by
10565 // reference. C lang does not support references, so pass all parameters as
10566 // pointers.
10567 // Create 'T omp_out;' variable.
10568 auto *OmpOutParm =
10569 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
10570 if (S != nullptr) {
10571 PushOnScopeChains(OmpInParm, S);
10572 PushOnScopeChains(OmpOutParm, S);
10573 } else {
10574 DRD->addDecl(OmpInParm);
10575 DRD->addDecl(OmpOutParm);
10576 }
10577}
10578
10579void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
10580 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10581 DiscardCleanupsInEvaluationContext();
10582 PopExpressionEvaluationContext();
10583
10584 PopDeclContext();
10585 PopFunctionScopeInfo();
10586
10587 if (Combiner != nullptr)
10588 DRD->setCombiner(Combiner);
10589 else
10590 DRD->setInvalidDecl();
10591}
10592
10593void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
10594 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10595
10596 // Enter new function scope.
10597 PushFunctionScope();
10598 getCurFunction()->setHasBranchProtectedScope();
10599
10600 if (S != nullptr)
10601 PushDeclContext(S, DRD);
10602 else
10603 CurContext = DRD;
10604
10605 PushExpressionEvaluationContext(PotentiallyEvaluated);
10606
10607 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010608 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
10609 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
10610 // uses semantics of argument handles by value, but it should be passed by
10611 // reference. C lang does not support references, so pass all parameters as
10612 // pointers.
10613 // Create 'T omp_priv;' variable.
10614 auto *OmpPrivParm =
10615 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010616 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
10617 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
10618 // uses semantics of argument handles by value, but it should be passed by
10619 // reference. C lang does not support references, so pass all parameters as
10620 // pointers.
10621 // Create 'T omp_orig;' variable.
10622 auto *OmpOrigParm =
10623 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010624 if (S != nullptr) {
10625 PushOnScopeChains(OmpPrivParm, S);
10626 PushOnScopeChains(OmpOrigParm, S);
10627 } else {
10628 DRD->addDecl(OmpPrivParm);
10629 DRD->addDecl(OmpOrigParm);
10630 }
10631}
10632
10633void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
10634 Expr *Initializer) {
10635 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10636 DiscardCleanupsInEvaluationContext();
10637 PopExpressionEvaluationContext();
10638
10639 PopDeclContext();
10640 PopFunctionScopeInfo();
10641
10642 if (Initializer != nullptr)
10643 DRD->setInitializer(Initializer);
10644 else
10645 DRD->setInvalidDecl();
10646}
10647
10648Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
10649 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
10650 for (auto *D : DeclReductions.get()) {
10651 if (IsValid) {
10652 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10653 if (S != nullptr)
10654 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
10655 } else
10656 D->setInvalidDecl();
10657 }
10658 return DeclReductions;
10659}
10660
Kelvin Li099bb8c2015-11-24 20:50:12 +000010661OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
10662 SourceLocation StartLoc,
10663 SourceLocation LParenLoc,
10664 SourceLocation EndLoc) {
10665 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010666
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010667 // OpenMP [teams Constrcut, Restrictions]
10668 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010669 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
10670 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010671 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010672
10673 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10674}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010675
10676OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
10677 SourceLocation StartLoc,
10678 SourceLocation LParenLoc,
10679 SourceLocation EndLoc) {
10680 Expr *ValExpr = ThreadLimit;
10681
10682 // OpenMP [teams Constrcut, Restrictions]
10683 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010684 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
10685 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010686 return nullptr;
10687
10688 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
10689 EndLoc);
10690}
Alexey Bataeva0569352015-12-01 10:17:31 +000010691
10692OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
10693 SourceLocation StartLoc,
10694 SourceLocation LParenLoc,
10695 SourceLocation EndLoc) {
10696 Expr *ValExpr = Priority;
10697
10698 // OpenMP [2.9.1, task Constrcut]
10699 // The priority-value is a non-negative numerical scalar expression.
10700 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
10701 /*StrictlyPositive=*/false))
10702 return nullptr;
10703
10704 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10705}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000010706
10707OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
10708 SourceLocation StartLoc,
10709 SourceLocation LParenLoc,
10710 SourceLocation EndLoc) {
10711 Expr *ValExpr = Grainsize;
10712
10713 // OpenMP [2.9.2, taskloop Constrcut]
10714 // The parameter of the grainsize clause must be a positive integer
10715 // expression.
10716 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
10717 /*StrictlyPositive=*/true))
10718 return nullptr;
10719
10720 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10721}
Alexey Bataev382967a2015-12-08 12:06:20 +000010722
10723OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
10724 SourceLocation StartLoc,
10725 SourceLocation LParenLoc,
10726 SourceLocation EndLoc) {
10727 Expr *ValExpr = NumTasks;
10728
10729 // OpenMP [2.9.2, taskloop Constrcut]
10730 // The parameter of the num_tasks clause must be a positive integer
10731 // expression.
10732 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
10733 /*StrictlyPositive=*/true))
10734 return nullptr;
10735
10736 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10737}
10738
Alexey Bataev28c75412015-12-15 08:19:24 +000010739OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
10740 SourceLocation LParenLoc,
10741 SourceLocation EndLoc) {
10742 // OpenMP [2.13.2, critical construct, Description]
10743 // ... where hint-expression is an integer constant expression that evaluates
10744 // to a valid lock hint.
10745 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
10746 if (HintExpr.isInvalid())
10747 return nullptr;
10748 return new (Context)
10749 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
10750}
10751
Carlo Bertollib4adf552016-01-15 18:50:31 +000010752OMPClause *Sema::ActOnOpenMPDistScheduleClause(
10753 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
10754 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
10755 SourceLocation EndLoc) {
10756 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
10757 std::string Values;
10758 Values += "'";
10759 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
10760 Values += "'";
10761 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
10762 << Values << getOpenMPClauseName(OMPC_dist_schedule);
10763 return nullptr;
10764 }
10765 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000010766 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000010767 if (ChunkSize) {
10768 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
10769 !ChunkSize->isInstantiationDependent() &&
10770 !ChunkSize->containsUnexpandedParameterPack()) {
10771 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
10772 ExprResult Val =
10773 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
10774 if (Val.isInvalid())
10775 return nullptr;
10776
10777 ValExpr = Val.get();
10778
10779 // OpenMP [2.7.1, Restrictions]
10780 // chunk_size must be a loop invariant integer expression with a positive
10781 // value.
10782 llvm::APSInt Result;
10783 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
10784 if (Result.isSigned() && !Result.isStrictlyPositive()) {
10785 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
10786 << "dist_schedule" << ChunkSize->getSourceRange();
10787 return nullptr;
10788 }
10789 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev5a3af132016-03-29 08:58:54 +000010790 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
10791 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
10792 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010793 }
10794 }
10795 }
10796
10797 return new (Context)
10798 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000010799 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010800}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000010801
10802OMPClause *Sema::ActOnOpenMPDefaultmapClause(
10803 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
10804 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
10805 SourceLocation KindLoc, SourceLocation EndLoc) {
10806 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
10807 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
10808 Kind != OMPC_DEFAULTMAP_scalar) {
10809 std::string Value;
10810 SourceLocation Loc;
10811 Value += "'";
10812 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
10813 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10814 OMPC_DEFAULTMAP_MODIFIER_tofrom);
10815 Loc = MLoc;
10816 } else {
10817 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10818 OMPC_DEFAULTMAP_scalar);
10819 Loc = KindLoc;
10820 }
10821 Value += "'";
10822 Diag(Loc, diag::err_omp_unexpected_clause_value)
10823 << Value << getOpenMPClauseName(OMPC_defaultmap);
10824 return nullptr;
10825 }
10826
10827 return new (Context)
10828 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
10829}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010830
10831bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
10832 DeclContext *CurLexicalContext = getCurLexicalContext();
10833 if (!CurLexicalContext->isFileContext() &&
10834 !CurLexicalContext->isExternCContext() &&
10835 !CurLexicalContext->isExternCXXContext()) {
10836 Diag(Loc, diag::err_omp_region_not_file_context);
10837 return false;
10838 }
10839 if (IsInOpenMPDeclareTargetContext) {
10840 Diag(Loc, diag::err_omp_enclosed_declare_target);
10841 return false;
10842 }
10843
10844 IsInOpenMPDeclareTargetContext = true;
10845 return true;
10846}
10847
10848void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
10849 assert(IsInOpenMPDeclareTargetContext &&
10850 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
10851
10852 IsInOpenMPDeclareTargetContext = false;
10853}
10854
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010855void
10856Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec,
10857 const DeclarationNameInfo &Id,
10858 OMPDeclareTargetDeclAttr::MapTypeTy MT,
10859 NamedDeclSetType &SameDirectiveDecls) {
10860 LookupResult Lookup(*this, Id, LookupOrdinaryName);
10861 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
10862
10863 if (Lookup.isAmbiguous())
10864 return;
10865 Lookup.suppressDiagnostics();
10866
10867 if (!Lookup.isSingleResult()) {
10868 if (TypoCorrection Corrected =
10869 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
10870 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
10871 CTK_ErrorRecovery)) {
10872 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
10873 << Id.getName());
10874 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
10875 return;
10876 }
10877
10878 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
10879 return;
10880 }
10881
10882 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
10883 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
10884 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
10885 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
10886
10887 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
10888 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
10889 ND->addAttr(A);
10890 if (ASTMutationListener *ML = Context.getASTMutationListener())
10891 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
10892 checkDeclIsAllowedInOpenMPTarget(nullptr, ND);
10893 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
10894 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
10895 << Id.getName();
10896 }
10897 } else
10898 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
10899}
10900
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010901static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
10902 Sema &SemaRef, Decl *D) {
10903 if (!D)
10904 return;
10905 Decl *LD = nullptr;
10906 if (isa<TagDecl>(D)) {
10907 LD = cast<TagDecl>(D)->getDefinition();
10908 } else if (isa<VarDecl>(D)) {
10909 LD = cast<VarDecl>(D)->getDefinition();
10910
10911 // If this is an implicit variable that is legal and we do not need to do
10912 // anything.
10913 if (cast<VarDecl>(D)->isImplicit()) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010914 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
10915 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
10916 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010917 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010918 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010919 return;
10920 }
10921
10922 } else if (isa<FunctionDecl>(D)) {
10923 const FunctionDecl *FD = nullptr;
10924 if (cast<FunctionDecl>(D)->hasBody(FD))
10925 LD = const_cast<FunctionDecl *>(FD);
10926
10927 // If the definition is associated with the current declaration in the
10928 // target region (it can be e.g. a lambda) that is legal and we do not need
10929 // to do anything else.
10930 if (LD == D) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010931 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
10932 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
10933 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010934 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010935 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010936 return;
10937 }
10938 }
10939 if (!LD)
10940 LD = D;
10941 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
10942 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
10943 // Outlined declaration is not declared target.
10944 if (LD->isOutOfLine()) {
10945 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
10946 SemaRef.Diag(SL, diag::note_used_here) << SR;
10947 } else {
10948 DeclContext *DC = LD->getDeclContext();
10949 while (DC) {
10950 if (isa<FunctionDecl>(DC) &&
10951 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
10952 break;
10953 DC = DC->getParent();
10954 }
10955 if (DC)
10956 return;
10957
10958 // Is not declared in target context.
10959 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
10960 SemaRef.Diag(SL, diag::note_used_here) << SR;
10961 }
10962 // Mark decl as declared target to prevent further diagnostic.
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010963 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
10964 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
10965 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010966 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010967 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010968 }
10969}
10970
10971static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
10972 Sema &SemaRef, DSAStackTy *Stack,
10973 ValueDecl *VD) {
10974 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
10975 return true;
10976 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
10977 return false;
10978 return true;
10979}
10980
10981void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
10982 if (!D || D->isInvalidDecl())
10983 return;
10984 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
10985 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
10986 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
10987 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
10988 if (DSAStack->isThreadPrivate(VD)) {
10989 Diag(SL, diag::err_omp_threadprivate_in_target);
10990 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
10991 return;
10992 }
10993 }
10994 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
10995 // Problem if any with var declared with incomplete type will be reported
10996 // as normal, so no need to check it here.
10997 if ((E || !VD->getType()->isIncompleteType()) &&
10998 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
10999 // Mark decl as declared target to prevent further diagnostic.
11000 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011001 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11002 Context, OMPDeclareTargetDeclAttr::MT_To);
11003 VD->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011004 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011005 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011006 }
11007 return;
11008 }
11009 }
11010 if (!E) {
11011 // Checking declaration inside declare target region.
11012 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
11013 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011014 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11015 Context, OMPDeclareTargetDeclAttr::MT_To);
11016 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011017 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011018 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011019 }
11020 return;
11021 }
11022 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
11023}