blob: b3f711bc7fa2b56b09bd7d562482c5eb91571dcc [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alexey Bataeva769e072013-03-22 06:34:35 +00006//
7//===----------------------------------------------------------------------===//
8/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00009/// This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000010/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000011///
12//===----------------------------------------------------------------------===//
13
Alexey Bataevb08f89f2015-08-14 12:25:37 +000014#include "TreeTransform.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000016#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva839ddd2016-03-17 10:19:46 +000017#include "clang/AST/CXXInheritance.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000018#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000019#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000020#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000021#include "clang/AST/StmtCXX.h"
22#include "clang/AST/StmtOpenMP.h"
23#include "clang/AST/StmtVisitor.h"
Patrick Lystere13b1e32019-01-02 19:28:48 +000024#include "clang/AST/TypeOrdering.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000025#include "clang/Basic/OpenMPKinds.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000026#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000027#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000028#include "clang/Sema/Scope.h"
29#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000030#include "clang/Sema/SemaInternal.h"
Alexey Bataevfa312f32017-07-21 18:48:21 +000031#include "llvm/ADT/PointerEmbeddedInt.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000032using namespace clang;
33
Alexey Bataev758e55e2013-09-06 18:03:48 +000034//===----------------------------------------------------------------------===//
35// Stack of data-sharing attributes for variables
36//===----------------------------------------------------------------------===//
37
Alexey Bataeve3727102018-04-18 15:57:46 +000038static const Expr *checkMapClauseExpressionBase(
Alexey Bataevf47c4b42017-09-26 13:47:31 +000039 Sema &SemaRef, Expr *E,
40 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
Alexey Bataevb7a9b742017-12-05 19:20:09 +000041 OpenMPClauseKind CKind, bool NoDiagnose);
Alexey Bataevf47c4b42017-09-26 13:47:31 +000042
Alexey Bataev758e55e2013-09-06 18:03:48 +000043namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000044/// Default data sharing attributes, which can be applied to directive.
Alexey Bataev758e55e2013-09-06 18:03:48 +000045enum DefaultDataSharingAttributes {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000046 DSA_unspecified = 0, /// Data sharing attribute not specified.
47 DSA_none = 1 << 0, /// Default data sharing attribute 'none'.
48 DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'.
Alexey Bataev2fd0cb22017-10-05 17:51:39 +000049};
50
51/// Attributes of the defaultmap clause.
52enum DefaultMapAttributes {
53 DMA_unspecified, /// Default mapping is not specified.
54 DMA_tofrom_scalar, /// Default mapping is 'tofrom:scalar'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000055};
Alexey Bataev7ff55242014-06-19 09:13:45 +000056
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000057/// Stack for tracking declarations used in OpenMP directives and
Alexey Bataev758e55e2013-09-06 18:03:48 +000058/// clauses and their data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +000059class DSAStackTy {
Alexey Bataev758e55e2013-09-06 18:03:48 +000060public:
Alexey Bataeve3727102018-04-18 15:57:46 +000061 struct DSAVarData {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000062 OpenMPDirectiveKind DKind = OMPD_unknown;
63 OpenMPClauseKind CKind = OMPC_unknown;
Alexey Bataeve3727102018-04-18 15:57:46 +000064 const Expr *RefExpr = nullptr;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000065 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000066 SourceLocation ImplicitDSALoc;
Alexey Bataev4d4624c2017-07-20 16:47:47 +000067 DSAVarData() = default;
Alexey Bataeve3727102018-04-18 15:57:46 +000068 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
69 const Expr *RefExpr, DeclRefExpr *PrivateCopy,
70 SourceLocation ImplicitDSALoc)
Alexey Bataevf189cb72017-07-24 14:52:13 +000071 : DKind(DKind), CKind(CKind), RefExpr(RefExpr),
72 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000073 };
Alexey Bataeve3727102018-04-18 15:57:46 +000074 using OperatorOffsetTy =
75 llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>;
Alexey Bataevf138fda2018-08-13 19:04:24 +000076 using DoacrossDependMapTy =
77 llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>;
Alexey Bataeved09d242014-05-28 05:53:51 +000078
Alexey Bataev758e55e2013-09-06 18:03:48 +000079private:
Alexey Bataeve3727102018-04-18 15:57:46 +000080 struct DSAInfo {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000081 OpenMPClauseKind Attributes = OMPC_unknown;
82 /// Pointer to a reference expression and a flag which shows that the
83 /// variable is marked as lastprivate(true) or not (false).
Alexey Bataeve3727102018-04-18 15:57:46 +000084 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000085 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000086 };
Alexey Bataeve3727102018-04-18 15:57:46 +000087 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>;
88 using AlignedMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>;
89 using LCDeclInfo = std::pair<unsigned, VarDecl *>;
90 using LoopControlVariablesMapTy =
91 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>;
Samuel Antao6890b092016-07-28 14:25:09 +000092 /// Struct that associates a component with the clause kind where they are
93 /// found.
94 struct MappedExprComponentTy {
95 OMPClauseMappableExprCommon::MappableExprComponentLists Components;
96 OpenMPClauseKind Kind = OMPC_unknown;
97 };
Alexey Bataeve3727102018-04-18 15:57:46 +000098 using MappedExprComponentsTy =
99 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>;
100 using CriticalsWithHintsTy =
101 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000102 struct ReductionData {
Alexey Bataeve3727102018-04-18 15:57:46 +0000103 using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000104 SourceRange ReductionRange;
Alexey Bataevf87fa882017-07-21 19:26:22 +0000105 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000106 ReductionData() = default;
107 void set(BinaryOperatorKind BO, SourceRange RR) {
108 ReductionRange = RR;
109 ReductionOp = BO;
110 }
111 void set(const Expr *RefExpr, SourceRange RR) {
112 ReductionRange = RR;
113 ReductionOp = RefExpr;
114 }
115 };
Alexey Bataeve3727102018-04-18 15:57:46 +0000116 using DeclReductionMapTy =
117 llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000118
Alexey Bataeve3727102018-04-18 15:57:46 +0000119 struct SharingMapTy {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000120 DeclSAMapTy SharingMap;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000121 DeclReductionMapTy ReductionMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000122 AlignedMapTy AlignedMap;
Samuel Antao90927002016-04-26 14:54:23 +0000123 MappedExprComponentsTy MappedExprComponents;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000124 LoopControlVariablesMapTy LCVMap;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000125 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000126 SourceLocation DefaultAttrLoc;
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000127 DefaultMapAttributes DefaultMapAttr = DMA_unspecified;
128 SourceLocation DefaultMapAttrLoc;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000129 OpenMPDirectiveKind Directive = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000130 DeclarationNameInfo DirectiveName;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000131 Scope *CurScope = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000132 SourceLocation ConstructLoc;
Alexey Bataev8b427062016-05-25 12:36:08 +0000133 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
134 /// get the data (loop counters etc.) about enclosing loop-based construct.
135 /// This data is required during codegen.
136 DoacrossDependMapTy DoacrossDepends;
Patrick Lyster16471942019-02-06 18:18:02 +0000137 /// First argument (Expr *) contains optional argument of the
Alexey Bataev346265e2015-09-25 10:37:12 +0000138 /// 'ordered' clause, the second one is true if the regions has 'ordered'
139 /// clause, false otherwise.
Alexey Bataevf138fda2018-08-13 19:04:24 +0000140 llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion;
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000141 unsigned AssociatedLoops = 1;
Alexey Bataev05be1da2019-07-18 17:49:13 +0000142 bool HasMutipleLoops = false;
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000143 const Decl *PossiblyLoopCounter = nullptr;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000144 bool NowaitRegion = false;
145 bool CancelRegion = false;
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000146 bool LoopStart = false;
Richard Smith0621a8f2019-05-31 00:45:10 +0000147 bool BodyComplete = false;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000148 SourceLocation InnerTeamsRegionLoc;
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000149 /// Reference to the taskgroup task_reduction reference expression.
150 Expr *TaskgroupReductionRef = nullptr;
Patrick Lystere13b1e32019-01-02 19:28:48 +0000151 llvm::DenseSet<QualType> MappedClassesQualTypes;
Alexey Bataeva495c642019-03-11 19:51:42 +0000152 /// List of globals marked as declare target link in this target region
153 /// (isOpenMPTargetExecutionDirective(Directive) == true).
154 llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls;
Alexey Bataeved09d242014-05-28 05:53:51 +0000155 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000156 Scope *CurScope, SourceLocation Loc)
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000157 : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
158 ConstructLoc(Loc) {}
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000159 SharingMapTy() = default;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000160 };
161
Alexey Bataeve3727102018-04-18 15:57:46 +0000162 using StackTy = SmallVector<SharingMapTy, 4>;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000163
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000164 /// Stack of used declaration and their data-sharing attributes.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000165 DeclSAMapTy Threadprivates;
Alexey Bataev4b465392017-04-26 15:06:24 +0000166 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
167 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000168 /// true, if check for DSA must be from parent directive, false, if
Alexey Bataev39f915b82015-05-08 10:41:21 +0000169 /// from current directive.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000170 OpenMPClauseKind ClauseKindMode = OMPC_unknown;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000171 Sema &SemaRef;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000172 bool ForceCapturing = false;
Alexey Bataevd1caf932019-09-30 14:05:26 +0000173 /// true if all the variables in the target executable directives must be
Alexey Bataev60705422018-10-30 15:50:12 +0000174 /// captured by reference.
175 bool ForceCaptureByReferenceInTargetExecutable = false;
Alexey Bataev28c75412015-12-15 08:19:24 +0000176 CriticalsWithHintsTy Criticals;
Richard Smith0621a8f2019-05-31 00:45:10 +0000177 unsigned IgnoredStackElements = 0;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000178
Richard Smith375dec52019-05-30 23:21:14 +0000179 /// Iterators over the stack iterate in order from innermost to outermost
180 /// directive.
181 using const_iterator = StackTy::const_reverse_iterator;
182 const_iterator begin() const {
Richard Smith0621a8f2019-05-31 00:45:10 +0000183 return Stack.empty() ? const_iterator()
184 : Stack.back().first.rbegin() + IgnoredStackElements;
Richard Smith375dec52019-05-30 23:21:14 +0000185 }
186 const_iterator end() const {
187 return Stack.empty() ? const_iterator() : Stack.back().first.rend();
188 }
189 using iterator = StackTy::reverse_iterator;
190 iterator begin() {
Richard Smith0621a8f2019-05-31 00:45:10 +0000191 return Stack.empty() ? iterator()
192 : Stack.back().first.rbegin() + IgnoredStackElements;
Richard Smith375dec52019-05-30 23:21:14 +0000193 }
194 iterator end() {
195 return Stack.empty() ? iterator() : Stack.back().first.rend();
196 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000197
Richard Smith375dec52019-05-30 23:21:14 +0000198 // Convenience operations to get at the elements of the stack.
Alexey Bataeved09d242014-05-28 05:53:51 +0000199
Alexey Bataev4b465392017-04-26 15:06:24 +0000200 bool isStackEmpty() const {
201 return Stack.empty() ||
202 Stack.back().second != CurrentNonCapturingFunctionScope ||
Richard Smith0621a8f2019-05-31 00:45:10 +0000203 Stack.back().first.size() <= IgnoredStackElements;
Alexey Bataev4b465392017-04-26 15:06:24 +0000204 }
Richard Smith375dec52019-05-30 23:21:14 +0000205 size_t getStackSize() const {
Richard Smith0621a8f2019-05-31 00:45:10 +0000206 return isStackEmpty() ? 0
207 : Stack.back().first.size() - IgnoredStackElements;
Richard Smith375dec52019-05-30 23:21:14 +0000208 }
209
210 SharingMapTy *getTopOfStackOrNull() {
Richard Smith0621a8f2019-05-31 00:45:10 +0000211 size_t Size = getStackSize();
212 if (Size == 0)
Richard Smith375dec52019-05-30 23:21:14 +0000213 return nullptr;
Richard Smith0621a8f2019-05-31 00:45:10 +0000214 return &Stack.back().first[Size - 1];
Richard Smith375dec52019-05-30 23:21:14 +0000215 }
216 const SharingMapTy *getTopOfStackOrNull() const {
217 return const_cast<DSAStackTy&>(*this).getTopOfStackOrNull();
218 }
219 SharingMapTy &getTopOfStack() {
220 assert(!isStackEmpty() && "no current directive");
221 return *getTopOfStackOrNull();
222 }
223 const SharingMapTy &getTopOfStack() const {
224 return const_cast<DSAStackTy&>(*this).getTopOfStack();
225 }
226
227 SharingMapTy *getSecondOnStackOrNull() {
228 size_t Size = getStackSize();
229 if (Size <= 1)
230 return nullptr;
231 return &Stack.back().first[Size - 2];
232 }
233 const SharingMapTy *getSecondOnStackOrNull() const {
234 return const_cast<DSAStackTy&>(*this).getSecondOnStackOrNull();
235 }
236
237 /// Get the stack element at a certain level (previously returned by
238 /// \c getNestingLevel).
239 ///
240 /// Note that nesting levels count from outermost to innermost, and this is
241 /// the reverse of our iteration order where new inner levels are pushed at
242 /// the front of the stack.
243 SharingMapTy &getStackElemAtLevel(unsigned Level) {
244 assert(Level < getStackSize() && "no such stack element");
245 return Stack.back().first[Level];
246 }
247 const SharingMapTy &getStackElemAtLevel(unsigned Level) const {
248 return const_cast<DSAStackTy&>(*this).getStackElemAtLevel(Level);
249 }
250
251 DSAVarData getDSA(const_iterator &Iter, ValueDecl *D) const;
252
253 /// Checks if the variable is a local for OpenMP region.
254 bool isOpenMPLocal(VarDecl *D, const_iterator Iter) const;
Alexey Bataev4b465392017-04-26 15:06:24 +0000255
Kelvin Li1408f912018-09-26 04:28:39 +0000256 /// Vector of previously declared requires directives
257 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls;
Alexey Bataev27ef9512019-03-20 20:14:22 +0000258 /// omp_allocator_handle_t type.
259 QualType OMPAllocatorHandleT;
260 /// Expression for the predefined allocators.
261 Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = {
262 nullptr};
Gheorghe-Teodor Bercea411a6242019-04-18 19:53:43 +0000263 /// Vector of previously encountered target directives
264 SmallVector<SourceLocation, 2> TargetLocations;
Kelvin Li1408f912018-09-26 04:28:39 +0000265
Alexey Bataev758e55e2013-09-06 18:03:48 +0000266public:
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000267 explicit DSAStackTy(Sema &S) : SemaRef(S) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000268
Alexey Bataev27ef9512019-03-20 20:14:22 +0000269 /// Sets omp_allocator_handle_t type.
270 void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; }
271 /// Gets omp_allocator_handle_t type.
272 QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; }
273 /// Sets the given default allocator.
274 void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
275 Expr *Allocator) {
276 OMPPredefinedAllocators[AllocatorKind] = Allocator;
277 }
278 /// Returns the specified default allocator.
279 Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const {
280 return OMPPredefinedAllocators[AllocatorKind];
281 }
282
Alexey Bataevaac108a2015-06-23 04:51:00 +0000283 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
Alexey Bataev3f82cfc2017-12-13 15:28:44 +0000284 OpenMPClauseKind getClauseParsingMode() const {
285 assert(isClauseParsingMode() && "Must be in clause parsing mode.");
286 return ClauseKindMode;
287 }
Alexey Bataevaac108a2015-06-23 04:51:00 +0000288 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000289
Richard Smith0621a8f2019-05-31 00:45:10 +0000290 bool isBodyComplete() const {
291 const SharingMapTy *Top = getTopOfStackOrNull();
292 return Top && Top->BodyComplete;
293 }
294 void setBodyComplete() {
295 getTopOfStack().BodyComplete = true;
296 }
297
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000298 bool isForceVarCapturing() const { return ForceCapturing; }
299 void setForceVarCapturing(bool V) { ForceCapturing = V; }
300
Alexey Bataev60705422018-10-30 15:50:12 +0000301 void setForceCaptureByReferenceInTargetExecutable(bool V) {
302 ForceCaptureByReferenceInTargetExecutable = V;
303 }
304 bool isForceCaptureByReferenceInTargetExecutable() const {
305 return ForceCaptureByReferenceInTargetExecutable;
306 }
307
Alexey Bataev758e55e2013-09-06 18:03:48 +0000308 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000309 Scope *CurScope, SourceLocation Loc) {
Richard Smith0621a8f2019-05-31 00:45:10 +0000310 assert(!IgnoredStackElements &&
311 "cannot change stack while ignoring elements");
Alexey Bataev4b465392017-04-26 15:06:24 +0000312 if (Stack.empty() ||
313 Stack.back().second != CurrentNonCapturingFunctionScope)
314 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope);
315 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc);
316 Stack.back().first.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000317 }
318
319 void pop() {
Richard Smith0621a8f2019-05-31 00:45:10 +0000320 assert(!IgnoredStackElements &&
321 "cannot change stack while ignoring elements");
Alexey Bataev4b465392017-04-26 15:06:24 +0000322 assert(!Stack.back().first.empty() &&
323 "Data-sharing attributes stack is empty!");
324 Stack.back().first.pop_back();
325 }
326
Richard Smith0621a8f2019-05-31 00:45:10 +0000327 /// RAII object to temporarily leave the scope of a directive when we want to
328 /// logically operate in its parent.
329 class ParentDirectiveScope {
330 DSAStackTy &Self;
331 bool Active;
332 public:
333 ParentDirectiveScope(DSAStackTy &Self, bool Activate)
334 : Self(Self), Active(false) {
335 if (Activate)
336 enable();
337 }
338 ~ParentDirectiveScope() { disable(); }
339 void disable() {
340 if (Active) {
341 --Self.IgnoredStackElements;
342 Active = false;
343 }
344 }
345 void enable() {
346 if (!Active) {
347 ++Self.IgnoredStackElements;
348 Active = true;
349 }
350 }
351 };
352
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000353 /// Marks that we're started loop parsing.
354 void loopInit() {
355 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
356 "Expected loop-based directive.");
Richard Smith375dec52019-05-30 23:21:14 +0000357 getTopOfStack().LoopStart = true;
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000358 }
359 /// Start capturing of the variables in the loop context.
360 void loopStart() {
361 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
362 "Expected loop-based directive.");
Richard Smith375dec52019-05-30 23:21:14 +0000363 getTopOfStack().LoopStart = false;
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000364 }
365 /// true, if variables are captured, false otherwise.
366 bool isLoopStarted() const {
367 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
368 "Expected loop-based directive.");
Richard Smith375dec52019-05-30 23:21:14 +0000369 return !getTopOfStack().LoopStart;
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000370 }
371 /// Marks (or clears) declaration as possibly loop counter.
372 void resetPossibleLoopCounter(const Decl *D = nullptr) {
Richard Smith375dec52019-05-30 23:21:14 +0000373 getTopOfStack().PossiblyLoopCounter =
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000374 D ? D->getCanonicalDecl() : D;
375 }
376 /// Gets the possible loop counter decl.
377 const Decl *getPossiblyLoopCunter() const {
Richard Smith375dec52019-05-30 23:21:14 +0000378 return getTopOfStack().PossiblyLoopCounter;
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000379 }
Alexey Bataev4b465392017-04-26 15:06:24 +0000380 /// Start new OpenMP region stack in new non-capturing function.
381 void pushFunction() {
Richard Smith0621a8f2019-05-31 00:45:10 +0000382 assert(!IgnoredStackElements &&
383 "cannot change stack while ignoring elements");
Alexey Bataev4b465392017-04-26 15:06:24 +0000384 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
385 assert(!isa<CapturingScopeInfo>(CurFnScope));
386 CurrentNonCapturingFunctionScope = CurFnScope;
387 }
388 /// Pop region stack for non-capturing function.
389 void popFunction(const FunctionScopeInfo *OldFSI) {
Richard Smith0621a8f2019-05-31 00:45:10 +0000390 assert(!IgnoredStackElements &&
391 "cannot change stack while ignoring elements");
Alexey Bataev4b465392017-04-26 15:06:24 +0000392 if (!Stack.empty() && Stack.back().second == OldFSI) {
393 assert(Stack.back().first.empty());
394 Stack.pop_back();
395 }
396 CurrentNonCapturingFunctionScope = nullptr;
397 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) {
398 if (!isa<CapturingScopeInfo>(FSI)) {
399 CurrentNonCapturingFunctionScope = FSI;
400 break;
401 }
402 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000403 }
404
Alexey Bataeve3727102018-04-18 15:57:46 +0000405 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) {
Alexey Bataev43a919f2018-04-13 17:48:43 +0000406 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint);
Alexey Bataev28c75412015-12-15 08:19:24 +0000407 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000408 const std::pair<const OMPCriticalDirective *, llvm::APSInt>
Alexey Bataev28c75412015-12-15 08:19:24 +0000409 getCriticalWithHint(const DeclarationNameInfo &Name) const {
410 auto I = Criticals.find(Name.getAsString());
411 if (I != Criticals.end())
412 return I->second;
413 return std::make_pair(nullptr, llvm::APSInt());
414 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000415 /// If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000416 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000417 /// for diagnostics.
Alexey Bataeve3727102018-04-18 15:57:46 +0000418 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000419
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000420 /// Register specified variable as loop control variable.
Alexey Bataeve3727102018-04-18 15:57:46 +0000421 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000422 /// Check if the specified variable is a loop control variable for
Alexey Bataev9c821032015-04-30 04:23:23 +0000423 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000424 /// \return The index of the loop control variable in the list of associated
425 /// for-loops (from outer to inner).
Alexey Bataeve3727102018-04-18 15:57:46 +0000426 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000427 /// Check if the specified variable is a loop control variable for
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000428 /// parent region.
429 /// \return The index of the loop control variable in the list of associated
430 /// for-loops (from outer to inner).
Alexey Bataeve3727102018-04-18 15:57:46 +0000431 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000432 /// Get the loop control variable for the I-th loop (or nullptr) in
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000433 /// parent directive.
Alexey Bataeve3727102018-04-18 15:57:46 +0000434 const ValueDecl *getParentLoopControlVariable(unsigned I) const;
Alexey Bataev9c821032015-04-30 04:23:23 +0000435
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000436 /// Adds explicit data sharing attribute to the specified declaration.
Alexey Bataeve3727102018-04-18 15:57:46 +0000437 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
Alexey Bataev90c228f2016-02-08 09:29:13 +0000438 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000439
Alexey Bataevfa312f32017-07-21 18:48:21 +0000440 /// Adds additional information for the reduction items with the reduction id
441 /// represented as an operator.
Alexey Bataeve3727102018-04-18 15:57:46 +0000442 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000443 BinaryOperatorKind BOK);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000444 /// Adds additional information for the reduction items with the reduction id
445 /// represented as reduction identifier.
Alexey Bataeve3727102018-04-18 15:57:46 +0000446 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000447 const Expr *ReductionRef);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000448 /// Returns the location and reduction operation from the innermost parent
449 /// region for the given \p D.
Alexey Bataeve3727102018-04-18 15:57:46 +0000450 const DSAVarData
451 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
452 BinaryOperatorKind &BOK,
453 Expr *&TaskgroupDescriptor) const;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000454 /// Returns the location and reduction operation from the innermost parent
455 /// region for the given \p D.
Alexey Bataeve3727102018-04-18 15:57:46 +0000456 const DSAVarData
457 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
458 const Expr *&ReductionRef,
459 Expr *&TaskgroupDescriptor) const;
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000460 /// Return reduction reference expression for the current taskgroup.
461 Expr *getTaskgroupReductionRef() const {
Richard Smith375dec52019-05-30 23:21:14 +0000462 assert(getTopOfStack().Directive == OMPD_taskgroup &&
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000463 "taskgroup reference expression requested for non taskgroup "
464 "directive.");
Richard Smith375dec52019-05-30 23:21:14 +0000465 return getTopOfStack().TaskgroupReductionRef;
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000466 }
Alexey Bataev88202be2017-07-27 13:20:36 +0000467 /// Checks if the given \p VD declaration is actually a taskgroup reduction
468 /// descriptor variable at the \p Level of OpenMP regions.
Alexey Bataeve3727102018-04-18 15:57:46 +0000469 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const {
Richard Smith375dec52019-05-30 23:21:14 +0000470 return getStackElemAtLevel(Level).TaskgroupReductionRef &&
471 cast<DeclRefExpr>(getStackElemAtLevel(Level).TaskgroupReductionRef)
Alexey Bataev88202be2017-07-27 13:20:36 +0000472 ->getDecl() == VD;
473 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000474
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000475 /// Returns data sharing attributes from top of the stack for the
Alexey Bataev758e55e2013-09-06 18:03:48 +0000476 /// specified declaration.
Alexey Bataeve3727102018-04-18 15:57:46 +0000477 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000478 /// Returns data-sharing attributes for the specified declaration.
Alexey Bataeve3727102018-04-18 15:57:46 +0000479 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000480 /// Checks if the specified variables has data-sharing attributes which
Alexey Bataevf29276e2014-06-18 04:14:57 +0000481 /// match specified \a CPred predicate in any directive which matches \a DPred
482 /// predicate.
Alexey Bataeve3727102018-04-18 15:57:46 +0000483 const DSAVarData
484 hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
485 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
486 bool FromParent) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000487 /// Checks if the specified variables has data-sharing attributes which
Alexey Bataevf29276e2014-06-18 04:14:57 +0000488 /// match specified \a CPred predicate in any innermost directive which
489 /// matches \a DPred predicate.
Alexey Bataeve3727102018-04-18 15:57:46 +0000490 const DSAVarData
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000491 hasInnermostDSA(ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000492 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
493 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000494 bool FromParent) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000495 /// Checks if the specified variables has explicit data-sharing
Alexey Bataevaac108a2015-06-23 04:51:00 +0000496 /// attributes which match specified \a CPred predicate at the specified
497 /// OpenMP region.
Alexey Bataeve3727102018-04-18 15:57:46 +0000498 bool hasExplicitDSA(const ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000499 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000500 unsigned Level, bool NotLastprivate = false) const;
Samuel Antao4be30e92015-10-02 17:14:03 +0000501
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000502 /// Returns true if the directive at level \Level matches in the
Samuel Antao4be30e92015-10-02 17:14:03 +0000503 /// specified \a DPred predicate.
504 bool hasExplicitDirective(
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000505 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000506 unsigned Level) const;
Samuel Antao4be30e92015-10-02 17:14:03 +0000507
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000508 /// Finds a directive which matches specified \a DPred predicate.
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000509 bool hasDirective(
510 const llvm::function_ref<bool(
511 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)>
512 DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000513 bool FromParent) const;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000514
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000515 /// Returns currently analyzed directive.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000516 OpenMPDirectiveKind getCurrentDirective() const {
Richard Smith375dec52019-05-30 23:21:14 +0000517 const SharingMapTy *Top = getTopOfStackOrNull();
518 return Top ? Top->Directive : OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000519 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000520 /// Returns directive kind at specified level.
Alexey Bataevdfa430f2017-12-08 15:03:50 +0000521 OpenMPDirectiveKind getDirective(unsigned Level) const {
522 assert(!isStackEmpty() && "No directive at specified level.");
Richard Smith375dec52019-05-30 23:21:14 +0000523 return getStackElemAtLevel(Level).Directive;
Alexey Bataevdfa430f2017-12-08 15:03:50 +0000524 }
Joel E. Denny7d5bc552019-08-22 03:34:30 +0000525 /// Returns the capture region at the specified level.
526 OpenMPDirectiveKind getCaptureRegion(unsigned Level,
527 unsigned OpenMPCaptureLevel) const {
528 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
529 getOpenMPCaptureRegions(CaptureRegions, getDirective(Level));
530 return CaptureRegions[OpenMPCaptureLevel];
531 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000532 /// Returns parent directive.
Alexey Bataev549210e2014-06-24 04:39:47 +0000533 OpenMPDirectiveKind getParentDirective() const {
Richard Smith375dec52019-05-30 23:21:14 +0000534 const SharingMapTy *Parent = getSecondOnStackOrNull();
535 return Parent ? Parent->Directive : OMPD_unknown;
Alexey Bataev549210e2014-06-24 04:39:47 +0000536 }
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000537
Kelvin Li1408f912018-09-26 04:28:39 +0000538 /// Add requires decl to internal vector
539 void addRequiresDecl(OMPRequiresDecl *RD) {
540 RequiresDecls.push_back(RD);
541 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000542
Alexey Bataev318f431b2019-03-22 15:25:12 +0000543 /// Checks if the defined 'requires' directive has specified type of clause.
544 template <typename ClauseType>
545 bool hasRequiresDeclWithClause() {
546 return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
547 return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
548 return isa<ClauseType>(C);
549 });
550 });
551 }
552
Kelvin Li1408f912018-09-26 04:28:39 +0000553 /// Checks for a duplicate clause amongst previously declared requires
554 /// directives
555 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const {
556 bool IsDuplicate = false;
557 for (OMPClause *CNew : ClauseList) {
558 for (const OMPRequiresDecl *D : RequiresDecls) {
559 for (const OMPClause *CPrev : D->clauselists()) {
560 if (CNew->getClauseKind() == CPrev->getClauseKind()) {
561 SemaRef.Diag(CNew->getBeginLoc(),
562 diag::err_omp_requires_clause_redeclaration)
563 << getOpenMPClauseName(CNew->getClauseKind());
564 SemaRef.Diag(CPrev->getBeginLoc(),
565 diag::note_omp_requires_previous_clause)
566 << getOpenMPClauseName(CPrev->getClauseKind());
567 IsDuplicate = true;
568 }
569 }
570 }
571 }
572 return IsDuplicate;
573 }
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +0000574
Gheorghe-Teodor Bercea411a6242019-04-18 19:53:43 +0000575 /// Add location of previously encountered target to internal vector
576 void addTargetDirLocation(SourceLocation LocStart) {
577 TargetLocations.push_back(LocStart);
578 }
579
580 // Return previously encountered target region locations.
581 ArrayRef<SourceLocation> getEncounteredTargetLocs() const {
582 return TargetLocations;
583 }
584
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000585 /// Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000586 void setDefaultDSANone(SourceLocation Loc) {
Richard Smith375dec52019-05-30 23:21:14 +0000587 getTopOfStack().DefaultAttr = DSA_none;
588 getTopOfStack().DefaultAttrLoc = Loc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000589 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000590 /// Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000591 void setDefaultDSAShared(SourceLocation Loc) {
Richard Smith375dec52019-05-30 23:21:14 +0000592 getTopOfStack().DefaultAttr = DSA_shared;
593 getTopOfStack().DefaultAttrLoc = Loc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000594 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000595 /// Set default data mapping attribute to 'tofrom:scalar'.
596 void setDefaultDMAToFromScalar(SourceLocation Loc) {
Richard Smith375dec52019-05-30 23:21:14 +0000597 getTopOfStack().DefaultMapAttr = DMA_tofrom_scalar;
598 getTopOfStack().DefaultMapAttrLoc = Loc;
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000599 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000600
601 DefaultDataSharingAttributes getDefaultDSA() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000602 return isStackEmpty() ? DSA_unspecified
Richard Smith375dec52019-05-30 23:21:14 +0000603 : getTopOfStack().DefaultAttr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000604 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000605 SourceLocation getDefaultDSALocation() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000606 return isStackEmpty() ? SourceLocation()
Richard Smith375dec52019-05-30 23:21:14 +0000607 : getTopOfStack().DefaultAttrLoc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000608 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000609 DefaultMapAttributes getDefaultDMA() const {
610 return isStackEmpty() ? DMA_unspecified
Richard Smith375dec52019-05-30 23:21:14 +0000611 : getTopOfStack().DefaultMapAttr;
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000612 }
613 DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const {
Richard Smith375dec52019-05-30 23:21:14 +0000614 return getStackElemAtLevel(Level).DefaultMapAttr;
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000615 }
616 SourceLocation getDefaultDMALocation() const {
617 return isStackEmpty() ? SourceLocation()
Richard Smith375dec52019-05-30 23:21:14 +0000618 : getTopOfStack().DefaultMapAttrLoc;
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000619 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000620
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000621 /// Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000622 bool isThreadPrivate(VarDecl *D) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000623 const DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000624 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000625 }
626
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000627 /// Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataevf138fda2018-08-13 19:04:24 +0000628 void setOrderedRegion(bool IsOrdered, const Expr *Param,
629 OMPOrderedClause *Clause) {
Alexey Bataevf138fda2018-08-13 19:04:24 +0000630 if (IsOrdered)
Richard Smith375dec52019-05-30 23:21:14 +0000631 getTopOfStack().OrderedRegion.emplace(Param, Clause);
Alexey Bataevf138fda2018-08-13 19:04:24 +0000632 else
Richard Smith375dec52019-05-30 23:21:14 +0000633 getTopOfStack().OrderedRegion.reset();
Alexey Bataevf138fda2018-08-13 19:04:24 +0000634 }
635 /// Returns true, if region is ordered (has associated 'ordered' clause),
636 /// false - otherwise.
637 bool isOrderedRegion() const {
Richard Smith375dec52019-05-30 23:21:14 +0000638 if (const SharingMapTy *Top = getTopOfStackOrNull())
639 return Top->OrderedRegion.hasValue();
640 return false;
Alexey Bataevf138fda2018-08-13 19:04:24 +0000641 }
642 /// Returns optional parameter for the ordered region.
643 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const {
Richard Smith375dec52019-05-30 23:21:14 +0000644 if (const SharingMapTy *Top = getTopOfStackOrNull())
645 if (Top->OrderedRegion.hasValue())
646 return Top->OrderedRegion.getValue();
647 return std::make_pair(nullptr, nullptr);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000648 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000649 /// Returns true, if parent region is ordered (has associated
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000650 /// 'ordered' clause), false - otherwise.
651 bool isParentOrderedRegion() const {
Richard Smith375dec52019-05-30 23:21:14 +0000652 if (const SharingMapTy *Parent = getSecondOnStackOrNull())
653 return Parent->OrderedRegion.hasValue();
654 return false;
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000655 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000656 /// Returns optional parameter for the ordered region.
Alexey Bataevf138fda2018-08-13 19:04:24 +0000657 std::pair<const Expr *, OMPOrderedClause *>
658 getParentOrderedRegionParam() const {
Richard Smith375dec52019-05-30 23:21:14 +0000659 if (const SharingMapTy *Parent = getSecondOnStackOrNull())
660 if (Parent->OrderedRegion.hasValue())
661 return Parent->OrderedRegion.getValue();
662 return std::make_pair(nullptr, nullptr);
Alexey Bataev346265e2015-09-25 10:37:12 +0000663 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000664 /// Marks current region as nowait (it has a 'nowait' clause).
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000665 void setNowaitRegion(bool IsNowait = true) {
Richard Smith375dec52019-05-30 23:21:14 +0000666 getTopOfStack().NowaitRegion = IsNowait;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000667 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000668 /// Returns true, if parent region is nowait (has associated
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000669 /// 'nowait' clause), false - otherwise.
670 bool isParentNowaitRegion() const {
Richard Smith375dec52019-05-30 23:21:14 +0000671 if (const SharingMapTy *Parent = getSecondOnStackOrNull())
672 return Parent->NowaitRegion;
673 return false;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000674 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000675 /// Marks parent region as cancel region.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000676 void setParentCancelRegion(bool Cancel = true) {
Richard Smith375dec52019-05-30 23:21:14 +0000677 if (SharingMapTy *Parent = getSecondOnStackOrNull())
678 Parent->CancelRegion |= Cancel;
Alexey Bataev25e5b442015-09-15 12:52:43 +0000679 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000680 /// Return true if current region has inner cancel construct.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000681 bool isCancelRegion() const {
Richard Smith375dec52019-05-30 23:21:14 +0000682 const SharingMapTy *Top = getTopOfStackOrNull();
683 return Top ? Top->CancelRegion : false;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000684 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000685
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000686 /// Set collapse value for the region.
Alexey Bataev4b465392017-04-26 15:06:24 +0000687 void setAssociatedLoops(unsigned Val) {
Richard Smith375dec52019-05-30 23:21:14 +0000688 getTopOfStack().AssociatedLoops = Val;
Alexey Bataev05be1da2019-07-18 17:49:13 +0000689 if (Val > 1)
690 getTopOfStack().HasMutipleLoops = true;
Alexey Bataev4b465392017-04-26 15:06:24 +0000691 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000692 /// Return collapse value for region.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000693 unsigned getAssociatedLoops() const {
Richard Smith375dec52019-05-30 23:21:14 +0000694 const SharingMapTy *Top = getTopOfStackOrNull();
695 return Top ? Top->AssociatedLoops : 0;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000696 }
Alexey Bataev05be1da2019-07-18 17:49:13 +0000697 /// Returns true if the construct is associated with multiple loops.
698 bool hasMutipleLoops() const {
699 const SharingMapTy *Top = getTopOfStackOrNull();
700 return Top ? Top->HasMutipleLoops : false;
701 }
Alexey Bataev9c821032015-04-30 04:23:23 +0000702
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000703 /// Marks current target region as one with closely nested teams
Alexey Bataev13314bf2014-10-09 04:18:56 +0000704 /// region.
705 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
Richard Smith375dec52019-05-30 23:21:14 +0000706 if (SharingMapTy *Parent = getSecondOnStackOrNull())
707 Parent->InnerTeamsRegionLoc = TeamsRegionLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000708 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000709 /// Returns true, if current region has closely nested teams region.
Alexey Bataev13314bf2014-10-09 04:18:56 +0000710 bool hasInnerTeamsRegion() const {
711 return getInnerTeamsRegionLoc().isValid();
712 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000713 /// Returns location of the nested teams region (if any).
Alexey Bataev13314bf2014-10-09 04:18:56 +0000714 SourceLocation getInnerTeamsRegionLoc() const {
Richard Smith375dec52019-05-30 23:21:14 +0000715 const SharingMapTy *Top = getTopOfStackOrNull();
716 return Top ? Top->InnerTeamsRegionLoc : SourceLocation();
Alexey Bataev13314bf2014-10-09 04:18:56 +0000717 }
718
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000719 Scope *getCurScope() const {
Richard Smith375dec52019-05-30 23:21:14 +0000720 const SharingMapTy *Top = getTopOfStackOrNull();
721 return Top ? Top->CurScope : nullptr;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000722 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000723 SourceLocation getConstructLoc() const {
Richard Smith375dec52019-05-30 23:21:14 +0000724 const SharingMapTy *Top = getTopOfStackOrNull();
725 return Top ? Top->ConstructLoc : SourceLocation();
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000726 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000727
Samuel Antao4c8035b2016-12-12 18:00:20 +0000728 /// Do the check specified in \a Check to all component lists and return true
729 /// if any issue is found.
Samuel Antao90927002016-04-26 14:54:23 +0000730 bool checkMappableExprComponentListsForDecl(
Alexey Bataeve3727102018-04-18 15:57:46 +0000731 const ValueDecl *VD, bool CurrentRegionOnly,
Samuel Antao6890b092016-07-28 14:25:09 +0000732 const llvm::function_ref<
733 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000734 OpenMPClauseKind)>
Alexey Bataeve3727102018-04-18 15:57:46 +0000735 Check) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000736 if (isStackEmpty())
737 return false;
Richard Smith375dec52019-05-30 23:21:14 +0000738 auto SI = begin();
739 auto SE = end();
Samuel Antao5de996e2016-01-22 20:21:36 +0000740
741 if (SI == SE)
742 return false;
743
Alexey Bataeve3727102018-04-18 15:57:46 +0000744 if (CurrentRegionOnly)
Samuel Antao5de996e2016-01-22 20:21:36 +0000745 SE = std::next(SI);
Alexey Bataeve3727102018-04-18 15:57:46 +0000746 else
747 std::advance(SI, 1);
Samuel Antao5de996e2016-01-22 20:21:36 +0000748
749 for (; SI != SE; ++SI) {
Samuel Antao90927002016-04-26 14:54:23 +0000750 auto MI = SI->MappedExprComponents.find(VD);
751 if (MI != SI->MappedExprComponents.end())
Alexey Bataeve3727102018-04-18 15:57:46 +0000752 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
753 MI->second.Components)
Samuel Antao6890b092016-07-28 14:25:09 +0000754 if (Check(L, MI->second.Kind))
Samuel Antao5de996e2016-01-22 20:21:36 +0000755 return true;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000756 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000757 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000758 }
759
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000760 /// Do the check specified in \a Check to all component lists at a given level
761 /// and return true if any issue is found.
762 bool checkMappableExprComponentListsForDeclAtLevel(
Alexey Bataeve3727102018-04-18 15:57:46 +0000763 const ValueDecl *VD, unsigned Level,
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000764 const llvm::function_ref<
765 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000766 OpenMPClauseKind)>
Alexey Bataeve3727102018-04-18 15:57:46 +0000767 Check) const {
Richard Smith375dec52019-05-30 23:21:14 +0000768 if (getStackSize() <= Level)
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000769 return false;
770
Richard Smith375dec52019-05-30 23:21:14 +0000771 const SharingMapTy &StackElem = getStackElemAtLevel(Level);
772 auto MI = StackElem.MappedExprComponents.find(VD);
773 if (MI != StackElem.MappedExprComponents.end())
Alexey Bataeve3727102018-04-18 15:57:46 +0000774 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
775 MI->second.Components)
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000776 if (Check(L, MI->second.Kind))
777 return true;
778 return false;
779 }
780
Samuel Antao4c8035b2016-12-12 18:00:20 +0000781 /// Create a new mappable expression component list associated with a given
782 /// declaration and initialize it with the provided list of components.
Samuel Antao90927002016-04-26 14:54:23 +0000783 void addMappableExpressionComponents(
Alexey Bataeve3727102018-04-18 15:57:46 +0000784 const ValueDecl *VD,
Samuel Antao6890b092016-07-28 14:25:09 +0000785 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
786 OpenMPClauseKind WhereFoundClauseKind) {
Richard Smith375dec52019-05-30 23:21:14 +0000787 MappedExprComponentTy &MEC = getTopOfStack().MappedExprComponents[VD];
Samuel Antao90927002016-04-26 14:54:23 +0000788 // Create new entry and append the new components there.
Samuel Antao6890b092016-07-28 14:25:09 +0000789 MEC.Components.resize(MEC.Components.size() + 1);
790 MEC.Components.back().append(Components.begin(), Components.end());
791 MEC.Kind = WhereFoundClauseKind;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000792 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000793
794 unsigned getNestingLevel() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000795 assert(!isStackEmpty());
Richard Smith375dec52019-05-30 23:21:14 +0000796 return getStackSize() - 1;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000797 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000798 void addDoacrossDependClause(OMPDependClause *C,
799 const OperatorOffsetTy &OpsOffs) {
Richard Smith375dec52019-05-30 23:21:14 +0000800 SharingMapTy *Parent = getSecondOnStackOrNull();
801 assert(Parent && isOpenMPWorksharingDirective(Parent->Directive));
802 Parent->DoacrossDepends.try_emplace(C, OpsOffs);
Alexey Bataev8b427062016-05-25 12:36:08 +0000803 }
804 llvm::iterator_range<DoacrossDependMapTy::const_iterator>
805 getDoacrossDependClauses() const {
Richard Smith375dec52019-05-30 23:21:14 +0000806 const SharingMapTy &StackElem = getTopOfStack();
Alexey Bataev4b465392017-04-26 15:06:24 +0000807 if (isOpenMPWorksharingDirective(StackElem.Directive)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000808 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends;
Alexey Bataev8b427062016-05-25 12:36:08 +0000809 return llvm::make_range(Ref.begin(), Ref.end());
810 }
Alexey Bataev4b465392017-04-26 15:06:24 +0000811 return llvm::make_range(StackElem.DoacrossDepends.end(),
812 StackElem.DoacrossDepends.end());
Alexey Bataev8b427062016-05-25 12:36:08 +0000813 }
Patrick Lystere13b1e32019-01-02 19:28:48 +0000814
815 // Store types of classes which have been explicitly mapped
816 void addMappedClassesQualTypes(QualType QT) {
Richard Smith375dec52019-05-30 23:21:14 +0000817 SharingMapTy &StackElem = getTopOfStack();
Patrick Lystere13b1e32019-01-02 19:28:48 +0000818 StackElem.MappedClassesQualTypes.insert(QT);
819 }
820
821 // Return set of mapped classes types
822 bool isClassPreviouslyMapped(QualType QT) const {
Richard Smith375dec52019-05-30 23:21:14 +0000823 const SharingMapTy &StackElem = getTopOfStack();
Patrick Lystere13b1e32019-01-02 19:28:48 +0000824 return StackElem.MappedClassesQualTypes.count(QT) != 0;
825 }
826
Alexey Bataeva495c642019-03-11 19:51:42 +0000827 /// Adds global declare target to the parent target region.
828 void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) {
829 assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
830 E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link &&
831 "Expected declare target link global.");
Richard Smith375dec52019-05-30 23:21:14 +0000832 for (auto &Elem : *this) {
833 if (isOpenMPTargetExecutionDirective(Elem.Directive)) {
834 Elem.DeclareTargetLinkVarDecls.push_back(E);
835 return;
836 }
Alexey Bataeva495c642019-03-11 19:51:42 +0000837 }
838 }
839
840 /// Returns the list of globals with declare target link if current directive
841 /// is target.
842 ArrayRef<DeclRefExpr *> getLinkGlobals() const {
843 assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) &&
844 "Expected target executable directive.");
Richard Smith375dec52019-05-30 23:21:14 +0000845 return getTopOfStack().DeclareTargetLinkVarDecls;
Alexey Bataeva495c642019-03-11 19:51:42 +0000846 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000847};
Alexey Bataev7e6803e2019-01-09 15:58:05 +0000848
849bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) {
850 return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind);
851}
852
853bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) {
Alexey Bataev412254a2019-05-09 18:44:53 +0000854 return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) ||
855 DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000856}
Alexey Bataeve3727102018-04-18 15:57:46 +0000857
Alexey Bataeved09d242014-05-28 05:53:51 +0000858} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000859
Alexey Bataeve3727102018-04-18 15:57:46 +0000860static const Expr *getExprAsWritten(const Expr *E) {
Bill Wendling7c44da22018-10-31 03:48:47 +0000861 if (const auto *FE = dyn_cast<FullExpr>(E))
862 E = FE->getSubExpr();
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000863
Alexey Bataeve3727102018-04-18 15:57:46 +0000864 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000865 E = MTE->GetTemporaryExpr();
866
Alexey Bataeve3727102018-04-18 15:57:46 +0000867 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000868 E = Binder->getSubExpr();
869
Alexey Bataeve3727102018-04-18 15:57:46 +0000870 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000871 E = ICE->getSubExprAsWritten();
872 return E->IgnoreParens();
873}
874
Alexey Bataeve3727102018-04-18 15:57:46 +0000875static Expr *getExprAsWritten(Expr *E) {
876 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E)));
877}
878
879static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
880 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
881 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000882 D = ME->getMemberDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +0000883 const auto *VD = dyn_cast<VarDecl>(D);
884 const auto *FD = dyn_cast<FieldDecl>(D);
David Majnemer9d168222016-08-05 17:44:54 +0000885 if (VD != nullptr) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000886 VD = VD->getCanonicalDecl();
887 D = VD;
888 } else {
889 assert(FD);
890 FD = FD->getCanonicalDecl();
891 D = FD;
892 }
893 return D;
894}
895
Alexey Bataeve3727102018-04-18 15:57:46 +0000896static ValueDecl *getCanonicalDecl(ValueDecl *D) {
897 return const_cast<ValueDecl *>(
898 getCanonicalDecl(const_cast<const ValueDecl *>(D)));
899}
900
Richard Smith375dec52019-05-30 23:21:14 +0000901DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter,
Alexey Bataeve3727102018-04-18 15:57:46 +0000902 ValueDecl *D) const {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000903 D = getCanonicalDecl(D);
904 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000905 const auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000906 DSAVarData DVar;
Richard Smith375dec52019-05-30 23:21:14 +0000907 if (Iter == end()) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000908 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
909 // in a region but not in construct]
910 // File-scope or namespace-scope variables referenced in called routines
911 // in the region are shared unless they appear in a threadprivate
912 // directive.
Alexey Bataeve3727102018-04-18 15:57:46 +0000913 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000914 DVar.CKind = OMPC_shared;
915
916 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
917 // in a region but not in construct]
918 // Variables with static storage duration that are declared in called
919 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000920 if (VD && VD->hasGlobalStorage())
921 DVar.CKind = OMPC_shared;
922
923 // Non-static data members are shared by default.
924 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000925 DVar.CKind = OMPC_shared;
926
Alexey Bataev758e55e2013-09-06 18:03:48 +0000927 return DVar;
928 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000929
Alexey Bataevec3da872014-01-31 05:15:34 +0000930 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
931 // in a Construct, C/C++, predetermined, p.1]
932 // Variables with automatic storage duration that are declared in a scope
933 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000934 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
935 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000936 DVar.CKind = OMPC_private;
937 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000938 }
939
Alexey Bataeveffbdf12017-07-21 17:24:30 +0000940 DVar.DKind = Iter->Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000941 // Explicitly specified attributes and local variables with predetermined
942 // attributes.
943 if (Iter->SharingMap.count(D)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000944 const DSAInfo &Data = Iter->SharingMap.lookup(D);
945 DVar.RefExpr = Data.RefExpr.getPointer();
946 DVar.PrivateCopy = Data.PrivateCopy;
947 DVar.CKind = Data.Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000948 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000949 return DVar;
950 }
951
952 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
953 // in a Construct, C/C++, implicitly determined, p.1]
954 // In a parallel or task construct, the data-sharing attributes of these
955 // variables are determined by the default clause, if present.
956 switch (Iter->DefaultAttr) {
957 case DSA_shared:
958 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000959 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000960 return DVar;
961 case DSA_none:
962 return DVar;
963 case DSA_unspecified:
964 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
965 // in a Construct, implicitly determined, p.2]
966 // In a parallel construct, if no default clause is present, these
967 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000968 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev5bbcead2019-10-14 17:17:41 +0000969 if ((isOpenMPParallelDirective(DVar.DKind) &&
970 !isOpenMPTaskLoopDirective(DVar.DKind)) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +0000971 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000972 DVar.CKind = OMPC_shared;
973 return DVar;
974 }
975
976 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
977 // in a Construct, implicitly determined, p.4]
978 // In a task construct, if no default clause is present, a variable that in
979 // the enclosing context is determined to be shared by all implicit tasks
980 // bound to the current team is shared.
Alexey Bataev35aaee62016-04-13 13:36:48 +0000981 if (isOpenMPTaskingDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000982 DSAVarData DVarTemp;
Richard Smith375dec52019-05-30 23:21:14 +0000983 const_iterator I = Iter, E = end();
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000984 do {
985 ++I;
Alexey Bataeved09d242014-05-28 05:53:51 +0000986 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
Alexey Bataev35aaee62016-04-13 13:36:48 +0000987 // Referenced in a Construct, implicitly determined, p.6]
Alexey Bataev758e55e2013-09-06 18:03:48 +0000988 // In a task construct, if no default clause is present, a variable
989 // whose data-sharing attribute is not determined by the rules above is
990 // firstprivate.
991 DVarTemp = getDSA(I, D);
992 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000993 DVar.RefExpr = nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000994 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000995 return DVar;
996 }
Alexey Bataev7e6803e2019-01-09 15:58:05 +0000997 } while (I != E && !isImplicitTaskingRegion(I->Directive));
Alexey Bataev758e55e2013-09-06 18:03:48 +0000998 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000999 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001000 return DVar;
1001 }
1002 }
1003 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1004 // in a Construct, implicitly determined, p.3]
1005 // For constructs other than task, if no default clause is present, these
1006 // variables inherit their data-sharing attributes from the enclosing
1007 // context.
Dmitry Polukhindc78bc822016-04-01 09:52:30 +00001008 return getDSA(++Iter, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001009}
1010
Alexey Bataeve3727102018-04-18 15:57:46 +00001011const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
1012 const Expr *NewDE) {
Alexey Bataev4b465392017-04-26 15:06:24 +00001013 assert(!isStackEmpty() && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001014 D = getCanonicalDecl(D);
Richard Smith375dec52019-05-30 23:21:14 +00001015 SharingMapTy &StackElem = getTopOfStack();
Alexey Bataev4b465392017-04-26 15:06:24 +00001016 auto It = StackElem.AlignedMap.find(D);
1017 if (It == StackElem.AlignedMap.end()) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001018 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
Alexey Bataev4b465392017-04-26 15:06:24 +00001019 StackElem.AlignedMap[D] = NewDE;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001020 return nullptr;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001021 }
Alexey Bataeve3727102018-04-18 15:57:46 +00001022 assert(It->second && "Unexpected nullptr expr in the aligned map");
1023 return It->second;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001024}
1025
Alexey Bataeve3727102018-04-18 15:57:46 +00001026void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
Alexey Bataev4b465392017-04-26 15:06:24 +00001027 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001028 D = getCanonicalDecl(D);
Richard Smith375dec52019-05-30 23:21:14 +00001029 SharingMapTy &StackElem = getTopOfStack();
Alexey Bataeve3727102018-04-18 15:57:46 +00001030 StackElem.LCVMap.try_emplace(
1031 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
Alexey Bataev9c821032015-04-30 04:23:23 +00001032}
1033
Alexey Bataeve3727102018-04-18 15:57:46 +00001034const DSAStackTy::LCDeclInfo
1035DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001036 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001037 D = getCanonicalDecl(D);
Richard Smith375dec52019-05-30 23:21:14 +00001038 const SharingMapTy &StackElem = getTopOfStack();
Alexey Bataev4b465392017-04-26 15:06:24 +00001039 auto It = StackElem.LCVMap.find(D);
1040 if (It != StackElem.LCVMap.end())
1041 return It->second;
1042 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +00001043}
1044
Alexey Bataeve3727102018-04-18 15:57:46 +00001045const DSAStackTy::LCDeclInfo
1046DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
Richard Smith375dec52019-05-30 23:21:14 +00001047 const SharingMapTy *Parent = getSecondOnStackOrNull();
1048 assert(Parent && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001049 D = getCanonicalDecl(D);
Richard Smith375dec52019-05-30 23:21:14 +00001050 auto It = Parent->LCVMap.find(D);
1051 if (It != Parent->LCVMap.end())
Alexey Bataev4b465392017-04-26 15:06:24 +00001052 return It->second;
1053 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +00001054}
1055
Alexey Bataeve3727102018-04-18 15:57:46 +00001056const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
Richard Smith375dec52019-05-30 23:21:14 +00001057 const SharingMapTy *Parent = getSecondOnStackOrNull();
1058 assert(Parent && "Data-sharing attributes stack is empty");
1059 if (Parent->LCVMap.size() < I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00001060 return nullptr;
Richard Smith375dec52019-05-30 23:21:14 +00001061 for (const auto &Pair : Parent->LCVMap)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001062 if (Pair.second.first == I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00001063 return Pair.first;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00001064 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +00001065}
1066
Alexey Bataeve3727102018-04-18 15:57:46 +00001067void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
Alexey Bataev90c228f2016-02-08 09:29:13 +00001068 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001069 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001070 if (A == OMPC_threadprivate) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001071 DSAInfo &Data = Threadprivates[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001072 Data.Attributes = A;
1073 Data.RefExpr.setPointer(E);
1074 Data.PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001075 } else {
Richard Smith375dec52019-05-30 23:21:14 +00001076 DSAInfo &Data = getTopOfStack().SharingMap[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001077 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
1078 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
1079 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
1080 (isLoopControlVariable(D).first && A == OMPC_private));
1081 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
1082 Data.RefExpr.setInt(/*IntVal=*/true);
1083 return;
1084 }
1085 const bool IsLastprivate =
1086 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
1087 Data.Attributes = A;
1088 Data.RefExpr.setPointerAndInt(E, IsLastprivate);
1089 Data.PrivateCopy = PrivateCopy;
1090 if (PrivateCopy) {
Richard Smith375dec52019-05-30 23:21:14 +00001091 DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()];
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001092 Data.Attributes = A;
1093 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
1094 Data.PrivateCopy = nullptr;
1095 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001096 }
1097}
1098
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001099/// Build a variable declaration for OpenMP loop iteration variable.
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001100static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001101 StringRef Name, const AttrVec *Attrs = nullptr,
1102 DeclRefExpr *OrigRef = nullptr) {
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001103 DeclContext *DC = SemaRef.CurContext;
1104 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
1105 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
Alexey Bataeve3727102018-04-18 15:57:46 +00001106 auto *Decl =
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001107 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
1108 if (Attrs) {
1109 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
1110 I != E; ++I)
1111 Decl->addAttr(*I);
1112 }
1113 Decl->setImplicit();
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001114 if (OrigRef) {
1115 Decl->addAttr(
1116 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
1117 }
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001118 return Decl;
1119}
1120
1121static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
1122 SourceLocation Loc,
1123 bool RefersToCapture = false) {
1124 D->setReferenced();
1125 D->markUsed(S.Context);
1126 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
1127 SourceLocation(), D, RefersToCapture, Loc, Ty,
1128 VK_LValue);
1129}
1130
Alexey Bataeve3727102018-04-18 15:57:46 +00001131void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001132 BinaryOperatorKind BOK) {
Alexey Bataevfa312f32017-07-21 18:48:21 +00001133 D = getCanonicalDecl(D);
1134 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +00001135 assert(
Richard Smith375dec52019-05-30 23:21:14 +00001136 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +00001137 "Additional reduction info may be specified only for reduction items.");
Richard Smith375dec52019-05-30 23:21:14 +00001138 ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
Alexey Bataevfa312f32017-07-21 18:48:21 +00001139 assert(ReductionData.ReductionRange.isInvalid() &&
Richard Smith375dec52019-05-30 23:21:14 +00001140 getTopOfStack().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +00001141 "Additional reduction info may be specified only once for reduction "
1142 "items.");
1143 ReductionData.set(BOK, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001144 Expr *&TaskgroupReductionRef =
Richard Smith375dec52019-05-30 23:21:14 +00001145 getTopOfStack().TaskgroupReductionRef;
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001146 if (!TaskgroupReductionRef) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001147 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1148 SemaRef.Context.VoidPtrTy, ".task_red.");
Alexey Bataevd070a582017-10-25 15:54:04 +00001149 TaskgroupReductionRef =
1150 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001151 }
Alexey Bataevfa312f32017-07-21 18:48:21 +00001152}
1153
Alexey Bataeve3727102018-04-18 15:57:46 +00001154void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001155 const Expr *ReductionRef) {
Alexey Bataevfa312f32017-07-21 18:48:21 +00001156 D = getCanonicalDecl(D);
1157 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +00001158 assert(
Richard Smith375dec52019-05-30 23:21:14 +00001159 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +00001160 "Additional reduction info may be specified only for reduction items.");
Richard Smith375dec52019-05-30 23:21:14 +00001161 ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
Alexey Bataevfa312f32017-07-21 18:48:21 +00001162 assert(ReductionData.ReductionRange.isInvalid() &&
Richard Smith375dec52019-05-30 23:21:14 +00001163 getTopOfStack().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +00001164 "Additional reduction info may be specified only once for reduction "
1165 "items.");
1166 ReductionData.set(ReductionRef, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001167 Expr *&TaskgroupReductionRef =
Richard Smith375dec52019-05-30 23:21:14 +00001168 getTopOfStack().TaskgroupReductionRef;
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001169 if (!TaskgroupReductionRef) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001170 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1171 SemaRef.Context.VoidPtrTy, ".task_red.");
Alexey Bataevd070a582017-10-25 15:54:04 +00001172 TaskgroupReductionRef =
1173 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001174 }
Alexey Bataevfa312f32017-07-21 18:48:21 +00001175}
1176
Alexey Bataeve3727102018-04-18 15:57:46 +00001177const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1178 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
1179 Expr *&TaskgroupDescriptor) const {
Alexey Bataevfa312f32017-07-21 18:48:21 +00001180 D = getCanonicalDecl(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001181 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
Richard Smith375dec52019-05-30 23:21:14 +00001182 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001183 const DSAInfo &Data = I->SharingMap.lookup(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001184 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
Alexey Bataevfa312f32017-07-21 18:48:21 +00001185 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001186 const ReductionData &ReductionData = I->ReductionMap.lookup(D);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001187 if (!ReductionData.ReductionOp ||
1188 ReductionData.ReductionOp.is<const Expr *>())
Alexey Bataevf189cb72017-07-24 14:52:13 +00001189 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001190 SR = ReductionData.ReductionRange;
Alexey Bataevf87fa882017-07-21 19:26:22 +00001191 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
Alexey Bataev88202be2017-07-27 13:20:36 +00001192 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1193 "expression for the descriptor is not "
1194 "set.");
1195 TaskgroupDescriptor = I->TaskgroupReductionRef;
Alexey Bataevf189cb72017-07-24 14:52:13 +00001196 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1197 Data.PrivateCopy, I->DefaultAttrLoc);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001198 }
Alexey Bataevf189cb72017-07-24 14:52:13 +00001199 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001200}
1201
Alexey Bataeve3727102018-04-18 15:57:46 +00001202const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1203 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
1204 Expr *&TaskgroupDescriptor) const {
Alexey Bataevfa312f32017-07-21 18:48:21 +00001205 D = getCanonicalDecl(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001206 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
Richard Smith375dec52019-05-30 23:21:14 +00001207 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001208 const DSAInfo &Data = I->SharingMap.lookup(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001209 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
Alexey Bataevfa312f32017-07-21 18:48:21 +00001210 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001211 const ReductionData &ReductionData = I->ReductionMap.lookup(D);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001212 if (!ReductionData.ReductionOp ||
1213 !ReductionData.ReductionOp.is<const Expr *>())
Alexey Bataevf189cb72017-07-24 14:52:13 +00001214 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001215 SR = ReductionData.ReductionRange;
1216 ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
Alexey Bataev88202be2017-07-27 13:20:36 +00001217 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1218 "expression for the descriptor is not "
1219 "set.");
1220 TaskgroupDescriptor = I->TaskgroupReductionRef;
Alexey Bataevf189cb72017-07-24 14:52:13 +00001221 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1222 Data.PrivateCopy, I->DefaultAttrLoc);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001223 }
Alexey Bataevf189cb72017-07-24 14:52:13 +00001224 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001225}
1226
Richard Smith375dec52019-05-30 23:21:14 +00001227bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001228 D = D->getCanonicalDecl();
Richard Smith375dec52019-05-30 23:21:14 +00001229 for (const_iterator E = end(); I != E; ++I) {
1230 if (isImplicitOrExplicitTaskingRegion(I->Directive) ||
1231 isOpenMPTargetExecutionDirective(I->Directive)) {
1232 Scope *TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
1233 Scope *CurScope = getCurScope();
1234 while (CurScope && CurScope != TopScope && !CurScope->isDeclScope(D))
1235 CurScope = CurScope->getParent();
1236 return CurScope != TopScope;
1237 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001238 }
Alexey Bataevec3da872014-01-31 05:15:34 +00001239 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001240}
1241
Joel E. Dennyd2649292019-01-04 22:11:56 +00001242static bool isConstNotMutableType(Sema &SemaRef, QualType Type,
1243 bool AcceptIfMutable = true,
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001244 bool *IsClassType = nullptr) {
1245 ASTContext &Context = SemaRef.getASTContext();
Joel E. Dennyd2649292019-01-04 22:11:56 +00001246 Type = Type.getNonReferenceType().getCanonicalType();
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001247 bool IsConstant = Type.isConstant(Context);
1248 Type = Context.getBaseElementType(Type);
Joel E. Dennyd2649292019-01-04 22:11:56 +00001249 const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus
1250 ? Type->getAsCXXRecordDecl()
1251 : nullptr;
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001252 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1253 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
1254 RD = CTD->getTemplatedDecl();
1255 if (IsClassType)
1256 *IsClassType = RD;
1257 return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD &&
1258 RD->hasDefinition() && RD->hasMutableFields());
1259}
1260
Joel E. Dennyd2649292019-01-04 22:11:56 +00001261static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D,
1262 QualType Type, OpenMPClauseKind CKind,
1263 SourceLocation ELoc,
1264 bool AcceptIfMutable = true,
1265 bool ListItemNotVar = false) {
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001266 ASTContext &Context = SemaRef.getASTContext();
1267 bool IsClassType;
Joel E. Dennyd2649292019-01-04 22:11:56 +00001268 if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) {
1269 unsigned Diag = ListItemNotVar
1270 ? diag::err_omp_const_list_item
1271 : IsClassType ? diag::err_omp_const_not_mutable_variable
1272 : diag::err_omp_const_variable;
1273 SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind);
1274 if (!ListItemNotVar && D) {
1275 const VarDecl *VD = dyn_cast<VarDecl>(D);
1276 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
1277 VarDecl::DeclarationOnly;
1278 SemaRef.Diag(D->getLocation(),
1279 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1280 << D;
1281 }
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001282 return true;
1283 }
1284 return false;
1285}
1286
Alexey Bataeve3727102018-04-18 15:57:46 +00001287const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1288 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001289 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001290 DSAVarData DVar;
1291
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001292 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001293 auto TI = Threadprivates.find(D);
1294 if (TI != Threadprivates.end()) {
1295 DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001296 DVar.CKind = OMPC_threadprivate;
1297 return DVar;
Alexey Bataeve3727102018-04-18 15:57:46 +00001298 }
1299 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
Alexey Bataev817d7f32017-11-14 21:01:01 +00001300 DVar.RefExpr = buildDeclRefExpr(
1301 SemaRef, VD, D->getType().getNonReferenceType(),
1302 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1303 DVar.CKind = OMPC_threadprivate;
1304 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
Alexey Bataev852525d2018-03-02 17:17:12 +00001305 return DVar;
1306 }
1307 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1308 // in a Construct, C/C++, predetermined, p.1]
1309 // Variables appearing in threadprivate directives are threadprivate.
1310 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
1311 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1312 SemaRef.getLangOpts().OpenMPUseTLS &&
1313 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
1314 (VD && VD->getStorageClass() == SC_Register &&
1315 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
1316 DVar.RefExpr = buildDeclRefExpr(
1317 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
1318 DVar.CKind = OMPC_threadprivate;
1319 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1320 return DVar;
1321 }
1322 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
1323 VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
1324 !isLoopControlVariable(D).first) {
Richard Smith375dec52019-05-30 23:21:14 +00001325 const_iterator IterTarget =
1326 std::find_if(begin(), end(), [](const SharingMapTy &Data) {
1327 return isOpenMPTargetExecutionDirective(Data.Directive);
1328 });
1329 if (IterTarget != end()) {
1330 const_iterator ParentIterTarget = IterTarget + 1;
1331 for (const_iterator Iter = begin();
1332 Iter != ParentIterTarget; ++Iter) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001333 if (isOpenMPLocal(VD, Iter)) {
1334 DVar.RefExpr =
1335 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1336 D->getLocation());
1337 DVar.CKind = OMPC_threadprivate;
1338 return DVar;
1339 }
Alexey Bataev852525d2018-03-02 17:17:12 +00001340 }
Richard Smith375dec52019-05-30 23:21:14 +00001341 if (!isClauseParsingMode() || IterTarget != begin()) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001342 auto DSAIter = IterTarget->SharingMap.find(D);
1343 if (DSAIter != IterTarget->SharingMap.end() &&
1344 isOpenMPPrivate(DSAIter->getSecond().Attributes)) {
1345 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1346 DVar.CKind = OMPC_threadprivate;
1347 return DVar;
Alexey Bataeve3727102018-04-18 15:57:46 +00001348 }
Richard Smith375dec52019-05-30 23:21:14 +00001349 const_iterator End = end();
Alexey Bataeve3727102018-04-18 15:57:46 +00001350 if (!SemaRef.isOpenMPCapturedByRef(
Joel E. Denny7d5bc552019-08-22 03:34:30 +00001351 D, std::distance(ParentIterTarget, End),
1352 /*OpenMPCaptureLevel=*/0)) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001353 DVar.RefExpr =
1354 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1355 IterTarget->ConstructLoc);
1356 DVar.CKind = OMPC_threadprivate;
1357 return DVar;
1358 }
1359 }
1360 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001361 }
1362
Alexey Bataev4b465392017-04-26 15:06:24 +00001363 if (isStackEmpty())
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001364 // Not in OpenMP execution region and top scope was already checked.
1365 return DVar;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001366
Alexey Bataev758e55e2013-09-06 18:03:48 +00001367 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001368 // in a Construct, C/C++, predetermined, p.4]
1369 // Static data members are shared.
1370 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1371 // in a Construct, C/C++, predetermined, p.7]
1372 // Variables with static storage duration that are declared in a scope
1373 // inside the construct are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001374 if (VD && VD->isStaticDataMember()) {
Alexey Bataev73f9d9aa2019-06-28 16:16:00 +00001375 // Check for explicitly specified attributes.
1376 const_iterator I = begin();
1377 const_iterator EndI = end();
1378 if (FromParent && I != EndI)
1379 ++I;
1380 auto It = I->SharingMap.find(D);
1381 if (It != I->SharingMap.end()) {
1382 const DSAInfo &Data = It->getSecond();
1383 DVar.RefExpr = Data.RefExpr.getPointer();
1384 DVar.PrivateCopy = Data.PrivateCopy;
1385 DVar.CKind = Data.Attributes;
1386 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1387 DVar.DKind = I->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +00001388 return DVar;
Alexey Bataev73f9d9aa2019-06-28 16:16:00 +00001389 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001390
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001391 DVar.CKind = OMPC_shared;
1392 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001393 }
1394
Alexey Bataev73f9d9aa2019-06-28 16:16:00 +00001395 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001396 // The predetermined shared attribute for const-qualified types having no
1397 // mutable members was removed after OpenMP 3.1.
1398 if (SemaRef.LangOpts.OpenMP <= 31) {
1399 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1400 // in a Construct, C/C++, predetermined, p.6]
1401 // Variables with const qualified type having no mutable member are
1402 // shared.
Joel E. Dennyd2649292019-01-04 22:11:56 +00001403 if (isConstNotMutableType(SemaRef, D->getType())) {
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001404 // Variables with const-qualified type having no mutable member may be
1405 // listed in a firstprivate clause, even if they are static data members.
1406 DSAVarData DVarTemp = hasInnermostDSA(
1407 D,
1408 [](OpenMPClauseKind C) {
1409 return C == OMPC_firstprivate || C == OMPC_shared;
1410 },
1411 MatchesAlways, FromParent);
1412 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
1413 return DVarTemp;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001414
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001415 DVar.CKind = OMPC_shared;
1416 return DVar;
1417 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001418 }
1419
Alexey Bataev758e55e2013-09-06 18:03:48 +00001420 // Explicitly specified attributes and local variables with predetermined
1421 // attributes.
Richard Smith375dec52019-05-30 23:21:14 +00001422 const_iterator I = begin();
1423 const_iterator EndI = end();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001424 if (FromParent && I != EndI)
Richard Smith375dec52019-05-30 23:21:14 +00001425 ++I;
Alexey Bataeve3727102018-04-18 15:57:46 +00001426 auto It = I->SharingMap.find(D);
1427 if (It != I->SharingMap.end()) {
1428 const DSAInfo &Data = It->getSecond();
1429 DVar.RefExpr = Data.RefExpr.getPointer();
1430 DVar.PrivateCopy = Data.PrivateCopy;
1431 DVar.CKind = Data.Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001432 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev4d4624c2017-07-20 16:47:47 +00001433 DVar.DKind = I->Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001434 }
1435
1436 return DVar;
1437}
1438
Alexey Bataeve3727102018-04-18 15:57:46 +00001439const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1440 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001441 if (isStackEmpty()) {
Richard Smith375dec52019-05-30 23:21:14 +00001442 const_iterator I;
Alexey Bataev4b465392017-04-26 15:06:24 +00001443 return getDSA(I, D);
1444 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001445 D = getCanonicalDecl(D);
Richard Smith375dec52019-05-30 23:21:14 +00001446 const_iterator StartI = begin();
1447 const_iterator EndI = end();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001448 if (FromParent && StartI != EndI)
Richard Smith375dec52019-05-30 23:21:14 +00001449 ++StartI;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001450 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001451}
1452
Alexey Bataeve3727102018-04-18 15:57:46 +00001453const DSAStackTy::DSAVarData
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001454DSAStackTy::hasDSA(ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001455 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1456 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001457 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001458 if (isStackEmpty())
1459 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001460 D = getCanonicalDecl(D);
Richard Smith375dec52019-05-30 23:21:14 +00001461 const_iterator I = begin();
1462 const_iterator EndI = end();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001463 if (FromParent && I != EndI)
Richard Smith375dec52019-05-30 23:21:14 +00001464 ++I;
1465 for (; I != EndI; ++I) {
1466 if (!DPred(I->Directive) &&
1467 !isImplicitOrExplicitTaskingRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +00001468 continue;
Richard Smith375dec52019-05-30 23:21:14 +00001469 const_iterator NewI = I;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001470 DSAVarData DVar = getDSA(NewI, D);
1471 if (I == NewI && CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001472 return DVar;
Alexey Bataev60859c02017-04-27 15:10:33 +00001473 }
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001474 return {};
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001475}
1476
Alexey Bataeve3727102018-04-18 15:57:46 +00001477const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001478 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1479 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001480 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001481 if (isStackEmpty())
1482 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001483 D = getCanonicalDecl(D);
Richard Smith375dec52019-05-30 23:21:14 +00001484 const_iterator StartI = begin();
1485 const_iterator EndI = end();
Alexey Bataeve3978122016-07-19 05:06:39 +00001486 if (FromParent && StartI != EndI)
Richard Smith375dec52019-05-30 23:21:14 +00001487 ++StartI;
Alexey Bataeve3978122016-07-19 05:06:39 +00001488 if (StartI == EndI || !DPred(StartI->Directive))
Alexey Bataev4b465392017-04-26 15:06:24 +00001489 return {};
Richard Smith375dec52019-05-30 23:21:14 +00001490 const_iterator NewI = StartI;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001491 DSAVarData DVar = getDSA(NewI, D);
1492 return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData();
Alexey Bataevc5e02582014-06-16 07:08:35 +00001493}
1494
Alexey Bataevaac108a2015-06-23 04:51:00 +00001495bool DSAStackTy::hasExplicitDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00001496 const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1497 unsigned Level, bool NotLastprivate) const {
Richard Smith375dec52019-05-30 23:21:14 +00001498 if (getStackSize() <= Level)
Alexey Bataev4b465392017-04-26 15:06:24 +00001499 return false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001500 D = getCanonicalDecl(D);
Richard Smith375dec52019-05-30 23:21:14 +00001501 const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1502 auto I = StackElem.SharingMap.find(D);
1503 if (I != StackElem.SharingMap.end() &&
1504 I->getSecond().RefExpr.getPointer() &&
1505 CPred(I->getSecond().Attributes) &&
1506 (!NotLastprivate || !I->getSecond().RefExpr.getInt()))
Alexey Bataev92b33652018-11-21 19:41:10 +00001507 return true;
1508 // Check predetermined rules for the loop control variables.
Richard Smith375dec52019-05-30 23:21:14 +00001509 auto LI = StackElem.LCVMap.find(D);
1510 if (LI != StackElem.LCVMap.end())
Alexey Bataev92b33652018-11-21 19:41:10 +00001511 return CPred(OMPC_private);
1512 return false;
Alexey Bataevaac108a2015-06-23 04:51:00 +00001513}
1514
Samuel Antao4be30e92015-10-02 17:14:03 +00001515bool DSAStackTy::hasExplicitDirective(
Alexey Bataeve3727102018-04-18 15:57:46 +00001516 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1517 unsigned Level) const {
Richard Smith375dec52019-05-30 23:21:14 +00001518 if (getStackSize() <= Level)
Alexey Bataev4b465392017-04-26 15:06:24 +00001519 return false;
Richard Smith375dec52019-05-30 23:21:14 +00001520 const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1521 return DPred(StackElem.Directive);
Samuel Antao4be30e92015-10-02 17:14:03 +00001522}
1523
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001524bool DSAStackTy::hasDirective(
1525 const llvm::function_ref<bool(OpenMPDirectiveKind,
1526 const DeclarationNameInfo &, SourceLocation)>
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001527 DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001528 bool FromParent) const {
Samuel Antaof0d79752016-05-27 15:21:27 +00001529 // We look only in the enclosing region.
Richard Smith375dec52019-05-30 23:21:14 +00001530 size_t Skip = FromParent ? 2 : 1;
1531 for (const_iterator I = begin() + std::min(Skip, getStackSize()), E = end();
1532 I != E; ++I) {
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001533 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1534 return true;
1535 }
1536 return false;
1537}
1538
Alexey Bataev758e55e2013-09-06 18:03:48 +00001539void Sema::InitDataSharingAttributesStack() {
1540 VarDataSharingAttributesStack = new DSAStackTy(*this);
1541}
1542
1543#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1544
Alexey Bataev4b465392017-04-26 15:06:24 +00001545void Sema::pushOpenMPFunctionRegion() {
1546 DSAStack->pushFunction();
1547}
1548
1549void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1550 DSAStack->popFunction(OldFSI);
1551}
1552
Alexey Bataevc416e642019-02-08 18:02:25 +00001553static bool isOpenMPDeviceDelayedContext(Sema &S) {
1554 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice &&
1555 "Expected OpenMP device compilation.");
1556 return !S.isInOpenMPTargetExecutionDirective() &&
1557 !S.isInOpenMPDeclareTargetContext();
1558}
1559
Alexey Bataev729e2422019-08-23 16:11:14 +00001560namespace {
1561/// Status of the function emission on the host/device.
1562enum class FunctionEmissionStatus {
1563 Emitted,
1564 Discarded,
1565 Unknown,
1566};
1567} // anonymous namespace
1568
Alexey Bataevc416e642019-02-08 18:02:25 +00001569Sema::DeviceDiagBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc,
1570 unsigned DiagID) {
1571 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice &&
1572 "Expected OpenMP device compilation.");
Yaxun Liu229c78d2019-10-09 23:54:10 +00001573 FunctionEmissionStatus FES = getEmissionStatus(getCurFunctionDecl());
Alexey Bataev729e2422019-08-23 16:11:14 +00001574 DeviceDiagBuilder::Kind Kind = DeviceDiagBuilder::K_Nop;
1575 switch (FES) {
1576 case FunctionEmissionStatus::Emitted:
1577 Kind = DeviceDiagBuilder::K_Immediate;
1578 break;
1579 case FunctionEmissionStatus::Unknown:
1580 Kind = isOpenMPDeviceDelayedContext(*this) ? DeviceDiagBuilder::K_Deferred
1581 : DeviceDiagBuilder::K_Immediate;
1582 break;
Yaxun Liu229c78d2019-10-09 23:54:10 +00001583 case FunctionEmissionStatus::TemplateDiscarded:
1584 case FunctionEmissionStatus::OMPDiscarded:
Alexey Bataev729e2422019-08-23 16:11:14 +00001585 Kind = DeviceDiagBuilder::K_Nop;
1586 break;
Yaxun Liu229c78d2019-10-09 23:54:10 +00001587 case FunctionEmissionStatus::CUDADiscarded:
1588 llvm_unreachable("CUDADiscarded unexpected in OpenMP device compilation");
1589 break;
Alexey Bataev729e2422019-08-23 16:11:14 +00001590 }
1591
1592 return DeviceDiagBuilder(Kind, Loc, DiagID, getCurFunctionDecl(), *this);
1593}
1594
Alexey Bataev729e2422019-08-23 16:11:14 +00001595Sema::DeviceDiagBuilder Sema::diagIfOpenMPHostCode(SourceLocation Loc,
1596 unsigned DiagID) {
1597 assert(LangOpts.OpenMP && !LangOpts.OpenMPIsDevice &&
1598 "Expected OpenMP host compilation.");
Yaxun Liu229c78d2019-10-09 23:54:10 +00001599 FunctionEmissionStatus FES = getEmissionStatus(getCurFunctionDecl());
Alexey Bataev729e2422019-08-23 16:11:14 +00001600 DeviceDiagBuilder::Kind Kind = DeviceDiagBuilder::K_Nop;
1601 switch (FES) {
1602 case FunctionEmissionStatus::Emitted:
1603 Kind = DeviceDiagBuilder::K_Immediate;
1604 break;
1605 case FunctionEmissionStatus::Unknown:
1606 Kind = DeviceDiagBuilder::K_Deferred;
1607 break;
Yaxun Liu229c78d2019-10-09 23:54:10 +00001608 case FunctionEmissionStatus::TemplateDiscarded:
1609 case FunctionEmissionStatus::OMPDiscarded:
1610 case FunctionEmissionStatus::CUDADiscarded:
Alexey Bataev729e2422019-08-23 16:11:14 +00001611 Kind = DeviceDiagBuilder::K_Nop;
1612 break;
1613 }
1614
1615 return DeviceDiagBuilder(Kind, Loc, DiagID, getCurFunctionDecl(), *this);
Alexey Bataevc416e642019-02-08 18:02:25 +00001616}
1617
Alexey Bataev9fd495b2019-08-20 19:50:13 +00001618void Sema::checkOpenMPDeviceFunction(SourceLocation Loc, FunctionDecl *Callee,
1619 bool CheckForDelayedContext) {
Alexey Bataevc416e642019-02-08 18:02:25 +00001620 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice &&
1621 "Expected OpenMP device compilation.");
1622 assert(Callee && "Callee may not be null.");
Alexey Bataev729e2422019-08-23 16:11:14 +00001623 Callee = Callee->getMostRecentDecl();
Alexey Bataevc416e642019-02-08 18:02:25 +00001624 FunctionDecl *Caller = getCurFunctionDecl();
1625
Alexey Bataev729e2422019-08-23 16:11:14 +00001626 // host only function are not available on the device.
Yaxun Liu229c78d2019-10-09 23:54:10 +00001627 if (Caller) {
1628 FunctionEmissionStatus CallerS = getEmissionStatus(Caller);
1629 FunctionEmissionStatus CalleeS = getEmissionStatus(Callee);
1630 assert(CallerS != FunctionEmissionStatus::CUDADiscarded &&
1631 CalleeS != FunctionEmissionStatus::CUDADiscarded &&
1632 "CUDADiscarded unexpected in OpenMP device function check");
1633 if ((CallerS == FunctionEmissionStatus::Emitted ||
1634 (!isOpenMPDeviceDelayedContext(*this) &&
1635 CallerS == FunctionEmissionStatus::Unknown)) &&
1636 CalleeS == FunctionEmissionStatus::OMPDiscarded) {
1637 StringRef HostDevTy = getOpenMPSimpleClauseTypeName(
1638 OMPC_device_type, OMPC_DEVICE_TYPE_host);
1639 Diag(Loc, diag::err_omp_wrong_device_function_call) << HostDevTy << 0;
1640 Diag(Callee->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(),
1641 diag::note_omp_marked_device_type_here)
1642 << HostDevTy;
1643 return;
1644 }
Alexey Bataev729e2422019-08-23 16:11:14 +00001645 }
Alexey Bataevc416e642019-02-08 18:02:25 +00001646 // If the caller is known-emitted, mark the callee as known-emitted.
1647 // Otherwise, mark the call in our call graph so we can traverse it later.
Alexey Bataev9fd495b2019-08-20 19:50:13 +00001648 if ((CheckForDelayedContext && !isOpenMPDeviceDelayedContext(*this)) ||
1649 (!Caller && !CheckForDelayedContext) ||
Yaxun Liu229c78d2019-10-09 23:54:10 +00001650 (Caller && getEmissionStatus(Caller) == FunctionEmissionStatus::Emitted))
Alexey Bataev9fd495b2019-08-20 19:50:13 +00001651 markKnownEmitted(*this, Caller, Callee, Loc,
1652 [CheckForDelayedContext](Sema &S, FunctionDecl *FD) {
Alexey Bataev729e2422019-08-23 16:11:14 +00001653 return CheckForDelayedContext &&
Yaxun Liu229c78d2019-10-09 23:54:10 +00001654 S.getEmissionStatus(FD) ==
Alexey Bataev729e2422019-08-23 16:11:14 +00001655 FunctionEmissionStatus::Emitted;
Alexey Bataev9fd495b2019-08-20 19:50:13 +00001656 });
Alexey Bataevc416e642019-02-08 18:02:25 +00001657 else if (Caller)
1658 DeviceCallGraph[Caller].insert({Callee, Loc});
1659}
1660
Alexey Bataev729e2422019-08-23 16:11:14 +00001661void Sema::checkOpenMPHostFunction(SourceLocation Loc, FunctionDecl *Callee,
1662 bool CheckCaller) {
1663 assert(LangOpts.OpenMP && !LangOpts.OpenMPIsDevice &&
1664 "Expected OpenMP host compilation.");
1665 assert(Callee && "Callee may not be null.");
1666 Callee = Callee->getMostRecentDecl();
1667 FunctionDecl *Caller = getCurFunctionDecl();
1668
1669 // device only function are not available on the host.
Yaxun Liu229c78d2019-10-09 23:54:10 +00001670 if (Caller) {
1671 FunctionEmissionStatus CallerS = getEmissionStatus(Caller);
1672 FunctionEmissionStatus CalleeS = getEmissionStatus(Callee);
1673 assert(
1674 (LangOpts.CUDA || (CallerS != FunctionEmissionStatus::CUDADiscarded &&
1675 CalleeS != FunctionEmissionStatus::CUDADiscarded)) &&
1676 "CUDADiscarded unexpected in OpenMP host function check");
1677 if (CallerS == FunctionEmissionStatus::Emitted &&
1678 CalleeS == FunctionEmissionStatus::OMPDiscarded) {
1679 StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName(
1680 OMPC_device_type, OMPC_DEVICE_TYPE_nohost);
1681 Diag(Loc, diag::err_omp_wrong_device_function_call) << NoHostDevTy << 1;
1682 Diag(Callee->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(),
1683 diag::note_omp_marked_device_type_here)
1684 << NoHostDevTy;
1685 return;
1686 }
Alexey Bataev729e2422019-08-23 16:11:14 +00001687 }
1688 // If the caller is known-emitted, mark the callee as known-emitted.
1689 // Otherwise, mark the call in our call graph so we can traverse it later.
Yaxun Liu229c78d2019-10-09 23:54:10 +00001690 if (!shouldIgnoreInHostDeviceCheck(Callee)) {
1691 if ((!CheckCaller && !Caller) ||
1692 (Caller &&
1693 getEmissionStatus(Caller) == FunctionEmissionStatus::Emitted))
1694 markKnownEmitted(
1695 *this, Caller, Callee, Loc, [CheckCaller](Sema &S, FunctionDecl *FD) {
1696 return CheckCaller &&
1697 S.getEmissionStatus(FD) == FunctionEmissionStatus::Emitted;
1698 });
1699 else if (Caller)
1700 DeviceCallGraph[Caller].insert({Callee, Loc});
1701 }
Alexey Bataev729e2422019-08-23 16:11:14 +00001702}
1703
Alexey Bataev123ad192019-02-27 20:29:45 +00001704void Sema::checkOpenMPDeviceExpr(const Expr *E) {
1705 assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice &&
1706 "OpenMP device compilation mode is expected.");
1707 QualType Ty = E->getType();
1708 if ((Ty->isFloat16Type() && !Context.getTargetInfo().hasFloat16Type()) ||
Alexey Bataev8557d1a2019-06-18 18:39:26 +00001709 ((Ty->isFloat128Type() ||
1710 (Ty->isRealFloatingType() && Context.getTypeSize(Ty) == 128)) &&
1711 !Context.getTargetInfo().hasFloat128Type()) ||
Alexey Bataev123ad192019-02-27 20:29:45 +00001712 (Ty->isIntegerType() && Context.getTypeSize(Ty) == 128 &&
1713 !Context.getTargetInfo().hasInt128Type()))
Alexey Bataev62892592019-07-08 19:21:54 +00001714 targetDiag(E->getExprLoc(), diag::err_omp_unsupported_type)
1715 << static_cast<unsigned>(Context.getTypeSize(Ty)) << Ty
1716 << Context.getTargetInfo().getTriple().str() << E->getSourceRange();
Alexey Bataev123ad192019-02-27 20:29:45 +00001717}
1718
Joel E. Denny7d5bc552019-08-22 03:34:30 +00001719bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level,
1720 unsigned OpenMPCaptureLevel) const {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001721 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1722
Alexey Bataeve3727102018-04-18 15:57:46 +00001723 ASTContext &Ctx = getASTContext();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001724 bool IsByRef = true;
1725
1726 // Find the directive that is associated with the provided scope.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00001727 D = cast<ValueDecl>(D->getCanonicalDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00001728 QualType Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001729
Joel E. Denny7d5bc552019-08-22 03:34:30 +00001730 bool IsVariableUsedInMapClause = false;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001731 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001732 // This table summarizes how a given variable should be passed to the device
1733 // given its type and the clauses where it appears. This table is based on
1734 // the description in OpenMP 4.5 [2.10.4, target Construct] and
1735 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1736 //
1737 // =========================================================================
1738 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
1739 // | |(tofrom:scalar)| | pvt | | | |
1740 // =========================================================================
1741 // | scl | | | | - | | bycopy|
1742 // | scl | | - | x | - | - | bycopy|
1743 // | scl | | x | - | - | - | null |
1744 // | scl | x | | | - | | byref |
1745 // | scl | x | - | x | - | - | bycopy|
1746 // | scl | x | x | - | - | - | null |
1747 // | scl | | - | - | - | x | byref |
1748 // | scl | x | - | - | - | x | byref |
1749 //
1750 // | agg | n.a. | | | - | | byref |
1751 // | agg | n.a. | - | x | - | - | byref |
1752 // | agg | n.a. | x | - | - | - | null |
1753 // | agg | n.a. | - | - | - | x | byref |
1754 // | agg | n.a. | - | - | - | x[] | byref |
1755 //
1756 // | ptr | n.a. | | | - | | bycopy|
1757 // | ptr | n.a. | - | x | - | - | bycopy|
1758 // | ptr | n.a. | x | - | - | - | null |
1759 // | ptr | n.a. | - | - | - | x | byref |
1760 // | ptr | n.a. | - | - | - | x[] | bycopy|
1761 // | ptr | n.a. | - | - | x | | bycopy|
1762 // | ptr | n.a. | - | - | x | x | bycopy|
1763 // | ptr | n.a. | - | - | x | x[] | bycopy|
1764 // =========================================================================
1765 // Legend:
1766 // scl - scalar
1767 // ptr - pointer
1768 // agg - aggregate
1769 // x - applies
1770 // - - invalid in this combination
1771 // [] - mapped with an array section
1772 // byref - should be mapped by reference
1773 // byval - should be mapped by value
1774 // null - initialize a local variable to null on the device
1775 //
1776 // Observations:
1777 // - All scalar declarations that show up in a map clause have to be passed
1778 // by reference, because they may have been mapped in the enclosing data
1779 // environment.
1780 // - If the scalar value does not fit the size of uintptr, it has to be
1781 // passed by reference, regardless the result in the table above.
1782 // - For pointers mapped by value that have either an implicit map or an
1783 // array section, the runtime library may pass the NULL value to the
1784 // device instead of the value passed to it by the compiler.
1785
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001786 if (Ty->isReferenceType())
1787 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
Samuel Antao86ace552016-04-27 22:40:57 +00001788
1789 // Locate map clauses and see if the variable being captured is referred to
1790 // in any of those clauses. Here we only care about variables, not fields,
1791 // because fields are part of aggregates.
Samuel Antao86ace552016-04-27 22:40:57 +00001792 bool IsVariableAssociatedWithSection = false;
1793
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +00001794 DSAStack->checkMappableExprComponentListsForDeclAtLevel(
Alexey Bataeve3727102018-04-18 15:57:46 +00001795 D, Level,
1796 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D](
1797 OMPClauseMappableExprCommon::MappableExprComponentListRef
Samuel Antao6890b092016-07-28 14:25:09 +00001798 MapExprComponents,
1799 OpenMPClauseKind WhereFoundClauseKind) {
1800 // Only the map clause information influences how a variable is
1801 // captured. E.g. is_device_ptr does not require changing the default
Samuel Antao4c8035b2016-12-12 18:00:20 +00001802 // behavior.
Samuel Antao6890b092016-07-28 14:25:09 +00001803 if (WhereFoundClauseKind != OMPC_map)
1804 return false;
Samuel Antao86ace552016-04-27 22:40:57 +00001805
1806 auto EI = MapExprComponents.rbegin();
1807 auto EE = MapExprComponents.rend();
1808
1809 assert(EI != EE && "Invalid map expression!");
1810
1811 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
1812 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
1813
1814 ++EI;
1815 if (EI == EE)
1816 return false;
1817
1818 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
1819 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
1820 isa<MemberExpr>(EI->getAssociatedExpression())) {
1821 IsVariableAssociatedWithSection = true;
1822 // There is nothing more we need to know about this variable.
1823 return true;
1824 }
1825
1826 // Keep looking for more map info.
1827 return false;
1828 });
1829
1830 if (IsVariableUsedInMapClause) {
1831 // If variable is identified in a map clause it is always captured by
1832 // reference except if it is a pointer that is dereferenced somehow.
1833 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
1834 } else {
Alexey Bataev3f96fe62017-12-13 17:31:39 +00001835 // By default, all the data that has a scalar type is mapped by copy
1836 // (except for reduction variables).
1837 IsByRef =
Alexey Bataev60705422018-10-30 15:50:12 +00001838 (DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
1839 !Ty->isAnyPointerType()) ||
Alexey Bataev3f96fe62017-12-13 17:31:39 +00001840 !Ty->isScalarType() ||
1841 DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar ||
1842 DSAStack->hasExplicitDSA(
1843 D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level);
Samuel Antao86ace552016-04-27 22:40:57 +00001844 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001845 }
1846
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001847 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00001848 IsByRef =
Joel E. Denny7d5bc552019-08-22 03:34:30 +00001849 ((IsVariableUsedInMapClause &&
1850 DSAStack->getCaptureRegion(Level, OpenMPCaptureLevel) ==
1851 OMPD_target) ||
1852 !DSAStack->hasExplicitDSA(
1853 D,
1854 [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
1855 Level, /*NotLastprivate=*/true)) &&
Alexey Bataev8e769ee2017-12-22 21:01:52 +00001856 // If the variable is artificial and must be captured by value - try to
1857 // capture by value.
Alexey Bataevd2202ca2017-12-27 17:58:32 +00001858 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
1859 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue());
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001860 }
1861
Samuel Antao86ace552016-04-27 22:40:57 +00001862 // When passing data by copy, we need to make sure it fits the uintptr size
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001863 // and alignment, because the runtime library only deals with uintptr types.
1864 // If it does not fit the uintptr size, we need to pass the data by reference
1865 // instead.
1866 if (!IsByRef &&
1867 (Ctx.getTypeSizeInChars(Ty) >
1868 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001869 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001870 IsByRef = true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001871 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001872
1873 return IsByRef;
1874}
1875
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001876unsigned Sema::getOpenMPNestingLevel() const {
1877 assert(getLangOpts().OpenMP);
1878 return DSAStack->getNestingLevel();
1879}
1880
Jonas Hahnfeld87d44262017-11-18 21:00:46 +00001881bool Sema::isInOpenMPTargetExecutionDirective() const {
1882 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
1883 !DSAStack->isClauseParsingMode()) ||
1884 DSAStack->hasDirective(
1885 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1886 SourceLocation) -> bool {
1887 return isOpenMPTargetExecutionDirective(K);
1888 },
1889 false);
1890}
1891
Alexey Bataev41ebe0c2019-05-09 18:14:57 +00001892VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo,
1893 unsigned StopAt) {
Alexey Bataevf841bd92014-12-16 07:00:22 +00001894 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001895 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +00001896
Richard Smith0621a8f2019-05-31 00:45:10 +00001897 // If we want to determine whether the variable should be captured from the
1898 // perspective of the current capturing scope, and we've already left all the
1899 // capturing scopes of the top directive on the stack, check from the
1900 // perspective of its parent directive (if any) instead.
1901 DSAStackTy::ParentDirectiveScope InParentDirectiveRAII(
1902 *DSAStack, CheckScopeInfo && DSAStack->isBodyComplete());
1903
Samuel Antao4be30e92015-10-02 17:14:03 +00001904 // If we are attempting to capture a global variable in a directive with
1905 // 'target' we return true so that this global is also mapped to the device.
1906 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001907 auto *VD = dyn_cast<VarDecl>(D);
Richard Smith0621a8f2019-05-31 00:45:10 +00001908 if (VD && !VD->hasLocalStorage() &&
1909 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) {
1910 if (isInOpenMPDeclareTargetContext()) {
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001911 // Try to mark variable as declare target if it is used in capturing
1912 // regions.
Alexey Bataev217ff1e2019-08-16 20:15:02 +00001913 if (LangOpts.OpenMP <= 45 &&
1914 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001915 checkDeclIsAllowedInOpenMPTarget(nullptr, VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00001916 return nullptr;
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001917 } else if (isInOpenMPTargetExecutionDirective()) {
1918 // If the declaration is enclosed in a 'declare target' directive,
1919 // then it should not be captured.
1920 //
Alexey Bataev97b72212018-08-14 18:31:20 +00001921 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001922 return nullptr;
1923 return VD;
1924 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001925 }
Samuel Antao4be30e92015-10-02 17:14:03 +00001926
Alexey Bataev41ebe0c2019-05-09 18:14:57 +00001927 if (CheckScopeInfo) {
1928 bool OpenMPFound = false;
1929 for (unsigned I = StopAt + 1; I > 0; --I) {
1930 FunctionScopeInfo *FSI = FunctionScopes[I - 1];
1931 if(!isa<CapturingScopeInfo>(FSI))
1932 return nullptr;
1933 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI))
1934 if (RSI->CapRegionKind == CR_OpenMP) {
1935 OpenMPFound = true;
1936 break;
1937 }
1938 }
1939 if (!OpenMPFound)
1940 return nullptr;
1941 }
1942
Alexey Bataev48977c32015-08-04 08:10:48 +00001943 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1944 (!DSAStack->isClauseParsingMode() ||
1945 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001946 auto &&Info = DSAStack->isLoopControlVariable(D);
1947 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001948 (VD && VD->hasLocalStorage() &&
Alexey Bataev7e6803e2019-01-09 15:58:05 +00001949 isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001950 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001951 return VD ? VD : Info.second;
Alexey Bataeve3727102018-04-18 15:57:46 +00001952 DSAStackTy::DSAVarData DVarPrivate =
1953 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001954 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +00001955 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataeve0eb66b2019-06-21 15:08:30 +00001956 // Threadprivate variables must not be captured.
1957 if (isOpenMPThreadPrivate(DVarPrivate.CKind))
1958 return nullptr;
1959 // The variable is not private or it is the variable in the directive with
1960 // default(none) clause and not used in any clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00001961 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate,
1962 [](OpenMPDirectiveKind) { return true; },
1963 DSAStack->isClauseParsingMode());
Alexey Bataev41ebe0c2019-05-09 18:14:57 +00001964 if (DVarPrivate.CKind != OMPC_unknown ||
1965 (VD && DSAStack->getDefaultDSA() == DSA_none))
Alexey Bataev90c228f2016-02-08 09:29:13 +00001966 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001967 }
Alexey Bataev90c228f2016-02-08 09:29:13 +00001968 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +00001969}
1970
Alexey Bataevdfa430f2017-12-08 15:03:50 +00001971void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
1972 unsigned Level) const {
1973 SmallVector<OpenMPDirectiveKind, 4> Regions;
1974 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
1975 FunctionScopesIndex -= Regions.size();
1976}
1977
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00001978void Sema::startOpenMPLoop() {
1979 assert(LangOpts.OpenMP && "OpenMP must be enabled.");
1980 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
1981 DSAStack->loopInit();
1982}
1983
Alexey Bataevbef93a92019-10-07 18:54:57 +00001984void Sema::startOpenMPCXXRangeFor() {
1985 assert(LangOpts.OpenMP && "OpenMP must be enabled.");
1986 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
1987 DSAStack->resetPossibleLoopCounter();
1988 DSAStack->loopStart();
1989 }
1990}
1991
Alexey Bataeve3727102018-04-18 15:57:46 +00001992bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const {
Alexey Bataevaac108a2015-06-23 04:51:00 +00001993 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00001994 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
1995 if (DSAStack->getAssociatedLoops() > 0 &&
1996 !DSAStack->isLoopStarted()) {
1997 DSAStack->resetPossibleLoopCounter(D);
1998 DSAStack->loopStart();
1999 return true;
2000 }
2001 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() ||
2002 DSAStack->isLoopControlVariable(D).first) &&
2003 !DSAStack->hasExplicitDSA(
2004 D, [](OpenMPClauseKind K) { return K != OMPC_private; }, Level) &&
2005 !isOpenMPSimdDirective(DSAStack->getCurrentDirective()))
2006 return true;
2007 }
Alexey Bataev0c99d192019-07-18 19:40:24 +00002008 if (const auto *VD = dyn_cast<VarDecl>(D)) {
2009 if (DSAStack->isThreadPrivate(const_cast<VarDecl *>(VD)) &&
2010 DSAStack->isForceVarCapturing() &&
2011 !DSAStack->hasExplicitDSA(
2012 D, [](OpenMPClauseKind K) { return K == OMPC_copyin; }, Level))
2013 return true;
2014 }
Alexey Bataevaac108a2015-06-23 04:51:00 +00002015 return DSAStack->hasExplicitDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002016 D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) ||
Alexey Bataev3f82cfc2017-12-13 15:28:44 +00002017 (DSAStack->isClauseParsingMode() &&
2018 DSAStack->getClauseParsingMode() == OMPC_private) ||
Alexey Bataev88202be2017-07-27 13:20:36 +00002019 // Consider taskgroup reduction descriptor variable a private to avoid
2020 // possible capture in the region.
2021 (DSAStack->hasExplicitDirective(
2022 [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; },
2023 Level) &&
2024 DSAStack->isTaskgroupReductionRef(D, Level));
Alexey Bataevaac108a2015-06-23 04:51:00 +00002025}
2026
Alexey Bataeve3727102018-04-18 15:57:46 +00002027void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
2028 unsigned Level) {
Alexey Bataev3b8d5582017-08-08 18:04:06 +00002029 assert(LangOpts.OpenMP && "OpenMP is not allowed");
2030 D = getCanonicalDecl(D);
2031 OpenMPClauseKind OMPC = OMPC_unknown;
2032 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
2033 const unsigned NewLevel = I - 1;
2034 if (DSAStack->hasExplicitDSA(D,
2035 [&OMPC](const OpenMPClauseKind K) {
2036 if (isOpenMPPrivate(K)) {
2037 OMPC = K;
2038 return true;
2039 }
2040 return false;
2041 },
2042 NewLevel))
2043 break;
2044 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
2045 D, NewLevel,
2046 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
2047 OpenMPClauseKind) { return true; })) {
2048 OMPC = OMPC_map;
2049 break;
2050 }
2051 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
2052 NewLevel)) {
Alexey Bataev63cc8e92018-03-20 14:45:59 +00002053 OMPC = OMPC_map;
2054 if (D->getType()->isScalarType() &&
2055 DSAStack->getDefaultDMAAtLevel(NewLevel) !=
2056 DefaultMapAttributes::DMA_tofrom_scalar)
2057 OMPC = OMPC_firstprivate;
Alexey Bataev3b8d5582017-08-08 18:04:06 +00002058 break;
2059 }
2060 }
2061 if (OMPC != OMPC_unknown)
2062 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC));
2063}
2064
Alexey Bataeve3727102018-04-18 15:57:46 +00002065bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D,
2066 unsigned Level) const {
Samuel Antao4be30e92015-10-02 17:14:03 +00002067 assert(LangOpts.OpenMP && "OpenMP is not allowed");
2068 // Return true if the current level is no longer enclosed in a target region.
2069
Alexey Bataeve3727102018-04-18 15:57:46 +00002070 const auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002071 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002072 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
2073 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +00002074}
2075
Alexey Bataeved09d242014-05-28 05:53:51 +00002076void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002077
Alexey Bataev729e2422019-08-23 16:11:14 +00002078void Sema::finalizeOpenMPDelayedAnalysis() {
2079 assert(LangOpts.OpenMP && "Expected OpenMP compilation mode.");
2080 // Diagnose implicit declare target functions and their callees.
2081 for (const auto &CallerCallees : DeviceCallGraph) {
2082 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
2083 OMPDeclareTargetDeclAttr::getDeviceType(
2084 CallerCallees.getFirst()->getMostRecentDecl());
2085 // Ignore host functions during device analyzis.
2086 if (LangOpts.OpenMPIsDevice && DevTy &&
2087 *DevTy == OMPDeclareTargetDeclAttr::DT_Host)
2088 continue;
2089 // Ignore nohost functions during host analyzis.
2090 if (!LangOpts.OpenMPIsDevice && DevTy &&
2091 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost)
2092 continue;
2093 for (const std::pair<CanonicalDeclPtr<FunctionDecl>, SourceLocation>
2094 &Callee : CallerCallees.getSecond()) {
2095 const FunctionDecl *FD = Callee.first->getMostRecentDecl();
2096 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
2097 OMPDeclareTargetDeclAttr::getDeviceType(FD);
2098 if (LangOpts.OpenMPIsDevice && DevTy &&
2099 *DevTy == OMPDeclareTargetDeclAttr::DT_Host) {
2100 // Diagnose host function called during device codegen.
2101 StringRef HostDevTy = getOpenMPSimpleClauseTypeName(
2102 OMPC_device_type, OMPC_DEVICE_TYPE_host);
2103 Diag(Callee.second, diag::err_omp_wrong_device_function_call)
2104 << HostDevTy << 0;
2105 Diag(FD->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(),
2106 diag::note_omp_marked_device_type_here)
2107 << HostDevTy;
2108 continue;
2109 }
2110 if (!LangOpts.OpenMPIsDevice && DevTy &&
2111 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) {
2112 // Diagnose nohost function called during host codegen.
2113 StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName(
2114 OMPC_device_type, OMPC_DEVICE_TYPE_nohost);
2115 Diag(Callee.second, diag::err_omp_wrong_device_function_call)
2116 << NoHostDevTy << 1;
2117 Diag(FD->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(),
2118 diag::note_omp_marked_device_type_here)
2119 << NoHostDevTy;
2120 continue;
2121 }
2122 }
2123 }
2124}
2125
Alexey Bataev758e55e2013-09-06 18:03:48 +00002126void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
2127 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +00002128 Scope *CurScope, SourceLocation Loc) {
2129 DSAStack->push(DKind, DirName, CurScope, Loc);
Faisal Valid143a0c2017-04-01 21:30:49 +00002130 PushExpressionEvaluationContext(
2131 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002132}
2133
Alexey Bataevaac108a2015-06-23 04:51:00 +00002134void Sema::StartOpenMPClause(OpenMPClauseKind K) {
2135 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +00002136}
2137
Alexey Bataevaac108a2015-06-23 04:51:00 +00002138void Sema::EndOpenMPClause() {
2139 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +00002140}
2141
Alexey Bataeve106f252019-04-01 14:25:31 +00002142static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
2143 ArrayRef<OMPClause *> Clauses);
2144
Alexey Bataev758e55e2013-09-06 18:03:48 +00002145void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002146 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
2147 // A variable of class type (or array thereof) that appears in a lastprivate
2148 // clause requires an accessible, unambiguous default constructor for the
2149 // class type, unless the list item is also specified in a firstprivate
2150 // clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00002151 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
2152 for (OMPClause *C : D->clauses()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00002153 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
2154 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +00002155 for (Expr *DE : Clause->varlists()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00002156 if (DE->isValueDependent() || DE->isTypeDependent()) {
2157 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002158 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00002159 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00002160 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +00002161 auto *VD = cast<VarDecl>(DRE->getDecl());
Alexey Bataev005248a2016-02-25 05:25:57 +00002162 QualType Type = VD->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +00002163 const DSAStackTy::DSAVarData DVar =
2164 DSAStack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002165 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00002166 // Generate helper private variable and initialize it with the
2167 // default value. The address of the original variable is replaced
2168 // by the address of the new private variable in CodeGen. This new
2169 // variable is not added to IdResolver, so the code in the OpenMP
2170 // region uses original variable for proper diagnostics.
Alexey Bataeve3727102018-04-18 15:57:46 +00002171 VarDecl *VDPrivate = buildVarDecl(
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00002172 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev63cc8e92018-03-20 14:45:59 +00002173 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE);
Richard Smith3beb7c62017-01-12 02:27:38 +00002174 ActOnUninitializedDecl(VDPrivate);
Alexey Bataeve106f252019-04-01 14:25:31 +00002175 if (VDPrivate->isInvalidDecl()) {
2176 PrivateCopies.push_back(nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00002177 continue;
Alexey Bataeve106f252019-04-01 14:25:31 +00002178 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00002179 PrivateCopies.push_back(buildDeclRefExpr(
2180 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00002181 } else {
2182 // The variable is also a firstprivate, so initialization sequence
2183 // for private copy is generated already.
2184 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002185 }
2186 }
Alexey Bataeve106f252019-04-01 14:25:31 +00002187 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002188 }
2189 }
Alexey Bataeve106f252019-04-01 14:25:31 +00002190 // Check allocate clauses.
2191 if (!CurContext->isDependentContext())
2192 checkAllocateClauses(*this, DSAStack, D->clauses());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002193 }
2194
Alexey Bataev758e55e2013-09-06 18:03:48 +00002195 DSAStack->pop();
2196 DiscardCleanupsInEvaluationContext();
2197 PopExpressionEvaluationContext();
2198}
2199
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002200static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
2201 Expr *NumIterations, Sema &SemaRef,
2202 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00002203
Alexey Bataeva769e072013-03-22 06:34:35 +00002204namespace {
2205
Alexey Bataeve3727102018-04-18 15:57:46 +00002206class VarDeclFilterCCC final : public CorrectionCandidateCallback {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002207private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00002208 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00002209
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002210public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00002211 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00002212 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002213 NamedDecl *ND = Candidate.getCorrectionDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +00002214 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002215 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00002216 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
2217 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00002218 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002219 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00002220 }
Bruno Ricci70ad3962019-03-25 17:08:51 +00002221
2222 std::unique_ptr<CorrectionCandidateCallback> clone() override {
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00002223 return std::make_unique<VarDeclFilterCCC>(*this);
Bruno Ricci70ad3962019-03-25 17:08:51 +00002224 }
2225
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002226};
Dmitry Polukhind69b5052016-05-09 14:59:13 +00002227
Alexey Bataeve3727102018-04-18 15:57:46 +00002228class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00002229private:
2230 Sema &SemaRef;
2231
2232public:
2233 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
2234 bool ValidateCandidate(const TypoCorrection &Candidate) override {
2235 NamedDecl *ND = Candidate.getCorrectionDecl();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002236 if (ND && ((isa<VarDecl>(ND) && ND->getKind() == Decl::Var) ||
2237 isa<FunctionDecl>(ND))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00002238 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
2239 SemaRef.getCurScope());
2240 }
2241 return false;
2242 }
Bruno Ricci70ad3962019-03-25 17:08:51 +00002243
2244 std::unique_ptr<CorrectionCandidateCallback> clone() override {
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00002245 return std::make_unique<VarOrFuncDeclFilterCCC>(*this);
Bruno Ricci70ad3962019-03-25 17:08:51 +00002246 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00002247};
2248
Alexey Bataeved09d242014-05-28 05:53:51 +00002249} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002250
2251ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
2252 CXXScopeSpec &ScopeSpec,
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002253 const DeclarationNameInfo &Id,
2254 OpenMPDirectiveKind Kind) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002255 LookupResult Lookup(*this, Id, LookupOrdinaryName);
2256 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
2257
2258 if (Lookup.isAmbiguous())
2259 return ExprError();
2260
2261 VarDecl *VD;
2262 if (!Lookup.isSingleResult()) {
Bruno Ricci70ad3962019-03-25 17:08:51 +00002263 VarDeclFilterCCC CCC(*this);
2264 if (TypoCorrection Corrected =
2265 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC,
2266 CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00002267 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002268 PDiag(Lookup.empty()
2269 ? diag::err_undeclared_var_use_suggest
2270 : diag::err_omp_expected_var_arg_suggest)
2271 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00002272 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002273 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00002274 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
2275 : diag::err_omp_expected_var_arg)
2276 << Id.getName();
2277 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002278 }
Alexey Bataeve3727102018-04-18 15:57:46 +00002279 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
2280 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
2281 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
2282 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002283 }
2284 Lookup.suppressDiagnostics();
2285
2286 // OpenMP [2.9.2, Syntax, C/C++]
2287 // Variables must be file-scope, namespace-scope, or static block-scope.
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002288 if (Kind == OMPD_threadprivate && !VD->hasGlobalStorage()) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002289 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002290 << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal();
Alexey Bataeved09d242014-05-28 05:53:51 +00002291 bool IsDecl =
2292 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002293 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00002294 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2295 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002296 return ExprError();
2297 }
2298
Alexey Bataev7d2960b2013-09-26 03:24:06 +00002299 VarDecl *CanonicalVD = VD->getCanonicalDecl();
George Burgess IV00f70bd2018-03-01 05:43:23 +00002300 NamedDecl *ND = CanonicalVD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002301 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
2302 // A threadprivate directive for file-scope variables must appear outside
2303 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00002304 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
2305 !getCurLexicalContext()->isTranslationUnit()) {
2306 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002307 << getOpenMPDirectiveName(Kind) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +00002308 bool IsDecl =
2309 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2310 Diag(VD->getLocation(),
2311 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2312 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00002313 return ExprError();
2314 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002315 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
2316 // A threadprivate directive for static class member variables must appear
2317 // in the class definition, in the same scope in which the member
2318 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00002319 if (CanonicalVD->isStaticDataMember() &&
2320 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
2321 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002322 << getOpenMPDirectiveName(Kind) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +00002323 bool IsDecl =
2324 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2325 Diag(VD->getLocation(),
2326 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2327 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00002328 return ExprError();
2329 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002330 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
2331 // A threadprivate directive for namespace-scope variables must appear
2332 // outside any definition or declaration other than the namespace
2333 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00002334 if (CanonicalVD->getDeclContext()->isNamespace() &&
2335 (!getCurLexicalContext()->isFileContext() ||
2336 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
2337 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002338 << getOpenMPDirectiveName(Kind) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +00002339 bool IsDecl =
2340 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2341 Diag(VD->getLocation(),
2342 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2343 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00002344 return ExprError();
2345 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002346 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
2347 // A threadprivate directive for static block-scope variables must appear
2348 // in the scope of the variable and not in a nested scope.
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002349 if (CanonicalVD->isLocalVarDecl() && CurScope &&
Alexey Bataev7d2960b2013-09-26 03:24:06 +00002350 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002351 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002352 << getOpenMPDirectiveName(Kind) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +00002353 bool IsDecl =
2354 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2355 Diag(VD->getLocation(),
2356 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2357 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002358 return ExprError();
2359 }
2360
2361 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
2362 // A threadprivate directive must lexically precede all references to any
2363 // of the variables in its list.
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002364 if (Kind == OMPD_threadprivate && VD->isUsed() &&
2365 !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002366 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002367 << getOpenMPDirectiveName(Kind) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002368 return ExprError();
2369 }
2370
2371 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00002372 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
2373 SourceLocation(), VD,
2374 /*RefersToEnclosingVariableOrCapture=*/false,
2375 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002376}
2377
Alexey Bataeved09d242014-05-28 05:53:51 +00002378Sema::DeclGroupPtrTy
2379Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
2380 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002381 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002382 CurContext->addDecl(D);
2383 return DeclGroupPtrTy::make(DeclGroupRef(D));
2384 }
David Blaikie0403cb12016-01-15 23:43:25 +00002385 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00002386}
2387
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002388namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00002389class LocalVarRefChecker final
2390 : public ConstStmtVisitor<LocalVarRefChecker, bool> {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002391 Sema &SemaRef;
2392
2393public:
2394 bool VisitDeclRefExpr(const DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002395 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002396 if (VD->hasLocalStorage()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002397 SemaRef.Diag(E->getBeginLoc(),
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002398 diag::err_omp_local_var_in_threadprivate_init)
2399 << E->getSourceRange();
2400 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
2401 << VD << VD->getSourceRange();
2402 return true;
2403 }
2404 }
2405 return false;
2406 }
2407 bool VisitStmt(const Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002408 for (const Stmt *Child : S->children()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002409 if (Child && Visit(Child))
2410 return true;
2411 }
2412 return false;
2413 }
Alexey Bataev23b69422014-06-18 07:08:49 +00002414 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002415};
2416} // namespace
2417
Alexey Bataeved09d242014-05-28 05:53:51 +00002418OMPThreadPrivateDecl *
2419Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002420 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +00002421 for (Expr *RefExpr : VarList) {
2422 auto *DE = cast<DeclRefExpr>(RefExpr);
2423 auto *VD = cast<VarDecl>(DE->getDecl());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002424 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00002425
Alexey Bataev376b4a42016-02-09 09:41:09 +00002426 // Mark variable as used.
2427 VD->setReferenced();
2428 VD->markUsed(Context);
2429
Alexey Bataevf56f98c2015-04-16 05:39:01 +00002430 QualType QType = VD->getType();
2431 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
2432 // It will be analyzed later.
2433 Vars.push_back(DE);
2434 continue;
2435 }
2436
Alexey Bataeva769e072013-03-22 06:34:35 +00002437 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2438 // A threadprivate variable must not have an incomplete type.
2439 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002440 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002441 continue;
2442 }
2443
2444 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2445 // A threadprivate variable must not have a reference type.
2446 if (VD->getType()->isReferenceType()) {
2447 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002448 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
2449 bool IsDecl =
2450 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2451 Diag(VD->getLocation(),
2452 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2453 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00002454 continue;
2455 }
2456
Samuel Antaof8b50122015-07-13 22:54:53 +00002457 // Check if this is a TLS variable. If TLS is not being supported, produce
2458 // the corresponding diagnostic.
2459 if ((VD->getTLSKind() != VarDecl::TLS_None &&
2460 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
2461 getLangOpts().OpenMPUseTLS &&
2462 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00002463 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
2464 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00002465 Diag(ILoc, diag::err_omp_var_thread_local)
2466 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00002467 bool IsDecl =
2468 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2469 Diag(VD->getLocation(),
2470 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2471 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00002472 continue;
2473 }
2474
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002475 // Check if initial value of threadprivate variable reference variable with
2476 // local storage (it is not supported by runtime).
Alexey Bataeve3727102018-04-18 15:57:46 +00002477 if (const Expr *Init = VD->getAnyInitializer()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002478 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002479 if (Checker.Visit(Init))
2480 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002481 }
2482
Alexey Bataeved09d242014-05-28 05:53:51 +00002483 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00002484 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00002485 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
2486 Context, SourceRange(Loc, Loc)));
Alexey Bataeve3727102018-04-18 15:57:46 +00002487 if (ASTMutationListener *ML = Context.getASTMutationListener())
Alexey Bataev97720002014-11-11 04:05:39 +00002488 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00002489 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002490 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00002491 if (!Vars.empty()) {
2492 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
2493 Vars);
2494 D->setAccess(AS_public);
2495 }
2496 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00002497}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002498
Alexey Bataev27ef9512019-03-20 20:14:22 +00002499static OMPAllocateDeclAttr::AllocatorTypeTy
2500getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) {
2501 if (!Allocator)
2502 return OMPAllocateDeclAttr::OMPDefaultMemAlloc;
2503 if (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
2504 Allocator->isInstantiationDependent() ||
Alexey Bataev441510e2019-03-21 19:05:07 +00002505 Allocator->containsUnexpandedParameterPack())
Alexey Bataev27ef9512019-03-20 20:14:22 +00002506 return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
Alexey Bataev27ef9512019-03-20 20:14:22 +00002507 auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
Alexey Bataeve106f252019-04-01 14:25:31 +00002508 const Expr *AE = Allocator->IgnoreParenImpCasts();
Alexey Bataev27ef9512019-03-20 20:14:22 +00002509 for (int I = OMPAllocateDeclAttr::OMPDefaultMemAlloc;
2510 I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
2511 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
Alexey Bataeve106f252019-04-01 14:25:31 +00002512 const Expr *DefAllocator = Stack->getAllocator(AllocatorKind);
Alexey Bataev441510e2019-03-21 19:05:07 +00002513 llvm::FoldingSetNodeID AEId, DAEId;
2514 AE->Profile(AEId, S.getASTContext(), /*Canonical=*/true);
2515 DefAllocator->Profile(DAEId, S.getASTContext(), /*Canonical=*/true);
2516 if (AEId == DAEId) {
Alexey Bataev27ef9512019-03-20 20:14:22 +00002517 AllocatorKindRes = AllocatorKind;
2518 break;
2519 }
Alexey Bataev27ef9512019-03-20 20:14:22 +00002520 }
Alexey Bataev27ef9512019-03-20 20:14:22 +00002521 return AllocatorKindRes;
2522}
2523
Alexey Bataeve106f252019-04-01 14:25:31 +00002524static bool checkPreviousOMPAllocateAttribute(
2525 Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD,
2526 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) {
2527 if (!VD->hasAttr<OMPAllocateDeclAttr>())
2528 return false;
2529 const auto *A = VD->getAttr<OMPAllocateDeclAttr>();
2530 Expr *PrevAllocator = A->getAllocator();
2531 OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind =
2532 getAllocatorKind(S, Stack, PrevAllocator);
2533 bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind;
2534 if (AllocatorsMatch &&
2535 AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc &&
2536 Allocator && PrevAllocator) {
2537 const Expr *AE = Allocator->IgnoreParenImpCasts();
2538 const Expr *PAE = PrevAllocator->IgnoreParenImpCasts();
2539 llvm::FoldingSetNodeID AEId, PAEId;
2540 AE->Profile(AEId, S.Context, /*Canonical=*/true);
2541 PAE->Profile(PAEId, S.Context, /*Canonical=*/true);
2542 AllocatorsMatch = AEId == PAEId;
2543 }
2544 if (!AllocatorsMatch) {
2545 SmallString<256> AllocatorBuffer;
2546 llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer);
2547 if (Allocator)
2548 Allocator->printPretty(AllocatorStream, nullptr, S.getPrintingPolicy());
2549 SmallString<256> PrevAllocatorBuffer;
2550 llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer);
2551 if (PrevAllocator)
2552 PrevAllocator->printPretty(PrevAllocatorStream, nullptr,
2553 S.getPrintingPolicy());
2554
2555 SourceLocation AllocatorLoc =
2556 Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc();
2557 SourceRange AllocatorRange =
2558 Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange();
2559 SourceLocation PrevAllocatorLoc =
2560 PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation();
2561 SourceRange PrevAllocatorRange =
2562 PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange();
2563 S.Diag(AllocatorLoc, diag::warn_omp_used_different_allocator)
2564 << (Allocator ? 1 : 0) << AllocatorStream.str()
2565 << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str()
2566 << AllocatorRange;
2567 S.Diag(PrevAllocatorLoc, diag::note_omp_previous_allocator)
2568 << PrevAllocatorRange;
2569 return true;
2570 }
2571 return false;
2572}
2573
2574static void
2575applyOMPAllocateAttribute(Sema &S, VarDecl *VD,
2576 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
2577 Expr *Allocator, SourceRange SR) {
2578 if (VD->hasAttr<OMPAllocateDeclAttr>())
2579 return;
2580 if (Allocator &&
2581 (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
2582 Allocator->isInstantiationDependent() ||
2583 Allocator->containsUnexpandedParameterPack()))
2584 return;
2585 auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind,
2586 Allocator, SR);
2587 VD->addAttr(A);
2588 if (ASTMutationListener *ML = S.Context.getASTMutationListener())
2589 ML->DeclarationMarkedOpenMPAllocate(VD, A);
2590}
2591
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002592Sema::DeclGroupPtrTy Sema::ActOnOpenMPAllocateDirective(
2593 SourceLocation Loc, ArrayRef<Expr *> VarList,
2594 ArrayRef<OMPClause *> Clauses, DeclContext *Owner) {
2595 assert(Clauses.size() <= 1 && "Expected at most one clause.");
2596 Expr *Allocator = nullptr;
Alexey Bataev2213dd62019-03-22 14:41:39 +00002597 if (Clauses.empty()) {
Alexey Bataevf4936072019-03-22 15:32:02 +00002598 // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions.
2599 // allocate directives that appear in a target region must specify an
2600 // allocator clause unless a requires directive with the dynamic_allocators
2601 // clause is present in the same compilation unit.
Alexey Bataev318f431b2019-03-22 15:25:12 +00002602 if (LangOpts.OpenMPIsDevice &&
2603 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
Alexey Bataev2213dd62019-03-22 14:41:39 +00002604 targetDiag(Loc, diag::err_expected_allocator_clause);
2605 } else {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002606 Allocator = cast<OMPAllocatorClause>(Clauses.back())->getAllocator();
Alexey Bataev2213dd62019-03-22 14:41:39 +00002607 }
Alexey Bataev27ef9512019-03-20 20:14:22 +00002608 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
2609 getAllocatorKind(*this, DSAStack, Allocator);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002610 SmallVector<Expr *, 8> Vars;
2611 for (Expr *RefExpr : VarList) {
2612 auto *DE = cast<DeclRefExpr>(RefExpr);
2613 auto *VD = cast<VarDecl>(DE->getDecl());
2614
2615 // Check if this is a TLS variable or global register.
2616 if (VD->getTLSKind() != VarDecl::TLS_None ||
2617 VD->hasAttr<OMPThreadPrivateDeclAttr>() ||
2618 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
2619 !VD->isLocalVarDecl()))
2620 continue;
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002621
Alexey Bataev282555a2019-03-19 20:33:44 +00002622 // If the used several times in the allocate directive, the same allocator
2623 // must be used.
Alexey Bataeve106f252019-04-01 14:25:31 +00002624 if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD,
2625 AllocatorKind, Allocator))
2626 continue;
Alexey Bataev282555a2019-03-19 20:33:44 +00002627
Alexey Bataevd2fc9652019-03-19 18:39:11 +00002628 // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++
2629 // If a list item has a static storage type, the allocator expression in the
2630 // allocator clause must be a constant expression that evaluates to one of
2631 // the predefined memory allocator values.
2632 if (Allocator && VD->hasGlobalStorage()) {
Alexey Bataev441510e2019-03-21 19:05:07 +00002633 if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) {
Alexey Bataevd2fc9652019-03-19 18:39:11 +00002634 Diag(Allocator->getExprLoc(),
2635 diag::err_omp_expected_predefined_allocator)
2636 << Allocator->getSourceRange();
2637 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2638 VarDecl::DeclarationOnly;
2639 Diag(VD->getLocation(),
2640 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2641 << VD;
2642 continue;
2643 }
2644 }
2645
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002646 Vars.push_back(RefExpr);
Alexey Bataeve106f252019-04-01 14:25:31 +00002647 applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator,
2648 DE->getSourceRange());
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002649 }
2650 if (Vars.empty())
2651 return nullptr;
2652 if (!Owner)
2653 Owner = getCurLexicalContext();
Alexey Bataeve106f252019-04-01 14:25:31 +00002654 auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002655 D->setAccess(AS_public);
2656 Owner->addDecl(D);
2657 return DeclGroupPtrTy::make(DeclGroupRef(D));
2658}
2659
2660Sema::DeclGroupPtrTy
Kelvin Li1408f912018-09-26 04:28:39 +00002661Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc,
2662 ArrayRef<OMPClause *> ClauseList) {
2663 OMPRequiresDecl *D = nullptr;
2664 if (!CurContext->isFileContext()) {
2665 Diag(Loc, diag::err_omp_invalid_scope) << "requires";
2666 } else {
2667 D = CheckOMPRequiresDecl(Loc, ClauseList);
2668 if (D) {
2669 CurContext->addDecl(D);
2670 DSAStack->addRequiresDecl(D);
2671 }
2672 }
2673 return DeclGroupPtrTy::make(DeclGroupRef(D));
2674}
2675
2676OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc,
2677 ArrayRef<OMPClause *> ClauseList) {
Gheorghe-Teodor Bercea411a6242019-04-18 19:53:43 +00002678 /// For target specific clauses, the requires directive cannot be
2679 /// specified after the handling of any of the target regions in the
2680 /// current compilation unit.
2681 ArrayRef<SourceLocation> TargetLocations =
2682 DSAStack->getEncounteredTargetLocs();
2683 if (!TargetLocations.empty()) {
2684 for (const OMPClause *CNew : ClauseList) {
2685 // Check if any of the requires clauses affect target regions.
2686 if (isa<OMPUnifiedSharedMemoryClause>(CNew) ||
2687 isa<OMPUnifiedAddressClause>(CNew) ||
2688 isa<OMPReverseOffloadClause>(CNew) ||
2689 isa<OMPDynamicAllocatorsClause>(CNew)) {
2690 Diag(Loc, diag::err_omp_target_before_requires)
2691 << getOpenMPClauseName(CNew->getClauseKind());
2692 for (SourceLocation TargetLoc : TargetLocations) {
2693 Diag(TargetLoc, diag::note_omp_requires_encountered_target);
2694 }
2695 }
2696 }
2697 }
2698
Kelvin Li1408f912018-09-26 04:28:39 +00002699 if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
2700 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc,
2701 ClauseList);
2702 return nullptr;
2703}
2704
Alexey Bataeve3727102018-04-18 15:57:46 +00002705static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
2706 const ValueDecl *D,
2707 const DSAStackTy::DSAVarData &DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00002708 bool IsLoopIterVar = false) {
2709 if (DVar.RefExpr) {
2710 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2711 << getOpenMPClauseName(DVar.CKind);
2712 return;
2713 }
2714 enum {
2715 PDSA_StaticMemberShared,
2716 PDSA_StaticLocalVarShared,
2717 PDSA_LoopIterVarPrivate,
2718 PDSA_LoopIterVarLinear,
2719 PDSA_LoopIterVarLastprivate,
2720 PDSA_ConstVarShared,
2721 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002722 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00002723 PDSA_LocalVarPrivate,
2724 PDSA_Implicit
2725 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002726 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002727 auto ReportLoc = D->getLocation();
2728 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002729 if (IsLoopIterVar) {
2730 if (DVar.CKind == OMPC_private)
2731 Reason = PDSA_LoopIterVarPrivate;
2732 else if (DVar.CKind == OMPC_lastprivate)
2733 Reason = PDSA_LoopIterVarLastprivate;
2734 else
2735 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00002736 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
2737 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002738 Reason = PDSA_TaskVarFirstprivate;
2739 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002740 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002741 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002742 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002743 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002744 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002745 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002746 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00002747 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002748 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00002749 ReportHint = true;
2750 Reason = PDSA_LocalVarPrivate;
2751 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00002752 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002753 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00002754 << Reason << ReportHint
2755 << getOpenMPDirectiveName(Stack->getCurrentDirective());
2756 } else if (DVar.ImplicitDSALoc.isValid()) {
2757 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
2758 << getOpenMPClauseName(DVar.CKind);
2759 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00002760}
2761
Alexey Bataev758e55e2013-09-06 18:03:48 +00002762namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00002763class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
Alexey Bataev758e55e2013-09-06 18:03:48 +00002764 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002765 Sema &SemaRef;
Alexey Bataeve3727102018-04-18 15:57:46 +00002766 bool ErrorFound = false;
2767 CapturedStmt *CS = nullptr;
2768 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
2769 llvm::SmallVector<Expr *, 4> ImplicitMap;
2770 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
2771 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
Alexey Bataeved09d242014-05-28 05:53:51 +00002772
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00002773 void VisitSubCaptures(OMPExecutableDirective *S) {
2774 // Check implicitly captured variables.
2775 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt())
2776 return;
Alexey Bataev1242d8f2019-06-28 20:45:14 +00002777 visitSubCaptures(S->getInnermostCapturedStmt());
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00002778 }
2779
Alexey Bataev758e55e2013-09-06 18:03:48 +00002780public:
2781 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00002782 if (E->isTypeDependent() || E->isValueDependent() ||
2783 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2784 return;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002785 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev412254a2019-05-09 18:44:53 +00002786 // Check the datasharing rules for the expressions in the clauses.
2787 if (!CS) {
2788 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
2789 if (!CED->hasAttr<OMPCaptureNoInitAttr>()) {
2790 Visit(CED->getInit());
2791 return;
2792 }
Alexey Bataev1242d8f2019-06-28 20:45:14 +00002793 } else if (VD->isImplicit() || isa<OMPCapturedExprDecl>(VD))
2794 // Do not analyze internal variables and do not enclose them into
2795 // implicit clauses.
2796 return;
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00002797 VD = VD->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002798 // Skip internally declared variables.
Alexey Bataev412254a2019-05-09 18:44:53 +00002799 if (VD->hasLocalStorage() && CS && !CS->capturesVariable(VD))
Alexey Bataeved09d242014-05-28 05:53:51 +00002800 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002801
Alexey Bataeve3727102018-04-18 15:57:46 +00002802 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002803 // Check if the variable has explicit DSA set and stop analysis if it so.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00002804 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
David Majnemer9d168222016-08-05 17:44:54 +00002805 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002806
Alexey Bataevafe50572017-10-06 17:00:28 +00002807 // Skip internally declared static variables.
Alexey Bataev92327c52018-03-26 16:40:55 +00002808 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
Alexey Bataev97b72212018-08-14 18:31:20 +00002809 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
Alexey Bataev412254a2019-05-09 18:44:53 +00002810 if (VD->hasGlobalStorage() && CS && !CS->capturesVariable(VD) &&
Gheorghe-Teodor Bercea5254f0a2019-06-14 17:58:26 +00002811 (Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
2812 !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link))
Alexey Bataevafe50572017-10-06 17:00:28 +00002813 return;
2814
Alexey Bataeve3727102018-04-18 15:57:46 +00002815 SourceLocation ELoc = E->getExprLoc();
2816 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002817 // The default(none) clause requires that each variable that is referenced
2818 // in the construct, and does not have a predetermined data-sharing
2819 // attribute, must have its data-sharing attribute explicitly determined
2820 // by being listed in a data-sharing attribute clause.
2821 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev7e6803e2019-01-09 15:58:05 +00002822 isImplicitOrExplicitTaskingRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002823 VarsWithInheritedDSA.count(VD) == 0) {
2824 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002825 return;
2826 }
2827
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002828 if (isOpenMPTargetExecutionDirective(DKind) &&
2829 !Stack->isLoopControlVariable(VD).first) {
2830 if (!Stack->checkMappableExprComponentListsForDecl(
2831 VD, /*CurrentRegionOnly=*/true,
2832 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2833 StackComponents,
2834 OpenMPClauseKind) {
2835 // Variable is used if it has been marked as an array, array
2836 // section or the variable iself.
2837 return StackComponents.size() == 1 ||
2838 std::all_of(
2839 std::next(StackComponents.rbegin()),
2840 StackComponents.rend(),
2841 [](const OMPClauseMappableExprCommon::
2842 MappableComponent &MC) {
2843 return MC.getAssociatedDeclaration() ==
2844 nullptr &&
2845 (isa<OMPArraySectionExpr>(
2846 MC.getAssociatedExpression()) ||
2847 isa<ArraySubscriptExpr>(
2848 MC.getAssociatedExpression()));
2849 });
2850 })) {
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002851 bool IsFirstprivate = false;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002852 // By default lambdas are captured as firstprivates.
2853 if (const auto *RD =
2854 VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002855 IsFirstprivate = RD->isLambda();
2856 IsFirstprivate =
2857 IsFirstprivate ||
2858 (VD->getType().getNonReferenceType()->isScalarType() &&
Alexey Bataev92327c52018-03-26 16:40:55 +00002859 Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res);
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002860 if (IsFirstprivate)
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002861 ImplicitFirstprivate.emplace_back(E);
2862 else
2863 ImplicitMap.emplace_back(E);
2864 return;
2865 }
2866 }
2867
Alexey Bataev758e55e2013-09-06 18:03:48 +00002868 // OpenMP [2.9.3.6, Restrictions, p.2]
2869 // A list item that appears in a reduction clause of the innermost
2870 // enclosing worksharing or parallel construct may not be accessed in an
2871 // explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002872 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002873 VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2874 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002875 return isOpenMPParallelDirective(K) ||
2876 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2877 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00002878 /*FromParent=*/true);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002879 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00002880 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002881 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002882 reportOriginalDsa(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002883 return;
2884 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002885
2886 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002887 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002888 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
Alexey Bataeva495c642019-03-11 19:51:42 +00002889 !Stack->isLoopControlVariable(VD).first) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002890 ImplicitFirstprivate.push_back(E);
Alexey Bataeva495c642019-03-11 19:51:42 +00002891 return;
2892 }
2893
2894 // Store implicitly used globals with declare target link for parent
2895 // target.
2896 if (!isOpenMPTargetExecutionDirective(DKind) && Res &&
2897 *Res == OMPDeclareTargetDeclAttr::MT_Link) {
2898 Stack->addToParentTargetRegionLinkGlobals(E);
2899 return;
2900 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002901 }
2902 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002903 void VisitMemberExpr(MemberExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00002904 if (E->isTypeDependent() || E->isValueDependent() ||
2905 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2906 return;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002907 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002908 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Patrick Lystere13b1e32019-01-02 19:28:48 +00002909 if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParens())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002910 if (!FD)
2911 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002912 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002913 // Check if the variable has explicit DSA set and stop analysis if it
2914 // so.
2915 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
2916 return;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002917
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002918 if (isOpenMPTargetExecutionDirective(DKind) &&
2919 !Stack->isLoopControlVariable(FD).first &&
2920 !Stack->checkMappableExprComponentListsForDecl(
2921 FD, /*CurrentRegionOnly=*/true,
2922 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2923 StackComponents,
2924 OpenMPClauseKind) {
2925 return isa<CXXThisExpr>(
2926 cast<MemberExpr>(
2927 StackComponents.back().getAssociatedExpression())
2928 ->getBase()
2929 ->IgnoreParens());
2930 })) {
2931 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
2932 // A bit-field cannot appear in a map clause.
2933 //
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002934 if (FD->isBitField())
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002935 return;
Patrick Lystere13b1e32019-01-02 19:28:48 +00002936
2937 // Check to see if the member expression is referencing a class that
2938 // has already been explicitly mapped
2939 if (Stack->isClassPreviouslyMapped(TE->getType()))
2940 return;
2941
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002942 ImplicitMap.emplace_back(E);
2943 return;
2944 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002945
Alexey Bataeve3727102018-04-18 15:57:46 +00002946 SourceLocation ELoc = E->getExprLoc();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002947 // OpenMP [2.9.3.6, Restrictions, p.2]
2948 // A list item that appears in a reduction clause of the innermost
2949 // enclosing worksharing or parallel construct may not be accessed in
2950 // an explicit task.
2951 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002952 FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2953 [](OpenMPDirectiveKind K) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002954 return isOpenMPParallelDirective(K) ||
2955 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2956 },
2957 /*FromParent=*/true);
2958 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2959 ErrorFound = true;
2960 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002961 reportOriginalDsa(SemaRef, Stack, FD, DVar);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002962 return;
2963 }
2964
2965 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002966 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002967 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
Alexey Bataevb40e05202018-10-24 18:53:12 +00002968 !Stack->isLoopControlVariable(FD).first) {
2969 // Check if there is a captured expression for the current field in the
2970 // region. Do not mark it as firstprivate unless there is no captured
2971 // expression.
2972 // TODO: try to make it firstprivate.
2973 if (DVar.CKind != OMPC_unknown)
2974 ImplicitFirstprivate.push_back(E);
2975 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002976 return;
2977 }
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002978 if (isOpenMPTargetExecutionDirective(DKind)) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002979 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
Alexey Bataeve3727102018-04-18 15:57:46 +00002980 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002981 /*NoDiagnose=*/true))
Alexey Bataev27041fa2017-12-05 15:22:49 +00002982 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002983 const auto *VD = cast<ValueDecl>(
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002984 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
2985 if (!Stack->checkMappableExprComponentListsForDecl(
2986 VD, /*CurrentRegionOnly=*/true,
2987 [&CurComponents](
2988 OMPClauseMappableExprCommon::MappableExprComponentListRef
2989 StackComponents,
2990 OpenMPClauseKind) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002991 auto CCI = CurComponents.rbegin();
Alexey Bataev5ec38932017-09-26 16:19:04 +00002992 auto CCE = CurComponents.rend();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002993 for (const auto &SC : llvm::reverse(StackComponents)) {
2994 // Do both expressions have the same kind?
2995 if (CCI->getAssociatedExpression()->getStmtClass() !=
2996 SC.getAssociatedExpression()->getStmtClass())
2997 if (!(isa<OMPArraySectionExpr>(
2998 SC.getAssociatedExpression()) &&
2999 isa<ArraySubscriptExpr>(
3000 CCI->getAssociatedExpression())))
3001 return false;
3002
Alexey Bataeve3727102018-04-18 15:57:46 +00003003 const Decl *CCD = CCI->getAssociatedDeclaration();
3004 const Decl *SCD = SC.getAssociatedDeclaration();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003005 CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
3006 SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
3007 if (SCD != CCD)
3008 return false;
3009 std::advance(CCI, 1);
Alexey Bataev5ec38932017-09-26 16:19:04 +00003010 if (CCI == CCE)
3011 break;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003012 }
3013 return true;
3014 })) {
3015 Visit(E->getBase());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003016 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003017 } else {
Alexey Bataev7fcacd82016-11-28 15:55:15 +00003018 Visit(E->getBase());
Alexey Bataeve3727102018-04-18 15:57:46 +00003019 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003020 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003021 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003022 for (OMPClause *C : S->clauses()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003023 // Skip analysis of arguments of implicitly defined firstprivate clause
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003024 // for task|target directives.
3025 // Skip analysis of arguments of implicitly defined map clause for target
3026 // directives.
3027 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
3028 C->isImplicit())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003029 for (Stmt *CC : C->children()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003030 if (CC)
3031 Visit(CC);
3032 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003033 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003034 }
Alexey Bataevf07946e2018-10-29 20:17:42 +00003035 // Check implicitly captured variables.
3036 VisitSubCaptures(S);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003037 }
3038 void VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003039 for (Stmt *C : S->children()) {
Alexey Bataev8fc7b5f2018-10-25 15:35:27 +00003040 if (C) {
Joel E. Denny0fdf5a92018-12-19 15:59:47 +00003041 // Check implicitly captured variables in the task-based directives to
3042 // check if they must be firstprivatized.
3043 Visit(C);
Alexey Bataev8fc7b5f2018-10-25 15:35:27 +00003044 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003045 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003046 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003047
Alexey Bataev1242d8f2019-06-28 20:45:14 +00003048 void visitSubCaptures(CapturedStmt *S) {
3049 for (const CapturedStmt::Capture &Cap : S->captures()) {
3050 if (!Cap.capturesVariable() && !Cap.capturesVariableByCopy())
3051 continue;
3052 VarDecl *VD = Cap.getCapturedVar();
3053 // Do not try to map the variable if it or its sub-component was mapped
3054 // already.
3055 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
3056 Stack->checkMappableExprComponentListsForDecl(
3057 VD, /*CurrentRegionOnly=*/true,
3058 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
3059 OpenMPClauseKind) { return true; }))
3060 continue;
3061 DeclRefExpr *DRE = buildDeclRefExpr(
3062 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context),
3063 Cap.getLocation(), /*RefersToCapture=*/true);
3064 Visit(DRE);
3065 }
3066 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003067 bool isErrorFound() const { return ErrorFound; }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003068 ArrayRef<Expr *> getImplicitFirstprivate() const {
3069 return ImplicitFirstprivate;
3070 }
3071 ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; }
Alexey Bataeve3727102018-04-18 15:57:46 +00003072 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003073 return VarsWithInheritedDSA;
3074 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003075
Alexey Bataev7ff55242014-06-19 09:13:45 +00003076 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
Alexey Bataeva495c642019-03-11 19:51:42 +00003077 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {
3078 // Process declare target link variables for the target directives.
3079 if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) {
3080 for (DeclRefExpr *E : Stack->getLinkGlobals())
3081 Visit(E);
3082 }
3083 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003084};
Alexey Bataeved09d242014-05-28 05:53:51 +00003085} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00003086
Alexey Bataevbae9a792014-06-27 10:37:06 +00003087void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00003088 switch (DKind) {
Kelvin Li70a12c52016-07-13 21:51:49 +00003089 case OMPD_parallel:
3090 case OMPD_parallel_for:
3091 case OMPD_parallel_for_simd:
3092 case OMPD_parallel_sections:
Carlo Bertolliba1487b2017-10-04 14:12:09 +00003093 case OMPD_teams:
Alexey Bataev999277a2017-12-06 14:31:09 +00003094 case OMPD_teams_distribute:
3095 case OMPD_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003096 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Alexey Bataev2377fe92015-09-10 08:12:02 +00003097 QualType KmpInt32PtrTy =
3098 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003099 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00003100 std::make_pair(".global_tid.", KmpInt32PtrTy),
3101 std::make_pair(".bound_tid.", KmpInt32PtrTy),
3102 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00003103 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00003104 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3105 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00003106 break;
3107 }
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00003108 case OMPD_target_teams:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00003109 case OMPD_target_parallel:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00003110 case OMPD_target_parallel_for:
Alexey Bataevdfa430f2017-12-08 15:03:50 +00003111 case OMPD_target_parallel_for_simd:
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00003112 case OMPD_target_teams_distribute:
3113 case OMPD_target_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003114 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3115 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3116 QualType KmpInt32PtrTy =
3117 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3118 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00003119 FunctionProtoType::ExtProtoInfo EPI;
3120 EPI.Variadic = true;
3121 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3122 Sema::CapturedParamNameType Params[] = {
3123 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003124 std::make_pair(".part_id.", KmpInt32PtrTy),
3125 std::make_pair(".privates.", VoidPtrTy),
3126 std::make_pair(
3127 ".copy_fn.",
3128 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00003129 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3130 std::make_pair(StringRef(), QualType()) // __context with shared vars
3131 };
3132 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Joel E. Denny7d5bc552019-08-22 03:34:30 +00003133 Params, /*OpenMPCaptureLevel=*/0);
Alexey Bataev0c869ef2018-01-16 15:57:07 +00003134 // Mark this captured region as inlined, because we don't use outlined
3135 // function directly.
3136 getCurCapturedRegion()->TheCapturedDecl->addAttr(
3137 AlwaysInlineAttr::CreateImplicit(
Erich Keane6a24e802019-09-13 17:39:31 +00003138 Context, {}, AttributeCommonInfo::AS_Keyword,
3139 AlwaysInlineAttr::Keyword_forceinline));
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003140 Sema::CapturedParamNameType ParamsTarget[] = {
3141 std::make_pair(StringRef(), QualType()) // __context with shared vars
3142 };
3143 // Start a captured region for 'target' with no implicit parameters.
3144 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Joel E. Denny7d5bc552019-08-22 03:34:30 +00003145 ParamsTarget, /*OpenMPCaptureLevel=*/1);
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00003146 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003147 std::make_pair(".global_tid.", KmpInt32PtrTy),
3148 std::make_pair(".bound_tid.", KmpInt32PtrTy),
3149 std::make_pair(StringRef(), QualType()) // __context with shared vars
3150 };
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00003151 // Start a captured region for 'teams' or 'parallel'. Both regions have
3152 // the same implicit parameters.
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003153 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Joel E. Denny7d5bc552019-08-22 03:34:30 +00003154 ParamsTeamsOrParallel, /*OpenMPCaptureLevel=*/2);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003155 break;
3156 }
Alexey Bataev8451efa2018-01-15 19:06:12 +00003157 case OMPD_target:
3158 case OMPD_target_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003159 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3160 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3161 QualType KmpInt32PtrTy =
3162 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3163 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00003164 FunctionProtoType::ExtProtoInfo EPI;
3165 EPI.Variadic = true;
3166 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3167 Sema::CapturedParamNameType Params[] = {
3168 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003169 std::make_pair(".part_id.", KmpInt32PtrTy),
3170 std::make_pair(".privates.", VoidPtrTy),
3171 std::make_pair(
3172 ".copy_fn.",
3173 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00003174 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3175 std::make_pair(StringRef(), QualType()) // __context with shared vars
3176 };
3177 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Joel E. Denny7d5bc552019-08-22 03:34:30 +00003178 Params, /*OpenMPCaptureLevel=*/0);
Alexey Bataev8451efa2018-01-15 19:06:12 +00003179 // Mark this captured region as inlined, because we don't use outlined
3180 // function directly.
3181 getCurCapturedRegion()->TheCapturedDecl->addAttr(
3182 AlwaysInlineAttr::CreateImplicit(
Erich Keane6a24e802019-09-13 17:39:31 +00003183 Context, {}, AttributeCommonInfo::AS_Keyword,
3184 AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev8451efa2018-01-15 19:06:12 +00003185 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Joel E. Denny7d5bc552019-08-22 03:34:30 +00003186 std::make_pair(StringRef(), QualType()),
3187 /*OpenMPCaptureLevel=*/1);
Alexey Bataev8451efa2018-01-15 19:06:12 +00003188 break;
3189 }
Kelvin Li70a12c52016-07-13 21:51:49 +00003190 case OMPD_simd:
3191 case OMPD_for:
3192 case OMPD_for_simd:
3193 case OMPD_sections:
3194 case OMPD_section:
3195 case OMPD_single:
3196 case OMPD_master:
3197 case OMPD_critical:
Kelvin Lia579b912016-07-14 02:54:56 +00003198 case OMPD_taskgroup:
3199 case OMPD_distribute:
Alexey Bataev46506272017-12-05 17:41:34 +00003200 case OMPD_distribute_simd:
Kelvin Li70a12c52016-07-13 21:51:49 +00003201 case OMPD_ordered:
3202 case OMPD_atomic:
Alexey Bataev8451efa2018-01-15 19:06:12 +00003203 case OMPD_target_data: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003204 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00003205 std::make_pair(StringRef(), QualType()) // __context with shared vars
3206 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00003207 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3208 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003209 break;
3210 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003211 case OMPD_task: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003212 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3213 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3214 QualType KmpInt32PtrTy =
3215 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3216 QualType Args[] = {VoidPtrTy};
Alexey Bataev3ae88e22015-05-22 08:56:35 +00003217 FunctionProtoType::ExtProtoInfo EPI;
3218 EPI.Variadic = true;
3219 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003220 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00003221 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003222 std::make_pair(".part_id.", KmpInt32PtrTy),
3223 std::make_pair(".privates.", VoidPtrTy),
3224 std::make_pair(
3225 ".copy_fn.",
3226 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev48591dd2016-04-20 04:01:36 +00003227 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003228 std::make_pair(StringRef(), QualType()) // __context with shared vars
3229 };
3230 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3231 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00003232 // Mark this captured region as inlined, because we don't use outlined
3233 // function directly.
3234 getCurCapturedRegion()->TheCapturedDecl->addAttr(
3235 AlwaysInlineAttr::CreateImplicit(
Erich Keane6a24e802019-09-13 17:39:31 +00003236 Context, {}, AttributeCommonInfo::AS_Keyword,
3237 AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003238 break;
3239 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00003240 case OMPD_taskloop:
Alexey Bataev60e51c42019-10-10 20:13:02 +00003241 case OMPD_taskloop_simd:
3242 case OMPD_master_taskloop: {
Alexey Bataev7292c292016-04-25 12:22:29 +00003243 QualType KmpInt32Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003244 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
3245 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00003246 QualType KmpUInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003247 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
3248 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00003249 QualType KmpInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003250 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
3251 .withConst();
3252 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3253 QualType KmpInt32PtrTy =
3254 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3255 QualType Args[] = {VoidPtrTy};
Alexey Bataev7292c292016-04-25 12:22:29 +00003256 FunctionProtoType::ExtProtoInfo EPI;
3257 EPI.Variadic = true;
3258 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00003259 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00003260 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003261 std::make_pair(".part_id.", KmpInt32PtrTy),
3262 std::make_pair(".privates.", VoidPtrTy),
Alexey Bataev7292c292016-04-25 12:22:29 +00003263 std::make_pair(
3264 ".copy_fn.",
3265 Context.getPointerType(CopyFnType).withConst().withRestrict()),
3266 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3267 std::make_pair(".lb.", KmpUInt64Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003268 std::make_pair(".ub.", KmpUInt64Ty),
3269 std::make_pair(".st.", KmpInt64Ty),
Alexey Bataev7292c292016-04-25 12:22:29 +00003270 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003271 std::make_pair(".reductions.", VoidPtrTy),
Alexey Bataev49f6e782015-12-01 04:18:41 +00003272 std::make_pair(StringRef(), QualType()) // __context with shared vars
3273 };
3274 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3275 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00003276 // Mark this captured region as inlined, because we don't use outlined
3277 // function directly.
3278 getCurCapturedRegion()->TheCapturedDecl->addAttr(
3279 AlwaysInlineAttr::CreateImplicit(
Erich Keane6a24e802019-09-13 17:39:31 +00003280 Context, {}, AttributeCommonInfo::AS_Keyword,
3281 AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev49f6e782015-12-01 04:18:41 +00003282 break;
3283 }
Alexey Bataev5bbcead2019-10-14 17:17:41 +00003284 case OMPD_parallel_master_taskloop: {
3285 QualType KmpInt32Ty =
3286 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
3287 .withConst();
3288 QualType KmpUInt64Ty =
3289 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
3290 .withConst();
3291 QualType KmpInt64Ty =
3292 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
3293 .withConst();
3294 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3295 QualType KmpInt32PtrTy =
3296 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3297 Sema::CapturedParamNameType ParamsParallel[] = {
3298 std::make_pair(".global_tid.", KmpInt32PtrTy),
3299 std::make_pair(".bound_tid.", KmpInt32PtrTy),
3300 std::make_pair(StringRef(), QualType()) // __context with shared vars
3301 };
3302 // Start a captured region for 'parallel'.
3303 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3304 ParamsParallel, /*OpenMPCaptureLevel=*/1);
3305 QualType Args[] = {VoidPtrTy};
3306 FunctionProtoType::ExtProtoInfo EPI;
3307 EPI.Variadic = true;
3308 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3309 Sema::CapturedParamNameType Params[] = {
3310 std::make_pair(".global_tid.", KmpInt32Ty),
3311 std::make_pair(".part_id.", KmpInt32PtrTy),
3312 std::make_pair(".privates.", VoidPtrTy),
3313 std::make_pair(
3314 ".copy_fn.",
3315 Context.getPointerType(CopyFnType).withConst().withRestrict()),
3316 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3317 std::make_pair(".lb.", KmpUInt64Ty),
3318 std::make_pair(".ub.", KmpUInt64Ty),
3319 std::make_pair(".st.", KmpInt64Ty),
3320 std::make_pair(".liter.", KmpInt32Ty),
3321 std::make_pair(".reductions.", VoidPtrTy),
3322 std::make_pair(StringRef(), QualType()) // __context with shared vars
3323 };
3324 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3325 Params, /*OpenMPCaptureLevel=*/2);
3326 // Mark this captured region as inlined, because we don't use outlined
3327 // function directly.
3328 getCurCapturedRegion()->TheCapturedDecl->addAttr(
3329 AlwaysInlineAttr::CreateImplicit(
3330 Context, {}, AttributeCommonInfo::AS_Keyword,
3331 AlwaysInlineAttr::Keyword_forceinline));
3332 break;
3333 }
Kelvin Li4a39add2016-07-05 05:00:15 +00003334 case OMPD_distribute_parallel_for_simd:
Alexey Bataev647dd842018-01-15 20:59:40 +00003335 case OMPD_distribute_parallel_for: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003336 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli9925f152016-06-27 14:55:37 +00003337 QualType KmpInt32PtrTy =
3338 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3339 Sema::CapturedParamNameType Params[] = {
3340 std::make_pair(".global_tid.", KmpInt32PtrTy),
3341 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003342 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
3343 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli9925f152016-06-27 14:55:37 +00003344 std::make_pair(StringRef(), QualType()) // __context with shared vars
3345 };
3346 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3347 Params);
3348 break;
3349 }
Alexey Bataev647dd842018-01-15 20:59:40 +00003350 case OMPD_target_teams_distribute_parallel_for:
3351 case OMPD_target_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003352 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli52978c32018-01-03 21:12:44 +00003353 QualType KmpInt32PtrTy =
3354 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003355 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
Carlo Bertolli52978c32018-01-03 21:12:44 +00003356
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003357 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00003358 FunctionProtoType::ExtProtoInfo EPI;
3359 EPI.Variadic = true;
3360 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3361 Sema::CapturedParamNameType Params[] = {
3362 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003363 std::make_pair(".part_id.", KmpInt32PtrTy),
3364 std::make_pair(".privates.", VoidPtrTy),
3365 std::make_pair(
3366 ".copy_fn.",
3367 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00003368 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3369 std::make_pair(StringRef(), QualType()) // __context with shared vars
3370 };
3371 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Joel E. Denny7d5bc552019-08-22 03:34:30 +00003372 Params, /*OpenMPCaptureLevel=*/0);
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00003373 // Mark this captured region as inlined, because we don't use outlined
3374 // function directly.
3375 getCurCapturedRegion()->TheCapturedDecl->addAttr(
3376 AlwaysInlineAttr::CreateImplicit(
Erich Keane6a24e802019-09-13 17:39:31 +00003377 Context, {}, AttributeCommonInfo::AS_Keyword,
3378 AlwaysInlineAttr::Keyword_forceinline));
Carlo Bertolli52978c32018-01-03 21:12:44 +00003379 Sema::CapturedParamNameType ParamsTarget[] = {
3380 std::make_pair(StringRef(), QualType()) // __context with shared vars
3381 };
3382 // Start a captured region for 'target' with no implicit parameters.
3383 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Joel E. Denny7d5bc552019-08-22 03:34:30 +00003384 ParamsTarget, /*OpenMPCaptureLevel=*/1);
Carlo Bertolli52978c32018-01-03 21:12:44 +00003385
3386 Sema::CapturedParamNameType ParamsTeams[] = {
3387 std::make_pair(".global_tid.", KmpInt32PtrTy),
3388 std::make_pair(".bound_tid.", KmpInt32PtrTy),
3389 std::make_pair(StringRef(), QualType()) // __context with shared vars
3390 };
3391 // Start a captured region for 'target' with no implicit parameters.
3392 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Joel E. Denny7d5bc552019-08-22 03:34:30 +00003393 ParamsTeams, /*OpenMPCaptureLevel=*/2);
Carlo Bertolli52978c32018-01-03 21:12:44 +00003394
3395 Sema::CapturedParamNameType ParamsParallel[] = {
3396 std::make_pair(".global_tid.", KmpInt32PtrTy),
3397 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003398 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
3399 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli52978c32018-01-03 21:12:44 +00003400 std::make_pair(StringRef(), QualType()) // __context with shared vars
3401 };
3402 // Start a captured region for 'teams' or 'parallel'. Both regions have
3403 // the same implicit parameters.
3404 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Joel E. Denny7d5bc552019-08-22 03:34:30 +00003405 ParamsParallel, /*OpenMPCaptureLevel=*/3);
Carlo Bertolli52978c32018-01-03 21:12:44 +00003406 break;
3407 }
3408
Alexey Bataev46506272017-12-05 17:41:34 +00003409 case OMPD_teams_distribute_parallel_for:
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00003410 case OMPD_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003411 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli62fae152017-11-20 20:46:39 +00003412 QualType KmpInt32PtrTy =
3413 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3414
3415 Sema::CapturedParamNameType ParamsTeams[] = {
3416 std::make_pair(".global_tid.", KmpInt32PtrTy),
3417 std::make_pair(".bound_tid.", KmpInt32PtrTy),
3418 std::make_pair(StringRef(), QualType()) // __context with shared vars
3419 };
3420 // Start a captured region for 'target' with no implicit parameters.
3421 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Joel E. Denny7d5bc552019-08-22 03:34:30 +00003422 ParamsTeams, /*OpenMPCaptureLevel=*/0);
Carlo Bertolli62fae152017-11-20 20:46:39 +00003423
3424 Sema::CapturedParamNameType ParamsParallel[] = {
3425 std::make_pair(".global_tid.", KmpInt32PtrTy),
3426 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003427 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
3428 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli62fae152017-11-20 20:46:39 +00003429 std::make_pair(StringRef(), QualType()) // __context with shared vars
3430 };
3431 // Start a captured region for 'teams' or 'parallel'. Both regions have
3432 // the same implicit parameters.
3433 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Joel E. Denny7d5bc552019-08-22 03:34:30 +00003434 ParamsParallel, /*OpenMPCaptureLevel=*/1);
Carlo Bertolli62fae152017-11-20 20:46:39 +00003435 break;
3436 }
Alexey Bataev7828b252017-11-21 17:08:48 +00003437 case OMPD_target_update:
3438 case OMPD_target_enter_data:
3439 case OMPD_target_exit_data: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003440 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3441 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3442 QualType KmpInt32PtrTy =
3443 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3444 QualType Args[] = {VoidPtrTy};
Alexey Bataev7828b252017-11-21 17:08:48 +00003445 FunctionProtoType::ExtProtoInfo EPI;
3446 EPI.Variadic = true;
3447 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3448 Sema::CapturedParamNameType Params[] = {
3449 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003450 std::make_pair(".part_id.", KmpInt32PtrTy),
3451 std::make_pair(".privates.", VoidPtrTy),
3452 std::make_pair(
3453 ".copy_fn.",
3454 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev7828b252017-11-21 17:08:48 +00003455 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3456 std::make_pair(StringRef(), QualType()) // __context with shared vars
3457 };
3458 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3459 Params);
3460 // Mark this captured region as inlined, because we don't use outlined
3461 // function directly.
3462 getCurCapturedRegion()->TheCapturedDecl->addAttr(
3463 AlwaysInlineAttr::CreateImplicit(
Erich Keane6a24e802019-09-13 17:39:31 +00003464 Context, {}, AttributeCommonInfo::AS_Keyword,
3465 AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev7828b252017-11-21 17:08:48 +00003466 break;
3467 }
Alexey Bataev9959db52014-05-06 10:08:46 +00003468 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00003469 case OMPD_allocate:
Alexey Bataevee9af452014-11-21 11:33:46 +00003470 case OMPD_taskyield:
3471 case OMPD_barrier:
3472 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003473 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00003474 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00003475 case OMPD_flush:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003476 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +00003477 case OMPD_declare_mapper:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003478 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003479 case OMPD_declare_target:
3480 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00003481 case OMPD_requires:
Alexey Bataevd158cf62019-09-13 20:18:17 +00003482 case OMPD_declare_variant:
Alexey Bataev9959db52014-05-06 10:08:46 +00003483 llvm_unreachable("OpenMP Directive is not allowed");
3484 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00003485 llvm_unreachable("Unknown OpenMP directive");
3486 }
3487}
3488
Alexey Bataev0e100032019-10-14 16:44:01 +00003489int Sema::getNumberOfConstructScopes(unsigned Level) const {
3490 return getOpenMPCaptureLevels(DSAStack->getDirective(Level));
3491}
3492
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003493int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
3494 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
3495 getOpenMPCaptureRegions(CaptureRegions, DKind);
3496 return CaptureRegions.size();
3497}
3498
Alexey Bataev3392d762016-02-16 11:18:12 +00003499static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00003500 Expr *CaptureExpr, bool WithInit,
3501 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00003502 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00003503 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00003504 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00003505 QualType Ty = Init->getType();
3506 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00003507 if (S.getLangOpts().CPlusPlus) {
Alexey Bataev4244be22016-02-11 05:35:55 +00003508 Ty = C.getLValueReferenceType(Ty);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00003509 } else {
Alexey Bataev4244be22016-02-11 05:35:55 +00003510 Ty = C.getPointerType(Ty);
3511 ExprResult Res =
3512 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
3513 if (!Res.isUsable())
3514 return nullptr;
3515 Init = Res.get();
3516 }
Alexey Bataev61205072016-03-02 04:57:40 +00003517 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00003518 }
Alexey Bataeva7206b92016-12-20 16:51:02 +00003519 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003520 CaptureExpr->getBeginLoc());
Alexey Bataev2bbf7212016-03-03 03:52:24 +00003521 if (!WithInit)
Alexey Bataeve3727102018-04-18 15:57:46 +00003522 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
Alexey Bataev4244be22016-02-11 05:35:55 +00003523 S.CurContext->addHiddenDecl(CED);
Richard Smith3beb7c62017-01-12 02:27:38 +00003524 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00003525 return CED;
3526}
3527
Alexey Bataev61205072016-03-02 04:57:40 +00003528static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
3529 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00003530 OMPCapturedExprDecl *CD;
Alexey Bataeve3727102018-04-18 15:57:46 +00003531 if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
Alexey Bataevb7a34b62016-02-25 03:59:29 +00003532 CD = cast<OMPCapturedExprDecl>(VD);
Alexey Bataeve3727102018-04-18 15:57:46 +00003533 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00003534 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
3535 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00003536 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00003537 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00003538}
3539
Alexey Bataev5a3af132016-03-29 08:58:54 +00003540static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00003541 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00003542 if (!Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00003543 OMPCapturedExprDecl *CD = buildCaptureDecl(
3544 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
3545 /*WithInit=*/true, /*AsExpression=*/true);
Alexey Bataev5a3af132016-03-29 08:58:54 +00003546 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
3547 CaptureExpr->getExprLoc());
3548 }
3549 ExprResult Res = Ref;
3550 if (!S.getLangOpts().CPlusPlus &&
3551 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
Alexey Bataev8e769ee2017-12-22 21:01:52 +00003552 Ref->getType()->isPointerType()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00003553 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00003554 if (!Res.isUsable())
3555 return ExprError();
3556 }
3557 return S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00003558}
3559
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003560namespace {
3561// OpenMP directives parsed in this section are represented as a
3562// CapturedStatement with an associated statement. If a syntax error
3563// is detected during the parsing of the associated statement, the
3564// compiler must abort processing and close the CapturedStatement.
3565//
3566// Combined directives such as 'target parallel' have more than one
3567// nested CapturedStatements. This RAII ensures that we unwind out
3568// of all the nested CapturedStatements when an error is found.
3569class CaptureRegionUnwinderRAII {
3570private:
3571 Sema &S;
3572 bool &ErrorFound;
Alexey Bataeve3727102018-04-18 15:57:46 +00003573 OpenMPDirectiveKind DKind = OMPD_unknown;
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003574
3575public:
3576 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
3577 OpenMPDirectiveKind DKind)
3578 : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
3579 ~CaptureRegionUnwinderRAII() {
3580 if (ErrorFound) {
3581 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
3582 while (--ThisCaptureLevel >= 0)
3583 S.ActOnCapturedRegionError();
3584 }
3585 }
3586};
3587} // namespace
3588
Alexey Bataevb600ae32019-07-01 17:46:52 +00003589void Sema::tryCaptureOpenMPLambdas(ValueDecl *V) {
3590 // Capture variables captured by reference in lambdas for target-based
3591 // directives.
3592 if (!CurContext->isDependentContext() &&
3593 (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) ||
3594 isOpenMPTargetDataManagementDirective(
3595 DSAStack->getCurrentDirective()))) {
3596 QualType Type = V->getType();
3597 if (const auto *RD = Type.getCanonicalType()
3598 .getNonReferenceType()
3599 ->getAsCXXRecordDecl()) {
3600 bool SavedForceCaptureByReferenceInTargetExecutable =
3601 DSAStack->isForceCaptureByReferenceInTargetExecutable();
3602 DSAStack->setForceCaptureByReferenceInTargetExecutable(
3603 /*V=*/true);
3604 if (RD->isLambda()) {
3605 llvm::DenseMap<const VarDecl *, FieldDecl *> Captures;
3606 FieldDecl *ThisCapture;
3607 RD->getCaptureFields(Captures, ThisCapture);
3608 for (const LambdaCapture &LC : RD->captures()) {
3609 if (LC.getCaptureKind() == LCK_ByRef) {
3610 VarDecl *VD = LC.getCapturedVar();
3611 DeclContext *VDC = VD->getDeclContext();
3612 if (!VDC->Encloses(CurContext))
3613 continue;
3614 MarkVariableReferenced(LC.getLocation(), VD);
3615 } else if (LC.getCaptureKind() == LCK_This) {
3616 QualType ThisTy = getCurrentThisType();
3617 if (!ThisTy.isNull() &&
3618 Context.typesAreCompatible(ThisTy, ThisCapture->getType()))
3619 CheckCXXThisCapture(LC.getLocation());
3620 }
3621 }
3622 }
3623 DSAStack->setForceCaptureByReferenceInTargetExecutable(
3624 SavedForceCaptureByReferenceInTargetExecutable);
3625 }
3626 }
3627}
3628
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003629StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
3630 ArrayRef<OMPClause *> Clauses) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003631 bool ErrorFound = false;
3632 CaptureRegionUnwinderRAII CaptureRegionUnwinder(
3633 *this, ErrorFound, DSAStack->getCurrentDirective());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003634 if (!S.isUsable()) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003635 ErrorFound = true;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003636 return StmtError();
3637 }
Alexey Bataev993d2802015-12-28 06:23:08 +00003638
Alexey Bataev2ba67042017-11-28 21:11:44 +00003639 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
3640 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
Alexey Bataev993d2802015-12-28 06:23:08 +00003641 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00003642 OMPScheduleClause *SC = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00003643 SmallVector<const OMPLinearClause *, 4> LCs;
3644 SmallVector<const OMPClauseWithPreInit *, 4> PICs;
Alexey Bataev040d5402015-05-12 08:35:28 +00003645 // This is required for proper codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003646 for (OMPClause *Clause : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00003647 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
3648 Clause->getClauseKind() == OMPC_in_reduction) {
3649 // Capture taskgroup task_reduction descriptors inside the tasking regions
3650 // with the corresponding in_reduction items.
3651 auto *IRC = cast<OMPInReductionClause>(Clause);
Alexey Bataeve3727102018-04-18 15:57:46 +00003652 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00003653 if (E)
3654 MarkDeclarationsReferencedInExpr(E);
3655 }
Alexey Bataev16dc7b62015-05-20 03:46:04 +00003656 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00003657 Clause->getClauseKind() == OMPC_copyprivate ||
3658 (getLangOpts().OpenMPUseTLS &&
3659 getASTContext().getTargetInfo().isTLSSupported() &&
3660 Clause->getClauseKind() == OMPC_copyin)) {
3661 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00003662 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeve3727102018-04-18 15:57:46 +00003663 for (Stmt *VarRef : Clause->children()) {
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003664 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00003665 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003666 }
3667 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00003668 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev2ba67042017-11-28 21:11:44 +00003669 } else if (CaptureRegions.size() > 1 ||
3670 CaptureRegions.back() != OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003671 if (auto *C = OMPClauseWithPreInit::get(Clause))
3672 PICs.push_back(C);
Alexey Bataev005248a2016-02-25 05:25:57 +00003673 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003674 if (Expr *E = C->getPostUpdateExpr())
Alexey Bataev005248a2016-02-25 05:25:57 +00003675 MarkDeclarationsReferencedInExpr(E);
3676 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003677 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00003678 if (Clause->getClauseKind() == OMPC_schedule)
3679 SC = cast<OMPScheduleClause>(Clause);
3680 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00003681 OC = cast<OMPOrderedClause>(Clause);
3682 else if (Clause->getClauseKind() == OMPC_linear)
3683 LCs.push_back(cast<OMPLinearClause>(Clause));
3684 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00003685 // OpenMP, 2.7.1 Loop Construct, Restrictions
3686 // The nonmonotonic modifier cannot be specified if an ordered clause is
3687 // specified.
3688 if (SC &&
3689 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
3690 SC->getSecondScheduleModifier() ==
3691 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
3692 OC) {
3693 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
3694 ? SC->getFirstScheduleModifierLoc()
3695 : SC->getSecondScheduleModifierLoc(),
3696 diag::err_omp_schedule_nonmonotonic_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00003697 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev6402bca2015-12-28 07:25:51 +00003698 ErrorFound = true;
3699 }
Alexey Bataev993d2802015-12-28 06:23:08 +00003700 if (!LCs.empty() && OC && OC->getNumForLoops()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003701 for (const OMPLinearClause *C : LCs) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003702 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00003703 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev993d2802015-12-28 06:23:08 +00003704 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00003705 ErrorFound = true;
3706 }
Alexey Bataev113438c2015-12-30 12:06:23 +00003707 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
3708 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
3709 OC->getNumForLoops()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003710 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
Alexey Bataev113438c2015-12-30 12:06:23 +00003711 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
3712 ErrorFound = true;
3713 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00003714 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00003715 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003716 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003717 StmtResult SR = S;
Richard Smith0621a8f2019-05-31 00:45:10 +00003718 unsigned CompletedRegions = 0;
Alexey Bataev2ba67042017-11-28 21:11:44 +00003719 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003720 // Mark all variables in private list clauses as used in inner region.
3721 // Required for proper codegen of combined directives.
3722 // TODO: add processing for other clauses.
Alexey Bataev2ba67042017-11-28 21:11:44 +00003723 if (ThisCaptureRegion != OMPD_unknown) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003724 for (const clang::OMPClauseWithPreInit *C : PICs) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003725 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
3726 // Find the particular capture region for the clause if the
3727 // directive is a combined one with multiple capture regions.
3728 // If the directive is not a combined one, the capture region
3729 // associated with the clause is OMPD_unknown and is generated
3730 // only once.
3731 if (CaptureRegion == ThisCaptureRegion ||
3732 CaptureRegion == OMPD_unknown) {
3733 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003734 for (Decl *D : DS->decls())
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003735 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
3736 }
3737 }
3738 }
3739 }
Richard Smith0621a8f2019-05-31 00:45:10 +00003740 if (++CompletedRegions == CaptureRegions.size())
3741 DSAStack->setBodyComplete();
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003742 SR = ActOnCapturedRegionEnd(SR.get());
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003743 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003744 return SR;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003745}
3746
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00003747static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
3748 OpenMPDirectiveKind CancelRegion,
3749 SourceLocation StartLoc) {
3750 // CancelRegion is only needed for cancel and cancellation_point.
3751 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
3752 return false;
3753
3754 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
3755 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
3756 return false;
3757
3758 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
3759 << getOpenMPDirectiveName(CancelRegion);
3760 return true;
3761}
3762
Alexey Bataeve3727102018-04-18 15:57:46 +00003763static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003764 OpenMPDirectiveKind CurrentRegion,
3765 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003766 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003767 SourceLocation StartLoc) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003768 if (Stack->getCurScope()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003769 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
3770 OpenMPDirectiveKind OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003771 bool NestingProhibited = false;
3772 bool CloseNesting = true;
David Majnemer9d168222016-08-05 17:44:54 +00003773 bool OrphanSeen = false;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003774 enum {
3775 NoRecommend,
3776 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00003777 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003778 ShouldBeInTargetRegion,
3779 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003780 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00003781 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003782 // OpenMP [2.16, Nesting of Regions]
3783 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003784 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00003785 // An ordered construct with the simd clause is the only OpenMP
3786 // construct that can appear in the simd region.
David Majnemer9d168222016-08-05 17:44:54 +00003787 // Allowing a SIMD construct nested in another SIMD construct is an
Kelvin Lifd8b5742016-07-01 14:30:25 +00003788 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3789 // message.
3790 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3791 ? diag::err_omp_prohibited_region_simd
3792 : diag::warn_omp_nesting_simd);
3793 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00003794 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003795 if (ParentRegion == OMPD_atomic) {
3796 // OpenMP [2.16, Nesting of Regions]
3797 // OpenMP constructs may not be nested inside an atomic region.
3798 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3799 return true;
3800 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003801 if (CurrentRegion == OMPD_section) {
3802 // OpenMP [2.7.2, sections Construct, Restrictions]
3803 // Orphaned section directives are prohibited. That is, the section
3804 // directives must appear within the sections construct and must not be
3805 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003806 if (ParentRegion != OMPD_sections &&
3807 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003808 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3809 << (ParentRegion != OMPD_unknown)
3810 << getOpenMPDirectiveName(ParentRegion);
3811 return true;
3812 }
3813 return false;
3814 }
Alexey Bataev185e88d2019-01-08 15:53:42 +00003815 // Allow some constructs (except teams and cancellation constructs) to be
3816 // orphaned (they could be used in functions, called from OpenMP regions
3817 // with the required preconditions).
Kelvin Libf594a52016-12-17 05:48:59 +00003818 if (ParentRegion == OMPD_unknown &&
Alexey Bataev185e88d2019-01-08 15:53:42 +00003819 !isOpenMPNestingTeamsDirective(CurrentRegion) &&
3820 CurrentRegion != OMPD_cancellation_point &&
3821 CurrentRegion != OMPD_cancel)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003822 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00003823 if (CurrentRegion == OMPD_cancellation_point ||
3824 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003825 // OpenMP [2.16, Nesting of Regions]
3826 // A cancellation point construct for which construct-type-clause is
3827 // taskgroup must be nested inside a task construct. A cancellation
3828 // point construct for which construct-type-clause is not taskgroup must
3829 // be closely nested inside an OpenMP construct that matches the type
3830 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00003831 // A cancel construct for which construct-type-clause is taskgroup must be
3832 // nested inside a task construct. A cancel construct for which
3833 // construct-type-clause is not taskgroup must be closely nested inside an
3834 // OpenMP construct that matches the type specified in
3835 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003836 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003837 !((CancelRegion == OMPD_parallel &&
3838 (ParentRegion == OMPD_parallel ||
3839 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00003840 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003841 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00003842 ParentRegion == OMPD_target_parallel_for ||
3843 ParentRegion == OMPD_distribute_parallel_for ||
Alexey Bataev16e79882017-11-22 21:12:03 +00003844 ParentRegion == OMPD_teams_distribute_parallel_for ||
3845 ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003846 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3847 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00003848 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3849 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev185e88d2019-01-08 15:53:42 +00003850 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003851 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00003852 // OpenMP [2.16, Nesting of Regions]
3853 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003854 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00003855 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003856 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003857 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3858 // OpenMP [2.16, Nesting of Regions]
3859 // A critical region may not be nested (closely or otherwise) inside a
3860 // critical region with the same name. Note that this restriction is not
3861 // sufficient to prevent deadlock.
3862 SourceLocation PreviousCriticalLoc;
David Majnemer9d168222016-08-05 17:44:54 +00003863 bool DeadLock = Stack->hasDirective(
3864 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
3865 const DeclarationNameInfo &DNI,
Alexey Bataeve3727102018-04-18 15:57:46 +00003866 SourceLocation Loc) {
David Majnemer9d168222016-08-05 17:44:54 +00003867 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
3868 PreviousCriticalLoc = Loc;
3869 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003870 }
3871 return false;
David Majnemer9d168222016-08-05 17:44:54 +00003872 },
3873 false /* skip top directive */);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003874 if (DeadLock) {
3875 SemaRef.Diag(StartLoc,
3876 diag::err_omp_prohibited_region_critical_same_name)
3877 << CurrentName.getName();
3878 if (PreviousCriticalLoc.isValid())
3879 SemaRef.Diag(PreviousCriticalLoc,
3880 diag::note_omp_previous_critical_region);
3881 return true;
3882 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003883 } else if (CurrentRegion == OMPD_barrier) {
3884 // OpenMP [2.16, Nesting of Regions]
3885 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003886 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003887 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3888 isOpenMPTaskingDirective(ParentRegion) ||
3889 ParentRegion == OMPD_master ||
3890 ParentRegion == OMPD_critical ||
3891 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00003892 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00003893 !isOpenMPParallelDirective(CurrentRegion) &&
3894 !isOpenMPTeamsDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003895 // OpenMP [2.16, Nesting of Regions]
3896 // A worksharing region may not be closely nested inside a worksharing,
3897 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003898 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3899 isOpenMPTaskingDirective(ParentRegion) ||
3900 ParentRegion == OMPD_master ||
3901 ParentRegion == OMPD_critical ||
3902 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003903 Recommend = ShouldBeInParallelRegion;
3904 } else if (CurrentRegion == OMPD_ordered) {
3905 // OpenMP [2.16, Nesting of Regions]
3906 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00003907 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003908 // An ordered region must be closely nested inside a loop region (or
3909 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003910 // OpenMP [2.8.1,simd Construct, Restrictions]
3911 // An ordered construct with the simd clause is the only OpenMP construct
3912 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003913 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003914 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003915 !(isOpenMPSimdDirective(ParentRegion) ||
3916 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003917 Recommend = ShouldBeInOrderedRegion;
Kelvin Libf594a52016-12-17 05:48:59 +00003918 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00003919 // OpenMP [2.16, Nesting of Regions]
3920 // If specified, a teams construct must be contained within a target
3921 // construct.
Alexey Bataev7a54d762019-09-10 20:19:58 +00003922 NestingProhibited =
3923 (SemaRef.LangOpts.OpenMP <= 45 && ParentRegion != OMPD_target) ||
3924 (SemaRef.LangOpts.OpenMP >= 50 && ParentRegion != OMPD_unknown &&
3925 ParentRegion != OMPD_target);
Kelvin Li2b51f722016-07-26 04:32:50 +00003926 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003927 Recommend = ShouldBeInTargetRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003928 }
Kelvin Libf594a52016-12-17 05:48:59 +00003929 if (!NestingProhibited &&
3930 !isOpenMPTargetExecutionDirective(CurrentRegion) &&
3931 !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
3932 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00003933 // OpenMP [2.16, Nesting of Regions]
3934 // distribute, parallel, parallel sections, parallel workshare, and the
3935 // parallel loop and parallel loop SIMD constructs are the only OpenMP
3936 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003937 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3938 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00003939 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003940 }
David Majnemer9d168222016-08-05 17:44:54 +00003941 if (!NestingProhibited &&
Kelvin Li02532872016-08-05 14:37:37 +00003942 isOpenMPNestingDistributeDirective(CurrentRegion)) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003943 // OpenMP 4.5 [2.17 Nesting of Regions]
3944 // The region associated with the distribute construct must be strictly
3945 // nested inside a teams region
Kelvin Libf594a52016-12-17 05:48:59 +00003946 NestingProhibited =
3947 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003948 Recommend = ShouldBeInTeamsRegion;
3949 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003950 if (!NestingProhibited &&
3951 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3952 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3953 // OpenMP 4.5 [2.17 Nesting of Regions]
3954 // If a target, target update, target data, target enter data, or
3955 // target exit data construct is encountered during execution of a
3956 // target region, the behavior is unspecified.
3957 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00003958 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
Alexey Bataeve3727102018-04-18 15:57:46 +00003959 SourceLocation) {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003960 if (isOpenMPTargetExecutionDirective(K)) {
3961 OffendingRegion = K;
3962 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003963 }
3964 return false;
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003965 },
3966 false /* don't skip top directive */);
3967 CloseNesting = false;
3968 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003969 if (NestingProhibited) {
Kelvin Li2b51f722016-07-26 04:32:50 +00003970 if (OrphanSeen) {
3971 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
3972 << getOpenMPDirectiveName(CurrentRegion) << Recommend;
3973 } else {
3974 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
3975 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3976 << Recommend << getOpenMPDirectiveName(CurrentRegion);
3977 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003978 return true;
3979 }
3980 }
3981 return false;
3982}
3983
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003984static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3985 ArrayRef<OMPClause *> Clauses,
3986 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3987 bool ErrorFound = false;
3988 unsigned NamedModifiersNumber = 0;
3989 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3990 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003991 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00003992 for (const OMPClause *C : Clauses) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003993 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3994 // At most one if clause without a directive-name-modifier can appear on
3995 // the directive.
3996 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3997 if (FoundNameModifiers[CurNM]) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003998 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003999 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
4000 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
4001 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00004002 } else if (CurNM != OMPD_unknown) {
4003 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004004 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00004005 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004006 FoundNameModifiers[CurNM] = IC;
4007 if (CurNM == OMPD_unknown)
4008 continue;
4009 // Check if the specified name modifier is allowed for the current
4010 // directive.
4011 // At most one if clause with the particular directive-name-modifier can
4012 // appear on the directive.
4013 bool MatchFound = false;
4014 for (auto NM : AllowedNameModifiers) {
4015 if (CurNM == NM) {
4016 MatchFound = true;
4017 break;
4018 }
4019 }
4020 if (!MatchFound) {
4021 S.Diag(IC->getNameModifierLoc(),
4022 diag::err_omp_wrong_if_directive_name_modifier)
4023 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
4024 ErrorFound = true;
4025 }
4026 }
4027 }
4028 // If any if clause on the directive includes a directive-name-modifier then
4029 // all if clauses on the directive must include a directive-name-modifier.
4030 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
4031 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004032 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004033 diag::err_omp_no_more_if_clause);
4034 } else {
4035 std::string Values;
4036 std::string Sep(", ");
4037 unsigned AllowedCnt = 0;
4038 unsigned TotalAllowedNum =
4039 AllowedNameModifiers.size() - NamedModifiersNumber;
4040 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
4041 ++Cnt) {
4042 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
4043 if (!FoundNameModifiers[NM]) {
4044 Values += "'";
4045 Values += getOpenMPDirectiveName(NM);
4046 Values += "'";
4047 if (AllowedCnt + 2 == TotalAllowedNum)
4048 Values += " or ";
4049 else if (AllowedCnt + 1 != TotalAllowedNum)
4050 Values += Sep;
4051 ++AllowedCnt;
4052 }
4053 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004054 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004055 diag::err_omp_unnamed_if_clause)
4056 << (TotalAllowedNum > 1) << Values;
4057 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004058 for (SourceLocation Loc : NameModifierLoc) {
Alexey Bataevecb156a2015-09-15 17:23:56 +00004059 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
4060 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004061 ErrorFound = true;
4062 }
4063 return ErrorFound;
4064}
4065
Alexey Bataeve106f252019-04-01 14:25:31 +00004066static std::pair<ValueDecl *, bool>
4067getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
4068 SourceRange &ERange, bool AllowArraySection = false) {
4069 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
4070 RefExpr->containsUnexpandedParameterPack())
4071 return std::make_pair(nullptr, true);
4072
4073 // OpenMP [3.1, C/C++]
4074 // A list item is a variable name.
4075 // OpenMP [2.9.3.3, Restrictions, p.1]
4076 // A variable that is part of another variable (as an array or
4077 // structure element) cannot appear in a private clause.
4078 RefExpr = RefExpr->IgnoreParens();
4079 enum {
4080 NoArrayExpr = -1,
4081 ArraySubscript = 0,
4082 OMPArraySection = 1
4083 } IsArrayExpr = NoArrayExpr;
4084 if (AllowArraySection) {
4085 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
4086 Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
4087 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
4088 Base = TempASE->getBase()->IgnoreParenImpCasts();
4089 RefExpr = Base;
4090 IsArrayExpr = ArraySubscript;
4091 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
4092 Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
4093 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
4094 Base = TempOASE->getBase()->IgnoreParenImpCasts();
4095 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
4096 Base = TempASE->getBase()->IgnoreParenImpCasts();
4097 RefExpr = Base;
4098 IsArrayExpr = OMPArraySection;
4099 }
4100 }
4101 ELoc = RefExpr->getExprLoc();
4102 ERange = RefExpr->getSourceRange();
4103 RefExpr = RefExpr->IgnoreParenImpCasts();
4104 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
4105 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
4106 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
4107 (S.getCurrentThisType().isNull() || !ME ||
4108 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
4109 !isa<FieldDecl>(ME->getMemberDecl()))) {
4110 if (IsArrayExpr != NoArrayExpr) {
4111 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
4112 << ERange;
4113 } else {
4114 S.Diag(ELoc,
4115 AllowArraySection
4116 ? diag::err_omp_expected_var_name_member_expr_or_array_item
4117 : diag::err_omp_expected_var_name_member_expr)
4118 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
4119 }
4120 return std::make_pair(nullptr, false);
4121 }
4122 return std::make_pair(
4123 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
4124}
4125
4126static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
Alexey Bataev471171c2019-03-28 19:15:36 +00004127 ArrayRef<OMPClause *> Clauses) {
4128 assert(!S.CurContext->isDependentContext() &&
4129 "Expected non-dependent context.");
Alexey Bataev471171c2019-03-28 19:15:36 +00004130 auto AllocateRange =
4131 llvm::make_filter_range(Clauses, OMPAllocateClause::classof);
Alexey Bataeve106f252019-04-01 14:25:31 +00004132 llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>>
4133 DeclToCopy;
4134 auto PrivateRange = llvm::make_filter_range(Clauses, [](const OMPClause *C) {
4135 return isOpenMPPrivate(C->getClauseKind());
4136 });
4137 for (OMPClause *Cl : PrivateRange) {
4138 MutableArrayRef<Expr *>::iterator I, It, Et;
4139 if (Cl->getClauseKind() == OMPC_private) {
4140 auto *PC = cast<OMPPrivateClause>(Cl);
4141 I = PC->private_copies().begin();
4142 It = PC->varlist_begin();
4143 Et = PC->varlist_end();
4144 } else if (Cl->getClauseKind() == OMPC_firstprivate) {
4145 auto *PC = cast<OMPFirstprivateClause>(Cl);
4146 I = PC->private_copies().begin();
4147 It = PC->varlist_begin();
4148 Et = PC->varlist_end();
4149 } else if (Cl->getClauseKind() == OMPC_lastprivate) {
4150 auto *PC = cast<OMPLastprivateClause>(Cl);
4151 I = PC->private_copies().begin();
4152 It = PC->varlist_begin();
4153 Et = PC->varlist_end();
4154 } else if (Cl->getClauseKind() == OMPC_linear) {
4155 auto *PC = cast<OMPLinearClause>(Cl);
4156 I = PC->privates().begin();
4157 It = PC->varlist_begin();
4158 Et = PC->varlist_end();
4159 } else if (Cl->getClauseKind() == OMPC_reduction) {
4160 auto *PC = cast<OMPReductionClause>(Cl);
4161 I = PC->privates().begin();
4162 It = PC->varlist_begin();
4163 Et = PC->varlist_end();
4164 } else if (Cl->getClauseKind() == OMPC_task_reduction) {
4165 auto *PC = cast<OMPTaskReductionClause>(Cl);
4166 I = PC->privates().begin();
4167 It = PC->varlist_begin();
4168 Et = PC->varlist_end();
4169 } else if (Cl->getClauseKind() == OMPC_in_reduction) {
4170 auto *PC = cast<OMPInReductionClause>(Cl);
4171 I = PC->privates().begin();
4172 It = PC->varlist_begin();
4173 Et = PC->varlist_end();
4174 } else {
4175 llvm_unreachable("Expected private clause.");
4176 }
4177 for (Expr *E : llvm::make_range(It, Et)) {
4178 if (!*I) {
4179 ++I;
4180 continue;
4181 }
4182 SourceLocation ELoc;
4183 SourceRange ERange;
4184 Expr *SimpleRefExpr = E;
4185 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
4186 /*AllowArraySection=*/true);
4187 DeclToCopy.try_emplace(Res.first,
4188 cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()));
4189 ++I;
4190 }
4191 }
Alexey Bataev471171c2019-03-28 19:15:36 +00004192 for (OMPClause *C : AllocateRange) {
4193 auto *AC = cast<OMPAllocateClause>(C);
4194 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
4195 getAllocatorKind(S, Stack, AC->getAllocator());
4196 // OpenMP, 2.11.4 allocate Clause, Restrictions.
4197 // For task, taskloop or target directives, allocation requests to memory
4198 // allocators with the trait access set to thread result in unspecified
4199 // behavior.
4200 if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc &&
4201 (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
4202 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()))) {
4203 S.Diag(AC->getAllocator()->getExprLoc(),
4204 diag::warn_omp_allocate_thread_on_task_target_directive)
4205 << getOpenMPDirectiveName(Stack->getCurrentDirective());
Alexey Bataeve106f252019-04-01 14:25:31 +00004206 }
4207 for (Expr *E : AC->varlists()) {
4208 SourceLocation ELoc;
4209 SourceRange ERange;
4210 Expr *SimpleRefExpr = E;
4211 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange);
4212 ValueDecl *VD = Res.first;
4213 DSAStackTy::DSAVarData Data = Stack->getTopDSA(VD, /*FromParent=*/false);
4214 if (!isOpenMPPrivate(Data.CKind)) {
4215 S.Diag(E->getExprLoc(),
4216 diag::err_omp_expected_private_copy_for_allocate);
4217 continue;
4218 }
4219 VarDecl *PrivateVD = DeclToCopy[VD];
4220 if (checkPreviousOMPAllocateAttribute(S, Stack, E, PrivateVD,
4221 AllocatorKind, AC->getAllocator()))
4222 continue;
4223 applyOMPAllocateAttribute(S, PrivateVD, AllocatorKind, AC->getAllocator(),
4224 E->getSourceRange());
Alexey Bataev471171c2019-03-28 19:15:36 +00004225 }
4226 }
Alexey Bataev471171c2019-03-28 19:15:36 +00004227}
4228
Alexey Bataev6d4ed052015-07-01 06:57:41 +00004229StmtResult Sema::ActOnOpenMPExecutableDirective(
4230 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
4231 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
4232 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004233 StmtResult Res = StmtError();
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00004234 // First check CancelRegion which is then used in checkNestingOfRegions.
4235 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
4236 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00004237 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00004238 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00004239
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004240 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataeve3727102018-04-18 15:57:46 +00004241 VarsWithInheritedDSAType VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004242 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00004243 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00004244 if (AStmt && !CurContext->isDependentContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00004245 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4246
4247 // Check default data sharing attributes for referenced variables.
4248 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
Arpith Chacko Jacob1f46b702017-01-23 15:38:49 +00004249 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
4250 Stmt *S = AStmt;
4251 while (--ThisCaptureLevel >= 0)
4252 S = cast<CapturedStmt>(S)->getCapturedStmt();
4253 DSAChecker.Visit(S);
Alexey Bataev1242d8f2019-06-28 20:45:14 +00004254 if (!isOpenMPTargetDataManagementDirective(Kind) &&
4255 !isOpenMPTaskingDirective(Kind)) {
4256 // Visit subcaptures to generate implicit clauses for captured vars.
4257 auto *CS = cast<CapturedStmt>(AStmt);
4258 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
4259 getOpenMPCaptureRegions(CaptureRegions, Kind);
4260 // Ignore outer tasking regions for target directives.
4261 if (CaptureRegions.size() > 1 && CaptureRegions.front() == OMPD_task)
4262 CS = cast<CapturedStmt>(CS->getCapturedStmt());
4263 DSAChecker.visitSubCaptures(CS);
4264 }
Alexey Bataev68446b72014-07-18 07:47:19 +00004265 if (DSAChecker.isErrorFound())
4266 return StmtError();
4267 // Generate list of implicitly defined firstprivate variables.
4268 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00004269
Alexey Bataev88202be2017-07-27 13:20:36 +00004270 SmallVector<Expr *, 4> ImplicitFirstprivates(
4271 DSAChecker.getImplicitFirstprivate().begin(),
4272 DSAChecker.getImplicitFirstprivate().end());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00004273 SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
4274 DSAChecker.getImplicitMap().end());
Alexey Bataev88202be2017-07-27 13:20:36 +00004275 // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +00004276 for (OMPClause *C : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00004277 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004278 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00004279 if (E)
4280 ImplicitFirstprivates.emplace_back(E);
4281 }
4282 }
4283 if (!ImplicitFirstprivates.empty()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00004284 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
Alexey Bataev88202be2017-07-27 13:20:36 +00004285 ImplicitFirstprivates, SourceLocation(), SourceLocation(),
4286 SourceLocation())) {
Alexey Bataev68446b72014-07-18 07:47:19 +00004287 ClausesWithImplicit.push_back(Implicit);
4288 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataev88202be2017-07-27 13:20:36 +00004289 ImplicitFirstprivates.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00004290 } else {
Alexey Bataev68446b72014-07-18 07:47:19 +00004291 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00004292 }
Alexey Bataev68446b72014-07-18 07:47:19 +00004293 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00004294 if (!ImplicitMaps.empty()) {
Michael Kruse4304e9d2019-02-19 16:38:20 +00004295 CXXScopeSpec MapperIdScopeSpec;
4296 DeclarationNameInfo MapperId;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00004297 if (OMPClause *Implicit = ActOnOpenMPMapClause(
Michael Kruse4304e9d2019-02-19 16:38:20 +00004298 llvm::None, llvm::None, MapperIdScopeSpec, MapperId,
4299 OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true, SourceLocation(),
4300 SourceLocation(), ImplicitMaps, OMPVarListLocTy())) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00004301 ClausesWithImplicit.emplace_back(Implicit);
4302 ErrorFound |=
4303 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00004304 } else {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00004305 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00004306 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00004307 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004308 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00004309
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004310 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004311 switch (Kind) {
4312 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00004313 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
4314 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004315 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004316 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004317 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00004318 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
4319 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004320 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00004321 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00004322 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
4323 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004324 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00004325 case OMPD_for_simd:
4326 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
4327 EndLoc, VarsWithInheritedDSA);
4328 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004329 case OMPD_sections:
4330 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
4331 EndLoc);
4332 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004333 case OMPD_section:
4334 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00004335 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004336 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
4337 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004338 case OMPD_single:
4339 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
4340 EndLoc);
4341 break;
Alexander Musman80c22892014-07-17 08:54:58 +00004342 case OMPD_master:
4343 assert(ClausesWithImplicit.empty() &&
4344 "No clauses are allowed for 'omp master' directive");
4345 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
4346 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004347 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00004348 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
4349 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004350 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00004351 case OMPD_parallel_for:
4352 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
4353 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004354 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004355 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00004356 case OMPD_parallel_for_simd:
4357 Res = ActOnOpenMPParallelForSimdDirective(
4358 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004359 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004360 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004361 case OMPD_parallel_sections:
4362 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
4363 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004364 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004365 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004366 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004367 Res =
4368 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004369 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004370 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00004371 case OMPD_taskyield:
4372 assert(ClausesWithImplicit.empty() &&
4373 "No clauses are allowed for 'omp taskyield' directive");
4374 assert(AStmt == nullptr &&
4375 "No associated statement allowed for 'omp taskyield' directive");
4376 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
4377 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004378 case OMPD_barrier:
4379 assert(ClausesWithImplicit.empty() &&
4380 "No clauses are allowed for 'omp barrier' directive");
4381 assert(AStmt == nullptr &&
4382 "No associated statement allowed for 'omp barrier' directive");
4383 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
4384 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00004385 case OMPD_taskwait:
4386 assert(ClausesWithImplicit.empty() &&
4387 "No clauses are allowed for 'omp taskwait' directive");
4388 assert(AStmt == nullptr &&
4389 "No associated statement allowed for 'omp taskwait' directive");
4390 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
4391 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004392 case OMPD_taskgroup:
Alexey Bataev169d96a2017-07-18 20:17:46 +00004393 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
4394 EndLoc);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004395 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00004396 case OMPD_flush:
4397 assert(AStmt == nullptr &&
4398 "No associated statement allowed for 'omp flush' directive");
4399 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
4400 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004401 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00004402 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
4403 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004404 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00004405 case OMPD_atomic:
4406 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
4407 EndLoc);
4408 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00004409 case OMPD_teams:
4410 Res =
4411 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
4412 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004413 case OMPD_target:
4414 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
4415 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004416 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004417 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00004418 case OMPD_target_parallel:
4419 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
4420 StartLoc, EndLoc);
4421 AllowedNameModifiers.push_back(OMPD_target);
4422 AllowedNameModifiers.push_back(OMPD_parallel);
4423 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00004424 case OMPD_target_parallel_for:
4425 Res = ActOnOpenMPTargetParallelForDirective(
4426 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4427 AllowedNameModifiers.push_back(OMPD_target);
4428 AllowedNameModifiers.push_back(OMPD_parallel);
4429 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00004430 case OMPD_cancellation_point:
4431 assert(ClausesWithImplicit.empty() &&
4432 "No clauses are allowed for 'omp cancellation point' directive");
4433 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
4434 "cancellation point' directive");
4435 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
4436 break;
Alexey Bataev80909872015-07-02 11:25:17 +00004437 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00004438 assert(AStmt == nullptr &&
4439 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00004440 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
4441 CancelRegion);
4442 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00004443 break;
Michael Wong65f367f2015-07-21 13:44:28 +00004444 case OMPD_target_data:
4445 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
4446 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004447 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00004448 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00004449 case OMPD_target_enter_data:
4450 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00004451 EndLoc, AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00004452 AllowedNameModifiers.push_back(OMPD_target_enter_data);
4453 break;
Samuel Antao72590762016-01-19 20:04:50 +00004454 case OMPD_target_exit_data:
4455 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00004456 EndLoc, AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00004457 AllowedNameModifiers.push_back(OMPD_target_exit_data);
4458 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00004459 case OMPD_taskloop:
4460 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
4461 EndLoc, VarsWithInheritedDSA);
4462 AllowedNameModifiers.push_back(OMPD_taskloop);
4463 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004464 case OMPD_taskloop_simd:
4465 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
4466 EndLoc, VarsWithInheritedDSA);
4467 AllowedNameModifiers.push_back(OMPD_taskloop);
4468 break;
Alexey Bataev60e51c42019-10-10 20:13:02 +00004469 case OMPD_master_taskloop:
4470 Res = ActOnOpenMPMasterTaskLoopDirective(
4471 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4472 AllowedNameModifiers.push_back(OMPD_taskloop);
4473 break;
Alexey Bataev5bbcead2019-10-14 17:17:41 +00004474 case OMPD_parallel_master_taskloop:
4475 Res = ActOnOpenMPParallelMasterTaskLoopDirective(
4476 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4477 AllowedNameModifiers.push_back(OMPD_taskloop);
4478 AllowedNameModifiers.push_back(OMPD_parallel);
4479 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004480 case OMPD_distribute:
4481 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
4482 EndLoc, VarsWithInheritedDSA);
4483 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00004484 case OMPD_target_update:
Alexey Bataev7828b252017-11-21 17:08:48 +00004485 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
4486 EndLoc, AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00004487 AllowedNameModifiers.push_back(OMPD_target_update);
4488 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00004489 case OMPD_distribute_parallel_for:
4490 Res = ActOnOpenMPDistributeParallelForDirective(
4491 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4492 AllowedNameModifiers.push_back(OMPD_parallel);
4493 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00004494 case OMPD_distribute_parallel_for_simd:
4495 Res = ActOnOpenMPDistributeParallelForSimdDirective(
4496 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4497 AllowedNameModifiers.push_back(OMPD_parallel);
4498 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00004499 case OMPD_distribute_simd:
4500 Res = ActOnOpenMPDistributeSimdDirective(
4501 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4502 break;
Kelvin Lia579b912016-07-14 02:54:56 +00004503 case OMPD_target_parallel_for_simd:
4504 Res = ActOnOpenMPTargetParallelForSimdDirective(
4505 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4506 AllowedNameModifiers.push_back(OMPD_target);
4507 AllowedNameModifiers.push_back(OMPD_parallel);
4508 break;
Kelvin Li986330c2016-07-20 22:57:10 +00004509 case OMPD_target_simd:
4510 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
4511 EndLoc, VarsWithInheritedDSA);
4512 AllowedNameModifiers.push_back(OMPD_target);
4513 break;
Kelvin Li02532872016-08-05 14:37:37 +00004514 case OMPD_teams_distribute:
David Majnemer9d168222016-08-05 17:44:54 +00004515 Res = ActOnOpenMPTeamsDistributeDirective(
4516 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Kelvin Li02532872016-08-05 14:37:37 +00004517 break;
Kelvin Li4e325f72016-10-25 12:50:55 +00004518 case OMPD_teams_distribute_simd:
4519 Res = ActOnOpenMPTeamsDistributeSimdDirective(
4520 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4521 break;
Kelvin Li579e41c2016-11-30 23:51:03 +00004522 case OMPD_teams_distribute_parallel_for_simd:
4523 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
4524 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4525 AllowedNameModifiers.push_back(OMPD_parallel);
4526 break;
Kelvin Li7ade93f2016-12-09 03:24:30 +00004527 case OMPD_teams_distribute_parallel_for:
4528 Res = ActOnOpenMPTeamsDistributeParallelForDirective(
4529 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4530 AllowedNameModifiers.push_back(OMPD_parallel);
4531 break;
Kelvin Libf594a52016-12-17 05:48:59 +00004532 case OMPD_target_teams:
4533 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
4534 EndLoc);
4535 AllowedNameModifiers.push_back(OMPD_target);
4536 break;
Kelvin Li83c451e2016-12-25 04:52:54 +00004537 case OMPD_target_teams_distribute:
4538 Res = ActOnOpenMPTargetTeamsDistributeDirective(
4539 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4540 AllowedNameModifiers.push_back(OMPD_target);
4541 break;
Kelvin Li80e8f562016-12-29 22:16:30 +00004542 case OMPD_target_teams_distribute_parallel_for:
4543 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
4544 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4545 AllowedNameModifiers.push_back(OMPD_target);
4546 AllowedNameModifiers.push_back(OMPD_parallel);
4547 break;
Kelvin Li1851df52017-01-03 05:23:48 +00004548 case OMPD_target_teams_distribute_parallel_for_simd:
4549 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
4550 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4551 AllowedNameModifiers.push_back(OMPD_target);
4552 AllowedNameModifiers.push_back(OMPD_parallel);
4553 break;
Kelvin Lida681182017-01-10 18:08:18 +00004554 case OMPD_target_teams_distribute_simd:
4555 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
4556 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4557 AllowedNameModifiers.push_back(OMPD_target);
4558 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00004559 case OMPD_declare_target:
4560 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004561 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00004562 case OMPD_allocate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00004563 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +00004564 case OMPD_declare_mapper:
Alexey Bataev587e1de2016-03-30 10:43:55 +00004565 case OMPD_declare_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00004566 case OMPD_requires:
Alexey Bataevd158cf62019-09-13 20:18:17 +00004567 case OMPD_declare_variant:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004568 llvm_unreachable("OpenMP Directive is not allowed");
4569 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004570 llvm_unreachable("Unknown OpenMP directive");
4571 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004572
Roman Lebedevb5700602019-03-20 16:32:36 +00004573 ErrorFound = Res.isInvalid() || ErrorFound;
4574
Alexey Bataev412254a2019-05-09 18:44:53 +00004575 // Check variables in the clauses if default(none) was specified.
4576 if (DSAStack->getDefaultDSA() == DSA_none) {
4577 DSAAttrChecker DSAChecker(DSAStack, *this, nullptr);
4578 for (OMPClause *C : Clauses) {
4579 switch (C->getClauseKind()) {
4580 case OMPC_num_threads:
4581 case OMPC_dist_schedule:
4582 // Do not analyse if no parent teams directive.
4583 if (isOpenMPTeamsDirective(DSAStack->getCurrentDirective()))
4584 break;
4585 continue;
4586 case OMPC_if:
4587 if (isOpenMPTeamsDirective(DSAStack->getCurrentDirective()) &&
4588 cast<OMPIfClause>(C)->getNameModifier() != OMPD_target)
4589 break;
4590 continue;
4591 case OMPC_schedule:
4592 break;
Alexey Bataevb9c55e22019-10-14 19:29:52 +00004593 case OMPC_grainsize:
4594 // Do not analyze if no parent parallel directive.
4595 if (isOpenMPParallelDirective(DSAStack->getCurrentDirective()))
4596 break;
4597 continue;
Alexey Bataevd88c7de2019-10-14 20:44:34 +00004598 case OMPC_num_tasks:
4599 // Do not analyze if no parent parallel directive.
4600 if (isOpenMPParallelDirective(DSAStack->getCurrentDirective()))
4601 break;
4602 continue;
Alexey Bataev3a842ec2019-10-15 19:37:05 +00004603 case OMPC_final:
4604 // Do not analyze if no parent parallel directive.
4605 if (isOpenMPParallelDirective(DSAStack->getCurrentDirective()))
4606 break;
4607 continue;
Alexey Bataev412254a2019-05-09 18:44:53 +00004608 case OMPC_ordered:
4609 case OMPC_device:
4610 case OMPC_num_teams:
4611 case OMPC_thread_limit:
4612 case OMPC_priority:
Alexey Bataev412254a2019-05-09 18:44:53 +00004613 case OMPC_hint:
4614 case OMPC_collapse:
4615 case OMPC_safelen:
4616 case OMPC_simdlen:
Alexey Bataev412254a2019-05-09 18:44:53 +00004617 case OMPC_default:
4618 case OMPC_proc_bind:
4619 case OMPC_private:
4620 case OMPC_firstprivate:
4621 case OMPC_lastprivate:
4622 case OMPC_shared:
4623 case OMPC_reduction:
4624 case OMPC_task_reduction:
4625 case OMPC_in_reduction:
4626 case OMPC_linear:
4627 case OMPC_aligned:
4628 case OMPC_copyin:
4629 case OMPC_copyprivate:
4630 case OMPC_nowait:
4631 case OMPC_untied:
4632 case OMPC_mergeable:
4633 case OMPC_allocate:
4634 case OMPC_read:
4635 case OMPC_write:
4636 case OMPC_update:
4637 case OMPC_capture:
4638 case OMPC_seq_cst:
4639 case OMPC_depend:
4640 case OMPC_threads:
4641 case OMPC_simd:
4642 case OMPC_map:
4643 case OMPC_nogroup:
4644 case OMPC_defaultmap:
4645 case OMPC_to:
4646 case OMPC_from:
4647 case OMPC_use_device_ptr:
4648 case OMPC_is_device_ptr:
4649 continue;
4650 case OMPC_allocator:
4651 case OMPC_flush:
4652 case OMPC_threadprivate:
4653 case OMPC_uniform:
4654 case OMPC_unknown:
4655 case OMPC_unified_address:
4656 case OMPC_unified_shared_memory:
4657 case OMPC_reverse_offload:
4658 case OMPC_dynamic_allocators:
4659 case OMPC_atomic_default_mem_order:
Alexey Bataev729e2422019-08-23 16:11:14 +00004660 case OMPC_device_type:
Alexey Bataevdba792c2019-09-23 18:13:31 +00004661 case OMPC_match:
Alexey Bataev412254a2019-05-09 18:44:53 +00004662 llvm_unreachable("Unexpected clause");
4663 }
4664 for (Stmt *CC : C->children()) {
4665 if (CC)
4666 DSAChecker.Visit(CC);
4667 }
4668 }
4669 for (auto &P : DSAChecker.getVarsWithInheritedDSA())
4670 VarsWithInheritedDSA[P.getFirst()] = P.getSecond();
4671 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004672 for (const auto &P : VarsWithInheritedDSA) {
Alexey Bataev1242d8f2019-06-28 20:45:14 +00004673 if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(P.getFirst()))
4674 continue;
4675 ErrorFound = true;
Alexey Bataev4acb8592014-07-07 13:01:15 +00004676 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
4677 << P.first << P.second->getSourceRange();
Alexey Bataev41ebe0c2019-05-09 18:14:57 +00004678 Diag(DSAStack->getDefaultDSALocation(), diag::note_omp_default_dsa_none);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004679 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004680
4681 if (!AllowedNameModifiers.empty())
4682 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
4683 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00004684
Alexey Bataeved09d242014-05-28 05:53:51 +00004685 if (ErrorFound)
4686 return StmtError();
Roman Lebedevb5700602019-03-20 16:32:36 +00004687
4688 if (!(Res.getAs<OMPExecutableDirective>()->isStandaloneDirective())) {
4689 Res.getAs<OMPExecutableDirective>()
4690 ->getStructuredBlock()
4691 ->setIsOMPStructuredBlock(true);
4692 }
4693
Gheorghe-Teodor Bercea411a6242019-04-18 19:53:43 +00004694 if (!CurContext->isDependentContext() &&
4695 isOpenMPTargetExecutionDirective(Kind) &&
4696 !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
4697 DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() ||
4698 DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() ||
4699 DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) {
4700 // Register target to DSA Stack.
4701 DSAStack->addTargetDirLocation(StartLoc);
4702 }
4703
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004704 return Res;
4705}
4706
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00004707Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
4708 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00004709 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00004710 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
4711 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00004712 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00004713 assert(Linears.size() == LinModifiers.size());
4714 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00004715 if (!DG || DG.get().isNull())
4716 return DeclGroupPtrTy();
4717
Alexey Bataevd158cf62019-09-13 20:18:17 +00004718 const int SimdId = 0;
Alexey Bataev587e1de2016-03-30 10:43:55 +00004719 if (!DG.get().isSingleDecl()) {
Alexey Bataevd158cf62019-09-13 20:18:17 +00004720 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant)
4721 << SimdId;
Alexey Bataev587e1de2016-03-30 10:43:55 +00004722 return DG;
4723 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004724 Decl *ADecl = DG.get().getSingleDecl();
Alexey Bataev587e1de2016-03-30 10:43:55 +00004725 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
4726 ADecl = FTD->getTemplatedDecl();
4727
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00004728 auto *FD = dyn_cast<FunctionDecl>(ADecl);
4729 if (!FD) {
Alexey Bataevd158cf62019-09-13 20:18:17 +00004730 Diag(ADecl->getLocation(), diag::err_omp_function_expected) << SimdId;
Alexey Bataev587e1de2016-03-30 10:43:55 +00004731 return DeclGroupPtrTy();
4732 }
4733
Alexey Bataev2af33e32016-04-07 12:45:37 +00004734 // OpenMP [2.8.2, declare simd construct, Description]
4735 // The parameter of the simdlen clause must be a constant positive integer
4736 // expression.
4737 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00004738 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00004739 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00004740 // OpenMP [2.8.2, declare simd construct, Description]
4741 // The special this pointer can be used as if was one of the arguments to the
4742 // function in any of the linear, aligned, or uniform clauses.
4743 // The uniform clause declares one or more arguments to have an invariant
4744 // value for all concurrent invocations of the function in the execution of a
4745 // single SIMD loop.
Alexey Bataeve3727102018-04-18 15:57:46 +00004746 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
4747 const Expr *UniformedLinearThis = nullptr;
4748 for (const Expr *E : Uniforms) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00004749 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00004750 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
4751 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00004752 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
4753 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00004754 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
Alexey Bataev43a919f2018-04-13 17:48:43 +00004755 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00004756 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00004757 }
4758 if (isa<CXXThisExpr>(E)) {
4759 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00004760 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00004761 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00004762 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
4763 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00004764 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00004765 // OpenMP [2.8.2, declare simd construct, Description]
4766 // The aligned clause declares that the object to which each list item points
4767 // is aligned to the number of bytes expressed in the optional parameter of
4768 // the aligned clause.
4769 // The special this pointer can be used as if was one of the arguments to the
4770 // function in any of the linear, aligned, or uniform clauses.
4771 // The type of list items appearing in the aligned clause must be array,
4772 // pointer, reference to array, or reference to pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +00004773 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
4774 const Expr *AlignedThis = nullptr;
4775 for (const Expr *E : Aligneds) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00004776 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00004777 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
4778 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
4779 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevd93d3762016-04-12 09:35:56 +00004780 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
4781 FD->getParamDecl(PVD->getFunctionScopeIndex())
4782 ->getCanonicalDecl() == CanonPVD) {
4783 // OpenMP [2.8.1, simd construct, Restrictions]
4784 // A list-item cannot appear in more than one aligned clause.
4785 if (AlignedArgs.count(CanonPVD) > 0) {
4786 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
4787 << 1 << E->getSourceRange();
4788 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
4789 diag::note_omp_explicit_dsa)
4790 << getOpenMPClauseName(OMPC_aligned);
4791 continue;
4792 }
4793 AlignedArgs[CanonPVD] = E;
4794 QualType QTy = PVD->getType()
4795 .getNonReferenceType()
4796 .getUnqualifiedType()
4797 .getCanonicalType();
4798 const Type *Ty = QTy.getTypePtrOrNull();
4799 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
4800 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
4801 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
4802 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
4803 }
4804 continue;
4805 }
4806 }
4807 if (isa<CXXThisExpr>(E)) {
4808 if (AlignedThis) {
4809 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
4810 << 2 << E->getSourceRange();
4811 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
4812 << getOpenMPClauseName(OMPC_aligned);
4813 }
4814 AlignedThis = E;
4815 continue;
4816 }
4817 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
4818 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
4819 }
4820 // The optional parameter of the aligned clause, alignment, must be a constant
4821 // positive integer expression. If no optional parameter is specified,
4822 // implementation-defined default alignments for SIMD instructions on the
4823 // target platforms are assumed.
Alexey Bataeve3727102018-04-18 15:57:46 +00004824 SmallVector<const Expr *, 4> NewAligns;
4825 for (Expr *E : Alignments) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00004826 ExprResult Align;
4827 if (E)
4828 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
4829 NewAligns.push_back(Align.get());
4830 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00004831 // OpenMP [2.8.2, declare simd construct, Description]
4832 // The linear clause declares one or more list items to be private to a SIMD
4833 // lane and to have a linear relationship with respect to the iteration space
4834 // of a loop.
4835 // The special this pointer can be used as if was one of the arguments to the
4836 // function in any of the linear, aligned, or uniform clauses.
4837 // When a linear-step expression is specified in a linear clause it must be
4838 // either a constant integer expression or an integer-typed parameter that is
4839 // specified in a uniform clause on the directive.
Alexey Bataeve3727102018-04-18 15:57:46 +00004840 llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
Alexey Bataevecba70f2016-04-12 11:02:11 +00004841 const bool IsUniformedThis = UniformedLinearThis != nullptr;
4842 auto MI = LinModifiers.begin();
Alexey Bataeve3727102018-04-18 15:57:46 +00004843 for (const Expr *E : Linears) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00004844 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
4845 ++MI;
4846 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00004847 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
4848 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
4849 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00004850 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
4851 FD->getParamDecl(PVD->getFunctionScopeIndex())
4852 ->getCanonicalDecl() == CanonPVD) {
4853 // OpenMP [2.15.3.7, linear Clause, Restrictions]
4854 // A list-item cannot appear in more than one linear clause.
4855 if (LinearArgs.count(CanonPVD) > 0) {
4856 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
4857 << getOpenMPClauseName(OMPC_linear)
4858 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
4859 Diag(LinearArgs[CanonPVD]->getExprLoc(),
4860 diag::note_omp_explicit_dsa)
4861 << getOpenMPClauseName(OMPC_linear);
4862 continue;
4863 }
4864 // Each argument can appear in at most one uniform or linear clause.
4865 if (UniformedArgs.count(CanonPVD) > 0) {
4866 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
4867 << getOpenMPClauseName(OMPC_linear)
4868 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
4869 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
4870 diag::note_omp_explicit_dsa)
4871 << getOpenMPClauseName(OMPC_uniform);
4872 continue;
4873 }
4874 LinearArgs[CanonPVD] = E;
4875 if (E->isValueDependent() || E->isTypeDependent() ||
4876 E->isInstantiationDependent() ||
4877 E->containsUnexpandedParameterPack())
4878 continue;
4879 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
4880 PVD->getOriginalType());
4881 continue;
4882 }
4883 }
4884 if (isa<CXXThisExpr>(E)) {
4885 if (UniformedLinearThis) {
4886 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
4887 << getOpenMPClauseName(OMPC_linear)
4888 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
4889 << E->getSourceRange();
4890 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
4891 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
4892 : OMPC_linear);
4893 continue;
4894 }
4895 UniformedLinearThis = E;
4896 if (E->isValueDependent() || E->isTypeDependent() ||
4897 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
4898 continue;
4899 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
4900 E->getType());
4901 continue;
4902 }
4903 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
4904 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
4905 }
4906 Expr *Step = nullptr;
4907 Expr *NewStep = nullptr;
4908 SmallVector<Expr *, 4> NewSteps;
Alexey Bataeve3727102018-04-18 15:57:46 +00004909 for (Expr *E : Steps) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00004910 // Skip the same step expression, it was checked already.
4911 if (Step == E || !E) {
4912 NewSteps.push_back(E ? NewStep : nullptr);
4913 continue;
4914 }
4915 Step = E;
Alexey Bataeve3727102018-04-18 15:57:46 +00004916 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step))
4917 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
4918 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00004919 if (UniformedArgs.count(CanonPVD) == 0) {
4920 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
4921 << Step->getSourceRange();
4922 } else if (E->isValueDependent() || E->isTypeDependent() ||
4923 E->isInstantiationDependent() ||
4924 E->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00004925 CanonPVD->getType()->hasIntegerRepresentation()) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00004926 NewSteps.push_back(Step);
Alexey Bataeve3727102018-04-18 15:57:46 +00004927 } else {
Alexey Bataevecba70f2016-04-12 11:02:11 +00004928 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
4929 << Step->getSourceRange();
4930 }
4931 continue;
4932 }
4933 NewStep = Step;
4934 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
4935 !Step->isInstantiationDependent() &&
4936 !Step->containsUnexpandedParameterPack()) {
4937 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
4938 .get();
4939 if (NewStep)
4940 NewStep = VerifyIntegerConstantExpression(NewStep).get();
4941 }
4942 NewSteps.push_back(NewStep);
4943 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00004944 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
4945 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00004946 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00004947 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
4948 const_cast<Expr **>(Linears.data()), Linears.size(),
4949 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
4950 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00004951 ADecl->addAttr(NewAttr);
Alexey Bataeva0063072019-09-16 17:06:31 +00004952 return DG;
Alexey Bataev587e1de2016-03-30 10:43:55 +00004953}
4954
Alexey Bataev0736f7f2019-09-18 16:24:31 +00004955Optional<std::pair<FunctionDecl *, Expr *>>
4956Sema::checkOpenMPDeclareVariantFunction(Sema::DeclGroupPtrTy DG,
4957 Expr *VariantRef, SourceRange SR) {
Alexey Bataevd158cf62019-09-13 20:18:17 +00004958 if (!DG || DG.get().isNull())
Alexey Bataev0736f7f2019-09-18 16:24:31 +00004959 return None;
Alexey Bataevd158cf62019-09-13 20:18:17 +00004960
4961 const int VariantId = 1;
4962 // Must be applied only to single decl.
4963 if (!DG.get().isSingleDecl()) {
4964 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant)
4965 << VariantId << SR;
Alexey Bataev0736f7f2019-09-18 16:24:31 +00004966 return None;
Alexey Bataevd158cf62019-09-13 20:18:17 +00004967 }
4968 Decl *ADecl = DG.get().getSingleDecl();
4969 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
4970 ADecl = FTD->getTemplatedDecl();
4971
4972 // Decl must be a function.
4973 auto *FD = dyn_cast<FunctionDecl>(ADecl);
4974 if (!FD) {
4975 Diag(ADecl->getLocation(), diag::err_omp_function_expected)
4976 << VariantId << SR;
Alexey Bataev0736f7f2019-09-18 16:24:31 +00004977 return None;
Alexey Bataevd158cf62019-09-13 20:18:17 +00004978 }
4979
4980 auto &&HasMultiVersionAttributes = [](const FunctionDecl *FD) {
4981 return FD->hasAttrs() &&
4982 (FD->hasAttr<CPUDispatchAttr>() || FD->hasAttr<CPUSpecificAttr>() ||
4983 FD->hasAttr<TargetAttr>());
4984 };
4985 // OpenMP is not compatible with CPU-specific attributes.
4986 if (HasMultiVersionAttributes(FD)) {
4987 Diag(FD->getLocation(), diag::err_omp_declare_variant_incompat_attributes)
4988 << SR;
Alexey Bataev0736f7f2019-09-18 16:24:31 +00004989 return None;
Alexey Bataevd158cf62019-09-13 20:18:17 +00004990 }
4991
4992 // Allow #pragma omp declare variant only if the function is not used.
Alexey Bataev12026142019-09-26 20:04:15 +00004993 if (FD->isUsed(false))
4994 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_used)
Alexey Bataevd158cf62019-09-13 20:18:17 +00004995 << FD->getLocation();
Alexey Bataev12026142019-09-26 20:04:15 +00004996
4997 // Check if the function was emitted already.
Alexey Bataev218bea92019-09-30 18:24:35 +00004998 const FunctionDecl *Definition;
4999 if (!FD->isThisDeclarationADefinition() && FD->isDefined(Definition) &&
5000 (LangOpts.EmitAllDecls || Context.DeclMustBeEmitted(Definition)))
Alexey Bataev12026142019-09-26 20:04:15 +00005001 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_emitted)
5002 << FD->getLocation();
Alexey Bataevd158cf62019-09-13 20:18:17 +00005003
5004 // The VariantRef must point to function.
5005 if (!VariantRef) {
5006 Diag(SR.getBegin(), diag::err_omp_function_expected) << VariantId;
Alexey Bataev0736f7f2019-09-18 16:24:31 +00005007 return None;
Alexey Bataevd158cf62019-09-13 20:18:17 +00005008 }
5009
5010 // Do not check templates, wait until instantiation.
5011 if (VariantRef->isTypeDependent() || VariantRef->isValueDependent() ||
5012 VariantRef->containsUnexpandedParameterPack() ||
Alexey Bataev0736f7f2019-09-18 16:24:31 +00005013 VariantRef->isInstantiationDependent() || FD->isDependentContext())
5014 return std::make_pair(FD, VariantRef);
Alexey Bataevd158cf62019-09-13 20:18:17 +00005015
5016 // Convert VariantRef expression to the type of the original function to
5017 // resolve possible conflicts.
5018 ExprResult VariantRefCast;
5019 if (LangOpts.CPlusPlus) {
5020 QualType FnPtrType;
5021 auto *Method = dyn_cast<CXXMethodDecl>(FD);
5022 if (Method && !Method->isStatic()) {
5023 const Type *ClassType =
5024 Context.getTypeDeclType(Method->getParent()).getTypePtr();
5025 FnPtrType = Context.getMemberPointerType(FD->getType(), ClassType);
5026 ExprResult ER;
5027 {
5028 // Build adrr_of unary op to correctly handle type checks for member
5029 // functions.
5030 Sema::TentativeAnalysisScope Trap(*this);
5031 ER = CreateBuiltinUnaryOp(VariantRef->getBeginLoc(), UO_AddrOf,
5032 VariantRef);
5033 }
5034 if (!ER.isUsable()) {
5035 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected)
5036 << VariantId << VariantRef->getSourceRange();
Alexey Bataev0736f7f2019-09-18 16:24:31 +00005037 return None;
Alexey Bataevd158cf62019-09-13 20:18:17 +00005038 }
5039 VariantRef = ER.get();
5040 } else {
5041 FnPtrType = Context.getPointerType(FD->getType());
5042 }
5043 ImplicitConversionSequence ICS =
5044 TryImplicitConversion(VariantRef, FnPtrType.getUnqualifiedType(),
5045 /*SuppressUserConversions=*/false,
5046 /*AllowExplicit=*/false,
5047 /*InOverloadResolution=*/false,
5048 /*CStyle=*/false,
5049 /*AllowObjCWritebackConversion=*/false);
5050 if (ICS.isFailure()) {
5051 Diag(VariantRef->getExprLoc(),
5052 diag::err_omp_declare_variant_incompat_types)
5053 << VariantRef->getType() << FnPtrType << VariantRef->getSourceRange();
Alexey Bataev0736f7f2019-09-18 16:24:31 +00005054 return None;
Alexey Bataevd158cf62019-09-13 20:18:17 +00005055 }
5056 VariantRefCast = PerformImplicitConversion(
5057 VariantRef, FnPtrType.getUnqualifiedType(), AA_Converting);
5058 if (!VariantRefCast.isUsable())
Alexey Bataev0736f7f2019-09-18 16:24:31 +00005059 return None;
Alexey Bataevd158cf62019-09-13 20:18:17 +00005060 // Drop previously built artificial addr_of unary op for member functions.
5061 if (Method && !Method->isStatic()) {
5062 Expr *PossibleAddrOfVariantRef = VariantRefCast.get();
5063 if (auto *UO = dyn_cast<UnaryOperator>(
5064 PossibleAddrOfVariantRef->IgnoreImplicit()))
5065 VariantRefCast = UO->getSubExpr();
5066 }
5067 } else {
5068 VariantRefCast = VariantRef;
5069 }
5070
5071 ExprResult ER = CheckPlaceholderExpr(VariantRefCast.get());
5072 if (!ER.isUsable() ||
5073 !ER.get()->IgnoreParenImpCasts()->getType()->isFunctionType()) {
5074 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected)
5075 << VariantId << VariantRef->getSourceRange();
Alexey Bataev0736f7f2019-09-18 16:24:31 +00005076 return None;
Alexey Bataevd158cf62019-09-13 20:18:17 +00005077 }
5078
5079 // The VariantRef must point to function.
5080 auto *DRE = dyn_cast<DeclRefExpr>(ER.get()->IgnoreParenImpCasts());
5081 if (!DRE) {
5082 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected)
5083 << VariantId << VariantRef->getSourceRange();
Alexey Bataev0736f7f2019-09-18 16:24:31 +00005084 return None;
Alexey Bataevd158cf62019-09-13 20:18:17 +00005085 }
5086 auto *NewFD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl());
5087 if (!NewFD) {
5088 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected)
5089 << VariantId << VariantRef->getSourceRange();
Alexey Bataev0736f7f2019-09-18 16:24:31 +00005090 return None;
Alexey Bataevd158cf62019-09-13 20:18:17 +00005091 }
5092
Alexey Bataevbf5d4292019-09-17 17:36:49 +00005093 // Check if variant function is not marked with declare variant directive.
5094 if (NewFD->hasAttrs() && NewFD->hasAttr<OMPDeclareVariantAttr>()) {
5095 Diag(VariantRef->getExprLoc(),
5096 diag::warn_omp_declare_variant_marked_as_declare_variant)
5097 << VariantRef->getSourceRange();
5098 SourceRange SR =
5099 NewFD->specific_attr_begin<OMPDeclareVariantAttr>()->getRange();
5100 Diag(SR.getBegin(), diag::note_omp_marked_declare_variant_here) << SR;
Alexey Bataev0736f7f2019-09-18 16:24:31 +00005101 return None;
Alexey Bataevbf5d4292019-09-17 17:36:49 +00005102 }
5103
Alexey Bataevd158cf62019-09-13 20:18:17 +00005104 enum DoesntSupport {
5105 VirtFuncs = 1,
5106 Constructors = 3,
5107 Destructors = 4,
5108 DeletedFuncs = 5,
5109 DefaultedFuncs = 6,
5110 ConstexprFuncs = 7,
5111 ConstevalFuncs = 8,
5112 };
5113 if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(FD)) {
5114 if (CXXFD->isVirtual()) {
5115 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
5116 << VirtFuncs;
Alexey Bataev0736f7f2019-09-18 16:24:31 +00005117 return None;
Alexey Bataevd158cf62019-09-13 20:18:17 +00005118 }
5119
5120 if (isa<CXXConstructorDecl>(FD)) {
5121 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
5122 << Constructors;
Alexey Bataev0736f7f2019-09-18 16:24:31 +00005123 return None;
Alexey Bataevd158cf62019-09-13 20:18:17 +00005124 }
5125
5126 if (isa<CXXDestructorDecl>(FD)) {
5127 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
5128 << Destructors;
Alexey Bataev0736f7f2019-09-18 16:24:31 +00005129 return None;
Alexey Bataevd158cf62019-09-13 20:18:17 +00005130 }
5131 }
5132
5133 if (FD->isDeleted()) {
5134 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
5135 << DeletedFuncs;
Alexey Bataev0736f7f2019-09-18 16:24:31 +00005136 return None;
Alexey Bataevd158cf62019-09-13 20:18:17 +00005137 }
5138
5139 if (FD->isDefaulted()) {
5140 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
5141 << DefaultedFuncs;
Alexey Bataev0736f7f2019-09-18 16:24:31 +00005142 return None;
Alexey Bataevd158cf62019-09-13 20:18:17 +00005143 }
5144
5145 if (FD->isConstexpr()) {
5146 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
5147 << (NewFD->isConsteval() ? ConstevalFuncs : ConstexprFuncs);
Alexey Bataev0736f7f2019-09-18 16:24:31 +00005148 return None;
Alexey Bataevd158cf62019-09-13 20:18:17 +00005149 }
5150
5151 // Check general compatibility.
5152 if (areMultiversionVariantFunctionsCompatible(
5153 FD, NewFD, PDiag(diag::err_omp_declare_variant_noproto),
5154 PartialDiagnosticAt(
5155 SR.getBegin(),
5156 PDiag(diag::note_omp_declare_variant_specified_here) << SR),
5157 PartialDiagnosticAt(
5158 VariantRef->getExprLoc(),
5159 PDiag(diag::err_omp_declare_variant_doesnt_support)),
5160 PartialDiagnosticAt(VariantRef->getExprLoc(),
5161 PDiag(diag::err_omp_declare_variant_diff)
5162 << FD->getLocation()),
Alexey Bataev6b06ead2019-10-08 14:56:20 +00005163 /*TemplatesSupported=*/true, /*ConstexprSupported=*/false,
5164 /*CLinkageMayDiffer=*/true))
Alexey Bataev0736f7f2019-09-18 16:24:31 +00005165 return None;
5166 return std::make_pair(FD, cast<Expr>(DRE));
5167}
Alexey Bataevd158cf62019-09-13 20:18:17 +00005168
Alexey Bataev9ff34742019-09-25 19:43:37 +00005169void Sema::ActOnOpenMPDeclareVariantDirective(
5170 FunctionDecl *FD, Expr *VariantRef, SourceRange SR,
5171 const Sema::OpenMPDeclareVariantCtsSelectorData &Data) {
5172 if (Data.CtxSet == OMPDeclareVariantAttr::CtxSetUnknown ||
5173 Data.Ctx == OMPDeclareVariantAttr::CtxUnknown)
5174 return;
Alexey Bataeva15a1412019-10-02 18:19:02 +00005175 Expr *Score = nullptr;
5176 OMPDeclareVariantAttr::ScoreType ST = OMPDeclareVariantAttr::ScoreUnknown;
5177 if (Data.CtxScore.isUsable()) {
5178 ST = OMPDeclareVariantAttr::ScoreSpecified;
5179 Score = Data.CtxScore.get();
5180 if (!Score->isTypeDependent() && !Score->isValueDependent() &&
5181 !Score->isInstantiationDependent() &&
5182 !Score->containsUnexpandedParameterPack()) {
5183 llvm::APSInt Result;
5184 ExprResult ICE = VerifyIntegerConstantExpression(Score, &Result);
5185 if (ICE.isInvalid())
5186 return;
5187 }
5188 }
Alexey Bataev9ff34742019-09-25 19:43:37 +00005189 auto *NewAttr = OMPDeclareVariantAttr::CreateImplicit(
Alexey Bataev303657a2019-10-08 19:44:16 +00005190 Context, VariantRef, Score, Data.CtxSet, ST, Data.Ctx,
5191 Data.ImplVendors.begin(), Data.ImplVendors.size(), SR);
Alexey Bataevbf5d4292019-09-17 17:36:49 +00005192 FD->addAttr(NewAttr);
Alexey Bataevd158cf62019-09-13 20:18:17 +00005193}
5194
Alexey Bataevbf5d4292019-09-17 17:36:49 +00005195void Sema::markOpenMPDeclareVariantFuncsReferenced(SourceLocation Loc,
5196 FunctionDecl *Func,
5197 bool MightBeOdrUse) {
5198 assert(LangOpts.OpenMP && "Expected OpenMP mode.");
5199
5200 if (!Func->isDependentContext() && Func->hasAttrs()) {
5201 for (OMPDeclareVariantAttr *A :
5202 Func->specific_attrs<OMPDeclareVariantAttr>()) {
5203 // TODO: add checks for active OpenMP context where possible.
5204 Expr *VariantRef = A->getVariantFuncRef();
5205 auto *DRE = dyn_cast<DeclRefExpr>(VariantRef->IgnoreParenImpCasts());
5206 auto *F = cast<FunctionDecl>(DRE->getDecl());
5207 if (!F->isDefined() && F->isTemplateInstantiation())
5208 InstantiateFunctionDefinition(Loc, F->getFirstDecl());
5209 MarkFunctionReferenced(Loc, F, MightBeOdrUse);
5210 }
5211 }
5212}
5213
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005214StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
5215 Stmt *AStmt,
5216 SourceLocation StartLoc,
5217 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005218 if (!AStmt)
5219 return StmtError();
5220
Alexey Bataeve3727102018-04-18 15:57:46 +00005221 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9959db52014-05-06 10:08:46 +00005222 // 1.2.2 OpenMP Language Terminology
5223 // Structured block - An executable statement with a single entry at the
5224 // top and a single exit at the bottom.
5225 // The point of exit cannot be a branch out of the structured block.
5226 // longjmp() and throw() must not violate the entry/exit criteria.
5227 CS->getCapturedDecl()->setNothrow();
5228
Reid Kleckner87a31802018-03-12 21:43:02 +00005229 setFunctionHasBranchProtectedScope();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005230
Alexey Bataev25e5b442015-09-15 12:52:43 +00005231 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5232 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005233}
5234
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005235namespace {
Alexey Bataevf8be4762019-08-14 19:30:06 +00005236/// Iteration space of a single for loop.
5237struct LoopIterationSpace final {
5238 /// True if the condition operator is the strict compare operator (<, > or
5239 /// !=).
5240 bool IsStrictCompare = false;
5241 /// Condition of the loop.
5242 Expr *PreCond = nullptr;
5243 /// This expression calculates the number of iterations in the loop.
5244 /// It is always possible to calculate it before starting the loop.
5245 Expr *NumIterations = nullptr;
5246 /// The loop counter variable.
5247 Expr *CounterVar = nullptr;
5248 /// Private loop counter variable.
5249 Expr *PrivateCounterVar = nullptr;
5250 /// This is initializer for the initial value of #CounterVar.
5251 Expr *CounterInit = nullptr;
5252 /// This is step for the #CounterVar used to generate its update:
5253 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
5254 Expr *CounterStep = nullptr;
5255 /// Should step be subtracted?
5256 bool Subtract = false;
5257 /// Source range of the loop init.
5258 SourceRange InitSrcRange;
5259 /// Source range of the loop condition.
5260 SourceRange CondSrcRange;
5261 /// Source range of the loop increment.
5262 SourceRange IncSrcRange;
5263 /// Minimum value that can have the loop control variable. Used to support
5264 /// non-rectangular loops. Applied only for LCV with the non-iterator types,
5265 /// since only such variables can be used in non-loop invariant expressions.
5266 Expr *MinValue = nullptr;
5267 /// Maximum value that can have the loop control variable. Used to support
5268 /// non-rectangular loops. Applied only for LCV with the non-iterator type,
5269 /// since only such variables can be used in non-loop invariant expressions.
5270 Expr *MaxValue = nullptr;
5271 /// true, if the lower bound depends on the outer loop control var.
5272 bool IsNonRectangularLB = false;
5273 /// true, if the upper bound depends on the outer loop control var.
5274 bool IsNonRectangularUB = false;
5275 /// Index of the loop this loop depends on and forms non-rectangular loop
5276 /// nest.
5277 unsigned LoopDependentIdx = 0;
5278 /// Final condition for the non-rectangular loop nest support. It is used to
5279 /// check that the number of iterations for this particular counter must be
5280 /// finished.
5281 Expr *FinalCondition = nullptr;
5282};
5283
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005284/// Helper class for checking canonical form of the OpenMP loops and
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005285/// extracting iteration space of each loop in the loop nest, that will be used
5286/// for IR generation.
5287class OpenMPIterationSpaceChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005288 /// Reference to Sema.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005289 Sema &SemaRef;
Alexey Bataev622af1d2019-04-24 19:58:30 +00005290 /// Data-sharing stack.
5291 DSAStackTy &Stack;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005292 /// A location for diagnostics (when there is no some better location).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005293 SourceLocation DefaultLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005294 /// A location for diagnostics (when increment is not compatible).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005295 SourceLocation ConditionLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005296 /// A source location for referring to loop init later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005297 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005298 /// A source location for referring to condition later.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005299 SourceRange ConditionSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005300 /// A source location for referring to increment later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005301 SourceRange IncrementSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005302 /// Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005303 ValueDecl *LCDecl = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005304 /// Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005305 Expr *LCRef = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005306 /// Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005307 Expr *LB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005308 /// Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005309 Expr *UB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005310 /// Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005311 Expr *Step = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005312 /// This flag is true when condition is one of:
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005313 /// Var < UB
5314 /// Var <= UB
5315 /// UB > Var
5316 /// UB >= Var
Kelvin Liefbe4af2018-11-21 19:10:48 +00005317 /// This will have no value when the condition is !=
5318 llvm::Optional<bool> TestIsLessOp;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005319 /// This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005320 bool TestIsStrictOp = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005321 /// This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005322 bool SubtractStep = false;
Alexey Bataev5ddc6d12019-04-26 19:28:37 +00005323 /// The outer loop counter this loop depends on (if any).
5324 const ValueDecl *DepDecl = nullptr;
5325 /// Contains number of loop (starts from 1) on which loop counter init
5326 /// expression of this loop depends on.
5327 Optional<unsigned> InitDependOnLC;
5328 /// Contains number of loop (starts from 1) on which loop counter condition
5329 /// expression of this loop depends on.
5330 Optional<unsigned> CondDependOnLC;
Alexey Bataev622af1d2019-04-24 19:58:30 +00005331 /// Checks if the provide statement depends on the loop counter.
Alexey Bataev5ddc6d12019-04-26 19:28:37 +00005332 Optional<unsigned> doesDependOnLoopCounter(const Stmt *S, bool IsInitializer);
Alexey Bataevf8be4762019-08-14 19:30:06 +00005333 /// Original condition required for checking of the exit condition for
5334 /// non-rectangular loop.
5335 Expr *Condition = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005336
5337public:
Alexey Bataev622af1d2019-04-24 19:58:30 +00005338 OpenMPIterationSpaceChecker(Sema &SemaRef, DSAStackTy &Stack,
5339 SourceLocation DefaultLoc)
5340 : SemaRef(SemaRef), Stack(Stack), DefaultLoc(DefaultLoc),
5341 ConditionLoc(DefaultLoc) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005342 /// Check init-expr for canonical loop form and save loop counter
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005343 /// variable - #Var and its initialization value - #LB.
Alexey Bataeve3727102018-04-18 15:57:46 +00005344 bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005345 /// Check test-expr for canonical form, save upper-bound (#UB), flags
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005346 /// for less/greater and for strict/non-strict comparison.
Alexey Bataeve3727102018-04-18 15:57:46 +00005347 bool checkAndSetCond(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005348 /// Check incr-expr for canonical loop form and return true if it
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005349 /// does not conform, otherwise save loop step (#Step).
Alexey Bataeve3727102018-04-18 15:57:46 +00005350 bool checkAndSetInc(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005351 /// Return the loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00005352 ValueDecl *getLoopDecl() const { return LCDecl; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005353 /// Return the reference expression to loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00005354 Expr *getLoopDeclRefExpr() const { return LCRef; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005355 /// Source range of the loop init.
Alexey Bataeve3727102018-04-18 15:57:46 +00005356 SourceRange getInitSrcRange() const { return InitSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005357 /// Source range of the loop condition.
Alexey Bataeve3727102018-04-18 15:57:46 +00005358 SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005359 /// Source range of the loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00005360 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005361 /// True if the step should be subtracted.
Alexey Bataeve3727102018-04-18 15:57:46 +00005362 bool shouldSubtractStep() const { return SubtractStep; }
Alexey Bataev316ccf62019-01-29 18:51:58 +00005363 /// True, if the compare operator is strict (<, > or !=).
5364 bool isStrictTestOp() const { return TestIsStrictOp; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005365 /// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00005366 Expr *buildNumIterations(
Alexey Bataevf8be4762019-08-14 19:30:06 +00005367 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType,
Alexey Bataeve3727102018-04-18 15:57:46 +00005368 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005369 /// Build the precondition expression for the loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00005370 Expr *
5371 buildPreCond(Scope *S, Expr *Cond,
5372 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005373 /// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005374 DeclRefExpr *
5375 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
5376 DSAStackTy &DSA) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005377 /// Build reference expression to the private counter be used for
Alexey Bataeva8899172015-08-06 12:30:57 +00005378 /// codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005379 Expr *buildPrivateCounterVar() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005380 /// Build initialization of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005381 Expr *buildCounterInit() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005382 /// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005383 Expr *buildCounterStep() const;
Alexey Bataevf138fda2018-08-13 19:04:24 +00005384 /// Build loop data with counter value for depend clauses in ordered
5385 /// directives.
5386 Expr *
5387 buildOrderedLoopData(Scope *S, Expr *Counter,
5388 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
5389 SourceLocation Loc, Expr *Inc = nullptr,
5390 OverloadedOperatorKind OOK = OO_Amp);
Alexey Bataevf8be4762019-08-14 19:30:06 +00005391 /// Builds the minimum value for the loop counter.
5392 std::pair<Expr *, Expr *> buildMinMaxValues(
5393 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
5394 /// Builds final condition for the non-rectangular loops.
5395 Expr *buildFinalCondition(Scope *S) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005396 /// Return true if any expression is dependent.
Alexey Bataeve3727102018-04-18 15:57:46 +00005397 bool dependent() const;
Alexey Bataevf8be4762019-08-14 19:30:06 +00005398 /// Returns true if the initializer forms non-rectangular loop.
5399 bool doesInitDependOnLC() const { return InitDependOnLC.hasValue(); }
5400 /// Returns true if the condition forms non-rectangular loop.
5401 bool doesCondDependOnLC() const { return CondDependOnLC.hasValue(); }
5402 /// Returns index of the loop we depend on (starting from 1), or 0 otherwise.
5403 unsigned getLoopDependentIdx() const {
5404 return InitDependOnLC.getValueOr(CondDependOnLC.getValueOr(0));
5405 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005406
5407private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005408 /// Check the right-hand side of an assignment in the increment
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005409 /// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +00005410 bool checkAndSetIncRHS(Expr *RHS);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005411 /// Helper to set loop counter variable and its initializer.
Alexey Bataev622af1d2019-04-24 19:58:30 +00005412 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB,
5413 bool EmitDiags);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005414 /// Helper to set upper bound.
Kelvin Liefbe4af2018-11-21 19:10:48 +00005415 bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp,
5416 SourceRange SR, SourceLocation SL);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005417 /// Helper to set loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00005418 bool setStep(Expr *NewStep, bool Subtract);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005419};
5420
Alexey Bataeve3727102018-04-18 15:57:46 +00005421bool OpenMPIterationSpaceChecker::dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005422 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005423 assert(!LB && !UB && !Step);
5424 return false;
5425 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005426 return LCDecl->getType()->isDependentType() ||
5427 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
5428 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005429}
5430
Alexey Bataeve3727102018-04-18 15:57:46 +00005431bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005432 Expr *NewLCRefExpr,
Alexey Bataev622af1d2019-04-24 19:58:30 +00005433 Expr *NewLB, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005434 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005435 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00005436 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005437 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005438 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005439 LCDecl = getCanonicalDecl(NewLCDecl);
5440 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005441 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
5442 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00005443 if ((Ctor->isCopyOrMoveConstructor() ||
5444 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
5445 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005446 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005447 LB = NewLB;
Alexey Bataev622af1d2019-04-24 19:58:30 +00005448 if (EmitDiags)
Alexey Bataev5ddc6d12019-04-26 19:28:37 +00005449 InitDependOnLC = doesDependOnLoopCounter(LB, /*IsInitializer=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005450 return false;
5451}
5452
Alexey Bataev316ccf62019-01-29 18:51:58 +00005453bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB,
5454 llvm::Optional<bool> LessOp,
Kelvin Liefbe4af2018-11-21 19:10:48 +00005455 bool StrictOp, SourceRange SR,
5456 SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005457 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005458 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
5459 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005460 if (!NewUB)
5461 return true;
5462 UB = NewUB;
Kelvin Liefbe4af2018-11-21 19:10:48 +00005463 if (LessOp)
5464 TestIsLessOp = LessOp;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005465 TestIsStrictOp = StrictOp;
5466 ConditionSrcRange = SR;
5467 ConditionLoc = SL;
Alexey Bataev5ddc6d12019-04-26 19:28:37 +00005468 CondDependOnLC = doesDependOnLoopCounter(UB, /*IsInitializer=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005469 return false;
5470}
5471
Alexey Bataeve3727102018-04-18 15:57:46 +00005472bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005473 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005474 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005475 if (!NewStep)
5476 return true;
5477 if (!NewStep->isValueDependent()) {
5478 // Check that the step is integer expression.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005479 SourceLocation StepLoc = NewStep->getBeginLoc();
Alexey Bataev5372fb82017-08-31 23:06:52 +00005480 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
5481 StepLoc, getExprAsWritten(NewStep));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005482 if (Val.isInvalid())
5483 return true;
5484 NewStep = Val.get();
5485
5486 // OpenMP [2.6, Canonical Loop Form, Restrictions]
5487 // If test-expr is of form var relational-op b and relational-op is < or
5488 // <= then incr-expr must cause var to increase on each iteration of the
5489 // loop. If test-expr is of form var relational-op b and relational-op is
5490 // > or >= then incr-expr must cause var to decrease on each iteration of
5491 // the loop.
5492 // If test-expr is of form b relational-op var and relational-op is < or
5493 // <= then incr-expr must cause var to decrease on each iteration of the
5494 // loop. If test-expr is of form b relational-op var and relational-op is
5495 // > or >= then incr-expr must cause var to increase on each iteration of
5496 // the loop.
5497 llvm::APSInt Result;
5498 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
5499 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
5500 bool IsConstNeg =
5501 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005502 bool IsConstPos =
5503 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005504 bool IsConstZero = IsConstant && !Result.getBoolValue();
Kelvin Liefbe4af2018-11-21 19:10:48 +00005505
5506 // != with increment is treated as <; != with decrement is treated as >
5507 if (!TestIsLessOp.hasValue())
5508 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005509 if (UB && (IsConstZero ||
Gheorghe-Teodor Berceaa3afcf22019-01-09 20:38:35 +00005510 (TestIsLessOp.getValue() ?
Kelvin Liefbe4af2018-11-21 19:10:48 +00005511 (IsConstNeg || (IsUnsigned && Subtract)) :
5512 (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005513 SemaRef.Diag(NewStep->getExprLoc(),
5514 diag::err_omp_loop_incr_not_compatible)
Kelvin Liefbe4af2018-11-21 19:10:48 +00005515 << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005516 SemaRef.Diag(ConditionLoc,
5517 diag::note_omp_loop_cond_requres_compatible_incr)
Kelvin Liefbe4af2018-11-21 19:10:48 +00005518 << TestIsLessOp.getValue() << ConditionSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005519 return true;
5520 }
Kelvin Liefbe4af2018-11-21 19:10:48 +00005521 if (TestIsLessOp.getValue() == Subtract) {
David Majnemer9d168222016-08-05 17:44:54 +00005522 NewStep =
5523 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
5524 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005525 Subtract = !Subtract;
5526 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005527 }
5528
5529 Step = NewStep;
5530 SubtractStep = Subtract;
5531 return false;
5532}
5533
Alexey Bataev622af1d2019-04-24 19:58:30 +00005534namespace {
5535/// Checker for the non-rectangular loops. Checks if the initializer or
5536/// condition expression references loop counter variable.
5537class LoopCounterRefChecker final
5538 : public ConstStmtVisitor<LoopCounterRefChecker, bool> {
5539 Sema &SemaRef;
5540 DSAStackTy &Stack;
5541 const ValueDecl *CurLCDecl = nullptr;
Alexey Bataev2f9ef332019-04-25 16:21:13 +00005542 const ValueDecl *DepDecl = nullptr;
Alexey Bataev5ddc6d12019-04-26 19:28:37 +00005543 const ValueDecl *PrevDepDecl = nullptr;
Alexey Bataev622af1d2019-04-24 19:58:30 +00005544 bool IsInitializer = true;
Alexey Bataev5ddc6d12019-04-26 19:28:37 +00005545 unsigned BaseLoopId = 0;
5546 bool checkDecl(const Expr *E, const ValueDecl *VD) {
5547 if (getCanonicalDecl(VD) == getCanonicalDecl(CurLCDecl)) {
5548 SemaRef.Diag(E->getExprLoc(), diag::err_omp_stmt_depends_on_loop_counter)
5549 << (IsInitializer ? 0 : 1);
5550 return false;
5551 }
5552 const auto &&Data = Stack.isLoopControlVariable(VD);
5553 // OpenMP, 2.9.1 Canonical Loop Form, Restrictions.
5554 // The type of the loop iterator on which we depend may not have a random
5555 // access iterator type.
5556 if (Data.first && VD->getType()->isRecordType()) {
5557 SmallString<128> Name;
5558 llvm::raw_svector_ostream OS(Name);
5559 VD->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(),
5560 /*Qualified=*/true);
5561 SemaRef.Diag(E->getExprLoc(),
5562 diag::err_omp_wrong_dependency_iterator_type)
5563 << OS.str();
5564 SemaRef.Diag(VD->getLocation(), diag::note_previous_decl) << VD;
5565 return false;
5566 }
5567 if (Data.first &&
5568 (DepDecl || (PrevDepDecl &&
5569 getCanonicalDecl(VD) != getCanonicalDecl(PrevDepDecl)))) {
5570 if (!DepDecl && PrevDepDecl)
5571 DepDecl = PrevDepDecl;
5572 SmallString<128> Name;
5573 llvm::raw_svector_ostream OS(Name);
5574 DepDecl->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(),
5575 /*Qualified=*/true);
5576 SemaRef.Diag(E->getExprLoc(),
5577 diag::err_omp_invariant_or_linear_dependency)
5578 << OS.str();
5579 return false;
5580 }
5581 if (Data.first) {
5582 DepDecl = VD;
5583 BaseLoopId = Data.first;
5584 }
5585 return Data.first;
5586 }
Alexey Bataev622af1d2019-04-24 19:58:30 +00005587
5588public:
5589 bool VisitDeclRefExpr(const DeclRefExpr *E) {
5590 const ValueDecl *VD = E->getDecl();
Alexey Bataev5ddc6d12019-04-26 19:28:37 +00005591 if (isa<VarDecl>(VD))
5592 return checkDecl(E, VD);
Alexey Bataev622af1d2019-04-24 19:58:30 +00005593 return false;
5594 }
5595 bool VisitMemberExpr(const MemberExpr *E) {
5596 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
5597 const ValueDecl *VD = E->getMemberDecl();
Mike Rice552c2c02019-07-17 15:18:45 +00005598 if (isa<VarDecl>(VD) || isa<FieldDecl>(VD))
5599 return checkDecl(E, VD);
Alexey Bataev622af1d2019-04-24 19:58:30 +00005600 }
5601 return false;
5602 }
5603 bool VisitStmt(const Stmt *S) {
Alexey Bataevf8be4762019-08-14 19:30:06 +00005604 bool Res = false;
Alexey Bataev2f9ef332019-04-25 16:21:13 +00005605 for (const Stmt *Child : S->children())
Alexey Bataevf8be4762019-08-14 19:30:06 +00005606 Res = (Child && Visit(Child)) || Res;
Alexey Bataev2f9ef332019-04-25 16:21:13 +00005607 return Res;
Alexey Bataev622af1d2019-04-24 19:58:30 +00005608 }
5609 explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack,
Alexey Bataev5ddc6d12019-04-26 19:28:37 +00005610 const ValueDecl *CurLCDecl, bool IsInitializer,
5611 const ValueDecl *PrevDepDecl = nullptr)
Alexey Bataev622af1d2019-04-24 19:58:30 +00005612 : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl),
Alexey Bataev5ddc6d12019-04-26 19:28:37 +00005613 PrevDepDecl(PrevDepDecl), IsInitializer(IsInitializer) {}
5614 unsigned getBaseLoopId() const {
5615 assert(CurLCDecl && "Expected loop dependency.");
5616 return BaseLoopId;
5617 }
5618 const ValueDecl *getDepDecl() const {
5619 assert(CurLCDecl && "Expected loop dependency.");
5620 return DepDecl;
5621 }
Alexey Bataev622af1d2019-04-24 19:58:30 +00005622};
5623} // namespace
5624
Alexey Bataev5ddc6d12019-04-26 19:28:37 +00005625Optional<unsigned>
5626OpenMPIterationSpaceChecker::doesDependOnLoopCounter(const Stmt *S,
5627 bool IsInitializer) {
Alexey Bataev622af1d2019-04-24 19:58:30 +00005628 // Check for the non-rectangular loops.
Alexey Bataev5ddc6d12019-04-26 19:28:37 +00005629 LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer,
5630 DepDecl);
5631 if (LoopStmtChecker.Visit(S)) {
5632 DepDecl = LoopStmtChecker.getDepDecl();
5633 return LoopStmtChecker.getBaseLoopId();
5634 }
5635 return llvm::None;
Alexey Bataev622af1d2019-04-24 19:58:30 +00005636}
5637
Alexey Bataeve3727102018-04-18 15:57:46 +00005638bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005639 // Check init-expr for canonical loop form and save loop counter
5640 // variable - #Var and its initialization value - #LB.
5641 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
5642 // var = lb
5643 // integer-type var = lb
5644 // random-access-iterator-type var = lb
5645 // pointer-type var = lb
5646 //
5647 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00005648 if (EmitDiags) {
5649 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
5650 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005651 return true;
5652 }
Tim Shen4a05bb82016-06-21 20:29:17 +00005653 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
5654 if (!ExprTemp->cleanupsHaveSideEffects())
5655 S = ExprTemp->getSubExpr();
5656
Alexander Musmana5f070a2014-10-01 06:03:56 +00005657 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005658 if (Expr *E = dyn_cast<Expr>(S))
5659 S = E->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00005660 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005661 if (BO->getOpcode() == BO_Assign) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005662 Expr *LHS = BO->getLHS()->IgnoreParens();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005663 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
5664 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
5665 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataev622af1d2019-04-24 19:58:30 +00005666 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
5667 EmitDiags);
5668 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS(), EmitDiags);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005669 }
5670 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
5671 if (ME->isArrow() &&
5672 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataev622af1d2019-04-24 19:58:30 +00005673 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
5674 EmitDiags);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005675 }
5676 }
David Majnemer9d168222016-08-05 17:44:54 +00005677 } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005678 if (DS->isSingleDecl()) {
David Majnemer9d168222016-08-05 17:44:54 +00005679 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00005680 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005681 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00005682 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005683 SemaRef.Diag(S->getBeginLoc(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005684 diag::ext_omp_loop_not_canonical_init)
5685 << S->getSourceRange();
Alexey Bataevf138fda2018-08-13 19:04:24 +00005686 return setLCDeclAndLB(
5687 Var,
5688 buildDeclRefExpr(SemaRef, Var,
5689 Var->getType().getNonReferenceType(),
5690 DS->getBeginLoc()),
Alexey Bataev622af1d2019-04-24 19:58:30 +00005691 Var->getInit(), EmitDiags);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005692 }
5693 }
5694 }
David Majnemer9d168222016-08-05 17:44:54 +00005695 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005696 if (CE->getOperator() == OO_Equal) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005697 Expr *LHS = CE->getArg(0);
David Majnemer9d168222016-08-05 17:44:54 +00005698 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005699 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
5700 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataev622af1d2019-04-24 19:58:30 +00005701 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
5702 EmitDiags);
5703 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1), EmitDiags);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005704 }
5705 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
5706 if (ME->isArrow() &&
5707 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataev622af1d2019-04-24 19:58:30 +00005708 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
5709 EmitDiags);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005710 }
5711 }
5712 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005713
Alexey Bataeve3727102018-04-18 15:57:46 +00005714 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005715 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00005716 if (EmitDiags) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005717 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init)
Alexey Bataev9c821032015-04-30 04:23:23 +00005718 << S->getSourceRange();
5719 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005720 return true;
5721}
5722
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005723/// Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005724/// variable (which may be the loop variable) if possible.
Alexey Bataeve3727102018-04-18 15:57:46 +00005725static const ValueDecl *getInitLCDecl(const Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005726 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00005727 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005728 E = getExprAsWritten(E);
Alexey Bataeve3727102018-04-18 15:57:46 +00005729 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005730 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00005731 if ((Ctor->isCopyOrMoveConstructor() ||
5732 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
5733 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005734 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00005735 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
5736 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005737 return getCanonicalDecl(VD);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005738 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005739 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005740 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
5741 return getCanonicalDecl(ME->getMemberDecl());
5742 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005743}
5744
Alexey Bataeve3727102018-04-18 15:57:46 +00005745bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005746 // Check test-expr for canonical form, save upper-bound UB, flags for
5747 // less/greater and for strict/non-strict comparison.
Alexey Bataev1be63402019-09-11 15:44:06 +00005748 // OpenMP [2.9] Canonical loop form. Test-expr may be one of the following:
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005749 // var relational-op b
5750 // b relational-op var
5751 //
Alexey Bataev1be63402019-09-11 15:44:06 +00005752 bool IneqCondIsCanonical = SemaRef.getLangOpts().OpenMP >= 50;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005753 if (!S) {
Alexey Bataev1be63402019-09-11 15:44:06 +00005754 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond)
5755 << (IneqCondIsCanonical ? 1 : 0) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005756 return true;
5757 }
Alexey Bataevf8be4762019-08-14 19:30:06 +00005758 Condition = S;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005759 S = getExprAsWritten(S);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005760 SourceLocation CondLoc = S->getBeginLoc();
David Majnemer9d168222016-08-05 17:44:54 +00005761 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005762 if (BO->isRelationalOp()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005763 if (getInitLCDecl(BO->getLHS()) == LCDecl)
5764 return setUB(BO->getRHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005765 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
5766 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
5767 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00005768 if (getInitLCDecl(BO->getRHS()) == LCDecl)
5769 return setUB(BO->getLHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005770 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
5771 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
5772 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataev1be63402019-09-11 15:44:06 +00005773 } else if (IneqCondIsCanonical && BO->getOpcode() == BO_NE)
5774 return setUB(
5775 getInitLCDecl(BO->getLHS()) == LCDecl ? BO->getRHS() : BO->getLHS(),
5776 /*LessOp=*/llvm::None,
5777 /*StrictOp=*/true, BO->getSourceRange(), BO->getOperatorLoc());
David Majnemer9d168222016-08-05 17:44:54 +00005778 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005779 if (CE->getNumArgs() == 2) {
5780 auto Op = CE->getOperator();
5781 switch (Op) {
5782 case OO_Greater:
5783 case OO_GreaterEqual:
5784 case OO_Less:
5785 case OO_LessEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00005786 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
5787 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005788 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
5789 CE->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00005790 if (getInitLCDecl(CE->getArg(1)) == LCDecl)
5791 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005792 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
5793 CE->getOperatorLoc());
5794 break;
Gheorghe-Teodor Berceaa3afcf22019-01-09 20:38:35 +00005795 case OO_ExclaimEqual:
Alexey Bataev1be63402019-09-11 15:44:06 +00005796 if (IneqCondIsCanonical)
5797 return setUB(getInitLCDecl(CE->getArg(0)) == LCDecl ? CE->getArg(1)
5798 : CE->getArg(0),
5799 /*LessOp=*/llvm::None,
5800 /*StrictOp=*/true, CE->getSourceRange(),
5801 CE->getOperatorLoc());
Kelvin Liefbe4af2018-11-21 19:10:48 +00005802 break;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005803 default:
5804 break;
5805 }
5806 }
5807 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005808 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005809 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005810 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataev1be63402019-09-11 15:44:06 +00005811 << (IneqCondIsCanonical ? 1 : 0) << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005812 return true;
5813}
5814
Alexey Bataeve3727102018-04-18 15:57:46 +00005815bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005816 // RHS of canonical loop form increment can be:
5817 // var + incr
5818 // incr + var
5819 // var - incr
5820 //
5821 RHS = RHS->IgnoreParenImpCasts();
David Majnemer9d168222016-08-05 17:44:54 +00005822 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005823 if (BO->isAdditiveOp()) {
5824 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataeve3727102018-04-18 15:57:46 +00005825 if (getInitLCDecl(BO->getLHS()) == LCDecl)
5826 return setStep(BO->getRHS(), !IsAdd);
5827 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl)
5828 return setStep(BO->getLHS(), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005829 }
David Majnemer9d168222016-08-05 17:44:54 +00005830 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005831 bool IsAdd = CE->getOperator() == OO_Plus;
5832 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005833 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
5834 return setStep(CE->getArg(1), !IsAdd);
5835 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl)
5836 return setStep(CE->getArg(0), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005837 }
5838 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005839 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005840 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005841 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005842 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005843 return true;
5844}
5845
Alexey Bataeve3727102018-04-18 15:57:46 +00005846bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005847 // Check incr-expr for canonical loop form and return true if it
5848 // does not conform.
5849 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
5850 // ++var
5851 // var++
5852 // --var
5853 // var--
5854 // var += incr
5855 // var -= incr
5856 // var = var + incr
5857 // var = incr + var
5858 // var = var - incr
5859 //
5860 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005861 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005862 return true;
5863 }
Tim Shen4a05bb82016-06-21 20:29:17 +00005864 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
5865 if (!ExprTemp->cleanupsHaveSideEffects())
5866 S = ExprTemp->getSubExpr();
5867
Alexander Musmana5f070a2014-10-01 06:03:56 +00005868 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005869 S = S->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00005870 if (auto *UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005871 if (UO->isIncrementDecrementOp() &&
Alexey Bataeve3727102018-04-18 15:57:46 +00005872 getInitLCDecl(UO->getSubExpr()) == LCDecl)
5873 return setStep(SemaRef
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005874 .ActOnIntegerConstant(UO->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00005875 (UO->isDecrementOp() ? -1 : 1))
5876 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00005877 /*Subtract=*/false);
David Majnemer9d168222016-08-05 17:44:54 +00005878 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005879 switch (BO->getOpcode()) {
5880 case BO_AddAssign:
5881 case BO_SubAssign:
Alexey Bataeve3727102018-04-18 15:57:46 +00005882 if (getInitLCDecl(BO->getLHS()) == LCDecl)
5883 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005884 break;
5885 case BO_Assign:
Alexey Bataeve3727102018-04-18 15:57:46 +00005886 if (getInitLCDecl(BO->getLHS()) == LCDecl)
5887 return checkAndSetIncRHS(BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005888 break;
5889 default:
5890 break;
5891 }
David Majnemer9d168222016-08-05 17:44:54 +00005892 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005893 switch (CE->getOperator()) {
5894 case OO_PlusPlus:
5895 case OO_MinusMinus:
Alexey Bataeve3727102018-04-18 15:57:46 +00005896 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
5897 return setStep(SemaRef
David Majnemer9d168222016-08-05 17:44:54 +00005898 .ActOnIntegerConstant(
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005899 CE->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00005900 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
5901 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00005902 /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005903 break;
5904 case OO_PlusEqual:
5905 case OO_MinusEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00005906 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
5907 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005908 break;
5909 case OO_Equal:
Alexey Bataeve3727102018-04-18 15:57:46 +00005910 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
5911 return checkAndSetIncRHS(CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005912 break;
5913 default:
5914 break;
5915 }
5916 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005917 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005918 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005919 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005920 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005921 return true;
5922}
Alexander Musmana5f070a2014-10-01 06:03:56 +00005923
Alexey Bataev5a3af132016-03-29 08:58:54 +00005924static ExprResult
5925tryBuildCapture(Sema &SemaRef, Expr *Capture,
Alexey Bataeve3727102018-04-18 15:57:46 +00005926 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00005927 if (SemaRef.CurContext->isDependentContext())
5928 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00005929 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
5930 return SemaRef.PerformImplicitConversion(
5931 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
5932 /*AllowExplicit=*/true);
5933 auto I = Captures.find(Capture);
5934 if (I != Captures.end())
5935 return buildCapture(SemaRef, Capture, I->second);
5936 DeclRefExpr *Ref = nullptr;
5937 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
5938 Captures[Capture] = Ref;
5939 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005940}
5941
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005942/// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00005943Expr *OpenMPIterationSpaceChecker::buildNumIterations(
Alexey Bataevf8be4762019-08-14 19:30:06 +00005944 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType,
Alexey Bataeve3727102018-04-18 15:57:46 +00005945 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00005946 ExprResult Diff;
Alexey Bataeve3727102018-04-18 15:57:46 +00005947 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005948 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00005949 SemaRef.getLangOpts().CPlusPlus) {
Alexey Bataevf8be4762019-08-14 19:30:06 +00005950 Expr *LBVal = LB;
5951 Expr *UBVal = UB;
5952 // LB = TestIsLessOp.getValue() ? min(LB(MinVal), LB(MaxVal)) :
5953 // max(LB(MinVal), LB(MaxVal))
5954 if (InitDependOnLC) {
5955 const LoopIterationSpace &IS =
5956 ResultIterSpaces[ResultIterSpaces.size() - 1 -
5957 InitDependOnLC.getValueOr(
5958 CondDependOnLC.getValueOr(0))];
5959 if (!IS.MinValue || !IS.MaxValue)
5960 return nullptr;
5961 // OuterVar = Min
5962 ExprResult MinValue =
5963 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue);
5964 if (!MinValue.isUsable())
5965 return nullptr;
5966
5967 ExprResult LBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
5968 IS.CounterVar, MinValue.get());
5969 if (!LBMinVal.isUsable())
5970 return nullptr;
5971 // OuterVar = Min, LBVal
5972 LBMinVal =
5973 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMinVal.get(), LBVal);
5974 if (!LBMinVal.isUsable())
5975 return nullptr;
5976 // (OuterVar = Min, LBVal)
5977 LBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMinVal.get());
5978 if (!LBMinVal.isUsable())
5979 return nullptr;
5980
5981 // OuterVar = Max
5982 ExprResult MaxValue =
5983 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue);
5984 if (!MaxValue.isUsable())
5985 return nullptr;
5986
5987 ExprResult LBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
5988 IS.CounterVar, MaxValue.get());
5989 if (!LBMaxVal.isUsable())
5990 return nullptr;
5991 // OuterVar = Max, LBVal
5992 LBMaxVal =
5993 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMaxVal.get(), LBVal);
5994 if (!LBMaxVal.isUsable())
5995 return nullptr;
5996 // (OuterVar = Max, LBVal)
5997 LBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMaxVal.get());
5998 if (!LBMaxVal.isUsable())
5999 return nullptr;
6000
6001 Expr *LBMin = tryBuildCapture(SemaRef, LBMinVal.get(), Captures).get();
6002 Expr *LBMax = tryBuildCapture(SemaRef, LBMaxVal.get(), Captures).get();
6003 if (!LBMin || !LBMax)
6004 return nullptr;
6005 // LB(MinVal) < LB(MaxVal)
6006 ExprResult MinLessMaxRes =
6007 SemaRef.BuildBinOp(S, DefaultLoc, BO_LT, LBMin, LBMax);
6008 if (!MinLessMaxRes.isUsable())
6009 return nullptr;
6010 Expr *MinLessMax =
6011 tryBuildCapture(SemaRef, MinLessMaxRes.get(), Captures).get();
6012 if (!MinLessMax)
6013 return nullptr;
6014 if (TestIsLessOp.getValue()) {
6015 // LB(MinVal) < LB(MaxVal) ? LB(MinVal) : LB(MaxVal) - min(LB(MinVal),
6016 // LB(MaxVal))
6017 ExprResult MinLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc,
6018 MinLessMax, LBMin, LBMax);
6019 if (!MinLB.isUsable())
6020 return nullptr;
6021 LBVal = MinLB.get();
6022 } else {
6023 // LB(MinVal) < LB(MaxVal) ? LB(MaxVal) : LB(MinVal) - max(LB(MinVal),
6024 // LB(MaxVal))
6025 ExprResult MaxLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc,
6026 MinLessMax, LBMax, LBMin);
6027 if (!MaxLB.isUsable())
6028 return nullptr;
6029 LBVal = MaxLB.get();
6030 }
6031 }
6032 // UB = TestIsLessOp.getValue() ? max(UB(MinVal), UB(MaxVal)) :
6033 // min(UB(MinVal), UB(MaxVal))
6034 if (CondDependOnLC) {
6035 const LoopIterationSpace &IS =
6036 ResultIterSpaces[ResultIterSpaces.size() - 1 -
6037 InitDependOnLC.getValueOr(
6038 CondDependOnLC.getValueOr(0))];
6039 if (!IS.MinValue || !IS.MaxValue)
6040 return nullptr;
6041 // OuterVar = Min
6042 ExprResult MinValue =
6043 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue);
6044 if (!MinValue.isUsable())
6045 return nullptr;
6046
6047 ExprResult UBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
6048 IS.CounterVar, MinValue.get());
6049 if (!UBMinVal.isUsable())
6050 return nullptr;
6051 // OuterVar = Min, UBVal
6052 UBMinVal =
6053 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMinVal.get(), UBVal);
6054 if (!UBMinVal.isUsable())
6055 return nullptr;
6056 // (OuterVar = Min, UBVal)
6057 UBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMinVal.get());
6058 if (!UBMinVal.isUsable())
6059 return nullptr;
6060
6061 // OuterVar = Max
6062 ExprResult MaxValue =
6063 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue);
6064 if (!MaxValue.isUsable())
6065 return nullptr;
6066
6067 ExprResult UBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
6068 IS.CounterVar, MaxValue.get());
6069 if (!UBMaxVal.isUsable())
6070 return nullptr;
6071 // OuterVar = Max, UBVal
6072 UBMaxVal =
6073 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMaxVal.get(), UBVal);
6074 if (!UBMaxVal.isUsable())
6075 return nullptr;
6076 // (OuterVar = Max, UBVal)
6077 UBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMaxVal.get());
6078 if (!UBMaxVal.isUsable())
6079 return nullptr;
6080
6081 Expr *UBMin = tryBuildCapture(SemaRef, UBMinVal.get(), Captures).get();
6082 Expr *UBMax = tryBuildCapture(SemaRef, UBMaxVal.get(), Captures).get();
6083 if (!UBMin || !UBMax)
6084 return nullptr;
6085 // UB(MinVal) > UB(MaxVal)
6086 ExprResult MinGreaterMaxRes =
6087 SemaRef.BuildBinOp(S, DefaultLoc, BO_GT, UBMin, UBMax);
6088 if (!MinGreaterMaxRes.isUsable())
6089 return nullptr;
6090 Expr *MinGreaterMax =
6091 tryBuildCapture(SemaRef, MinGreaterMaxRes.get(), Captures).get();
6092 if (!MinGreaterMax)
6093 return nullptr;
6094 if (TestIsLessOp.getValue()) {
6095 // UB(MinVal) > UB(MaxVal) ? UB(MinVal) : UB(MaxVal) - max(UB(MinVal),
6096 // UB(MaxVal))
6097 ExprResult MaxUB = SemaRef.ActOnConditionalOp(
6098 DefaultLoc, DefaultLoc, MinGreaterMax, UBMin, UBMax);
6099 if (!MaxUB.isUsable())
6100 return nullptr;
6101 UBVal = MaxUB.get();
6102 } else {
6103 // UB(MinVal) > UB(MaxVal) ? UB(MaxVal) : UB(MinVal) - min(UB(MinVal),
6104 // UB(MaxVal))
6105 ExprResult MinUB = SemaRef.ActOnConditionalOp(
6106 DefaultLoc, DefaultLoc, MinGreaterMax, UBMax, UBMin);
6107 if (!MinUB.isUsable())
6108 return nullptr;
6109 UBVal = MinUB.get();
6110 }
6111 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00006112 // Upper - Lower
Alexey Bataevf8be4762019-08-14 19:30:06 +00006113 Expr *UBExpr = TestIsLessOp.getValue() ? UBVal : LBVal;
6114 Expr *LBExpr = TestIsLessOp.getValue() ? LBVal : UBVal;
Alexey Bataev5a3af132016-03-29 08:58:54 +00006115 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
6116 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006117 if (!Upper || !Lower)
6118 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00006119
6120 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
6121
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006122 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00006123 // BuildBinOp already emitted error, this one is to point user to upper
6124 // and lower bound, and to tell what is passed to 'operator-'.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006125 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
Alexander Musmana5f070a2014-10-01 06:03:56 +00006126 << Upper->getSourceRange() << Lower->getSourceRange();
6127 return nullptr;
6128 }
6129 }
6130
6131 if (!Diff.isUsable())
6132 return nullptr;
6133
6134 // Upper - Lower [- 1]
6135 if (TestIsStrictOp)
6136 Diff = SemaRef.BuildBinOp(
6137 S, DefaultLoc, BO_Sub, Diff.get(),
6138 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
6139 if (!Diff.isUsable())
6140 return nullptr;
6141
6142 // Upper - Lower [- 1] + Step
Alexey Bataeve3727102018-04-18 15:57:46 +00006143 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00006144 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006145 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006146 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00006147 if (!Diff.isUsable())
6148 return nullptr;
6149
6150 // Parentheses (for dumping/debugging purposes only).
6151 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
6152 if (!Diff.isUsable())
6153 return nullptr;
6154
6155 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006156 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00006157 if (!Diff.isUsable())
6158 return nullptr;
6159
Alexander Musman174b3ca2014-10-06 11:16:29 +00006160 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006161 QualType Type = Diff.get()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +00006162 ASTContext &C = SemaRef.Context;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006163 bool UseVarType = VarType->hasIntegerRepresentation() &&
6164 C.getTypeSize(Type) > C.getTypeSize(VarType);
6165 if (!Type->isIntegerType() || UseVarType) {
6166 unsigned NewSize =
6167 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
6168 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
6169 : Type->hasSignedIntegerRepresentation();
6170 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00006171 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
6172 Diff = SemaRef.PerformImplicitConversion(
6173 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
6174 if (!Diff.isUsable())
6175 return nullptr;
6176 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006177 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00006178 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00006179 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
6180 if (NewSize != C.getTypeSize(Type)) {
6181 if (NewSize < C.getTypeSize(Type)) {
6182 assert(NewSize == 64 && "incorrect loop var size");
6183 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
6184 << InitSrcRange << ConditionSrcRange;
6185 }
6186 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006187 NewSize, Type->hasSignedIntegerRepresentation() ||
6188 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00006189 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
6190 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
6191 Sema::AA_Converting, true);
6192 if (!Diff.isUsable())
6193 return nullptr;
6194 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00006195 }
6196 }
6197
Alexander Musmana5f070a2014-10-01 06:03:56 +00006198 return Diff.get();
6199}
6200
Alexey Bataevf8be4762019-08-14 19:30:06 +00006201std::pair<Expr *, Expr *> OpenMPIterationSpaceChecker::buildMinMaxValues(
6202 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
6203 // Do not build for iterators, they cannot be used in non-rectangular loop
6204 // nests.
6205 if (LCDecl->getType()->isRecordType())
6206 return std::make_pair(nullptr, nullptr);
6207 // If we subtract, the min is in the condition, otherwise the min is in the
6208 // init value.
6209 Expr *MinExpr = nullptr;
6210 Expr *MaxExpr = nullptr;
6211 Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB;
6212 Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB;
6213 bool LBNonRect = TestIsLessOp.getValue() ? InitDependOnLC.hasValue()
6214 : CondDependOnLC.hasValue();
6215 bool UBNonRect = TestIsLessOp.getValue() ? CondDependOnLC.hasValue()
6216 : InitDependOnLC.hasValue();
6217 Expr *Lower =
6218 LBNonRect ? LBExpr : tryBuildCapture(SemaRef, LBExpr, Captures).get();
6219 Expr *Upper =
6220 UBNonRect ? UBExpr : tryBuildCapture(SemaRef, UBExpr, Captures).get();
6221 if (!Upper || !Lower)
6222 return std::make_pair(nullptr, nullptr);
6223
6224 if (TestIsLessOp.getValue())
6225 MinExpr = Lower;
6226 else
6227 MaxExpr = Upper;
6228
6229 // Build minimum/maximum value based on number of iterations.
6230 ExprResult Diff;
6231 QualType VarType = LCDecl->getType().getNonReferenceType();
6232
6233 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
6234 if (!Diff.isUsable())
6235 return std::make_pair(nullptr, nullptr);
6236
6237 // Upper - Lower [- 1]
6238 if (TestIsStrictOp)
6239 Diff = SemaRef.BuildBinOp(
6240 S, DefaultLoc, BO_Sub, Diff.get(),
6241 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
6242 if (!Diff.isUsable())
6243 return std::make_pair(nullptr, nullptr);
6244
6245 // Upper - Lower [- 1] + Step
6246 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
6247 if (!NewStep.isUsable())
6248 return std::make_pair(nullptr, nullptr);
6249
6250 // Parentheses (for dumping/debugging purposes only).
6251 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
6252 if (!Diff.isUsable())
6253 return std::make_pair(nullptr, nullptr);
6254
6255 // (Upper - Lower [- 1]) / Step
6256 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
6257 if (!Diff.isUsable())
6258 return std::make_pair(nullptr, nullptr);
6259
6260 // ((Upper - Lower [- 1]) / Step) * Step
6261 // Parentheses (for dumping/debugging purposes only).
6262 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
6263 if (!Diff.isUsable())
6264 return std::make_pair(nullptr, nullptr);
6265
6266 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Mul, Diff.get(), NewStep.get());
6267 if (!Diff.isUsable())
6268 return std::make_pair(nullptr, nullptr);
6269
6270 // Convert to the original type or ptrdiff_t, if original type is pointer.
6271 if (!VarType->isAnyPointerType() &&
6272 !SemaRef.Context.hasSameType(Diff.get()->getType(), VarType)) {
6273 Diff = SemaRef.PerformImplicitConversion(
6274 Diff.get(), VarType, Sema::AA_Converting, /*AllowExplicit=*/true);
6275 } else if (VarType->isAnyPointerType() &&
6276 !SemaRef.Context.hasSameType(
6277 Diff.get()->getType(),
6278 SemaRef.Context.getUnsignedPointerDiffType())) {
6279 Diff = SemaRef.PerformImplicitConversion(
6280 Diff.get(), SemaRef.Context.getUnsignedPointerDiffType(),
6281 Sema::AA_Converting, /*AllowExplicit=*/true);
6282 }
6283 if (!Diff.isUsable())
6284 return std::make_pair(nullptr, nullptr);
6285
6286 // Parentheses (for dumping/debugging purposes only).
6287 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
6288 if (!Diff.isUsable())
6289 return std::make_pair(nullptr, nullptr);
6290
6291 if (TestIsLessOp.getValue()) {
6292 // MinExpr = Lower;
6293 // MaxExpr = Lower + (((Upper - Lower [- 1]) / Step) * Step)
6294 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Lower, Diff.get());
6295 if (!Diff.isUsable())
6296 return std::make_pair(nullptr, nullptr);
6297 Diff = SemaRef.ActOnFinishFullExpr(Diff.get(), /*DiscardedValue*/ false);
6298 if (!Diff.isUsable())
6299 return std::make_pair(nullptr, nullptr);
6300 MaxExpr = Diff.get();
6301 } else {
6302 // MaxExpr = Upper;
6303 // MinExpr = Upper - (((Upper - Lower [- 1]) / Step) * Step)
6304 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Diff.get());
6305 if (!Diff.isUsable())
6306 return std::make_pair(nullptr, nullptr);
6307 Diff = SemaRef.ActOnFinishFullExpr(Diff.get(), /*DiscardedValue*/ false);
6308 if (!Diff.isUsable())
6309 return std::make_pair(nullptr, nullptr);
6310 MinExpr = Diff.get();
6311 }
6312
6313 return std::make_pair(MinExpr, MaxExpr);
6314}
6315
6316Expr *OpenMPIterationSpaceChecker::buildFinalCondition(Scope *S) const {
6317 if (InitDependOnLC || CondDependOnLC)
6318 return Condition;
6319 return nullptr;
6320}
6321
Alexey Bataeve3727102018-04-18 15:57:46 +00006322Expr *OpenMPIterationSpaceChecker::buildPreCond(
Alexey Bataev5a3af132016-03-29 08:58:54 +00006323 Scope *S, Expr *Cond,
Alexey Bataeve3727102018-04-18 15:57:46 +00006324 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev658ad4d2019-10-01 16:19:10 +00006325 // Do not build a precondition when the condition/initialization is dependent
6326 // to prevent pessimistic early loop exit.
6327 // TODO: this can be improved by calculating min/max values but not sure that
6328 // it will be very effective.
6329 if (CondDependOnLC || InitDependOnLC)
6330 return SemaRef.PerformImplicitConversion(
6331 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(),
6332 SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
6333 /*AllowExplicit=*/true).get();
6334
Alexey Bataev62dbb972015-04-22 11:59:37 +00006335 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
Richard Smith2e3ed4a2019-08-16 19:53:22 +00006336 Sema::TentativeAnalysisScope Trap(SemaRef);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006337
Alexey Bataev658ad4d2019-10-01 16:19:10 +00006338 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures);
6339 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00006340 if (!NewLB.isUsable() || !NewUB.isUsable())
6341 return nullptr;
6342
Alexey Bataeve3727102018-04-18 15:57:46 +00006343 ExprResult CondExpr =
6344 SemaRef.BuildBinOp(S, DefaultLoc,
Gheorghe-Teodor Berceaa3afcf22019-01-09 20:38:35 +00006345 TestIsLessOp.getValue() ?
Kelvin Liefbe4af2018-11-21 19:10:48 +00006346 (TestIsStrictOp ? BO_LT : BO_LE) :
6347 (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataeve3727102018-04-18 15:57:46 +00006348 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00006349 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00006350 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
6351 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00006352 CondExpr = SemaRef.PerformImplicitConversion(
6353 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
6354 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00006355 }
Richard Smith2e3ed4a2019-08-16 19:53:22 +00006356
Sergi Mateo Bellidof3e00fe2019-02-01 08:39:01 +00006357 // Otherwise use original loop condition and evaluate it in runtime.
Alexey Bataev62dbb972015-04-22 11:59:37 +00006358 return CondExpr.isUsable() ? CondExpr.get() : Cond;
6359}
6360
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006361/// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006362DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
Alexey Bataevf138fda2018-08-13 19:04:24 +00006363 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
6364 DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006365 auto *VD = dyn_cast<VarDecl>(LCDecl);
6366 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006367 VD = SemaRef.isOpenMPCapturedDecl(LCDecl);
6368 DeclRefExpr *Ref = buildDeclRefExpr(
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006369 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00006370 const DSAStackTy::DSAVarData Data =
6371 DSA.getTopDSA(LCDecl, /*FromParent=*/false);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006372 // If the loop control decl is explicitly marked as private, do not mark it
6373 // as captured again.
6374 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
6375 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006376 return Ref;
6377 }
Alexey Bataev0d8fcdf2019-03-14 20:36:00 +00006378 return cast<DeclRefExpr>(LCRef);
Alexey Bataeva8899172015-08-06 12:30:57 +00006379}
6380
Alexey Bataeve3727102018-04-18 15:57:46 +00006381Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006382 if (LCDecl && !LCDecl->isInvalidDecl()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006383 QualType Type = LCDecl->getType().getNonReferenceType();
6384 VarDecl *PrivateVar = buildVarDecl(
Alexey Bataev63cc8e92018-03-20 14:45:59 +00006385 SemaRef, DefaultLoc, Type, LCDecl->getName(),
6386 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
6387 isa<VarDecl>(LCDecl)
6388 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
6389 : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00006390 if (PrivateVar->isInvalidDecl())
6391 return nullptr;
6392 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
6393 }
6394 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00006395}
6396
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006397/// Build initialization of the counter to be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006398Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00006399
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006400/// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006401Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00006402
Alexey Bataevf138fda2018-08-13 19:04:24 +00006403Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
6404 Scope *S, Expr *Counter,
6405 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
6406 Expr *Inc, OverloadedOperatorKind OOK) {
6407 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get();
6408 if (!Cnt)
6409 return nullptr;
6410 if (Inc) {
6411 assert((OOK == OO_Plus || OOK == OO_Minus) &&
6412 "Expected only + or - operations for depend clauses.");
6413 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
6414 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get();
6415 if (!Cnt)
6416 return nullptr;
6417 }
6418 ExprResult Diff;
6419 QualType VarType = LCDecl->getType().getNonReferenceType();
6420 if (VarType->isIntegerType() || VarType->isPointerType() ||
6421 SemaRef.getLangOpts().CPlusPlus) {
6422 // Upper - Lower
Alexey Bataev316ccf62019-01-29 18:51:58 +00006423 Expr *Upper = TestIsLessOp.getValue()
6424 ? Cnt
6425 : tryBuildCapture(SemaRef, UB, Captures).get();
6426 Expr *Lower = TestIsLessOp.getValue()
6427 ? tryBuildCapture(SemaRef, LB, Captures).get()
6428 : Cnt;
Alexey Bataevf138fda2018-08-13 19:04:24 +00006429 if (!Upper || !Lower)
6430 return nullptr;
6431
6432 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
6433
6434 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
6435 // BuildBinOp already emitted error, this one is to point user to upper
6436 // and lower bound, and to tell what is passed to 'operator-'.
6437 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
6438 << Upper->getSourceRange() << Lower->getSourceRange();
6439 return nullptr;
6440 }
6441 }
6442
6443 if (!Diff.isUsable())
6444 return nullptr;
6445
6446 // Parentheses (for dumping/debugging purposes only).
6447 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
6448 if (!Diff.isUsable())
6449 return nullptr;
6450
6451 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
6452 if (!NewStep.isUsable())
6453 return nullptr;
6454 // (Upper - Lower) / Step
6455 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
6456 if (!Diff.isUsable())
6457 return nullptr;
6458
6459 return Diff.get();
6460}
Alexey Bataev23b69422014-06-18 07:08:49 +00006461} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006462
Alexey Bataev9c821032015-04-30 04:23:23 +00006463void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
6464 assert(getLangOpts().OpenMP && "OpenMP is not active.");
6465 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006466 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
6467 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00006468 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevce901812018-12-19 18:16:37 +00006469 DSAStack->loopStart();
Alexey Bataev622af1d2019-04-24 19:58:30 +00006470 OpenMPIterationSpaceChecker ISC(*this, *DSAStack, ForLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00006471 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) {
6472 if (ValueDecl *D = ISC.getLoopDecl()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006473 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev05be1da2019-07-18 17:49:13 +00006474 DeclRefExpr *PrivateRef = nullptr;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006475 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006476 if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006477 VD = Private;
Alexey Bataeve3727102018-04-18 15:57:46 +00006478 } else {
Alexey Bataev05be1da2019-07-18 17:49:13 +00006479 PrivateRef = buildCapture(*this, D, ISC.getLoopDeclRefExpr(),
6480 /*WithInit=*/false);
6481 VD = cast<VarDecl>(PrivateRef->getDecl());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006482 }
6483 }
6484 DSAStack->addLoopControlVariable(D, VD);
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00006485 const Decl *LD = DSAStack->getPossiblyLoopCunter();
6486 if (LD != D->getCanonicalDecl()) {
6487 DSAStack->resetPossibleLoopCounter();
6488 if (auto *Var = dyn_cast_or_null<VarDecl>(LD))
6489 MarkDeclarationsReferencedInExpr(
6490 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var),
6491 Var->getType().getNonLValueExprType(Context),
6492 ForLoc, /*RefersToCapture=*/true));
6493 }
Alexey Bataev05be1da2019-07-18 17:49:13 +00006494 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
6495 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables
6496 // Referenced in a Construct, C/C++]. The loop iteration variable in the
6497 // associated for-loop of a simd construct with just one associated
6498 // for-loop may be listed in a linear clause with a constant-linear-step
6499 // that is the increment of the associated for-loop. The loop iteration
6500 // variable(s) in the associated for-loop(s) of a for or parallel for
6501 // construct may be listed in a private or lastprivate clause.
6502 DSAStackTy::DSAVarData DVar =
6503 DSAStack->getTopDSA(D, /*FromParent=*/false);
6504 // If LoopVarRefExpr is nullptr it means the corresponding loop variable
6505 // is declared in the loop and it is predetermined as a private.
6506 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
6507 OpenMPClauseKind PredeterminedCKind =
6508 isOpenMPSimdDirective(DKind)
6509 ? (DSAStack->hasMutipleLoops() ? OMPC_lastprivate : OMPC_linear)
6510 : OMPC_private;
6511 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
6512 DVar.CKind != PredeterminedCKind && DVar.RefExpr &&
6513 (LangOpts.OpenMP <= 45 || (DVar.CKind != OMPC_lastprivate &&
6514 DVar.CKind != OMPC_private))) ||
6515 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
Alexey Bataev60e51c42019-10-10 20:13:02 +00006516 DKind == OMPD_master_taskloop ||
Alexey Bataev5bbcead2019-10-14 17:17:41 +00006517 DKind == OMPD_parallel_master_taskloop ||
Alexey Bataev05be1da2019-07-18 17:49:13 +00006518 isOpenMPDistributeDirective(DKind)) &&
6519 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
6520 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
6521 (DVar.CKind != OMPC_private || DVar.RefExpr)) {
6522 Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa)
6523 << getOpenMPClauseName(DVar.CKind)
6524 << getOpenMPDirectiveName(DKind)
6525 << getOpenMPClauseName(PredeterminedCKind);
6526 if (DVar.RefExpr == nullptr)
6527 DVar.CKind = PredeterminedCKind;
6528 reportOriginalDsa(*this, DSAStack, D, DVar,
6529 /*IsLoopIterVar=*/true);
6530 } else if (LoopDeclRefExpr) {
6531 // Make the loop iteration variable private (for worksharing
6532 // constructs), linear (for simd directives with the only one
6533 // associated loop) or lastprivate (for simd directives with several
6534 // collapsed or ordered loops).
6535 if (DVar.CKind == OMPC_unknown)
6536 DSAStack->addDSA(D, LoopDeclRefExpr, PredeterminedCKind,
6537 PrivateRef);
6538 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006539 }
6540 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006541 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00006542 }
6543}
6544
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006545/// Called on a for stmt to check and extract its iteration space
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006546/// for further processing (such as collapsing).
Alexey Bataeve3727102018-04-18 15:57:46 +00006547static bool checkOpenMPIterationSpace(
Alexey Bataev4acb8592014-07-07 13:01:15 +00006548 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
6549 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataevf138fda2018-08-13 19:04:24 +00006550 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
6551 Expr *OrderedLoopCountExpr,
Alexey Bataeve3727102018-04-18 15:57:46 +00006552 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexey Bataevf8be4762019-08-14 19:30:06 +00006553 llvm::MutableArrayRef<LoopIterationSpace> ResultIterSpaces,
Alexey Bataeve3727102018-04-18 15:57:46 +00006554 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbef93a92019-10-07 18:54:57 +00006555 // OpenMP [2.9.1, Canonical Loop Form]
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006556 // for (init-expr; test-expr; incr-expr) structured-block
Alexey Bataevbef93a92019-10-07 18:54:57 +00006557 // for (range-decl: range-expr) structured-block
David Majnemer9d168222016-08-05 17:44:54 +00006558 auto *For = dyn_cast_or_null<ForStmt>(S);
Alexey Bataevbef93a92019-10-07 18:54:57 +00006559 auto *CXXFor = dyn_cast_or_null<CXXForRangeStmt>(S);
6560 // Ranged for is supported only in OpenMP 5.0.
6561 if (!For && (SemaRef.LangOpts.OpenMP <= 45 || !CXXFor)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006562 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00006563 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
Alexey Bataevf138fda2018-08-13 19:04:24 +00006564 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount
Alexey Bataev10e775f2015-07-30 11:36:16 +00006565 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
Alexey Bataevf138fda2018-08-13 19:04:24 +00006566 if (TotalNestedLoopCount > 1) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006567 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
6568 SemaRef.Diag(DSA.getConstructLoc(),
6569 diag::note_omp_collapse_ordered_expr)
6570 << 2 << CollapseLoopCountExpr->getSourceRange()
6571 << OrderedLoopCountExpr->getSourceRange();
6572 else if (CollapseLoopCountExpr)
6573 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
6574 diag::note_omp_collapse_ordered_expr)
6575 << 0 << CollapseLoopCountExpr->getSourceRange();
6576 else
6577 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
6578 diag::note_omp_collapse_ordered_expr)
6579 << 1 << OrderedLoopCountExpr->getSourceRange();
6580 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006581 return true;
6582 }
Alexey Bataevbef93a92019-10-07 18:54:57 +00006583 assert(((For && For->getBody()) || (CXXFor && CXXFor->getBody())) &&
6584 "No loop body.");
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006585
Alexey Bataevbef93a92019-10-07 18:54:57 +00006586 OpenMPIterationSpaceChecker ISC(SemaRef, DSA,
6587 For ? For->getForLoc() : CXXFor->getForLoc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006588
6589 // Check init.
Alexey Bataevbef93a92019-10-07 18:54:57 +00006590 Stmt *Init = For ? For->getInit() : CXXFor->getBeginStmt();
Alexey Bataeve3727102018-04-18 15:57:46 +00006591 if (ISC.checkAndSetInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006592 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006593
6594 bool HasErrors = false;
6595
6596 // Check loop variable's type.
Alexey Bataeve3727102018-04-18 15:57:46 +00006597 if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006598 // OpenMP [2.6, Canonical Loop Form]
6599 // Var is one of the following:
6600 // A variable of signed or unsigned integer type.
6601 // For C++, a variable of a random access iterator type.
6602 // For C, a variable of a pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +00006603 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006604 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
6605 !VarType->isPointerType() &&
6606 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006607 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006608 << SemaRef.getLangOpts().CPlusPlus;
6609 HasErrors = true;
6610 }
6611
6612 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
6613 // a Construct
6614 // The loop iteration variable(s) in the associated for-loop(s) of a for or
6615 // parallel for construct is (are) private.
6616 // The loop iteration variable in the associated for-loop of a simd
6617 // construct with just one associated for-loop is linear with a
6618 // constant-linear-step that is the increment of the associated for-loop.
6619 // Exclude loop var from the list of variables with implicitly defined data
6620 // sharing attributes.
6621 VarsWithImplicitDSA.erase(LCDecl);
6622
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006623 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
6624
6625 // Check test-expr.
Alexey Bataevbef93a92019-10-07 18:54:57 +00006626 HasErrors |= ISC.checkAndSetCond(For ? For->getCond() : CXXFor->getCond());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006627
6628 // Check incr-expr.
Alexey Bataevbef93a92019-10-07 18:54:57 +00006629 HasErrors |= ISC.checkAndSetInc(For ? For->getInc() : CXXFor->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006630 }
6631
Alexey Bataeve3727102018-04-18 15:57:46 +00006632 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006633 return HasErrors;
6634
Alexander Musmana5f070a2014-10-01 06:03:56 +00006635 // Build the loop's iteration space representation.
Alexey Bataevbef93a92019-10-07 18:54:57 +00006636 ResultIterSpaces[CurrentNestedLoopCount].PreCond = ISC.buildPreCond(
6637 DSA.getCurScope(), For ? For->getCond() : CXXFor->getCond(), Captures);
Alexey Bataevf8be4762019-08-14 19:30:06 +00006638 ResultIterSpaces[CurrentNestedLoopCount].NumIterations =
6639 ISC.buildNumIterations(DSA.getCurScope(), ResultIterSpaces,
6640 (isOpenMPWorksharingDirective(DKind) ||
6641 isOpenMPTaskLoopDirective(DKind) ||
6642 isOpenMPDistributeDirective(DKind)),
6643 Captures);
6644 ResultIterSpaces[CurrentNestedLoopCount].CounterVar =
6645 ISC.buildCounterVar(Captures, DSA);
6646 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar =
6647 ISC.buildPrivateCounterVar();
6648 ResultIterSpaces[CurrentNestedLoopCount].CounterInit = ISC.buildCounterInit();
6649 ResultIterSpaces[CurrentNestedLoopCount].CounterStep = ISC.buildCounterStep();
6650 ResultIterSpaces[CurrentNestedLoopCount].InitSrcRange = ISC.getInitSrcRange();
6651 ResultIterSpaces[CurrentNestedLoopCount].CondSrcRange =
6652 ISC.getConditionSrcRange();
6653 ResultIterSpaces[CurrentNestedLoopCount].IncSrcRange =
6654 ISC.getIncrementSrcRange();
6655 ResultIterSpaces[CurrentNestedLoopCount].Subtract = ISC.shouldSubtractStep();
6656 ResultIterSpaces[CurrentNestedLoopCount].IsStrictCompare =
6657 ISC.isStrictTestOp();
6658 std::tie(ResultIterSpaces[CurrentNestedLoopCount].MinValue,
6659 ResultIterSpaces[CurrentNestedLoopCount].MaxValue) =
6660 ISC.buildMinMaxValues(DSA.getCurScope(), Captures);
6661 ResultIterSpaces[CurrentNestedLoopCount].FinalCondition =
6662 ISC.buildFinalCondition(DSA.getCurScope());
6663 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularLB =
6664 ISC.doesInitDependOnLC();
6665 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularUB =
6666 ISC.doesCondDependOnLC();
6667 ResultIterSpaces[CurrentNestedLoopCount].LoopDependentIdx =
6668 ISC.getLoopDependentIdx();
Alexander Musmana5f070a2014-10-01 06:03:56 +00006669
Alexey Bataevf8be4762019-08-14 19:30:06 +00006670 HasErrors |=
6671 (ResultIterSpaces[CurrentNestedLoopCount].PreCond == nullptr ||
6672 ResultIterSpaces[CurrentNestedLoopCount].NumIterations == nullptr ||
6673 ResultIterSpaces[CurrentNestedLoopCount].CounterVar == nullptr ||
6674 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar == nullptr ||
6675 ResultIterSpaces[CurrentNestedLoopCount].CounterInit == nullptr ||
6676 ResultIterSpaces[CurrentNestedLoopCount].CounterStep == nullptr);
Alexey Bataevf138fda2018-08-13 19:04:24 +00006677 if (!HasErrors && DSA.isOrderedRegion()) {
6678 if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
6679 if (CurrentNestedLoopCount <
6680 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
6681 DSA.getOrderedRegionParam().second->setLoopNumIterations(
Alexey Bataevf8be4762019-08-14 19:30:06 +00006682 CurrentNestedLoopCount,
6683 ResultIterSpaces[CurrentNestedLoopCount].NumIterations);
Alexey Bataevf138fda2018-08-13 19:04:24 +00006684 DSA.getOrderedRegionParam().second->setLoopCounter(
Alexey Bataevf8be4762019-08-14 19:30:06 +00006685 CurrentNestedLoopCount,
6686 ResultIterSpaces[CurrentNestedLoopCount].CounterVar);
Alexey Bataevf138fda2018-08-13 19:04:24 +00006687 }
6688 }
6689 for (auto &Pair : DSA.getDoacrossDependClauses()) {
6690 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) {
6691 // Erroneous case - clause has some problems.
6692 continue;
6693 }
6694 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink &&
6695 Pair.second.size() <= CurrentNestedLoopCount) {
6696 // Erroneous case - clause has some problems.
6697 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr);
6698 continue;
6699 }
6700 Expr *CntValue;
6701 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
6702 CntValue = ISC.buildOrderedLoopData(
Alexey Bataevf8be4762019-08-14 19:30:06 +00006703 DSA.getCurScope(),
6704 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
Alexey Bataevf138fda2018-08-13 19:04:24 +00006705 Pair.first->getDependencyLoc());
6706 else
6707 CntValue = ISC.buildOrderedLoopData(
Alexey Bataevf8be4762019-08-14 19:30:06 +00006708 DSA.getCurScope(),
6709 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
Alexey Bataevf138fda2018-08-13 19:04:24 +00006710 Pair.first->getDependencyLoc(),
6711 Pair.second[CurrentNestedLoopCount].first,
6712 Pair.second[CurrentNestedLoopCount].second);
6713 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue);
6714 }
6715 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00006716
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006717 return HasErrors;
6718}
6719
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006720/// Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00006721static ExprResult
Alexey Bataeve3727102018-04-18 15:57:46 +00006722buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
Alexey Bataevf8be4762019-08-14 19:30:06 +00006723 ExprResult Start, bool IsNonRectangularLB,
Alexey Bataeve3727102018-04-18 15:57:46 +00006724 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006725 // Build 'VarRef = Start.
Alexey Bataevf8be4762019-08-14 19:30:06 +00006726 ExprResult NewStart = IsNonRectangularLB
6727 ? Start.get()
6728 : tryBuildCapture(SemaRef, Start.get(), Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00006729 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006730 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00006731 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00006732 VarRef.get()->getType())) {
6733 NewStart = SemaRef.PerformImplicitConversion(
6734 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
6735 /*AllowExplicit=*/true);
6736 if (!NewStart.isUsable())
6737 return ExprError();
6738 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006739
Alexey Bataeve3727102018-04-18 15:57:46 +00006740 ExprResult Init =
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006741 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
6742 return Init;
6743}
6744
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006745/// Build 'VarRef = Start + Iter * Step'.
Alexey Bataeve3727102018-04-18 15:57:46 +00006746static ExprResult buildCounterUpdate(
6747 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
6748 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
Alexey Bataevf8be4762019-08-14 19:30:06 +00006749 bool IsNonRectangularLB,
Alexey Bataeve3727102018-04-18 15:57:46 +00006750 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00006751 // Add parentheses (for debugging purposes only).
6752 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
6753 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
6754 !Step.isUsable())
6755 return ExprError();
6756
Alexey Bataev5a3af132016-03-29 08:58:54 +00006757 ExprResult NewStep = Step;
6758 if (Captures)
6759 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006760 if (NewStep.isInvalid())
6761 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006762 ExprResult Update =
6763 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00006764 if (!Update.isUsable())
6765 return ExprError();
6766
Alexey Bataevc0214e02016-02-16 12:13:49 +00006767 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
6768 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataevf8be4762019-08-14 19:30:06 +00006769 if (!Start.isUsable())
6770 return ExprError();
6771 ExprResult NewStart = SemaRef.ActOnParenExpr(Loc, Loc, Start.get());
6772 if (!NewStart.isUsable())
6773 return ExprError();
6774 if (Captures && !IsNonRectangularLB)
Alexey Bataev5a3af132016-03-29 08:58:54 +00006775 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006776 if (NewStart.isInvalid())
6777 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00006778
Alexey Bataevc0214e02016-02-16 12:13:49 +00006779 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
6780 ExprResult SavedUpdate = Update;
6781 ExprResult UpdateVal;
6782 if (VarRef.get()->getType()->isOverloadableType() ||
6783 NewStart.get()->getType()->isOverloadableType() ||
6784 Update.get()->getType()->isOverloadableType()) {
Richard Smith2e3ed4a2019-08-16 19:53:22 +00006785 Sema::TentativeAnalysisScope Trap(SemaRef);
6786
Alexey Bataevc0214e02016-02-16 12:13:49 +00006787 Update =
6788 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
6789 if (Update.isUsable()) {
6790 UpdateVal =
6791 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
6792 VarRef.get(), SavedUpdate.get());
6793 if (UpdateVal.isUsable()) {
6794 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
6795 UpdateVal.get());
6796 }
6797 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00006798 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00006799
Alexey Bataevc0214e02016-02-16 12:13:49 +00006800 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
6801 if (!Update.isUsable() || !UpdateVal.isUsable()) {
6802 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
6803 NewStart.get(), SavedUpdate.get());
6804 if (!Update.isUsable())
6805 return ExprError();
6806
Alexey Bataev11481f52016-02-17 10:29:05 +00006807 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
6808 VarRef.get()->getType())) {
6809 Update = SemaRef.PerformImplicitConversion(
6810 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
6811 if (!Update.isUsable())
6812 return ExprError();
6813 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00006814
6815 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
6816 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00006817 return Update;
6818}
6819
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006820/// Convert integer expression \a E to make it have at least \a Bits
Alexander Musmana5f070a2014-10-01 06:03:56 +00006821/// bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00006822static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00006823 if (E == nullptr)
6824 return ExprError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006825 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00006826 QualType OldType = E->getType();
6827 unsigned HasBits = C.getTypeSize(OldType);
6828 if (HasBits >= Bits)
6829 return ExprResult(E);
6830 // OK to convert to signed, because new type has more bits than old.
6831 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
6832 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
6833 true);
6834}
6835
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006836/// Check if the given expression \a E is a constant integer that fits
Alexander Musmana5f070a2014-10-01 06:03:56 +00006837/// into \a Bits bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00006838static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00006839 if (E == nullptr)
6840 return false;
6841 llvm::APSInt Result;
6842 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
6843 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
6844 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006845}
6846
Alexey Bataev5a3af132016-03-29 08:58:54 +00006847/// Build preinits statement for the given declarations.
6848static Stmt *buildPreInits(ASTContext &Context,
Alexey Bataevc5514062017-10-25 15:44:52 +00006849 MutableArrayRef<Decl *> PreInits) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00006850 if (!PreInits.empty()) {
6851 return new (Context) DeclStmt(
6852 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
6853 SourceLocation(), SourceLocation());
6854 }
6855 return nullptr;
6856}
6857
6858/// Build preinits statement for the given declarations.
Alexey Bataevc5514062017-10-25 15:44:52 +00006859static Stmt *
6860buildPreInits(ASTContext &Context,
Alexey Bataeve3727102018-04-18 15:57:46 +00006861 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00006862 if (!Captures.empty()) {
6863 SmallVector<Decl *, 16> PreInits;
Alexey Bataeve3727102018-04-18 15:57:46 +00006864 for (const auto &Pair : Captures)
Alexey Bataev5a3af132016-03-29 08:58:54 +00006865 PreInits.push_back(Pair.second->getDecl());
6866 return buildPreInits(Context, PreInits);
6867 }
6868 return nullptr;
6869}
6870
6871/// Build postupdate expression for the given list of postupdates expressions.
6872static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
6873 Expr *PostUpdate = nullptr;
6874 if (!PostUpdates.empty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006875 for (Expr *E : PostUpdates) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00006876 Expr *ConvE = S.BuildCStyleCastExpr(
6877 E->getExprLoc(),
6878 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
6879 E->getExprLoc(), E)
6880 .get();
6881 PostUpdate = PostUpdate
6882 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
6883 PostUpdate, ConvE)
6884 .get()
6885 : ConvE;
6886 }
6887 }
6888 return PostUpdate;
6889}
6890
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006891/// Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00006892/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
6893/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00006894static unsigned
Alexey Bataeve3727102018-04-18 15:57:46 +00006895checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
Alexey Bataev10e775f2015-07-30 11:36:16 +00006896 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
6897 DSAStackTy &DSA,
Alexey Bataeve3727102018-04-18 15:57:46 +00006898 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00006899 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00006900 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006901 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00006902 // Found 'collapse' clause - calculate collapse number.
Fangrui Song407659a2018-11-30 23:41:18 +00006903 Expr::EvalResult Result;
Dmitri Gribenko04323c22019-05-17 17:16:53 +00006904 if (!CollapseLoopCountExpr->isValueDependent() &&
6905 CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
Fangrui Song407659a2018-11-30 23:41:18 +00006906 NestedLoopCount = Result.Val.getInt().getLimitedValue();
Dmitri Gribenko04323c22019-05-17 17:16:53 +00006907 } else {
Rui Ueyama49a3ad22019-07-16 04:46:31 +00006908 Built.clear(/*Size=*/1);
Dmitri Gribenko04323c22019-05-17 17:16:53 +00006909 return 1;
6910 }
Alexey Bataev10e775f2015-07-30 11:36:16 +00006911 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00006912 unsigned OrderedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006913 if (OrderedLoopCountExpr) {
6914 // Found 'ordered' clause - calculate collapse number.
Fangrui Song407659a2018-11-30 23:41:18 +00006915 Expr::EvalResult EVResult;
Dmitri Gribenko04323c22019-05-17 17:16:53 +00006916 if (!OrderedLoopCountExpr->isValueDependent() &&
6917 OrderedLoopCountExpr->EvaluateAsInt(EVResult,
6918 SemaRef.getASTContext())) {
Fangrui Song407659a2018-11-30 23:41:18 +00006919 llvm::APSInt Result = EVResult.Val.getInt();
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006920 if (Result.getLimitedValue() < NestedLoopCount) {
6921 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
6922 diag::err_omp_wrong_ordered_loop_count)
6923 << OrderedLoopCountExpr->getSourceRange();
6924 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
6925 diag::note_collapse_loop_count)
6926 << CollapseLoopCountExpr->getSourceRange();
6927 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00006928 OrderedLoopCount = Result.getLimitedValue();
Dmitri Gribenko04323c22019-05-17 17:16:53 +00006929 } else {
Rui Ueyama49a3ad22019-07-16 04:46:31 +00006930 Built.clear(/*Size=*/1);
Dmitri Gribenko04323c22019-05-17 17:16:53 +00006931 return 1;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006932 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00006933 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006934 // This is helper routine for loop directives (e.g., 'for', 'simd',
6935 // 'for simd', etc.).
Alexey Bataeve3727102018-04-18 15:57:46 +00006936 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev316ccf62019-01-29 18:51:58 +00006937 SmallVector<LoopIterationSpace, 4> IterSpaces(
6938 std::max(OrderedLoopCount, NestedLoopCount));
Alexander Musmana5f070a2014-10-01 06:03:56 +00006939 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006940 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00006941 if (checkOpenMPIterationSpace(
6942 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
6943 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
Alexey Bataevf8be4762019-08-14 19:30:06 +00006944 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces, Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00006945 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006946 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00006947 // OpenMP [2.8.1, simd construct, Restrictions]
6948 // All loops associated with the construct must be perfectly nested; that
6949 // is, there must be no intervening code nor any OpenMP directive between
6950 // any two loops.
Alexey Bataevbef93a92019-10-07 18:54:57 +00006951 if (auto *For = dyn_cast<ForStmt>(CurStmt)) {
6952 CurStmt = For->getBody();
6953 } else {
6954 assert(isa<CXXForRangeStmt>(CurStmt) &&
6955 "Expected canonical for or range-based for loops.");
6956 CurStmt = cast<CXXForRangeStmt>(CurStmt)->getBody();
6957 }
6958 CurStmt = CurStmt->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006959 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00006960 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) {
6961 if (checkOpenMPIterationSpace(
6962 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
6963 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
Alexey Bataevf8be4762019-08-14 19:30:06 +00006964 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces, Captures))
Alexey Bataevf138fda2018-08-13 19:04:24 +00006965 return 0;
6966 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) {
6967 // Handle initialization of captured loop iterator variables.
6968 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar);
6969 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) {
6970 Captures[DRE] = DRE;
6971 }
6972 }
6973 // Move on to the next nested for loop, or to the loop body.
6974 // OpenMP [2.8.1, simd construct, Restrictions]
6975 // All loops associated with the construct must be perfectly nested; that
6976 // is, there must be no intervening code nor any OpenMP directive between
6977 // any two loops.
Alexey Bataevbef93a92019-10-07 18:54:57 +00006978 if (auto *For = dyn_cast<ForStmt>(CurStmt)) {
6979 CurStmt = For->getBody();
6980 } else {
6981 assert(isa<CXXForRangeStmt>(CurStmt) &&
6982 "Expected canonical for or range-based for loops.");
6983 CurStmt = cast<CXXForRangeStmt>(CurStmt)->getBody();
6984 }
6985 CurStmt = CurStmt->IgnoreContainers();
Alexey Bataevf138fda2018-08-13 19:04:24 +00006986 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006987
Alexander Musmana5f070a2014-10-01 06:03:56 +00006988 Built.clear(/* size */ NestedLoopCount);
6989
6990 if (SemaRef.CurContext->isDependentContext())
6991 return NestedLoopCount;
6992
6993 // An example of what is generated for the following code:
6994 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00006995 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00006996 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00006997 // for (k = 0; k < NK; ++k)
6998 // for (j = J0; j < NJ; j+=2) {
6999 // <loop body>
7000 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00007001 //
7002 // We generate the code below.
7003 // Note: the loop body may be outlined in CodeGen.
7004 // Note: some counters may be C++ classes, operator- is used to find number of
7005 // iterations and operator+= to calculate counter value.
7006 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
7007 // or i64 is currently supported).
7008 //
7009 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
7010 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
7011 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
7012 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
7013 // // similar updates for vars in clauses (e.g. 'linear')
7014 // <loop body (using local i and j)>
7015 // }
7016 // i = NI; // assign final values of counters
7017 // j = NJ;
7018 //
7019
7020 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
7021 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00007022 // Precondition tests if there is at least one iteration (all conditions are
7023 // true).
7024 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexey Bataeve3727102018-04-18 15:57:46 +00007025 Expr *N0 = IterSpaces[0].NumIterations;
7026 ExprResult LastIteration32 =
7027 widenIterationCount(/*Bits=*/32,
7028 SemaRef
7029 .PerformImplicitConversion(
7030 N0->IgnoreImpCasts(), N0->getType(),
7031 Sema::AA_Converting, /*AllowExplicit=*/true)
7032 .get(),
7033 SemaRef);
7034 ExprResult LastIteration64 = widenIterationCount(
7035 /*Bits=*/64,
7036 SemaRef
7037 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(),
7038 Sema::AA_Converting,
7039 /*AllowExplicit=*/true)
7040 .get(),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00007041 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00007042
7043 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
7044 return NestedLoopCount;
7045
Alexey Bataeve3727102018-04-18 15:57:46 +00007046 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00007047 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
7048
7049 Scope *CurScope = DSA.getCurScope();
7050 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00007051 if (PreCond.isUsable()) {
Alexey Bataeva7206b92016-12-20 16:51:02 +00007052 PreCond =
7053 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
7054 PreCond.get(), IterSpaces[Cnt].PreCond);
Alexey Bataev62dbb972015-04-22 11:59:37 +00007055 }
Alexey Bataeve3727102018-04-18 15:57:46 +00007056 Expr *N = IterSpaces[Cnt].NumIterations;
Alexey Bataeva7206b92016-12-20 16:51:02 +00007057 SourceLocation Loc = N->getExprLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00007058 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
7059 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00007060 LastIteration32 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00007061 CurScope, Loc, BO_Mul, LastIteration32.get(),
David Majnemer9d168222016-08-05 17:44:54 +00007062 SemaRef
7063 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
7064 Sema::AA_Converting,
7065 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00007066 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00007067 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00007068 LastIteration64 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00007069 CurScope, Loc, BO_Mul, LastIteration64.get(),
David Majnemer9d168222016-08-05 17:44:54 +00007070 SemaRef
7071 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
7072 Sema::AA_Converting,
7073 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00007074 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00007075 }
7076
7077 // Choose either the 32-bit or 64-bit version.
7078 ExprResult LastIteration = LastIteration64;
Gheorghe-Teodor Berceaa3afcf22019-01-09 20:38:35 +00007079 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse ||
7080 (LastIteration32.isUsable() &&
7081 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
7082 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
7083 fitsInto(
7084 /*Bits=*/32,
7085 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
7086 LastIteration64.get(), SemaRef))))
Alexander Musmana5f070a2014-10-01 06:03:56 +00007087 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00007088 QualType VType = LastIteration.get()->getType();
7089 QualType RealVType = VType;
7090 QualType StrideVType = VType;
7091 if (isOpenMPTaskLoopDirective(DKind)) {
7092 VType =
7093 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
7094 StrideVType =
7095 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
7096 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00007097
7098 if (!LastIteration.isUsable())
7099 return 0;
7100
7101 // Save the number of iterations.
7102 ExprResult NumIterations = LastIteration;
7103 {
7104 LastIteration = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00007105 CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
7106 LastIteration.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00007107 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
7108 if (!LastIteration.isUsable())
7109 return 0;
7110 }
7111
7112 // Calculate the last iteration number beforehand instead of doing this on
7113 // each iteration. Do not do this if the number of iterations may be kfold-ed.
7114 llvm::APSInt Result;
7115 bool IsConstant =
7116 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
7117 ExprResult CalcLastIteration;
7118 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00007119 ExprResult SaveRef =
7120 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00007121 LastIteration = SaveRef;
7122
7123 // Prepare SaveRef + 1.
7124 NumIterations = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00007125 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00007126 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
7127 if (!NumIterations.isUsable())
7128 return 0;
7129 }
7130
7131 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
7132
David Majnemer9d168222016-08-05 17:44:54 +00007133 // Build variables passed into runtime, necessary for worksharing directives.
Carlo Bertolliffafe102017-04-20 00:39:39 +00007134 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007135 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
7136 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00007137 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007138 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
7139 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00007140 SemaRef.AddInitializerToDecl(LBDecl,
7141 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
7142 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00007143
7144 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007145 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
7146 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00007147 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00007148 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00007149
7150 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
7151 // This will be used to implement clause 'lastprivate'.
7152 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007153 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
7154 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00007155 SemaRef.AddInitializerToDecl(ILDecl,
7156 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
7157 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00007158
7159 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00007160 VarDecl *STDecl =
7161 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
7162 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00007163 SemaRef.AddInitializerToDecl(STDecl,
7164 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
7165 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00007166
7167 // Build expression: UB = min(UB, LastIteration)
David Majnemer9d168222016-08-05 17:44:54 +00007168 // It is necessary for CodeGen of directives with static scheduling.
Alexander Musmanc6388682014-12-15 07:07:06 +00007169 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
7170 UB.get(), LastIteration.get());
7171 ExprResult CondOp = SemaRef.ActOnConditionalOp(
Alexey Bataev86ec3fe2018-07-25 14:40:26 +00007172 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(),
7173 LastIteration.get(), UB.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00007174 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
7175 CondOp.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00007176 EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false);
Carlo Bertolli9925f152016-06-27 14:55:37 +00007177
7178 // If we have a combined directive that combines 'distribute', 'for' or
7179 // 'simd' we need to be able to access the bounds of the schedule of the
7180 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
7181 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
7182 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Carlo Bertolliffafe102017-04-20 00:39:39 +00007183 // Lower bound variable, initialized with zero.
7184 VarDecl *CombLBDecl =
7185 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
7186 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
7187 SemaRef.AddInitializerToDecl(
7188 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
7189 /*DirectInit*/ false);
7190
7191 // Upper bound variable, initialized with last iteration number.
7192 VarDecl *CombUBDecl =
7193 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
7194 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
7195 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
7196 /*DirectInit*/ false);
7197
7198 ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
7199 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
7200 ExprResult CombCondOp =
7201 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
7202 LastIteration.get(), CombUB.get());
7203 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
7204 CombCondOp.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00007205 CombEUB =
7206 SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00007207
Alexey Bataeve3727102018-04-18 15:57:46 +00007208 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
Carlo Bertolli9925f152016-06-27 14:55:37 +00007209 // We expect to have at least 2 more parameters than the 'parallel'
7210 // directive does - the lower and upper bounds of the previous schedule.
7211 assert(CD->getNumParams() >= 4 &&
7212 "Unexpected number of parameters in loop combined directive");
7213
7214 // Set the proper type for the bounds given what we learned from the
7215 // enclosed loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00007216 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
7217 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
Carlo Bertolli9925f152016-06-27 14:55:37 +00007218
7219 // Previous lower and upper bounds are obtained from the region
7220 // parameters.
7221 PrevLB =
7222 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
7223 PrevUB =
7224 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
7225 }
Alexander Musmanc6388682014-12-15 07:07:06 +00007226 }
7227
7228 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00007229 ExprResult IV;
Carlo Bertolliffafe102017-04-20 00:39:39 +00007230 ExprResult Init, CombInit;
Alexander Musmana5f070a2014-10-01 06:03:56 +00007231 {
Alexey Bataev7292c292016-04-25 12:22:29 +00007232 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
7233 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
David Majnemer9d168222016-08-05 17:44:54 +00007234 Expr *RHS =
7235 (isOpenMPWorksharingDirective(DKind) ||
7236 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
7237 ? LB.get()
7238 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
Alexander Musmanc6388682014-12-15 07:07:06 +00007239 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00007240 Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00007241
7242 if (isOpenMPLoopBoundSharingDirective(DKind)) {
7243 Expr *CombRHS =
7244 (isOpenMPWorksharingDirective(DKind) ||
7245 isOpenMPTaskLoopDirective(DKind) ||
7246 isOpenMPDistributeDirective(DKind))
7247 ? CombLB.get()
7248 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
7249 CombInit =
7250 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00007251 CombInit =
7252 SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00007253 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00007254 }
7255
Alexey Bataev316ccf62019-01-29 18:51:58 +00007256 bool UseStrictCompare =
7257 RealVType->hasUnsignedIntegerRepresentation() &&
7258 llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) {
7259 return LIS.IsStrictCompare;
7260 });
7261 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for
7262 // unsigned IV)) for worksharing loops.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007263 SourceLocation CondLoc = AStmt->getBeginLoc();
Alexey Bataev316ccf62019-01-29 18:51:58 +00007264 Expr *BoundUB = UB.get();
7265 if (UseStrictCompare) {
7266 BoundUB =
7267 SemaRef
7268 .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB,
7269 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
7270 .get();
7271 BoundUB =
7272 SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get();
7273 }
Alexander Musmanc6388682014-12-15 07:07:06 +00007274 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007275 (isOpenMPWorksharingDirective(DKind) ||
7276 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexey Bataev316ccf62019-01-29 18:51:58 +00007277 ? SemaRef.BuildBinOp(CurScope, CondLoc,
7278 UseStrictCompare ? BO_LT : BO_LE, IV.get(),
7279 BoundUB)
Alexander Musmanc6388682014-12-15 07:07:06 +00007280 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
7281 NumIterations.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00007282 ExprResult CombDistCond;
7283 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Alexey Bataev316ccf62019-01-29 18:51:58 +00007284 CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
7285 NumIterations.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00007286 }
7287
Carlo Bertolliffafe102017-04-20 00:39:39 +00007288 ExprResult CombCond;
7289 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Alexey Bataev316ccf62019-01-29 18:51:58 +00007290 Expr *BoundCombUB = CombUB.get();
7291 if (UseStrictCompare) {
7292 BoundCombUB =
7293 SemaRef
7294 .BuildBinOp(
7295 CurScope, CondLoc, BO_Add, BoundCombUB,
7296 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
7297 .get();
7298 BoundCombUB =
7299 SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false)
7300 .get();
7301 }
Carlo Bertolliffafe102017-04-20 00:39:39 +00007302 CombCond =
Alexey Bataev316ccf62019-01-29 18:51:58 +00007303 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE,
7304 IV.get(), BoundCombUB);
Carlo Bertolliffafe102017-04-20 00:39:39 +00007305 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00007306 // Loop increment (IV = IV + 1)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007307 SourceLocation IncLoc = AStmt->getBeginLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00007308 ExprResult Inc =
7309 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
7310 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
7311 if (!Inc.isUsable())
7312 return 0;
7313 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00007314 Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00007315 if (!Inc.isUsable())
7316 return 0;
7317
7318 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
7319 // Used for directives with static scheduling.
Carlo Bertolliffafe102017-04-20 00:39:39 +00007320 // In combined construct, add combined version that use CombLB and CombUB
7321 // base variables for the update
7322 ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007323 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
7324 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00007325 // LB + ST
7326 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
7327 if (!NextLB.isUsable())
7328 return 0;
7329 // LB = LB + ST
7330 NextLB =
7331 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00007332 NextLB =
7333 SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00007334 if (!NextLB.isUsable())
7335 return 0;
7336 // UB + ST
7337 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
7338 if (!NextUB.isUsable())
7339 return 0;
7340 // UB = UB + ST
7341 NextUB =
7342 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00007343 NextUB =
7344 SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00007345 if (!NextUB.isUsable())
7346 return 0;
Carlo Bertolliffafe102017-04-20 00:39:39 +00007347 if (isOpenMPLoopBoundSharingDirective(DKind)) {
7348 CombNextLB =
7349 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
7350 if (!NextLB.isUsable())
7351 return 0;
7352 // LB = LB + ST
7353 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
7354 CombNextLB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00007355 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(),
7356 /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00007357 if (!CombNextLB.isUsable())
7358 return 0;
7359 // UB + ST
7360 CombNextUB =
7361 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
7362 if (!CombNextUB.isUsable())
7363 return 0;
7364 // UB = UB + ST
7365 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
7366 CombNextUB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00007367 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(),
7368 /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00007369 if (!CombNextUB.isUsable())
7370 return 0;
7371 }
Alexander Musmanc6388682014-12-15 07:07:06 +00007372 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00007373
Carlo Bertolliffafe102017-04-20 00:39:39 +00007374 // Create increment expression for distribute loop when combined in a same
Carlo Bertolli8429d812017-02-17 21:29:13 +00007375 // directive with for as IV = IV + ST; ensure upper bound expression based
7376 // on PrevUB instead of NumIterations - used to implement 'for' when found
7377 // in combination with 'distribute', like in 'distribute parallel for'
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007378 SourceLocation DistIncLoc = AStmt->getBeginLoc();
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00007379 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond;
Carlo Bertolli8429d812017-02-17 21:29:13 +00007380 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Alexey Bataev316ccf62019-01-29 18:51:58 +00007381 DistCond = SemaRef.BuildBinOp(
7382 CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB);
Carlo Bertolli8429d812017-02-17 21:29:13 +00007383 assert(DistCond.isUsable() && "distribute cond expr was not built");
7384
7385 DistInc =
7386 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
7387 assert(DistInc.isUsable() && "distribute inc expr was not built");
7388 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
7389 DistInc.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00007390 DistInc =
7391 SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false);
Carlo Bertolli8429d812017-02-17 21:29:13 +00007392 assert(DistInc.isUsable() && "distribute inc expr was not built");
7393
7394 // Build expression: UB = min(UB, prevUB) for #for in composite or combined
7395 // construct
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007396 SourceLocation DistEUBLoc = AStmt->getBeginLoc();
Carlo Bertolli8429d812017-02-17 21:29:13 +00007397 ExprResult IsUBGreater =
7398 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
7399 ExprResult CondOp = SemaRef.ActOnConditionalOp(
7400 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
7401 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
7402 CondOp.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00007403 PrevEUB =
7404 SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false);
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00007405
Alexey Bataev316ccf62019-01-29 18:51:58 +00007406 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in
7407 // parallel for is in combination with a distribute directive with
7408 // schedule(static, 1)
7409 Expr *BoundPrevUB = PrevUB.get();
7410 if (UseStrictCompare) {
7411 BoundPrevUB =
7412 SemaRef
7413 .BuildBinOp(
7414 CurScope, CondLoc, BO_Add, BoundPrevUB,
7415 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
7416 .get();
7417 BoundPrevUB =
7418 SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false)
7419 .get();
7420 }
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00007421 ParForInDistCond =
Alexey Bataev316ccf62019-01-29 18:51:58 +00007422 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE,
7423 IV.get(), BoundPrevUB);
Carlo Bertolli8429d812017-02-17 21:29:13 +00007424 }
7425
Alexander Musmana5f070a2014-10-01 06:03:56 +00007426 // Build updates and final values of the loop counters.
7427 bool HasErrors = false;
7428 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00007429 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00007430 Built.Updates.resize(NestedLoopCount);
7431 Built.Finals.resize(NestedLoopCount);
Alexey Bataevf8be4762019-08-14 19:30:06 +00007432 Built.DependentCounters.resize(NestedLoopCount);
7433 Built.DependentInits.resize(NestedLoopCount);
7434 Built.FinalsConditions.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00007435 {
Gheorghe-Teodor Bercea677960642019-01-09 20:45:26 +00007436 // We implement the following algorithm for obtaining the
7437 // original loop iteration variable values based on the
7438 // value of the collapsed loop iteration variable IV.
7439 //
7440 // Let n+1 be the number of collapsed loops in the nest.
7441 // Iteration variables (I0, I1, .... In)
7442 // Iteration counts (N0, N1, ... Nn)
7443 //
7444 // Acc = IV;
7445 //
7446 // To compute Ik for loop k, 0 <= k <= n, generate:
7447 // Prod = N(k+1) * N(k+2) * ... * Nn;
7448 // Ik = Acc / Prod;
7449 // Acc -= Ik * Prod;
7450 //
7451 ExprResult Acc = IV;
7452 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00007453 LoopIterationSpace &IS = IterSpaces[Cnt];
7454 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
Alexander Musmana5f070a2014-10-01 06:03:56 +00007455 ExprResult Iter;
Alexander Musmana5f070a2014-10-01 06:03:56 +00007456
Gheorghe-Teodor Bercea677960642019-01-09 20:45:26 +00007457 // Compute prod
7458 ExprResult Prod =
7459 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
7460 for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K)
7461 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(),
7462 IterSpaces[K].NumIterations);
7463
7464 // Iter = Acc / Prod
7465 // If there is at least one more inner loop to avoid
7466 // multiplication by 1.
7467 if (Cnt + 1 < NestedLoopCount)
7468 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div,
7469 Acc.get(), Prod.get());
7470 else
7471 Iter = Acc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00007472 if (!Iter.isUsable()) {
7473 HasErrors = true;
7474 break;
7475 }
7476
Gheorghe-Teodor Bercea677960642019-01-09 20:45:26 +00007477 // Update Acc:
7478 // Acc -= Iter * Prod
7479 // Check if there is at least one more inner loop to avoid
7480 // multiplication by 1.
7481 if (Cnt + 1 < NestedLoopCount)
7482 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul,
7483 Iter.get(), Prod.get());
7484 else
7485 Prod = Iter;
7486 Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub,
7487 Acc.get(), Prod.get());
7488
Alexey Bataev39f915b82015-05-08 10:41:21 +00007489 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007490 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00007491 DeclRefExpr *CounterVar = buildDeclRefExpr(
7492 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
7493 /*RefersToCapture=*/true);
Alexey Bataevf8be4762019-08-14 19:30:06 +00007494 ExprResult Init =
7495 buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
7496 IS.CounterInit, IS.IsNonRectangularLB, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00007497 if (!Init.isUsable()) {
7498 HasErrors = true;
7499 break;
7500 }
Alexey Bataeve3727102018-04-18 15:57:46 +00007501 ExprResult Update = buildCounterUpdate(
Alexey Bataev5a3af132016-03-29 08:58:54 +00007502 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
Alexey Bataevf8be4762019-08-14 19:30:06 +00007503 IS.CounterStep, IS.Subtract, IS.IsNonRectangularLB, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00007504 if (!Update.isUsable()) {
7505 HasErrors = true;
7506 break;
7507 }
7508
7509 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
Alexey Bataevf8be4762019-08-14 19:30:06 +00007510 ExprResult Final =
7511 buildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
7512 IS.CounterInit, IS.NumIterations, IS.CounterStep,
7513 IS.Subtract, IS.IsNonRectangularLB, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00007514 if (!Final.isUsable()) {
7515 HasErrors = true;
7516 break;
7517 }
7518
Alexander Musmana5f070a2014-10-01 06:03:56 +00007519 if (!Update.isUsable() || !Final.isUsable()) {
7520 HasErrors = true;
7521 break;
7522 }
7523 // Save results
7524 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00007525 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00007526 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00007527 Built.Updates[Cnt] = Update.get();
7528 Built.Finals[Cnt] = Final.get();
Alexey Bataevf8be4762019-08-14 19:30:06 +00007529 Built.DependentCounters[Cnt] = nullptr;
7530 Built.DependentInits[Cnt] = nullptr;
7531 Built.FinalsConditions[Cnt] = nullptr;
Alexey Bataev658ad4d2019-10-01 16:19:10 +00007532 if (IS.IsNonRectangularLB || IS.IsNonRectangularUB) {
Alexey Bataevf8be4762019-08-14 19:30:06 +00007533 Built.DependentCounters[Cnt] =
7534 Built.Counters[NestedLoopCount - 1 - IS.LoopDependentIdx];
7535 Built.DependentInits[Cnt] =
7536 Built.Inits[NestedLoopCount - 1 - IS.LoopDependentIdx];
7537 Built.FinalsConditions[Cnt] = IS.FinalCondition;
7538 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00007539 }
7540 }
7541
7542 if (HasErrors)
7543 return 0;
7544
7545 // Save results
7546 Built.IterationVarRef = IV.get();
7547 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00007548 Built.NumIterations = NumIterations.get();
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00007549 Built.CalcLastIteration = SemaRef
7550 .ActOnFinishFullExpr(CalcLastIteration.get(),
Alexey Bataevf8be4762019-08-14 19:30:06 +00007551 /*DiscardedValue=*/false)
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00007552 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00007553 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00007554 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00007555 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00007556 Built.Init = Init.get();
7557 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00007558 Built.LB = LB.get();
7559 Built.UB = UB.get();
7560 Built.IL = IL.get();
7561 Built.ST = ST.get();
7562 Built.EUB = EUB.get();
7563 Built.NLB = NextLB.get();
7564 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00007565 Built.PrevLB = PrevLB.get();
7566 Built.PrevUB = PrevUB.get();
Carlo Bertolli8429d812017-02-17 21:29:13 +00007567 Built.DistInc = DistInc.get();
7568 Built.PrevEUB = PrevEUB.get();
Carlo Bertolliffafe102017-04-20 00:39:39 +00007569 Built.DistCombinedFields.LB = CombLB.get();
7570 Built.DistCombinedFields.UB = CombUB.get();
7571 Built.DistCombinedFields.EUB = CombEUB.get();
7572 Built.DistCombinedFields.Init = CombInit.get();
7573 Built.DistCombinedFields.Cond = CombCond.get();
7574 Built.DistCombinedFields.NLB = CombNextLB.get();
7575 Built.DistCombinedFields.NUB = CombNextUB.get();
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00007576 Built.DistCombinedFields.DistCond = CombDistCond.get();
7577 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00007578
Alexey Bataevabfc0692014-06-25 06:52:00 +00007579 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007580}
7581
Alexey Bataev10e775f2015-07-30 11:36:16 +00007582static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00007583 auto CollapseClauses =
7584 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
7585 if (CollapseClauses.begin() != CollapseClauses.end())
7586 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00007587 return nullptr;
7588}
7589
Alexey Bataev10e775f2015-07-30 11:36:16 +00007590static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00007591 auto OrderedClauses =
7592 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
7593 if (OrderedClauses.begin() != OrderedClauses.end())
7594 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00007595 return nullptr;
7596}
7597
Kelvin Lic5609492016-07-15 04:39:07 +00007598static bool checkSimdlenSafelenSpecified(Sema &S,
7599 const ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007600 const OMPSafelenClause *Safelen = nullptr;
7601 const OMPSimdlenClause *Simdlen = nullptr;
Kelvin Lic5609492016-07-15 04:39:07 +00007602
Alexey Bataeve3727102018-04-18 15:57:46 +00007603 for (const OMPClause *Clause : Clauses) {
Kelvin Lic5609492016-07-15 04:39:07 +00007604 if (Clause->getClauseKind() == OMPC_safelen)
7605 Safelen = cast<OMPSafelenClause>(Clause);
7606 else if (Clause->getClauseKind() == OMPC_simdlen)
7607 Simdlen = cast<OMPSimdlenClause>(Clause);
7608 if (Safelen && Simdlen)
7609 break;
7610 }
7611
7612 if (Simdlen && Safelen) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007613 const Expr *SimdlenLength = Simdlen->getSimdlen();
7614 const Expr *SafelenLength = Safelen->getSafelen();
Kelvin Lic5609492016-07-15 04:39:07 +00007615 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
7616 SimdlenLength->isInstantiationDependent() ||
7617 SimdlenLength->containsUnexpandedParameterPack())
7618 return false;
7619 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
7620 SafelenLength->isInstantiationDependent() ||
7621 SafelenLength->containsUnexpandedParameterPack())
7622 return false;
Fangrui Song407659a2018-11-30 23:41:18 +00007623 Expr::EvalResult SimdlenResult, SafelenResult;
7624 SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context);
7625 SafelenLength->EvaluateAsInt(SafelenResult, S.Context);
7626 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt();
7627 llvm::APSInt SafelenRes = SafelenResult.Val.getInt();
Kelvin Lic5609492016-07-15 04:39:07 +00007628 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
7629 // If both simdlen and safelen clauses are specified, the value of the
7630 // simdlen parameter must be less than or equal to the value of the safelen
7631 // parameter.
7632 if (SimdlenRes > SafelenRes) {
7633 S.Diag(SimdlenLength->getExprLoc(),
7634 diag::err_omp_wrong_simdlen_safelen_values)
7635 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
7636 return true;
7637 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00007638 }
7639 return false;
7640}
7641
Alexey Bataeve3727102018-04-18 15:57:46 +00007642StmtResult
7643Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
7644 SourceLocation StartLoc, SourceLocation EndLoc,
7645 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007646 if (!AStmt)
7647 return StmtError();
7648
7649 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00007650 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007651 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7652 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007653 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00007654 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
7655 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00007656 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00007657 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00007658
Alexander Musmana5f070a2014-10-01 06:03:56 +00007659 assert((CurContext->isDependentContext() || B.builtAll()) &&
7660 "omp simd loop exprs were not built");
7661
Alexander Musman3276a272015-03-21 10:12:56 +00007662 if (!CurContext->isDependentContext()) {
7663 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007664 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007665 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexander Musman3276a272015-03-21 10:12:56 +00007666 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007667 B.NumIterations, *this, CurScope,
7668 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00007669 return StmtError();
7670 }
7671 }
7672
Kelvin Lic5609492016-07-15 04:39:07 +00007673 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00007674 return StmtError();
7675
Reid Kleckner87a31802018-03-12 21:43:02 +00007676 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00007677 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
7678 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00007679}
7680
Alexey Bataeve3727102018-04-18 15:57:46 +00007681StmtResult
7682Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
7683 SourceLocation StartLoc, SourceLocation EndLoc,
7684 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007685 if (!AStmt)
7686 return StmtError();
7687
7688 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00007689 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007690 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7691 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007692 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00007693 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
7694 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00007695 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00007696 return StmtError();
7697
Alexander Musmana5f070a2014-10-01 06:03:56 +00007698 assert((CurContext->isDependentContext() || B.builtAll()) &&
7699 "omp for loop exprs were not built");
7700
Alexey Bataev54acd402015-08-04 11:18:19 +00007701 if (!CurContext->isDependentContext()) {
7702 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007703 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007704 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00007705 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007706 B.NumIterations, *this, CurScope,
7707 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00007708 return StmtError();
7709 }
7710 }
7711
Reid Kleckner87a31802018-03-12 21:43:02 +00007712 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00007713 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00007714 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00007715}
7716
Alexander Musmanf82886e2014-09-18 05:12:34 +00007717StmtResult Sema::ActOnOpenMPForSimdDirective(
7718 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007719 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007720 if (!AStmt)
7721 return StmtError();
7722
7723 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00007724 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007725 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7726 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00007727 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007728 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00007729 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7730 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00007731 if (NestedLoopCount == 0)
7732 return StmtError();
7733
Alexander Musmanc6388682014-12-15 07:07:06 +00007734 assert((CurContext->isDependentContext() || B.builtAll()) &&
7735 "omp for simd loop exprs were not built");
7736
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00007737 if (!CurContext->isDependentContext()) {
7738 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007739 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007740 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00007741 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007742 B.NumIterations, *this, CurScope,
7743 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00007744 return StmtError();
7745 }
7746 }
7747
Kelvin Lic5609492016-07-15 04:39:07 +00007748 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00007749 return StmtError();
7750
Reid Kleckner87a31802018-03-12 21:43:02 +00007751 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00007752 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
7753 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00007754}
7755
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00007756StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
7757 Stmt *AStmt,
7758 SourceLocation StartLoc,
7759 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007760 if (!AStmt)
7761 return StmtError();
7762
7763 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00007764 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00007765 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00007766 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00007767 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00007768 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00007769 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00007770 return StmtError();
7771 // All associated statements must be '#pragma omp section' except for
7772 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00007773 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00007774 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
7775 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007776 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev1e0498a2014-06-26 08:21:58 +00007777 diag::err_omp_sections_substmt_not_section);
7778 return StmtError();
7779 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00007780 cast<OMPSectionDirective>(SectionStmt)
7781 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00007782 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00007783 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007784 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00007785 return StmtError();
7786 }
7787
Reid Kleckner87a31802018-03-12 21:43:02 +00007788 setFunctionHasBranchProtectedScope();
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00007789
Alexey Bataev25e5b442015-09-15 12:52:43 +00007790 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
7791 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00007792}
7793
Alexey Bataev1e0498a2014-06-26 08:21:58 +00007794StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
7795 SourceLocation StartLoc,
7796 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007797 if (!AStmt)
7798 return StmtError();
7799
7800 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00007801
Reid Kleckner87a31802018-03-12 21:43:02 +00007802 setFunctionHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00007803 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00007804
Alexey Bataev25e5b442015-09-15 12:52:43 +00007805 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
7806 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00007807}
7808
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00007809StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
7810 Stmt *AStmt,
7811 SourceLocation StartLoc,
7812 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007813 if (!AStmt)
7814 return StmtError();
7815
7816 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00007817
Reid Kleckner87a31802018-03-12 21:43:02 +00007818 setFunctionHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00007819
Alexey Bataev3255bf32015-01-19 05:20:46 +00007820 // OpenMP [2.7.3, single Construct, Restrictions]
7821 // The copyprivate clause must not be used with the nowait clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00007822 const OMPClause *Nowait = nullptr;
7823 const OMPClause *Copyprivate = nullptr;
7824 for (const OMPClause *Clause : Clauses) {
Alexey Bataev3255bf32015-01-19 05:20:46 +00007825 if (Clause->getClauseKind() == OMPC_nowait)
7826 Nowait = Clause;
7827 else if (Clause->getClauseKind() == OMPC_copyprivate)
7828 Copyprivate = Clause;
7829 if (Copyprivate && Nowait) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007830 Diag(Copyprivate->getBeginLoc(),
Alexey Bataev3255bf32015-01-19 05:20:46 +00007831 diag::err_omp_single_copyprivate_with_nowait);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007832 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here);
Alexey Bataev3255bf32015-01-19 05:20:46 +00007833 return StmtError();
7834 }
7835 }
7836
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00007837 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7838}
7839
Alexander Musman80c22892014-07-17 08:54:58 +00007840StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
7841 SourceLocation StartLoc,
7842 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007843 if (!AStmt)
7844 return StmtError();
7845
7846 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00007847
Reid Kleckner87a31802018-03-12 21:43:02 +00007848 setFunctionHasBranchProtectedScope();
Alexander Musman80c22892014-07-17 08:54:58 +00007849
7850 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
7851}
7852
Alexey Bataev28c75412015-12-15 08:19:24 +00007853StmtResult Sema::ActOnOpenMPCriticalDirective(
7854 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
7855 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007856 if (!AStmt)
7857 return StmtError();
7858
7859 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00007860
Alexey Bataev28c75412015-12-15 08:19:24 +00007861 bool ErrorFound = false;
7862 llvm::APSInt Hint;
7863 SourceLocation HintLoc;
7864 bool DependentHint = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00007865 for (const OMPClause *C : Clauses) {
Alexey Bataev28c75412015-12-15 08:19:24 +00007866 if (C->getClauseKind() == OMPC_hint) {
7867 if (!DirName.getName()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007868 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name);
Alexey Bataev28c75412015-12-15 08:19:24 +00007869 ErrorFound = true;
7870 }
7871 Expr *E = cast<OMPHintClause>(C)->getHint();
7872 if (E->isTypeDependent() || E->isValueDependent() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00007873 E->isInstantiationDependent()) {
Alexey Bataev28c75412015-12-15 08:19:24 +00007874 DependentHint = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00007875 } else {
Alexey Bataev28c75412015-12-15 08:19:24 +00007876 Hint = E->EvaluateKnownConstInt(Context);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007877 HintLoc = C->getBeginLoc();
Alexey Bataev28c75412015-12-15 08:19:24 +00007878 }
7879 }
7880 }
7881 if (ErrorFound)
7882 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00007883 const auto Pair = DSAStack->getCriticalWithHint(DirName);
Alexey Bataev28c75412015-12-15 08:19:24 +00007884 if (Pair.first && DirName.getName() && !DependentHint) {
7885 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
7886 Diag(StartLoc, diag::err_omp_critical_with_hint);
Alexey Bataeve3727102018-04-18 15:57:46 +00007887 if (HintLoc.isValid())
Alexey Bataev28c75412015-12-15 08:19:24 +00007888 Diag(HintLoc, diag::note_omp_critical_hint_here)
7889 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00007890 else
Alexey Bataev28c75412015-12-15 08:19:24 +00007891 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
Alexey Bataeve3727102018-04-18 15:57:46 +00007892 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007893 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here)
Alexey Bataev28c75412015-12-15 08:19:24 +00007894 << 1
7895 << C->getHint()->EvaluateKnownConstInt(Context).toString(
7896 /*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00007897 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007898 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1;
Alexey Bataeve3727102018-04-18 15:57:46 +00007899 }
Alexey Bataev28c75412015-12-15 08:19:24 +00007900 }
7901 }
7902
Reid Kleckner87a31802018-03-12 21:43:02 +00007903 setFunctionHasBranchProtectedScope();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00007904
Alexey Bataev28c75412015-12-15 08:19:24 +00007905 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
7906 Clauses, AStmt);
7907 if (!Pair.first && DirName.getName() && !DependentHint)
7908 DSAStack->addCriticalWithHint(Dir, Hint);
7909 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00007910}
7911
Alexey Bataev4acb8592014-07-07 13:01:15 +00007912StmtResult Sema::ActOnOpenMPParallelForDirective(
7913 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007914 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007915 if (!AStmt)
7916 return StmtError();
7917
Alexey Bataeve3727102018-04-18 15:57:46 +00007918 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev4acb8592014-07-07 13:01:15 +00007919 // 1.2.2 OpenMP Language Terminology
7920 // Structured block - An executable statement with a single entry at the
7921 // top and a single exit at the bottom.
7922 // The point of exit cannot be a branch out of the structured block.
7923 // longjmp() and throw() must not violate the entry/exit criteria.
7924 CS->getCapturedDecl()->setNothrow();
7925
Alexander Musmanc6388682014-12-15 07:07:06 +00007926 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007927 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7928 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00007929 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007930 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00007931 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7932 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00007933 if (NestedLoopCount == 0)
7934 return StmtError();
7935
Alexander Musmana5f070a2014-10-01 06:03:56 +00007936 assert((CurContext->isDependentContext() || B.builtAll()) &&
7937 "omp parallel for loop exprs were not built");
7938
Alexey Bataev54acd402015-08-04 11:18:19 +00007939 if (!CurContext->isDependentContext()) {
7940 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007941 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007942 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00007943 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007944 B.NumIterations, *this, CurScope,
7945 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00007946 return StmtError();
7947 }
7948 }
7949
Reid Kleckner87a31802018-03-12 21:43:02 +00007950 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00007951 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00007952 NestedLoopCount, Clauses, AStmt, B,
7953 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00007954}
7955
Alexander Musmane4e893b2014-09-23 09:33:00 +00007956StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
7957 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007958 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007959 if (!AStmt)
7960 return StmtError();
7961
Alexey Bataeve3727102018-04-18 15:57:46 +00007962 auto *CS = cast<CapturedStmt>(AStmt);
Alexander Musmane4e893b2014-09-23 09:33:00 +00007963 // 1.2.2 OpenMP Language Terminology
7964 // Structured block - An executable statement with a single entry at the
7965 // top and a single exit at the bottom.
7966 // The point of exit cannot be a branch out of the structured block.
7967 // longjmp() and throw() must not violate the entry/exit criteria.
7968 CS->getCapturedDecl()->setNothrow();
7969
Alexander Musmanc6388682014-12-15 07:07:06 +00007970 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007971 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7972 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00007973 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007974 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00007975 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7976 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00007977 if (NestedLoopCount == 0)
7978 return StmtError();
7979
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00007980 if (!CurContext->isDependentContext()) {
7981 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007982 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007983 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00007984 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007985 B.NumIterations, *this, CurScope,
7986 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00007987 return StmtError();
7988 }
7989 }
7990
Kelvin Lic5609492016-07-15 04:39:07 +00007991 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00007992 return StmtError();
7993
Reid Kleckner87a31802018-03-12 21:43:02 +00007994 setFunctionHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00007995 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00007996 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00007997}
7998
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00007999StmtResult
8000Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
8001 Stmt *AStmt, SourceLocation StartLoc,
8002 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008003 if (!AStmt)
8004 return StmtError();
8005
8006 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00008007 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00008008 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00008009 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00008010 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00008011 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00008012 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00008013 return StmtError();
8014 // All associated statements must be '#pragma omp section' except for
8015 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00008016 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00008017 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
8018 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008019 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00008020 diag::err_omp_parallel_sections_substmt_not_section);
8021 return StmtError();
8022 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00008023 cast<OMPSectionDirective>(SectionStmt)
8024 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00008025 }
8026 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008027 Diag(AStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00008028 diag::err_omp_parallel_sections_not_compound_stmt);
8029 return StmtError();
8030 }
8031
Reid Kleckner87a31802018-03-12 21:43:02 +00008032 setFunctionHasBranchProtectedScope();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00008033
Alexey Bataev25e5b442015-09-15 12:52:43 +00008034 return OMPParallelSectionsDirective::Create(
8035 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00008036}
8037
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008038StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
8039 Stmt *AStmt, SourceLocation StartLoc,
8040 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008041 if (!AStmt)
8042 return StmtError();
8043
David Majnemer9d168222016-08-05 17:44:54 +00008044 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008045 // 1.2.2 OpenMP Language Terminology
8046 // Structured block - An executable statement with a single entry at the
8047 // top and a single exit at the bottom.
8048 // The point of exit cannot be a branch out of the structured block.
8049 // longjmp() and throw() must not violate the entry/exit criteria.
8050 CS->getCapturedDecl()->setNothrow();
8051
Reid Kleckner87a31802018-03-12 21:43:02 +00008052 setFunctionHasBranchProtectedScope();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008053
Alexey Bataev25e5b442015-09-15 12:52:43 +00008054 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
8055 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008056}
8057
Alexey Bataev68446b72014-07-18 07:47:19 +00008058StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
8059 SourceLocation EndLoc) {
8060 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
8061}
8062
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00008063StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
8064 SourceLocation EndLoc) {
8065 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
8066}
8067
Alexey Bataev2df347a2014-07-18 10:17:07 +00008068StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
8069 SourceLocation EndLoc) {
8070 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
8071}
8072
Alexey Bataev169d96a2017-07-18 20:17:46 +00008073StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
8074 Stmt *AStmt,
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00008075 SourceLocation StartLoc,
8076 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008077 if (!AStmt)
8078 return StmtError();
8079
8080 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00008081
Reid Kleckner87a31802018-03-12 21:43:02 +00008082 setFunctionHasBranchProtectedScope();
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00008083
Alexey Bataev169d96a2017-07-18 20:17:46 +00008084 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
Alexey Bataev3b1b8952017-07-25 15:53:26 +00008085 AStmt,
8086 DSAStack->getTaskgroupReductionRef());
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00008087}
8088
Alexey Bataev6125da92014-07-21 11:26:11 +00008089StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
8090 SourceLocation StartLoc,
8091 SourceLocation EndLoc) {
8092 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
8093 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
8094}
8095
Alexey Bataev346265e2015-09-25 10:37:12 +00008096StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
8097 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00008098 SourceLocation StartLoc,
8099 SourceLocation EndLoc) {
Alexey Bataeve3727102018-04-18 15:57:46 +00008100 const OMPClause *DependFound = nullptr;
8101 const OMPClause *DependSourceClause = nullptr;
8102 const OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00008103 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00008104 const OMPThreadsClause *TC = nullptr;
8105 const OMPSIMDClause *SC = nullptr;
8106 for (const OMPClause *C : Clauses) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008107 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
8108 DependFound = C;
8109 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
8110 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008111 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataeveb482352015-12-18 05:05:56 +00008112 << getOpenMPDirectiveName(OMPD_ordered)
8113 << getOpenMPClauseName(OMPC_depend) << 2;
8114 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00008115 } else {
Alexey Bataeveb482352015-12-18 05:05:56 +00008116 DependSourceClause = C;
Alexey Bataeve3727102018-04-18 15:57:46 +00008117 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008118 if (DependSinkClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008119 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008120 << 0;
8121 ErrorFound = true;
8122 }
8123 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
8124 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008125 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008126 << 1;
8127 ErrorFound = true;
8128 }
8129 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00008130 }
Alexey Bataeve3727102018-04-18 15:57:46 +00008131 } else if (C->getClauseKind() == OMPC_threads) {
Alexey Bataev346265e2015-09-25 10:37:12 +00008132 TC = cast<OMPThreadsClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00008133 } else if (C->getClauseKind() == OMPC_simd) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008134 SC = cast<OMPSIMDClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00008135 }
Alexey Bataev346265e2015-09-25 10:37:12 +00008136 }
Alexey Bataeveb482352015-12-18 05:05:56 +00008137 if (!ErrorFound && !SC &&
8138 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008139 // OpenMP [2.8.1,simd Construct, Restrictions]
8140 // An ordered construct with the simd clause is the only OpenMP construct
8141 // that can appear in the simd region.
8142 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00008143 ErrorFound = true;
8144 } else if (DependFound && (TC || SC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008145 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd)
Alexey Bataeveb482352015-12-18 05:05:56 +00008146 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
8147 ErrorFound = true;
Alexey Bataevf138fda2018-08-13 19:04:24 +00008148 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008149 Diag(DependFound->getBeginLoc(),
Alexey Bataeveb482352015-12-18 05:05:56 +00008150 diag::err_omp_ordered_directive_without_param);
8151 ErrorFound = true;
8152 } else if (TC || Clauses.empty()) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00008153 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008154 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
Alexey Bataeveb482352015-12-18 05:05:56 +00008155 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
8156 << (TC != nullptr);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008157 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param);
Alexey Bataeveb482352015-12-18 05:05:56 +00008158 ErrorFound = true;
8159 }
8160 }
8161 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008162 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00008163
8164 if (AStmt) {
8165 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8166
Reid Kleckner87a31802018-03-12 21:43:02 +00008167 setFunctionHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008168 }
Alexey Bataev346265e2015-09-25 10:37:12 +00008169
8170 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00008171}
8172
Alexey Bataev1d160b12015-03-13 12:27:31 +00008173namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008174/// Helper class for checking expression in 'omp atomic [update]'
Alexey Bataev1d160b12015-03-13 12:27:31 +00008175/// construct.
8176class OpenMPAtomicUpdateChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008177 /// Error results for atomic update expressions.
Alexey Bataev1d160b12015-03-13 12:27:31 +00008178 enum ExprAnalysisErrorCode {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008179 /// A statement is not an expression statement.
Alexey Bataev1d160b12015-03-13 12:27:31 +00008180 NotAnExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008181 /// Expression is not builtin binary or unary operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00008182 NotABinaryOrUnaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008183 /// Unary operation is not post-/pre- increment/decrement operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00008184 NotAnUnaryIncDecExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008185 /// An expression is not of scalar type.
Alexey Bataev1d160b12015-03-13 12:27:31 +00008186 NotAScalarType,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008187 /// A binary operation is not an assignment operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00008188 NotAnAssignmentOp,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008189 /// RHS part of the binary operation is not a binary expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00008190 NotABinaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008191 /// RHS part is not additive/multiplicative/shift/biwise binary
Alexey Bataev1d160b12015-03-13 12:27:31 +00008192 /// expression.
8193 NotABinaryOperator,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008194 /// RHS binary operation does not have reference to the updated LHS
Alexey Bataev1d160b12015-03-13 12:27:31 +00008195 /// part.
8196 NotAnUpdateExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008197 /// No errors is found.
Alexey Bataev1d160b12015-03-13 12:27:31 +00008198 NoError
8199 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008200 /// Reference to Sema.
Alexey Bataev1d160b12015-03-13 12:27:31 +00008201 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008202 /// A location for note diagnostics (when error is found).
Alexey Bataev1d160b12015-03-13 12:27:31 +00008203 SourceLocation NoteLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008204 /// 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00008205 Expr *X;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008206 /// 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00008207 Expr *E;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008208 /// Helper expression of the form
Alexey Bataevb4505a72015-03-30 05:20:59 +00008209 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
8210 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
8211 Expr *UpdateExpr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008212 /// Is 'x' a LHS in a RHS part of full update expression. It is
Alexey Bataevb4505a72015-03-30 05:20:59 +00008213 /// important for non-associative operations.
8214 bool IsXLHSInRHSPart;
8215 BinaryOperatorKind Op;
8216 SourceLocation OpLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008217 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00008218 /// if it is a prefix unary operation.
8219 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00008220
8221public:
8222 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00008223 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00008224 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008225 /// Check specified statement that it is suitable for 'atomic update'
Alexey Bataev1d160b12015-03-13 12:27:31 +00008226 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00008227 /// expression. If DiagId and NoteId == 0, then only check is performed
8228 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00008229 /// \param DiagId Diagnostic which should be emitted if error is found.
8230 /// \param NoteId Diagnostic note for the main error message.
8231 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00008232 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008233 /// Return the 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00008234 Expr *getX() const { return X; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008235 /// Return the 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00008236 Expr *getExpr() const { return E; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008237 /// Return the update expression used in calculation of the updated
Alexey Bataevb4505a72015-03-30 05:20:59 +00008238 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
8239 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
8240 Expr *getUpdateExpr() const { return UpdateExpr; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008241 /// Return true if 'x' is LHS in RHS part of full update expression,
Alexey Bataevb4505a72015-03-30 05:20:59 +00008242 /// false otherwise.
8243 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
8244
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008245 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00008246 /// if it is a prefix unary operation.
8247 bool isPostfixUpdate() const { return IsPostfixUpdate; }
8248
Alexey Bataev1d160b12015-03-13 12:27:31 +00008249private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00008250 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
8251 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00008252};
8253} // namespace
8254
8255bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
8256 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
8257 ExprAnalysisErrorCode ErrorFound = NoError;
8258 SourceLocation ErrorLoc, NoteLoc;
8259 SourceRange ErrorRange, NoteRange;
8260 // Allowed constructs are:
8261 // x = x binop expr;
8262 // x = expr binop x;
8263 if (AtomicBinOp->getOpcode() == BO_Assign) {
8264 X = AtomicBinOp->getLHS();
Alexey Bataeve3727102018-04-18 15:57:46 +00008265 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00008266 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
8267 if (AtomicInnerBinOp->isMultiplicativeOp() ||
8268 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
8269 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00008270 Op = AtomicInnerBinOp->getOpcode();
8271 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +00008272 Expr *LHS = AtomicInnerBinOp->getLHS();
8273 Expr *RHS = AtomicInnerBinOp->getRHS();
Alexey Bataev1d160b12015-03-13 12:27:31 +00008274 llvm::FoldingSetNodeID XId, LHSId, RHSId;
8275 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
8276 /*Canonical=*/true);
8277 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
8278 /*Canonical=*/true);
8279 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
8280 /*Canonical=*/true);
8281 if (XId == LHSId) {
8282 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00008283 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00008284 } else if (XId == RHSId) {
8285 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00008286 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00008287 } else {
8288 ErrorLoc = AtomicInnerBinOp->getExprLoc();
8289 ErrorRange = AtomicInnerBinOp->getSourceRange();
8290 NoteLoc = X->getExprLoc();
8291 NoteRange = X->getSourceRange();
8292 ErrorFound = NotAnUpdateExpression;
8293 }
8294 } else {
8295 ErrorLoc = AtomicInnerBinOp->getExprLoc();
8296 ErrorRange = AtomicInnerBinOp->getSourceRange();
8297 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
8298 NoteRange = SourceRange(NoteLoc, NoteLoc);
8299 ErrorFound = NotABinaryOperator;
8300 }
8301 } else {
8302 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
8303 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
8304 ErrorFound = NotABinaryExpression;
8305 }
8306 } else {
8307 ErrorLoc = AtomicBinOp->getExprLoc();
8308 ErrorRange = AtomicBinOp->getSourceRange();
8309 NoteLoc = AtomicBinOp->getOperatorLoc();
8310 NoteRange = SourceRange(NoteLoc, NoteLoc);
8311 ErrorFound = NotAnAssignmentOp;
8312 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00008313 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00008314 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
8315 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
8316 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00008317 }
8318 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00008319 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00008320 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00008321}
8322
8323bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
8324 unsigned NoteId) {
8325 ExprAnalysisErrorCode ErrorFound = NoError;
8326 SourceLocation ErrorLoc, NoteLoc;
8327 SourceRange ErrorRange, NoteRange;
8328 // Allowed constructs are:
8329 // x++;
8330 // x--;
8331 // ++x;
8332 // --x;
8333 // x binop= expr;
8334 // x = x binop expr;
8335 // x = expr binop x;
8336 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
8337 AtomicBody = AtomicBody->IgnoreParenImpCasts();
8338 if (AtomicBody->getType()->isScalarType() ||
8339 AtomicBody->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00008340 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00008341 AtomicBody->IgnoreParenImpCasts())) {
8342 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00008343 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00008344 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00008345 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00008346 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00008347 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00008348 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00008349 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
8350 AtomicBody->IgnoreParenImpCasts())) {
8351 // Check for Binary Operation
David Majnemer9d168222016-08-05 17:44:54 +00008352 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
Alexey Bataevb4505a72015-03-30 05:20:59 +00008353 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00008354 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
David Majnemer9d168222016-08-05 17:44:54 +00008355 AtomicBody->IgnoreParenImpCasts())) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00008356 // Check for Unary Operation
8357 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00008358 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00008359 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
8360 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00008361 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00008362 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
8363 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00008364 } else {
8365 ErrorFound = NotAnUnaryIncDecExpression;
8366 ErrorLoc = AtomicUnaryOp->getExprLoc();
8367 ErrorRange = AtomicUnaryOp->getSourceRange();
8368 NoteLoc = AtomicUnaryOp->getOperatorLoc();
8369 NoteRange = SourceRange(NoteLoc, NoteLoc);
8370 }
Alexey Bataev5a195472015-09-04 12:55:50 +00008371 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00008372 ErrorFound = NotABinaryOrUnaryExpression;
8373 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
8374 NoteRange = ErrorRange = AtomicBody->getSourceRange();
8375 }
8376 } else {
8377 ErrorFound = NotAScalarType;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008378 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00008379 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
8380 }
8381 } else {
8382 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008383 NoteLoc = ErrorLoc = S->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00008384 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
8385 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00008386 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00008387 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
8388 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
8389 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00008390 }
8391 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00008392 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00008393 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00008394 // Build an update expression of form 'OpaqueValueExpr(x) binop
8395 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
8396 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
8397 auto *OVEX = new (SemaRef.getASTContext())
8398 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
8399 auto *OVEExpr = new (SemaRef.getASTContext())
8400 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
Alexey Bataeve3727102018-04-18 15:57:46 +00008401 ExprResult Update =
Alexey Bataevb4505a72015-03-30 05:20:59 +00008402 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
8403 IsXLHSInRHSPart ? OVEExpr : OVEX);
8404 if (Update.isInvalid())
8405 return true;
8406 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
8407 Sema::AA_Casting);
8408 if (Update.isInvalid())
8409 return true;
8410 UpdateExpr = Update.get();
8411 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00008412 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00008413}
8414
Alexey Bataev0162e452014-07-22 10:10:35 +00008415StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
8416 Stmt *AStmt,
8417 SourceLocation StartLoc,
8418 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008419 if (!AStmt)
8420 return StmtError();
8421
David Majnemer9d168222016-08-05 17:44:54 +00008422 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00008423 // 1.2.2 OpenMP Language Terminology
8424 // Structured block - An executable statement with a single entry at the
8425 // top and a single exit at the bottom.
8426 // The point of exit cannot be a branch out of the structured block.
8427 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00008428 OpenMPClauseKind AtomicKind = OMPC_unknown;
8429 SourceLocation AtomicKindLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00008430 for (const OMPClause *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00008431 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00008432 C->getClauseKind() == OMPC_update ||
8433 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00008434 if (AtomicKind != OMPC_unknown) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008435 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00008436 << SourceRange(C->getBeginLoc(), C->getEndLoc());
Alexey Bataevdea47612014-07-23 07:46:59 +00008437 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
8438 << getOpenMPClauseName(AtomicKind);
8439 } else {
8440 AtomicKind = C->getClauseKind();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008441 AtomicKindLoc = C->getBeginLoc();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008442 }
8443 }
8444 }
Alexey Bataev62cec442014-11-18 10:14:22 +00008445
Alexey Bataeve3727102018-04-18 15:57:46 +00008446 Stmt *Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00008447 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
8448 Body = EWC->getSubExpr();
8449
Alexey Bataev62cec442014-11-18 10:14:22 +00008450 Expr *X = nullptr;
8451 Expr *V = nullptr;
8452 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00008453 Expr *UE = nullptr;
8454 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00008455 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00008456 // OpenMP [2.12.6, atomic Construct]
8457 // In the next expressions:
8458 // * x and v (as applicable) are both l-value expressions with scalar type.
8459 // * During the execution of an atomic region, multiple syntactic
8460 // occurrences of x must designate the same storage location.
8461 // * Neither of v and expr (as applicable) may access the storage location
8462 // designated by x.
8463 // * Neither of x and expr (as applicable) may access the storage location
8464 // designated by v.
8465 // * expr is an expression with scalar type.
8466 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
8467 // * binop, binop=, ++, and -- are not overloaded operators.
8468 // * The expression x binop expr must be numerically equivalent to x binop
8469 // (expr). This requirement is satisfied if the operators in expr have
8470 // precedence greater than binop, or by using parentheses around expr or
8471 // subexpressions of expr.
8472 // * The expression expr binop x must be numerically equivalent to (expr)
8473 // binop x. This requirement is satisfied if the operators in expr have
8474 // precedence equal to or greater than binop, or by using parentheses around
8475 // expr or subexpressions of expr.
8476 // * For forms that allow multiple occurrences of x, the number of times
8477 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00008478 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00008479 enum {
8480 NotAnExpression,
8481 NotAnAssignmentOp,
8482 NotAScalarType,
8483 NotAnLValue,
8484 NoError
8485 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00008486 SourceLocation ErrorLoc, NoteLoc;
8487 SourceRange ErrorRange, NoteRange;
8488 // If clause is read:
8489 // v = x;
Alexey Bataeve3727102018-04-18 15:57:46 +00008490 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
8491 const auto *AtomicBinOp =
Alexey Bataev62cec442014-11-18 10:14:22 +00008492 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
8493 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
8494 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
8495 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
8496 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
8497 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
8498 if (!X->isLValue() || !V->isLValue()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00008499 const Expr *NotLValueExpr = X->isLValue() ? V : X;
Alexey Bataev62cec442014-11-18 10:14:22 +00008500 ErrorFound = NotAnLValue;
8501 ErrorLoc = AtomicBinOp->getExprLoc();
8502 ErrorRange = AtomicBinOp->getSourceRange();
8503 NoteLoc = NotLValueExpr->getExprLoc();
8504 NoteRange = NotLValueExpr->getSourceRange();
8505 }
8506 } else if (!X->isInstantiationDependent() ||
8507 !V->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00008508 const Expr *NotScalarExpr =
Alexey Bataev62cec442014-11-18 10:14:22 +00008509 (X->isInstantiationDependent() || X->getType()->isScalarType())
8510 ? V
8511 : X;
8512 ErrorFound = NotAScalarType;
8513 ErrorLoc = AtomicBinOp->getExprLoc();
8514 ErrorRange = AtomicBinOp->getSourceRange();
8515 NoteLoc = NotScalarExpr->getExprLoc();
8516 NoteRange = NotScalarExpr->getSourceRange();
8517 }
Alexey Bataev5a195472015-09-04 12:55:50 +00008518 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00008519 ErrorFound = NotAnAssignmentOp;
8520 ErrorLoc = AtomicBody->getExprLoc();
8521 ErrorRange = AtomicBody->getSourceRange();
8522 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
8523 : AtomicBody->getExprLoc();
8524 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
8525 : AtomicBody->getSourceRange();
8526 }
8527 } else {
8528 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008529 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataev62cec442014-11-18 10:14:22 +00008530 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00008531 }
Alexey Bataev62cec442014-11-18 10:14:22 +00008532 if (ErrorFound != NoError) {
8533 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
8534 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00008535 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
8536 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00008537 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00008538 }
8539 if (CurContext->isDependentContext())
Alexey Bataev62cec442014-11-18 10:14:22 +00008540 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00008541 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00008542 enum {
8543 NotAnExpression,
8544 NotAnAssignmentOp,
8545 NotAScalarType,
8546 NotAnLValue,
8547 NoError
8548 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00008549 SourceLocation ErrorLoc, NoteLoc;
8550 SourceRange ErrorRange, NoteRange;
8551 // If clause is write:
8552 // x = expr;
Alexey Bataeve3727102018-04-18 15:57:46 +00008553 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
8554 const auto *AtomicBinOp =
Alexey Bataevf33eba62014-11-28 07:21:40 +00008555 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
8556 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00008557 X = AtomicBinOp->getLHS();
8558 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00008559 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
8560 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
8561 if (!X->isLValue()) {
8562 ErrorFound = NotAnLValue;
8563 ErrorLoc = AtomicBinOp->getExprLoc();
8564 ErrorRange = AtomicBinOp->getSourceRange();
8565 NoteLoc = X->getExprLoc();
8566 NoteRange = X->getSourceRange();
8567 }
8568 } else if (!X->isInstantiationDependent() ||
8569 !E->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00008570 const Expr *NotScalarExpr =
Alexey Bataevf33eba62014-11-28 07:21:40 +00008571 (X->isInstantiationDependent() || X->getType()->isScalarType())
8572 ? E
8573 : X;
8574 ErrorFound = NotAScalarType;
8575 ErrorLoc = AtomicBinOp->getExprLoc();
8576 ErrorRange = AtomicBinOp->getSourceRange();
8577 NoteLoc = NotScalarExpr->getExprLoc();
8578 NoteRange = NotScalarExpr->getSourceRange();
8579 }
Alexey Bataev5a195472015-09-04 12:55:50 +00008580 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00008581 ErrorFound = NotAnAssignmentOp;
8582 ErrorLoc = AtomicBody->getExprLoc();
8583 ErrorRange = AtomicBody->getSourceRange();
8584 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
8585 : AtomicBody->getExprLoc();
8586 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
8587 : AtomicBody->getSourceRange();
8588 }
8589 } else {
8590 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008591 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevf33eba62014-11-28 07:21:40 +00008592 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00008593 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00008594 if (ErrorFound != NoError) {
8595 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
8596 << ErrorRange;
8597 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
8598 << NoteRange;
8599 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00008600 }
8601 if (CurContext->isDependentContext())
Alexey Bataevf33eba62014-11-28 07:21:40 +00008602 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00008603 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00008604 // If clause is update:
8605 // x++;
8606 // x--;
8607 // ++x;
8608 // --x;
8609 // x binop= expr;
8610 // x = x binop expr;
8611 // x = expr binop x;
8612 OpenMPAtomicUpdateChecker Checker(*this);
8613 if (Checker.checkStatement(
8614 Body, (AtomicKind == OMPC_update)
8615 ? diag::err_omp_atomic_update_not_expression_statement
8616 : diag::err_omp_atomic_not_expression_statement,
8617 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00008618 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00008619 if (!CurContext->isDependentContext()) {
8620 E = Checker.getExpr();
8621 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00008622 UE = Checker.getUpdateExpr();
8623 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00008624 }
Alexey Bataev459dec02014-07-24 06:46:57 +00008625 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00008626 enum {
8627 NotAnAssignmentOp,
8628 NotACompoundStatement,
8629 NotTwoSubstatements,
8630 NotASpecificExpression,
8631 NoError
8632 } ErrorFound = NoError;
8633 SourceLocation ErrorLoc, NoteLoc;
8634 SourceRange ErrorRange, NoteRange;
Alexey Bataeve3727102018-04-18 15:57:46 +00008635 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00008636 // If clause is a capture:
8637 // v = x++;
8638 // v = x--;
8639 // v = ++x;
8640 // v = --x;
8641 // v = x binop= expr;
8642 // v = x = x binop expr;
8643 // v = x = expr binop x;
Alexey Bataeve3727102018-04-18 15:57:46 +00008644 const auto *AtomicBinOp =
Alexey Bataevb78ca832015-04-01 03:33:17 +00008645 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
8646 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
8647 V = AtomicBinOp->getLHS();
8648 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
8649 OpenMPAtomicUpdateChecker Checker(*this);
8650 if (Checker.checkStatement(
8651 Body, diag::err_omp_atomic_capture_not_expression_statement,
8652 diag::note_omp_atomic_update))
8653 return StmtError();
8654 E = Checker.getExpr();
8655 X = Checker.getX();
8656 UE = Checker.getUpdateExpr();
8657 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
8658 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00008659 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00008660 ErrorLoc = AtomicBody->getExprLoc();
8661 ErrorRange = AtomicBody->getSourceRange();
8662 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
8663 : AtomicBody->getExprLoc();
8664 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
8665 : AtomicBody->getSourceRange();
8666 ErrorFound = NotAnAssignmentOp;
8667 }
8668 if (ErrorFound != NoError) {
8669 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
8670 << ErrorRange;
8671 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
8672 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00008673 }
Alexey Bataeve3727102018-04-18 15:57:46 +00008674 if (CurContext->isDependentContext())
8675 UE = V = E = X = nullptr;
Alexey Bataevb78ca832015-04-01 03:33:17 +00008676 } else {
8677 // If clause is a capture:
8678 // { v = x; x = expr; }
8679 // { v = x; x++; }
8680 // { v = x; x--; }
8681 // { v = x; ++x; }
8682 // { v = x; --x; }
8683 // { v = x; x binop= expr; }
8684 // { v = x; x = x binop expr; }
8685 // { v = x; x = expr binop x; }
8686 // { x++; v = x; }
8687 // { x--; v = x; }
8688 // { ++x; v = x; }
8689 // { --x; v = x; }
8690 // { x binop= expr; v = x; }
8691 // { x = x binop expr; v = x; }
8692 // { x = expr binop x; v = x; }
8693 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
8694 // Check that this is { expr1; expr2; }
8695 if (CS->size() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00008696 Stmt *First = CS->body_front();
8697 Stmt *Second = CS->body_back();
Alexey Bataevb78ca832015-04-01 03:33:17 +00008698 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
8699 First = EWC->getSubExpr()->IgnoreParenImpCasts();
8700 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
8701 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
8702 // Need to find what subexpression is 'v' and what is 'x'.
8703 OpenMPAtomicUpdateChecker Checker(*this);
8704 bool IsUpdateExprFound = !Checker.checkStatement(Second);
8705 BinaryOperator *BinOp = nullptr;
8706 if (IsUpdateExprFound) {
8707 BinOp = dyn_cast<BinaryOperator>(First);
8708 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
8709 }
8710 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
8711 // { v = x; x++; }
8712 // { v = x; x--; }
8713 // { v = x; ++x; }
8714 // { v = x; --x; }
8715 // { v = x; x binop= expr; }
8716 // { v = x; x = x binop expr; }
8717 // { v = x; x = expr binop x; }
8718 // Check that the first expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00008719 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00008720 llvm::FoldingSetNodeID XId, PossibleXId;
8721 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
8722 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
8723 IsUpdateExprFound = XId == PossibleXId;
8724 if (IsUpdateExprFound) {
8725 V = BinOp->getLHS();
8726 X = Checker.getX();
8727 E = Checker.getExpr();
8728 UE = Checker.getUpdateExpr();
8729 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00008730 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00008731 }
8732 }
8733 if (!IsUpdateExprFound) {
8734 IsUpdateExprFound = !Checker.checkStatement(First);
8735 BinOp = nullptr;
8736 if (IsUpdateExprFound) {
8737 BinOp = dyn_cast<BinaryOperator>(Second);
8738 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
8739 }
8740 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
8741 // { x++; v = x; }
8742 // { x--; v = x; }
8743 // { ++x; v = x; }
8744 // { --x; v = x; }
8745 // { x binop= expr; v = x; }
8746 // { x = x binop expr; v = x; }
8747 // { x = expr binop x; v = x; }
8748 // Check that the second expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00008749 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00008750 llvm::FoldingSetNodeID XId, PossibleXId;
8751 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
8752 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
8753 IsUpdateExprFound = XId == PossibleXId;
8754 if (IsUpdateExprFound) {
8755 V = BinOp->getLHS();
8756 X = Checker.getX();
8757 E = Checker.getExpr();
8758 UE = Checker.getUpdateExpr();
8759 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00008760 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00008761 }
8762 }
8763 }
8764 if (!IsUpdateExprFound) {
8765 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00008766 auto *FirstExpr = dyn_cast<Expr>(First);
8767 auto *SecondExpr = dyn_cast<Expr>(Second);
8768 if (!FirstExpr || !SecondExpr ||
8769 !(FirstExpr->isInstantiationDependent() ||
8770 SecondExpr->isInstantiationDependent())) {
8771 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
8772 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00008773 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00008774 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008775 : First->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00008776 NoteRange = ErrorRange = FirstBinOp
8777 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00008778 : SourceRange(ErrorLoc, ErrorLoc);
8779 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00008780 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
8781 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
8782 ErrorFound = NotAnAssignmentOp;
8783 NoteLoc = ErrorLoc = SecondBinOp
8784 ? SecondBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008785 : Second->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00008786 NoteRange = ErrorRange =
8787 SecondBinOp ? SecondBinOp->getSourceRange()
8788 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00008789 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00008790 Expr *PossibleXRHSInFirst =
Alexey Bataev5a195472015-09-04 12:55:50 +00008791 FirstBinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00008792 Expr *PossibleXLHSInSecond =
Alexey Bataev5a195472015-09-04 12:55:50 +00008793 SecondBinOp->getLHS()->IgnoreParenImpCasts();
8794 llvm::FoldingSetNodeID X1Id, X2Id;
8795 PossibleXRHSInFirst->Profile(X1Id, Context,
8796 /*Canonical=*/true);
8797 PossibleXLHSInSecond->Profile(X2Id, Context,
8798 /*Canonical=*/true);
8799 IsUpdateExprFound = X1Id == X2Id;
8800 if (IsUpdateExprFound) {
8801 V = FirstBinOp->getLHS();
8802 X = SecondBinOp->getLHS();
8803 E = SecondBinOp->getRHS();
8804 UE = nullptr;
8805 IsXLHSInRHSPart = false;
8806 IsPostfixUpdate = true;
8807 } else {
8808 ErrorFound = NotASpecificExpression;
8809 ErrorLoc = FirstBinOp->getExprLoc();
8810 ErrorRange = FirstBinOp->getSourceRange();
8811 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
8812 NoteRange = SecondBinOp->getRHS()->getSourceRange();
8813 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00008814 }
8815 }
8816 }
8817 }
8818 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008819 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00008820 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008821 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00008822 ErrorFound = NotTwoSubstatements;
8823 }
8824 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008825 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00008826 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008827 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00008828 ErrorFound = NotACompoundStatement;
8829 }
8830 if (ErrorFound != NoError) {
8831 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
8832 << ErrorRange;
8833 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
8834 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00008835 }
Alexey Bataeve3727102018-04-18 15:57:46 +00008836 if (CurContext->isDependentContext())
8837 UE = V = E = X = nullptr;
Alexey Bataev459dec02014-07-24 06:46:57 +00008838 }
Alexey Bataevdea47612014-07-23 07:46:59 +00008839 }
Alexey Bataev0162e452014-07-22 10:10:35 +00008840
Reid Kleckner87a31802018-03-12 21:43:02 +00008841 setFunctionHasBranchProtectedScope();
Alexey Bataev0162e452014-07-22 10:10:35 +00008842
Alexey Bataev62cec442014-11-18 10:14:22 +00008843 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00008844 X, V, E, UE, IsXLHSInRHSPart,
8845 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00008846}
8847
Alexey Bataev0bd520b2014-09-19 08:19:49 +00008848StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
8849 Stmt *AStmt,
8850 SourceLocation StartLoc,
8851 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008852 if (!AStmt)
8853 return StmtError();
8854
Alexey Bataeve3727102018-04-18 15:57:46 +00008855 auto *CS = cast<CapturedStmt>(AStmt);
Samuel Antao4af1b7b2015-12-02 17:44:43 +00008856 // 1.2.2 OpenMP Language Terminology
8857 // Structured block - An executable statement with a single entry at the
8858 // top and a single exit at the bottom.
8859 // The point of exit cannot be a branch out of the structured block.
8860 // longjmp() and throw() must not violate the entry/exit criteria.
8861 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00008862 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
8863 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8864 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8865 // 1.2.2 OpenMP Language Terminology
8866 // Structured block - An executable statement with a single entry at the
8867 // top and a single exit at the bottom.
8868 // The point of exit cannot be a branch out of the structured block.
8869 // longjmp() and throw() must not violate the entry/exit criteria.
8870 CS->getCapturedDecl()->setNothrow();
8871 }
Alexey Bataev0bd520b2014-09-19 08:19:49 +00008872
Alexey Bataev13314bf2014-10-09 04:18:56 +00008873 // OpenMP [2.16, Nesting of Regions]
8874 // If specified, a teams construct must be contained within a target
8875 // construct. That target construct must contain no statements or directives
8876 // outside of the teams construct.
8877 if (DSAStack->hasInnerTeamsRegion()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00008878 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
Alexey Bataev13314bf2014-10-09 04:18:56 +00008879 bool OMPTeamsFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00008880 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00008881 auto I = CS->body_begin();
8882 while (I != CS->body_end()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00008883 const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
Kelvin Li620ba602019-02-05 16:43:00 +00008884 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) ||
8885 OMPTeamsFound) {
8886
Alexey Bataev13314bf2014-10-09 04:18:56 +00008887 OMPTeamsFound = false;
8888 break;
8889 }
8890 ++I;
8891 }
8892 assert(I != CS->body_end() && "Not found statement");
8893 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00008894 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00008895 const auto *OED = dyn_cast<OMPExecutableDirective>(S);
Kelvin Li3834dce2016-06-27 19:15:43 +00008896 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00008897 }
8898 if (!OMPTeamsFound) {
8899 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
8900 Diag(DSAStack->getInnerTeamsRegionLoc(),
8901 diag::note_omp_nested_teams_construct_here);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008902 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here)
Alexey Bataev13314bf2014-10-09 04:18:56 +00008903 << isa<OMPExecutableDirective>(S);
8904 return StmtError();
8905 }
8906 }
8907
Reid Kleckner87a31802018-03-12 21:43:02 +00008908 setFunctionHasBranchProtectedScope();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00008909
8910 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
8911}
8912
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00008913StmtResult
8914Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
8915 Stmt *AStmt, SourceLocation StartLoc,
8916 SourceLocation EndLoc) {
8917 if (!AStmt)
8918 return StmtError();
8919
Alexey Bataeve3727102018-04-18 15:57:46 +00008920 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00008921 // 1.2.2 OpenMP Language Terminology
8922 // Structured block - An executable statement with a single entry at the
8923 // top and a single exit at the bottom.
8924 // The point of exit cannot be a branch out of the structured block.
8925 // longjmp() and throw() must not violate the entry/exit criteria.
8926 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00008927 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
8928 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8929 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8930 // 1.2.2 OpenMP Language Terminology
8931 // Structured block - An executable statement with a single entry at the
8932 // top and a single exit at the bottom.
8933 // The point of exit cannot be a branch out of the structured block.
8934 // longjmp() and throw() must not violate the entry/exit criteria.
8935 CS->getCapturedDecl()->setNothrow();
8936 }
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00008937
Reid Kleckner87a31802018-03-12 21:43:02 +00008938 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00008939
8940 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
8941 AStmt);
8942}
8943
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00008944StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
8945 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008946 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00008947 if (!AStmt)
8948 return StmtError();
8949
Alexey Bataeve3727102018-04-18 15:57:46 +00008950 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00008951 // 1.2.2 OpenMP Language Terminology
8952 // Structured block - An executable statement with a single entry at the
8953 // top and a single exit at the bottom.
8954 // The point of exit cannot be a branch out of the structured block.
8955 // longjmp() and throw() must not violate the entry/exit criteria.
8956 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008957 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
8958 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8959 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8960 // 1.2.2 OpenMP Language Terminology
8961 // Structured block - An executable statement with a single entry at the
8962 // top and a single exit at the bottom.
8963 // The point of exit cannot be a branch out of the structured block.
8964 // longjmp() and throw() must not violate the entry/exit criteria.
8965 CS->getCapturedDecl()->setNothrow();
8966 }
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00008967
8968 OMPLoopDirective::HelperExprs B;
8969 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
8970 // define the nested loops number.
8971 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00008972 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008973 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00008974 VarsWithImplicitDSA, B);
8975 if (NestedLoopCount == 0)
8976 return StmtError();
8977
8978 assert((CurContext->isDependentContext() || B.builtAll()) &&
8979 "omp target parallel for loop exprs were not built");
8980
8981 if (!CurContext->isDependentContext()) {
8982 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008983 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00008984 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00008985 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00008986 B.NumIterations, *this, CurScope,
8987 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00008988 return StmtError();
8989 }
8990 }
8991
Reid Kleckner87a31802018-03-12 21:43:02 +00008992 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00008993 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
8994 NestedLoopCount, Clauses, AStmt,
8995 B, DSAStack->isCancelRegion());
8996}
8997
Alexey Bataev95b64a92017-05-30 16:00:04 +00008998/// Check for existence of a map clause in the list of clauses.
8999static bool hasClauses(ArrayRef<OMPClause *> Clauses,
9000 const OpenMPClauseKind K) {
9001 return llvm::any_of(
9002 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
9003}
Samuel Antaodf67fc42016-01-19 19:15:56 +00009004
Alexey Bataev95b64a92017-05-30 16:00:04 +00009005template <typename... Params>
9006static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
9007 const Params... ClauseTypes) {
9008 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
Samuel Antaodf67fc42016-01-19 19:15:56 +00009009}
9010
Michael Wong65f367f2015-07-21 13:44:28 +00009011StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
9012 Stmt *AStmt,
9013 SourceLocation StartLoc,
9014 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009015 if (!AStmt)
9016 return StmtError();
9017
9018 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9019
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00009020 // OpenMP [2.10.1, Restrictions, p. 97]
9021 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00009022 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
9023 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
9024 << "'map' or 'use_device_ptr'"
David Majnemer9d168222016-08-05 17:44:54 +00009025 << getOpenMPDirectiveName(OMPD_target_data);
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00009026 return StmtError();
9027 }
9028
Reid Kleckner87a31802018-03-12 21:43:02 +00009029 setFunctionHasBranchProtectedScope();
Michael Wong65f367f2015-07-21 13:44:28 +00009030
9031 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
9032 AStmt);
9033}
9034
Samuel Antaodf67fc42016-01-19 19:15:56 +00009035StmtResult
9036Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
9037 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00009038 SourceLocation EndLoc, Stmt *AStmt) {
9039 if (!AStmt)
9040 return StmtError();
9041
Alexey Bataeve3727102018-04-18 15:57:46 +00009042 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00009043 // 1.2.2 OpenMP Language Terminology
9044 // Structured block - An executable statement with a single entry at the
9045 // top and a single exit at the bottom.
9046 // The point of exit cannot be a branch out of the structured block.
9047 // longjmp() and throw() must not violate the entry/exit criteria.
9048 CS->getCapturedDecl()->setNothrow();
9049 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
9050 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9051 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9052 // 1.2.2 OpenMP Language Terminology
9053 // Structured block - An executable statement with a single entry at the
9054 // top and a single exit at the bottom.
9055 // The point of exit cannot be a branch out of the structured block.
9056 // longjmp() and throw() must not violate the entry/exit criteria.
9057 CS->getCapturedDecl()->setNothrow();
9058 }
9059
Samuel Antaodf67fc42016-01-19 19:15:56 +00009060 // OpenMP [2.10.2, Restrictions, p. 99]
9061 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00009062 if (!hasClauses(Clauses, OMPC_map)) {
9063 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
9064 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
Samuel Antaodf67fc42016-01-19 19:15:56 +00009065 return StmtError();
9066 }
9067
Alexey Bataev7828b252017-11-21 17:08:48 +00009068 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
9069 AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00009070}
9071
Samuel Antao72590762016-01-19 20:04:50 +00009072StmtResult
9073Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
9074 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00009075 SourceLocation EndLoc, Stmt *AStmt) {
9076 if (!AStmt)
9077 return StmtError();
9078
Alexey Bataeve3727102018-04-18 15:57:46 +00009079 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00009080 // 1.2.2 OpenMP Language Terminology
9081 // Structured block - An executable statement with a single entry at the
9082 // top and a single exit at the bottom.
9083 // The point of exit cannot be a branch out of the structured block.
9084 // longjmp() and throw() must not violate the entry/exit criteria.
9085 CS->getCapturedDecl()->setNothrow();
9086 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
9087 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9088 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9089 // 1.2.2 OpenMP Language Terminology
9090 // Structured block - An executable statement with a single entry at the
9091 // top and a single exit at the bottom.
9092 // The point of exit cannot be a branch out of the structured block.
9093 // longjmp() and throw() must not violate the entry/exit criteria.
9094 CS->getCapturedDecl()->setNothrow();
9095 }
9096
Samuel Antao72590762016-01-19 20:04:50 +00009097 // OpenMP [2.10.3, Restrictions, p. 102]
9098 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00009099 if (!hasClauses(Clauses, OMPC_map)) {
9100 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
9101 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
Samuel Antao72590762016-01-19 20:04:50 +00009102 return StmtError();
9103 }
9104
Alexey Bataev7828b252017-11-21 17:08:48 +00009105 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
9106 AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00009107}
9108
Samuel Antao686c70c2016-05-26 17:30:50 +00009109StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
9110 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00009111 SourceLocation EndLoc,
9112 Stmt *AStmt) {
9113 if (!AStmt)
9114 return StmtError();
9115
Alexey Bataeve3727102018-04-18 15:57:46 +00009116 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00009117 // 1.2.2 OpenMP Language Terminology
9118 // Structured block - An executable statement with a single entry at the
9119 // top and a single exit at the bottom.
9120 // The point of exit cannot be a branch out of the structured block.
9121 // longjmp() and throw() must not violate the entry/exit criteria.
9122 CS->getCapturedDecl()->setNothrow();
9123 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
9124 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9125 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9126 // 1.2.2 OpenMP Language Terminology
9127 // Structured block - An executable statement with a single entry at the
9128 // top and a single exit at the bottom.
9129 // The point of exit cannot be a branch out of the structured block.
9130 // longjmp() and throw() must not violate the entry/exit criteria.
9131 CS->getCapturedDecl()->setNothrow();
9132 }
9133
Alexey Bataev95b64a92017-05-30 16:00:04 +00009134 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
Samuel Antao686c70c2016-05-26 17:30:50 +00009135 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
9136 return StmtError();
9137 }
Alexey Bataev7828b252017-11-21 17:08:48 +00009138 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
9139 AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00009140}
9141
Alexey Bataev13314bf2014-10-09 04:18:56 +00009142StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
9143 Stmt *AStmt, SourceLocation StartLoc,
9144 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009145 if (!AStmt)
9146 return StmtError();
9147
Alexey Bataeve3727102018-04-18 15:57:46 +00009148 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev13314bf2014-10-09 04:18:56 +00009149 // 1.2.2 OpenMP Language Terminology
9150 // Structured block - An executable statement with a single entry at the
9151 // top and a single exit at the bottom.
9152 // The point of exit cannot be a branch out of the structured block.
9153 // longjmp() and throw() must not violate the entry/exit criteria.
9154 CS->getCapturedDecl()->setNothrow();
9155
Reid Kleckner87a31802018-03-12 21:43:02 +00009156 setFunctionHasBranchProtectedScope();
Alexey Bataev13314bf2014-10-09 04:18:56 +00009157
Alexey Bataevceabd412017-11-30 18:01:54 +00009158 DSAStack->setParentTeamsRegionLoc(StartLoc);
9159
Alexey Bataev13314bf2014-10-09 04:18:56 +00009160 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
9161}
9162
Alexey Bataev6d4ed052015-07-01 06:57:41 +00009163StmtResult
9164Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
9165 SourceLocation EndLoc,
9166 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00009167 if (DSAStack->isParentNowaitRegion()) {
9168 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
9169 return StmtError();
9170 }
9171 if (DSAStack->isParentOrderedRegion()) {
9172 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
9173 return StmtError();
9174 }
9175 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
9176 CancelRegion);
9177}
9178
Alexey Bataev87933c72015-09-18 08:07:34 +00009179StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
9180 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00009181 SourceLocation EndLoc,
9182 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev80909872015-07-02 11:25:17 +00009183 if (DSAStack->isParentNowaitRegion()) {
9184 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
9185 return StmtError();
9186 }
9187 if (DSAStack->isParentOrderedRegion()) {
9188 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
9189 return StmtError();
9190 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00009191 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00009192 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
9193 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00009194}
9195
Alexey Bataev382967a2015-12-08 12:06:20 +00009196static bool checkGrainsizeNumTasksClauses(Sema &S,
9197 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009198 const OMPClause *PrevClause = nullptr;
Alexey Bataev382967a2015-12-08 12:06:20 +00009199 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00009200 for (const OMPClause *C : Clauses) {
Alexey Bataev382967a2015-12-08 12:06:20 +00009201 if (C->getClauseKind() == OMPC_grainsize ||
9202 C->getClauseKind() == OMPC_num_tasks) {
9203 if (!PrevClause)
9204 PrevClause = C;
9205 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00009206 S.Diag(C->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00009207 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
9208 << getOpenMPClauseName(C->getClauseKind())
9209 << getOpenMPClauseName(PrevClause->getClauseKind());
Stephen Kellyf2ceec42018-08-09 21:08:08 +00009210 S.Diag(PrevClause->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00009211 diag::note_omp_previous_grainsize_num_tasks)
9212 << getOpenMPClauseName(PrevClause->getClauseKind());
9213 ErrorFound = true;
9214 }
9215 }
9216 }
9217 return ErrorFound;
9218}
9219
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00009220static bool checkReductionClauseWithNogroup(Sema &S,
9221 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009222 const OMPClause *ReductionClause = nullptr;
9223 const OMPClause *NogroupClause = nullptr;
9224 for (const OMPClause *C : Clauses) {
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00009225 if (C->getClauseKind() == OMPC_reduction) {
9226 ReductionClause = C;
9227 if (NogroupClause)
9228 break;
9229 continue;
9230 }
9231 if (C->getClauseKind() == OMPC_nogroup) {
9232 NogroupClause = C;
9233 if (ReductionClause)
9234 break;
9235 continue;
9236 }
9237 }
9238 if (ReductionClause && NogroupClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00009239 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup)
9240 << SourceRange(NogroupClause->getBeginLoc(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00009241 NogroupClause->getEndLoc());
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00009242 return true;
9243 }
9244 return false;
9245}
9246
Alexey Bataev49f6e782015-12-01 04:18:41 +00009247StmtResult Sema::ActOnOpenMPTaskLoopDirective(
9248 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009249 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00009250 if (!AStmt)
9251 return StmtError();
9252
9253 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9254 OMPLoopDirective::HelperExprs B;
9255 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
9256 // define the nested loops number.
9257 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00009258 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00009259 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00009260 VarsWithImplicitDSA, B);
9261 if (NestedLoopCount == 0)
9262 return StmtError();
9263
9264 assert((CurContext->isDependentContext() || B.builtAll()) &&
9265 "omp for loop exprs were not built");
9266
Alexey Bataev382967a2015-12-08 12:06:20 +00009267 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
9268 // The grainsize clause and num_tasks clause are mutually exclusive and may
9269 // not appear on the same taskloop directive.
9270 if (checkGrainsizeNumTasksClauses(*this, Clauses))
9271 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00009272 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
9273 // If a reduction clause is present on the taskloop directive, the nogroup
9274 // clause must not be specified.
9275 if (checkReductionClauseWithNogroup(*this, Clauses))
9276 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00009277
Reid Kleckner87a31802018-03-12 21:43:02 +00009278 setFunctionHasBranchProtectedScope();
Alexey Bataev49f6e782015-12-01 04:18:41 +00009279 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
9280 NestedLoopCount, Clauses, AStmt, B);
9281}
9282
Alexey Bataev0a6ed842015-12-03 09:40:15 +00009283StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
9284 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009285 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00009286 if (!AStmt)
9287 return StmtError();
9288
9289 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9290 OMPLoopDirective::HelperExprs B;
9291 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
9292 // define the nested loops number.
9293 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00009294 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00009295 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
9296 VarsWithImplicitDSA, B);
9297 if (NestedLoopCount == 0)
9298 return StmtError();
9299
9300 assert((CurContext->isDependentContext() || B.builtAll()) &&
9301 "omp for loop exprs were not built");
9302
Alexey Bataev5a3af132016-03-29 08:58:54 +00009303 if (!CurContext->isDependentContext()) {
9304 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00009305 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00009306 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev5a3af132016-03-29 08:58:54 +00009307 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009308 B.NumIterations, *this, CurScope,
9309 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00009310 return StmtError();
9311 }
9312 }
9313
Alexey Bataev382967a2015-12-08 12:06:20 +00009314 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
9315 // The grainsize clause and num_tasks clause are mutually exclusive and may
9316 // not appear on the same taskloop directive.
9317 if (checkGrainsizeNumTasksClauses(*this, Clauses))
9318 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00009319 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
9320 // If a reduction clause is present on the taskloop directive, the nogroup
9321 // clause must not be specified.
9322 if (checkReductionClauseWithNogroup(*this, Clauses))
9323 return StmtError();
Alexey Bataev438388c2017-11-22 18:34:02 +00009324 if (checkSimdlenSafelenSpecified(*this, Clauses))
9325 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00009326
Reid Kleckner87a31802018-03-12 21:43:02 +00009327 setFunctionHasBranchProtectedScope();
Alexey Bataev0a6ed842015-12-03 09:40:15 +00009328 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
9329 NestedLoopCount, Clauses, AStmt, B);
9330}
9331
Alexey Bataev60e51c42019-10-10 20:13:02 +00009332StmtResult Sema::ActOnOpenMPMasterTaskLoopDirective(
9333 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9334 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9335 if (!AStmt)
9336 return StmtError();
9337
9338 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9339 OMPLoopDirective::HelperExprs B;
9340 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
9341 // define the nested loops number.
9342 unsigned NestedLoopCount =
9343 checkOpenMPLoop(OMPD_master_taskloop, getCollapseNumberExpr(Clauses),
9344 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
9345 VarsWithImplicitDSA, B);
9346 if (NestedLoopCount == 0)
9347 return StmtError();
9348
9349 assert((CurContext->isDependentContext() || B.builtAll()) &&
9350 "omp for loop exprs were not built");
9351
9352 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
9353 // The grainsize clause and num_tasks clause are mutually exclusive and may
9354 // not appear on the same taskloop directive.
9355 if (checkGrainsizeNumTasksClauses(*this, Clauses))
9356 return StmtError();
9357 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
9358 // If a reduction clause is present on the taskloop directive, the nogroup
9359 // clause must not be specified.
9360 if (checkReductionClauseWithNogroup(*this, Clauses))
9361 return StmtError();
9362
9363 setFunctionHasBranchProtectedScope();
9364 return OMPMasterTaskLoopDirective::Create(Context, StartLoc, EndLoc,
9365 NestedLoopCount, Clauses, AStmt, B);
9366}
9367
Alexey Bataev5bbcead2019-10-14 17:17:41 +00009368StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopDirective(
9369 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9370 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9371 if (!AStmt)
9372 return StmtError();
9373
9374 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9375 auto *CS = cast<CapturedStmt>(AStmt);
9376 // 1.2.2 OpenMP Language Terminology
9377 // Structured block - An executable statement with a single entry at the
9378 // top and a single exit at the bottom.
9379 // The point of exit cannot be a branch out of the structured block.
9380 // longjmp() and throw() must not violate the entry/exit criteria.
9381 CS->getCapturedDecl()->setNothrow();
9382 for (int ThisCaptureLevel =
9383 getOpenMPCaptureLevels(OMPD_parallel_master_taskloop);
9384 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9385 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9386 // 1.2.2 OpenMP Language Terminology
9387 // Structured block - An executable statement with a single entry at the
9388 // top and a single exit at the bottom.
9389 // The point of exit cannot be a branch out of the structured block.
9390 // longjmp() and throw() must not violate the entry/exit criteria.
9391 CS->getCapturedDecl()->setNothrow();
9392 }
9393
9394 OMPLoopDirective::HelperExprs B;
9395 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
9396 // define the nested loops number.
9397 unsigned NestedLoopCount = checkOpenMPLoop(
9398 OMPD_parallel_master_taskloop, getCollapseNumberExpr(Clauses),
9399 /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack,
9400 VarsWithImplicitDSA, B);
9401 if (NestedLoopCount == 0)
9402 return StmtError();
9403
9404 assert((CurContext->isDependentContext() || B.builtAll()) &&
9405 "omp for loop exprs were not built");
9406
9407 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
9408 // The grainsize clause and num_tasks clause are mutually exclusive and may
9409 // not appear on the same taskloop directive.
9410 if (checkGrainsizeNumTasksClauses(*this, Clauses))
9411 return StmtError();
9412 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
9413 // If a reduction clause is present on the taskloop directive, the nogroup
9414 // clause must not be specified.
9415 if (checkReductionClauseWithNogroup(*this, Clauses))
9416 return StmtError();
9417
9418 setFunctionHasBranchProtectedScope();
9419 return OMPParallelMasterTaskLoopDirective::Create(
9420 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9421}
9422
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00009423StmtResult Sema::ActOnOpenMPDistributeDirective(
9424 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009425 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00009426 if (!AStmt)
9427 return StmtError();
9428
9429 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9430 OMPLoopDirective::HelperExprs B;
9431 // In presence of clause 'collapse' with number of loops, it will
9432 // define the nested loops number.
9433 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00009434 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00009435 nullptr /*ordered not a clause on distribute*/, AStmt,
9436 *this, *DSAStack, VarsWithImplicitDSA, B);
9437 if (NestedLoopCount == 0)
9438 return StmtError();
9439
9440 assert((CurContext->isDependentContext() || B.builtAll()) &&
9441 "omp for loop exprs were not built");
9442
Reid Kleckner87a31802018-03-12 21:43:02 +00009443 setFunctionHasBranchProtectedScope();
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00009444 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
9445 NestedLoopCount, Clauses, AStmt, B);
9446}
9447
Carlo Bertolli9925f152016-06-27 14:55:37 +00009448StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
9449 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009450 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli9925f152016-06-27 14:55:37 +00009451 if (!AStmt)
9452 return StmtError();
9453
Alexey Bataeve3727102018-04-18 15:57:46 +00009454 auto *CS = cast<CapturedStmt>(AStmt);
Carlo Bertolli9925f152016-06-27 14:55:37 +00009455 // 1.2.2 OpenMP Language Terminology
9456 // Structured block - An executable statement with a single entry at the
9457 // top and a single exit at the bottom.
9458 // The point of exit cannot be a branch out of the structured block.
9459 // longjmp() and throw() must not violate the entry/exit criteria.
9460 CS->getCapturedDecl()->setNothrow();
Alexey Bataev7f96c372017-11-22 17:19:31 +00009461 for (int ThisCaptureLevel =
9462 getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
9463 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9464 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9465 // 1.2.2 OpenMP Language Terminology
9466 // Structured block - An executable statement with a single entry at the
9467 // top and a single exit at the bottom.
9468 // The point of exit cannot be a branch out of the structured block.
9469 // longjmp() and throw() must not violate the entry/exit criteria.
9470 CS->getCapturedDecl()->setNothrow();
9471 }
Carlo Bertolli9925f152016-06-27 14:55:37 +00009472
9473 OMPLoopDirective::HelperExprs B;
9474 // In presence of clause 'collapse' with number of loops, it will
9475 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00009476 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli9925f152016-06-27 14:55:37 +00009477 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev7f96c372017-11-22 17:19:31 +00009478 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Carlo Bertolli9925f152016-06-27 14:55:37 +00009479 VarsWithImplicitDSA, B);
9480 if (NestedLoopCount == 0)
9481 return StmtError();
9482
9483 assert((CurContext->isDependentContext() || B.builtAll()) &&
9484 "omp for loop exprs were not built");
9485
Reid Kleckner87a31802018-03-12 21:43:02 +00009486 setFunctionHasBranchProtectedScope();
Carlo Bertolli9925f152016-06-27 14:55:37 +00009487 return OMPDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00009488 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
9489 DSAStack->isCancelRegion());
Carlo Bertolli9925f152016-06-27 14:55:37 +00009490}
9491
Kelvin Li4a39add2016-07-05 05:00:15 +00009492StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
9493 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009494 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4a39add2016-07-05 05:00:15 +00009495 if (!AStmt)
9496 return StmtError();
9497
Alexey Bataeve3727102018-04-18 15:57:46 +00009498 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4a39add2016-07-05 05:00:15 +00009499 // 1.2.2 OpenMP Language Terminology
9500 // Structured block - An executable statement with a single entry at the
9501 // top and a single exit at the bottom.
9502 // The point of exit cannot be a branch out of the structured block.
9503 // longjmp() and throw() must not violate the entry/exit criteria.
9504 CS->getCapturedDecl()->setNothrow();
Alexey Bataev974acd62017-11-27 19:38:52 +00009505 for (int ThisCaptureLevel =
9506 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
9507 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9508 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9509 // 1.2.2 OpenMP Language Terminology
9510 // Structured block - An executable statement with a single entry at the
9511 // top and a single exit at the bottom.
9512 // The point of exit cannot be a branch out of the structured block.
9513 // longjmp() and throw() must not violate the entry/exit criteria.
9514 CS->getCapturedDecl()->setNothrow();
9515 }
Kelvin Li4a39add2016-07-05 05:00:15 +00009516
9517 OMPLoopDirective::HelperExprs B;
9518 // In presence of clause 'collapse' with number of loops, it will
9519 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00009520 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li4a39add2016-07-05 05:00:15 +00009521 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev974acd62017-11-27 19:38:52 +00009522 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li4a39add2016-07-05 05:00:15 +00009523 VarsWithImplicitDSA, B);
9524 if (NestedLoopCount == 0)
9525 return StmtError();
9526
9527 assert((CurContext->isDependentContext() || B.builtAll()) &&
9528 "omp for loop exprs were not built");
9529
Alexey Bataev438388c2017-11-22 18:34:02 +00009530 if (!CurContext->isDependentContext()) {
9531 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00009532 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00009533 if (auto *LC = dyn_cast<OMPLinearClause>(C))
9534 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9535 B.NumIterations, *this, CurScope,
9536 DSAStack))
9537 return StmtError();
9538 }
9539 }
9540
Kelvin Lic5609492016-07-15 04:39:07 +00009541 if (checkSimdlenSafelenSpecified(*this, Clauses))
9542 return StmtError();
9543
Reid Kleckner87a31802018-03-12 21:43:02 +00009544 setFunctionHasBranchProtectedScope();
Kelvin Li4a39add2016-07-05 05:00:15 +00009545 return OMPDistributeParallelForSimdDirective::Create(
9546 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9547}
9548
Kelvin Li787f3fc2016-07-06 04:45:38 +00009549StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
9550 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009551 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li787f3fc2016-07-06 04:45:38 +00009552 if (!AStmt)
9553 return StmtError();
9554
Alexey Bataeve3727102018-04-18 15:57:46 +00009555 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li787f3fc2016-07-06 04:45:38 +00009556 // 1.2.2 OpenMP Language Terminology
9557 // Structured block - An executable statement with a single entry at the
9558 // top and a single exit at the bottom.
9559 // The point of exit cannot be a branch out of the structured block.
9560 // longjmp() and throw() must not violate the entry/exit criteria.
9561 CS->getCapturedDecl()->setNothrow();
Alexey Bataev617db5f2017-12-04 15:38:33 +00009562 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
9563 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9564 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9565 // 1.2.2 OpenMP Language Terminology
9566 // Structured block - An executable statement with a single entry at the
9567 // top and a single exit at the bottom.
9568 // The point of exit cannot be a branch out of the structured block.
9569 // longjmp() and throw() must not violate the entry/exit criteria.
9570 CS->getCapturedDecl()->setNothrow();
9571 }
Kelvin Li787f3fc2016-07-06 04:45:38 +00009572
9573 OMPLoopDirective::HelperExprs B;
9574 // In presence of clause 'collapse' with number of loops, it will
9575 // define the nested loops number.
9576 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00009577 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev617db5f2017-12-04 15:38:33 +00009578 nullptr /*ordered not a clause on distribute*/, CS, *this,
9579 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li787f3fc2016-07-06 04:45:38 +00009580 if (NestedLoopCount == 0)
9581 return StmtError();
9582
9583 assert((CurContext->isDependentContext() || B.builtAll()) &&
9584 "omp for loop exprs were not built");
9585
Alexey Bataev438388c2017-11-22 18:34:02 +00009586 if (!CurContext->isDependentContext()) {
9587 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00009588 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00009589 if (auto *LC = dyn_cast<OMPLinearClause>(C))
9590 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9591 B.NumIterations, *this, CurScope,
9592 DSAStack))
9593 return StmtError();
9594 }
9595 }
9596
Kelvin Lic5609492016-07-15 04:39:07 +00009597 if (checkSimdlenSafelenSpecified(*this, Clauses))
9598 return StmtError();
9599
Reid Kleckner87a31802018-03-12 21:43:02 +00009600 setFunctionHasBranchProtectedScope();
Kelvin Li787f3fc2016-07-06 04:45:38 +00009601 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
9602 NestedLoopCount, Clauses, AStmt, B);
9603}
9604
Kelvin Lia579b912016-07-14 02:54:56 +00009605StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
9606 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009607 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lia579b912016-07-14 02:54:56 +00009608 if (!AStmt)
9609 return StmtError();
9610
Alexey Bataeve3727102018-04-18 15:57:46 +00009611 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Lia579b912016-07-14 02:54:56 +00009612 // 1.2.2 OpenMP Language Terminology
9613 // Structured block - An executable statement with a single entry at the
9614 // top and a single exit at the bottom.
9615 // The point of exit cannot be a branch out of the structured block.
9616 // longjmp() and throw() must not violate the entry/exit criteria.
9617 CS->getCapturedDecl()->setNothrow();
Alexey Bataev5d7edca2017-11-09 17:32:15 +00009618 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
9619 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9620 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9621 // 1.2.2 OpenMP Language Terminology
9622 // Structured block - An executable statement with a single entry at the
9623 // top and a single exit at the bottom.
9624 // The point of exit cannot be a branch out of the structured block.
9625 // longjmp() and throw() must not violate the entry/exit criteria.
9626 CS->getCapturedDecl()->setNothrow();
9627 }
Kelvin Lia579b912016-07-14 02:54:56 +00009628
9629 OMPLoopDirective::HelperExprs B;
9630 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
9631 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00009632 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lia579b912016-07-14 02:54:56 +00009633 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev5d7edca2017-11-09 17:32:15 +00009634 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Lia579b912016-07-14 02:54:56 +00009635 VarsWithImplicitDSA, B);
9636 if (NestedLoopCount == 0)
9637 return StmtError();
9638
9639 assert((CurContext->isDependentContext() || B.builtAll()) &&
9640 "omp target parallel for simd loop exprs were not built");
9641
9642 if (!CurContext->isDependentContext()) {
9643 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00009644 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00009645 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Lia579b912016-07-14 02:54:56 +00009646 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9647 B.NumIterations, *this, CurScope,
9648 DSAStack))
9649 return StmtError();
9650 }
9651 }
Kelvin Lic5609492016-07-15 04:39:07 +00009652 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00009653 return StmtError();
9654
Reid Kleckner87a31802018-03-12 21:43:02 +00009655 setFunctionHasBranchProtectedScope();
Kelvin Lia579b912016-07-14 02:54:56 +00009656 return OMPTargetParallelForSimdDirective::Create(
9657 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9658}
9659
Kelvin Li986330c2016-07-20 22:57:10 +00009660StmtResult Sema::ActOnOpenMPTargetSimdDirective(
9661 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009662 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li986330c2016-07-20 22:57:10 +00009663 if (!AStmt)
9664 return StmtError();
9665
Alexey Bataeve3727102018-04-18 15:57:46 +00009666 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li986330c2016-07-20 22:57:10 +00009667 // 1.2.2 OpenMP Language Terminology
9668 // Structured block - An executable statement with a single entry at the
9669 // top and a single exit at the bottom.
9670 // The point of exit cannot be a branch out of the structured block.
9671 // longjmp() and throw() must not violate the entry/exit criteria.
9672 CS->getCapturedDecl()->setNothrow();
Alexey Bataevf8365372017-11-17 17:57:25 +00009673 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
9674 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9675 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9676 // 1.2.2 OpenMP Language Terminology
9677 // Structured block - An executable statement with a single entry at the
9678 // top and a single exit at the bottom.
9679 // The point of exit cannot be a branch out of the structured block.
9680 // longjmp() and throw() must not violate the entry/exit criteria.
9681 CS->getCapturedDecl()->setNothrow();
9682 }
9683
Kelvin Li986330c2016-07-20 22:57:10 +00009684 OMPLoopDirective::HelperExprs B;
9685 // In presence of clause 'collapse' with number of loops, it will define the
9686 // nested loops number.
David Majnemer9d168222016-08-05 17:44:54 +00009687 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00009688 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevf8365372017-11-17 17:57:25 +00009689 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Li986330c2016-07-20 22:57:10 +00009690 VarsWithImplicitDSA, B);
9691 if (NestedLoopCount == 0)
9692 return StmtError();
9693
9694 assert((CurContext->isDependentContext() || B.builtAll()) &&
9695 "omp target simd loop exprs were not built");
9696
9697 if (!CurContext->isDependentContext()) {
9698 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00009699 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00009700 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Li986330c2016-07-20 22:57:10 +00009701 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9702 B.NumIterations, *this, CurScope,
9703 DSAStack))
9704 return StmtError();
9705 }
9706 }
9707
9708 if (checkSimdlenSafelenSpecified(*this, Clauses))
9709 return StmtError();
9710
Reid Kleckner87a31802018-03-12 21:43:02 +00009711 setFunctionHasBranchProtectedScope();
Kelvin Li986330c2016-07-20 22:57:10 +00009712 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
9713 NestedLoopCount, Clauses, AStmt, B);
9714}
9715
Kelvin Li02532872016-08-05 14:37:37 +00009716StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
9717 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009718 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li02532872016-08-05 14:37:37 +00009719 if (!AStmt)
9720 return StmtError();
9721
Alexey Bataeve3727102018-04-18 15:57:46 +00009722 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li02532872016-08-05 14:37:37 +00009723 // 1.2.2 OpenMP Language Terminology
9724 // Structured block - An executable statement with a single entry at the
9725 // top and a single exit at the bottom.
9726 // The point of exit cannot be a branch out of the structured block.
9727 // longjmp() and throw() must not violate the entry/exit criteria.
9728 CS->getCapturedDecl()->setNothrow();
Alexey Bataev95c6dd42017-11-29 15:14:16 +00009729 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
9730 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9731 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9732 // 1.2.2 OpenMP Language Terminology
9733 // Structured block - An executable statement with a single entry at the
9734 // top and a single exit at the bottom.
9735 // The point of exit cannot be a branch out of the structured block.
9736 // longjmp() and throw() must not violate the entry/exit criteria.
9737 CS->getCapturedDecl()->setNothrow();
9738 }
Kelvin Li02532872016-08-05 14:37:37 +00009739
9740 OMPLoopDirective::HelperExprs B;
9741 // In presence of clause 'collapse' with number of loops, it will
9742 // define the nested loops number.
9743 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00009744 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
Alexey Bataev95c6dd42017-11-29 15:14:16 +00009745 nullptr /*ordered not a clause on distribute*/, CS, *this,
9746 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li02532872016-08-05 14:37:37 +00009747 if (NestedLoopCount == 0)
9748 return StmtError();
9749
9750 assert((CurContext->isDependentContext() || B.builtAll()) &&
9751 "omp teams distribute loop exprs were not built");
9752
Reid Kleckner87a31802018-03-12 21:43:02 +00009753 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00009754
9755 DSAStack->setParentTeamsRegionLoc(StartLoc);
9756
David Majnemer9d168222016-08-05 17:44:54 +00009757 return OMPTeamsDistributeDirective::Create(
9758 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Kelvin Li02532872016-08-05 14:37:37 +00009759}
9760
Kelvin Li4e325f72016-10-25 12:50:55 +00009761StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
9762 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009763 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4e325f72016-10-25 12:50:55 +00009764 if (!AStmt)
9765 return StmtError();
9766
Alexey Bataeve3727102018-04-18 15:57:46 +00009767 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4e325f72016-10-25 12:50:55 +00009768 // 1.2.2 OpenMP Language Terminology
9769 // Structured block - An executable statement with a single entry at the
9770 // top and a single exit at the bottom.
9771 // The point of exit cannot be a branch out of the structured block.
9772 // longjmp() and throw() must not violate the entry/exit criteria.
9773 CS->getCapturedDecl()->setNothrow();
Alexey Bataev999277a2017-12-06 14:31:09 +00009774 for (int ThisCaptureLevel =
9775 getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
9776 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9777 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9778 // 1.2.2 OpenMP Language Terminology
9779 // Structured block - An executable statement with a single entry at the
9780 // top and a single exit at the bottom.
9781 // The point of exit cannot be a branch out of the structured block.
9782 // longjmp() and throw() must not violate the entry/exit criteria.
9783 CS->getCapturedDecl()->setNothrow();
9784 }
9785
Kelvin Li4e325f72016-10-25 12:50:55 +00009786
9787 OMPLoopDirective::HelperExprs B;
9788 // In presence of clause 'collapse' with number of loops, it will
9789 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00009790 unsigned NestedLoopCount = checkOpenMPLoop(
Samuel Antao4c8035b2016-12-12 18:00:20 +00009791 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev999277a2017-12-06 14:31:09 +00009792 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Samuel Antao4c8035b2016-12-12 18:00:20 +00009793 VarsWithImplicitDSA, B);
Kelvin Li4e325f72016-10-25 12:50:55 +00009794
9795 if (NestedLoopCount == 0)
9796 return StmtError();
9797
9798 assert((CurContext->isDependentContext() || B.builtAll()) &&
9799 "omp teams distribute simd loop exprs were not built");
9800
9801 if (!CurContext->isDependentContext()) {
9802 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00009803 for (OMPClause *C : Clauses) {
Kelvin Li4e325f72016-10-25 12:50:55 +00009804 if (auto *LC = dyn_cast<OMPLinearClause>(C))
9805 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9806 B.NumIterations, *this, CurScope,
9807 DSAStack))
9808 return StmtError();
9809 }
9810 }
9811
9812 if (checkSimdlenSafelenSpecified(*this, Clauses))
9813 return StmtError();
9814
Reid Kleckner87a31802018-03-12 21:43:02 +00009815 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00009816
9817 DSAStack->setParentTeamsRegionLoc(StartLoc);
9818
Kelvin Li4e325f72016-10-25 12:50:55 +00009819 return OMPTeamsDistributeSimdDirective::Create(
9820 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9821}
9822
Kelvin Li579e41c2016-11-30 23:51:03 +00009823StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
9824 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009825 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li579e41c2016-11-30 23:51:03 +00009826 if (!AStmt)
9827 return StmtError();
9828
Alexey Bataeve3727102018-04-18 15:57:46 +00009829 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li579e41c2016-11-30 23:51:03 +00009830 // 1.2.2 OpenMP Language Terminology
9831 // Structured block - An executable statement with a single entry at the
9832 // top and a single exit at the bottom.
9833 // The point of exit cannot be a branch out of the structured block.
9834 // longjmp() and throw() must not violate the entry/exit criteria.
9835 CS->getCapturedDecl()->setNothrow();
9836
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00009837 for (int ThisCaptureLevel =
9838 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
9839 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9840 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9841 // 1.2.2 OpenMP Language Terminology
9842 // Structured block - An executable statement with a single entry at the
9843 // top and a single exit at the bottom.
9844 // The point of exit cannot be a branch out of the structured block.
9845 // longjmp() and throw() must not violate the entry/exit criteria.
9846 CS->getCapturedDecl()->setNothrow();
9847 }
9848
Kelvin Li579e41c2016-11-30 23:51:03 +00009849 OMPLoopDirective::HelperExprs B;
9850 // In presence of clause 'collapse' with number of loops, it will
9851 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00009852 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li579e41c2016-11-30 23:51:03 +00009853 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00009854 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li579e41c2016-11-30 23:51:03 +00009855 VarsWithImplicitDSA, B);
9856
9857 if (NestedLoopCount == 0)
9858 return StmtError();
9859
9860 assert((CurContext->isDependentContext() || B.builtAll()) &&
9861 "omp for loop exprs were not built");
9862
9863 if (!CurContext->isDependentContext()) {
9864 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00009865 for (OMPClause *C : Clauses) {
Kelvin Li579e41c2016-11-30 23:51:03 +00009866 if (auto *LC = dyn_cast<OMPLinearClause>(C))
9867 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9868 B.NumIterations, *this, CurScope,
9869 DSAStack))
9870 return StmtError();
9871 }
9872 }
9873
9874 if (checkSimdlenSafelenSpecified(*this, Clauses))
9875 return StmtError();
9876
Reid Kleckner87a31802018-03-12 21:43:02 +00009877 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00009878
9879 DSAStack->setParentTeamsRegionLoc(StartLoc);
9880
Kelvin Li579e41c2016-11-30 23:51:03 +00009881 return OMPTeamsDistributeParallelForSimdDirective::Create(
9882 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9883}
9884
Kelvin Li7ade93f2016-12-09 03:24:30 +00009885StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
9886 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009887 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li7ade93f2016-12-09 03:24:30 +00009888 if (!AStmt)
9889 return StmtError();
9890
Alexey Bataeve3727102018-04-18 15:57:46 +00009891 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li7ade93f2016-12-09 03:24:30 +00009892 // 1.2.2 OpenMP Language Terminology
9893 // Structured block - An executable statement with a single entry at the
9894 // top and a single exit at the bottom.
9895 // The point of exit cannot be a branch out of the structured block.
9896 // longjmp() and throw() must not violate the entry/exit criteria.
9897 CS->getCapturedDecl()->setNothrow();
9898
Carlo Bertolli62fae152017-11-20 20:46:39 +00009899 for (int ThisCaptureLevel =
9900 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
9901 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9902 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9903 // 1.2.2 OpenMP Language Terminology
9904 // Structured block - An executable statement with a single entry at the
9905 // top and a single exit at the bottom.
9906 // The point of exit cannot be a branch out of the structured block.
9907 // longjmp() and throw() must not violate the entry/exit criteria.
9908 CS->getCapturedDecl()->setNothrow();
9909 }
9910
Kelvin Li7ade93f2016-12-09 03:24:30 +00009911 OMPLoopDirective::HelperExprs B;
9912 // In presence of clause 'collapse' with number of loops, it will
9913 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00009914 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li7ade93f2016-12-09 03:24:30 +00009915 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Carlo Bertolli62fae152017-11-20 20:46:39 +00009916 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li7ade93f2016-12-09 03:24:30 +00009917 VarsWithImplicitDSA, B);
9918
9919 if (NestedLoopCount == 0)
9920 return StmtError();
9921
9922 assert((CurContext->isDependentContext() || B.builtAll()) &&
9923 "omp for loop exprs were not built");
9924
Reid Kleckner87a31802018-03-12 21:43:02 +00009925 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00009926
9927 DSAStack->setParentTeamsRegionLoc(StartLoc);
9928
Kelvin Li7ade93f2016-12-09 03:24:30 +00009929 return OMPTeamsDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00009930 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
9931 DSAStack->isCancelRegion());
Kelvin Li7ade93f2016-12-09 03:24:30 +00009932}
9933
Kelvin Libf594a52016-12-17 05:48:59 +00009934StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
9935 Stmt *AStmt,
9936 SourceLocation StartLoc,
9937 SourceLocation EndLoc) {
9938 if (!AStmt)
9939 return StmtError();
9940
Alexey Bataeve3727102018-04-18 15:57:46 +00009941 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Libf594a52016-12-17 05:48:59 +00009942 // 1.2.2 OpenMP Language Terminology
9943 // Structured block - An executable statement with a single entry at the
9944 // top and a single exit at the bottom.
9945 // The point of exit cannot be a branch out of the structured block.
9946 // longjmp() and throw() must not violate the entry/exit criteria.
9947 CS->getCapturedDecl()->setNothrow();
9948
Alexey Bataevf9fc42e2017-11-22 14:25:55 +00009949 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
9950 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9951 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9952 // 1.2.2 OpenMP Language Terminology
9953 // Structured block - An executable statement with a single entry at the
9954 // top and a single exit at the bottom.
9955 // The point of exit cannot be a branch out of the structured block.
9956 // longjmp() and throw() must not violate the entry/exit criteria.
9957 CS->getCapturedDecl()->setNothrow();
9958 }
Reid Kleckner87a31802018-03-12 21:43:02 +00009959 setFunctionHasBranchProtectedScope();
Kelvin Libf594a52016-12-17 05:48:59 +00009960
9961 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
9962 AStmt);
9963}
9964
Kelvin Li83c451e2016-12-25 04:52:54 +00009965StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
9966 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009967 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li83c451e2016-12-25 04:52:54 +00009968 if (!AStmt)
9969 return StmtError();
9970
Alexey Bataeve3727102018-04-18 15:57:46 +00009971 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li83c451e2016-12-25 04:52:54 +00009972 // 1.2.2 OpenMP Language Terminology
9973 // Structured block - An executable statement with a single entry at the
9974 // top and a single exit at the bottom.
9975 // The point of exit cannot be a branch out of the structured block.
9976 // longjmp() and throw() must not violate the entry/exit criteria.
9977 CS->getCapturedDecl()->setNothrow();
Alexey Bataevdfa430f2017-12-08 15:03:50 +00009978 for (int ThisCaptureLevel =
9979 getOpenMPCaptureLevels(OMPD_target_teams_distribute);
9980 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9981 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9982 // 1.2.2 OpenMP Language Terminology
9983 // Structured block - An executable statement with a single entry at the
9984 // top and a single exit at the bottom.
9985 // The point of exit cannot be a branch out of the structured block.
9986 // longjmp() and throw() must not violate the entry/exit criteria.
9987 CS->getCapturedDecl()->setNothrow();
9988 }
Kelvin Li83c451e2016-12-25 04:52:54 +00009989
9990 OMPLoopDirective::HelperExprs B;
9991 // In presence of clause 'collapse' with number of loops, it will
9992 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00009993 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataevdfa430f2017-12-08 15:03:50 +00009994 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
9995 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li83c451e2016-12-25 04:52:54 +00009996 VarsWithImplicitDSA, B);
9997 if (NestedLoopCount == 0)
9998 return StmtError();
9999
10000 assert((CurContext->isDependentContext() || B.builtAll()) &&
10001 "omp target teams distribute loop exprs were not built");
10002
Reid Kleckner87a31802018-03-12 21:43:02 +000010003 setFunctionHasBranchProtectedScope();
Kelvin Li83c451e2016-12-25 04:52:54 +000010004 return OMPTargetTeamsDistributeDirective::Create(
10005 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
10006}
10007
Kelvin Li80e8f562016-12-29 22:16:30 +000010008StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
10009 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +000010010 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li80e8f562016-12-29 22:16:30 +000010011 if (!AStmt)
10012 return StmtError();
10013
Alexey Bataeve3727102018-04-18 15:57:46 +000010014 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li80e8f562016-12-29 22:16:30 +000010015 // 1.2.2 OpenMP Language Terminology
10016 // Structured block - An executable statement with a single entry at the
10017 // top and a single exit at the bottom.
10018 // The point of exit cannot be a branch out of the structured block.
10019 // longjmp() and throw() must not violate the entry/exit criteria.
10020 CS->getCapturedDecl()->setNothrow();
Carlo Bertolli52978c32018-01-03 21:12:44 +000010021 for (int ThisCaptureLevel =
10022 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
10023 ThisCaptureLevel > 1; --ThisCaptureLevel) {
10024 CS = cast<CapturedStmt>(CS->getCapturedStmt());
10025 // 1.2.2 OpenMP Language Terminology
10026 // Structured block - An executable statement with a single entry at the
10027 // top and a single exit at the bottom.
10028 // The point of exit cannot be a branch out of the structured block.
10029 // longjmp() and throw() must not violate the entry/exit criteria.
10030 CS->getCapturedDecl()->setNothrow();
10031 }
10032
Kelvin Li80e8f562016-12-29 22:16:30 +000010033 OMPLoopDirective::HelperExprs B;
10034 // In presence of clause 'collapse' with number of loops, it will
10035 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +000010036 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli52978c32018-01-03 21:12:44 +000010037 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
10038 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li80e8f562016-12-29 22:16:30 +000010039 VarsWithImplicitDSA, B);
10040 if (NestedLoopCount == 0)
10041 return StmtError();
10042
10043 assert((CurContext->isDependentContext() || B.builtAll()) &&
10044 "omp target teams distribute parallel for loop exprs were not built");
10045
Alexey Bataev647dd842018-01-15 20:59:40 +000010046 if (!CurContext->isDependentContext()) {
10047 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000010048 for (OMPClause *C : Clauses) {
Alexey Bataev647dd842018-01-15 20:59:40 +000010049 if (auto *LC = dyn_cast<OMPLinearClause>(C))
10050 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
10051 B.NumIterations, *this, CurScope,
10052 DSAStack))
10053 return StmtError();
10054 }
10055 }
10056
Reid Kleckner87a31802018-03-12 21:43:02 +000010057 setFunctionHasBranchProtectedScope();
Kelvin Li80e8f562016-12-29 22:16:30 +000010058 return OMPTargetTeamsDistributeParallelForDirective::Create(
Alexey Bataev16e79882017-11-22 21:12:03 +000010059 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
10060 DSAStack->isCancelRegion());
Kelvin Li80e8f562016-12-29 22:16:30 +000010061}
10062
Kelvin Li1851df52017-01-03 05:23:48 +000010063StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
10064 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +000010065 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li1851df52017-01-03 05:23:48 +000010066 if (!AStmt)
10067 return StmtError();
10068
Alexey Bataeve3727102018-04-18 15:57:46 +000010069 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li1851df52017-01-03 05:23:48 +000010070 // 1.2.2 OpenMP Language Terminology
10071 // Structured block - An executable statement with a single entry at the
10072 // top and a single exit at the bottom.
10073 // The point of exit cannot be a branch out of the structured block.
10074 // longjmp() and throw() must not violate the entry/exit criteria.
10075 CS->getCapturedDecl()->setNothrow();
Alexey Bataev647dd842018-01-15 20:59:40 +000010076 for (int ThisCaptureLevel = getOpenMPCaptureLevels(
10077 OMPD_target_teams_distribute_parallel_for_simd);
10078 ThisCaptureLevel > 1; --ThisCaptureLevel) {
10079 CS = cast<CapturedStmt>(CS->getCapturedStmt());
10080 // 1.2.2 OpenMP Language Terminology
10081 // Structured block - An executable statement with a single entry at the
10082 // top and a single exit at the bottom.
10083 // The point of exit cannot be a branch out of the structured block.
10084 // longjmp() and throw() must not violate the entry/exit criteria.
10085 CS->getCapturedDecl()->setNothrow();
10086 }
Kelvin Li1851df52017-01-03 05:23:48 +000010087
10088 OMPLoopDirective::HelperExprs B;
10089 // In presence of clause 'collapse' with number of loops, it will
10090 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +000010091 unsigned NestedLoopCount =
10092 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
Alexey Bataev647dd842018-01-15 20:59:40 +000010093 getCollapseNumberExpr(Clauses),
10094 nullptr /*ordered not a clause on distribute*/, CS, *this,
10095 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li1851df52017-01-03 05:23:48 +000010096 if (NestedLoopCount == 0)
10097 return StmtError();
10098
10099 assert((CurContext->isDependentContext() || B.builtAll()) &&
10100 "omp target teams distribute parallel for simd loop exprs were not "
10101 "built");
10102
10103 if (!CurContext->isDependentContext()) {
10104 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000010105 for (OMPClause *C : Clauses) {
Kelvin Li1851df52017-01-03 05:23:48 +000010106 if (auto *LC = dyn_cast<OMPLinearClause>(C))
10107 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
10108 B.NumIterations, *this, CurScope,
10109 DSAStack))
10110 return StmtError();
10111 }
10112 }
10113
Alexey Bataev438388c2017-11-22 18:34:02 +000010114 if (checkSimdlenSafelenSpecified(*this, Clauses))
10115 return StmtError();
10116
Reid Kleckner87a31802018-03-12 21:43:02 +000010117 setFunctionHasBranchProtectedScope();
Kelvin Li1851df52017-01-03 05:23:48 +000010118 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
10119 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
10120}
10121
Kelvin Lida681182017-01-10 18:08:18 +000010122StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
10123 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +000010124 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lida681182017-01-10 18:08:18 +000010125 if (!AStmt)
10126 return StmtError();
10127
10128 auto *CS = cast<CapturedStmt>(AStmt);
10129 // 1.2.2 OpenMP Language Terminology
10130 // Structured block - An executable statement with a single entry at the
10131 // top and a single exit at the bottom.
10132 // The point of exit cannot be a branch out of the structured block.
10133 // longjmp() and throw() must not violate the entry/exit criteria.
10134 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfbe17fb2017-12-13 19:45:06 +000010135 for (int ThisCaptureLevel =
10136 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
10137 ThisCaptureLevel > 1; --ThisCaptureLevel) {
10138 CS = cast<CapturedStmt>(CS->getCapturedStmt());
10139 // 1.2.2 OpenMP Language Terminology
10140 // Structured block - An executable statement with a single entry at the
10141 // top and a single exit at the bottom.
10142 // The point of exit cannot be a branch out of the structured block.
10143 // longjmp() and throw() must not violate the entry/exit criteria.
10144 CS->getCapturedDecl()->setNothrow();
10145 }
Kelvin Lida681182017-01-10 18:08:18 +000010146
10147 OMPLoopDirective::HelperExprs B;
10148 // In presence of clause 'collapse' with number of loops, it will
10149 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +000010150 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lida681182017-01-10 18:08:18 +000010151 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevfbe17fb2017-12-13 19:45:06 +000010152 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Lida681182017-01-10 18:08:18 +000010153 VarsWithImplicitDSA, B);
10154 if (NestedLoopCount == 0)
10155 return StmtError();
10156
10157 assert((CurContext->isDependentContext() || B.builtAll()) &&
10158 "omp target teams distribute simd loop exprs were not built");
10159
Alexey Bataev438388c2017-11-22 18:34:02 +000010160 if (!CurContext->isDependentContext()) {
10161 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000010162 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +000010163 if (auto *LC = dyn_cast<OMPLinearClause>(C))
10164 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
10165 B.NumIterations, *this, CurScope,
10166 DSAStack))
10167 return StmtError();
10168 }
10169 }
10170
10171 if (checkSimdlenSafelenSpecified(*this, Clauses))
10172 return StmtError();
10173
Reid Kleckner87a31802018-03-12 21:43:02 +000010174 setFunctionHasBranchProtectedScope();
Kelvin Lida681182017-01-10 18:08:18 +000010175 return OMPTargetTeamsDistributeSimdDirective::Create(
10176 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
10177}
10178
Alexey Bataeved09d242014-05-28 05:53:51 +000010179OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +000010180 SourceLocation StartLoc,
10181 SourceLocation LParenLoc,
10182 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +000010183 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +000010184 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +000010185 case OMPC_final:
10186 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
10187 break;
Alexey Bataev568a8332014-03-06 06:15:19 +000010188 case OMPC_num_threads:
10189 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
10190 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +000010191 case OMPC_safelen:
10192 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
10193 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +000010194 case OMPC_simdlen:
10195 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
10196 break;
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000010197 case OMPC_allocator:
10198 Res = ActOnOpenMPAllocatorClause(Expr, StartLoc, LParenLoc, EndLoc);
10199 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +000010200 case OMPC_collapse:
10201 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
10202 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +000010203 case OMPC_ordered:
10204 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
10205 break;
Michael Wonge710d542015-08-07 16:16:36 +000010206 case OMPC_device:
10207 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
10208 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010209 case OMPC_num_teams:
10210 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
10211 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010212 case OMPC_thread_limit:
10213 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
10214 break;
Alexey Bataeva0569352015-12-01 10:17:31 +000010215 case OMPC_priority:
10216 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
10217 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000010218 case OMPC_grainsize:
10219 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
10220 break;
Alexey Bataev382967a2015-12-08 12:06:20 +000010221 case OMPC_num_tasks:
10222 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
10223 break;
Alexey Bataev28c75412015-12-15 08:19:24 +000010224 case OMPC_hint:
10225 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
10226 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +000010227 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +000010228 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +000010229 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +000010230 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +000010231 case OMPC_private:
10232 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +000010233 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +000010234 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +000010235 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +000010236 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +000010237 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +000010238 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010239 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010240 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +000010241 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +000010242 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +000010243 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +000010244 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +000010245 case OMPC_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000010246 case OMPC_allocate:
Alexey Bataev6125da92014-07-21 11:26:11 +000010247 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +000010248 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +000010249 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +000010250 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +000010251 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +000010252 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010253 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +000010254 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +000010255 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +000010256 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +000010257 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +000010258 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000010259 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +000010260 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +000010261 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +000010262 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +000010263 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +000010264 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +000010265 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +000010266 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +000010267 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +000010268 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +000010269 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +000010270 case OMPC_atomic_default_mem_order:
Alexey Bataev729e2422019-08-23 16:11:14 +000010271 case OMPC_device_type:
Alexey Bataevdba792c2019-09-23 18:13:31 +000010272 case OMPC_match:
Alexey Bataevaadd52e2014-02-13 05:29:23 +000010273 llvm_unreachable("Clause is not allowed.");
10274 }
10275 return Res;
10276}
10277
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010278// An OpenMP directive such as 'target parallel' has two captured regions:
10279// for the 'target' and 'parallel' respectively. This function returns
10280// the region in which to capture expressions associated with a clause.
10281// A return value of OMPD_unknown signifies that the expression should not
10282// be captured.
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +000010283static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
10284 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
10285 OpenMPDirectiveKind NameModifier = OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010286 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010287 switch (CKind) {
10288 case OMPC_if:
10289 switch (DKind) {
10290 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +000010291 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +000010292 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010293 // If this clause applies to the nested 'parallel' region, capture within
10294 // the 'target' region, otherwise do not capture.
10295 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
10296 CaptureRegion = OMPD_target;
10297 break;
Carlo Bertolli52978c32018-01-03 21:12:44 +000010298 case OMPD_target_teams_distribute_parallel_for:
10299 case OMPD_target_teams_distribute_parallel_for_simd:
10300 // If this clause applies to the nested 'parallel' region, capture within
10301 // the 'teams' region, otherwise do not capture.
10302 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
10303 CaptureRegion = OMPD_teams;
10304 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +000010305 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +000010306 case OMPD_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +000010307 CaptureRegion = OMPD_teams;
10308 break;
Alexey Bataevd2202ca2017-12-27 17:58:32 +000010309 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +000010310 case OMPD_target_enter_data:
10311 case OMPD_target_exit_data:
Alexey Bataevd2202ca2017-12-27 17:58:32 +000010312 CaptureRegion = OMPD_task;
10313 break;
Alexey Bataev5bbcead2019-10-14 17:17:41 +000010314 case OMPD_parallel_master_taskloop:
10315 if (NameModifier == OMPD_unknown || NameModifier == OMPD_taskloop)
10316 CaptureRegion = OMPD_parallel;
10317 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010318 case OMPD_cancel:
10319 case OMPD_parallel:
10320 case OMPD_parallel_sections:
10321 case OMPD_parallel_for:
10322 case OMPD_parallel_for_simd:
10323 case OMPD_target:
10324 case OMPD_target_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010325 case OMPD_target_teams:
10326 case OMPD_target_teams_distribute:
10327 case OMPD_target_teams_distribute_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010328 case OMPD_distribute_parallel_for:
10329 case OMPD_distribute_parallel_for_simd:
10330 case OMPD_task:
10331 case OMPD_taskloop:
10332 case OMPD_taskloop_simd:
Alexey Bataev60e51c42019-10-10 20:13:02 +000010333 case OMPD_master_taskloop:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010334 case OMPD_target_data:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010335 // Do not capture if-clause expressions.
10336 break;
10337 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000010338 case OMPD_allocate:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010339 case OMPD_taskyield:
10340 case OMPD_barrier:
10341 case OMPD_taskwait:
10342 case OMPD_cancellation_point:
10343 case OMPD_flush:
10344 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +000010345 case OMPD_declare_mapper:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010346 case OMPD_declare_simd:
Alexey Bataevd158cf62019-09-13 20:18:17 +000010347 case OMPD_declare_variant:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010348 case OMPD_declare_target:
10349 case OMPD_end_declare_target:
10350 case OMPD_teams:
10351 case OMPD_simd:
10352 case OMPD_for:
10353 case OMPD_for_simd:
10354 case OMPD_sections:
10355 case OMPD_section:
10356 case OMPD_single:
10357 case OMPD_master:
10358 case OMPD_critical:
10359 case OMPD_taskgroup:
10360 case OMPD_distribute:
10361 case OMPD_ordered:
10362 case OMPD_atomic:
10363 case OMPD_distribute_simd:
10364 case OMPD_teams_distribute:
10365 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +000010366 case OMPD_requires:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010367 llvm_unreachable("Unexpected OpenMP directive with if-clause");
10368 case OMPD_unknown:
10369 llvm_unreachable("Unknown OpenMP directive");
10370 }
10371 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +000010372 case OMPC_num_threads:
10373 switch (DKind) {
10374 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +000010375 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +000010376 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +000010377 CaptureRegion = OMPD_target;
10378 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +000010379 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +000010380 case OMPD_teams_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +000010381 case OMPD_target_teams_distribute_parallel_for:
10382 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +000010383 CaptureRegion = OMPD_teams;
10384 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +000010385 case OMPD_parallel:
10386 case OMPD_parallel_sections:
10387 case OMPD_parallel_for:
10388 case OMPD_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +000010389 case OMPD_distribute_parallel_for:
10390 case OMPD_distribute_parallel_for_simd:
Alexey Bataev5bbcead2019-10-14 17:17:41 +000010391 case OMPD_parallel_master_taskloop:
Alexey Bataev2ba67042017-11-28 21:11:44 +000010392 // Do not capture num_threads-clause expressions.
10393 break;
10394 case OMPD_target_data:
10395 case OMPD_target_enter_data:
10396 case OMPD_target_exit_data:
10397 case OMPD_target_update:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +000010398 case OMPD_target:
10399 case OMPD_target_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +000010400 case OMPD_target_teams:
10401 case OMPD_target_teams_distribute:
10402 case OMPD_target_teams_distribute_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +000010403 case OMPD_cancel:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +000010404 case OMPD_task:
10405 case OMPD_taskloop:
10406 case OMPD_taskloop_simd:
Alexey Bataev60e51c42019-10-10 20:13:02 +000010407 case OMPD_master_taskloop:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +000010408 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000010409 case OMPD_allocate:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +000010410 case OMPD_taskyield:
10411 case OMPD_barrier:
10412 case OMPD_taskwait:
10413 case OMPD_cancellation_point:
10414 case OMPD_flush:
10415 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +000010416 case OMPD_declare_mapper:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +000010417 case OMPD_declare_simd:
Alexey Bataevd158cf62019-09-13 20:18:17 +000010418 case OMPD_declare_variant:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +000010419 case OMPD_declare_target:
10420 case OMPD_end_declare_target:
10421 case OMPD_teams:
10422 case OMPD_simd:
10423 case OMPD_for:
10424 case OMPD_for_simd:
10425 case OMPD_sections:
10426 case OMPD_section:
10427 case OMPD_single:
10428 case OMPD_master:
10429 case OMPD_critical:
10430 case OMPD_taskgroup:
10431 case OMPD_distribute:
10432 case OMPD_ordered:
10433 case OMPD_atomic:
10434 case OMPD_distribute_simd:
10435 case OMPD_teams_distribute:
10436 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +000010437 case OMPD_requires:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +000010438 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
10439 case OMPD_unknown:
10440 llvm_unreachable("Unknown OpenMP directive");
10441 }
10442 break;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000010443 case OMPC_num_teams:
10444 switch (DKind) {
10445 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +000010446 case OMPD_target_teams_distribute:
10447 case OMPD_target_teams_distribute_simd:
10448 case OMPD_target_teams_distribute_parallel_for:
10449 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000010450 CaptureRegion = OMPD_target;
10451 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +000010452 case OMPD_teams_distribute_parallel_for:
10453 case OMPD_teams_distribute_parallel_for_simd:
10454 case OMPD_teams:
10455 case OMPD_teams_distribute:
10456 case OMPD_teams_distribute_simd:
10457 // Do not capture num_teams-clause expressions.
10458 break;
10459 case OMPD_distribute_parallel_for:
10460 case OMPD_distribute_parallel_for_simd:
10461 case OMPD_task:
10462 case OMPD_taskloop:
10463 case OMPD_taskloop_simd:
Alexey Bataev60e51c42019-10-10 20:13:02 +000010464 case OMPD_master_taskloop:
Alexey Bataev5bbcead2019-10-14 17:17:41 +000010465 case OMPD_parallel_master_taskloop:
Alexey Bataev2ba67042017-11-28 21:11:44 +000010466 case OMPD_target_data:
10467 case OMPD_target_enter_data:
10468 case OMPD_target_exit_data:
10469 case OMPD_target_update:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000010470 case OMPD_cancel:
10471 case OMPD_parallel:
10472 case OMPD_parallel_sections:
10473 case OMPD_parallel_for:
10474 case OMPD_parallel_for_simd:
10475 case OMPD_target:
10476 case OMPD_target_simd:
10477 case OMPD_target_parallel:
10478 case OMPD_target_parallel_for:
10479 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000010480 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000010481 case OMPD_allocate:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000010482 case OMPD_taskyield:
10483 case OMPD_barrier:
10484 case OMPD_taskwait:
10485 case OMPD_cancellation_point:
10486 case OMPD_flush:
10487 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +000010488 case OMPD_declare_mapper:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000010489 case OMPD_declare_simd:
Alexey Bataevd158cf62019-09-13 20:18:17 +000010490 case OMPD_declare_variant:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000010491 case OMPD_declare_target:
10492 case OMPD_end_declare_target:
10493 case OMPD_simd:
10494 case OMPD_for:
10495 case OMPD_for_simd:
10496 case OMPD_sections:
10497 case OMPD_section:
10498 case OMPD_single:
10499 case OMPD_master:
10500 case OMPD_critical:
10501 case OMPD_taskgroup:
10502 case OMPD_distribute:
10503 case OMPD_ordered:
10504 case OMPD_atomic:
10505 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +000010506 case OMPD_requires:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000010507 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
10508 case OMPD_unknown:
10509 llvm_unreachable("Unknown OpenMP directive");
10510 }
10511 break;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000010512 case OMPC_thread_limit:
10513 switch (DKind) {
10514 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +000010515 case OMPD_target_teams_distribute:
10516 case OMPD_target_teams_distribute_simd:
10517 case OMPD_target_teams_distribute_parallel_for:
10518 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000010519 CaptureRegion = OMPD_target;
10520 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +000010521 case OMPD_teams_distribute_parallel_for:
10522 case OMPD_teams_distribute_parallel_for_simd:
10523 case OMPD_teams:
10524 case OMPD_teams_distribute:
10525 case OMPD_teams_distribute_simd:
10526 // Do not capture thread_limit-clause expressions.
10527 break;
10528 case OMPD_distribute_parallel_for:
10529 case OMPD_distribute_parallel_for_simd:
10530 case OMPD_task:
10531 case OMPD_taskloop:
10532 case OMPD_taskloop_simd:
Alexey Bataev60e51c42019-10-10 20:13:02 +000010533 case OMPD_master_taskloop:
Alexey Bataev5bbcead2019-10-14 17:17:41 +000010534 case OMPD_parallel_master_taskloop:
Alexey Bataev2ba67042017-11-28 21:11:44 +000010535 case OMPD_target_data:
10536 case OMPD_target_enter_data:
10537 case OMPD_target_exit_data:
10538 case OMPD_target_update:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000010539 case OMPD_cancel:
10540 case OMPD_parallel:
10541 case OMPD_parallel_sections:
10542 case OMPD_parallel_for:
10543 case OMPD_parallel_for_simd:
10544 case OMPD_target:
10545 case OMPD_target_simd:
10546 case OMPD_target_parallel:
10547 case OMPD_target_parallel_for:
10548 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000010549 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000010550 case OMPD_allocate:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000010551 case OMPD_taskyield:
10552 case OMPD_barrier:
10553 case OMPD_taskwait:
10554 case OMPD_cancellation_point:
10555 case OMPD_flush:
10556 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +000010557 case OMPD_declare_mapper:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000010558 case OMPD_declare_simd:
Alexey Bataevd158cf62019-09-13 20:18:17 +000010559 case OMPD_declare_variant:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000010560 case OMPD_declare_target:
10561 case OMPD_end_declare_target:
10562 case OMPD_simd:
10563 case OMPD_for:
10564 case OMPD_for_simd:
10565 case OMPD_sections:
10566 case OMPD_section:
10567 case OMPD_single:
10568 case OMPD_master:
10569 case OMPD_critical:
10570 case OMPD_taskgroup:
10571 case OMPD_distribute:
10572 case OMPD_ordered:
10573 case OMPD_atomic:
10574 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +000010575 case OMPD_requires:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000010576 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
10577 case OMPD_unknown:
10578 llvm_unreachable("Unknown OpenMP directive");
10579 }
10580 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010581 case OMPC_schedule:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +000010582 switch (DKind) {
Alexey Bataev2ba67042017-11-28 21:11:44 +000010583 case OMPD_parallel_for:
10584 case OMPD_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +000010585 case OMPD_distribute_parallel_for:
Alexey Bataev974acd62017-11-27 19:38:52 +000010586 case OMPD_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +000010587 case OMPD_teams_distribute_parallel_for:
10588 case OMPD_teams_distribute_parallel_for_simd:
10589 case OMPD_target_parallel_for:
10590 case OMPD_target_parallel_for_simd:
10591 case OMPD_target_teams_distribute_parallel_for:
10592 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +000010593 CaptureRegion = OMPD_parallel;
10594 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +000010595 case OMPD_for:
10596 case OMPD_for_simd:
10597 // Do not capture schedule-clause expressions.
Alexey Bataevfb0ebec2017-11-08 20:16:14 +000010598 break;
10599 case OMPD_task:
10600 case OMPD_taskloop:
10601 case OMPD_taskloop_simd:
Alexey Bataev60e51c42019-10-10 20:13:02 +000010602 case OMPD_master_taskloop:
Alexey Bataev5bbcead2019-10-14 17:17:41 +000010603 case OMPD_parallel_master_taskloop:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +000010604 case OMPD_target_data:
10605 case OMPD_target_enter_data:
10606 case OMPD_target_exit_data:
10607 case OMPD_target_update:
10608 case OMPD_teams:
10609 case OMPD_teams_distribute:
10610 case OMPD_teams_distribute_simd:
10611 case OMPD_target_teams_distribute:
10612 case OMPD_target_teams_distribute_simd:
10613 case OMPD_target:
10614 case OMPD_target_simd:
10615 case OMPD_target_parallel:
10616 case OMPD_cancel:
10617 case OMPD_parallel:
10618 case OMPD_parallel_sections:
10619 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000010620 case OMPD_allocate:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +000010621 case OMPD_taskyield:
10622 case OMPD_barrier:
10623 case OMPD_taskwait:
10624 case OMPD_cancellation_point:
10625 case OMPD_flush:
10626 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +000010627 case OMPD_declare_mapper:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +000010628 case OMPD_declare_simd:
Alexey Bataevd158cf62019-09-13 20:18:17 +000010629 case OMPD_declare_variant:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +000010630 case OMPD_declare_target:
10631 case OMPD_end_declare_target:
10632 case OMPD_simd:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +000010633 case OMPD_sections:
10634 case OMPD_section:
10635 case OMPD_single:
10636 case OMPD_master:
10637 case OMPD_critical:
10638 case OMPD_taskgroup:
10639 case OMPD_distribute:
10640 case OMPD_ordered:
10641 case OMPD_atomic:
10642 case OMPD_distribute_simd:
10643 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +000010644 case OMPD_requires:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +000010645 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
10646 case OMPD_unknown:
10647 llvm_unreachable("Unknown OpenMP directive");
10648 }
10649 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010650 case OMPC_dist_schedule:
Carlo Bertolli62fae152017-11-20 20:46:39 +000010651 switch (DKind) {
10652 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +000010653 case OMPD_teams_distribute_parallel_for_simd:
10654 case OMPD_teams_distribute:
10655 case OMPD_teams_distribute_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +000010656 case OMPD_target_teams_distribute_parallel_for:
10657 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +000010658 case OMPD_target_teams_distribute:
10659 case OMPD_target_teams_distribute_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +000010660 CaptureRegion = OMPD_teams;
Alexey Bataev2ba67042017-11-28 21:11:44 +000010661 break;
10662 case OMPD_distribute_parallel_for:
10663 case OMPD_distribute_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +000010664 case OMPD_distribute:
Carlo Bertolli62fae152017-11-20 20:46:39 +000010665 case OMPD_distribute_simd:
10666 // Do not capture thread_limit-clause expressions.
10667 break;
10668 case OMPD_parallel_for:
10669 case OMPD_parallel_for_simd:
10670 case OMPD_target_parallel_for_simd:
10671 case OMPD_target_parallel_for:
10672 case OMPD_task:
10673 case OMPD_taskloop:
10674 case OMPD_taskloop_simd:
Alexey Bataev60e51c42019-10-10 20:13:02 +000010675 case OMPD_master_taskloop:
Alexey Bataev5bbcead2019-10-14 17:17:41 +000010676 case OMPD_parallel_master_taskloop:
Carlo Bertolli62fae152017-11-20 20:46:39 +000010677 case OMPD_target_data:
10678 case OMPD_target_enter_data:
10679 case OMPD_target_exit_data:
10680 case OMPD_target_update:
10681 case OMPD_teams:
10682 case OMPD_target:
10683 case OMPD_target_simd:
10684 case OMPD_target_parallel:
10685 case OMPD_cancel:
10686 case OMPD_parallel:
10687 case OMPD_parallel_sections:
10688 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000010689 case OMPD_allocate:
Carlo Bertolli62fae152017-11-20 20:46:39 +000010690 case OMPD_taskyield:
10691 case OMPD_barrier:
10692 case OMPD_taskwait:
10693 case OMPD_cancellation_point:
10694 case OMPD_flush:
10695 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +000010696 case OMPD_declare_mapper:
Carlo Bertolli62fae152017-11-20 20:46:39 +000010697 case OMPD_declare_simd:
Alexey Bataevd158cf62019-09-13 20:18:17 +000010698 case OMPD_declare_variant:
Carlo Bertolli62fae152017-11-20 20:46:39 +000010699 case OMPD_declare_target:
10700 case OMPD_end_declare_target:
10701 case OMPD_simd:
10702 case OMPD_for:
10703 case OMPD_for_simd:
10704 case OMPD_sections:
10705 case OMPD_section:
10706 case OMPD_single:
10707 case OMPD_master:
10708 case OMPD_critical:
10709 case OMPD_taskgroup:
Carlo Bertolli62fae152017-11-20 20:46:39 +000010710 case OMPD_ordered:
10711 case OMPD_atomic:
10712 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +000010713 case OMPD_requires:
Carlo Bertolli62fae152017-11-20 20:46:39 +000010714 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
10715 case OMPD_unknown:
10716 llvm_unreachable("Unknown OpenMP directive");
10717 }
10718 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +000010719 case OMPC_device:
10720 switch (DKind) {
Alexey Bataevd2202ca2017-12-27 17:58:32 +000010721 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +000010722 case OMPD_target_enter_data:
10723 case OMPD_target_exit_data:
Alexey Bataev8451efa2018-01-15 19:06:12 +000010724 case OMPD_target:
Alexey Bataevf41c88f2018-01-16 15:05:16 +000010725 case OMPD_target_simd:
Alexey Bataev0c869ef2018-01-16 15:57:07 +000010726 case OMPD_target_teams:
Alexey Bataev54d5c7d2018-01-16 16:27:49 +000010727 case OMPD_target_parallel:
Alexey Bataev79df7562018-01-16 16:46:46 +000010728 case OMPD_target_teams_distribute:
Alexey Bataev8d16a432018-01-16 17:22:50 +000010729 case OMPD_target_teams_distribute_simd:
Alexey Bataev8ed895512018-01-16 17:41:04 +000010730 case OMPD_target_parallel_for:
Alexey Bataevd60d1ba2018-01-16 17:55:15 +000010731 case OMPD_target_parallel_for_simd:
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +000010732 case OMPD_target_teams_distribute_parallel_for:
Alexey Bataev9350fc32018-01-16 19:18:24 +000010733 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataevd2202ca2017-12-27 17:58:32 +000010734 CaptureRegion = OMPD_task;
10735 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +000010736 case OMPD_target_data:
Alexey Bataev2ba67042017-11-28 21:11:44 +000010737 // Do not capture device-clause expressions.
10738 break;
10739 case OMPD_teams_distribute_parallel_for:
10740 case OMPD_teams_distribute_parallel_for_simd:
10741 case OMPD_teams:
10742 case OMPD_teams_distribute:
10743 case OMPD_teams_distribute_simd:
10744 case OMPD_distribute_parallel_for:
10745 case OMPD_distribute_parallel_for_simd:
10746 case OMPD_task:
10747 case OMPD_taskloop:
10748 case OMPD_taskloop_simd:
Alexey Bataev60e51c42019-10-10 20:13:02 +000010749 case OMPD_master_taskloop:
Alexey Bataev5bbcead2019-10-14 17:17:41 +000010750 case OMPD_parallel_master_taskloop:
Alexey Bataev2ba67042017-11-28 21:11:44 +000010751 case OMPD_cancel:
10752 case OMPD_parallel:
10753 case OMPD_parallel_sections:
10754 case OMPD_parallel_for:
10755 case OMPD_parallel_for_simd:
10756 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000010757 case OMPD_allocate:
Alexey Bataev2ba67042017-11-28 21:11:44 +000010758 case OMPD_taskyield:
10759 case OMPD_barrier:
10760 case OMPD_taskwait:
10761 case OMPD_cancellation_point:
10762 case OMPD_flush:
10763 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +000010764 case OMPD_declare_mapper:
Alexey Bataev2ba67042017-11-28 21:11:44 +000010765 case OMPD_declare_simd:
Alexey Bataevd158cf62019-09-13 20:18:17 +000010766 case OMPD_declare_variant:
Alexey Bataev2ba67042017-11-28 21:11:44 +000010767 case OMPD_declare_target:
10768 case OMPD_end_declare_target:
10769 case OMPD_simd:
10770 case OMPD_for:
10771 case OMPD_for_simd:
10772 case OMPD_sections:
10773 case OMPD_section:
10774 case OMPD_single:
10775 case OMPD_master:
10776 case OMPD_critical:
10777 case OMPD_taskgroup:
10778 case OMPD_distribute:
10779 case OMPD_ordered:
10780 case OMPD_atomic:
10781 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +000010782 case OMPD_requires:
Alexey Bataev2ba67042017-11-28 21:11:44 +000010783 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
10784 case OMPD_unknown:
10785 llvm_unreachable("Unknown OpenMP directive");
10786 }
10787 break;
Alexey Bataevb9c55e22019-10-14 19:29:52 +000010788 case OMPC_grainsize:
Alexey Bataevd88c7de2019-10-14 20:44:34 +000010789 case OMPC_num_tasks:
Alexey Bataev3a842ec2019-10-15 19:37:05 +000010790 case OMPC_final:
Alexey Bataevb9c55e22019-10-14 19:29:52 +000010791 switch (DKind) {
10792 case OMPD_task:
10793 case OMPD_taskloop:
10794 case OMPD_taskloop_simd:
10795 case OMPD_master_taskloop:
10796 break;
10797 case OMPD_parallel_master_taskloop:
10798 CaptureRegion = OMPD_parallel;
10799 break;
10800 case OMPD_target_update:
10801 case OMPD_target_enter_data:
10802 case OMPD_target_exit_data:
10803 case OMPD_target:
10804 case OMPD_target_simd:
10805 case OMPD_target_teams:
10806 case OMPD_target_parallel:
10807 case OMPD_target_teams_distribute:
10808 case OMPD_target_teams_distribute_simd:
10809 case OMPD_target_parallel_for:
10810 case OMPD_target_parallel_for_simd:
10811 case OMPD_target_teams_distribute_parallel_for:
10812 case OMPD_target_teams_distribute_parallel_for_simd:
10813 case OMPD_target_data:
10814 case OMPD_teams_distribute_parallel_for:
10815 case OMPD_teams_distribute_parallel_for_simd:
10816 case OMPD_teams:
10817 case OMPD_teams_distribute:
10818 case OMPD_teams_distribute_simd:
10819 case OMPD_distribute_parallel_for:
10820 case OMPD_distribute_parallel_for_simd:
10821 case OMPD_cancel:
10822 case OMPD_parallel:
10823 case OMPD_parallel_sections:
10824 case OMPD_parallel_for:
10825 case OMPD_parallel_for_simd:
10826 case OMPD_threadprivate:
10827 case OMPD_allocate:
10828 case OMPD_taskyield:
10829 case OMPD_barrier:
10830 case OMPD_taskwait:
10831 case OMPD_cancellation_point:
10832 case OMPD_flush:
10833 case OMPD_declare_reduction:
10834 case OMPD_declare_mapper:
10835 case OMPD_declare_simd:
10836 case OMPD_declare_variant:
10837 case OMPD_declare_target:
10838 case OMPD_end_declare_target:
10839 case OMPD_simd:
10840 case OMPD_for:
10841 case OMPD_for_simd:
10842 case OMPD_sections:
10843 case OMPD_section:
10844 case OMPD_single:
10845 case OMPD_master:
10846 case OMPD_critical:
10847 case OMPD_taskgroup:
10848 case OMPD_distribute:
10849 case OMPD_ordered:
10850 case OMPD_atomic:
10851 case OMPD_distribute_simd:
10852 case OMPD_requires:
10853 llvm_unreachable("Unexpected OpenMP directive with grainsize-clause");
10854 case OMPD_unknown:
10855 llvm_unreachable("Unknown OpenMP directive");
10856 }
10857 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010858 case OMPC_firstprivate:
10859 case OMPC_lastprivate:
10860 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +000010861 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +000010862 case OMPC_in_reduction:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010863 case OMPC_linear:
10864 case OMPC_default:
10865 case OMPC_proc_bind:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010866 case OMPC_safelen:
10867 case OMPC_simdlen:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000010868 case OMPC_allocator:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010869 case OMPC_collapse:
10870 case OMPC_private:
10871 case OMPC_shared:
10872 case OMPC_aligned:
10873 case OMPC_copyin:
10874 case OMPC_copyprivate:
10875 case OMPC_ordered:
10876 case OMPC_nowait:
10877 case OMPC_untied:
10878 case OMPC_mergeable:
10879 case OMPC_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000010880 case OMPC_allocate:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010881 case OMPC_flush:
10882 case OMPC_read:
10883 case OMPC_write:
10884 case OMPC_update:
10885 case OMPC_capture:
10886 case OMPC_seq_cst:
10887 case OMPC_depend:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010888 case OMPC_threads:
10889 case OMPC_simd:
10890 case OMPC_map:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010891 case OMPC_priority:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010892 case OMPC_nogroup:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010893 case OMPC_hint:
10894 case OMPC_defaultmap:
10895 case OMPC_unknown:
10896 case OMPC_uniform:
10897 case OMPC_to:
10898 case OMPC_from:
10899 case OMPC_use_device_ptr:
10900 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +000010901 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +000010902 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +000010903 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +000010904 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +000010905 case OMPC_atomic_default_mem_order:
Alexey Bataev729e2422019-08-23 16:11:14 +000010906 case OMPC_device_type:
Alexey Bataevdba792c2019-09-23 18:13:31 +000010907 case OMPC_match:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010908 llvm_unreachable("Unexpected OpenMP clause.");
10909 }
10910 return CaptureRegion;
10911}
10912
Alexey Bataev6b8046a2015-09-03 07:23:48 +000010913OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
10914 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +000010915 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +000010916 SourceLocation NameModifierLoc,
10917 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +000010918 SourceLocation EndLoc) {
10919 Expr *ValExpr = Condition;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010920 Stmt *HelperValStmt = nullptr;
10921 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Alexey Bataevaadd52e2014-02-13 05:29:23 +000010922 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
10923 !Condition->isInstantiationDependent() &&
10924 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +000010925 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +000010926 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000010927 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +000010928
Alexey Bataev8e769ee2017-12-22 21:01:52 +000010929 ValExpr = Val.get();
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010930
10931 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
10932 CaptureRegion =
10933 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
Alexey Bataev2ba67042017-11-28 21:11:44 +000010934 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000010935 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000010936 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010937 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
10938 HelperValStmt = buildPreInits(Context, Captures);
10939 }
Alexey Bataevaadd52e2014-02-13 05:29:23 +000010940 }
10941
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010942 return new (Context)
10943 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
10944 LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +000010945}
10946
Alexey Bataev3778b602014-07-17 07:32:53 +000010947OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
10948 SourceLocation StartLoc,
10949 SourceLocation LParenLoc,
10950 SourceLocation EndLoc) {
10951 Expr *ValExpr = Condition;
Alexey Bataev3a842ec2019-10-15 19:37:05 +000010952 Stmt *HelperValStmt = nullptr;
10953 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Alexey Bataev3778b602014-07-17 07:32:53 +000010954 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
10955 !Condition->isInstantiationDependent() &&
10956 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +000010957 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +000010958 if (Val.isInvalid())
10959 return nullptr;
10960
Richard Smith03a4aa32016-06-23 19:02:52 +000010961 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3a842ec2019-10-15 19:37:05 +000010962
10963 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
10964 CaptureRegion = getOpenMPCaptureRegionForClause(DKind, OMPC_final);
10965 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
10966 ValExpr = MakeFullExpr(ValExpr).get();
10967 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
10968 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
10969 HelperValStmt = buildPreInits(Context, Captures);
10970 }
Alexey Bataev3778b602014-07-17 07:32:53 +000010971 }
10972
Alexey Bataev3a842ec2019-10-15 19:37:05 +000010973 return new (Context) OMPFinalClause(ValExpr, HelperValStmt, CaptureRegion,
10974 StartLoc, LParenLoc, EndLoc);
Alexey Bataev3778b602014-07-17 07:32:53 +000010975}
Alexey Bataev3a842ec2019-10-15 19:37:05 +000010976
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000010977ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
10978 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +000010979 if (!Op)
10980 return ExprError();
10981
10982 class IntConvertDiagnoser : public ICEConvertDiagnoser {
10983 public:
10984 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +000010985 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +000010986 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
10987 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +000010988 return S.Diag(Loc, diag::err_omp_not_integral) << T;
10989 }
Alexey Bataeved09d242014-05-28 05:53:51 +000010990 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
10991 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +000010992 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
10993 }
Alexey Bataeved09d242014-05-28 05:53:51 +000010994 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
10995 QualType T,
10996 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +000010997 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
10998 }
Alexey Bataeved09d242014-05-28 05:53:51 +000010999 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
11000 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +000011001 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +000011002 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +000011003 }
Alexey Bataeved09d242014-05-28 05:53:51 +000011004 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
11005 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +000011006 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
11007 }
Alexey Bataeved09d242014-05-28 05:53:51 +000011008 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
11009 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +000011010 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +000011011 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +000011012 }
Alexey Bataeved09d242014-05-28 05:53:51 +000011013 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
11014 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +000011015 llvm_unreachable("conversion functions are permitted");
11016 }
11017 } ConvertDiagnoser;
11018 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
11019}
11020
Alexey Bataevb9c55e22019-10-14 19:29:52 +000011021static bool
11022isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, OpenMPClauseKind CKind,
11023 bool StrictlyPositive, bool BuildCapture = false,
11024 OpenMPDirectiveKind DKind = OMPD_unknown,
11025 OpenMPDirectiveKind *CaptureRegion = nullptr,
11026 Stmt **HelperValStmt = nullptr) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011027 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
11028 !ValExpr->isInstantiationDependent()) {
11029 SourceLocation Loc = ValExpr->getExprLoc();
11030 ExprResult Value =
11031 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
11032 if (Value.isInvalid())
11033 return false;
11034
11035 ValExpr = Value.get();
11036 // The expression must evaluate to a non-negative integer value.
11037 llvm::APSInt Result;
11038 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +000011039 Result.isSigned() &&
11040 !((!StrictlyPositive && Result.isNonNegative()) ||
11041 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011042 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +000011043 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
11044 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011045 return false;
11046 }
Alexey Bataevb9c55e22019-10-14 19:29:52 +000011047 if (!BuildCapture)
11048 return true;
11049 *CaptureRegion = getOpenMPCaptureRegionForClause(DKind, CKind);
11050 if (*CaptureRegion != OMPD_unknown &&
11051 !SemaRef.CurContext->isDependentContext()) {
11052 ValExpr = SemaRef.MakeFullExpr(ValExpr).get();
11053 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
11054 ValExpr = tryBuildCapture(SemaRef, ValExpr, Captures).get();
11055 *HelperValStmt = buildPreInits(SemaRef.Context, Captures);
11056 }
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011057 }
11058 return true;
11059}
11060
Alexey Bataev568a8332014-03-06 06:15:19 +000011061OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
11062 SourceLocation StartLoc,
11063 SourceLocation LParenLoc,
11064 SourceLocation EndLoc) {
11065 Expr *ValExpr = NumThreads;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +000011066 Stmt *HelperValStmt = nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +000011067
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011068 // OpenMP [2.5, Restrictions]
11069 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000011070 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
Alexey Bataeva0569352015-12-01 10:17:31 +000011071 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011072 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +000011073
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +000011074 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000011075 OpenMPDirectiveKind CaptureRegion =
11076 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
11077 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000011078 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000011079 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +000011080 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11081 HelperValStmt = buildPreInits(Context, Captures);
11082 }
11083
11084 return new (Context) OMPNumThreadsClause(
11085 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +000011086}
11087
Alexey Bataev62c87d22014-03-21 04:51:18 +000011088ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011089 OpenMPClauseKind CKind,
11090 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +000011091 if (!E)
11092 return ExprError();
11093 if (E->isValueDependent() || E->isTypeDependent() ||
11094 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +000011095 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +000011096 llvm::APSInt Result;
11097 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
11098 if (ICE.isInvalid())
11099 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011100 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
11101 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +000011102 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011103 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
11104 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +000011105 return ExprError();
11106 }
Alexander Musman09184fe2014-09-30 05:29:28 +000011107 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
11108 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
11109 << E->getSourceRange();
11110 return ExprError();
11111 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011112 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
11113 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +000011114 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011115 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +000011116 return ICE;
11117}
11118
11119OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
11120 SourceLocation LParenLoc,
11121 SourceLocation EndLoc) {
11122 // OpenMP [2.8.1, simd construct, Description]
11123 // The parameter of the safelen clause must be a constant
11124 // positive integer expression.
11125 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
11126 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011127 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +000011128 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +000011129 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +000011130}
11131
Alexey Bataev66b15b52015-08-21 11:14:16 +000011132OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
11133 SourceLocation LParenLoc,
11134 SourceLocation EndLoc) {
11135 // OpenMP [2.8.1, simd construct, Description]
11136 // The parameter of the simdlen clause must be a constant
11137 // positive integer expression.
11138 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
11139 if (Simdlen.isInvalid())
11140 return nullptr;
11141 return new (Context)
11142 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
11143}
11144
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000011145/// Tries to find omp_allocator_handle_t type.
Alexey Bataev27ef9512019-03-20 20:14:22 +000011146static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc,
11147 DSAStackTy *Stack) {
11148 QualType OMPAllocatorHandleT = Stack->getOMPAllocatorHandleT();
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000011149 if (!OMPAllocatorHandleT.isNull())
11150 return true;
Alexey Bataev27ef9512019-03-20 20:14:22 +000011151 // Build the predefined allocator expressions.
11152 bool ErrorFound = false;
11153 for (int I = OMPAllocateDeclAttr::OMPDefaultMemAlloc;
11154 I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
11155 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
11156 StringRef Allocator =
11157 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind);
11158 DeclarationName AllocatorName = &S.getASTContext().Idents.get(Allocator);
11159 auto *VD = dyn_cast_or_null<ValueDecl>(
11160 S.LookupSingleName(S.TUScope, AllocatorName, Loc, Sema::LookupAnyName));
11161 if (!VD) {
11162 ErrorFound = true;
11163 break;
11164 }
11165 QualType AllocatorType =
11166 VD->getType().getNonLValueExprType(S.getASTContext());
11167 ExprResult Res = S.BuildDeclRefExpr(VD, AllocatorType, VK_LValue, Loc);
11168 if (!Res.isUsable()) {
11169 ErrorFound = true;
11170 break;
11171 }
11172 if (OMPAllocatorHandleT.isNull())
11173 OMPAllocatorHandleT = AllocatorType;
11174 if (!S.getASTContext().hasSameType(OMPAllocatorHandleT, AllocatorType)) {
11175 ErrorFound = true;
11176 break;
11177 }
11178 Stack->setAllocator(AllocatorKind, Res.get());
11179 }
11180 if (ErrorFound) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000011181 S.Diag(Loc, diag::err_implied_omp_allocator_handle_t_not_found);
11182 return false;
11183 }
Alexey Bataev27ef9512019-03-20 20:14:22 +000011184 OMPAllocatorHandleT.addConst();
11185 Stack->setOMPAllocatorHandleT(OMPAllocatorHandleT);
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000011186 return true;
11187}
11188
11189OMPClause *Sema::ActOnOpenMPAllocatorClause(Expr *A, SourceLocation StartLoc,
11190 SourceLocation LParenLoc,
11191 SourceLocation EndLoc) {
11192 // OpenMP [2.11.3, allocate Directive, Description]
11193 // allocator is an expression of omp_allocator_handle_t type.
Alexey Bataev27ef9512019-03-20 20:14:22 +000011194 if (!findOMPAllocatorHandleT(*this, A->getExprLoc(), DSAStack))
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000011195 return nullptr;
11196
11197 ExprResult Allocator = DefaultLvalueConversion(A);
11198 if (Allocator.isInvalid())
11199 return nullptr;
Alexey Bataev27ef9512019-03-20 20:14:22 +000011200 Allocator = PerformImplicitConversion(Allocator.get(),
11201 DSAStack->getOMPAllocatorHandleT(),
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000011202 Sema::AA_Initializing,
11203 /*AllowExplicit=*/true);
11204 if (Allocator.isInvalid())
11205 return nullptr;
11206 return new (Context)
11207 OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc);
11208}
11209
Alexander Musman64d33f12014-06-04 07:53:32 +000011210OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
11211 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +000011212 SourceLocation LParenLoc,
11213 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +000011214 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +000011215 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +000011216 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +000011217 // The parameter of the collapse clause must be a constant
11218 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +000011219 ExprResult NumForLoopsResult =
11220 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
11221 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +000011222 return nullptr;
11223 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +000011224 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +000011225}
11226
Alexey Bataev10e775f2015-07-30 11:36:16 +000011227OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
11228 SourceLocation EndLoc,
11229 SourceLocation LParenLoc,
11230 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +000011231 // OpenMP [2.7.1, loop construct, Description]
11232 // OpenMP [2.8.1, simd construct, Description]
11233 // OpenMP [2.9.6, distribute construct, Description]
11234 // The parameter of the ordered clause must be a constant
11235 // positive integer expression if any.
11236 if (NumForLoops && LParenLoc.isValid()) {
11237 ExprResult NumForLoopsResult =
11238 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
11239 if (NumForLoopsResult.isInvalid())
11240 return nullptr;
11241 NumForLoops = NumForLoopsResult.get();
Alexey Bataeve3727102018-04-18 15:57:46 +000011242 } else {
Alexey Bataev346265e2015-09-25 10:37:12 +000011243 NumForLoops = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000011244 }
Alexey Bataevf138fda2018-08-13 19:04:24 +000011245 auto *Clause = OMPOrderedClause::Create(
11246 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0,
11247 StartLoc, LParenLoc, EndLoc);
11248 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause);
11249 return Clause;
Alexey Bataev10e775f2015-07-30 11:36:16 +000011250}
11251
Alexey Bataeved09d242014-05-28 05:53:51 +000011252OMPClause *Sema::ActOnOpenMPSimpleClause(
11253 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
11254 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011255 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011256 switch (Kind) {
11257 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +000011258 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +000011259 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
11260 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011261 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +000011262 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +000011263 Res = ActOnOpenMPProcBindClause(
11264 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
11265 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +000011266 break;
Patrick Lyster7a2a27c2018-11-02 12:18:11 +000011267 case OMPC_atomic_default_mem_order:
11268 Res = ActOnOpenMPAtomicDefaultMemOrderClause(
11269 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument),
11270 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
11271 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +000011272 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +000011273 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +000011274 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +000011275 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +000011276 case OMPC_simdlen:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000011277 case OMPC_allocator:
Alexander Musman8bd31e62014-05-27 15:12:19 +000011278 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +000011279 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011280 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +000011281 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +000011282 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +000011283 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +000011284 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +000011285 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +000011286 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +000011287 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011288 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011289 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +000011290 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +000011291 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +000011292 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +000011293 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +000011294 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011295 case OMPC_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000011296 case OMPC_allocate:
Alexey Bataev6125da92014-07-21 11:26:11 +000011297 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +000011298 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +000011299 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +000011300 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +000011301 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +000011302 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011303 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +000011304 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +000011305 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +000011306 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +000011307 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +000011308 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011309 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +000011310 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000011311 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +000011312 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +000011313 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +000011314 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +000011315 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011316 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011317 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +000011318 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +000011319 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +000011320 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +000011321 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +000011322 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +000011323 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +000011324 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +000011325 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +000011326 case OMPC_dynamic_allocators:
Alexey Bataev729e2422019-08-23 16:11:14 +000011327 case OMPC_device_type:
Alexey Bataevdba792c2019-09-23 18:13:31 +000011328 case OMPC_match:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011329 llvm_unreachable("Clause is not allowed.");
11330 }
11331 return Res;
11332}
11333
Alexey Bataev6402bca2015-12-28 07:25:51 +000011334static std::string
11335getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
11336 ArrayRef<unsigned> Exclude = llvm::None) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011337 SmallString<256> Buffer;
11338 llvm::raw_svector_ostream Out(Buffer);
Alexey Bataev6402bca2015-12-28 07:25:51 +000011339 unsigned Bound = Last >= 2 ? Last - 2 : 0;
11340 unsigned Skipped = Exclude.size();
11341 auto S = Exclude.begin(), E = Exclude.end();
Alexey Bataeve3727102018-04-18 15:57:46 +000011342 for (unsigned I = First; I < Last; ++I) {
11343 if (std::find(S, E, I) != E) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000011344 --Skipped;
11345 continue;
11346 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011347 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'";
11348 if (I == Bound - Skipped)
11349 Out << " or ";
11350 else if (I != Bound + 1 - Skipped)
11351 Out << ", ";
Alexey Bataev6402bca2015-12-28 07:25:51 +000011352 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011353 return Out.str();
Alexey Bataev6402bca2015-12-28 07:25:51 +000011354}
11355
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011356OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
11357 SourceLocation KindKwLoc,
11358 SourceLocation StartLoc,
11359 SourceLocation LParenLoc,
11360 SourceLocation EndLoc) {
11361 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +000011362 static_assert(OMPC_DEFAULT_unknown > 0,
11363 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011364 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000011365 << getListOfPossibleValues(OMPC_default, /*First=*/0,
11366 /*Last=*/OMPC_DEFAULT_unknown)
11367 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011368 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011369 }
Alexey Bataev758e55e2013-09-06 18:03:48 +000011370 switch (Kind) {
11371 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +000011372 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +000011373 break;
11374 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +000011375 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +000011376 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +000011377 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +000011378 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +000011379 break;
11380 }
Alexey Bataeved09d242014-05-28 05:53:51 +000011381 return new (Context)
11382 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011383}
11384
Alexey Bataevbcbadb62014-05-06 06:04:14 +000011385OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
11386 SourceLocation KindKwLoc,
11387 SourceLocation StartLoc,
11388 SourceLocation LParenLoc,
11389 SourceLocation EndLoc) {
11390 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +000011391 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000011392 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
11393 /*Last=*/OMPC_PROC_BIND_unknown)
11394 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011395 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +000011396 }
Alexey Bataeved09d242014-05-28 05:53:51 +000011397 return new (Context)
11398 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +000011399}
11400
Patrick Lyster7a2a27c2018-11-02 12:18:11 +000011401OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause(
11402 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc,
11403 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
11404 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) {
11405 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
11406 << getListOfPossibleValues(
11407 OMPC_atomic_default_mem_order, /*First=*/0,
11408 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown)
11409 << getOpenMPClauseName(OMPC_atomic_default_mem_order);
11410 return nullptr;
11411 }
11412 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc,
11413 LParenLoc, EndLoc);
11414}
11415
Alexey Bataev56dafe82014-06-20 07:16:17 +000011416OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +000011417 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +000011418 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +000011419 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +000011420 SourceLocation EndLoc) {
11421 OMPClause *Res = nullptr;
11422 switch (Kind) {
11423 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +000011424 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
11425 assert(Argument.size() == NumberOfElements &&
11426 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +000011427 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +000011428 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
11429 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
11430 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
11431 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
11432 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +000011433 break;
11434 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +000011435 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
11436 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
11437 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
11438 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011439 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +000011440 case OMPC_dist_schedule:
11441 Res = ActOnOpenMPDistScheduleClause(
11442 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
11443 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
11444 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011445 case OMPC_defaultmap:
11446 enum { Modifier, DefaultmapKind };
11447 Res = ActOnOpenMPDefaultmapClause(
11448 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
11449 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
David Majnemer9d168222016-08-05 17:44:54 +000011450 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
11451 EndLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011452 break;
Alexey Bataev3778b602014-07-17 07:32:53 +000011453 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +000011454 case OMPC_num_threads:
11455 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +000011456 case OMPC_simdlen:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000011457 case OMPC_allocator:
Alexey Bataev56dafe82014-06-20 07:16:17 +000011458 case OMPC_collapse:
11459 case OMPC_default:
11460 case OMPC_proc_bind:
11461 case OMPC_private:
11462 case OMPC_firstprivate:
11463 case OMPC_lastprivate:
11464 case OMPC_shared:
11465 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +000011466 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +000011467 case OMPC_in_reduction:
Alexey Bataev56dafe82014-06-20 07:16:17 +000011468 case OMPC_linear:
11469 case OMPC_aligned:
11470 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +000011471 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +000011472 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +000011473 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +000011474 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +000011475 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +000011476 case OMPC_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000011477 case OMPC_allocate:
Alexey Bataev6125da92014-07-21 11:26:11 +000011478 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +000011479 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +000011480 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +000011481 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +000011482 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +000011483 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011484 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +000011485 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +000011486 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +000011487 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +000011488 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +000011489 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011490 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +000011491 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000011492 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +000011493 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +000011494 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +000011495 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +000011496 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +000011497 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +000011498 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +000011499 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +000011500 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +000011501 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +000011502 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +000011503 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +000011504 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +000011505 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +000011506 case OMPC_atomic_default_mem_order:
Alexey Bataev729e2422019-08-23 16:11:14 +000011507 case OMPC_device_type:
Alexey Bataevdba792c2019-09-23 18:13:31 +000011508 case OMPC_match:
Alexey Bataev56dafe82014-06-20 07:16:17 +000011509 llvm_unreachable("Clause is not allowed.");
11510 }
11511 return Res;
11512}
11513
Alexey Bataev6402bca2015-12-28 07:25:51 +000011514static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
11515 OpenMPScheduleClauseModifier M2,
11516 SourceLocation M1Loc, SourceLocation M2Loc) {
11517 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
11518 SmallVector<unsigned, 2> Excluded;
11519 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
11520 Excluded.push_back(M2);
11521 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
11522 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
11523 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
11524 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
11525 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
11526 << getListOfPossibleValues(OMPC_schedule,
11527 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
11528 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
11529 Excluded)
11530 << getOpenMPClauseName(OMPC_schedule);
11531 return true;
11532 }
11533 return false;
11534}
11535
Alexey Bataev56dafe82014-06-20 07:16:17 +000011536OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +000011537 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +000011538 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +000011539 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
11540 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
11541 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
11542 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
11543 return nullptr;
11544 // OpenMP, 2.7.1, Loop Construct, Restrictions
11545 // Either the monotonic modifier or the nonmonotonic modifier can be specified
11546 // but not both.
11547 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
11548 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
11549 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
11550 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
11551 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
11552 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
11553 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
11554 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
11555 return nullptr;
11556 }
Alexey Bataev56dafe82014-06-20 07:16:17 +000011557 if (Kind == OMPC_SCHEDULE_unknown) {
11558 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +000011559 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
11560 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
11561 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
11562 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
11563 Exclude);
11564 } else {
11565 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
11566 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +000011567 }
11568 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
11569 << Values << getOpenMPClauseName(OMPC_schedule);
11570 return nullptr;
11571 }
Alexey Bataev6402bca2015-12-28 07:25:51 +000011572 // OpenMP, 2.7.1, Loop Construct, Restrictions
11573 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
11574 // schedule(guided).
11575 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
11576 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
11577 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
11578 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
11579 diag::err_omp_schedule_nonmonotonic_static);
11580 return nullptr;
11581 }
Alexey Bataev56dafe82014-06-20 07:16:17 +000011582 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000011583 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +000011584 if (ChunkSize) {
11585 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
11586 !ChunkSize->isInstantiationDependent() &&
11587 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011588 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Alexey Bataev56dafe82014-06-20 07:16:17 +000011589 ExprResult Val =
11590 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
11591 if (Val.isInvalid())
11592 return nullptr;
11593
11594 ValExpr = Val.get();
11595
11596 // OpenMP [2.7.1, Restrictions]
11597 // chunk_size must be a loop invariant integer expression with a positive
11598 // value.
11599 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +000011600 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
11601 if (Result.isSigned() && !Result.isStrictlyPositive()) {
11602 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +000011603 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +000011604 return nullptr;
11605 }
Alexey Bataev2ba67042017-11-28 21:11:44 +000011606 } else if (getOpenMPCaptureRegionForClause(
11607 DSAStack->getCurrentDirective(), OMPC_schedule) !=
11608 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +000011609 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000011610 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000011611 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +000011612 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11613 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +000011614 }
11615 }
11616 }
11617
Alexey Bataev6402bca2015-12-28 07:25:51 +000011618 return new (Context)
11619 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +000011620 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +000011621}
11622
Alexey Bataev142e1fc2014-06-20 09:44:06 +000011623OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
11624 SourceLocation StartLoc,
11625 SourceLocation EndLoc) {
11626 OMPClause *Res = nullptr;
11627 switch (Kind) {
11628 case OMPC_ordered:
11629 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
11630 break;
Alexey Bataev236070f2014-06-20 11:19:47 +000011631 case OMPC_nowait:
11632 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
11633 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +000011634 case OMPC_untied:
11635 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
11636 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +000011637 case OMPC_mergeable:
11638 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
11639 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +000011640 case OMPC_read:
11641 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
11642 break;
Alexey Bataevdea47612014-07-23 07:46:59 +000011643 case OMPC_write:
11644 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
11645 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +000011646 case OMPC_update:
11647 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
11648 break;
Alexey Bataev459dec02014-07-24 06:46:57 +000011649 case OMPC_capture:
11650 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
11651 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +000011652 case OMPC_seq_cst:
11653 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
11654 break;
Alexey Bataev346265e2015-09-25 10:37:12 +000011655 case OMPC_threads:
11656 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
11657 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +000011658 case OMPC_simd:
11659 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
11660 break;
Alexey Bataevb825de12015-12-07 10:51:44 +000011661 case OMPC_nogroup:
11662 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
11663 break;
Kelvin Li1408f912018-09-26 04:28:39 +000011664 case OMPC_unified_address:
11665 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
11666 break;
Patrick Lyster4a370b92018-10-01 13:47:43 +000011667 case OMPC_unified_shared_memory:
11668 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
11669 break;
Patrick Lyster6bdf63b2018-10-03 20:07:58 +000011670 case OMPC_reverse_offload:
11671 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc);
11672 break;
Patrick Lyster3fe9e392018-10-11 14:41:10 +000011673 case OMPC_dynamic_allocators:
11674 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
11675 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +000011676 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +000011677 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +000011678 case OMPC_num_threads:
11679 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +000011680 case OMPC_simdlen:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000011681 case OMPC_allocator:
Alexey Bataev142e1fc2014-06-20 09:44:06 +000011682 case OMPC_collapse:
11683 case OMPC_schedule:
11684 case OMPC_private:
11685 case OMPC_firstprivate:
11686 case OMPC_lastprivate:
11687 case OMPC_shared:
11688 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +000011689 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +000011690 case OMPC_in_reduction:
Alexey Bataev142e1fc2014-06-20 09:44:06 +000011691 case OMPC_linear:
11692 case OMPC_aligned:
11693 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +000011694 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +000011695 case OMPC_default:
11696 case OMPC_proc_bind:
11697 case OMPC_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000011698 case OMPC_allocate:
Alexey Bataev6125da92014-07-21 11:26:11 +000011699 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011700 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +000011701 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +000011702 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +000011703 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011704 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +000011705 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000011706 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +000011707 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +000011708 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +000011709 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011710 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +000011711 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +000011712 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +000011713 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +000011714 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +000011715 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +000011716 case OMPC_is_device_ptr:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +000011717 case OMPC_atomic_default_mem_order:
Alexey Bataev729e2422019-08-23 16:11:14 +000011718 case OMPC_device_type:
Alexey Bataevdba792c2019-09-23 18:13:31 +000011719 case OMPC_match:
Alexey Bataev142e1fc2014-06-20 09:44:06 +000011720 llvm_unreachable("Clause is not allowed.");
11721 }
11722 return Res;
11723}
11724
Alexey Bataev236070f2014-06-20 11:19:47 +000011725OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
11726 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +000011727 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +000011728 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
11729}
11730
Alexey Bataev7aea99a2014-07-17 12:19:31 +000011731OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
11732 SourceLocation EndLoc) {
11733 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
11734}
11735
Alexey Bataev74ba3a52014-07-17 12:47:03 +000011736OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
11737 SourceLocation EndLoc) {
11738 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
11739}
11740
Alexey Bataevf98b00c2014-07-23 02:27:21 +000011741OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
11742 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +000011743 return new (Context) OMPReadClause(StartLoc, EndLoc);
11744}
11745
Alexey Bataevdea47612014-07-23 07:46:59 +000011746OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
11747 SourceLocation EndLoc) {
11748 return new (Context) OMPWriteClause(StartLoc, EndLoc);
11749}
11750
Alexey Bataev67a4f222014-07-23 10:25:33 +000011751OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
11752 SourceLocation EndLoc) {
11753 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
11754}
11755
Alexey Bataev459dec02014-07-24 06:46:57 +000011756OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
11757 SourceLocation EndLoc) {
11758 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
11759}
11760
Alexey Bataev82bad8b2014-07-24 08:55:34 +000011761OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
11762 SourceLocation EndLoc) {
11763 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
11764}
11765
Alexey Bataev346265e2015-09-25 10:37:12 +000011766OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
11767 SourceLocation EndLoc) {
11768 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
11769}
11770
Alexey Bataevd14d1e62015-09-28 06:39:35 +000011771OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
11772 SourceLocation EndLoc) {
11773 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
11774}
11775
Alexey Bataevb825de12015-12-07 10:51:44 +000011776OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
11777 SourceLocation EndLoc) {
11778 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
11779}
11780
Kelvin Li1408f912018-09-26 04:28:39 +000011781OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
11782 SourceLocation EndLoc) {
11783 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc);
11784}
11785
Patrick Lyster4a370b92018-10-01 13:47:43 +000011786OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
11787 SourceLocation EndLoc) {
11788 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
11789}
11790
Patrick Lyster6bdf63b2018-10-03 20:07:58 +000011791OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc,
11792 SourceLocation EndLoc) {
11793 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc);
11794}
11795
Patrick Lyster3fe9e392018-10-11 14:41:10 +000011796OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
11797 SourceLocation EndLoc) {
11798 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
11799}
11800
Alexey Bataevc5e02582014-06-16 07:08:35 +000011801OMPClause *Sema::ActOnOpenMPVarListClause(
11802 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
Michael Kruse4304e9d2019-02-19 16:38:20 +000011803 const OMPVarListLocTy &Locs, SourceLocation ColonLoc,
11804 CXXScopeSpec &ReductionOrMapperIdScopeSpec,
11805 DeclarationNameInfo &ReductionOrMapperId, OpenMPDependClauseKind DepKind,
Kelvin Lief579432018-12-18 22:18:41 +000011806 OpenMPLinearClauseKind LinKind,
11807 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
Michael Kruse4304e9d2019-02-19 16:38:20 +000011808 ArrayRef<SourceLocation> MapTypeModifiersLoc, OpenMPMapClauseKind MapType,
11809 bool IsMapTypeImplicit, SourceLocation DepLinMapLoc) {
11810 SourceLocation StartLoc = Locs.StartLoc;
11811 SourceLocation LParenLoc = Locs.LParenLoc;
11812 SourceLocation EndLoc = Locs.EndLoc;
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011813 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011814 switch (Kind) {
11815 case OMPC_private:
11816 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
11817 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +000011818 case OMPC_firstprivate:
11819 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
11820 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +000011821 case OMPC_lastprivate:
11822 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
11823 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +000011824 case OMPC_shared:
11825 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
11826 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011827 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +000011828 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
Michael Kruse4304e9d2019-02-19 16:38:20 +000011829 EndLoc, ReductionOrMapperIdScopeSpec,
11830 ReductionOrMapperId);
Alexey Bataevc5e02582014-06-16 07:08:35 +000011831 break;
Alexey Bataev169d96a2017-07-18 20:17:46 +000011832 case OMPC_task_reduction:
11833 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
Michael Kruse4304e9d2019-02-19 16:38:20 +000011834 EndLoc, ReductionOrMapperIdScopeSpec,
11835 ReductionOrMapperId);
Alexey Bataev169d96a2017-07-18 20:17:46 +000011836 break;
Alexey Bataevfa312f32017-07-21 18:48:21 +000011837 case OMPC_in_reduction:
Michael Kruse4304e9d2019-02-19 16:38:20 +000011838 Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
11839 EndLoc, ReductionOrMapperIdScopeSpec,
11840 ReductionOrMapperId);
Alexey Bataevfa312f32017-07-21 18:48:21 +000011841 break;
Alexander Musman8dba6642014-04-22 13:09:42 +000011842 case OMPC_linear:
11843 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +000011844 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +000011845 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011846 case OMPC_aligned:
11847 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
11848 ColonLoc, EndLoc);
11849 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011850 case OMPC_copyin:
11851 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
11852 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011853 case OMPC_copyprivate:
11854 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
11855 break;
Alexey Bataev6125da92014-07-21 11:26:11 +000011856 case OMPC_flush:
11857 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
11858 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011859 case OMPC_depend:
David Majnemer9d168222016-08-05 17:44:54 +000011860 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
Kelvin Li0bff7af2015-11-23 05:32:03 +000011861 StartLoc, LParenLoc, EndLoc);
11862 break;
11863 case OMPC_map:
Michael Kruse4304e9d2019-02-19 16:38:20 +000011864 Res = ActOnOpenMPMapClause(MapTypeModifiers, MapTypeModifiersLoc,
11865 ReductionOrMapperIdScopeSpec,
11866 ReductionOrMapperId, MapType, IsMapTypeImplicit,
11867 DepLinMapLoc, ColonLoc, VarList, Locs);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011868 break;
Samuel Antao661c0902016-05-26 17:39:58 +000011869 case OMPC_to:
Michael Kruse01f670d2019-02-22 22:29:42 +000011870 Res = ActOnOpenMPToClause(VarList, ReductionOrMapperIdScopeSpec,
11871 ReductionOrMapperId, Locs);
Samuel Antao661c0902016-05-26 17:39:58 +000011872 break;
Samuel Antaoec172c62016-05-26 17:49:04 +000011873 case OMPC_from:
Michael Kruse0336c752019-02-25 20:34:15 +000011874 Res = ActOnOpenMPFromClause(VarList, ReductionOrMapperIdScopeSpec,
11875 ReductionOrMapperId, Locs);
Samuel Antaoec172c62016-05-26 17:49:04 +000011876 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +000011877 case OMPC_use_device_ptr:
Michael Kruse4304e9d2019-02-19 16:38:20 +000011878 Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs);
Carlo Bertolli2404b172016-07-13 15:37:16 +000011879 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +000011880 case OMPC_is_device_ptr:
Michael Kruse4304e9d2019-02-19 16:38:20 +000011881 Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs);
Carlo Bertolli70594e92016-07-13 17:16:49 +000011882 break;
Alexey Bataeve04483e2019-03-27 14:14:31 +000011883 case OMPC_allocate:
11884 Res = ActOnOpenMPAllocateClause(TailExpr, VarList, StartLoc, LParenLoc,
11885 ColonLoc, EndLoc);
11886 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +000011887 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +000011888 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +000011889 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +000011890 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +000011891 case OMPC_simdlen:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000011892 case OMPC_allocator:
Alexander Musman8bd31e62014-05-27 15:12:19 +000011893 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011894 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +000011895 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +000011896 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +000011897 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +000011898 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +000011899 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +000011900 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011901 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +000011902 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +000011903 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +000011904 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +000011905 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +000011906 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +000011907 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +000011908 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +000011909 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +000011910 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011911 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +000011912 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000011913 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +000011914 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +000011915 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +000011916 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +000011917 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011918 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011919 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +000011920 case OMPC_uniform:
Kelvin Li1408f912018-09-26 04:28:39 +000011921 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +000011922 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +000011923 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +000011924 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +000011925 case OMPC_atomic_default_mem_order:
Alexey Bataev729e2422019-08-23 16:11:14 +000011926 case OMPC_device_type:
Alexey Bataevdba792c2019-09-23 18:13:31 +000011927 case OMPC_match:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011928 llvm_unreachable("Clause is not allowed.");
11929 }
11930 return Res;
11931}
11932
Alexey Bataev90c228f2016-02-08 09:29:13 +000011933ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +000011934 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +000011935 ExprResult Res = BuildDeclRefExpr(
11936 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
11937 if (!Res.isUsable())
11938 return ExprError();
11939 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
11940 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
11941 if (!Res.isUsable())
11942 return ExprError();
11943 }
11944 if (VK != VK_LValue && Res.get()->isGLValue()) {
11945 Res = DefaultLvalueConversion(Res.get());
11946 if (!Res.isUsable())
11947 return ExprError();
11948 }
11949 return Res;
11950}
11951
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011952OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
11953 SourceLocation StartLoc,
11954 SourceLocation LParenLoc,
11955 SourceLocation EndLoc) {
11956 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +000011957 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +000011958 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011959 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000011960 SourceLocation ELoc;
11961 SourceRange ERange;
11962 Expr *SimpleRefExpr = RefExpr;
11963 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +000011964 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011965 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011966 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +000011967 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011968 }
Alexey Bataevd985eda2016-02-10 11:29:16 +000011969 ValueDecl *D = Res.first;
11970 if (!D)
11971 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011972
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000011973 QualType Type = D->getType();
11974 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011975
11976 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
11977 // A variable that appears in a private clause must not have an incomplete
11978 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000011979 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011980 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011981 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011982
Joel E. Dennye6234d1422019-01-04 22:11:31 +000011983 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
11984 // A variable that is privatized must not have a const-qualified type
11985 // unless it is of class type with a mutable member. This restriction does
11986 // not apply to the firstprivate clause.
11987 //
11988 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions]
11989 // A variable that appears in a private clause must not have a
11990 // const-qualified type unless it is of class type with a mutable member.
Joel E. Dennyd2649292019-01-04 22:11:56 +000011991 if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc))
Joel E. Dennye6234d1422019-01-04 22:11:31 +000011992 continue;
11993
Alexey Bataev758e55e2013-09-06 18:03:48 +000011994 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
11995 // in a Construct]
11996 // Variables with the predetermined data-sharing attributes may not be
11997 // listed in data-sharing attributes clauses, except for the cases
11998 // listed below. For these exceptions only, listing a predetermined
11999 // variable in a data-sharing attribute clause is allowed and overrides
12000 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +000012001 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev758e55e2013-09-06 18:03:48 +000012002 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +000012003 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
12004 << getOpenMPClauseName(OMPC_private);
Alexey Bataeve3727102018-04-18 15:57:46 +000012005 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +000012006 continue;
12007 }
12008
Alexey Bataeve3727102018-04-18 15:57:46 +000012009 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevccb59ec2015-05-19 08:44:56 +000012010 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000012011 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Kelvin Libf594a52016-12-17 05:48:59 +000012012 isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +000012013 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
12014 << getOpenMPClauseName(OMPC_private) << Type
Kelvin Libf594a52016-12-17 05:48:59 +000012015 << getOpenMPDirectiveName(CurrDir);
Alexey Bataevccb59ec2015-05-19 08:44:56 +000012016 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000012017 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +000012018 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000012019 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +000012020 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000012021 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +000012022 continue;
12023 }
12024
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012025 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
12026 // A list item cannot appear in both a map clause and a data-sharing
12027 // attribute clause on the same construct
Joel E. Denny7d5bc552019-08-22 03:34:30 +000012028 //
12029 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
12030 // A list item cannot appear in both a map clause and a data-sharing
12031 // attribute clause on the same construct unless the construct is a
12032 // combined construct.
12033 if ((LangOpts.OpenMP <= 45 && isOpenMPTargetExecutionDirective(CurrDir)) ||
12034 CurrDir == OMPD_target) {
Samuel Antao6890b092016-07-28 14:25:09 +000012035 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +000012036 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000012037 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +000012038 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
12039 OpenMPClauseKind WhereFoundClauseKind) -> bool {
12040 ConflictKind = WhereFoundClauseKind;
12041 return true;
12042 })) {
12043 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012044 << getOpenMPClauseName(OMPC_private)
Samuel Antao6890b092016-07-28 14:25:09 +000012045 << getOpenMPClauseName(ConflictKind)
Kelvin Libf594a52016-12-17 05:48:59 +000012046 << getOpenMPDirectiveName(CurrDir);
Alexey Bataeve3727102018-04-18 15:57:46 +000012047 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012048 continue;
12049 }
12050 }
12051
Alexey Bataevf120c0d2015-05-19 07:46:42 +000012052 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
12053 // A variable of class type (or array thereof) that appears in a private
12054 // clause requires an accessible, unambiguous default constructor for the
12055 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +000012056 // Generate helper private variable and initialize it with the default
12057 // value. The address of the original variable is replaced by the address of
12058 // the new private variable in CodeGen. This new variable is not added to
12059 // IdResolver, so the code in the OpenMP region uses original variable for
12060 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000012061 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000012062 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000012063 buildVarDecl(*this, ELoc, Type, D->getName(),
12064 D->hasAttrs() ? &D->getAttrs() : nullptr,
12065 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Richard Smith3beb7c62017-01-12 02:27:38 +000012066 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev03b340a2014-10-21 03:16:40 +000012067 if (VDPrivate->isInvalidDecl())
12068 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +000012069 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000012070 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +000012071
Alexey Bataev90c228f2016-02-08 09:29:13 +000012072 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000012073 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +000012074 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +000012075 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000012076 Vars.push_back((VD || CurContext->isDependentContext())
12077 ? RefExpr->IgnoreParens()
12078 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +000012079 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000012080 }
12081
Alexey Bataeved09d242014-05-28 05:53:51 +000012082 if (Vars.empty())
12083 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000012084
Alexey Bataev03b340a2014-10-21 03:16:40 +000012085 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
12086 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000012087}
12088
Alexey Bataev4a5bb772014-10-08 14:01:46 +000012089namespace {
12090class DiagsUninitializedSeveretyRAII {
12091private:
12092 DiagnosticsEngine &Diags;
12093 SourceLocation SavedLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +000012094 bool IsIgnored = false;
Alexey Bataev4a5bb772014-10-08 14:01:46 +000012095
12096public:
12097 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
12098 bool IsIgnored)
12099 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
12100 if (!IsIgnored) {
12101 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
12102 /*Map*/ diag::Severity::Ignored, Loc);
12103 }
12104 }
12105 ~DiagsUninitializedSeveretyRAII() {
12106 if (!IsIgnored)
12107 Diags.popMappings(SavedLoc);
12108 }
12109};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000012110}
Alexey Bataev4a5bb772014-10-08 14:01:46 +000012111
Alexey Bataevd5af8e42013-10-01 05:32:34 +000012112OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
12113 SourceLocation StartLoc,
12114 SourceLocation LParenLoc,
12115 SourceLocation EndLoc) {
12116 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +000012117 SmallVector<Expr *, 8> PrivateCopies;
12118 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +000012119 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000012120 bool IsImplicitClause =
12121 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
Alexey Bataeve3727102018-04-18 15:57:46 +000012122 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000012123
Alexey Bataeve3727102018-04-18 15:57:46 +000012124 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000012125 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000012126 SourceLocation ELoc;
12127 SourceRange ERange;
12128 Expr *SimpleRefExpr = RefExpr;
12129 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +000012130 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +000012131 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000012132 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000012133 PrivateCopies.push_back(nullptr);
12134 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000012135 }
Alexey Bataevd985eda2016-02-10 11:29:16 +000012136 ValueDecl *D = Res.first;
12137 if (!D)
12138 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +000012139
Alexey Bataev60da77e2016-02-29 05:54:20 +000012140 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +000012141 QualType Type = D->getType();
12142 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000012143
12144 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
12145 // A variable that appears in a private clause must not have an incomplete
12146 // type or a reference type.
12147 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +000012148 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +000012149 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000012150 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +000012151
12152 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
12153 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +000012154 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +000012155 // class type.
Alexey Bataeve3727102018-04-18 15:57:46 +000012156 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +000012157
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000012158 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +000012159 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000012160 if (!IsImplicitClause) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012161 DSAStackTy::DSAVarData DVar =
12162 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +000012163 TopDVar = DVar;
Alexey Bataeveffbdf12017-07-21 17:24:30 +000012164 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevf120c0d2015-05-19 07:46:42 +000012165 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000012166 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
12167 // A list item that specifies a given variable may not appear in more
12168 // than one clause on the same directive, except that a variable may be
12169 // specified in both firstprivate and lastprivate clauses.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000012170 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
12171 // A list item may appear in a firstprivate or lastprivate clause but not
12172 // both.
Alexey Bataevd5af8e42013-10-01 05:32:34 +000012173 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +000012174 (isOpenMPDistributeDirective(CurrDir) ||
12175 DVar.CKind != OMPC_lastprivate) &&
Alexey Bataeveffbdf12017-07-21 17:24:30 +000012176 DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +000012177 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +000012178 << getOpenMPClauseName(DVar.CKind)
12179 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000012180 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000012181 continue;
12182 }
12183
12184 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
12185 // in a Construct]
12186 // Variables with the predetermined data-sharing attributes may not be
12187 // listed in data-sharing attributes clauses, except for the cases
12188 // listed below. For these exceptions only, listing a predetermined
12189 // variable in a data-sharing attribute clause is allowed and overrides
12190 // the variable's predetermined data-sharing attributes.
12191 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
12192 // in a Construct, C/C++, p.2]
12193 // Variables with const-qualified type having no mutable member may be
12194 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +000012195 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +000012196 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
12197 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +000012198 << getOpenMPClauseName(DVar.CKind)
12199 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000012200 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000012201 continue;
12202 }
12203
12204 // OpenMP [2.9.3.4, Restrictions, p.2]
12205 // A list item that is private within a parallel region must not appear
12206 // in a firstprivate clause on a worksharing construct if any of the
12207 // worksharing regions arising from the worksharing construct ever bind
12208 // to any of the parallel regions arising from the parallel construct.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000012209 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
12210 // A list item that is private within a teams region must not appear in a
12211 // firstprivate clause on a distribute construct if any of the distribute
12212 // regions arising from the distribute construct ever bind to any of the
12213 // teams regions arising from the teams construct.
12214 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
12215 // A list item that appears in a reduction clause of a teams construct
12216 // must not appear in a firstprivate clause on a distribute construct if
12217 // any of the distribute regions arising from the distribute construct
12218 // ever bind to any of the teams regions arising from the teams construct.
12219 if ((isOpenMPWorksharingDirective(CurrDir) ||
12220 isOpenMPDistributeDirective(CurrDir)) &&
Kelvin Li579e41c2016-11-30 23:51:03 +000012221 !isOpenMPParallelDirective(CurrDir) &&
12222 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +000012223 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000012224 if (DVar.CKind != OMPC_shared &&
12225 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +000012226 isOpenMPTeamsDirective(DVar.DKind) ||
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000012227 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +000012228 Diag(ELoc, diag::err_omp_required_access)
12229 << getOpenMPClauseName(OMPC_firstprivate)
12230 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000012231 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +000012232 continue;
12233 }
12234 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +000012235 // OpenMP [2.9.3.4, Restrictions, p.3]
12236 // A list item that appears in a reduction clause of a parallel construct
12237 // must not appear in a firstprivate clause on a worksharing or task
12238 // construct if any of the worksharing or task regions arising from the
12239 // worksharing or task construct ever bind to any of the parallel regions
12240 // arising from the parallel construct.
12241 // OpenMP [2.9.3.4, Restrictions, p.4]
12242 // A list item that appears in a reduction clause in worksharing
12243 // construct must not appear in a firstprivate clause in a task construct
12244 // encountered during execution of any of the worksharing regions arising
12245 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +000012246 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000012247 DVar = DSAStack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +000012248 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
12249 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000012250 return isOpenMPParallelDirective(K) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +000012251 isOpenMPWorksharingDirective(K) ||
12252 isOpenMPTeamsDirective(K);
Alexey Bataev7ace49d2016-05-17 08:55:33 +000012253 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +000012254 /*FromParent=*/true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000012255 if (DVar.CKind == OMPC_reduction &&
12256 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +000012257 isOpenMPWorksharingDirective(DVar.DKind) ||
12258 isOpenMPTeamsDirective(DVar.DKind))) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000012259 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
12260 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataeve3727102018-04-18 15:57:46 +000012261 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000012262 continue;
12263 }
12264 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +000012265
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012266 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
12267 // A list item cannot appear in both a map clause and a data-sharing
12268 // attribute clause on the same construct
Joel E. Denny7d5bc552019-08-22 03:34:30 +000012269 //
12270 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
12271 // A list item cannot appear in both a map clause and a data-sharing
12272 // attribute clause on the same construct unless the construct is a
12273 // combined construct.
12274 if ((LangOpts.OpenMP <= 45 &&
12275 isOpenMPTargetExecutionDirective(CurrDir)) ||
12276 CurrDir == OMPD_target) {
Samuel Antao6890b092016-07-28 14:25:09 +000012277 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +000012278 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000012279 VD, /*CurrentRegionOnly=*/true,
Alexey Bataeve3727102018-04-18 15:57:46 +000012280 [&ConflictKind](
12281 OMPClauseMappableExprCommon::MappableExprComponentListRef,
12282 OpenMPClauseKind WhereFoundClauseKind) {
Samuel Antao6890b092016-07-28 14:25:09 +000012283 ConflictKind = WhereFoundClauseKind;
12284 return true;
12285 })) {
12286 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012287 << getOpenMPClauseName(OMPC_firstprivate)
Samuel Antao6890b092016-07-28 14:25:09 +000012288 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012289 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000012290 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012291 continue;
12292 }
12293 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +000012294 }
12295
Alexey Bataevccb59ec2015-05-19 08:44:56 +000012296 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000012297 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +000012298 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +000012299 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
12300 << getOpenMPClauseName(OMPC_firstprivate) << Type
12301 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
12302 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +000012303 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +000012304 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +000012305 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +000012306 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +000012307 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +000012308 continue;
12309 }
12310
Alexey Bataevf120c0d2015-05-19 07:46:42 +000012311 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000012312 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000012313 buildVarDecl(*this, ELoc, Type, D->getName(),
12314 D->hasAttrs() ? &D->getAttrs() : nullptr,
12315 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000012316 // Generate helper private variable and initialize it with the value of the
12317 // original variable. The address of the original variable is replaced by
12318 // the address of the new private variable in the CodeGen. This new variable
12319 // is not added to IdResolver, so the code in the OpenMP region uses
12320 // original variable for proper diagnostics and variable capturing.
12321 Expr *VDInitRefExpr = nullptr;
12322 // For arrays generate initializer for single element and replace it by the
12323 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000012324 if (Type->isArrayType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012325 VarDecl *VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +000012326 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +000012327 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +000012328 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +000012329 ElemType = ElemType.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000012330 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
12331 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +000012332 InitializedEntity Entity =
12333 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000012334 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
12335
12336 InitializationSequence InitSeq(*this, Entity, Kind, Init);
12337 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
12338 if (Result.isInvalid())
12339 VDPrivate->setInvalidDecl();
12340 else
12341 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +000012342 // Remove temp variable declaration.
12343 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000012344 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +000012345 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
12346 ".firstprivate.temp");
Alexey Bataevd985eda2016-02-10 11:29:16 +000012347 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
12348 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +000012349 AddInitializerToDecl(VDPrivate,
12350 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000012351 /*DirectInit=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000012352 }
12353 if (VDPrivate->isInvalidDecl()) {
12354 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +000012355 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +000012356 diag::note_omp_task_predetermined_firstprivate_here);
12357 }
12358 continue;
12359 }
12360 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000012361 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +000012362 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
12363 RefExpr->getExprLoc());
12364 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000012365 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012366 if (TopDVar.CKind == OMPC_lastprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000012367 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000012368 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000012369 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000012370 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000012371 ExprCaptures.push_back(Ref->getDecl());
12372 }
Alexey Bataev417089f2016-02-17 13:19:37 +000012373 }
Alexey Bataevd985eda2016-02-10 11:29:16 +000012374 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000012375 Vars.push_back((VD || CurContext->isDependentContext())
12376 ? RefExpr->IgnoreParens()
12377 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000012378 PrivateCopies.push_back(VDPrivateRefExpr);
12379 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000012380 }
12381
Alexey Bataeved09d242014-05-28 05:53:51 +000012382 if (Vars.empty())
12383 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +000012384
12385 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +000012386 Vars, PrivateCopies, Inits,
12387 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +000012388}
12389
Alexander Musman1bb328c2014-06-04 13:06:39 +000012390OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
12391 SourceLocation StartLoc,
12392 SourceLocation LParenLoc,
12393 SourceLocation EndLoc) {
12394 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +000012395 SmallVector<Expr *, 8> SrcExprs;
12396 SmallVector<Expr *, 8> DstExprs;
12397 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +000012398 SmallVector<Decl *, 4> ExprCaptures;
12399 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeve3727102018-04-18 15:57:46 +000012400 for (Expr *RefExpr : VarList) {
Alexander Musman1bb328c2014-06-04 13:06:39 +000012401 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000012402 SourceLocation ELoc;
12403 SourceRange ERange;
12404 Expr *SimpleRefExpr = RefExpr;
12405 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +000012406 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +000012407 // It will be analyzed later.
12408 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +000012409 SrcExprs.push_back(nullptr);
12410 DstExprs.push_back(nullptr);
12411 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +000012412 }
Alexey Bataev74caaf22016-02-20 04:09:36 +000012413 ValueDecl *D = Res.first;
12414 if (!D)
12415 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000012416
Alexey Bataev74caaf22016-02-20 04:09:36 +000012417 QualType Type = D->getType();
12418 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +000012419
12420 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
12421 // A variable that appears in a lastprivate clause must not have an
12422 // incomplete type or a reference type.
12423 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +000012424 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +000012425 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000012426 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +000012427
Joel E. Dennye6234d1422019-01-04 22:11:31 +000012428 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
12429 // A variable that is privatized must not have a const-qualified type
12430 // unless it is of class type with a mutable member. This restriction does
12431 // not apply to the firstprivate clause.
12432 //
12433 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions]
12434 // A variable that appears in a lastprivate clause must not have a
12435 // const-qualified type unless it is of class type with a mutable member.
Joel E. Dennyd2649292019-01-04 22:11:56 +000012436 if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc))
Joel E. Dennye6234d1422019-01-04 22:11:31 +000012437 continue;
12438
Alexey Bataeveffbdf12017-07-21 17:24:30 +000012439 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexander Musman1bb328c2014-06-04 13:06:39 +000012440 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
12441 // in a Construct]
12442 // Variables with the predetermined data-sharing attributes may not be
12443 // listed in data-sharing attributes clauses, except for the cases
12444 // listed below.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000012445 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
12446 // A list item may appear in a firstprivate or lastprivate clause but not
12447 // both.
Alexey Bataeve3727102018-04-18 15:57:46 +000012448 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman1bb328c2014-06-04 13:06:39 +000012449 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +000012450 (isOpenMPDistributeDirective(CurrDir) ||
12451 DVar.CKind != OMPC_firstprivate) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +000012452 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
12453 Diag(ELoc, diag::err_omp_wrong_dsa)
12454 << getOpenMPClauseName(DVar.CKind)
12455 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000012456 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +000012457 continue;
12458 }
12459
Alexey Bataevf29276e2014-06-18 04:14:57 +000012460 // OpenMP [2.14.3.5, Restrictions, p.2]
12461 // A list item that is private within a parallel region, or that appears in
12462 // the reduction clause of a parallel construct, must not appear in a
12463 // lastprivate clause on a worksharing construct if any of the corresponding
12464 // worksharing regions ever binds to any of the corresponding parallel
12465 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +000012466 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +000012467 if (isOpenMPWorksharingDirective(CurrDir) &&
Kelvin Li579e41c2016-11-30 23:51:03 +000012468 !isOpenMPParallelDirective(CurrDir) &&
12469 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +000012470 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +000012471 if (DVar.CKind != OMPC_shared) {
12472 Diag(ELoc, diag::err_omp_required_access)
12473 << getOpenMPClauseName(OMPC_lastprivate)
12474 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000012475 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +000012476 continue;
12477 }
12478 }
Alexey Bataev74caaf22016-02-20 04:09:36 +000012479
Alexander Musman1bb328c2014-06-04 13:06:39 +000012480 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +000012481 // A variable of class type (or array thereof) that appears in a
12482 // lastprivate clause requires an accessible, unambiguous default
12483 // constructor for the class type, unless the list item is also specified
12484 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +000012485 // A variable of class type (or array thereof) that appears in a
12486 // lastprivate clause requires an accessible, unambiguous copy assignment
12487 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +000012488 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000012489 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(),
12490 Type.getUnqualifiedType(), ".lastprivate.src",
12491 D->hasAttrs() ? &D->getAttrs() : nullptr);
12492 DeclRefExpr *PseudoSrcExpr =
Alexey Bataev74caaf22016-02-20 04:09:36 +000012493 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +000012494 VarDecl *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +000012495 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +000012496 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000012497 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +000012498 // For arrays generate assignment operation for single element and replace
12499 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000012500 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
12501 PseudoDstExpr, PseudoSrcExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +000012502 if (AssignmentOp.isInvalid())
12503 continue;
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000012504 AssignmentOp =
12505 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev38e89532015-04-16 04:54:05 +000012506 if (AssignmentOp.isInvalid())
12507 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000012508
Alexey Bataev74caaf22016-02-20 04:09:36 +000012509 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000012510 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012511 if (TopDVar.CKind == OMPC_firstprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000012512 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000012513 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000012514 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000012515 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000012516 ExprCaptures.push_back(Ref->getDecl());
12517 }
12518 if (TopDVar.CKind == OMPC_firstprivate ||
Alexey Bataeve3727102018-04-18 15:57:46 +000012519 (!isOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +000012520 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +000012521 ExprResult RefRes = DefaultLvalueConversion(Ref);
12522 if (!RefRes.isUsable())
12523 continue;
12524 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +000012525 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
12526 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +000012527 if (!PostUpdateRes.isUsable())
12528 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +000012529 ExprPostUpdates.push_back(
12530 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +000012531 }
12532 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +000012533 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000012534 Vars.push_back((VD || CurContext->isDependentContext())
12535 ? RefExpr->IgnoreParens()
12536 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +000012537 SrcExprs.push_back(PseudoSrcExpr);
12538 DstExprs.push_back(PseudoDstExpr);
12539 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +000012540 }
12541
12542 if (Vars.empty())
12543 return nullptr;
12544
12545 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +000012546 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +000012547 buildPreInits(Context, ExprCaptures),
12548 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +000012549}
12550
Alexey Bataev758e55e2013-09-06 18:03:48 +000012551OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
12552 SourceLocation StartLoc,
12553 SourceLocation LParenLoc,
12554 SourceLocation EndLoc) {
12555 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000012556 for (Expr *RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +000012557 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000012558 SourceLocation ELoc;
12559 SourceRange ERange;
12560 Expr *SimpleRefExpr = RefExpr;
12561 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000012562 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +000012563 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000012564 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +000012565 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +000012566 ValueDecl *D = Res.first;
12567 if (!D)
12568 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +000012569
Alexey Bataevb7a34b62016-02-25 03:59:29 +000012570 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +000012571 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
12572 // in a Construct]
12573 // Variables with the predetermined data-sharing attributes may not be
12574 // listed in data-sharing attributes clauses, except for the cases
12575 // listed below. For these exceptions only, listing a predetermined
12576 // variable in a data-sharing attribute clause is allowed and overrides
12577 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +000012578 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeved09d242014-05-28 05:53:51 +000012579 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
12580 DVar.RefExpr) {
12581 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
12582 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000012583 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +000012584 continue;
12585 }
12586
Alexey Bataevb7a34b62016-02-25 03:59:29 +000012587 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000012588 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +000012589 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000012590 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000012591 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
12592 ? RefExpr->IgnoreParens()
12593 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +000012594 }
12595
Alexey Bataeved09d242014-05-28 05:53:51 +000012596 if (Vars.empty())
12597 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000012598
12599 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
12600}
12601
Alexey Bataevc5e02582014-06-16 07:08:35 +000012602namespace {
12603class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
12604 DSAStackTy *Stack;
12605
12606public:
12607 bool VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012608 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
12609 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevc5e02582014-06-16 07:08:35 +000012610 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
12611 return false;
12612 if (DVar.CKind != OMPC_unknown)
12613 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000012614 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +000012615 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; },
Alexey Bataeveffbdf12017-07-21 17:24:30 +000012616 /*FromParent=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000012617 return DVarPrivate.CKind != OMPC_unknown;
Alexey Bataevc5e02582014-06-16 07:08:35 +000012618 }
12619 return false;
12620 }
12621 bool VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012622 for (Stmt *Child : S->children()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000012623 if (Child && Visit(Child))
12624 return true;
12625 }
12626 return false;
12627 }
Alexey Bataev23b69422014-06-18 07:08:49 +000012628 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +000012629};
Alexey Bataev23b69422014-06-18 07:08:49 +000012630} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +000012631
Alexey Bataev60da77e2016-02-29 05:54:20 +000012632namespace {
12633// Transform MemberExpression for specified FieldDecl of current class to
12634// DeclRefExpr to specified OMPCapturedExprDecl.
12635class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
12636 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
Alexey Bataeve3727102018-04-18 15:57:46 +000012637 ValueDecl *Field = nullptr;
12638 DeclRefExpr *CapturedExpr = nullptr;
Alexey Bataev60da77e2016-02-29 05:54:20 +000012639
12640public:
12641 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
12642 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
12643
12644 ExprResult TransformMemberExpr(MemberExpr *E) {
12645 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
12646 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +000012647 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +000012648 return CapturedExpr;
12649 }
12650 return BaseTransform::TransformMemberExpr(E);
12651 }
12652 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
12653};
12654} // namespace
12655
Alexey Bataev97d18bf2018-04-11 19:21:00 +000012656template <typename T, typename U>
Michael Kruse4304e9d2019-02-19 16:38:20 +000012657static T filterLookupForUDReductionAndMapper(
12658 SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012659 for (U &Set : Lookups) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012660 for (auto *D : Set) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012661 if (T Res = Gen(cast<ValueDecl>(D)))
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012662 return Res;
12663 }
12664 }
12665 return T();
12666}
12667
Alexey Bataev43b90b72018-09-12 16:31:59 +000012668static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
12669 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
12670
12671 for (auto RD : D->redecls()) {
12672 // Don't bother with extra checks if we already know this one isn't visible.
12673 if (RD == D)
12674 continue;
12675
12676 auto ND = cast<NamedDecl>(RD);
12677 if (LookupResult::isVisible(SemaRef, ND))
12678 return ND;
12679 }
12680
12681 return nullptr;
12682}
12683
12684static void
Michael Kruse4304e9d2019-02-19 16:38:20 +000012685argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id,
Alexey Bataev43b90b72018-09-12 16:31:59 +000012686 SourceLocation Loc, QualType Ty,
12687 SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
12688 // Find all of the associated namespaces and classes based on the
12689 // arguments we have.
12690 Sema::AssociatedNamespaceSet AssociatedNamespaces;
12691 Sema::AssociatedClassSet AssociatedClasses;
12692 OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
12693 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces,
12694 AssociatedClasses);
12695
12696 // C++ [basic.lookup.argdep]p3:
12697 // Let X be the lookup set produced by unqualified lookup (3.4.1)
12698 // and let Y be the lookup set produced by argument dependent
12699 // lookup (defined as follows). If X contains [...] then Y is
12700 // empty. Otherwise Y is the set of declarations found in the
12701 // namespaces associated with the argument types as described
12702 // below. The set of declarations found by the lookup of the name
12703 // is the union of X and Y.
12704 //
12705 // Here, we compute Y and add its members to the overloaded
12706 // candidate set.
12707 for (auto *NS : AssociatedNamespaces) {
12708 // When considering an associated namespace, the lookup is the
12709 // same as the lookup performed when the associated namespace is
12710 // used as a qualifier (3.4.3.2) except that:
12711 //
12712 // -- Any using-directives in the associated namespace are
12713 // ignored.
12714 //
12715 // -- Any namespace-scope friend functions declared in
12716 // associated classes are visible within their respective
12717 // namespaces even if they are not visible during an ordinary
12718 // lookup (11.4).
Michael Kruse4304e9d2019-02-19 16:38:20 +000012719 DeclContext::lookup_result R = NS->lookup(Id.getName());
Alexey Bataev43b90b72018-09-12 16:31:59 +000012720 for (auto *D : R) {
12721 auto *Underlying = D;
12722 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
12723 Underlying = USD->getTargetDecl();
12724
Michael Kruse4304e9d2019-02-19 16:38:20 +000012725 if (!isa<OMPDeclareReductionDecl>(Underlying) &&
12726 !isa<OMPDeclareMapperDecl>(Underlying))
Alexey Bataev43b90b72018-09-12 16:31:59 +000012727 continue;
12728
12729 if (!SemaRef.isVisible(D)) {
12730 D = findAcceptableDecl(SemaRef, D);
12731 if (!D)
12732 continue;
12733 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
12734 Underlying = USD->getTargetDecl();
12735 }
12736 Lookups.emplace_back();
12737 Lookups.back().addDecl(Underlying);
12738 }
12739 }
12740}
12741
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012742static ExprResult
12743buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
12744 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
12745 const DeclarationNameInfo &ReductionId, QualType Ty,
12746 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
12747 if (ReductionIdScopeSpec.isInvalid())
12748 return ExprError();
12749 SmallVector<UnresolvedSet<8>, 4> Lookups;
12750 if (S) {
12751 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
12752 Lookup.suppressDiagnostics();
12753 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012754 NamedDecl *D = Lookup.getRepresentativeDecl();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012755 do {
12756 S = S->getParent();
12757 } while (S && !S->isDeclScope(D));
12758 if (S)
12759 S = S->getParent();
Alexey Bataev43b90b72018-09-12 16:31:59 +000012760 Lookups.emplace_back();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012761 Lookups.back().append(Lookup.begin(), Lookup.end());
12762 Lookup.clear();
12763 }
12764 } else if (auto *ULE =
12765 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
12766 Lookups.push_back(UnresolvedSet<8>());
12767 Decl *PrevD = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000012768 for (NamedDecl *D : ULE->decls()) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012769 if (D == PrevD)
12770 Lookups.push_back(UnresolvedSet<8>());
Don Hintonf170dff2019-03-19 06:14:14 +000012771 else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(D))
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012772 Lookups.back().addDecl(DRD);
12773 PrevD = D;
12774 }
12775 }
Alexey Bataevfdc20352017-08-25 15:43:55 +000012776 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
12777 Ty->isInstantiationDependentType() ||
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012778 Ty->containsUnexpandedParameterPack() ||
Michael Kruse4304e9d2019-02-19 16:38:20 +000012779 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012780 return !D->isInvalidDecl() &&
12781 (D->getType()->isDependentType() ||
12782 D->getType()->isInstantiationDependentType() ||
12783 D->getType()->containsUnexpandedParameterPack());
12784 })) {
12785 UnresolvedSet<8> ResSet;
Alexey Bataeve3727102018-04-18 15:57:46 +000012786 for (const UnresolvedSet<8> &Set : Lookups) {
Alexey Bataev43b90b72018-09-12 16:31:59 +000012787 if (Set.empty())
12788 continue;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012789 ResSet.append(Set.begin(), Set.end());
12790 // The last item marks the end of all declarations at the specified scope.
12791 ResSet.addDecl(Set[Set.size() - 1]);
12792 }
12793 return UnresolvedLookupExpr::Create(
12794 SemaRef.Context, /*NamingClass=*/nullptr,
12795 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
12796 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
12797 }
Alexey Bataev43b90b72018-09-12 16:31:59 +000012798 // Lookup inside the classes.
12799 // C++ [over.match.oper]p3:
12800 // For a unary operator @ with an operand of a type whose
12801 // cv-unqualified version is T1, and for a binary operator @ with
12802 // a left operand of a type whose cv-unqualified version is T1 and
12803 // a right operand of a type whose cv-unqualified version is T2,
12804 // three sets of candidate functions, designated member
12805 // candidates, non-member candidates and built-in candidates, are
12806 // constructed as follows:
12807 // -- If T1 is a complete class type or a class currently being
12808 // defined, the set of member candidates is the result of the
12809 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
12810 // the set of member candidates is empty.
12811 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
12812 Lookup.suppressDiagnostics();
12813 if (const auto *TyRec = Ty->getAs<RecordType>()) {
12814 // Complete the type if it can be completed.
12815 // If the type is neither complete nor being defined, bail out now.
12816 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() ||
12817 TyRec->getDecl()->getDefinition()) {
12818 Lookup.clear();
12819 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl());
12820 if (Lookup.empty()) {
12821 Lookups.emplace_back();
12822 Lookups.back().append(Lookup.begin(), Lookup.end());
12823 }
12824 }
12825 }
12826 // Perform ADL.
Alexey Bataev09232662019-04-04 17:28:22 +000012827 if (SemaRef.getLangOpts().CPlusPlus)
Alexey Bataev74a04e82019-03-13 19:31:34 +000012828 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups);
Alexey Bataev09232662019-04-04 17:28:22 +000012829 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
12830 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
12831 if (!D->isInvalidDecl() &&
12832 SemaRef.Context.hasSameType(D->getType(), Ty))
12833 return D;
12834 return nullptr;
12835 }))
12836 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(),
12837 VK_LValue, Loc);
12838 if (SemaRef.getLangOpts().CPlusPlus) {
Alexey Bataev74a04e82019-03-13 19:31:34 +000012839 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
12840 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
12841 if (!D->isInvalidDecl() &&
12842 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
12843 !Ty.isMoreQualifiedThan(D->getType()))
12844 return D;
12845 return nullptr;
12846 })) {
12847 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
12848 /*DetectVirtual=*/false);
12849 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
12850 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
12851 VD->getType().getUnqualifiedType()))) {
12852 if (SemaRef.CheckBaseClassAccess(
12853 Loc, VD->getType(), Ty, Paths.front(),
12854 /*DiagID=*/0) != Sema::AR_inaccessible) {
12855 SemaRef.BuildBasePathArray(Paths, BasePath);
12856 return SemaRef.BuildDeclRefExpr(
12857 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc);
12858 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012859 }
12860 }
12861 }
12862 }
12863 if (ReductionIdScopeSpec.isSet()) {
12864 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
12865 return ExprError();
12866 }
12867 return ExprEmpty();
12868}
12869
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012870namespace {
12871/// Data for the reduction-based clauses.
12872struct ReductionData {
12873 /// List of original reduction items.
12874 SmallVector<Expr *, 8> Vars;
12875 /// List of private copies of the reduction items.
12876 SmallVector<Expr *, 8> Privates;
12877 /// LHS expressions for the reduction_op expressions.
12878 SmallVector<Expr *, 8> LHSs;
12879 /// RHS expressions for the reduction_op expressions.
12880 SmallVector<Expr *, 8> RHSs;
12881 /// Reduction operation expression.
12882 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev88202be2017-07-27 13:20:36 +000012883 /// Taskgroup descriptors for the corresponding reduction items in
12884 /// in_reduction clauses.
12885 SmallVector<Expr *, 8> TaskgroupDescriptors;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012886 /// List of captures for clause.
12887 SmallVector<Decl *, 4> ExprCaptures;
12888 /// List of postupdate expressions.
12889 SmallVector<Expr *, 4> ExprPostUpdates;
12890 ReductionData() = delete;
12891 /// Reserves required memory for the reduction data.
12892 ReductionData(unsigned Size) {
12893 Vars.reserve(Size);
12894 Privates.reserve(Size);
12895 LHSs.reserve(Size);
12896 RHSs.reserve(Size);
12897 ReductionOps.reserve(Size);
Alexey Bataev88202be2017-07-27 13:20:36 +000012898 TaskgroupDescriptors.reserve(Size);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012899 ExprCaptures.reserve(Size);
12900 ExprPostUpdates.reserve(Size);
12901 }
12902 /// Stores reduction item and reduction operation only (required for dependent
12903 /// reduction item).
12904 void push(Expr *Item, Expr *ReductionOp) {
12905 Vars.emplace_back(Item);
12906 Privates.emplace_back(nullptr);
12907 LHSs.emplace_back(nullptr);
12908 RHSs.emplace_back(nullptr);
12909 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000012910 TaskgroupDescriptors.emplace_back(nullptr);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012911 }
12912 /// Stores reduction data.
Alexey Bataev88202be2017-07-27 13:20:36 +000012913 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
12914 Expr *TaskgroupDescriptor) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012915 Vars.emplace_back(Item);
12916 Privates.emplace_back(Private);
12917 LHSs.emplace_back(LHS);
12918 RHSs.emplace_back(RHS);
12919 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000012920 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012921 }
12922};
12923} // namespace
12924
Alexey Bataeve3727102018-04-18 15:57:46 +000012925static bool checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000012926 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
12927 SmallVectorImpl<llvm::APSInt> &ArraySizes) {
12928 const Expr *Length = OASE->getLength();
12929 if (Length == nullptr) {
12930 // For array sections of the form [1:] or [:], we would need to analyze
12931 // the lower bound...
12932 if (OASE->getColonLoc().isValid())
12933 return false;
12934
12935 // This is an array subscript which has implicit length 1!
12936 SingleElement = true;
12937 ArraySizes.push_back(llvm::APSInt::get(1));
12938 } else {
Fangrui Song407659a2018-11-30 23:41:18 +000012939 Expr::EvalResult Result;
12940 if (!Length->EvaluateAsInt(Result, Context))
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000012941 return false;
12942
Fangrui Song407659a2018-11-30 23:41:18 +000012943 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000012944 SingleElement = (ConstantLengthValue.getSExtValue() == 1);
12945 ArraySizes.push_back(ConstantLengthValue);
12946 }
12947
12948 // Get the base of this array section and walk up from there.
12949 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
12950
12951 // We require length = 1 for all array sections except the right-most to
12952 // guarantee that the memory region is contiguous and has no holes in it.
12953 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
12954 Length = TempOASE->getLength();
12955 if (Length == nullptr) {
12956 // For array sections of the form [1:] or [:], we would need to analyze
12957 // the lower bound...
12958 if (OASE->getColonLoc().isValid())
12959 return false;
12960
12961 // This is an array subscript which has implicit length 1!
12962 ArraySizes.push_back(llvm::APSInt::get(1));
12963 } else {
Fangrui Song407659a2018-11-30 23:41:18 +000012964 Expr::EvalResult Result;
12965 if (!Length->EvaluateAsInt(Result, Context))
12966 return false;
12967
12968 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
12969 if (ConstantLengthValue.getSExtValue() != 1)
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000012970 return false;
12971
12972 ArraySizes.push_back(ConstantLengthValue);
12973 }
12974 Base = TempOASE->getBase()->IgnoreParenImpCasts();
12975 }
12976
12977 // If we have a single element, we don't need to add the implicit lengths.
12978 if (!SingleElement) {
12979 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
12980 // Has implicit length 1!
12981 ArraySizes.push_back(llvm::APSInt::get(1));
12982 Base = TempASE->getBase()->IgnoreParenImpCasts();
12983 }
12984 }
12985
12986 // This array section can be privatized as a single value or as a constant
12987 // sized array.
12988 return true;
12989}
12990
Alexey Bataeve3727102018-04-18 15:57:46 +000012991static bool actOnOMPReductionKindClause(
Alexey Bataev169d96a2017-07-18 20:17:46 +000012992 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
12993 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
12994 SourceLocation ColonLoc, SourceLocation EndLoc,
12995 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012996 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012997 DeclarationName DN = ReductionId.getName();
12998 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
Alexey Bataevc5e02582014-06-16 07:08:35 +000012999 BinaryOperatorKind BOK = BO_Comma;
13000
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013001 ASTContext &Context = S.Context;
Alexey Bataevc5e02582014-06-16 07:08:35 +000013002 // OpenMP [2.14.3.6, reduction clause]
13003 // C
13004 // reduction-identifier is either an identifier or one of the following
13005 // operators: +, -, *, &, |, ^, && and ||
13006 // C++
13007 // reduction-identifier is either an id-expression or one of the following
13008 // operators: +, -, *, &, |, ^, && and ||
Alexey Bataevc5e02582014-06-16 07:08:35 +000013009 switch (OOK) {
13010 case OO_Plus:
13011 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000013012 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +000013013 break;
13014 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000013015 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +000013016 break;
13017 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000013018 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +000013019 break;
13020 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000013021 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +000013022 break;
13023 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000013024 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +000013025 break;
13026 case OO_AmpAmp:
13027 BOK = BO_LAnd;
13028 break;
13029 case OO_PipePipe:
13030 BOK = BO_LOr;
13031 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000013032 case OO_New:
13033 case OO_Delete:
13034 case OO_Array_New:
13035 case OO_Array_Delete:
13036 case OO_Slash:
13037 case OO_Percent:
13038 case OO_Tilde:
13039 case OO_Exclaim:
13040 case OO_Equal:
13041 case OO_Less:
13042 case OO_Greater:
13043 case OO_LessEqual:
13044 case OO_GreaterEqual:
13045 case OO_PlusEqual:
13046 case OO_MinusEqual:
13047 case OO_StarEqual:
13048 case OO_SlashEqual:
13049 case OO_PercentEqual:
13050 case OO_CaretEqual:
13051 case OO_AmpEqual:
13052 case OO_PipeEqual:
13053 case OO_LessLess:
13054 case OO_GreaterGreater:
13055 case OO_LessLessEqual:
13056 case OO_GreaterGreaterEqual:
13057 case OO_EqualEqual:
13058 case OO_ExclaimEqual:
Richard Smithd30b23d2017-12-01 02:13:10 +000013059 case OO_Spaceship:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000013060 case OO_PlusPlus:
13061 case OO_MinusMinus:
13062 case OO_Comma:
13063 case OO_ArrowStar:
13064 case OO_Arrow:
13065 case OO_Call:
13066 case OO_Subscript:
13067 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +000013068 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000013069 case NUM_OVERLOADED_OPERATORS:
13070 llvm_unreachable("Unexpected reduction identifier");
13071 case OO_None:
Alexey Bataeve3727102018-04-18 15:57:46 +000013072 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000013073 if (II->isStr("max"))
13074 BOK = BO_GT;
13075 else if (II->isStr("min"))
13076 BOK = BO_LT;
13077 }
13078 break;
13079 }
13080 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013081 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +000013082 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataev4d4624c2017-07-20 16:47:47 +000013083 else
13084 ReductionIdRange.setBegin(ReductionId.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000013085 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000013086
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013087 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
13088 bool FirstIter = true;
Alexey Bataeve3727102018-04-18 15:57:46 +000013089 for (Expr *RefExpr : VarList) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000013090 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +000013091 // OpenMP [2.1, C/C++]
13092 // A list item is a variable or array section, subject to the restrictions
13093 // specified in Section 2.4 on page 42 and in each of the sections
13094 // describing clauses and directives for which a list appears.
13095 // OpenMP [2.14.3.3, Restrictions, p.1]
13096 // A variable that is part of another variable (as an array or
13097 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013098 if (!FirstIter && IR != ER)
13099 ++IR;
13100 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +000013101 SourceLocation ELoc;
13102 SourceRange ERange;
13103 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013104 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
Alexey Bataev60da77e2016-02-29 05:54:20 +000013105 /*AllowArraySection=*/true);
13106 if (Res.second) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013107 // Try to find 'declare reduction' corresponding construct before using
13108 // builtin/overloaded operators.
13109 QualType Type = Context.DependentTy;
13110 CXXCastPath BasePath;
13111 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013112 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013113 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013114 Expr *ReductionOp = nullptr;
13115 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013116 (DeclareReductionRef.isUnset() ||
13117 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013118 ReductionOp = DeclareReductionRef.get();
13119 // It will be analyzed later.
13120 RD.push(RefExpr, ReductionOp);
Alexey Bataevc5e02582014-06-16 07:08:35 +000013121 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000013122 ValueDecl *D = Res.first;
13123 if (!D)
13124 continue;
13125
Alexey Bataev88202be2017-07-27 13:20:36 +000013126 Expr *TaskgroupDescriptor = nullptr;
Alexey Bataeva1764212015-09-30 09:22:36 +000013127 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +000013128 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
13129 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +000013130 if (ASE) {
Alexey Bataev31300ed2016-02-04 11:27:03 +000013131 Type = ASE->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000013132 } else if (OASE) {
13133 QualType BaseType =
13134 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
13135 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
Alexey Bataeva1764212015-09-30 09:22:36 +000013136 Type = ATy->getElementType();
13137 else
13138 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +000013139 Type = Type.getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000013140 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +000013141 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
Alexey Bataeve3727102018-04-18 15:57:46 +000013142 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000013143 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +000013144
Alexey Bataevc5e02582014-06-16 07:08:35 +000013145 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
13146 // A variable that appears in a private clause must not have an incomplete
13147 // type or a reference type.
Joel E. Denny3cabf732018-06-28 19:54:49 +000013148 if (S.RequireCompleteType(ELoc, D->getType(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013149 diag::err_omp_reduction_incomplete_type))
Alexey Bataevc5e02582014-06-16 07:08:35 +000013150 continue;
13151 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +000013152 // A list item that appears in a reduction clause must not be
13153 // const-qualified.
Joel E. Dennyd2649292019-01-04 22:11:56 +000013154 if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc,
13155 /*AcceptIfMutable*/ false, ASE || OASE))
Alexey Bataevc5e02582014-06-16 07:08:35 +000013156 continue;
Alexey Bataevbc529672018-09-28 19:33:14 +000013157
13158 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
Alexey Bataevc5e02582014-06-16 07:08:35 +000013159 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
13160 // If a list-item is a reference type then it must bind to the same object
13161 // for all threads of the team.
Alexey Bataevbc529672018-09-28 19:33:14 +000013162 if (!ASE && !OASE) {
13163 if (VD) {
13164 VarDecl *VDDef = VD->getDefinition();
13165 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
13166 DSARefChecker Check(Stack);
13167 if (Check.Visit(VDDef->getInit())) {
13168 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
13169 << getOpenMPClauseName(ClauseKind) << ERange;
13170 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
13171 continue;
13172 }
Alexey Bataeva1764212015-09-30 09:22:36 +000013173 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000013174 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013175
Alexey Bataevbc529672018-09-28 19:33:14 +000013176 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
13177 // in a Construct]
13178 // Variables with the predetermined data-sharing attributes may not be
13179 // listed in data-sharing attributes clauses, except for the cases
13180 // listed below. For these exceptions only, listing a predetermined
13181 // variable in a data-sharing attribute clause is allowed and overrides
13182 // the variable's predetermined data-sharing attributes.
13183 // OpenMP [2.14.3.6, Restrictions, p.3]
13184 // Any number of reduction clauses can be specified on the directive,
13185 // but a list item can appear only once in the reduction clauses for that
13186 // directive.
13187 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
13188 if (DVar.CKind == OMPC_reduction) {
13189 S.Diag(ELoc, diag::err_omp_once_referenced)
13190 << getOpenMPClauseName(ClauseKind);
13191 if (DVar.RefExpr)
13192 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
13193 continue;
13194 }
13195 if (DVar.CKind != OMPC_unknown) {
13196 S.Diag(ELoc, diag::err_omp_wrong_dsa)
13197 << getOpenMPClauseName(DVar.CKind)
13198 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataeve3727102018-04-18 15:57:46 +000013199 reportOriginalDsa(S, Stack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000013200 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +000013201 }
Alexey Bataevbc529672018-09-28 19:33:14 +000013202
13203 // OpenMP [2.14.3.6, Restrictions, p.1]
13204 // A list item that appears in a reduction clause of a worksharing
13205 // construct must be shared in the parallel regions to which any of the
13206 // worksharing regions arising from the worksharing construct bind.
13207 if (isOpenMPWorksharingDirective(CurrDir) &&
13208 !isOpenMPParallelDirective(CurrDir) &&
13209 !isOpenMPTeamsDirective(CurrDir)) {
13210 DVar = Stack->getImplicitDSA(D, true);
13211 if (DVar.CKind != OMPC_shared) {
13212 S.Diag(ELoc, diag::err_omp_required_access)
13213 << getOpenMPClauseName(OMPC_reduction)
13214 << getOpenMPClauseName(OMPC_shared);
13215 reportOriginalDsa(S, Stack, D, DVar);
13216 continue;
13217 }
13218 }
Alexey Bataevf29276e2014-06-18 04:14:57 +000013219 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000013220
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013221 // Try to find 'declare reduction' corresponding construct before using
13222 // builtin/overloaded operators.
13223 CXXCastPath BasePath;
13224 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013225 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013226 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
13227 if (DeclareReductionRef.isInvalid())
13228 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013229 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013230 (DeclareReductionRef.isUnset() ||
13231 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013232 RD.push(RefExpr, DeclareReductionRef.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013233 continue;
13234 }
13235 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
13236 // Not allowed reduction identifier is found.
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013237 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013238 diag::err_omp_unknown_reduction_identifier)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013239 << Type << ReductionIdRange;
13240 continue;
13241 }
13242
13243 // OpenMP [2.14.3.6, reduction clause, Restrictions]
13244 // The type of a list item that appears in a reduction clause must be valid
13245 // for the reduction-identifier. For a max or min reduction in C, the type
13246 // of the list item must be an allowed arithmetic data type: char, int,
13247 // float, double, or _Bool, possibly modified with long, short, signed, or
13248 // unsigned. For a max or min reduction in C++, the type of the list item
13249 // must be an allowed arithmetic data type: char, wchar_t, int, float,
13250 // double, or bool, possibly modified with long, short, signed, or unsigned.
13251 if (DeclareReductionRef.isUnset()) {
13252 if ((BOK == BO_GT || BOK == BO_LT) &&
13253 !(Type->isScalarType() ||
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013254 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
13255 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
Alexey Bataev169d96a2017-07-18 20:17:46 +000013256 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013257 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013258 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
13259 VarDecl::DeclarationOnly;
13260 S.Diag(D->getLocation(),
13261 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013262 << D;
13263 }
13264 continue;
13265 }
13266 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013267 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000013268 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
13269 << getOpenMPClauseName(ClauseKind);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013270 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013271 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
13272 VarDecl::DeclarationOnly;
13273 S.Diag(D->getLocation(),
13274 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013275 << D;
13276 }
13277 continue;
13278 }
13279 }
13280
Alexey Bataev794ba0d2015-04-10 10:43:45 +000013281 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000013282 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
13283 D->hasAttrs() ? &D->getAttrs() : nullptr);
13284 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
13285 D->hasAttrs() ? &D->getAttrs() : nullptr);
13286 QualType PrivateTy = Type;
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000013287
13288 // Try if we can determine constant lengths for all array sections and avoid
13289 // the VLA.
13290 bool ConstantLengthOASE = false;
13291 if (OASE) {
13292 bool SingleElement;
13293 llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
Alexey Bataeve3727102018-04-18 15:57:46 +000013294 ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000013295 Context, OASE, SingleElement, ArraySizes);
13296
13297 // If we don't have a single element, we must emit a constant array type.
13298 if (ConstantLengthOASE && !SingleElement) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013299 for (llvm::APSInt &Size : ArraySizes)
Richard Smith772e2662019-10-04 01:25:59 +000013300 PrivateTy = Context.getConstantArrayType(PrivateTy, Size, nullptr,
13301 ArrayType::Normal,
13302 /*IndexTypeQuals=*/0);
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000013303 }
13304 }
13305
13306 if ((OASE && !ConstantLengthOASE) ||
Jonas Hahnfeld96087f32017-11-02 13:30:42 +000013307 (!OASE && !ASE &&
Alexey Bataev60da77e2016-02-29 05:54:20 +000013308 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev85260312019-07-11 20:35:31 +000013309 if (!Context.getTargetInfo().isVLASupported()) {
13310 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective())) {
13311 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
13312 S.Diag(ELoc, diag::note_vla_unsupported);
13313 } else {
13314 S.targetDiag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
13315 S.targetDiag(ELoc, diag::note_vla_unsupported);
13316 }
Jonas Hahnfeld87d44262017-11-18 21:00:46 +000013317 continue;
13318 }
David Majnemer9d168222016-08-05 17:44:54 +000013319 // For arrays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +000013320 // Create pseudo array type for private copy. The size for this array will
13321 // be generated during codegen.
13322 // For array subscripts or single variables Private Ty is the same as Type
13323 // (type of the variable or single array element).
13324 PrivateTy = Context.getVariableArrayType(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013325 Type,
Alexey Bataevd070a582017-10-25 15:54:04 +000013326 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
Alexey Bataevf24e7b12015-10-08 09:10:53 +000013327 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +000013328 } else if (!ASE && !OASE &&
Alexey Bataeve3727102018-04-18 15:57:46 +000013329 Context.getAsArrayType(D->getType().getNonReferenceType())) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000013330 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000013331 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000013332 // Private copy.
Alexey Bataeve3727102018-04-18 15:57:46 +000013333 VarDecl *PrivateVD =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000013334 buildVarDecl(S, ELoc, PrivateTy, D->getName(),
13335 D->hasAttrs() ? &D->getAttrs() : nullptr,
13336 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000013337 // Add initializer for private variable.
13338 Expr *Init = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000013339 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
13340 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013341 if (DeclareReductionRef.isUsable()) {
13342 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
13343 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
13344 if (DRD->getInitializer()) {
13345 Init = DRDRef;
13346 RHSVD->setInit(DRDRef);
13347 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000013348 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013349 } else {
13350 switch (BOK) {
13351 case BO_Add:
13352 case BO_Xor:
13353 case BO_Or:
13354 case BO_LOr:
13355 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
13356 if (Type->isScalarType() || Type->isAnyComplexType())
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013357 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013358 break;
13359 case BO_Mul:
13360 case BO_LAnd:
13361 if (Type->isScalarType() || Type->isAnyComplexType()) {
13362 // '*' and '&&' reduction ops - initializer is '1'.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013363 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +000013364 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013365 break;
13366 case BO_And: {
13367 // '&' reduction op - initializer is '~0'.
13368 QualType OrigType = Type;
13369 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
13370 Type = ComplexTy->getElementType();
13371 if (Type->isRealFloatingType()) {
13372 llvm::APFloat InitValue =
13373 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
13374 /*isIEEE=*/true);
13375 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
13376 Type, ELoc);
13377 } else if (Type->isScalarType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013378 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013379 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
13380 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
13381 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
13382 }
13383 if (Init && OrigType->isAnyComplexType()) {
13384 // Init = 0xFFFF + 0xFFFFi;
13385 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013386 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013387 }
13388 Type = OrigType;
13389 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000013390 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013391 case BO_LT:
13392 case BO_GT: {
13393 // 'min' reduction op - initializer is 'Largest representable number in
13394 // the reduction list item type'.
13395 // 'max' reduction op - initializer is 'Least representable number in
13396 // the reduction list item type'.
13397 if (Type->isIntegerType() || Type->isPointerType()) {
13398 bool IsSigned = Type->hasSignedIntegerRepresentation();
Alexey Bataeve3727102018-04-18 15:57:46 +000013399 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013400 QualType IntTy =
13401 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
13402 llvm::APInt InitValue =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013403 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
13404 : llvm::APInt::getMinValue(Size)
13405 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
13406 : llvm::APInt::getMaxValue(Size);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013407 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
13408 if (Type->isPointerType()) {
13409 // Cast to pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +000013410 ExprResult CastExpr = S.BuildCStyleCastExpr(
Alexey Bataevd070a582017-10-25 15:54:04 +000013411 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013412 if (CastExpr.isInvalid())
13413 continue;
13414 Init = CastExpr.get();
13415 }
13416 } else if (Type->isRealFloatingType()) {
13417 llvm::APFloat InitValue = llvm::APFloat::getLargest(
13418 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
13419 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
13420 Type, ELoc);
13421 }
13422 break;
13423 }
13424 case BO_PtrMemD:
13425 case BO_PtrMemI:
13426 case BO_MulAssign:
13427 case BO_Div:
13428 case BO_Rem:
13429 case BO_Sub:
13430 case BO_Shl:
13431 case BO_Shr:
13432 case BO_LE:
13433 case BO_GE:
13434 case BO_EQ:
13435 case BO_NE:
Richard Smithc70f1d62017-12-14 15:16:18 +000013436 case BO_Cmp:
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013437 case BO_AndAssign:
13438 case BO_XorAssign:
13439 case BO_OrAssign:
13440 case BO_Assign:
13441 case BO_AddAssign:
13442 case BO_SubAssign:
13443 case BO_DivAssign:
13444 case BO_RemAssign:
13445 case BO_ShlAssign:
13446 case BO_ShrAssign:
13447 case BO_Comma:
13448 llvm_unreachable("Unexpected reduction operation");
13449 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +000013450 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013451 if (Init && DeclareReductionRef.isUnset())
13452 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
13453 else if (!Init)
13454 S.ActOnUninitializedDecl(RHSVD);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013455 if (RHSVD->isInvalidDecl())
13456 continue;
Alexey Bataev09232662019-04-04 17:28:22 +000013457 if (!RHSVD->hasInit() &&
13458 (DeclareReductionRef.isUnset() || !S.LangOpts.CPlusPlus)) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013459 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
13460 << Type << ReductionIdRange;
13461 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
13462 VarDecl::DeclarationOnly;
13463 S.Diag(D->getLocation(),
13464 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000013465 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000013466 continue;
13467 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000013468 // Store initializer for single element in private copy. Will be used during
13469 // codegen.
13470 PrivateVD->setInit(RHSVD->getInit());
13471 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataeve3727102018-04-18 15:57:46 +000013472 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013473 ExprResult ReductionOp;
13474 if (DeclareReductionRef.isUsable()) {
13475 QualType RedTy = DeclareReductionRef.get()->getType();
13476 QualType PtrRedTy = Context.getPointerType(RedTy);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013477 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
13478 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013479 if (!BasePath.empty()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013480 LHS = S.DefaultLvalueConversion(LHS.get());
13481 RHS = S.DefaultLvalueConversion(RHS.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013482 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
13483 CK_UncheckedDerivedToBase, LHS.get(),
13484 &BasePath, LHS.get()->getValueKind());
13485 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
13486 CK_UncheckedDerivedToBase, RHS.get(),
13487 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +000013488 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013489 FunctionProtoType::ExtProtoInfo EPI;
13490 QualType Params[] = {PtrRedTy, PtrRedTy};
13491 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
13492 auto *OVE = new (Context) OpaqueValueExpr(
13493 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013494 S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013495 Expr *Args[] = {LHS.get(), RHS.get()};
Bruno Riccic5885cf2018-12-21 15:20:32 +000013496 ReductionOp =
13497 CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013498 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013499 ReductionOp = S.BuildBinOp(
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013500 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013501 if (ReductionOp.isUsable()) {
13502 if (BOK != BO_LT && BOK != BO_GT) {
13503 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013504 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013505 BO_Assign, LHSDRE, ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013506 } else {
Alexey Bataevd070a582017-10-25 15:54:04 +000013507 auto *ConditionalOp = new (Context)
13508 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
13509 Type, VK_LValue, OK_Ordinary);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013510 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013511 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013512 BO_Assign, LHSDRE, ConditionalOp);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013513 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000013514 if (ReductionOp.isUsable())
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000013515 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(),
13516 /*DiscardedValue*/ false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013517 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000013518 if (!ReductionOp.isUsable())
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013519 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +000013520 }
13521
Alexey Bataevfa312f32017-07-21 18:48:21 +000013522 // OpenMP [2.15.4.6, Restrictions, p.2]
13523 // A list item that appears in an in_reduction clause of a task construct
13524 // must appear in a task_reduction clause of a construct associated with a
13525 // taskgroup region that includes the participating task in its taskgroup
13526 // set. The construct associated with the innermost region that meets this
13527 // condition must specify the same reduction-identifier as the in_reduction
13528 // clause.
13529 if (ClauseKind == OMPC_in_reduction) {
Alexey Bataevfa312f32017-07-21 18:48:21 +000013530 SourceRange ParentSR;
13531 BinaryOperatorKind ParentBOK;
13532 const Expr *ParentReductionOp;
Alexey Bataev88202be2017-07-27 13:20:36 +000013533 Expr *ParentBOKTD, *ParentReductionOpTD;
Alexey Bataevf189cb72017-07-24 14:52:13 +000013534 DSAStackTy::DSAVarData ParentBOKDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000013535 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
13536 ParentBOKTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000013537 DSAStackTy::DSAVarData ParentReductionOpDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000013538 Stack->getTopMostTaskgroupReductionData(
13539 D, ParentSR, ParentReductionOp, ParentReductionOpTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000013540 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
13541 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
13542 if (!IsParentBOK && !IsParentReductionOp) {
13543 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
13544 continue;
13545 }
Alexey Bataevfa312f32017-07-21 18:48:21 +000013546 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
13547 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
13548 IsParentReductionOp) {
13549 bool EmitError = true;
13550 if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
13551 llvm::FoldingSetNodeID RedId, ParentRedId;
13552 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
13553 DeclareReductionRef.get()->Profile(RedId, Context,
13554 /*Canonical=*/true);
13555 EmitError = RedId != ParentRedId;
13556 }
13557 if (EmitError) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013558 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfa312f32017-07-21 18:48:21 +000013559 diag::err_omp_reduction_identifier_mismatch)
13560 << ReductionIdRange << RefExpr->getSourceRange();
13561 S.Diag(ParentSR.getBegin(),
13562 diag::note_omp_previous_reduction_identifier)
Alexey Bataevf189cb72017-07-24 14:52:13 +000013563 << ParentSR
13564 << (IsParentBOK ? ParentBOKDSA.RefExpr
13565 : ParentReductionOpDSA.RefExpr)
13566 ->getSourceRange();
Alexey Bataevfa312f32017-07-21 18:48:21 +000013567 continue;
13568 }
13569 }
Alexey Bataev88202be2017-07-27 13:20:36 +000013570 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
13571 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
Alexey Bataevfa312f32017-07-21 18:48:21 +000013572 }
13573
Alexey Bataev60da77e2016-02-29 05:54:20 +000013574 DeclRefExpr *Ref = nullptr;
13575 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013576 if (!VD && !S.CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000013577 if (ASE || OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013578 TransformExprToCaptures RebuildToCapture(S, D);
Alexey Bataev60da77e2016-02-29 05:54:20 +000013579 VarsExpr =
13580 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
13581 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +000013582 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013583 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +000013584 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013585 if (!S.isOpenMPCapturedDecl(D)) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013586 RD.ExprCaptures.emplace_back(Ref->getDecl());
Alexey Bataev5a3af132016-03-29 08:58:54 +000013587 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013588 ExprResult RefRes = S.DefaultLvalueConversion(Ref);
Alexey Bataev5a3af132016-03-29 08:58:54 +000013589 if (!RefRes.isUsable())
13590 continue;
13591 ExprResult PostUpdateRes =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013592 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
13593 RefRes.get());
Alexey Bataev5a3af132016-03-29 08:58:54 +000013594 if (!PostUpdateRes.isUsable())
13595 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013596 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
13597 Stack->getCurrentDirective() == OMPD_taskgroup) {
13598 S.Diag(RefExpr->getExprLoc(),
13599 diag::err_omp_reduction_non_addressable_expression)
Alexey Bataevbcd0ae02017-07-11 19:16:44 +000013600 << RefExpr->getSourceRange();
13601 continue;
13602 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013603 RD.ExprPostUpdates.emplace_back(
13604 S.IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +000013605 }
13606 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000013607 }
Alexey Bataev169d96a2017-07-18 20:17:46 +000013608 // All reduction items are still marked as reduction (to do not increase
13609 // code base size).
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013610 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
Alexey Bataevf189cb72017-07-24 14:52:13 +000013611 if (CurrDir == OMPD_taskgroup) {
13612 if (DeclareReductionRef.isUsable())
Alexey Bataev3b1b8952017-07-25 15:53:26 +000013613 Stack->addTaskgroupReductionData(D, ReductionIdRange,
13614 DeclareReductionRef.get());
Alexey Bataevf189cb72017-07-24 14:52:13 +000013615 else
Alexey Bataev3b1b8952017-07-25 15:53:26 +000013616 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
Alexey Bataevf189cb72017-07-24 14:52:13 +000013617 }
Alexey Bataev88202be2017-07-27 13:20:36 +000013618 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
13619 TaskgroupDescriptor);
Alexey Bataevc5e02582014-06-16 07:08:35 +000013620 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013621 return RD.Vars.empty();
13622}
Alexey Bataevc5e02582014-06-16 07:08:35 +000013623
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013624OMPClause *Sema::ActOnOpenMPReductionClause(
13625 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
13626 SourceLocation ColonLoc, SourceLocation EndLoc,
13627 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
13628 ArrayRef<Expr *> UnresolvedReductions) {
13629 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000013630 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
Alexey Bataev169d96a2017-07-18 20:17:46 +000013631 StartLoc, LParenLoc, ColonLoc, EndLoc,
13632 ReductionIdScopeSpec, ReductionId,
13633 UnresolvedReductions, RD))
Alexey Bataevc5e02582014-06-16 07:08:35 +000013634 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +000013635
Alexey Bataevc5e02582014-06-16 07:08:35 +000013636 return OMPReductionClause::Create(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013637 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
13638 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
13639 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
13640 buildPreInits(Context, RD.ExprCaptures),
13641 buildPostUpdate(*this, RD.ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +000013642}
13643
Alexey Bataev169d96a2017-07-18 20:17:46 +000013644OMPClause *Sema::ActOnOpenMPTaskReductionClause(
13645 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
13646 SourceLocation ColonLoc, SourceLocation EndLoc,
13647 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
13648 ArrayRef<Expr *> UnresolvedReductions) {
13649 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000013650 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList,
13651 StartLoc, LParenLoc, ColonLoc, EndLoc,
13652 ReductionIdScopeSpec, ReductionId,
Alexey Bataev169d96a2017-07-18 20:17:46 +000013653 UnresolvedReductions, RD))
13654 return nullptr;
13655
13656 return OMPTaskReductionClause::Create(
13657 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
13658 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
13659 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
13660 buildPreInits(Context, RD.ExprCaptures),
13661 buildPostUpdate(*this, RD.ExprPostUpdates));
13662}
13663
Alexey Bataevfa312f32017-07-21 18:48:21 +000013664OMPClause *Sema::ActOnOpenMPInReductionClause(
13665 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
13666 SourceLocation ColonLoc, SourceLocation EndLoc,
13667 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
13668 ArrayRef<Expr *> UnresolvedReductions) {
13669 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000013670 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
Alexey Bataevfa312f32017-07-21 18:48:21 +000013671 StartLoc, LParenLoc, ColonLoc, EndLoc,
13672 ReductionIdScopeSpec, ReductionId,
13673 UnresolvedReductions, RD))
13674 return nullptr;
13675
13676 return OMPInReductionClause::Create(
13677 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
13678 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
Alexey Bataev88202be2017-07-27 13:20:36 +000013679 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
Alexey Bataevfa312f32017-07-21 18:48:21 +000013680 buildPreInits(Context, RD.ExprCaptures),
13681 buildPostUpdate(*this, RD.ExprPostUpdates));
13682}
13683
Alexey Bataevecba70f2016-04-12 11:02:11 +000013684bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
13685 SourceLocation LinLoc) {
13686 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
13687 LinKind == OMPC_LINEAR_unknown) {
13688 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
13689 return true;
13690 }
13691 return false;
13692}
13693
Alexey Bataeve3727102018-04-18 15:57:46 +000013694bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
Alexey Bataevecba70f2016-04-12 11:02:11 +000013695 OpenMPLinearClauseKind LinKind,
13696 QualType Type) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013697 const auto *VD = dyn_cast_or_null<VarDecl>(D);
Alexey Bataevecba70f2016-04-12 11:02:11 +000013698 // A variable must not have an incomplete type or a reference type.
13699 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
13700 return true;
13701 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
13702 !Type->isReferenceType()) {
13703 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
13704 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
13705 return true;
13706 }
13707 Type = Type.getNonReferenceType();
13708
Joel E. Dennybae586f2019-01-04 22:12:13 +000013709 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
13710 // A variable that is privatized must not have a const-qualified type
13711 // unless it is of class type with a mutable member. This restriction does
13712 // not apply to the firstprivate clause.
13713 if (rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc))
Alexey Bataevecba70f2016-04-12 11:02:11 +000013714 return true;
Alexey Bataevecba70f2016-04-12 11:02:11 +000013715
13716 // A list item must be of integral or pointer type.
13717 Type = Type.getUnqualifiedType().getCanonicalType();
13718 const auto *Ty = Type.getTypePtrOrNull();
13719 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
13720 !Ty->isPointerType())) {
13721 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
13722 if (D) {
13723 bool IsDecl =
13724 !VD ||
13725 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
13726 Diag(D->getLocation(),
13727 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
13728 << D;
13729 }
13730 return true;
13731 }
13732 return false;
13733}
13734
Alexey Bataev182227b2015-08-20 10:54:39 +000013735OMPClause *Sema::ActOnOpenMPLinearClause(
13736 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
13737 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
13738 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +000013739 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000013740 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +000013741 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +000013742 SmallVector<Decl *, 4> ExprCaptures;
13743 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +000013744 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +000013745 LinKind = OMPC_LINEAR_val;
Alexey Bataeve3727102018-04-18 15:57:46 +000013746 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000013747 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +000013748 SourceLocation ELoc;
13749 SourceRange ERange;
13750 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000013751 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000013752 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +000013753 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000013754 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000013755 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +000013756 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +000013757 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +000013758 ValueDecl *D = Res.first;
13759 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +000013760 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +000013761
Alexey Bataev2bbf7212016-03-03 03:52:24 +000013762 QualType Type = D->getType();
13763 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +000013764
13765 // OpenMP [2.14.3.7, linear clause]
13766 // A list-item cannot appear in more than one linear clause.
13767 // A list-item that appears in a linear clause cannot appear in any
13768 // other data-sharing attribute clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000013769 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman8dba6642014-04-22 13:09:42 +000013770 if (DVar.RefExpr) {
13771 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
13772 << getOpenMPClauseName(OMPC_linear);
Alexey Bataeve3727102018-04-18 15:57:46 +000013773 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +000013774 continue;
13775 }
13776
Alexey Bataevecba70f2016-04-12 11:02:11 +000013777 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +000013778 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +000013779 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +000013780
Alexey Bataevbd9fec12015-08-18 06:47:21 +000013781 // Build private copy of original var.
Alexey Bataeve3727102018-04-18 15:57:46 +000013782 VarDecl *Private =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000013783 buildVarDecl(*this, ELoc, Type, D->getName(),
13784 D->hasAttrs() ? &D->getAttrs() : nullptr,
13785 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000013786 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +000013787 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +000013788 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000013789 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +000013790 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000013791 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000013792 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000013793 if (!isOpenMPCapturedDecl(D)) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000013794 ExprCaptures.push_back(Ref->getDecl());
13795 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
13796 ExprResult RefRes = DefaultLvalueConversion(Ref);
13797 if (!RefRes.isUsable())
13798 continue;
13799 ExprResult PostUpdateRes =
13800 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
13801 SimpleRefExpr, RefRes.get());
13802 if (!PostUpdateRes.isUsable())
13803 continue;
13804 ExprPostUpdates.push_back(
13805 IgnoredValueConversions(PostUpdateRes.get()).get());
13806 }
13807 }
13808 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000013809 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +000013810 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000013811 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +000013812 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000013813 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000013814 /*DirectInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000013815 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000013816
13817 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000013818 Vars.push_back((VD || CurContext->isDependentContext())
13819 ? RefExpr->IgnoreParens()
13820 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000013821 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +000013822 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +000013823 }
13824
13825 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000013826 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000013827
13828 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +000013829 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000013830 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
13831 !Step->isInstantiationDependent() &&
13832 !Step->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013833 SourceLocation StepLoc = Step->getBeginLoc();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000013834 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +000013835 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000013836 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +000013837 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +000013838
Alexander Musman3276a272015-03-21 10:12:56 +000013839 // Build var to save the step value.
13840 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +000013841 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +000013842 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +000013843 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +000013844 ExprResult CalcStep =
13845 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000013846 CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false);
Alexander Musman3276a272015-03-21 10:12:56 +000013847
Alexander Musman8dba6642014-04-22 13:09:42 +000013848 // Warn about zero linear step (it would be probably better specified as
13849 // making corresponding variables 'const').
13850 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +000013851 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
13852 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +000013853 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
13854 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +000013855 if (!IsConstant && CalcStep.isUsable()) {
13856 // Calculate the step beforehand instead of doing this on each iteration.
13857 // (This is not used if the number of iterations may be kfold-ed).
13858 CalcStepExpr = CalcStep.get();
13859 }
Alexander Musman8dba6642014-04-22 13:09:42 +000013860 }
13861
Alexey Bataev182227b2015-08-20 10:54:39 +000013862 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
13863 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +000013864 StepExpr, CalcStepExpr,
13865 buildPreInits(Context, ExprCaptures),
13866 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +000013867}
13868
Alexey Bataev5dff95c2016-04-22 03:56:56 +000013869static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
13870 Expr *NumIterations, Sema &SemaRef,
13871 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +000013872 // Walk the vars and build update/final expressions for the CodeGen.
13873 SmallVector<Expr *, 8> Updates;
13874 SmallVector<Expr *, 8> Finals;
Alexey Bataev195ae902019-08-08 13:42:45 +000013875 SmallVector<Expr *, 8> UsedExprs;
Alexander Musman3276a272015-03-21 10:12:56 +000013876 Expr *Step = Clause.getStep();
13877 Expr *CalcStep = Clause.getCalcStep();
13878 // OpenMP [2.14.3.7, linear clause]
13879 // If linear-step is not specified it is assumed to be 1.
Alexey Bataeve3727102018-04-18 15:57:46 +000013880 if (!Step)
Alexander Musman3276a272015-03-21 10:12:56 +000013881 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013882 else if (CalcStep)
Alexander Musman3276a272015-03-21 10:12:56 +000013883 Step = cast<BinaryOperator>(CalcStep)->getLHS();
13884 bool HasErrors = false;
13885 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +000013886 auto CurPrivate = Clause.privates().begin();
Alexey Bataeve3727102018-04-18 15:57:46 +000013887 OpenMPLinearClauseKind LinKind = Clause.getModifier();
13888 for (Expr *RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +000013889 SourceLocation ELoc;
13890 SourceRange ERange;
13891 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000013892 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000013893 ValueDecl *D = Res.first;
13894 if (Res.second || !D) {
13895 Updates.push_back(nullptr);
13896 Finals.push_back(nullptr);
13897 HasErrors = true;
13898 continue;
13899 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +000013900 auto &&Info = Stack->isLoopControlVariable(D);
Alexey Bataev2b86f212017-11-29 21:31:48 +000013901 // OpenMP [2.15.11, distribute simd Construct]
13902 // A list item may not appear in a linear clause, unless it is the loop
13903 // iteration variable.
13904 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
13905 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
13906 SemaRef.Diag(ELoc,
13907 diag::err_omp_linear_distribute_var_non_loop_iteration);
13908 Updates.push_back(nullptr);
13909 Finals.push_back(nullptr);
13910 HasErrors = true;
13911 continue;
13912 }
Alexander Musman3276a272015-03-21 10:12:56 +000013913 Expr *InitExpr = *CurInit;
13914
13915 // Build privatized reference to the current linear var.
David Majnemer9d168222016-08-05 17:44:54 +000013916 auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000013917 Expr *CapturedRef;
13918 if (LinKind == OMPC_LINEAR_uval)
13919 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
13920 else
13921 CapturedRef =
13922 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
13923 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
13924 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000013925
13926 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000013927 ExprResult Update;
Alexey Bataeve3727102018-04-18 15:57:46 +000013928 if (!Info.first)
Alexey Bataevf8be4762019-08-14 19:30:06 +000013929 Update = buildCounterUpdate(
13930 SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, InitExpr, IV, Step,
13931 /*Subtract=*/false, /*IsNonRectangularLB=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000013932 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000013933 Update = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013934 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000013935 /*DiscardedValue*/ false);
Alexander Musman3276a272015-03-21 10:12:56 +000013936
13937 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000013938 ExprResult Final;
Alexey Bataeve3727102018-04-18 15:57:46 +000013939 if (!Info.first)
13940 Final =
13941 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataevf8be4762019-08-14 19:30:06 +000013942 InitExpr, NumIterations, Step, /*Subtract=*/false,
13943 /*IsNonRectangularLB=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000013944 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000013945 Final = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013946 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000013947 /*DiscardedValue*/ false);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000013948
Alexander Musman3276a272015-03-21 10:12:56 +000013949 if (!Update.isUsable() || !Final.isUsable()) {
13950 Updates.push_back(nullptr);
13951 Finals.push_back(nullptr);
Alexey Bataev195ae902019-08-08 13:42:45 +000013952 UsedExprs.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +000013953 HasErrors = true;
13954 } else {
13955 Updates.push_back(Update.get());
13956 Finals.push_back(Final.get());
Alexey Bataev195ae902019-08-08 13:42:45 +000013957 if (!Info.first)
13958 UsedExprs.push_back(SimpleRefExpr);
Alexander Musman3276a272015-03-21 10:12:56 +000013959 }
Richard Trieucc3949d2016-02-18 22:34:54 +000013960 ++CurInit;
13961 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +000013962 }
Alexey Bataev195ae902019-08-08 13:42:45 +000013963 if (Expr *S = Clause.getStep())
13964 UsedExprs.push_back(S);
13965 // Fill the remaining part with the nullptr.
13966 UsedExprs.append(Clause.varlist_size() + 1 - UsedExprs.size(), nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +000013967 Clause.setUpdates(Updates);
13968 Clause.setFinals(Finals);
Alexey Bataev195ae902019-08-08 13:42:45 +000013969 Clause.setUsedExprs(UsedExprs);
Alexander Musman3276a272015-03-21 10:12:56 +000013970 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +000013971}
13972
Alexander Musmanf0d76e72014-05-29 14:36:25 +000013973OMPClause *Sema::ActOnOpenMPAlignedClause(
13974 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
13975 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000013976 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000013977 for (Expr *RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +000013978 assert(RefExpr && "NULL expr in OpenMP linear clause.");
13979 SourceLocation ELoc;
13980 SourceRange ERange;
13981 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000013982 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev1efd1662016-03-29 10:59:56 +000013983 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000013984 // It will be analyzed later.
13985 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000013986 }
Alexey Bataev1efd1662016-03-29 10:59:56 +000013987 ValueDecl *D = Res.first;
13988 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000013989 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000013990
Alexey Bataev1efd1662016-03-29 10:59:56 +000013991 QualType QType = D->getType();
13992 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000013993
13994 // OpenMP [2.8.1, simd construct, Restrictions]
13995 // The type of list items appearing in the aligned clause must be
13996 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000013997 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +000013998 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +000013999 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000014000 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +000014001 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000014002 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +000014003 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +000014004 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +000014005 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +000014006 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +000014007 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000014008 continue;
14009 }
14010
14011 // OpenMP [2.8.1, simd construct, Restrictions]
14012 // A list-item cannot appear in more than one aligned clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000014013 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +000014014 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000014015 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
14016 << getOpenMPClauseName(OMPC_aligned);
14017 continue;
14018 }
14019
Alexey Bataev1efd1662016-03-29 10:59:56 +000014020 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000014021 if (!VD && isOpenMPCapturedDecl(D))
Alexey Bataev1efd1662016-03-29 10:59:56 +000014022 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
14023 Vars.push_back(DefaultFunctionArrayConversion(
14024 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
14025 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +000014026 }
14027
14028 // OpenMP [2.8.1, simd construct, Description]
14029 // The parameter of the aligned clause, alignment, must be a constant
14030 // positive integer expression.
14031 // If no optional parameter is specified, implementation-defined default
14032 // alignments for SIMD instructions on the target platforms are assumed.
14033 if (Alignment != nullptr) {
14034 ExprResult AlignResult =
14035 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
14036 if (AlignResult.isInvalid())
14037 return nullptr;
14038 Alignment = AlignResult.get();
14039 }
14040 if (Vars.empty())
14041 return nullptr;
14042
14043 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
14044 EndLoc, Vars, Alignment);
14045}
14046
Alexey Bataevd48bcd82014-03-31 03:36:38 +000014047OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
14048 SourceLocation StartLoc,
14049 SourceLocation LParenLoc,
14050 SourceLocation EndLoc) {
14051 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +000014052 SmallVector<Expr *, 8> SrcExprs;
14053 SmallVector<Expr *, 8> DstExprs;
14054 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000014055 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000014056 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
14057 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +000014058 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000014059 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000014060 SrcExprs.push_back(nullptr);
14061 DstExprs.push_back(nullptr);
14062 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000014063 continue;
14064 }
14065
Alexey Bataeved09d242014-05-28 05:53:51 +000014066 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000014067 // OpenMP [2.1, C/C++]
14068 // A list item is a variable name.
14069 // OpenMP [2.14.4.1, Restrictions, p.1]
14070 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +000014071 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000014072 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000014073 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
14074 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000014075 continue;
14076 }
14077
14078 Decl *D = DE->getDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000014079 auto *VD = cast<VarDecl>(D);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000014080
14081 QualType Type = VD->getType();
14082 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
14083 // It will be analyzed later.
14084 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000014085 SrcExprs.push_back(nullptr);
14086 DstExprs.push_back(nullptr);
14087 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000014088 continue;
14089 }
14090
14091 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
14092 // A list item that appears in a copyin clause must be threadprivate.
14093 if (!DSAStack->isThreadPrivate(VD)) {
14094 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +000014095 << getOpenMPClauseName(OMPC_copyin)
14096 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000014097 continue;
14098 }
14099
14100 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
14101 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +000014102 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +000014103 // operator for the class type.
Alexey Bataeve3727102018-04-18 15:57:46 +000014104 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
14105 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000014106 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000014107 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000014108 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +000014109 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +000014110 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000014111 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst",
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000014112 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000014113 DeclRefExpr *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +000014114 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +000014115 // For arrays generate assignment operation for single element and replace
14116 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000014117 ExprResult AssignmentOp =
14118 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr,
14119 PseudoSrcExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000014120 if (AssignmentOp.isInvalid())
14121 continue;
14122 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000014123 /*DiscardedValue*/ false);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000014124 if (AssignmentOp.isInvalid())
14125 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000014126
14127 DSAStack->addDSA(VD, DE, OMPC_copyin);
14128 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000014129 SrcExprs.push_back(PseudoSrcExpr);
14130 DstExprs.push_back(PseudoDstExpr);
14131 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +000014132 }
14133
Alexey Bataeved09d242014-05-28 05:53:51 +000014134 if (Vars.empty())
14135 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000014136
Alexey Bataevf56f98c2015-04-16 05:39:01 +000014137 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
14138 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000014139}
14140
Alexey Bataevbae9a792014-06-27 10:37:06 +000014141OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
14142 SourceLocation StartLoc,
14143 SourceLocation LParenLoc,
14144 SourceLocation EndLoc) {
14145 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +000014146 SmallVector<Expr *, 8> SrcExprs;
14147 SmallVector<Expr *, 8> DstExprs;
14148 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000014149 for (Expr *RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +000014150 assert(RefExpr && "NULL expr in OpenMP linear clause.");
14151 SourceLocation ELoc;
14152 SourceRange ERange;
14153 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000014154 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataeve122da12016-03-17 10:50:17 +000014155 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000014156 // It will be analyzed later.
14157 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000014158 SrcExprs.push_back(nullptr);
14159 DstExprs.push_back(nullptr);
14160 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +000014161 }
Alexey Bataeve122da12016-03-17 10:50:17 +000014162 ValueDecl *D = Res.first;
14163 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +000014164 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000014165
Alexey Bataeve122da12016-03-17 10:50:17 +000014166 QualType Type = D->getType();
14167 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +000014168
14169 // OpenMP [2.14.4.2, Restrictions, p.2]
14170 // A list item that appears in a copyprivate clause may not appear in a
14171 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +000014172 if (!VD || !DSAStack->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000014173 DSAStackTy::DSAVarData DVar =
14174 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000014175 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
14176 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000014177 Diag(ELoc, diag::err_omp_wrong_dsa)
14178 << getOpenMPClauseName(DVar.CKind)
14179 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000014180 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000014181 continue;
14182 }
14183
14184 // OpenMP [2.11.4.2, Restrictions, p.1]
14185 // All list items that appear in a copyprivate clause must be either
14186 // threadprivate or private in the enclosing context.
14187 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +000014188 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000014189 if (DVar.CKind == OMPC_shared) {
14190 Diag(ELoc, diag::err_omp_required_access)
14191 << getOpenMPClauseName(OMPC_copyprivate)
14192 << "threadprivate or private in the enclosing context";
Alexey Bataeve3727102018-04-18 15:57:46 +000014193 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000014194 continue;
14195 }
14196 }
14197 }
14198
Alexey Bataev7a3e5852015-05-19 08:19:24 +000014199 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000014200 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000014201 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000014202 << getOpenMPClauseName(OMPC_copyprivate) << Type
14203 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000014204 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000014205 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000014206 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000014207 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000014208 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000014209 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000014210 continue;
14211 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000014212
Alexey Bataevbae9a792014-06-27 10:37:06 +000014213 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
14214 // A variable of class type (or array thereof) that appears in a
14215 // copyin clause requires an accessible, unambiguous copy assignment
14216 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000014217 Type = Context.getBaseElementType(Type.getNonReferenceType())
14218 .getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000014219 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000014220 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src",
Alexey Bataeve122da12016-03-17 10:50:17 +000014221 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000014222 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
14223 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000014224 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst",
Alexey Bataeve122da12016-03-17 10:50:17 +000014225 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000014226 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
14227 ExprResult AssignmentOp = BuildBinOp(
14228 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000014229 if (AssignmentOp.isInvalid())
14230 continue;
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000014231 AssignmentOp =
14232 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000014233 if (AssignmentOp.isInvalid())
14234 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000014235
14236 // No need to mark vars as copyprivate, they are already threadprivate or
14237 // implicitly private.
Alexey Bataeve3727102018-04-18 15:57:46 +000014238 assert(VD || isOpenMPCapturedDecl(D));
Alexey Bataeve122da12016-03-17 10:50:17 +000014239 Vars.push_back(
14240 VD ? RefExpr->IgnoreParens()
14241 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000014242 SrcExprs.push_back(PseudoSrcExpr);
14243 DstExprs.push_back(PseudoDstExpr);
14244 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000014245 }
14246
14247 if (Vars.empty())
14248 return nullptr;
14249
Alexey Bataeva63048e2015-03-23 06:18:07 +000014250 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
14251 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000014252}
14253
Alexey Bataev6125da92014-07-21 11:26:11 +000014254OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
14255 SourceLocation StartLoc,
14256 SourceLocation LParenLoc,
14257 SourceLocation EndLoc) {
14258 if (VarList.empty())
14259 return nullptr;
14260
14261 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
14262}
Alexey Bataevdea47612014-07-23 07:46:59 +000014263
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000014264OMPClause *
14265Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
14266 SourceLocation DepLoc, SourceLocation ColonLoc,
14267 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
14268 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000014269 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000014270 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000014271 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000014272 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000014273 return nullptr;
14274 }
14275 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000014276 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
14277 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000014278 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000014279 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000014280 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
14281 /*Last=*/OMPC_DEPEND_unknown, Except)
14282 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000014283 return nullptr;
14284 }
14285 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000014286 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000014287 llvm::APSInt DepCounter(/*BitWidth=*/32);
14288 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
Alexey Bataevf138fda2018-08-13 19:04:24 +000014289 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
14290 if (const Expr *OrderedCountExpr =
14291 DSAStack->getParentOrderedRegionParam().first) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000014292 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
14293 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000014294 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000014295 }
Alexey Bataeve3727102018-04-18 15:57:46 +000014296 for (Expr *RefExpr : VarList) {
Alexey Bataev17daedf2018-02-15 22:42:57 +000014297 assert(RefExpr && "NULL expr in OpenMP shared clause.");
14298 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
14299 // It will be analyzed later.
14300 Vars.push_back(RefExpr);
14301 continue;
14302 }
14303
14304 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +000014305 Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev17daedf2018-02-15 22:42:57 +000014306 if (DepKind == OMPC_DEPEND_sink) {
Alexey Bataevf138fda2018-08-13 19:04:24 +000014307 if (DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000014308 DepCounter >= TotalDepCount) {
14309 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
14310 continue;
14311 }
14312 ++DepCounter;
14313 // OpenMP [2.13.9, Summary]
14314 // depend(dependence-type : vec), where dependence-type is:
14315 // 'sink' and where vec is the iteration vector, which has the form:
14316 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
14317 // where n is the value specified by the ordered clause in the loop
14318 // directive, xi denotes the loop iteration variable of the i-th nested
14319 // loop associated with the loop directive, and di is a constant
14320 // non-negative integer.
14321 if (CurContext->isDependentContext()) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000014322 // It will be analyzed later.
14323 Vars.push_back(RefExpr);
14324 continue;
14325 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000014326 SimpleExpr = SimpleExpr->IgnoreImplicit();
14327 OverloadedOperatorKind OOK = OO_None;
14328 SourceLocation OOLoc;
14329 Expr *LHS = SimpleExpr;
14330 Expr *RHS = nullptr;
14331 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
14332 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
14333 OOLoc = BO->getOperatorLoc();
14334 LHS = BO->getLHS()->IgnoreParenImpCasts();
14335 RHS = BO->getRHS()->IgnoreParenImpCasts();
14336 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
14337 OOK = OCE->getOperator();
14338 OOLoc = OCE->getOperatorLoc();
14339 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
14340 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
14341 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
14342 OOK = MCE->getMethodDecl()
14343 ->getNameInfo()
14344 .getName()
14345 .getCXXOverloadedOperator();
14346 OOLoc = MCE->getCallee()->getExprLoc();
14347 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
14348 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000014349 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000014350 SourceLocation ELoc;
14351 SourceRange ERange;
Alexey Bataevbc529672018-09-28 19:33:14 +000014352 auto Res = getPrivateItem(*this, LHS, ELoc, ERange);
Alexey Bataev17daedf2018-02-15 22:42:57 +000014353 if (Res.second) {
14354 // It will be analyzed later.
14355 Vars.push_back(RefExpr);
14356 }
14357 ValueDecl *D = Res.first;
14358 if (!D)
14359 continue;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000014360
Alexey Bataev17daedf2018-02-15 22:42:57 +000014361 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
14362 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
14363 continue;
14364 }
14365 if (RHS) {
14366 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
14367 RHS, OMPC_depend, /*StrictlyPositive=*/false);
14368 if (RHSRes.isInvalid())
14369 continue;
14370 }
14371 if (!CurContext->isDependentContext() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000014372 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000014373 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +000014374 const ValueDecl *VD =
Alexey Bataev17daedf2018-02-15 22:42:57 +000014375 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
Alexey Bataeve3727102018-04-18 15:57:46 +000014376 if (VD)
Alexey Bataev17daedf2018-02-15 22:42:57 +000014377 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
14378 << 1 << VD;
Alexey Bataeve3727102018-04-18 15:57:46 +000014379 else
Alexey Bataev17daedf2018-02-15 22:42:57 +000014380 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
Alexey Bataev17daedf2018-02-15 22:42:57 +000014381 continue;
14382 }
Alexey Bataeve3727102018-04-18 15:57:46 +000014383 OpsOffs.emplace_back(RHS, OOK);
Alexey Bataev17daedf2018-02-15 22:42:57 +000014384 } else {
14385 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
14386 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
14387 (ASE &&
14388 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() &&
14389 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
14390 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
14391 << RefExpr->getSourceRange();
14392 continue;
14393 }
Richard Smith2e3ed4a2019-08-16 19:53:22 +000014394
14395 ExprResult Res;
14396 {
14397 Sema::TentativeAnalysisScope Trap(*this);
14398 Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf,
14399 RefExpr->IgnoreParenImpCasts());
14400 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000014401 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
14402 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
14403 << RefExpr->getSourceRange();
14404 continue;
14405 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000014406 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000014407 Vars.push_back(RefExpr->IgnoreParenImpCasts());
Alexey Bataeva636c7f2015-12-23 10:27:45 +000014408 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000014409
14410 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
14411 TotalDepCount > VarList.size() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000014412 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000014413 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
14414 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
14415 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
14416 }
14417 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
14418 Vars.empty())
14419 return nullptr;
14420
Alexey Bataev8b427062016-05-25 12:36:08 +000014421 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataevf138fda2018-08-13 19:04:24 +000014422 DepKind, DepLoc, ColonLoc, Vars,
14423 TotalDepCount.getZExtValue());
Alexey Bataev17daedf2018-02-15 22:42:57 +000014424 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
14425 DSAStack->isParentOrderedRegion())
Alexey Bataev8b427062016-05-25 12:36:08 +000014426 DSAStack->addDoacrossDependClause(C, OpsOffs);
14427 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000014428}
Michael Wonge710d542015-08-07 16:16:36 +000014429
14430OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
14431 SourceLocation LParenLoc,
14432 SourceLocation EndLoc) {
14433 Expr *ValExpr = Device;
Alexey Bataev931e19b2017-10-02 16:32:39 +000014434 Stmt *HelperValStmt = nullptr;
Michael Wonge710d542015-08-07 16:16:36 +000014435
Kelvin Lia15fb1a2015-11-27 18:47:36 +000014436 // OpenMP [2.9.1, Restrictions]
14437 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000014438 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
Alexey Bataeva0569352015-12-01 10:17:31 +000014439 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000014440 return nullptr;
14441
Alexey Bataev931e19b2017-10-02 16:32:39 +000014442 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000014443 OpenMPDirectiveKind CaptureRegion =
14444 getOpenMPCaptureRegionForClause(DKind, OMPC_device);
14445 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000014446 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000014447 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev931e19b2017-10-02 16:32:39 +000014448 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
14449 HelperValStmt = buildPreInits(Context, Captures);
14450 }
14451
Alexey Bataev8451efa2018-01-15 19:06:12 +000014452 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion,
14453 StartLoc, LParenLoc, EndLoc);
Michael Wonge710d542015-08-07 16:16:36 +000014454}
Kelvin Li0bff7af2015-11-23 05:32:03 +000014455
Alexey Bataeve3727102018-04-18 15:57:46 +000014456static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
Alexey Bataev95c23e72018-02-27 21:31:11 +000014457 DSAStackTy *Stack, QualType QTy,
14458 bool FullCheck = true) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000014459 NamedDecl *ND;
14460 if (QTy->isIncompleteType(&ND)) {
14461 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
14462 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +000014463 }
Alexey Bataev95c23e72018-02-27 21:31:11 +000014464 if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
14465 !QTy.isTrivialType(SemaRef.Context))
14466 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
Kelvin Li0bff7af2015-11-23 05:32:03 +000014467 return true;
14468}
14469
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000014470/// Return true if it can be proven that the provided array expression
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014471/// (array section or array subscript) does NOT specify the whole size of the
14472/// array whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000014473static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014474 const Expr *E,
14475 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000014476 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014477
14478 // If this is an array subscript, it refers to the whole size if the size of
14479 // the dimension is constant and equals 1. Also, an array section assumes the
14480 // format of an array subscript if no colon is used.
14481 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000014482 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014483 return ATy->getSize().getSExtValue() != 1;
14484 // Size can't be evaluated statically.
14485 return false;
14486 }
14487
14488 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000014489 const Expr *LowerBound = OASE->getLowerBound();
14490 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014491
14492 // If there is a lower bound that does not evaluates to zero, we are not
David Majnemer9d168222016-08-05 17:44:54 +000014493 // covering the whole dimension.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014494 if (LowerBound) {
Fangrui Song407659a2018-11-30 23:41:18 +000014495 Expr::EvalResult Result;
14496 if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014497 return false; // Can't get the integer value as a constant.
Fangrui Song407659a2018-11-30 23:41:18 +000014498
14499 llvm::APSInt ConstLowerBound = Result.Val.getInt();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014500 if (ConstLowerBound.getSExtValue())
14501 return true;
14502 }
14503
14504 // If we don't have a length we covering the whole dimension.
14505 if (!Length)
14506 return false;
14507
14508 // If the base is a pointer, we don't have a way to get the size of the
14509 // pointee.
14510 if (BaseQTy->isPointerType())
14511 return false;
14512
14513 // We can only check if the length is the same as the size of the dimension
14514 // if we have a constant array.
Alexey Bataeve3727102018-04-18 15:57:46 +000014515 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014516 if (!CATy)
14517 return false;
14518
Fangrui Song407659a2018-11-30 23:41:18 +000014519 Expr::EvalResult Result;
14520 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014521 return false; // Can't get the integer value as a constant.
14522
Fangrui Song407659a2018-11-30 23:41:18 +000014523 llvm::APSInt ConstLength = Result.Val.getInt();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014524 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
14525}
14526
14527// Return true if it can be proven that the provided array expression (array
14528// section or array subscript) does NOT specify a single element of the array
14529// whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000014530static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
David Majnemer9d168222016-08-05 17:44:54 +000014531 const Expr *E,
14532 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000014533 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014534
14535 // An array subscript always refer to a single element. Also, an array section
14536 // assumes the format of an array subscript if no colon is used.
14537 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
14538 return false;
14539
14540 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000014541 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014542
14543 // If we don't have a length we have to check if the array has unitary size
14544 // for this dimension. Also, we should always expect a length if the base type
14545 // is pointer.
14546 if (!Length) {
Alexey Bataeve3727102018-04-18 15:57:46 +000014547 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014548 return ATy->getSize().getSExtValue() != 1;
14549 // We cannot assume anything.
14550 return false;
14551 }
14552
14553 // Check if the length evaluates to 1.
Fangrui Song407659a2018-11-30 23:41:18 +000014554 Expr::EvalResult Result;
14555 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014556 return false; // Can't get the integer value as a constant.
14557
Fangrui Song407659a2018-11-30 23:41:18 +000014558 llvm::APSInt ConstLength = Result.Val.getInt();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014559 return ConstLength.getSExtValue() != 1;
14560}
14561
Samuel Antao661c0902016-05-26 17:39:58 +000014562// Return the expression of the base of the mappable expression or null if it
14563// cannot be determined and do all the necessary checks to see if the expression
14564// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000014565// components of the expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000014566static const Expr *checkMapClauseExpressionBase(
Samuel Antao90927002016-04-26 14:54:23 +000014567 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000014568 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
Alexey Bataevb7a9b742017-12-05 19:20:09 +000014569 OpenMPClauseKind CKind, bool NoDiagnose) {
Samuel Antao5de996e2016-01-22 20:21:36 +000014570 SourceLocation ELoc = E->getExprLoc();
14571 SourceRange ERange = E->getSourceRange();
14572
14573 // The base of elements of list in a map clause have to be either:
14574 // - a reference to variable or field.
14575 // - a member expression.
14576 // - an array expression.
14577 //
14578 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
14579 // reference to 'r'.
14580 //
14581 // If we have:
14582 //
14583 // struct SS {
14584 // Bla S;
14585 // foo() {
14586 // #pragma omp target map (S.Arr[:12]);
14587 // }
14588 // }
14589 //
14590 // We want to retrieve the member expression 'this->S';
14591
Alexey Bataeve3727102018-04-18 15:57:46 +000014592 const Expr *RelevantExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000014593
Samuel Antao5de996e2016-01-22 20:21:36 +000014594 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
14595 // If a list item is an array section, it must specify contiguous storage.
14596 //
14597 // For this restriction it is sufficient that we make sure only references
14598 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014599 // exist except in the rightmost expression (unless they cover the whole
14600 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000014601 //
14602 // r.ArrS[3:5].Arr[6:7]
14603 //
14604 // r.ArrS[3:5].x
14605 //
14606 // but these would be valid:
14607 // r.ArrS[3].Arr[6:7]
14608 //
14609 // r.ArrS[3].x
14610
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014611 bool AllowUnitySizeArraySection = true;
14612 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000014613
Dmitry Polukhin644a9252016-03-11 07:58:34 +000014614 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000014615 E = E->IgnoreParenImpCasts();
14616
14617 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
14618 if (!isa<VarDecl>(CurE->getDecl()))
Alexey Bataev27041fa2017-12-05 15:22:49 +000014619 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000014620
14621 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014622
14623 // If we got a reference to a declaration, we should not expect any array
14624 // section before that.
14625 AllowUnitySizeArraySection = false;
14626 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000014627
14628 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000014629 CurComponents.emplace_back(CurE, CurE->getDecl());
14630 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000014631 Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts();
Samuel Antao5de996e2016-01-22 20:21:36 +000014632
14633 if (isa<CXXThisExpr>(BaseE))
14634 // We found a base expression: this->Val.
14635 RelevantExpr = CurE;
14636 else
14637 E = BaseE;
14638
14639 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000014640 if (!NoDiagnose) {
14641 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
14642 << CurE->getSourceRange();
14643 return nullptr;
14644 }
14645 if (RelevantExpr)
14646 return nullptr;
14647 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000014648 }
14649
14650 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
14651
14652 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
14653 // A bit-field cannot appear in a map clause.
14654 //
14655 if (FD->isBitField()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000014656 if (!NoDiagnose) {
14657 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
14658 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
14659 return nullptr;
14660 }
14661 if (RelevantExpr)
14662 return nullptr;
14663 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000014664 }
14665
14666 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
14667 // If the type of a list item is a reference to a type T then the type
14668 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014669 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000014670
14671 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
14672 // A list item cannot be a variable that is a member of a structure with
14673 // a union type.
14674 //
Alexey Bataeve3727102018-04-18 15:57:46 +000014675 if (CurType->isUnionType()) {
14676 if (!NoDiagnose) {
14677 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
14678 << CurE->getSourceRange();
14679 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000014680 }
Alexey Bataeve3727102018-04-18 15:57:46 +000014681 continue;
Alexey Bataevb7a9b742017-12-05 19:20:09 +000014682 }
Samuel Antao5de996e2016-01-22 20:21:36 +000014683
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014684 // If we got a member expression, we should not expect any array section
14685 // before that:
14686 //
14687 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
14688 // If a list item is an element of a structure, only the rightmost symbol
14689 // of the variable reference can be an array section.
14690 //
14691 AllowUnitySizeArraySection = false;
14692 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000014693
14694 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000014695 CurComponents.emplace_back(CurE, FD);
14696 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000014697 E = CurE->getBase()->IgnoreParenImpCasts();
14698
14699 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000014700 if (!NoDiagnose) {
14701 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
14702 << 0 << CurE->getSourceRange();
14703 return nullptr;
14704 }
14705 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000014706 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014707
14708 // If we got an array subscript that express the whole dimension we
14709 // can have any array expressions before. If it only expressing part of
14710 // the dimension, we can only have unitary-size array expressions.
Alexey Bataeve3727102018-04-18 15:57:46 +000014711 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014712 E->getType()))
14713 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000014714
Patrick Lystere13b1e32019-01-02 19:28:48 +000014715 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) {
14716 Expr::EvalResult Result;
14717 if (CurE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext())) {
14718 if (!Result.Val.getInt().isNullValue()) {
14719 SemaRef.Diag(CurE->getIdx()->getExprLoc(),
14720 diag::err_omp_invalid_map_this_expr);
14721 SemaRef.Diag(CurE->getIdx()->getExprLoc(),
14722 diag::note_omp_invalid_subscript_on_this_ptr_map);
14723 }
14724 }
14725 RelevantExpr = TE;
14726 }
14727
Samuel Antao90927002016-04-26 14:54:23 +000014728 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000014729 CurComponents.emplace_back(CurE, nullptr);
14730 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000014731 assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
Samuel Antao5de996e2016-01-22 20:21:36 +000014732 E = CurE->getBase()->IgnoreParenImpCasts();
14733
Alexey Bataev27041fa2017-12-05 15:22:49 +000014734 QualType CurType =
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014735 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
14736
Samuel Antao5de996e2016-01-22 20:21:36 +000014737 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
14738 // If the type of a list item is a reference to a type T then the type
14739 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000014740 if (CurType->isReferenceType())
14741 CurType = CurType->getPointeeType();
14742
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014743 bool IsPointer = CurType->isAnyPointerType();
14744
14745 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000014746 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
14747 << 0 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000014748 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000014749 }
14750
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014751 bool NotWhole =
Alexey Bataeve3727102018-04-18 15:57:46 +000014752 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014753 bool NotUnity =
Alexey Bataeve3727102018-04-18 15:57:46 +000014754 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014755
Samuel Antaodab51bb2016-07-18 23:22:11 +000014756 if (AllowWholeSizeArraySection) {
14757 // Any array section is currently allowed. Allowing a whole size array
14758 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014759 //
14760 // If this array section refers to the whole dimension we can still
14761 // accept other array sections before this one, except if the base is a
14762 // pointer. Otherwise, only unitary sections are accepted.
14763 if (NotWhole || IsPointer)
14764 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +000014765 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014766 // A unity or whole array section is not allowed and that is not
14767 // compatible with the properties of the current array section.
14768 SemaRef.Diag(
14769 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
14770 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000014771 return nullptr;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014772 }
Samuel Antao90927002016-04-26 14:54:23 +000014773
Patrick Lystere13b1e32019-01-02 19:28:48 +000014774 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) {
14775 Expr::EvalResult ResultR;
14776 Expr::EvalResult ResultL;
14777 if (CurE->getLength()->EvaluateAsInt(ResultR,
14778 SemaRef.getASTContext())) {
14779 if (!ResultR.Val.getInt().isOneValue()) {
14780 SemaRef.Diag(CurE->getLength()->getExprLoc(),
14781 diag::err_omp_invalid_map_this_expr);
14782 SemaRef.Diag(CurE->getLength()->getExprLoc(),
14783 diag::note_omp_invalid_length_on_this_ptr_mapping);
14784 }
14785 }
14786 if (CurE->getLowerBound() && CurE->getLowerBound()->EvaluateAsInt(
14787 ResultL, SemaRef.getASTContext())) {
14788 if (!ResultL.Val.getInt().isNullValue()) {
14789 SemaRef.Diag(CurE->getLowerBound()->getExprLoc(),
14790 diag::err_omp_invalid_map_this_expr);
14791 SemaRef.Diag(CurE->getLowerBound()->getExprLoc(),
14792 diag::note_omp_invalid_lower_bound_on_this_ptr_mapping);
14793 }
14794 }
14795 RelevantExpr = TE;
14796 }
14797
Samuel Antao90927002016-04-26 14:54:23 +000014798 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000014799 CurComponents.emplace_back(CurE, nullptr);
14800 } else {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000014801 if (!NoDiagnose) {
14802 // If nothing else worked, this is not a valid map clause expression.
14803 SemaRef.Diag(
14804 ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
14805 << ERange;
14806 }
Alexey Bataev27041fa2017-12-05 15:22:49 +000014807 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000014808 }
Samuel Antao5de996e2016-01-22 20:21:36 +000014809 }
14810
14811 return RelevantExpr;
14812}
14813
14814// Return true if expression E associated with value VD has conflicts with other
14815// map information.
Alexey Bataeve3727102018-04-18 15:57:46 +000014816static bool checkMapConflicts(
14817 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
Samuel Antao90927002016-04-26 14:54:23 +000014818 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000014819 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
14820 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000014821 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000014822 SourceLocation ELoc = E->getExprLoc();
14823 SourceRange ERange = E->getSourceRange();
14824
14825 // In order to easily check the conflicts we need to match each component of
14826 // the expression under test with the components of the expressions that are
14827 // already in the stack.
14828
Samuel Antao5de996e2016-01-22 20:21:36 +000014829 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000014830 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000014831 "Map clause expression with unexpected base!");
14832
14833 // Variables to help detecting enclosing problems in data environment nests.
14834 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000014835 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000014836
Samuel Antao90927002016-04-26 14:54:23 +000014837 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
14838 VD, CurrentRegionOnly,
Alexey Bataeve3727102018-04-18 15:57:46 +000014839 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
14840 ERange, CKind, &EnclosingExpr,
14841 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
14842 StackComponents,
14843 OpenMPClauseKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000014844 assert(!StackComponents.empty() &&
14845 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000014846 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000014847 "Map clause expression with unexpected base!");
Fangrui Song16fe49a2018-04-18 19:32:01 +000014848 (void)VD;
Samuel Antao5de996e2016-01-22 20:21:36 +000014849
Samuel Antao90927002016-04-26 14:54:23 +000014850 // The whole expression in the stack.
Alexey Bataeve3727102018-04-18 15:57:46 +000014851 const Expr *RE = StackComponents.front().getAssociatedExpression();
Samuel Antao90927002016-04-26 14:54:23 +000014852
Samuel Antao5de996e2016-01-22 20:21:36 +000014853 // Expressions must start from the same base. Here we detect at which
14854 // point both expressions diverge from each other and see if we can
14855 // detect if the memory referred to both expressions is contiguous and
14856 // do not overlap.
14857 auto CI = CurComponents.rbegin();
14858 auto CE = CurComponents.rend();
14859 auto SI = StackComponents.rbegin();
14860 auto SE = StackComponents.rend();
14861 for (; CI != CE && SI != SE; ++CI, ++SI) {
14862
14863 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
14864 // At most one list item can be an array item derived from a given
14865 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000014866 if (CurrentRegionOnly &&
14867 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
14868 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
14869 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
14870 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
14871 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000014872 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000014873 << CI->getAssociatedExpression()->getSourceRange();
14874 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
14875 diag::note_used_here)
14876 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000014877 return true;
14878 }
14879
14880 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000014881 if (CI->getAssociatedExpression()->getStmtClass() !=
14882 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000014883 break;
14884
14885 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000014886 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000014887 break;
14888 }
Kelvin Li9f645ae2016-07-18 22:49:16 +000014889 // Check if the extra components of the expressions in the enclosing
14890 // data environment are redundant for the current base declaration.
14891 // If they are, the maps completely overlap, which is legal.
14892 for (; SI != SE; ++SI) {
14893 QualType Type;
Alexey Bataeve3727102018-04-18 15:57:46 +000014894 if (const auto *ASE =
David Majnemer9d168222016-08-05 17:44:54 +000014895 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +000014896 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +000014897 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(
David Majnemer9d168222016-08-05 17:44:54 +000014898 SI->getAssociatedExpression())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000014899 const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
Kelvin Li9f645ae2016-07-18 22:49:16 +000014900 Type =
14901 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
14902 }
14903 if (Type.isNull() || Type->isAnyPointerType() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000014904 checkArrayExpressionDoesNotReferToWholeSize(
Kelvin Li9f645ae2016-07-18 22:49:16 +000014905 SemaRef, SI->getAssociatedExpression(), Type))
14906 break;
14907 }
Samuel Antao5de996e2016-01-22 20:21:36 +000014908
14909 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
14910 // List items of map clauses in the same construct must not share
14911 // original storage.
14912 //
14913 // If the expressions are exactly the same or one is a subset of the
14914 // other, it means they are sharing storage.
14915 if (CI == CE && SI == SE) {
14916 if (CurrentRegionOnly) {
Alexey Bataeve3727102018-04-18 15:57:46 +000014917 if (CKind == OMPC_map) {
Samuel Antao661c0902016-05-26 17:39:58 +000014918 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000014919 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000014920 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000014921 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
14922 << ERange;
14923 }
Samuel Antao5de996e2016-01-22 20:21:36 +000014924 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
14925 << RE->getSourceRange();
14926 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000014927 }
Alexey Bataeve3727102018-04-18 15:57:46 +000014928 // If we find the same expression in the enclosing data environment,
14929 // that is legal.
14930 IsEnclosedByDataEnvironmentExpr = true;
14931 return false;
Samuel Antao5de996e2016-01-22 20:21:36 +000014932 }
14933
Samuel Antao90927002016-04-26 14:54:23 +000014934 QualType DerivedType =
14935 std::prev(CI)->getAssociatedDeclaration()->getType();
14936 SourceLocation DerivedLoc =
14937 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000014938
14939 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
14940 // If the type of a list item is a reference to a type T then the type
14941 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000014942 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000014943
14944 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
14945 // A variable for which the type is pointer and an array section
14946 // derived from that variable must not appear as list items of map
14947 // clauses of the same construct.
14948 //
14949 // Also, cover one of the cases in:
14950 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
14951 // If any part of the original storage of a list item has corresponding
14952 // storage in the device data environment, all of the original storage
14953 // must have corresponding storage in the device data environment.
14954 //
14955 if (DerivedType->isAnyPointerType()) {
14956 if (CI == CE || SI == SE) {
14957 SemaRef.Diag(
14958 DerivedLoc,
14959 diag::err_omp_pointer_mapped_along_with_derived_section)
14960 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000014961 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
14962 << RE->getSourceRange();
14963 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +000014964 }
14965 if (CI->getAssociatedExpression()->getStmtClass() !=
Alexey Bataev2819260b2018-02-27 17:42:00 +000014966 SI->getAssociatedExpression()->getStmtClass() ||
14967 CI->getAssociatedDeclaration()->getCanonicalDecl() ==
14968 SI->getAssociatedDeclaration()->getCanonicalDecl()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000014969 assert(CI != CE && SI != SE);
Alexey Bataev2819260b2018-02-27 17:42:00 +000014970 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
Samuel Antao5de996e2016-01-22 20:21:36 +000014971 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000014972 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
14973 << RE->getSourceRange();
14974 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000014975 }
Samuel Antao5de996e2016-01-22 20:21:36 +000014976 }
14977
14978 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
14979 // List items of map clauses in the same construct must not share
14980 // original storage.
14981 //
14982 // An expression is a subset of the other.
14983 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000014984 if (CKind == OMPC_map) {
Alexey Bataeve82445f2018-09-20 13:54:02 +000014985 if (CI != CE || SI != SE) {
14986 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
14987 // a pointer.
14988 auto Begin =
14989 CI != CE ? CurComponents.begin() : StackComponents.begin();
14990 auto End = CI != CE ? CurComponents.end() : StackComponents.end();
14991 auto It = Begin;
14992 while (It != End && !It->getAssociatedDeclaration())
14993 std::advance(It, 1);
14994 assert(It != End &&
14995 "Expected at least one component with the declaration.");
14996 if (It != Begin && It->getAssociatedDeclaration()
14997 ->getType()
14998 .getCanonicalType()
14999 ->isAnyPointerType()) {
15000 IsEnclosedByDataEnvironmentExpr = false;
15001 EnclosingExpr = nullptr;
15002 return false;
15003 }
15004 }
Samuel Antao661c0902016-05-26 17:39:58 +000015005 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000015006 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000015007 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000015008 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
15009 << ERange;
15010 }
Samuel Antao5de996e2016-01-22 20:21:36 +000015011 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
15012 << RE->getSourceRange();
15013 return true;
15014 }
15015
15016 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000015017 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000015018 if (!CurrentRegionOnly && SI != SE)
15019 EnclosingExpr = RE;
15020
15021 // The current expression is a subset of the expression in the data
15022 // environment.
15023 IsEnclosedByDataEnvironmentExpr |=
15024 (!CurrentRegionOnly && CI != CE && SI == SE);
15025
15026 return false;
15027 });
15028
15029 if (CurrentRegionOnly)
15030 return FoundError;
15031
15032 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
15033 // If any part of the original storage of a list item has corresponding
15034 // storage in the device data environment, all of the original storage must
15035 // have corresponding storage in the device data environment.
15036 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
15037 // If a list item is an element of a structure, and a different element of
15038 // the structure has a corresponding list item in the device data environment
15039 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000015040 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000015041 // data environment prior to the task encountering the construct.
15042 //
15043 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
15044 SemaRef.Diag(ELoc,
15045 diag::err_omp_original_storage_is_shared_and_does_not_contain)
15046 << ERange;
15047 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
15048 << EnclosingExpr->getSourceRange();
15049 return true;
15050 }
15051
15052 return FoundError;
15053}
15054
Michael Kruse4304e9d2019-02-19 16:38:20 +000015055// Look up the user-defined mapper given the mapper name and mapped type, and
15056// build a reference to it.
Benjamin Kramerba2ea932019-03-28 17:18:42 +000015057static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S,
15058 CXXScopeSpec &MapperIdScopeSpec,
15059 const DeclarationNameInfo &MapperId,
15060 QualType Type,
15061 Expr *UnresolvedMapper) {
Michael Kruse4304e9d2019-02-19 16:38:20 +000015062 if (MapperIdScopeSpec.isInvalid())
15063 return ExprError();
Michael Kruse945249b2019-09-26 22:53:01 +000015064 // Get the actual type for the array type.
15065 if (Type->isArrayType()) {
15066 assert(Type->getAsArrayTypeUnsafe() && "Expect to get a valid array type");
15067 Type = Type->getAsArrayTypeUnsafe()->getElementType().getCanonicalType();
15068 }
Michael Kruse4304e9d2019-02-19 16:38:20 +000015069 // Find all user-defined mappers with the given MapperId.
15070 SmallVector<UnresolvedSet<8>, 4> Lookups;
15071 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName);
15072 Lookup.suppressDiagnostics();
15073 if (S) {
15074 while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) {
15075 NamedDecl *D = Lookup.getRepresentativeDecl();
15076 while (S && !S->isDeclScope(D))
15077 S = S->getParent();
15078 if (S)
15079 S = S->getParent();
15080 Lookups.emplace_back();
15081 Lookups.back().append(Lookup.begin(), Lookup.end());
15082 Lookup.clear();
15083 }
15084 } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) {
15085 // Extract the user-defined mappers with the given MapperId.
15086 Lookups.push_back(UnresolvedSet<8>());
15087 for (NamedDecl *D : ULE->decls()) {
15088 auto *DMD = cast<OMPDeclareMapperDecl>(D);
15089 assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation.");
15090 Lookups.back().addDecl(DMD);
15091 }
15092 }
15093 // Defer the lookup for dependent types. The results will be passed through
15094 // UnresolvedMapper on instantiation.
15095 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() ||
15096 Type->isInstantiationDependentType() ||
15097 Type->containsUnexpandedParameterPack() ||
15098 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) {
15099 return !D->isInvalidDecl() &&
15100 (D->getType()->isDependentType() ||
15101 D->getType()->isInstantiationDependentType() ||
15102 D->getType()->containsUnexpandedParameterPack());
15103 })) {
15104 UnresolvedSet<8> URS;
15105 for (const UnresolvedSet<8> &Set : Lookups) {
15106 if (Set.empty())
15107 continue;
15108 URS.append(Set.begin(), Set.end());
15109 }
15110 return UnresolvedLookupExpr::Create(
15111 SemaRef.Context, /*NamingClass=*/nullptr,
15112 MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId,
15113 /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end());
15114 }
Michael Kruse945249b2019-09-26 22:53:01 +000015115 SourceLocation Loc = MapperId.getLoc();
Michael Kruse4304e9d2019-02-19 16:38:20 +000015116 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
15117 // The type must be of struct, union or class type in C and C++
Michael Kruse945249b2019-09-26 22:53:01 +000015118 if (!Type->isStructureOrClassType() && !Type->isUnionType() &&
15119 (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default")) {
15120 SemaRef.Diag(Loc, diag::err_omp_mapper_wrong_type);
15121 return ExprError();
15122 }
Michael Kruse4304e9d2019-02-19 16:38:20 +000015123 // Perform argument dependent lookup.
15124 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet())
15125 argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups);
15126 // Return the first user-defined mapper with the desired type.
15127 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
15128 Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * {
15129 if (!D->isInvalidDecl() &&
15130 SemaRef.Context.hasSameType(D->getType(), Type))
15131 return D;
15132 return nullptr;
15133 }))
15134 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc);
15135 // Find the first user-defined mapper with a type derived from the desired
15136 // type.
15137 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
15138 Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * {
15139 if (!D->isInvalidDecl() &&
15140 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) &&
15141 !Type.isMoreQualifiedThan(D->getType()))
15142 return D;
15143 return nullptr;
15144 })) {
15145 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
15146 /*DetectVirtual=*/false);
15147 if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) {
15148 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
15149 VD->getType().getUnqualifiedType()))) {
15150 if (SemaRef.CheckBaseClassAccess(
15151 Loc, VD->getType(), Type, Paths.front(),
15152 /*DiagID=*/0) != Sema::AR_inaccessible) {
15153 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc);
15154 }
15155 }
15156 }
15157 }
15158 // Report error if a mapper is specified, but cannot be found.
15159 if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") {
15160 SemaRef.Diag(Loc, diag::err_omp_invalid_mapper)
15161 << Type << MapperId.getName();
15162 return ExprError();
15163 }
15164 return ExprEmpty();
15165}
15166
Samuel Antao661c0902016-05-26 17:39:58 +000015167namespace {
15168// Utility struct that gathers all the related lists associated with a mappable
15169// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000015170struct MappableVarListInfo {
Samuel Antao661c0902016-05-26 17:39:58 +000015171 // The list of expressions.
15172 ArrayRef<Expr *> VarList;
15173 // The list of processed expressions.
15174 SmallVector<Expr *, 16> ProcessedVarList;
15175 // The mappble components for each expression.
15176 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
15177 // The base declaration of the variable.
15178 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
Michael Kruse4304e9d2019-02-19 16:38:20 +000015179 // The reference to the user-defined mapper associated with every expression.
15180 SmallVector<Expr *, 16> UDMapperList;
Samuel Antao661c0902016-05-26 17:39:58 +000015181
15182 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
15183 // We have a list of components and base declarations for each entry in the
15184 // variable list.
15185 VarComponents.reserve(VarList.size());
15186 VarBaseDeclarations.reserve(VarList.size());
15187 }
15188};
15189}
15190
15191// Check the validity of the provided variable list for the provided clause kind
Michael Kruse4304e9d2019-02-19 16:38:20 +000015192// \a CKind. In the check process the valid expressions, mappable expression
15193// components, variables, and user-defined mappers are extracted and used to
15194// fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a
15195// UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec,
15196// and \a MapperId are expected to be valid if the clause kind is 'map'.
15197static void checkMappableExpressionList(
15198 Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind,
15199 MappableVarListInfo &MVLI, SourceLocation StartLoc,
Michael Kruse01f670d2019-02-22 22:29:42 +000015200 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId,
15201 ArrayRef<Expr *> UnresolvedMappers,
Michael Kruse4304e9d2019-02-19 16:38:20 +000015202 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
Michael Kruse01f670d2019-02-22 22:29:42 +000015203 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000015204 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
15205 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000015206 "Unexpected clause kind with mappable expressions!");
Michael Kruse01f670d2019-02-22 22:29:42 +000015207
15208 // If the identifier of user-defined mapper is not specified, it is "default".
15209 // We do not change the actual name in this clause to distinguish whether a
15210 // mapper is specified explicitly, i.e., it is not explicitly specified when
15211 // MapperId.getName() is empty.
15212 if (!MapperId.getName() || MapperId.getName().isEmpty()) {
15213 auto &DeclNames = SemaRef.getASTContext().DeclarationNames;
15214 MapperId.setName(DeclNames.getIdentifier(
15215 &SemaRef.getASTContext().Idents.get("default")));
15216 }
Michael Kruse4304e9d2019-02-19 16:38:20 +000015217
15218 // Iterators to find the current unresolved mapper expression.
15219 auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end();
15220 bool UpdateUMIt = false;
15221 Expr *UnresolvedMapper = nullptr;
Kelvin Li0bff7af2015-11-23 05:32:03 +000015222
Samuel Antao90927002016-04-26 14:54:23 +000015223 // Keep track of the mappable components and base declarations in this clause.
15224 // Each entry in the list is going to have a list of components associated. We
15225 // record each set of the components so that we can build the clause later on.
15226 // In the end we should have the same amount of declarations and component
15227 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000015228
Alexey Bataeve3727102018-04-18 15:57:46 +000015229 for (Expr *RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000015230 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000015231 SourceLocation ELoc = RE->getExprLoc();
15232
Michael Kruse4304e9d2019-02-19 16:38:20 +000015233 // Find the current unresolved mapper expression.
15234 if (UpdateUMIt && UMIt != UMEnd) {
15235 UMIt++;
15236 assert(
15237 UMIt != UMEnd &&
15238 "Expect the size of UnresolvedMappers to match with that of VarList");
15239 }
15240 UpdateUMIt = true;
15241 if (UMIt != UMEnd)
15242 UnresolvedMapper = *UMIt;
15243
Alexey Bataeve3727102018-04-18 15:57:46 +000015244 const Expr *VE = RE->IgnoreParenLValueCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000015245
15246 if (VE->isValueDependent() || VE->isTypeDependent() ||
15247 VE->isInstantiationDependent() ||
15248 VE->containsUnexpandedParameterPack()) {
Michael Kruse0336c752019-02-25 20:34:15 +000015249 // Try to find the associated user-defined mapper.
15250 ExprResult ER = buildUserDefinedMapperRef(
15251 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
15252 VE->getType().getCanonicalType(), UnresolvedMapper);
15253 if (ER.isInvalid())
15254 continue;
15255 MVLI.UDMapperList.push_back(ER.get());
Samuel Antao5de996e2016-01-22 20:21:36 +000015256 // We can only analyze this information once the missing information is
15257 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000015258 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000015259 continue;
15260 }
15261
Alexey Bataeve3727102018-04-18 15:57:46 +000015262 Expr *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000015263
Samuel Antao5de996e2016-01-22 20:21:36 +000015264 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000015265 SemaRef.Diag(ELoc,
15266 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000015267 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000015268 continue;
15269 }
15270
Samuel Antao90927002016-04-26 14:54:23 +000015271 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
15272 ValueDecl *CurDeclaration = nullptr;
15273
15274 // Obtain the array or member expression bases if required. Also, fill the
15275 // components array with all the components identified in the process.
Alexey Bataeve3727102018-04-18 15:57:46 +000015276 const Expr *BE = checkMapClauseExpressionBase(
15277 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false);
Samuel Antao5de996e2016-01-22 20:21:36 +000015278 if (!BE)
15279 continue;
15280
Samuel Antao90927002016-04-26 14:54:23 +000015281 assert(!CurComponents.empty() &&
15282 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000015283
Patrick Lystere13b1e32019-01-02 19:28:48 +000015284 if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) {
15285 // Add store "this" pointer to class in DSAStackTy for future checking
15286 DSAS->addMappedClassesQualTypes(TE->getType());
Michael Kruse0336c752019-02-25 20:34:15 +000015287 // Try to find the associated user-defined mapper.
15288 ExprResult ER = buildUserDefinedMapperRef(
15289 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
15290 VE->getType().getCanonicalType(), UnresolvedMapper);
15291 if (ER.isInvalid())
15292 continue;
15293 MVLI.UDMapperList.push_back(ER.get());
Patrick Lystere13b1e32019-01-02 19:28:48 +000015294 // Skip restriction checking for variable or field declarations
15295 MVLI.ProcessedVarList.push_back(RE);
15296 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
15297 MVLI.VarComponents.back().append(CurComponents.begin(),
15298 CurComponents.end());
15299 MVLI.VarBaseDeclarations.push_back(nullptr);
15300 continue;
15301 }
15302
Samuel Antao90927002016-04-26 14:54:23 +000015303 // For the following checks, we rely on the base declaration which is
15304 // expected to be associated with the last component. The declaration is
15305 // expected to be a variable or a field (if 'this' is being mapped).
15306 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
15307 assert(CurDeclaration && "Null decl on map clause.");
15308 assert(
15309 CurDeclaration->isCanonicalDecl() &&
15310 "Expecting components to have associated only canonical declarations.");
15311
15312 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
Alexey Bataeve3727102018-04-18 15:57:46 +000015313 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000015314
15315 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000015316 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000015317
15318 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000015319 // threadprivate variables cannot appear in a map clause.
15320 // OpenMP 4.5 [2.10.5, target update Construct]
15321 // threadprivate variables cannot appear in a from clause.
15322 if (VD && DSAS->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000015323 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000015324 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
15325 << getOpenMPClauseName(CKind);
Alexey Bataeve3727102018-04-18 15:57:46 +000015326 reportOriginalDsa(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000015327 continue;
15328 }
15329
Samuel Antao5de996e2016-01-22 20:21:36 +000015330 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
15331 // A list item cannot appear in both a map clause and a data-sharing
15332 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000015333
Samuel Antao5de996e2016-01-22 20:21:36 +000015334 // Check conflicts with other map clause expressions. We check the conflicts
15335 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000015336 // environment, because the restrictions are different. We only have to
15337 // check conflicts across regions for the map clauses.
Alexey Bataeve3727102018-04-18 15:57:46 +000015338 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000015339 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000015340 break;
Samuel Antao661c0902016-05-26 17:39:58 +000015341 if (CKind == OMPC_map &&
Alexey Bataeve3727102018-04-18 15:57:46 +000015342 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000015343 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000015344 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000015345
Samuel Antao661c0902016-05-26 17:39:58 +000015346 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000015347 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
15348 // If the type of a list item is a reference to a type T then the type will
15349 // be considered to be T for all purposes of this clause.
Alexey Bataev354df2e2018-05-02 18:44:10 +000015350 auto I = llvm::find_if(
15351 CurComponents,
15352 [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
15353 return MC.getAssociatedDeclaration();
15354 });
15355 assert(I != CurComponents.end() && "Null decl on map clause.");
15356 QualType Type =
15357 I->getAssociatedDeclaration()->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000015358
Samuel Antao661c0902016-05-26 17:39:58 +000015359 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
15360 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000015361 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000015362 // A list item must have a mappable type.
Alexey Bataeve3727102018-04-18 15:57:46 +000015363 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
Samuel Antao661c0902016-05-26 17:39:58 +000015364 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000015365 continue;
15366
Samuel Antao661c0902016-05-26 17:39:58 +000015367 if (CKind == OMPC_map) {
15368 // target enter data
15369 // OpenMP [2.10.2, Restrictions, p. 99]
15370 // A map-type must be specified in all map clauses and must be either
15371 // to or alloc.
15372 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
15373 if (DKind == OMPD_target_enter_data &&
15374 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
15375 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
15376 << (IsMapTypeImplicit ? 1 : 0)
15377 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
15378 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000015379 continue;
15380 }
Samuel Antao661c0902016-05-26 17:39:58 +000015381
15382 // target exit_data
15383 // OpenMP [2.10.3, Restrictions, p. 102]
15384 // A map-type must be specified in all map clauses and must be either
15385 // from, release, or delete.
15386 if (DKind == OMPD_target_exit_data &&
15387 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
15388 MapType == OMPC_MAP_delete)) {
15389 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
15390 << (IsMapTypeImplicit ? 1 : 0)
15391 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
15392 << getOpenMPDirectiveName(DKind);
15393 continue;
15394 }
15395
15396 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
15397 // A list item cannot appear in both a map clause and a data-sharing
15398 // attribute clause on the same construct
Joel E. Denny7d5bc552019-08-22 03:34:30 +000015399 //
15400 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
15401 // A list item cannot appear in both a map clause and a data-sharing
15402 // attribute clause on the same construct unless the construct is a
15403 // combined construct.
15404 if (VD && ((SemaRef.LangOpts.OpenMP <= 45 &&
15405 isOpenMPTargetExecutionDirective(DKind)) ||
15406 DKind == OMPD_target)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000015407 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000015408 if (isOpenMPPrivate(DVar.CKind)) {
Samuel Antao6890b092016-07-28 14:25:09 +000015409 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Samuel Antao661c0902016-05-26 17:39:58 +000015410 << getOpenMPClauseName(DVar.CKind)
Samuel Antao6890b092016-07-28 14:25:09 +000015411 << getOpenMPClauseName(OMPC_map)
Samuel Antao661c0902016-05-26 17:39:58 +000015412 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000015413 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar);
Samuel Antao661c0902016-05-26 17:39:58 +000015414 continue;
15415 }
15416 }
Michael Kruse01f670d2019-02-22 22:29:42 +000015417 }
Michael Kruse4304e9d2019-02-19 16:38:20 +000015418
Michael Kruse01f670d2019-02-22 22:29:42 +000015419 // Try to find the associated user-defined mapper.
Michael Kruse0336c752019-02-25 20:34:15 +000015420 ExprResult ER = buildUserDefinedMapperRef(
15421 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
15422 Type.getCanonicalType(), UnresolvedMapper);
15423 if (ER.isInvalid())
15424 continue;
15425 MVLI.UDMapperList.push_back(ER.get());
Carlo Bertollib74bfc82016-03-18 21:43:32 +000015426
Samuel Antao90927002016-04-26 14:54:23 +000015427 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000015428 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000015429
15430 // Store the components in the stack so that they can be used to check
15431 // against other clauses later on.
Samuel Antao6890b092016-07-28 14:25:09 +000015432 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
15433 /*WhereFoundClauseKind=*/OMPC_map);
Samuel Antao90927002016-04-26 14:54:23 +000015434
15435 // Save the components and declaration to create the clause. For purposes of
15436 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000015437 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000015438 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
15439 MVLI.VarComponents.back().append(CurComponents.begin(),
15440 CurComponents.end());
15441 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
15442 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000015443 }
Samuel Antao661c0902016-05-26 17:39:58 +000015444}
15445
Michael Kruse4304e9d2019-02-19 16:38:20 +000015446OMPClause *Sema::ActOnOpenMPMapClause(
15447 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
15448 ArrayRef<SourceLocation> MapTypeModifiersLoc,
15449 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
15450 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc,
15451 SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
15452 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) {
15453 OpenMPMapModifierKind Modifiers[] = {OMPC_MAP_MODIFIER_unknown,
15454 OMPC_MAP_MODIFIER_unknown,
15455 OMPC_MAP_MODIFIER_unknown};
Kelvin Lief579432018-12-18 22:18:41 +000015456 SourceLocation ModifiersLoc[OMPMapClause::NumberOfModifiers];
15457
15458 // Process map-type-modifiers, flag errors for duplicate modifiers.
15459 unsigned Count = 0;
15460 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) {
15461 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown &&
15462 llvm::find(Modifiers, MapTypeModifiers[I]) != std::end(Modifiers)) {
15463 Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier);
15464 continue;
15465 }
15466 assert(Count < OMPMapClause::NumberOfModifiers &&
Gheorghe-Teodor Berceaa3afcf22019-01-09 20:38:35 +000015467 "Modifiers exceed the allowed number of map type modifiers");
Kelvin Lief579432018-12-18 22:18:41 +000015468 Modifiers[Count] = MapTypeModifiers[I];
15469 ModifiersLoc[Count] = MapTypeModifiersLoc[I];
15470 ++Count;
15471 }
15472
Michael Kruse4304e9d2019-02-19 16:38:20 +000015473 MappableVarListInfo MVLI(VarList);
15474 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc,
Michael Kruse01f670d2019-02-22 22:29:42 +000015475 MapperIdScopeSpec, MapperId, UnresolvedMappers,
15476 MapType, IsMapTypeImplicit);
Michael Kruse4304e9d2019-02-19 16:38:20 +000015477
Samuel Antao5de996e2016-01-22 20:21:36 +000015478 // We need to produce a map clause even if we don't have variables so that
15479 // other diagnostics related with non-existing map clauses are accurate.
Michael Kruse4304e9d2019-02-19 16:38:20 +000015480 return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList,
15481 MVLI.VarBaseDeclarations, MVLI.VarComponents,
15482 MVLI.UDMapperList, Modifiers, ModifiersLoc,
15483 MapperIdScopeSpec.getWithLocInContext(Context),
15484 MapperId, MapType, IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000015485}
Kelvin Li099bb8c2015-11-24 20:50:12 +000015486
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015487QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
15488 TypeResult ParsedType) {
15489 assert(ParsedType.isUsable());
15490
15491 QualType ReductionType = GetTypeFromParser(ParsedType.get());
15492 if (ReductionType.isNull())
15493 return QualType();
15494
15495 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
15496 // A type name in a declare reduction directive cannot be a function type, an
15497 // array type, a reference type, or a type qualified with const, volatile or
15498 // restrict.
15499 if (ReductionType.hasQualifiers()) {
15500 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
15501 return QualType();
15502 }
15503
15504 if (ReductionType->isFunctionType()) {
15505 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
15506 return QualType();
15507 }
15508 if (ReductionType->isReferenceType()) {
15509 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
15510 return QualType();
15511 }
15512 if (ReductionType->isArrayType()) {
15513 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
15514 return QualType();
15515 }
15516 return ReductionType;
15517}
15518
15519Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
15520 Scope *S, DeclContext *DC, DeclarationName Name,
15521 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
15522 AccessSpecifier AS, Decl *PrevDeclInScope) {
15523 SmallVector<Decl *, 8> Decls;
15524 Decls.reserve(ReductionTypes.size());
15525
15526 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
Richard Smithbecb92d2017-10-10 22:33:17 +000015527 forRedeclarationInCurContext());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015528 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
15529 // A reduction-identifier may not be re-declared in the current scope for the
15530 // same type or for a type that is compatible according to the base language
15531 // rules.
15532 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
15533 OMPDeclareReductionDecl *PrevDRD = nullptr;
15534 bool InCompoundScope = true;
15535 if (S != nullptr) {
15536 // Find previous declaration with the same name not referenced in other
15537 // declarations.
15538 FunctionScopeInfo *ParentFn = getEnclosingFunction();
15539 InCompoundScope =
15540 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
15541 LookupName(Lookup, S);
15542 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
15543 /*AllowInlineNamespace=*/false);
15544 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
Alexey Bataeve3727102018-04-18 15:57:46 +000015545 LookupResult::Filter Filter = Lookup.makeFilter();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015546 while (Filter.hasNext()) {
15547 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
15548 if (InCompoundScope) {
15549 auto I = UsedAsPrevious.find(PrevDecl);
15550 if (I == UsedAsPrevious.end())
15551 UsedAsPrevious[PrevDecl] = false;
Alexey Bataeve3727102018-04-18 15:57:46 +000015552 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015553 UsedAsPrevious[D] = true;
15554 }
15555 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
15556 PrevDecl->getLocation();
15557 }
15558 Filter.done();
15559 if (InCompoundScope) {
Alexey Bataeve3727102018-04-18 15:57:46 +000015560 for (const auto &PrevData : UsedAsPrevious) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015561 if (!PrevData.second) {
15562 PrevDRD = PrevData.first;
15563 break;
15564 }
15565 }
15566 }
15567 } else if (PrevDeclInScope != nullptr) {
15568 auto *PrevDRDInScope = PrevDRD =
15569 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
15570 do {
15571 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
15572 PrevDRDInScope->getLocation();
15573 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
15574 } while (PrevDRDInScope != nullptr);
15575 }
Alexey Bataeve3727102018-04-18 15:57:46 +000015576 for (const auto &TyData : ReductionTypes) {
15577 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015578 bool Invalid = false;
15579 if (I != PreviousRedeclTypes.end()) {
15580 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
15581 << TyData.first;
15582 Diag(I->second, diag::note_previous_definition);
15583 Invalid = true;
15584 }
15585 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
15586 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
15587 Name, TyData.first, PrevDRD);
15588 DC->addDecl(DRD);
15589 DRD->setAccess(AS);
15590 Decls.push_back(DRD);
15591 if (Invalid)
15592 DRD->setInvalidDecl();
15593 else
15594 PrevDRD = DRD;
15595 }
15596
15597 return DeclGroupPtrTy::make(
15598 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
15599}
15600
15601void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
15602 auto *DRD = cast<OMPDeclareReductionDecl>(D);
15603
15604 // Enter new function scope.
15605 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000015606 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015607 getCurFunction()->setHasOMPDeclareReductionCombiner();
15608
15609 if (S != nullptr)
15610 PushDeclContext(S, DRD);
15611 else
15612 CurContext = DRD;
15613
Faisal Valid143a0c2017-04-01 21:30:49 +000015614 PushExpressionEvaluationContext(
15615 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015616
15617 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000015618 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
15619 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
15620 // uses semantics of argument handles by value, but it should be passed by
15621 // reference. C lang does not support references, so pass all parameters as
15622 // pointers.
15623 // Create 'T omp_in;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000015624 VarDecl *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000015625 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015626 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
15627 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
15628 // uses semantics of argument handles by value, but it should be passed by
15629 // reference. C lang does not support references, so pass all parameters as
15630 // pointers.
15631 // Create 'T omp_out;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000015632 VarDecl *OmpOutParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015633 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
15634 if (S != nullptr) {
15635 PushOnScopeChains(OmpInParm, S);
15636 PushOnScopeChains(OmpOutParm, S);
15637 } else {
15638 DRD->addDecl(OmpInParm);
15639 DRD->addDecl(OmpOutParm);
15640 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000015641 Expr *InE =
15642 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation());
15643 Expr *OutE =
15644 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation());
15645 DRD->setCombinerData(InE, OutE);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015646}
15647
15648void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
15649 auto *DRD = cast<OMPDeclareReductionDecl>(D);
15650 DiscardCleanupsInEvaluationContext();
15651 PopExpressionEvaluationContext();
15652
15653 PopDeclContext();
15654 PopFunctionScopeInfo();
15655
15656 if (Combiner != nullptr)
15657 DRD->setCombiner(Combiner);
15658 else
15659 DRD->setInvalidDecl();
15660}
15661
Alexey Bataev070f43a2017-09-06 14:49:58 +000015662VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015663 auto *DRD = cast<OMPDeclareReductionDecl>(D);
15664
15665 // Enter new function scope.
15666 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000015667 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015668
15669 if (S != nullptr)
15670 PushDeclContext(S, DRD);
15671 else
15672 CurContext = DRD;
15673
Faisal Valid143a0c2017-04-01 21:30:49 +000015674 PushExpressionEvaluationContext(
15675 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015676
15677 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015678 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
15679 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
15680 // uses semantics of argument handles by value, but it should be passed by
15681 // reference. C lang does not support references, so pass all parameters as
15682 // pointers.
15683 // Create 'T omp_priv;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000015684 VarDecl *OmpPrivParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015685 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000015686 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
15687 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
15688 // uses semantics of argument handles by value, but it should be passed by
15689 // reference. C lang does not support references, so pass all parameters as
15690 // pointers.
15691 // Create 'T omp_orig;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000015692 VarDecl *OmpOrigParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000015693 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015694 if (S != nullptr) {
15695 PushOnScopeChains(OmpPrivParm, S);
15696 PushOnScopeChains(OmpOrigParm, S);
15697 } else {
15698 DRD->addDecl(OmpPrivParm);
15699 DRD->addDecl(OmpOrigParm);
15700 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000015701 Expr *OrigE =
15702 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation());
15703 Expr *PrivE =
15704 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation());
15705 DRD->setInitializerData(OrigE, PrivE);
Alexey Bataev070f43a2017-09-06 14:49:58 +000015706 return OmpPrivParm;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015707}
15708
Alexey Bataev070f43a2017-09-06 14:49:58 +000015709void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
15710 VarDecl *OmpPrivParm) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015711 auto *DRD = cast<OMPDeclareReductionDecl>(D);
15712 DiscardCleanupsInEvaluationContext();
15713 PopExpressionEvaluationContext();
15714
15715 PopDeclContext();
15716 PopFunctionScopeInfo();
15717
Alexey Bataev070f43a2017-09-06 14:49:58 +000015718 if (Initializer != nullptr) {
15719 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
15720 } else if (OmpPrivParm->hasInit()) {
15721 DRD->setInitializer(OmpPrivParm->getInit(),
15722 OmpPrivParm->isDirectInit()
15723 ? OMPDeclareReductionDecl::DirectInit
15724 : OMPDeclareReductionDecl::CopyInit);
15725 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015726 DRD->setInvalidDecl();
Alexey Bataev070f43a2017-09-06 14:49:58 +000015727 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015728}
15729
15730Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
15731 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000015732 for (Decl *D : DeclReductions.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015733 if (IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000015734 if (S)
15735 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S,
15736 /*AddToContext=*/false);
15737 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015738 D->setInvalidDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000015739 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015740 }
15741 return DeclReductions;
15742}
15743
Michael Kruse251e1482019-02-01 20:25:04 +000015744TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) {
15745 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
15746 QualType T = TInfo->getType();
15747 if (D.isInvalidType())
15748 return true;
15749
15750 if (getLangOpts().CPlusPlus) {
15751 // Check that there are no default arguments (C++ only).
15752 CheckExtraCXXDefaultArguments(D);
15753 }
15754
15755 return CreateParsedType(T, TInfo);
15756}
15757
15758QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc,
15759 TypeResult ParsedType) {
15760 assert(ParsedType.isUsable() && "Expect usable parsed mapper type");
15761
15762 QualType MapperType = GetTypeFromParser(ParsedType.get());
15763 assert(!MapperType.isNull() && "Expect valid mapper type");
15764
15765 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
15766 // The type must be of struct, union or class type in C and C++
15767 if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) {
15768 Diag(TyLoc, diag::err_omp_mapper_wrong_type);
15769 return QualType();
15770 }
15771 return MapperType;
15772}
15773
15774OMPDeclareMapperDecl *Sema::ActOnOpenMPDeclareMapperDirectiveStart(
15775 Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType,
15776 SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS,
15777 Decl *PrevDeclInScope) {
15778 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName,
15779 forRedeclarationInCurContext());
15780 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
15781 // A mapper-identifier may not be redeclared in the current scope for the
15782 // same type or for a type that is compatible according to the base language
15783 // rules.
15784 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
15785 OMPDeclareMapperDecl *PrevDMD = nullptr;
15786 bool InCompoundScope = true;
15787 if (S != nullptr) {
15788 // Find previous declaration with the same name not referenced in other
15789 // declarations.
15790 FunctionScopeInfo *ParentFn = getEnclosingFunction();
15791 InCompoundScope =
15792 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
15793 LookupName(Lookup, S);
15794 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
15795 /*AllowInlineNamespace=*/false);
15796 llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious;
15797 LookupResult::Filter Filter = Lookup.makeFilter();
15798 while (Filter.hasNext()) {
15799 auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next());
15800 if (InCompoundScope) {
15801 auto I = UsedAsPrevious.find(PrevDecl);
15802 if (I == UsedAsPrevious.end())
15803 UsedAsPrevious[PrevDecl] = false;
15804 if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope())
15805 UsedAsPrevious[D] = true;
15806 }
15807 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
15808 PrevDecl->getLocation();
15809 }
15810 Filter.done();
15811 if (InCompoundScope) {
15812 for (const auto &PrevData : UsedAsPrevious) {
15813 if (!PrevData.second) {
15814 PrevDMD = PrevData.first;
15815 break;
15816 }
15817 }
15818 }
15819 } else if (PrevDeclInScope) {
15820 auto *PrevDMDInScope = PrevDMD =
15821 cast<OMPDeclareMapperDecl>(PrevDeclInScope);
15822 do {
15823 PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] =
15824 PrevDMDInScope->getLocation();
15825 PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope();
15826 } while (PrevDMDInScope != nullptr);
15827 }
15828 const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType());
15829 bool Invalid = false;
15830 if (I != PreviousRedeclTypes.end()) {
15831 Diag(StartLoc, diag::err_omp_declare_mapper_redefinition)
15832 << MapperType << Name;
15833 Diag(I->second, diag::note_previous_definition);
15834 Invalid = true;
15835 }
15836 auto *DMD = OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name,
15837 MapperType, VN, PrevDMD);
15838 DC->addDecl(DMD);
15839 DMD->setAccess(AS);
15840 if (Invalid)
15841 DMD->setInvalidDecl();
15842
15843 // Enter new function scope.
15844 PushFunctionScope();
15845 setFunctionHasBranchProtectedScope();
15846
15847 CurContext = DMD;
15848
15849 return DMD;
15850}
15851
15852void Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(OMPDeclareMapperDecl *DMD,
15853 Scope *S,
15854 QualType MapperType,
15855 SourceLocation StartLoc,
15856 DeclarationName VN) {
15857 VarDecl *VD = buildVarDecl(*this, StartLoc, MapperType, VN.getAsString());
15858 if (S)
15859 PushOnScopeChains(VD, S);
15860 else
15861 DMD->addDecl(VD);
15862 Expr *MapperVarRefExpr = buildDeclRefExpr(*this, VD, MapperType, StartLoc);
15863 DMD->setMapperVarRef(MapperVarRefExpr);
15864}
15865
15866Sema::DeclGroupPtrTy
15867Sema::ActOnOpenMPDeclareMapperDirectiveEnd(OMPDeclareMapperDecl *D, Scope *S,
15868 ArrayRef<OMPClause *> ClauseList) {
15869 PopDeclContext();
15870 PopFunctionScopeInfo();
15871
15872 if (D) {
15873 if (S)
15874 PushOnScopeChains(D, S, /*AddToContext=*/false);
15875 D->CreateClauses(Context, ClauseList);
15876 }
15877
15878 return DeclGroupPtrTy::make(DeclGroupRef(D));
15879}
15880
David Majnemer9d168222016-08-05 17:44:54 +000015881OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
Kelvin Li099bb8c2015-11-24 20:50:12 +000015882 SourceLocation StartLoc,
15883 SourceLocation LParenLoc,
15884 SourceLocation EndLoc) {
15885 Expr *ValExpr = NumTeams;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000015886 Stmt *HelperValStmt = nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000015887
Kelvin Lia15fb1a2015-11-27 18:47:36 +000015888 // OpenMP [teams Constrcut, Restrictions]
15889 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000015890 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
Alexey Bataeva0569352015-12-01 10:17:31 +000015891 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000015892 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000015893
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000015894 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000015895 OpenMPDirectiveKind CaptureRegion =
15896 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
15897 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000015898 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000015899 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000015900 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
15901 HelperValStmt = buildPreInits(Context, Captures);
15902 }
15903
15904 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
15905 StartLoc, LParenLoc, EndLoc);
Kelvin Li099bb8c2015-11-24 20:50:12 +000015906}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000015907
15908OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
15909 SourceLocation StartLoc,
15910 SourceLocation LParenLoc,
15911 SourceLocation EndLoc) {
15912 Expr *ValExpr = ThreadLimit;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000015913 Stmt *HelperValStmt = nullptr;
Kelvin Lia15fb1a2015-11-27 18:47:36 +000015914
15915 // OpenMP [teams Constrcut, Restrictions]
15916 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000015917 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
Alexey Bataeva0569352015-12-01 10:17:31 +000015918 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000015919 return nullptr;
15920
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000015921 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000015922 OpenMPDirectiveKind CaptureRegion =
15923 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
15924 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000015925 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000015926 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000015927 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
15928 HelperValStmt = buildPreInits(Context, Captures);
15929 }
15930
15931 return new (Context) OMPThreadLimitClause(
15932 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Kelvin Lia15fb1a2015-11-27 18:47:36 +000015933}
Alexey Bataeva0569352015-12-01 10:17:31 +000015934
15935OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
15936 SourceLocation StartLoc,
15937 SourceLocation LParenLoc,
15938 SourceLocation EndLoc) {
15939 Expr *ValExpr = Priority;
15940
15941 // OpenMP [2.9.1, task Constrcut]
15942 // The priority-value is a non-negative numerical scalar expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000015943 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
Alexey Bataeva0569352015-12-01 10:17:31 +000015944 /*StrictlyPositive=*/false))
15945 return nullptr;
15946
15947 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
15948}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000015949
15950OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
15951 SourceLocation StartLoc,
15952 SourceLocation LParenLoc,
15953 SourceLocation EndLoc) {
15954 Expr *ValExpr = Grainsize;
Alexey Bataevb9c55e22019-10-14 19:29:52 +000015955 Stmt *HelperValStmt = nullptr;
15956 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000015957
15958 // OpenMP [2.9.2, taskloop Constrcut]
15959 // The parameter of the grainsize clause must be a positive integer
15960 // expression.
Alexey Bataevb9c55e22019-10-14 19:29:52 +000015961 if (!isNonNegativeIntegerValue(
15962 ValExpr, *this, OMPC_grainsize,
15963 /*StrictlyPositive=*/true, /*BuildCapture=*/true,
15964 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt))
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000015965 return nullptr;
15966
Alexey Bataevb9c55e22019-10-14 19:29:52 +000015967 return new (Context) OMPGrainsizeClause(ValExpr, HelperValStmt, CaptureRegion,
15968 StartLoc, LParenLoc, EndLoc);
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000015969}
Alexey Bataev382967a2015-12-08 12:06:20 +000015970
15971OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
15972 SourceLocation StartLoc,
15973 SourceLocation LParenLoc,
15974 SourceLocation EndLoc) {
15975 Expr *ValExpr = NumTasks;
Alexey Bataevd88c7de2019-10-14 20:44:34 +000015976 Stmt *HelperValStmt = nullptr;
15977 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Alexey Bataev382967a2015-12-08 12:06:20 +000015978
15979 // OpenMP [2.9.2, taskloop Constrcut]
15980 // The parameter of the num_tasks clause must be a positive integer
15981 // expression.
Alexey Bataevd88c7de2019-10-14 20:44:34 +000015982 if (!isNonNegativeIntegerValue(
15983 ValExpr, *this, OMPC_num_tasks,
15984 /*StrictlyPositive=*/true, /*BuildCapture=*/true,
15985 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt))
Alexey Bataev382967a2015-12-08 12:06:20 +000015986 return nullptr;
15987
Alexey Bataevd88c7de2019-10-14 20:44:34 +000015988 return new (Context) OMPNumTasksClause(ValExpr, HelperValStmt, CaptureRegion,
15989 StartLoc, LParenLoc, EndLoc);
Alexey Bataev382967a2015-12-08 12:06:20 +000015990}
15991
Alexey Bataev28c75412015-12-15 08:19:24 +000015992OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
15993 SourceLocation LParenLoc,
15994 SourceLocation EndLoc) {
15995 // OpenMP [2.13.2, critical construct, Description]
15996 // ... where hint-expression is an integer constant expression that evaluates
15997 // to a valid lock hint.
15998 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
15999 if (HintExpr.isInvalid())
16000 return nullptr;
16001 return new (Context)
16002 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
16003}
16004
Carlo Bertollib4adf552016-01-15 18:50:31 +000016005OMPClause *Sema::ActOnOpenMPDistScheduleClause(
16006 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
16007 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
16008 SourceLocation EndLoc) {
16009 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
16010 std::string Values;
16011 Values += "'";
16012 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
16013 Values += "'";
16014 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
16015 << Values << getOpenMPClauseName(OMPC_dist_schedule);
16016 return nullptr;
16017 }
16018 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000016019 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000016020 if (ChunkSize) {
16021 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
16022 !ChunkSize->isInstantiationDependent() &&
16023 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000016024 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Carlo Bertollib4adf552016-01-15 18:50:31 +000016025 ExprResult Val =
16026 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
16027 if (Val.isInvalid())
16028 return nullptr;
16029
16030 ValExpr = Val.get();
16031
16032 // OpenMP [2.7.1, Restrictions]
16033 // chunk_size must be a loop invariant integer expression with a positive
16034 // value.
16035 llvm::APSInt Result;
16036 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
16037 if (Result.isSigned() && !Result.isStrictlyPositive()) {
16038 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
16039 << "dist_schedule" << ChunkSize->getSourceRange();
16040 return nullptr;
16041 }
Alexey Bataev2ba67042017-11-28 21:11:44 +000016042 } else if (getOpenMPCaptureRegionForClause(
16043 DSAStack->getCurrentDirective(), OMPC_dist_schedule) !=
16044 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +000016045 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000016046 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000016047 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +000016048 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
16049 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000016050 }
16051 }
16052 }
16053
16054 return new (Context)
16055 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000016056 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000016057}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000016058
16059OMPClause *Sema::ActOnOpenMPDefaultmapClause(
16060 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
16061 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
16062 SourceLocation KindLoc, SourceLocation EndLoc) {
16063 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
David Majnemer9d168222016-08-05 17:44:54 +000016064 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000016065 std::string Value;
16066 SourceLocation Loc;
16067 Value += "'";
16068 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
16069 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000016070 OMPC_DEFAULTMAP_MODIFIER_tofrom);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000016071 Loc = MLoc;
16072 } else {
16073 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000016074 OMPC_DEFAULTMAP_scalar);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000016075 Loc = KindLoc;
16076 }
16077 Value += "'";
16078 Diag(Loc, diag::err_omp_unexpected_clause_value)
16079 << Value << getOpenMPClauseName(OMPC_defaultmap);
16080 return nullptr;
16081 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +000016082 DSAStack->setDefaultDMAToFromScalar(StartLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000016083
16084 return new (Context)
16085 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
16086}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000016087
16088bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
16089 DeclContext *CurLexicalContext = getCurLexicalContext();
16090 if (!CurLexicalContext->isFileContext() &&
16091 !CurLexicalContext->isExternCContext() &&
Alexey Bataev502ec492017-10-03 20:00:00 +000016092 !CurLexicalContext->isExternCXXContext() &&
16093 !isa<CXXRecordDecl>(CurLexicalContext) &&
16094 !isa<ClassTemplateDecl>(CurLexicalContext) &&
16095 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
16096 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000016097 Diag(Loc, diag::err_omp_region_not_file_context);
16098 return false;
16099 }
Kelvin Libc38e632018-09-10 02:07:09 +000016100 ++DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000016101 return true;
16102}
16103
16104void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
Kelvin Libc38e632018-09-10 02:07:09 +000016105 assert(DeclareTargetNestingLevel > 0 &&
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000016106 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
Kelvin Libc38e632018-09-10 02:07:09 +000016107 --DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000016108}
16109
Alexey Bataev729e2422019-08-23 16:11:14 +000016110NamedDecl *
16111Sema::lookupOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec,
16112 const DeclarationNameInfo &Id,
16113 NamedDeclSetType &SameDirectiveDecls) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000016114 LookupResult Lookup(*this, Id, LookupOrdinaryName);
16115 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
16116
16117 if (Lookup.isAmbiguous())
Alexey Bataev729e2422019-08-23 16:11:14 +000016118 return nullptr;
Dmitry Polukhind69b5052016-05-09 14:59:13 +000016119 Lookup.suppressDiagnostics();
16120
16121 if (!Lookup.isSingleResult()) {
Bruno Ricci70ad3962019-03-25 17:08:51 +000016122 VarOrFuncDeclFilterCCC CCC(*this);
Dmitry Polukhind69b5052016-05-09 14:59:13 +000016123 if (TypoCorrection Corrected =
Bruno Ricci70ad3962019-03-25 17:08:51 +000016124 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC,
Dmitry Polukhind69b5052016-05-09 14:59:13 +000016125 CTK_ErrorRecovery)) {
16126 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
16127 << Id.getName());
16128 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
Alexey Bataev729e2422019-08-23 16:11:14 +000016129 return nullptr;
Dmitry Polukhind69b5052016-05-09 14:59:13 +000016130 }
16131
16132 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
Alexey Bataev729e2422019-08-23 16:11:14 +000016133 return nullptr;
Dmitry Polukhind69b5052016-05-09 14:59:13 +000016134 }
16135
16136 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
Alexey Bataev729e2422019-08-23 16:11:14 +000016137 if (!isa<VarDecl>(ND) && !isa<FunctionDecl>(ND) &&
16138 !isa<FunctionTemplateDecl>(ND)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000016139 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
Alexey Bataev729e2422019-08-23 16:11:14 +000016140 return nullptr;
16141 }
16142 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
16143 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
16144 return ND;
16145}
16146
16147void Sema::ActOnOpenMPDeclareTargetName(
16148 NamedDecl *ND, SourceLocation Loc, OMPDeclareTargetDeclAttr::MapTypeTy MT,
16149 OMPDeclareTargetDeclAttr::DevTypeTy DT) {
16150 assert((isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
16151 isa<FunctionTemplateDecl>(ND)) &&
16152 "Expected variable, function or function template.");
16153
16154 // Diagnose marking after use as it may lead to incorrect diagnosis and
16155 // codegen.
16156 if (LangOpts.OpenMP >= 50 &&
16157 (ND->isUsed(/*CheckUsedAttr=*/false) || ND->isReferenced()))
16158 Diag(Loc, diag::warn_omp_declare_target_after_first_use);
16159
16160 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
16161 OMPDeclareTargetDeclAttr::getDeviceType(cast<ValueDecl>(ND));
16162 if (DevTy.hasValue() && *DevTy != DT) {
16163 Diag(Loc, diag::err_omp_device_type_mismatch)
16164 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(DT)
16165 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(*DevTy);
16166 return;
16167 }
16168 Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
16169 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(cast<ValueDecl>(ND));
16170 if (!Res) {
16171 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT, DT,
16172 SourceRange(Loc, Loc));
16173 ND->addAttr(A);
16174 if (ASTMutationListener *ML = Context.getASTMutationListener())
16175 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
16176 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Loc);
16177 } else if (*Res != MT) {
16178 Diag(Loc, diag::err_omp_declare_target_to_and_link) << ND;
Alexey Bataeve3727102018-04-18 15:57:46 +000016179 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +000016180}
16181
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000016182static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
16183 Sema &SemaRef, Decl *D) {
Alexey Bataev30a78212018-09-11 13:59:10 +000016184 if (!D || !isa<VarDecl>(D))
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000016185 return;
Alexey Bataev30a78212018-09-11 13:59:10 +000016186 auto *VD = cast<VarDecl>(D);
Alexey Bataev217ff1e2019-08-16 20:15:02 +000016187 Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy =
16188 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
16189 if (SemaRef.LangOpts.OpenMP >= 50 &&
16190 (SemaRef.getCurLambda(/*IgnoreNonLambdaCapturingScope=*/true) ||
16191 SemaRef.getCurBlock() || SemaRef.getCurCapturedRegion()) &&
16192 VD->hasGlobalStorage()) {
16193 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy =
16194 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
16195 if (!MapTy || *MapTy != OMPDeclareTargetDeclAttr::MT_To) {
16196 // OpenMP 5.0, 2.12.7 declare target Directive, Restrictions
16197 // If a lambda declaration and definition appears between a
16198 // declare target directive and the matching end declare target
16199 // directive, all variables that are captured by the lambda
16200 // expression must also appear in a to clause.
16201 SemaRef.Diag(VD->getLocation(),
Alexey Bataevc4299552019-08-20 17:50:13 +000016202 diag::err_omp_lambda_capture_in_declare_target_not_to);
Alexey Bataev217ff1e2019-08-16 20:15:02 +000016203 SemaRef.Diag(SL, diag::note_var_explicitly_captured_here)
16204 << VD << 0 << SR;
16205 return;
16206 }
16207 }
16208 if (MapTy.hasValue())
Alexey Bataev30a78212018-09-11 13:59:10 +000016209 return;
16210 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context);
16211 SemaRef.Diag(SL, diag::note_used_here) << SR;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000016212}
16213
16214static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
16215 Sema &SemaRef, DSAStackTy *Stack,
16216 ValueDecl *VD) {
Alexey Bataevebcfc9e2019-08-22 16:48:26 +000016217 return OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) ||
Alexey Bataeve3727102018-04-18 15:57:46 +000016218 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
16219 /*FullCheck=*/false);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000016220}
16221
Kelvin Li1ce87c72017-12-12 20:08:12 +000016222void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
16223 SourceLocation IdLoc) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000016224 if (!D || D->isInvalidDecl())
16225 return;
16226 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
Stephen Kellyf2ceec42018-08-09 21:08:08 +000016227 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
Alexey Bataeve3727102018-04-18 15:57:46 +000016228 if (auto *VD = dyn_cast<VarDecl>(D)) {
Alexey Bataevc1943e72018-07-09 19:58:08 +000016229 // Only global variables can be marked as declare target.
Alexey Bataev30a78212018-09-11 13:59:10 +000016230 if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
16231 !VD->isStaticDataMember())
Alexey Bataevc1943e72018-07-09 19:58:08 +000016232 return;
16233 // 2.10.6: threadprivate variable cannot appear in a declare target
16234 // directive.
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000016235 if (DSAStack->isThreadPrivate(VD)) {
16236 Diag(SL, diag::err_omp_threadprivate_in_target);
Alexey Bataeve3727102018-04-18 15:57:46 +000016237 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000016238 return;
16239 }
16240 }
Alexey Bataev97b72212018-08-14 18:31:20 +000016241 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
16242 D = FTD->getTemplatedDecl();
Alexey Bataev9fd495b2019-08-20 19:50:13 +000016243 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
Alexey Bataev30a78212018-09-11 13:59:10 +000016244 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
16245 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD);
Alexey Bataev9fd495b2019-08-20 19:50:13 +000016246 if (IdLoc.isValid() && Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
Kelvin Li1ce87c72017-12-12 20:08:12 +000016247 Diag(IdLoc, diag::err_omp_function_in_link_clause);
16248 Diag(FD->getLocation(), diag::note_defined_here) << FD;
16249 return;
16250 }
Alexey Bataev9fd495b2019-08-20 19:50:13 +000016251 // Mark the function as must be emitted for the device.
Alexey Bataev729e2422019-08-23 16:11:14 +000016252 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
16253 OMPDeclareTargetDeclAttr::getDeviceType(FD);
16254 if (LangOpts.OpenMPIsDevice && Res.hasValue() && IdLoc.isValid() &&
16255 *DevTy != OMPDeclareTargetDeclAttr::DT_Host)
Alexey Bataev9fd495b2019-08-20 19:50:13 +000016256 checkOpenMPDeviceFunction(IdLoc, FD, /*CheckForDelayedContext=*/false);
Alexey Bataev729e2422019-08-23 16:11:14 +000016257 if (!LangOpts.OpenMPIsDevice && Res.hasValue() && IdLoc.isValid() &&
16258 *DevTy != OMPDeclareTargetDeclAttr::DT_NoHost)
16259 checkOpenMPHostFunction(IdLoc, FD, /*CheckCaller=*/false);
Kelvin Li1ce87c72017-12-12 20:08:12 +000016260 }
Alexey Bataev30a78212018-09-11 13:59:10 +000016261 if (auto *VD = dyn_cast<ValueDecl>(D)) {
16262 // Problem if any with var declared with incomplete type will be reported
16263 // as normal, so no need to check it here.
16264 if ((E || !VD->getType()->isIncompleteType()) &&
16265 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD))
16266 return;
16267 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
16268 // Checking declaration inside declare target region.
16269 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
16270 isa<FunctionTemplateDecl>(D)) {
16271 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
Alexey Bataev729e2422019-08-23 16:11:14 +000016272 Context, OMPDeclareTargetDeclAttr::MT_To,
16273 OMPDeclareTargetDeclAttr::DT_Any, SourceRange(IdLoc, IdLoc));
Alexey Bataev30a78212018-09-11 13:59:10 +000016274 D->addAttr(A);
16275 if (ASTMutationListener *ML = Context.getASTMutationListener())
16276 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
16277 }
16278 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000016279 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000016280 }
Alexey Bataev30a78212018-09-11 13:59:10 +000016281 if (!E)
16282 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000016283 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
16284}
Samuel Antao661c0902016-05-26 17:39:58 +000016285
16286OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
Michael Kruse01f670d2019-02-22 22:29:42 +000016287 CXXScopeSpec &MapperIdScopeSpec,
16288 DeclarationNameInfo &MapperId,
16289 const OMPVarListLocTy &Locs,
16290 ArrayRef<Expr *> UnresolvedMappers) {
Samuel Antao661c0902016-05-26 17:39:58 +000016291 MappableVarListInfo MVLI(VarList);
Michael Kruse01f670d2019-02-22 22:29:42 +000016292 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc,
16293 MapperIdScopeSpec, MapperId, UnresolvedMappers);
Samuel Antao661c0902016-05-26 17:39:58 +000016294 if (MVLI.ProcessedVarList.empty())
16295 return nullptr;
16296
Michael Kruse01f670d2019-02-22 22:29:42 +000016297 return OMPToClause::Create(
16298 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
16299 MVLI.VarComponents, MVLI.UDMapperList,
16300 MapperIdScopeSpec.getWithLocInContext(Context), MapperId);
Samuel Antao661c0902016-05-26 17:39:58 +000016301}
Samuel Antaoec172c62016-05-26 17:49:04 +000016302
16303OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
Michael Kruse0336c752019-02-25 20:34:15 +000016304 CXXScopeSpec &MapperIdScopeSpec,
16305 DeclarationNameInfo &MapperId,
16306 const OMPVarListLocTy &Locs,
16307 ArrayRef<Expr *> UnresolvedMappers) {
Samuel Antaoec172c62016-05-26 17:49:04 +000016308 MappableVarListInfo MVLI(VarList);
Michael Kruse01f670d2019-02-22 22:29:42 +000016309 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc,
16310 MapperIdScopeSpec, MapperId, UnresolvedMappers);
Samuel Antaoec172c62016-05-26 17:49:04 +000016311 if (MVLI.ProcessedVarList.empty())
16312 return nullptr;
16313
Michael Kruse0336c752019-02-25 20:34:15 +000016314 return OMPFromClause::Create(
16315 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
16316 MVLI.VarComponents, MVLI.UDMapperList,
16317 MapperIdScopeSpec.getWithLocInContext(Context), MapperId);
Samuel Antaoec172c62016-05-26 17:49:04 +000016318}
Carlo Bertolli2404b172016-07-13 15:37:16 +000016319
16320OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
Michael Kruse4304e9d2019-02-19 16:38:20 +000016321 const OMPVarListLocTy &Locs) {
Samuel Antaocc10b852016-07-28 14:23:26 +000016322 MappableVarListInfo MVLI(VarList);
16323 SmallVector<Expr *, 8> PrivateCopies;
16324 SmallVector<Expr *, 8> Inits;
16325
Alexey Bataeve3727102018-04-18 15:57:46 +000016326 for (Expr *RefExpr : VarList) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000016327 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
16328 SourceLocation ELoc;
16329 SourceRange ERange;
16330 Expr *SimpleRefExpr = RefExpr;
16331 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
16332 if (Res.second) {
16333 // It will be analyzed later.
Samuel Antaocc10b852016-07-28 14:23:26 +000016334 MVLI.ProcessedVarList.push_back(RefExpr);
16335 PrivateCopies.push_back(nullptr);
16336 Inits.push_back(nullptr);
Carlo Bertolli2404b172016-07-13 15:37:16 +000016337 }
16338 ValueDecl *D = Res.first;
16339 if (!D)
16340 continue;
16341
16342 QualType Type = D->getType();
Samuel Antaocc10b852016-07-28 14:23:26 +000016343 Type = Type.getNonReferenceType().getUnqualifiedType();
16344
16345 auto *VD = dyn_cast<VarDecl>(D);
16346
16347 // Item should be a pointer or reference to pointer.
16348 if (!Type->isPointerType()) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000016349 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
16350 << 0 << RefExpr->getSourceRange();
16351 continue;
16352 }
Samuel Antaocc10b852016-07-28 14:23:26 +000016353
16354 // Build the private variable and the expression that refers to it.
Alexey Bataev63cc8e92018-03-20 14:45:59 +000016355 auto VDPrivate =
16356 buildVarDecl(*this, ELoc, Type, D->getName(),
16357 D->hasAttrs() ? &D->getAttrs() : nullptr,
16358 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Samuel Antaocc10b852016-07-28 14:23:26 +000016359 if (VDPrivate->isInvalidDecl())
16360 continue;
16361
16362 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000016363 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Samuel Antaocc10b852016-07-28 14:23:26 +000016364 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
16365
16366 // Add temporary variable to initialize the private copy of the pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +000016367 VarDecl *VDInit =
Samuel Antaocc10b852016-07-28 14:23:26 +000016368 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
Alexey Bataeve3727102018-04-18 15:57:46 +000016369 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
16370 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc());
Samuel Antaocc10b852016-07-28 14:23:26 +000016371 AddInitializerToDecl(VDPrivate,
16372 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000016373 /*DirectInit=*/false);
Samuel Antaocc10b852016-07-28 14:23:26 +000016374
16375 // If required, build a capture to implement the privatization initialized
16376 // with the current list item value.
16377 DeclRefExpr *Ref = nullptr;
16378 if (!VD)
16379 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
16380 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
16381 PrivateCopies.push_back(VDPrivateRefExpr);
16382 Inits.push_back(VDInitRefExpr);
16383
16384 // We need to add a data sharing attribute for this variable to make sure it
16385 // is correctly captured. A variable that shows up in a use_device_ptr has
16386 // similar properties of a first private variable.
16387 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
16388
16389 // Create a mappable component for the list item. List items in this clause
16390 // only need a component.
16391 MVLI.VarBaseDeclarations.push_back(D);
16392 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
16393 MVLI.VarComponents.back().push_back(
16394 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
Carlo Bertolli2404b172016-07-13 15:37:16 +000016395 }
16396
Samuel Antaocc10b852016-07-28 14:23:26 +000016397 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli2404b172016-07-13 15:37:16 +000016398 return nullptr;
16399
Samuel Antaocc10b852016-07-28 14:23:26 +000016400 return OMPUseDevicePtrClause::Create(
Michael Kruse4304e9d2019-02-19 16:38:20 +000016401 Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits,
16402 MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +000016403}
Carlo Bertolli70594e92016-07-13 17:16:49 +000016404
16405OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
Michael Kruse4304e9d2019-02-19 16:38:20 +000016406 const OMPVarListLocTy &Locs) {
Samuel Antao6890b092016-07-28 14:25:09 +000016407 MappableVarListInfo MVLI(VarList);
Alexey Bataeve3727102018-04-18 15:57:46 +000016408 for (Expr *RefExpr : VarList) {
Kelvin Li84376252016-12-14 15:39:58 +000016409 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
Carlo Bertolli70594e92016-07-13 17:16:49 +000016410 SourceLocation ELoc;
16411 SourceRange ERange;
16412 Expr *SimpleRefExpr = RefExpr;
16413 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
16414 if (Res.second) {
16415 // It will be analyzed later.
Samuel Antao6890b092016-07-28 14:25:09 +000016416 MVLI.ProcessedVarList.push_back(RefExpr);
Carlo Bertolli70594e92016-07-13 17:16:49 +000016417 }
16418 ValueDecl *D = Res.first;
16419 if (!D)
16420 continue;
16421
16422 QualType Type = D->getType();
16423 // item should be a pointer or array or reference to pointer or array
16424 if (!Type.getNonReferenceType()->isPointerType() &&
16425 !Type.getNonReferenceType()->isArrayType()) {
16426 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
16427 << 0 << RefExpr->getSourceRange();
16428 continue;
16429 }
Samuel Antao6890b092016-07-28 14:25:09 +000016430
16431 // Check if the declaration in the clause does not show up in any data
16432 // sharing attribute.
Alexey Bataeve3727102018-04-18 15:57:46 +000016433 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Samuel Antao6890b092016-07-28 14:25:09 +000016434 if (isOpenMPPrivate(DVar.CKind)) {
16435 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
16436 << getOpenMPClauseName(DVar.CKind)
16437 << getOpenMPClauseName(OMPC_is_device_ptr)
16438 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000016439 reportOriginalDsa(*this, DSAStack, D, DVar);
Samuel Antao6890b092016-07-28 14:25:09 +000016440 continue;
16441 }
16442
Alexey Bataeve3727102018-04-18 15:57:46 +000016443 const Expr *ConflictExpr;
Samuel Antao6890b092016-07-28 14:25:09 +000016444 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000016445 D, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +000016446 [&ConflictExpr](
16447 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
16448 OpenMPClauseKind) -> bool {
16449 ConflictExpr = R.front().getAssociatedExpression();
16450 return true;
16451 })) {
16452 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
16453 Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
16454 << ConflictExpr->getSourceRange();
16455 continue;
16456 }
16457
16458 // Store the components in the stack so that they can be used to check
16459 // against other clauses later on.
16460 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
16461 DSAStack->addMappableExpressionComponents(
16462 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
16463
16464 // Record the expression we've just processed.
16465 MVLI.ProcessedVarList.push_back(SimpleRefExpr);
16466
16467 // Create a mappable component for the list item. List items in this clause
16468 // only need a component. We use a null declaration to signal fields in
16469 // 'this'.
16470 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
16471 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
16472 "Unexpected device pointer expression!");
16473 MVLI.VarBaseDeclarations.push_back(
16474 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
16475 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
16476 MVLI.VarComponents.back().push_back(MC);
Carlo Bertolli70594e92016-07-13 17:16:49 +000016477 }
16478
Samuel Antao6890b092016-07-28 14:25:09 +000016479 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli70594e92016-07-13 17:16:49 +000016480 return nullptr;
16481
Michael Kruse4304e9d2019-02-19 16:38:20 +000016482 return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList,
16483 MVLI.VarBaseDeclarations,
16484 MVLI.VarComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +000016485}
Alexey Bataeve04483e2019-03-27 14:14:31 +000016486
16487OMPClause *Sema::ActOnOpenMPAllocateClause(
16488 Expr *Allocator, ArrayRef<Expr *> VarList, SourceLocation StartLoc,
16489 SourceLocation ColonLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
16490 if (Allocator) {
16491 // OpenMP [2.11.4 allocate Clause, Description]
16492 // allocator is an expression of omp_allocator_handle_t type.
16493 if (!findOMPAllocatorHandleT(*this, Allocator->getExprLoc(), DSAStack))
16494 return nullptr;
16495
16496 ExprResult AllocatorRes = DefaultLvalueConversion(Allocator);
16497 if (AllocatorRes.isInvalid())
16498 return nullptr;
16499 AllocatorRes = PerformImplicitConversion(AllocatorRes.get(),
16500 DSAStack->getOMPAllocatorHandleT(),
16501 Sema::AA_Initializing,
16502 /*AllowExplicit=*/true);
16503 if (AllocatorRes.isInvalid())
16504 return nullptr;
16505 Allocator = AllocatorRes.get();
Alexey Bataev84c8bae2019-04-01 16:56:59 +000016506 } else {
16507 // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions.
16508 // allocate clauses that appear on a target construct or on constructs in a
16509 // target region must specify an allocator expression unless a requires
16510 // directive with the dynamic_allocators clause is present in the same
16511 // compilation unit.
16512 if (LangOpts.OpenMPIsDevice &&
16513 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
16514 targetDiag(StartLoc, diag::err_expected_allocator_expression);
Alexey Bataeve04483e2019-03-27 14:14:31 +000016515 }
16516 // Analyze and build list of variables.
16517 SmallVector<Expr *, 8> Vars;
16518 for (Expr *RefExpr : VarList) {
16519 assert(RefExpr && "NULL expr in OpenMP private clause.");
16520 SourceLocation ELoc;
16521 SourceRange ERange;
16522 Expr *SimpleRefExpr = RefExpr;
16523 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
16524 if (Res.second) {
16525 // It will be analyzed later.
16526 Vars.push_back(RefExpr);
16527 }
16528 ValueDecl *D = Res.first;
16529 if (!D)
16530 continue;
16531
16532 auto *VD = dyn_cast<VarDecl>(D);
16533 DeclRefExpr *Ref = nullptr;
16534 if (!VD && !CurContext->isDependentContext())
16535 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
16536 Vars.push_back((VD || CurContext->isDependentContext())
16537 ? RefExpr->IgnoreParens()
16538 : Ref);
16539 }
16540
16541 if (Vars.empty())
16542 return nullptr;
16543
16544 return OMPAllocateClause::Create(Context, StartLoc, LParenLoc, Allocator,
16545 ColonLoc, EndLoc, Vars);
16546}