blob: c5db691ebb68ba8b4bde97a1259774eab7c1b32c [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 Bataev60705422018-10-30 15:50:12 +0000173 /// true if all the vaiables in the target executable directives must be
174 /// 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 Bataev13314bf2014-10-09 04:18:56 +0000969 if (isOpenMPParallelDirective(DVar.DKind) ||
970 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000971 DVar.CKind = OMPC_shared;
972 return DVar;
973 }
974
975 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
976 // in a Construct, implicitly determined, p.4]
977 // In a task construct, if no default clause is present, a variable that in
978 // the enclosing context is determined to be shared by all implicit tasks
979 // bound to the current team is shared.
Alexey Bataev35aaee62016-04-13 13:36:48 +0000980 if (isOpenMPTaskingDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000981 DSAVarData DVarTemp;
Richard Smith375dec52019-05-30 23:21:14 +0000982 const_iterator I = Iter, E = end();
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000983 do {
984 ++I;
Alexey Bataeved09d242014-05-28 05:53:51 +0000985 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
Alexey Bataev35aaee62016-04-13 13:36:48 +0000986 // Referenced in a Construct, implicitly determined, p.6]
Alexey Bataev758e55e2013-09-06 18:03:48 +0000987 // In a task construct, if no default clause is present, a variable
988 // whose data-sharing attribute is not determined by the rules above is
989 // firstprivate.
990 DVarTemp = getDSA(I, D);
991 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000992 DVar.RefExpr = nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000993 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000994 return DVar;
995 }
Alexey Bataev7e6803e2019-01-09 15:58:05 +0000996 } while (I != E && !isImplicitTaskingRegion(I->Directive));
Alexey Bataev758e55e2013-09-06 18:03:48 +0000997 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000998 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000999 return DVar;
1000 }
1001 }
1002 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1003 // in a Construct, implicitly determined, p.3]
1004 // For constructs other than task, if no default clause is present, these
1005 // variables inherit their data-sharing attributes from the enclosing
1006 // context.
Dmitry Polukhindc78bc822016-04-01 09:52:30 +00001007 return getDSA(++Iter, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001008}
1009
Alexey Bataeve3727102018-04-18 15:57:46 +00001010const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
1011 const Expr *NewDE) {
Alexey Bataev4b465392017-04-26 15:06:24 +00001012 assert(!isStackEmpty() && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001013 D = getCanonicalDecl(D);
Richard Smith375dec52019-05-30 23:21:14 +00001014 SharingMapTy &StackElem = getTopOfStack();
Alexey Bataev4b465392017-04-26 15:06:24 +00001015 auto It = StackElem.AlignedMap.find(D);
1016 if (It == StackElem.AlignedMap.end()) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001017 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
Alexey Bataev4b465392017-04-26 15:06:24 +00001018 StackElem.AlignedMap[D] = NewDE;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001019 return nullptr;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001020 }
Alexey Bataeve3727102018-04-18 15:57:46 +00001021 assert(It->second && "Unexpected nullptr expr in the aligned map");
1022 return It->second;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001023}
1024
Alexey Bataeve3727102018-04-18 15:57:46 +00001025void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
Alexey Bataev4b465392017-04-26 15:06:24 +00001026 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001027 D = getCanonicalDecl(D);
Richard Smith375dec52019-05-30 23:21:14 +00001028 SharingMapTy &StackElem = getTopOfStack();
Alexey Bataeve3727102018-04-18 15:57:46 +00001029 StackElem.LCVMap.try_emplace(
1030 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
Alexey Bataev9c821032015-04-30 04:23:23 +00001031}
1032
Alexey Bataeve3727102018-04-18 15:57:46 +00001033const DSAStackTy::LCDeclInfo
1034DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001035 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001036 D = getCanonicalDecl(D);
Richard Smith375dec52019-05-30 23:21:14 +00001037 const SharingMapTy &StackElem = getTopOfStack();
Alexey Bataev4b465392017-04-26 15:06:24 +00001038 auto It = StackElem.LCVMap.find(D);
1039 if (It != StackElem.LCVMap.end())
1040 return It->second;
1041 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +00001042}
1043
Alexey Bataeve3727102018-04-18 15:57:46 +00001044const DSAStackTy::LCDeclInfo
1045DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
Richard Smith375dec52019-05-30 23:21:14 +00001046 const SharingMapTy *Parent = getSecondOnStackOrNull();
1047 assert(Parent && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001048 D = getCanonicalDecl(D);
Richard Smith375dec52019-05-30 23:21:14 +00001049 auto It = Parent->LCVMap.find(D);
1050 if (It != Parent->LCVMap.end())
Alexey Bataev4b465392017-04-26 15:06:24 +00001051 return It->second;
1052 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +00001053}
1054
Alexey Bataeve3727102018-04-18 15:57:46 +00001055const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
Richard Smith375dec52019-05-30 23:21:14 +00001056 const SharingMapTy *Parent = getSecondOnStackOrNull();
1057 assert(Parent && "Data-sharing attributes stack is empty");
1058 if (Parent->LCVMap.size() < I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00001059 return nullptr;
Richard Smith375dec52019-05-30 23:21:14 +00001060 for (const auto &Pair : Parent->LCVMap)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001061 if (Pair.second.first == I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00001062 return Pair.first;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00001063 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +00001064}
1065
Alexey Bataeve3727102018-04-18 15:57:46 +00001066void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
Alexey Bataev90c228f2016-02-08 09:29:13 +00001067 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001068 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001069 if (A == OMPC_threadprivate) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001070 DSAInfo &Data = Threadprivates[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001071 Data.Attributes = A;
1072 Data.RefExpr.setPointer(E);
1073 Data.PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001074 } else {
Richard Smith375dec52019-05-30 23:21:14 +00001075 DSAInfo &Data = getTopOfStack().SharingMap[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001076 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
1077 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
1078 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
1079 (isLoopControlVariable(D).first && A == OMPC_private));
1080 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
1081 Data.RefExpr.setInt(/*IntVal=*/true);
1082 return;
1083 }
1084 const bool IsLastprivate =
1085 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
1086 Data.Attributes = A;
1087 Data.RefExpr.setPointerAndInt(E, IsLastprivate);
1088 Data.PrivateCopy = PrivateCopy;
1089 if (PrivateCopy) {
Richard Smith375dec52019-05-30 23:21:14 +00001090 DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()];
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001091 Data.Attributes = A;
1092 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
1093 Data.PrivateCopy = nullptr;
1094 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001095 }
1096}
1097
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001098/// Build a variable declaration for OpenMP loop iteration variable.
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001099static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001100 StringRef Name, const AttrVec *Attrs = nullptr,
1101 DeclRefExpr *OrigRef = nullptr) {
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001102 DeclContext *DC = SemaRef.CurContext;
1103 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
1104 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
Alexey Bataeve3727102018-04-18 15:57:46 +00001105 auto *Decl =
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001106 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
1107 if (Attrs) {
1108 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
1109 I != E; ++I)
1110 Decl->addAttr(*I);
1111 }
1112 Decl->setImplicit();
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001113 if (OrigRef) {
1114 Decl->addAttr(
1115 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
1116 }
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001117 return Decl;
1118}
1119
1120static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
1121 SourceLocation Loc,
1122 bool RefersToCapture = false) {
1123 D->setReferenced();
1124 D->markUsed(S.Context);
1125 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
1126 SourceLocation(), D, RefersToCapture, Loc, Ty,
1127 VK_LValue);
1128}
1129
Alexey Bataeve3727102018-04-18 15:57:46 +00001130void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001131 BinaryOperatorKind BOK) {
Alexey Bataevfa312f32017-07-21 18:48:21 +00001132 D = getCanonicalDecl(D);
1133 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +00001134 assert(
Richard Smith375dec52019-05-30 23:21:14 +00001135 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +00001136 "Additional reduction info may be specified only for reduction items.");
Richard Smith375dec52019-05-30 23:21:14 +00001137 ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
Alexey Bataevfa312f32017-07-21 18:48:21 +00001138 assert(ReductionData.ReductionRange.isInvalid() &&
Richard Smith375dec52019-05-30 23:21:14 +00001139 getTopOfStack().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +00001140 "Additional reduction info may be specified only once for reduction "
1141 "items.");
1142 ReductionData.set(BOK, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001143 Expr *&TaskgroupReductionRef =
Richard Smith375dec52019-05-30 23:21:14 +00001144 getTopOfStack().TaskgroupReductionRef;
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001145 if (!TaskgroupReductionRef) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001146 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1147 SemaRef.Context.VoidPtrTy, ".task_red.");
Alexey Bataevd070a582017-10-25 15:54:04 +00001148 TaskgroupReductionRef =
1149 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001150 }
Alexey Bataevfa312f32017-07-21 18:48:21 +00001151}
1152
Alexey Bataeve3727102018-04-18 15:57:46 +00001153void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001154 const Expr *ReductionRef) {
Alexey Bataevfa312f32017-07-21 18:48:21 +00001155 D = getCanonicalDecl(D);
1156 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +00001157 assert(
Richard Smith375dec52019-05-30 23:21:14 +00001158 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +00001159 "Additional reduction info may be specified only for reduction items.");
Richard Smith375dec52019-05-30 23:21:14 +00001160 ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
Alexey Bataevfa312f32017-07-21 18:48:21 +00001161 assert(ReductionData.ReductionRange.isInvalid() &&
Richard Smith375dec52019-05-30 23:21:14 +00001162 getTopOfStack().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +00001163 "Additional reduction info may be specified only once for reduction "
1164 "items.");
1165 ReductionData.set(ReductionRef, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001166 Expr *&TaskgroupReductionRef =
Richard Smith375dec52019-05-30 23:21:14 +00001167 getTopOfStack().TaskgroupReductionRef;
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001168 if (!TaskgroupReductionRef) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001169 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1170 SemaRef.Context.VoidPtrTy, ".task_red.");
Alexey Bataevd070a582017-10-25 15:54:04 +00001171 TaskgroupReductionRef =
1172 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001173 }
Alexey Bataevfa312f32017-07-21 18:48:21 +00001174}
1175
Alexey Bataeve3727102018-04-18 15:57:46 +00001176const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1177 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
1178 Expr *&TaskgroupDescriptor) const {
Alexey Bataevfa312f32017-07-21 18:48:21 +00001179 D = getCanonicalDecl(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001180 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
Richard Smith375dec52019-05-30 23:21:14 +00001181 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001182 const DSAInfo &Data = I->SharingMap.lookup(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001183 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
Alexey Bataevfa312f32017-07-21 18:48:21 +00001184 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001185 const ReductionData &ReductionData = I->ReductionMap.lookup(D);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001186 if (!ReductionData.ReductionOp ||
1187 ReductionData.ReductionOp.is<const Expr *>())
Alexey Bataevf189cb72017-07-24 14:52:13 +00001188 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001189 SR = ReductionData.ReductionRange;
Alexey Bataevf87fa882017-07-21 19:26:22 +00001190 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
Alexey Bataev88202be2017-07-27 13:20:36 +00001191 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1192 "expression for the descriptor is not "
1193 "set.");
1194 TaskgroupDescriptor = I->TaskgroupReductionRef;
Alexey Bataevf189cb72017-07-24 14:52:13 +00001195 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1196 Data.PrivateCopy, I->DefaultAttrLoc);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001197 }
Alexey Bataevf189cb72017-07-24 14:52:13 +00001198 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001199}
1200
Alexey Bataeve3727102018-04-18 15:57:46 +00001201const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1202 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
1203 Expr *&TaskgroupDescriptor) const {
Alexey Bataevfa312f32017-07-21 18:48:21 +00001204 D = getCanonicalDecl(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001205 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
Richard Smith375dec52019-05-30 23:21:14 +00001206 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001207 const DSAInfo &Data = I->SharingMap.lookup(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001208 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
Alexey Bataevfa312f32017-07-21 18:48:21 +00001209 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001210 const ReductionData &ReductionData = I->ReductionMap.lookup(D);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001211 if (!ReductionData.ReductionOp ||
1212 !ReductionData.ReductionOp.is<const Expr *>())
Alexey Bataevf189cb72017-07-24 14:52:13 +00001213 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001214 SR = ReductionData.ReductionRange;
1215 ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
Alexey Bataev88202be2017-07-27 13:20:36 +00001216 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1217 "expression for the descriptor is not "
1218 "set.");
1219 TaskgroupDescriptor = I->TaskgroupReductionRef;
Alexey Bataevf189cb72017-07-24 14:52:13 +00001220 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1221 Data.PrivateCopy, I->DefaultAttrLoc);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001222 }
Alexey Bataevf189cb72017-07-24 14:52:13 +00001223 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001224}
1225
Richard Smith375dec52019-05-30 23:21:14 +00001226bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001227 D = D->getCanonicalDecl();
Richard Smith375dec52019-05-30 23:21:14 +00001228 for (const_iterator E = end(); I != E; ++I) {
1229 if (isImplicitOrExplicitTaskingRegion(I->Directive) ||
1230 isOpenMPTargetExecutionDirective(I->Directive)) {
1231 Scope *TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
1232 Scope *CurScope = getCurScope();
1233 while (CurScope && CurScope != TopScope && !CurScope->isDeclScope(D))
1234 CurScope = CurScope->getParent();
1235 return CurScope != TopScope;
1236 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001237 }
Alexey Bataevec3da872014-01-31 05:15:34 +00001238 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001239}
1240
Joel E. Dennyd2649292019-01-04 22:11:56 +00001241static bool isConstNotMutableType(Sema &SemaRef, QualType Type,
1242 bool AcceptIfMutable = true,
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001243 bool *IsClassType = nullptr) {
1244 ASTContext &Context = SemaRef.getASTContext();
Joel E. Dennyd2649292019-01-04 22:11:56 +00001245 Type = Type.getNonReferenceType().getCanonicalType();
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001246 bool IsConstant = Type.isConstant(Context);
1247 Type = Context.getBaseElementType(Type);
Joel E. Dennyd2649292019-01-04 22:11:56 +00001248 const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus
1249 ? Type->getAsCXXRecordDecl()
1250 : nullptr;
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001251 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1252 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
1253 RD = CTD->getTemplatedDecl();
1254 if (IsClassType)
1255 *IsClassType = RD;
1256 return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD &&
1257 RD->hasDefinition() && RD->hasMutableFields());
1258}
1259
Joel E. Dennyd2649292019-01-04 22:11:56 +00001260static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D,
1261 QualType Type, OpenMPClauseKind CKind,
1262 SourceLocation ELoc,
1263 bool AcceptIfMutable = true,
1264 bool ListItemNotVar = false) {
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001265 ASTContext &Context = SemaRef.getASTContext();
1266 bool IsClassType;
Joel E. Dennyd2649292019-01-04 22:11:56 +00001267 if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) {
1268 unsigned Diag = ListItemNotVar
1269 ? diag::err_omp_const_list_item
1270 : IsClassType ? diag::err_omp_const_not_mutable_variable
1271 : diag::err_omp_const_variable;
1272 SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind);
1273 if (!ListItemNotVar && D) {
1274 const VarDecl *VD = dyn_cast<VarDecl>(D);
1275 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
1276 VarDecl::DeclarationOnly;
1277 SemaRef.Diag(D->getLocation(),
1278 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1279 << D;
1280 }
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001281 return true;
1282 }
1283 return false;
1284}
1285
Alexey Bataeve3727102018-04-18 15:57:46 +00001286const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1287 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001288 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001289 DSAVarData DVar;
1290
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001291 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001292 auto TI = Threadprivates.find(D);
1293 if (TI != Threadprivates.end()) {
1294 DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001295 DVar.CKind = OMPC_threadprivate;
1296 return DVar;
Alexey Bataeve3727102018-04-18 15:57:46 +00001297 }
1298 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
Alexey Bataev817d7f32017-11-14 21:01:01 +00001299 DVar.RefExpr = buildDeclRefExpr(
1300 SemaRef, VD, D->getType().getNonReferenceType(),
1301 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1302 DVar.CKind = OMPC_threadprivate;
1303 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
Alexey Bataev852525d2018-03-02 17:17:12 +00001304 return DVar;
1305 }
1306 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1307 // in a Construct, C/C++, predetermined, p.1]
1308 // Variables appearing in threadprivate directives are threadprivate.
1309 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
1310 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1311 SemaRef.getLangOpts().OpenMPUseTLS &&
1312 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
1313 (VD && VD->getStorageClass() == SC_Register &&
1314 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
1315 DVar.RefExpr = buildDeclRefExpr(
1316 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
1317 DVar.CKind = OMPC_threadprivate;
1318 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1319 return DVar;
1320 }
1321 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
1322 VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
1323 !isLoopControlVariable(D).first) {
Richard Smith375dec52019-05-30 23:21:14 +00001324 const_iterator IterTarget =
1325 std::find_if(begin(), end(), [](const SharingMapTy &Data) {
1326 return isOpenMPTargetExecutionDirective(Data.Directive);
1327 });
1328 if (IterTarget != end()) {
1329 const_iterator ParentIterTarget = IterTarget + 1;
1330 for (const_iterator Iter = begin();
1331 Iter != ParentIterTarget; ++Iter) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001332 if (isOpenMPLocal(VD, Iter)) {
1333 DVar.RefExpr =
1334 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1335 D->getLocation());
1336 DVar.CKind = OMPC_threadprivate;
1337 return DVar;
1338 }
Alexey Bataev852525d2018-03-02 17:17:12 +00001339 }
Richard Smith375dec52019-05-30 23:21:14 +00001340 if (!isClauseParsingMode() || IterTarget != begin()) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001341 auto DSAIter = IterTarget->SharingMap.find(D);
1342 if (DSAIter != IterTarget->SharingMap.end() &&
1343 isOpenMPPrivate(DSAIter->getSecond().Attributes)) {
1344 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1345 DVar.CKind = OMPC_threadprivate;
1346 return DVar;
Alexey Bataeve3727102018-04-18 15:57:46 +00001347 }
Richard Smith375dec52019-05-30 23:21:14 +00001348 const_iterator End = end();
Alexey Bataeve3727102018-04-18 15:57:46 +00001349 if (!SemaRef.isOpenMPCapturedByRef(
Joel E. Denny7d5bc552019-08-22 03:34:30 +00001350 D, std::distance(ParentIterTarget, End),
1351 /*OpenMPCaptureLevel=*/0)) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001352 DVar.RefExpr =
1353 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1354 IterTarget->ConstructLoc);
1355 DVar.CKind = OMPC_threadprivate;
1356 return DVar;
1357 }
1358 }
1359 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001360 }
1361
Alexey Bataev4b465392017-04-26 15:06:24 +00001362 if (isStackEmpty())
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001363 // Not in OpenMP execution region and top scope was already checked.
1364 return DVar;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001365
Alexey Bataev758e55e2013-09-06 18:03:48 +00001366 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001367 // in a Construct, C/C++, predetermined, p.4]
1368 // Static data members are shared.
1369 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1370 // in a Construct, C/C++, predetermined, p.7]
1371 // Variables with static storage duration that are declared in a scope
1372 // inside the construct are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001373 if (VD && VD->isStaticDataMember()) {
Alexey Bataev73f9d9aa2019-06-28 16:16:00 +00001374 // Check for explicitly specified attributes.
1375 const_iterator I = begin();
1376 const_iterator EndI = end();
1377 if (FromParent && I != EndI)
1378 ++I;
1379 auto It = I->SharingMap.find(D);
1380 if (It != I->SharingMap.end()) {
1381 const DSAInfo &Data = It->getSecond();
1382 DVar.RefExpr = Data.RefExpr.getPointer();
1383 DVar.PrivateCopy = Data.PrivateCopy;
1384 DVar.CKind = Data.Attributes;
1385 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1386 DVar.DKind = I->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +00001387 return DVar;
Alexey Bataev73f9d9aa2019-06-28 16:16:00 +00001388 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001389
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001390 DVar.CKind = OMPC_shared;
1391 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001392 }
1393
Alexey Bataev73f9d9aa2019-06-28 16:16:00 +00001394 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001395 // The predetermined shared attribute for const-qualified types having no
1396 // mutable members was removed after OpenMP 3.1.
1397 if (SemaRef.LangOpts.OpenMP <= 31) {
1398 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1399 // in a Construct, C/C++, predetermined, p.6]
1400 // Variables with const qualified type having no mutable member are
1401 // shared.
Joel E. Dennyd2649292019-01-04 22:11:56 +00001402 if (isConstNotMutableType(SemaRef, D->getType())) {
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001403 // Variables with const-qualified type having no mutable member may be
1404 // listed in a firstprivate clause, even if they are static data members.
1405 DSAVarData DVarTemp = hasInnermostDSA(
1406 D,
1407 [](OpenMPClauseKind C) {
1408 return C == OMPC_firstprivate || C == OMPC_shared;
1409 },
1410 MatchesAlways, FromParent);
1411 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
1412 return DVarTemp;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001413
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001414 DVar.CKind = OMPC_shared;
1415 return DVar;
1416 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001417 }
1418
Alexey Bataev758e55e2013-09-06 18:03:48 +00001419 // Explicitly specified attributes and local variables with predetermined
1420 // attributes.
Richard Smith375dec52019-05-30 23:21:14 +00001421 const_iterator I = begin();
1422 const_iterator EndI = end();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001423 if (FromParent && I != EndI)
Richard Smith375dec52019-05-30 23:21:14 +00001424 ++I;
Alexey Bataeve3727102018-04-18 15:57:46 +00001425 auto It = I->SharingMap.find(D);
1426 if (It != I->SharingMap.end()) {
1427 const DSAInfo &Data = It->getSecond();
1428 DVar.RefExpr = Data.RefExpr.getPointer();
1429 DVar.PrivateCopy = Data.PrivateCopy;
1430 DVar.CKind = Data.Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001431 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev4d4624c2017-07-20 16:47:47 +00001432 DVar.DKind = I->Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001433 }
1434
1435 return DVar;
1436}
1437
Alexey Bataeve3727102018-04-18 15:57:46 +00001438const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1439 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001440 if (isStackEmpty()) {
Richard Smith375dec52019-05-30 23:21:14 +00001441 const_iterator I;
Alexey Bataev4b465392017-04-26 15:06:24 +00001442 return getDSA(I, D);
1443 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001444 D = getCanonicalDecl(D);
Richard Smith375dec52019-05-30 23:21:14 +00001445 const_iterator StartI = begin();
1446 const_iterator EndI = end();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001447 if (FromParent && StartI != EndI)
Richard Smith375dec52019-05-30 23:21:14 +00001448 ++StartI;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001449 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001450}
1451
Alexey Bataeve3727102018-04-18 15:57:46 +00001452const DSAStackTy::DSAVarData
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001453DSAStackTy::hasDSA(ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001454 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1455 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001456 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001457 if (isStackEmpty())
1458 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001459 D = getCanonicalDecl(D);
Richard Smith375dec52019-05-30 23:21:14 +00001460 const_iterator I = begin();
1461 const_iterator EndI = end();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001462 if (FromParent && I != EndI)
Richard Smith375dec52019-05-30 23:21:14 +00001463 ++I;
1464 for (; I != EndI; ++I) {
1465 if (!DPred(I->Directive) &&
1466 !isImplicitOrExplicitTaskingRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +00001467 continue;
Richard Smith375dec52019-05-30 23:21:14 +00001468 const_iterator NewI = I;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001469 DSAVarData DVar = getDSA(NewI, D);
1470 if (I == NewI && CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001471 return DVar;
Alexey Bataev60859c02017-04-27 15:10:33 +00001472 }
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001473 return {};
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001474}
1475
Alexey Bataeve3727102018-04-18 15:57:46 +00001476const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001477 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1478 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001479 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001480 if (isStackEmpty())
1481 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001482 D = getCanonicalDecl(D);
Richard Smith375dec52019-05-30 23:21:14 +00001483 const_iterator StartI = begin();
1484 const_iterator EndI = end();
Alexey Bataeve3978122016-07-19 05:06:39 +00001485 if (FromParent && StartI != EndI)
Richard Smith375dec52019-05-30 23:21:14 +00001486 ++StartI;
Alexey Bataeve3978122016-07-19 05:06:39 +00001487 if (StartI == EndI || !DPred(StartI->Directive))
Alexey Bataev4b465392017-04-26 15:06:24 +00001488 return {};
Richard Smith375dec52019-05-30 23:21:14 +00001489 const_iterator NewI = StartI;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001490 DSAVarData DVar = getDSA(NewI, D);
1491 return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData();
Alexey Bataevc5e02582014-06-16 07:08:35 +00001492}
1493
Alexey Bataevaac108a2015-06-23 04:51:00 +00001494bool DSAStackTy::hasExplicitDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00001495 const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1496 unsigned Level, bool NotLastprivate) const {
Richard Smith375dec52019-05-30 23:21:14 +00001497 if (getStackSize() <= Level)
Alexey Bataev4b465392017-04-26 15:06:24 +00001498 return false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001499 D = getCanonicalDecl(D);
Richard Smith375dec52019-05-30 23:21:14 +00001500 const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1501 auto I = StackElem.SharingMap.find(D);
1502 if (I != StackElem.SharingMap.end() &&
1503 I->getSecond().RefExpr.getPointer() &&
1504 CPred(I->getSecond().Attributes) &&
1505 (!NotLastprivate || !I->getSecond().RefExpr.getInt()))
Alexey Bataev92b33652018-11-21 19:41:10 +00001506 return true;
1507 // Check predetermined rules for the loop control variables.
Richard Smith375dec52019-05-30 23:21:14 +00001508 auto LI = StackElem.LCVMap.find(D);
1509 if (LI != StackElem.LCVMap.end())
Alexey Bataev92b33652018-11-21 19:41:10 +00001510 return CPred(OMPC_private);
1511 return false;
Alexey Bataevaac108a2015-06-23 04:51:00 +00001512}
1513
Samuel Antao4be30e92015-10-02 17:14:03 +00001514bool DSAStackTy::hasExplicitDirective(
Alexey Bataeve3727102018-04-18 15:57:46 +00001515 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1516 unsigned Level) const {
Richard Smith375dec52019-05-30 23:21:14 +00001517 if (getStackSize() <= Level)
Alexey Bataev4b465392017-04-26 15:06:24 +00001518 return false;
Richard Smith375dec52019-05-30 23:21:14 +00001519 const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1520 return DPred(StackElem.Directive);
Samuel Antao4be30e92015-10-02 17:14:03 +00001521}
1522
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001523bool DSAStackTy::hasDirective(
1524 const llvm::function_ref<bool(OpenMPDirectiveKind,
1525 const DeclarationNameInfo &, SourceLocation)>
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001526 DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001527 bool FromParent) const {
Samuel Antaof0d79752016-05-27 15:21:27 +00001528 // We look only in the enclosing region.
Richard Smith375dec52019-05-30 23:21:14 +00001529 size_t Skip = FromParent ? 2 : 1;
1530 for (const_iterator I = begin() + std::min(Skip, getStackSize()), E = end();
1531 I != E; ++I) {
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001532 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1533 return true;
1534 }
1535 return false;
1536}
1537
Alexey Bataev758e55e2013-09-06 18:03:48 +00001538void Sema::InitDataSharingAttributesStack() {
1539 VarDataSharingAttributesStack = new DSAStackTy(*this);
1540}
1541
1542#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1543
Alexey Bataev4b465392017-04-26 15:06:24 +00001544void Sema::pushOpenMPFunctionRegion() {
1545 DSAStack->pushFunction();
1546}
1547
1548void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1549 DSAStack->popFunction(OldFSI);
1550}
1551
Alexey Bataevc416e642019-02-08 18:02:25 +00001552static bool isOpenMPDeviceDelayedContext(Sema &S) {
1553 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice &&
1554 "Expected OpenMP device compilation.");
1555 return !S.isInOpenMPTargetExecutionDirective() &&
1556 !S.isInOpenMPDeclareTargetContext();
1557}
1558
Alexey Bataev729e2422019-08-23 16:11:14 +00001559namespace {
1560/// Status of the function emission on the host/device.
1561enum class FunctionEmissionStatus {
1562 Emitted,
1563 Discarded,
1564 Unknown,
1565};
1566} // anonymous namespace
1567
Alexey Bataevc416e642019-02-08 18:02:25 +00001568/// Do we know that we will eventually codegen the given function?
Alexey Bataev729e2422019-08-23 16:11:14 +00001569static FunctionEmissionStatus isKnownDeviceEmitted(Sema &S, FunctionDecl *FD) {
Alexey Bataevc416e642019-02-08 18:02:25 +00001570 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice &&
1571 "Expected OpenMP device compilation.");
1572 // Templates are emitted when they're instantiated.
1573 if (FD->isDependentContext())
Alexey Bataev729e2422019-08-23 16:11:14 +00001574 return FunctionEmissionStatus::Discarded;
Alexey Bataevc416e642019-02-08 18:02:25 +00001575
Alexey Bataev729e2422019-08-23 16:11:14 +00001576 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
1577 OMPDeclareTargetDeclAttr::getDeviceType(FD->getCanonicalDecl());
1578 if (DevTy.hasValue())
1579 return (*DevTy == OMPDeclareTargetDeclAttr::DT_Host)
1580 ? FunctionEmissionStatus::Discarded
1581 : FunctionEmissionStatus::Emitted;
Alexey Bataevc416e642019-02-08 18:02:25 +00001582
1583 // Otherwise, the function is known-emitted if it's in our set of
1584 // known-emitted functions.
Alexey Bataev729e2422019-08-23 16:11:14 +00001585 return (S.DeviceKnownEmittedFns.count(FD) > 0)
1586 ? FunctionEmissionStatus::Emitted
1587 : FunctionEmissionStatus::Unknown;
Alexey Bataevc416e642019-02-08 18:02:25 +00001588}
1589
1590Sema::DeviceDiagBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc,
1591 unsigned DiagID) {
1592 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice &&
1593 "Expected OpenMP device compilation.");
Alexey Bataev729e2422019-08-23 16:11:14 +00001594 FunctionEmissionStatus FES =
1595 isKnownDeviceEmitted(*this, getCurFunctionDecl());
1596 DeviceDiagBuilder::Kind Kind = DeviceDiagBuilder::K_Nop;
1597 switch (FES) {
1598 case FunctionEmissionStatus::Emitted:
1599 Kind = DeviceDiagBuilder::K_Immediate;
1600 break;
1601 case FunctionEmissionStatus::Unknown:
1602 Kind = isOpenMPDeviceDelayedContext(*this) ? DeviceDiagBuilder::K_Deferred
1603 : DeviceDiagBuilder::K_Immediate;
1604 break;
1605 case FunctionEmissionStatus::Discarded:
1606 Kind = DeviceDiagBuilder::K_Nop;
1607 break;
1608 }
1609
1610 return DeviceDiagBuilder(Kind, Loc, DiagID, getCurFunctionDecl(), *this);
1611}
1612
1613/// Do we know that we will eventually codegen the given function?
1614static FunctionEmissionStatus isKnownHostEmitted(Sema &S, FunctionDecl *FD) {
1615 assert(S.LangOpts.OpenMP && !S.LangOpts.OpenMPIsDevice &&
1616 "Expected OpenMP host compilation.");
1617 // In OpenMP 4.5 all the functions are host functions.
1618 if (S.LangOpts.OpenMP <= 45)
1619 return FunctionEmissionStatus::Emitted;
1620
1621 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
1622 OMPDeclareTargetDeclAttr::getDeviceType(FD->getCanonicalDecl());
1623 if (DevTy.hasValue())
1624 return (*DevTy == OMPDeclareTargetDeclAttr::DT_NoHost)
1625 ? FunctionEmissionStatus::Discarded
1626 : FunctionEmissionStatus::Emitted;
1627
1628 // Otherwise, the function is known-emitted if it's in our set of
1629 // known-emitted functions.
1630 return (S.DeviceKnownEmittedFns.count(FD) > 0)
1631 ? FunctionEmissionStatus::Emitted
1632 : FunctionEmissionStatus::Unknown;
1633}
1634
1635Sema::DeviceDiagBuilder Sema::diagIfOpenMPHostCode(SourceLocation Loc,
1636 unsigned DiagID) {
1637 assert(LangOpts.OpenMP && !LangOpts.OpenMPIsDevice &&
1638 "Expected OpenMP host compilation.");
1639 FunctionEmissionStatus FES =
1640 isKnownHostEmitted(*this, getCurFunctionDecl());
1641 DeviceDiagBuilder::Kind Kind = DeviceDiagBuilder::K_Nop;
1642 switch (FES) {
1643 case FunctionEmissionStatus::Emitted:
1644 Kind = DeviceDiagBuilder::K_Immediate;
1645 break;
1646 case FunctionEmissionStatus::Unknown:
1647 Kind = DeviceDiagBuilder::K_Deferred;
1648 break;
1649 case FunctionEmissionStatus::Discarded:
1650 Kind = DeviceDiagBuilder::K_Nop;
1651 break;
1652 }
1653
1654 return DeviceDiagBuilder(Kind, Loc, DiagID, getCurFunctionDecl(), *this);
Alexey Bataevc416e642019-02-08 18:02:25 +00001655}
1656
Alexey Bataev9fd495b2019-08-20 19:50:13 +00001657void Sema::checkOpenMPDeviceFunction(SourceLocation Loc, FunctionDecl *Callee,
1658 bool CheckForDelayedContext) {
Alexey Bataevc416e642019-02-08 18:02:25 +00001659 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice &&
1660 "Expected OpenMP device compilation.");
1661 assert(Callee && "Callee may not be null.");
Alexey Bataev729e2422019-08-23 16:11:14 +00001662 Callee = Callee->getMostRecentDecl();
Alexey Bataevc416e642019-02-08 18:02:25 +00001663 FunctionDecl *Caller = getCurFunctionDecl();
1664
Alexey Bataev729e2422019-08-23 16:11:14 +00001665 // host only function are not available on the device.
1666 if (Caller &&
1667 (isKnownDeviceEmitted(*this, Caller) == FunctionEmissionStatus::Emitted ||
1668 (!isOpenMPDeviceDelayedContext(*this) &&
1669 isKnownDeviceEmitted(*this, Caller) ==
1670 FunctionEmissionStatus::Unknown)) &&
1671 isKnownDeviceEmitted(*this, Callee) ==
1672 FunctionEmissionStatus::Discarded) {
1673 StringRef HostDevTy =
1674 getOpenMPSimpleClauseTypeName(OMPC_device_type, OMPC_DEVICE_TYPE_host);
1675 Diag(Loc, diag::err_omp_wrong_device_function_call) << HostDevTy << 0;
1676 Diag(Callee->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(),
1677 diag::note_omp_marked_device_type_here)
1678 << HostDevTy;
1679 return;
1680 }
Alexey Bataevc416e642019-02-08 18:02:25 +00001681 // If the caller is known-emitted, mark the callee as known-emitted.
1682 // Otherwise, mark the call in our call graph so we can traverse it later.
Alexey Bataev9fd495b2019-08-20 19:50:13 +00001683 if ((CheckForDelayedContext && !isOpenMPDeviceDelayedContext(*this)) ||
1684 (!Caller && !CheckForDelayedContext) ||
Alexey Bataev729e2422019-08-23 16:11:14 +00001685 (Caller &&
1686 isKnownDeviceEmitted(*this, Caller) == FunctionEmissionStatus::Emitted))
Alexey Bataev9fd495b2019-08-20 19:50:13 +00001687 markKnownEmitted(*this, Caller, Callee, Loc,
1688 [CheckForDelayedContext](Sema &S, FunctionDecl *FD) {
Alexey Bataev729e2422019-08-23 16:11:14 +00001689 return CheckForDelayedContext &&
1690 isKnownDeviceEmitted(S, FD) ==
1691 FunctionEmissionStatus::Emitted;
Alexey Bataev9fd495b2019-08-20 19:50:13 +00001692 });
Alexey Bataevc416e642019-02-08 18:02:25 +00001693 else if (Caller)
1694 DeviceCallGraph[Caller].insert({Callee, Loc});
1695}
1696
Alexey Bataev729e2422019-08-23 16:11:14 +00001697void Sema::checkOpenMPHostFunction(SourceLocation Loc, FunctionDecl *Callee,
1698 bool CheckCaller) {
1699 assert(LangOpts.OpenMP && !LangOpts.OpenMPIsDevice &&
1700 "Expected OpenMP host compilation.");
1701 assert(Callee && "Callee may not be null.");
1702 Callee = Callee->getMostRecentDecl();
1703 FunctionDecl *Caller = getCurFunctionDecl();
1704
1705 // device only function are not available on the host.
1706 if (Caller &&
1707 isKnownHostEmitted(*this, Caller) == FunctionEmissionStatus::Emitted &&
1708 isKnownHostEmitted(*this, Callee) == FunctionEmissionStatus::Discarded) {
1709 StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName(
1710 OMPC_device_type, OMPC_DEVICE_TYPE_nohost);
1711 Diag(Loc, diag::err_omp_wrong_device_function_call) << NoHostDevTy << 1;
1712 Diag(Callee->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(),
1713 diag::note_omp_marked_device_type_here)
1714 << NoHostDevTy;
1715 return;
1716 }
1717 // If the caller is known-emitted, mark the callee as known-emitted.
1718 // Otherwise, mark the call in our call graph so we can traverse it later.
1719 if ((!CheckCaller && !Caller) ||
1720 (Caller &&
1721 isKnownHostEmitted(*this, Caller) == FunctionEmissionStatus::Emitted))
1722 markKnownEmitted(
1723 *this, Caller, Callee, Loc, [CheckCaller](Sema &S, FunctionDecl *FD) {
1724 return CheckCaller &&
1725 isKnownHostEmitted(S, FD) == FunctionEmissionStatus::Emitted;
1726 });
1727 else if (Caller)
1728 DeviceCallGraph[Caller].insert({Callee, Loc});
1729}
1730
Alexey Bataev123ad192019-02-27 20:29:45 +00001731void Sema::checkOpenMPDeviceExpr(const Expr *E) {
1732 assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice &&
1733 "OpenMP device compilation mode is expected.");
1734 QualType Ty = E->getType();
1735 if ((Ty->isFloat16Type() && !Context.getTargetInfo().hasFloat16Type()) ||
Alexey Bataev8557d1a2019-06-18 18:39:26 +00001736 ((Ty->isFloat128Type() ||
1737 (Ty->isRealFloatingType() && Context.getTypeSize(Ty) == 128)) &&
1738 !Context.getTargetInfo().hasFloat128Type()) ||
Alexey Bataev123ad192019-02-27 20:29:45 +00001739 (Ty->isIntegerType() && Context.getTypeSize(Ty) == 128 &&
1740 !Context.getTargetInfo().hasInt128Type()))
Alexey Bataev62892592019-07-08 19:21:54 +00001741 targetDiag(E->getExprLoc(), diag::err_omp_unsupported_type)
1742 << static_cast<unsigned>(Context.getTypeSize(Ty)) << Ty
1743 << Context.getTargetInfo().getTriple().str() << E->getSourceRange();
Alexey Bataev123ad192019-02-27 20:29:45 +00001744}
1745
Joel E. Denny7d5bc552019-08-22 03:34:30 +00001746bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level,
1747 unsigned OpenMPCaptureLevel) const {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001748 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1749
Alexey Bataeve3727102018-04-18 15:57:46 +00001750 ASTContext &Ctx = getASTContext();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001751 bool IsByRef = true;
1752
1753 // Find the directive that is associated with the provided scope.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00001754 D = cast<ValueDecl>(D->getCanonicalDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00001755 QualType Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001756
Joel E. Denny7d5bc552019-08-22 03:34:30 +00001757 bool IsVariableUsedInMapClause = false;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001758 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001759 // This table summarizes how a given variable should be passed to the device
1760 // given its type and the clauses where it appears. This table is based on
1761 // the description in OpenMP 4.5 [2.10.4, target Construct] and
1762 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1763 //
1764 // =========================================================================
1765 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
1766 // | |(tofrom:scalar)| | pvt | | | |
1767 // =========================================================================
1768 // | scl | | | | - | | bycopy|
1769 // | scl | | - | x | - | - | bycopy|
1770 // | scl | | x | - | - | - | null |
1771 // | scl | x | | | - | | byref |
1772 // | scl | x | - | x | - | - | bycopy|
1773 // | scl | x | x | - | - | - | null |
1774 // | scl | | - | - | - | x | byref |
1775 // | scl | x | - | - | - | x | byref |
1776 //
1777 // | agg | n.a. | | | - | | byref |
1778 // | agg | n.a. | - | x | - | - | byref |
1779 // | agg | n.a. | x | - | - | - | null |
1780 // | agg | n.a. | - | - | - | x | byref |
1781 // | agg | n.a. | - | - | - | x[] | byref |
1782 //
1783 // | ptr | n.a. | | | - | | bycopy|
1784 // | ptr | n.a. | - | x | - | - | bycopy|
1785 // | ptr | n.a. | x | - | - | - | null |
1786 // | ptr | n.a. | - | - | - | x | byref |
1787 // | ptr | n.a. | - | - | - | x[] | bycopy|
1788 // | ptr | n.a. | - | - | x | | bycopy|
1789 // | ptr | n.a. | - | - | x | x | bycopy|
1790 // | ptr | n.a. | - | - | x | x[] | bycopy|
1791 // =========================================================================
1792 // Legend:
1793 // scl - scalar
1794 // ptr - pointer
1795 // agg - aggregate
1796 // x - applies
1797 // - - invalid in this combination
1798 // [] - mapped with an array section
1799 // byref - should be mapped by reference
1800 // byval - should be mapped by value
1801 // null - initialize a local variable to null on the device
1802 //
1803 // Observations:
1804 // - All scalar declarations that show up in a map clause have to be passed
1805 // by reference, because they may have been mapped in the enclosing data
1806 // environment.
1807 // - If the scalar value does not fit the size of uintptr, it has to be
1808 // passed by reference, regardless the result in the table above.
1809 // - For pointers mapped by value that have either an implicit map or an
1810 // array section, the runtime library may pass the NULL value to the
1811 // device instead of the value passed to it by the compiler.
1812
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001813 if (Ty->isReferenceType())
1814 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
Samuel Antao86ace552016-04-27 22:40:57 +00001815
1816 // Locate map clauses and see if the variable being captured is referred to
1817 // in any of those clauses. Here we only care about variables, not fields,
1818 // because fields are part of aggregates.
Samuel Antao86ace552016-04-27 22:40:57 +00001819 bool IsVariableAssociatedWithSection = false;
1820
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +00001821 DSAStack->checkMappableExprComponentListsForDeclAtLevel(
Alexey Bataeve3727102018-04-18 15:57:46 +00001822 D, Level,
1823 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D](
1824 OMPClauseMappableExprCommon::MappableExprComponentListRef
Samuel Antao6890b092016-07-28 14:25:09 +00001825 MapExprComponents,
1826 OpenMPClauseKind WhereFoundClauseKind) {
1827 // Only the map clause information influences how a variable is
1828 // captured. E.g. is_device_ptr does not require changing the default
Samuel Antao4c8035b2016-12-12 18:00:20 +00001829 // behavior.
Samuel Antao6890b092016-07-28 14:25:09 +00001830 if (WhereFoundClauseKind != OMPC_map)
1831 return false;
Samuel Antao86ace552016-04-27 22:40:57 +00001832
1833 auto EI = MapExprComponents.rbegin();
1834 auto EE = MapExprComponents.rend();
1835
1836 assert(EI != EE && "Invalid map expression!");
1837
1838 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
1839 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
1840
1841 ++EI;
1842 if (EI == EE)
1843 return false;
1844
1845 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
1846 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
1847 isa<MemberExpr>(EI->getAssociatedExpression())) {
1848 IsVariableAssociatedWithSection = true;
1849 // There is nothing more we need to know about this variable.
1850 return true;
1851 }
1852
1853 // Keep looking for more map info.
1854 return false;
1855 });
1856
1857 if (IsVariableUsedInMapClause) {
1858 // If variable is identified in a map clause it is always captured by
1859 // reference except if it is a pointer that is dereferenced somehow.
1860 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
1861 } else {
Alexey Bataev3f96fe62017-12-13 17:31:39 +00001862 // By default, all the data that has a scalar type is mapped by copy
1863 // (except for reduction variables).
1864 IsByRef =
Alexey Bataev60705422018-10-30 15:50:12 +00001865 (DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
1866 !Ty->isAnyPointerType()) ||
Alexey Bataev3f96fe62017-12-13 17:31:39 +00001867 !Ty->isScalarType() ||
1868 DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar ||
1869 DSAStack->hasExplicitDSA(
1870 D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level);
Samuel Antao86ace552016-04-27 22:40:57 +00001871 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001872 }
1873
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001874 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00001875 IsByRef =
Joel E. Denny7d5bc552019-08-22 03:34:30 +00001876 ((IsVariableUsedInMapClause &&
1877 DSAStack->getCaptureRegion(Level, OpenMPCaptureLevel) ==
1878 OMPD_target) ||
1879 !DSAStack->hasExplicitDSA(
1880 D,
1881 [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
1882 Level, /*NotLastprivate=*/true)) &&
Alexey Bataev8e769ee2017-12-22 21:01:52 +00001883 // If the variable is artificial and must be captured by value - try to
1884 // capture by value.
Alexey Bataevd2202ca2017-12-27 17:58:32 +00001885 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
1886 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue());
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001887 }
1888
Samuel Antao86ace552016-04-27 22:40:57 +00001889 // When passing data by copy, we need to make sure it fits the uintptr size
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001890 // and alignment, because the runtime library only deals with uintptr types.
1891 // If it does not fit the uintptr size, we need to pass the data by reference
1892 // instead.
1893 if (!IsByRef &&
1894 (Ctx.getTypeSizeInChars(Ty) >
1895 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001896 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001897 IsByRef = true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001898 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001899
1900 return IsByRef;
1901}
1902
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001903unsigned Sema::getOpenMPNestingLevel() const {
1904 assert(getLangOpts().OpenMP);
1905 return DSAStack->getNestingLevel();
1906}
1907
Jonas Hahnfeld87d44262017-11-18 21:00:46 +00001908bool Sema::isInOpenMPTargetExecutionDirective() const {
1909 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
1910 !DSAStack->isClauseParsingMode()) ||
1911 DSAStack->hasDirective(
1912 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1913 SourceLocation) -> bool {
1914 return isOpenMPTargetExecutionDirective(K);
1915 },
1916 false);
1917}
1918
Alexey Bataev41ebe0c2019-05-09 18:14:57 +00001919VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo,
1920 unsigned StopAt) {
Alexey Bataevf841bd92014-12-16 07:00:22 +00001921 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001922 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +00001923
Richard Smith0621a8f2019-05-31 00:45:10 +00001924 // If we want to determine whether the variable should be captured from the
1925 // perspective of the current capturing scope, and we've already left all the
1926 // capturing scopes of the top directive on the stack, check from the
1927 // perspective of its parent directive (if any) instead.
1928 DSAStackTy::ParentDirectiveScope InParentDirectiveRAII(
1929 *DSAStack, CheckScopeInfo && DSAStack->isBodyComplete());
1930
Samuel Antao4be30e92015-10-02 17:14:03 +00001931 // If we are attempting to capture a global variable in a directive with
1932 // 'target' we return true so that this global is also mapped to the device.
1933 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001934 auto *VD = dyn_cast<VarDecl>(D);
Richard Smith0621a8f2019-05-31 00:45:10 +00001935 if (VD && !VD->hasLocalStorage() &&
1936 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) {
1937 if (isInOpenMPDeclareTargetContext()) {
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001938 // Try to mark variable as declare target if it is used in capturing
1939 // regions.
Alexey Bataev217ff1e2019-08-16 20:15:02 +00001940 if (LangOpts.OpenMP <= 45 &&
1941 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001942 checkDeclIsAllowedInOpenMPTarget(nullptr, VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00001943 return nullptr;
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001944 } else if (isInOpenMPTargetExecutionDirective()) {
1945 // If the declaration is enclosed in a 'declare target' directive,
1946 // then it should not be captured.
1947 //
Alexey Bataev97b72212018-08-14 18:31:20 +00001948 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001949 return nullptr;
1950 return VD;
1951 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001952 }
Samuel Antao4be30e92015-10-02 17:14:03 +00001953
Alexey Bataev41ebe0c2019-05-09 18:14:57 +00001954 if (CheckScopeInfo) {
1955 bool OpenMPFound = false;
1956 for (unsigned I = StopAt + 1; I > 0; --I) {
1957 FunctionScopeInfo *FSI = FunctionScopes[I - 1];
1958 if(!isa<CapturingScopeInfo>(FSI))
1959 return nullptr;
1960 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI))
1961 if (RSI->CapRegionKind == CR_OpenMP) {
1962 OpenMPFound = true;
1963 break;
1964 }
1965 }
1966 if (!OpenMPFound)
1967 return nullptr;
1968 }
1969
Alexey Bataev48977c32015-08-04 08:10:48 +00001970 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1971 (!DSAStack->isClauseParsingMode() ||
1972 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001973 auto &&Info = DSAStack->isLoopControlVariable(D);
1974 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001975 (VD && VD->hasLocalStorage() &&
Alexey Bataev7e6803e2019-01-09 15:58:05 +00001976 isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001977 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001978 return VD ? VD : Info.second;
Alexey Bataeve3727102018-04-18 15:57:46 +00001979 DSAStackTy::DSAVarData DVarPrivate =
1980 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001981 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +00001982 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataeve0eb66b2019-06-21 15:08:30 +00001983 // Threadprivate variables must not be captured.
1984 if (isOpenMPThreadPrivate(DVarPrivate.CKind))
1985 return nullptr;
1986 // The variable is not private or it is the variable in the directive with
1987 // default(none) clause and not used in any clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00001988 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate,
1989 [](OpenMPDirectiveKind) { return true; },
1990 DSAStack->isClauseParsingMode());
Alexey Bataev41ebe0c2019-05-09 18:14:57 +00001991 if (DVarPrivate.CKind != OMPC_unknown ||
1992 (VD && DSAStack->getDefaultDSA() == DSA_none))
Alexey Bataev90c228f2016-02-08 09:29:13 +00001993 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001994 }
Alexey Bataev90c228f2016-02-08 09:29:13 +00001995 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +00001996}
1997
Alexey Bataevdfa430f2017-12-08 15:03:50 +00001998void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
1999 unsigned Level) const {
2000 SmallVector<OpenMPDirectiveKind, 4> Regions;
2001 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
2002 FunctionScopesIndex -= Regions.size();
2003}
2004
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00002005void Sema::startOpenMPLoop() {
2006 assert(LangOpts.OpenMP && "OpenMP must be enabled.");
2007 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
2008 DSAStack->loopInit();
2009}
2010
Alexey Bataeve3727102018-04-18 15:57:46 +00002011bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const {
Alexey Bataevaac108a2015-06-23 04:51:00 +00002012 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00002013 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2014 if (DSAStack->getAssociatedLoops() > 0 &&
2015 !DSAStack->isLoopStarted()) {
2016 DSAStack->resetPossibleLoopCounter(D);
2017 DSAStack->loopStart();
2018 return true;
2019 }
2020 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() ||
2021 DSAStack->isLoopControlVariable(D).first) &&
2022 !DSAStack->hasExplicitDSA(
2023 D, [](OpenMPClauseKind K) { return K != OMPC_private; }, Level) &&
2024 !isOpenMPSimdDirective(DSAStack->getCurrentDirective()))
2025 return true;
2026 }
Alexey Bataev0c99d192019-07-18 19:40:24 +00002027 if (const auto *VD = dyn_cast<VarDecl>(D)) {
2028 if (DSAStack->isThreadPrivate(const_cast<VarDecl *>(VD)) &&
2029 DSAStack->isForceVarCapturing() &&
2030 !DSAStack->hasExplicitDSA(
2031 D, [](OpenMPClauseKind K) { return K == OMPC_copyin; }, Level))
2032 return true;
2033 }
Alexey Bataevaac108a2015-06-23 04:51:00 +00002034 return DSAStack->hasExplicitDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002035 D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) ||
Alexey Bataev3f82cfc2017-12-13 15:28:44 +00002036 (DSAStack->isClauseParsingMode() &&
2037 DSAStack->getClauseParsingMode() == OMPC_private) ||
Alexey Bataev88202be2017-07-27 13:20:36 +00002038 // Consider taskgroup reduction descriptor variable a private to avoid
2039 // possible capture in the region.
2040 (DSAStack->hasExplicitDirective(
2041 [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; },
2042 Level) &&
2043 DSAStack->isTaskgroupReductionRef(D, Level));
Alexey Bataevaac108a2015-06-23 04:51:00 +00002044}
2045
Alexey Bataeve3727102018-04-18 15:57:46 +00002046void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
2047 unsigned Level) {
Alexey Bataev3b8d5582017-08-08 18:04:06 +00002048 assert(LangOpts.OpenMP && "OpenMP is not allowed");
2049 D = getCanonicalDecl(D);
2050 OpenMPClauseKind OMPC = OMPC_unknown;
2051 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
2052 const unsigned NewLevel = I - 1;
2053 if (DSAStack->hasExplicitDSA(D,
2054 [&OMPC](const OpenMPClauseKind K) {
2055 if (isOpenMPPrivate(K)) {
2056 OMPC = K;
2057 return true;
2058 }
2059 return false;
2060 },
2061 NewLevel))
2062 break;
2063 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
2064 D, NewLevel,
2065 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
2066 OpenMPClauseKind) { return true; })) {
2067 OMPC = OMPC_map;
2068 break;
2069 }
2070 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
2071 NewLevel)) {
Alexey Bataev63cc8e92018-03-20 14:45:59 +00002072 OMPC = OMPC_map;
2073 if (D->getType()->isScalarType() &&
2074 DSAStack->getDefaultDMAAtLevel(NewLevel) !=
2075 DefaultMapAttributes::DMA_tofrom_scalar)
2076 OMPC = OMPC_firstprivate;
Alexey Bataev3b8d5582017-08-08 18:04:06 +00002077 break;
2078 }
2079 }
2080 if (OMPC != OMPC_unknown)
2081 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC));
2082}
2083
Alexey Bataeve3727102018-04-18 15:57:46 +00002084bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D,
2085 unsigned Level) const {
Samuel Antao4be30e92015-10-02 17:14:03 +00002086 assert(LangOpts.OpenMP && "OpenMP is not allowed");
2087 // Return true if the current level is no longer enclosed in a target region.
2088
Alexey Bataeve3727102018-04-18 15:57:46 +00002089 const auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002090 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002091 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
2092 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +00002093}
2094
Alexey Bataeved09d242014-05-28 05:53:51 +00002095void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002096
Alexey Bataev729e2422019-08-23 16:11:14 +00002097void Sema::finalizeOpenMPDelayedAnalysis() {
2098 assert(LangOpts.OpenMP && "Expected OpenMP compilation mode.");
2099 // Diagnose implicit declare target functions and their callees.
2100 for (const auto &CallerCallees : DeviceCallGraph) {
2101 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
2102 OMPDeclareTargetDeclAttr::getDeviceType(
2103 CallerCallees.getFirst()->getMostRecentDecl());
2104 // Ignore host functions during device analyzis.
2105 if (LangOpts.OpenMPIsDevice && DevTy &&
2106 *DevTy == OMPDeclareTargetDeclAttr::DT_Host)
2107 continue;
2108 // Ignore nohost functions during host analyzis.
2109 if (!LangOpts.OpenMPIsDevice && DevTy &&
2110 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost)
2111 continue;
2112 for (const std::pair<CanonicalDeclPtr<FunctionDecl>, SourceLocation>
2113 &Callee : CallerCallees.getSecond()) {
2114 const FunctionDecl *FD = Callee.first->getMostRecentDecl();
2115 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
2116 OMPDeclareTargetDeclAttr::getDeviceType(FD);
2117 if (LangOpts.OpenMPIsDevice && DevTy &&
2118 *DevTy == OMPDeclareTargetDeclAttr::DT_Host) {
2119 // Diagnose host function called during device codegen.
2120 StringRef HostDevTy = getOpenMPSimpleClauseTypeName(
2121 OMPC_device_type, OMPC_DEVICE_TYPE_host);
2122 Diag(Callee.second, diag::err_omp_wrong_device_function_call)
2123 << HostDevTy << 0;
2124 Diag(FD->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(),
2125 diag::note_omp_marked_device_type_here)
2126 << HostDevTy;
2127 continue;
2128 }
2129 if (!LangOpts.OpenMPIsDevice && DevTy &&
2130 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) {
2131 // Diagnose nohost function called during host codegen.
2132 StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName(
2133 OMPC_device_type, OMPC_DEVICE_TYPE_nohost);
2134 Diag(Callee.second, diag::err_omp_wrong_device_function_call)
2135 << NoHostDevTy << 1;
2136 Diag(FD->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(),
2137 diag::note_omp_marked_device_type_here)
2138 << NoHostDevTy;
2139 continue;
2140 }
2141 }
2142 }
2143}
2144
Alexey Bataev758e55e2013-09-06 18:03:48 +00002145void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
2146 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +00002147 Scope *CurScope, SourceLocation Loc) {
2148 DSAStack->push(DKind, DirName, CurScope, Loc);
Faisal Valid143a0c2017-04-01 21:30:49 +00002149 PushExpressionEvaluationContext(
2150 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002151}
2152
Alexey Bataevaac108a2015-06-23 04:51:00 +00002153void Sema::StartOpenMPClause(OpenMPClauseKind K) {
2154 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +00002155}
2156
Alexey Bataevaac108a2015-06-23 04:51:00 +00002157void Sema::EndOpenMPClause() {
2158 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +00002159}
2160
Alexey Bataeve106f252019-04-01 14:25:31 +00002161static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
2162 ArrayRef<OMPClause *> Clauses);
2163
Alexey Bataev758e55e2013-09-06 18:03:48 +00002164void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002165 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
2166 // A variable of class type (or array thereof) that appears in a lastprivate
2167 // clause requires an accessible, unambiguous default constructor for the
2168 // class type, unless the list item is also specified in a firstprivate
2169 // clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00002170 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
2171 for (OMPClause *C : D->clauses()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00002172 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
2173 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +00002174 for (Expr *DE : Clause->varlists()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00002175 if (DE->isValueDependent() || DE->isTypeDependent()) {
2176 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002177 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00002178 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00002179 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +00002180 auto *VD = cast<VarDecl>(DRE->getDecl());
Alexey Bataev005248a2016-02-25 05:25:57 +00002181 QualType Type = VD->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +00002182 const DSAStackTy::DSAVarData DVar =
2183 DSAStack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002184 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00002185 // Generate helper private variable and initialize it with the
2186 // default value. The address of the original variable is replaced
2187 // by the address of the new private variable in CodeGen. This new
2188 // variable is not added to IdResolver, so the code in the OpenMP
2189 // region uses original variable for proper diagnostics.
Alexey Bataeve3727102018-04-18 15:57:46 +00002190 VarDecl *VDPrivate = buildVarDecl(
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00002191 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev63cc8e92018-03-20 14:45:59 +00002192 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE);
Richard Smith3beb7c62017-01-12 02:27:38 +00002193 ActOnUninitializedDecl(VDPrivate);
Alexey Bataeve106f252019-04-01 14:25:31 +00002194 if (VDPrivate->isInvalidDecl()) {
2195 PrivateCopies.push_back(nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00002196 continue;
Alexey Bataeve106f252019-04-01 14:25:31 +00002197 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00002198 PrivateCopies.push_back(buildDeclRefExpr(
2199 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00002200 } else {
2201 // The variable is also a firstprivate, so initialization sequence
2202 // for private copy is generated already.
2203 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002204 }
2205 }
Alexey Bataeve106f252019-04-01 14:25:31 +00002206 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002207 }
2208 }
Alexey Bataeve106f252019-04-01 14:25:31 +00002209 // Check allocate clauses.
2210 if (!CurContext->isDependentContext())
2211 checkAllocateClauses(*this, DSAStack, D->clauses());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002212 }
2213
Alexey Bataev758e55e2013-09-06 18:03:48 +00002214 DSAStack->pop();
2215 DiscardCleanupsInEvaluationContext();
2216 PopExpressionEvaluationContext();
2217}
2218
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002219static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
2220 Expr *NumIterations, Sema &SemaRef,
2221 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00002222
Alexey Bataeva769e072013-03-22 06:34:35 +00002223namespace {
2224
Alexey Bataeve3727102018-04-18 15:57:46 +00002225class VarDeclFilterCCC final : public CorrectionCandidateCallback {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002226private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00002227 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00002228
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002229public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00002230 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00002231 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002232 NamedDecl *ND = Candidate.getCorrectionDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +00002233 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002234 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00002235 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
2236 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00002237 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002238 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00002239 }
Bruno Ricci70ad3962019-03-25 17:08:51 +00002240
2241 std::unique_ptr<CorrectionCandidateCallback> clone() override {
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00002242 return std::make_unique<VarDeclFilterCCC>(*this);
Bruno Ricci70ad3962019-03-25 17:08:51 +00002243 }
2244
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002245};
Dmitry Polukhind69b5052016-05-09 14:59:13 +00002246
Alexey Bataeve3727102018-04-18 15:57:46 +00002247class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00002248private:
2249 Sema &SemaRef;
2250
2251public:
2252 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
2253 bool ValidateCandidate(const TypoCorrection &Candidate) override {
2254 NamedDecl *ND = Candidate.getCorrectionDecl();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002255 if (ND && ((isa<VarDecl>(ND) && ND->getKind() == Decl::Var) ||
2256 isa<FunctionDecl>(ND))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00002257 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
2258 SemaRef.getCurScope());
2259 }
2260 return false;
2261 }
Bruno Ricci70ad3962019-03-25 17:08:51 +00002262
2263 std::unique_ptr<CorrectionCandidateCallback> clone() override {
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00002264 return std::make_unique<VarOrFuncDeclFilterCCC>(*this);
Bruno Ricci70ad3962019-03-25 17:08:51 +00002265 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +00002266};
2267
Alexey Bataeved09d242014-05-28 05:53:51 +00002268} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002269
2270ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
2271 CXXScopeSpec &ScopeSpec,
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002272 const DeclarationNameInfo &Id,
2273 OpenMPDirectiveKind Kind) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002274 LookupResult Lookup(*this, Id, LookupOrdinaryName);
2275 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
2276
2277 if (Lookup.isAmbiguous())
2278 return ExprError();
2279
2280 VarDecl *VD;
2281 if (!Lookup.isSingleResult()) {
Bruno Ricci70ad3962019-03-25 17:08:51 +00002282 VarDeclFilterCCC CCC(*this);
2283 if (TypoCorrection Corrected =
2284 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC,
2285 CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00002286 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002287 PDiag(Lookup.empty()
2288 ? diag::err_undeclared_var_use_suggest
2289 : diag::err_omp_expected_var_arg_suggest)
2290 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00002291 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002292 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00002293 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
2294 : diag::err_omp_expected_var_arg)
2295 << Id.getName();
2296 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002297 }
Alexey Bataeve3727102018-04-18 15:57:46 +00002298 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
2299 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
2300 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
2301 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002302 }
2303 Lookup.suppressDiagnostics();
2304
2305 // OpenMP [2.9.2, Syntax, C/C++]
2306 // Variables must be file-scope, namespace-scope, or static block-scope.
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002307 if (Kind == OMPD_threadprivate && !VD->hasGlobalStorage()) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002308 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002309 << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal();
Alexey Bataeved09d242014-05-28 05:53:51 +00002310 bool IsDecl =
2311 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002312 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00002313 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2314 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002315 return ExprError();
2316 }
2317
Alexey Bataev7d2960b2013-09-26 03:24:06 +00002318 VarDecl *CanonicalVD = VD->getCanonicalDecl();
George Burgess IV00f70bd2018-03-01 05:43:23 +00002319 NamedDecl *ND = CanonicalVD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002320 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
2321 // A threadprivate directive for file-scope variables must appear outside
2322 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00002323 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
2324 !getCurLexicalContext()->isTranslationUnit()) {
2325 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002326 << getOpenMPDirectiveName(Kind) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +00002327 bool IsDecl =
2328 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2329 Diag(VD->getLocation(),
2330 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2331 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00002332 return ExprError();
2333 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002334 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
2335 // A threadprivate directive for static class member variables must appear
2336 // in the class definition, in the same scope in which the member
2337 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00002338 if (CanonicalVD->isStaticDataMember() &&
2339 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
2340 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002341 << getOpenMPDirectiveName(Kind) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +00002342 bool IsDecl =
2343 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2344 Diag(VD->getLocation(),
2345 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2346 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00002347 return ExprError();
2348 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002349 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
2350 // A threadprivate directive for namespace-scope variables must appear
2351 // outside any definition or declaration other than the namespace
2352 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00002353 if (CanonicalVD->getDeclContext()->isNamespace() &&
2354 (!getCurLexicalContext()->isFileContext() ||
2355 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
2356 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002357 << getOpenMPDirectiveName(Kind) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +00002358 bool IsDecl =
2359 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2360 Diag(VD->getLocation(),
2361 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2362 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00002363 return ExprError();
2364 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002365 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
2366 // A threadprivate directive for static block-scope variables must appear
2367 // in the scope of the variable and not in a nested scope.
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002368 if (CanonicalVD->isLocalVarDecl() && CurScope &&
Alexey Bataev7d2960b2013-09-26 03:24:06 +00002369 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002370 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002371 << getOpenMPDirectiveName(Kind) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +00002372 bool IsDecl =
2373 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2374 Diag(VD->getLocation(),
2375 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2376 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002377 return ExprError();
2378 }
2379
2380 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
2381 // A threadprivate directive must lexically precede all references to any
2382 // of the variables in its list.
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002383 if (Kind == OMPD_threadprivate && VD->isUsed() &&
2384 !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002385 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002386 << getOpenMPDirectiveName(Kind) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002387 return ExprError();
2388 }
2389
2390 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00002391 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
2392 SourceLocation(), VD,
2393 /*RefersToEnclosingVariableOrCapture=*/false,
2394 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002395}
2396
Alexey Bataeved09d242014-05-28 05:53:51 +00002397Sema::DeclGroupPtrTy
2398Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
2399 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002400 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002401 CurContext->addDecl(D);
2402 return DeclGroupPtrTy::make(DeclGroupRef(D));
2403 }
David Blaikie0403cb12016-01-15 23:43:25 +00002404 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00002405}
2406
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002407namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00002408class LocalVarRefChecker final
2409 : public ConstStmtVisitor<LocalVarRefChecker, bool> {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002410 Sema &SemaRef;
2411
2412public:
2413 bool VisitDeclRefExpr(const DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002414 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002415 if (VD->hasLocalStorage()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002416 SemaRef.Diag(E->getBeginLoc(),
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002417 diag::err_omp_local_var_in_threadprivate_init)
2418 << E->getSourceRange();
2419 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
2420 << VD << VD->getSourceRange();
2421 return true;
2422 }
2423 }
2424 return false;
2425 }
2426 bool VisitStmt(const Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002427 for (const Stmt *Child : S->children()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002428 if (Child && Visit(Child))
2429 return true;
2430 }
2431 return false;
2432 }
Alexey Bataev23b69422014-06-18 07:08:49 +00002433 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002434};
2435} // namespace
2436
Alexey Bataeved09d242014-05-28 05:53:51 +00002437OMPThreadPrivateDecl *
2438Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002439 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +00002440 for (Expr *RefExpr : VarList) {
2441 auto *DE = cast<DeclRefExpr>(RefExpr);
2442 auto *VD = cast<VarDecl>(DE->getDecl());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002443 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00002444
Alexey Bataev376b4a42016-02-09 09:41:09 +00002445 // Mark variable as used.
2446 VD->setReferenced();
2447 VD->markUsed(Context);
2448
Alexey Bataevf56f98c2015-04-16 05:39:01 +00002449 QualType QType = VD->getType();
2450 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
2451 // It will be analyzed later.
2452 Vars.push_back(DE);
2453 continue;
2454 }
2455
Alexey Bataeva769e072013-03-22 06:34:35 +00002456 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2457 // A threadprivate variable must not have an incomplete type.
2458 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002459 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002460 continue;
2461 }
2462
2463 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2464 // A threadprivate variable must not have a reference type.
2465 if (VD->getType()->isReferenceType()) {
2466 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002467 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
2468 bool IsDecl =
2469 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2470 Diag(VD->getLocation(),
2471 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2472 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00002473 continue;
2474 }
2475
Samuel Antaof8b50122015-07-13 22:54:53 +00002476 // Check if this is a TLS variable. If TLS is not being supported, produce
2477 // the corresponding diagnostic.
2478 if ((VD->getTLSKind() != VarDecl::TLS_None &&
2479 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
2480 getLangOpts().OpenMPUseTLS &&
2481 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00002482 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
2483 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00002484 Diag(ILoc, diag::err_omp_var_thread_local)
2485 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00002486 bool IsDecl =
2487 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2488 Diag(VD->getLocation(),
2489 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2490 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00002491 continue;
2492 }
2493
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002494 // Check if initial value of threadprivate variable reference variable with
2495 // local storage (it is not supported by runtime).
Alexey Bataeve3727102018-04-18 15:57:46 +00002496 if (const Expr *Init = VD->getAnyInitializer()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002497 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002498 if (Checker.Visit(Init))
2499 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002500 }
2501
Alexey Bataeved09d242014-05-28 05:53:51 +00002502 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00002503 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00002504 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
2505 Context, SourceRange(Loc, Loc)));
Alexey Bataeve3727102018-04-18 15:57:46 +00002506 if (ASTMutationListener *ML = Context.getASTMutationListener())
Alexey Bataev97720002014-11-11 04:05:39 +00002507 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00002508 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002509 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00002510 if (!Vars.empty()) {
2511 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
2512 Vars);
2513 D->setAccess(AS_public);
2514 }
2515 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00002516}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002517
Alexey Bataev27ef9512019-03-20 20:14:22 +00002518static OMPAllocateDeclAttr::AllocatorTypeTy
2519getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) {
2520 if (!Allocator)
2521 return OMPAllocateDeclAttr::OMPDefaultMemAlloc;
2522 if (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
2523 Allocator->isInstantiationDependent() ||
Alexey Bataev441510e2019-03-21 19:05:07 +00002524 Allocator->containsUnexpandedParameterPack())
Alexey Bataev27ef9512019-03-20 20:14:22 +00002525 return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
Alexey Bataev27ef9512019-03-20 20:14:22 +00002526 auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
Alexey Bataeve106f252019-04-01 14:25:31 +00002527 const Expr *AE = Allocator->IgnoreParenImpCasts();
Alexey Bataev27ef9512019-03-20 20:14:22 +00002528 for (int I = OMPAllocateDeclAttr::OMPDefaultMemAlloc;
2529 I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
2530 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
Alexey Bataeve106f252019-04-01 14:25:31 +00002531 const Expr *DefAllocator = Stack->getAllocator(AllocatorKind);
Alexey Bataev441510e2019-03-21 19:05:07 +00002532 llvm::FoldingSetNodeID AEId, DAEId;
2533 AE->Profile(AEId, S.getASTContext(), /*Canonical=*/true);
2534 DefAllocator->Profile(DAEId, S.getASTContext(), /*Canonical=*/true);
2535 if (AEId == DAEId) {
Alexey Bataev27ef9512019-03-20 20:14:22 +00002536 AllocatorKindRes = AllocatorKind;
2537 break;
2538 }
Alexey Bataev27ef9512019-03-20 20:14:22 +00002539 }
Alexey Bataev27ef9512019-03-20 20:14:22 +00002540 return AllocatorKindRes;
2541}
2542
Alexey Bataeve106f252019-04-01 14:25:31 +00002543static bool checkPreviousOMPAllocateAttribute(
2544 Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD,
2545 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) {
2546 if (!VD->hasAttr<OMPAllocateDeclAttr>())
2547 return false;
2548 const auto *A = VD->getAttr<OMPAllocateDeclAttr>();
2549 Expr *PrevAllocator = A->getAllocator();
2550 OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind =
2551 getAllocatorKind(S, Stack, PrevAllocator);
2552 bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind;
2553 if (AllocatorsMatch &&
2554 AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc &&
2555 Allocator && PrevAllocator) {
2556 const Expr *AE = Allocator->IgnoreParenImpCasts();
2557 const Expr *PAE = PrevAllocator->IgnoreParenImpCasts();
2558 llvm::FoldingSetNodeID AEId, PAEId;
2559 AE->Profile(AEId, S.Context, /*Canonical=*/true);
2560 PAE->Profile(PAEId, S.Context, /*Canonical=*/true);
2561 AllocatorsMatch = AEId == PAEId;
2562 }
2563 if (!AllocatorsMatch) {
2564 SmallString<256> AllocatorBuffer;
2565 llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer);
2566 if (Allocator)
2567 Allocator->printPretty(AllocatorStream, nullptr, S.getPrintingPolicy());
2568 SmallString<256> PrevAllocatorBuffer;
2569 llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer);
2570 if (PrevAllocator)
2571 PrevAllocator->printPretty(PrevAllocatorStream, nullptr,
2572 S.getPrintingPolicy());
2573
2574 SourceLocation AllocatorLoc =
2575 Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc();
2576 SourceRange AllocatorRange =
2577 Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange();
2578 SourceLocation PrevAllocatorLoc =
2579 PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation();
2580 SourceRange PrevAllocatorRange =
2581 PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange();
2582 S.Diag(AllocatorLoc, diag::warn_omp_used_different_allocator)
2583 << (Allocator ? 1 : 0) << AllocatorStream.str()
2584 << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str()
2585 << AllocatorRange;
2586 S.Diag(PrevAllocatorLoc, diag::note_omp_previous_allocator)
2587 << PrevAllocatorRange;
2588 return true;
2589 }
2590 return false;
2591}
2592
2593static void
2594applyOMPAllocateAttribute(Sema &S, VarDecl *VD,
2595 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
2596 Expr *Allocator, SourceRange SR) {
2597 if (VD->hasAttr<OMPAllocateDeclAttr>())
2598 return;
2599 if (Allocator &&
2600 (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
2601 Allocator->isInstantiationDependent() ||
2602 Allocator->containsUnexpandedParameterPack()))
2603 return;
2604 auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind,
2605 Allocator, SR);
2606 VD->addAttr(A);
2607 if (ASTMutationListener *ML = S.Context.getASTMutationListener())
2608 ML->DeclarationMarkedOpenMPAllocate(VD, A);
2609}
2610
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002611Sema::DeclGroupPtrTy Sema::ActOnOpenMPAllocateDirective(
2612 SourceLocation Loc, ArrayRef<Expr *> VarList,
2613 ArrayRef<OMPClause *> Clauses, DeclContext *Owner) {
2614 assert(Clauses.size() <= 1 && "Expected at most one clause.");
2615 Expr *Allocator = nullptr;
Alexey Bataev2213dd62019-03-22 14:41:39 +00002616 if (Clauses.empty()) {
Alexey Bataevf4936072019-03-22 15:32:02 +00002617 // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions.
2618 // allocate directives that appear in a target region must specify an
2619 // allocator clause unless a requires directive with the dynamic_allocators
2620 // clause is present in the same compilation unit.
Alexey Bataev318f431b2019-03-22 15:25:12 +00002621 if (LangOpts.OpenMPIsDevice &&
2622 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
Alexey Bataev2213dd62019-03-22 14:41:39 +00002623 targetDiag(Loc, diag::err_expected_allocator_clause);
2624 } else {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002625 Allocator = cast<OMPAllocatorClause>(Clauses.back())->getAllocator();
Alexey Bataev2213dd62019-03-22 14:41:39 +00002626 }
Alexey Bataev27ef9512019-03-20 20:14:22 +00002627 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
2628 getAllocatorKind(*this, DSAStack, Allocator);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002629 SmallVector<Expr *, 8> Vars;
2630 for (Expr *RefExpr : VarList) {
2631 auto *DE = cast<DeclRefExpr>(RefExpr);
2632 auto *VD = cast<VarDecl>(DE->getDecl());
2633
2634 // Check if this is a TLS variable or global register.
2635 if (VD->getTLSKind() != VarDecl::TLS_None ||
2636 VD->hasAttr<OMPThreadPrivateDeclAttr>() ||
2637 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
2638 !VD->isLocalVarDecl()))
2639 continue;
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002640
Alexey Bataev282555a2019-03-19 20:33:44 +00002641 // If the used several times in the allocate directive, the same allocator
2642 // must be used.
Alexey Bataeve106f252019-04-01 14:25:31 +00002643 if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD,
2644 AllocatorKind, Allocator))
2645 continue;
Alexey Bataev282555a2019-03-19 20:33:44 +00002646
Alexey Bataevd2fc9652019-03-19 18:39:11 +00002647 // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++
2648 // If a list item has a static storage type, the allocator expression in the
2649 // allocator clause must be a constant expression that evaluates to one of
2650 // the predefined memory allocator values.
2651 if (Allocator && VD->hasGlobalStorage()) {
Alexey Bataev441510e2019-03-21 19:05:07 +00002652 if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) {
Alexey Bataevd2fc9652019-03-19 18:39:11 +00002653 Diag(Allocator->getExprLoc(),
2654 diag::err_omp_expected_predefined_allocator)
2655 << Allocator->getSourceRange();
2656 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2657 VarDecl::DeclarationOnly;
2658 Diag(VD->getLocation(),
2659 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2660 << VD;
2661 continue;
2662 }
2663 }
2664
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002665 Vars.push_back(RefExpr);
Alexey Bataeve106f252019-04-01 14:25:31 +00002666 applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator,
2667 DE->getSourceRange());
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002668 }
2669 if (Vars.empty())
2670 return nullptr;
2671 if (!Owner)
2672 Owner = getCurLexicalContext();
Alexey Bataeve106f252019-04-01 14:25:31 +00002673 auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002674 D->setAccess(AS_public);
2675 Owner->addDecl(D);
2676 return DeclGroupPtrTy::make(DeclGroupRef(D));
2677}
2678
2679Sema::DeclGroupPtrTy
Kelvin Li1408f912018-09-26 04:28:39 +00002680Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc,
2681 ArrayRef<OMPClause *> ClauseList) {
2682 OMPRequiresDecl *D = nullptr;
2683 if (!CurContext->isFileContext()) {
2684 Diag(Loc, diag::err_omp_invalid_scope) << "requires";
2685 } else {
2686 D = CheckOMPRequiresDecl(Loc, ClauseList);
2687 if (D) {
2688 CurContext->addDecl(D);
2689 DSAStack->addRequiresDecl(D);
2690 }
2691 }
2692 return DeclGroupPtrTy::make(DeclGroupRef(D));
2693}
2694
2695OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc,
2696 ArrayRef<OMPClause *> ClauseList) {
Gheorghe-Teodor Bercea411a6242019-04-18 19:53:43 +00002697 /// For target specific clauses, the requires directive cannot be
2698 /// specified after the handling of any of the target regions in the
2699 /// current compilation unit.
2700 ArrayRef<SourceLocation> TargetLocations =
2701 DSAStack->getEncounteredTargetLocs();
2702 if (!TargetLocations.empty()) {
2703 for (const OMPClause *CNew : ClauseList) {
2704 // Check if any of the requires clauses affect target regions.
2705 if (isa<OMPUnifiedSharedMemoryClause>(CNew) ||
2706 isa<OMPUnifiedAddressClause>(CNew) ||
2707 isa<OMPReverseOffloadClause>(CNew) ||
2708 isa<OMPDynamicAllocatorsClause>(CNew)) {
2709 Diag(Loc, diag::err_omp_target_before_requires)
2710 << getOpenMPClauseName(CNew->getClauseKind());
2711 for (SourceLocation TargetLoc : TargetLocations) {
2712 Diag(TargetLoc, diag::note_omp_requires_encountered_target);
2713 }
2714 }
2715 }
2716 }
2717
Kelvin Li1408f912018-09-26 04:28:39 +00002718 if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
2719 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc,
2720 ClauseList);
2721 return nullptr;
2722}
2723
Alexey Bataeve3727102018-04-18 15:57:46 +00002724static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
2725 const ValueDecl *D,
2726 const DSAStackTy::DSAVarData &DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00002727 bool IsLoopIterVar = false) {
2728 if (DVar.RefExpr) {
2729 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2730 << getOpenMPClauseName(DVar.CKind);
2731 return;
2732 }
2733 enum {
2734 PDSA_StaticMemberShared,
2735 PDSA_StaticLocalVarShared,
2736 PDSA_LoopIterVarPrivate,
2737 PDSA_LoopIterVarLinear,
2738 PDSA_LoopIterVarLastprivate,
2739 PDSA_ConstVarShared,
2740 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002741 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00002742 PDSA_LocalVarPrivate,
2743 PDSA_Implicit
2744 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002745 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002746 auto ReportLoc = D->getLocation();
2747 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002748 if (IsLoopIterVar) {
2749 if (DVar.CKind == OMPC_private)
2750 Reason = PDSA_LoopIterVarPrivate;
2751 else if (DVar.CKind == OMPC_lastprivate)
2752 Reason = PDSA_LoopIterVarLastprivate;
2753 else
2754 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00002755 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
2756 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002757 Reason = PDSA_TaskVarFirstprivate;
2758 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002759 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002760 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002761 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002762 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002763 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002764 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002765 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00002766 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002767 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00002768 ReportHint = true;
2769 Reason = PDSA_LocalVarPrivate;
2770 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00002771 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002772 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00002773 << Reason << ReportHint
2774 << getOpenMPDirectiveName(Stack->getCurrentDirective());
2775 } else if (DVar.ImplicitDSALoc.isValid()) {
2776 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
2777 << getOpenMPClauseName(DVar.CKind);
2778 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00002779}
2780
Alexey Bataev758e55e2013-09-06 18:03:48 +00002781namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00002782class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
Alexey Bataev758e55e2013-09-06 18:03:48 +00002783 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002784 Sema &SemaRef;
Alexey Bataeve3727102018-04-18 15:57:46 +00002785 bool ErrorFound = false;
2786 CapturedStmt *CS = nullptr;
2787 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
2788 llvm::SmallVector<Expr *, 4> ImplicitMap;
2789 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
2790 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
Alexey Bataeved09d242014-05-28 05:53:51 +00002791
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00002792 void VisitSubCaptures(OMPExecutableDirective *S) {
2793 // Check implicitly captured variables.
2794 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt())
2795 return;
Alexey Bataev1242d8f2019-06-28 20:45:14 +00002796 visitSubCaptures(S->getInnermostCapturedStmt());
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00002797 }
2798
Alexey Bataev758e55e2013-09-06 18:03:48 +00002799public:
2800 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00002801 if (E->isTypeDependent() || E->isValueDependent() ||
2802 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2803 return;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002804 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev412254a2019-05-09 18:44:53 +00002805 // Check the datasharing rules for the expressions in the clauses.
2806 if (!CS) {
2807 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
2808 if (!CED->hasAttr<OMPCaptureNoInitAttr>()) {
2809 Visit(CED->getInit());
2810 return;
2811 }
Alexey Bataev1242d8f2019-06-28 20:45:14 +00002812 } else if (VD->isImplicit() || isa<OMPCapturedExprDecl>(VD))
2813 // Do not analyze internal variables and do not enclose them into
2814 // implicit clauses.
2815 return;
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00002816 VD = VD->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002817 // Skip internally declared variables.
Alexey Bataev412254a2019-05-09 18:44:53 +00002818 if (VD->hasLocalStorage() && CS && !CS->capturesVariable(VD))
Alexey Bataeved09d242014-05-28 05:53:51 +00002819 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002820
Alexey Bataeve3727102018-04-18 15:57:46 +00002821 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002822 // Check if the variable has explicit DSA set and stop analysis if it so.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00002823 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
David Majnemer9d168222016-08-05 17:44:54 +00002824 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002825
Alexey Bataevafe50572017-10-06 17:00:28 +00002826 // Skip internally declared static variables.
Alexey Bataev92327c52018-03-26 16:40:55 +00002827 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
Alexey Bataev97b72212018-08-14 18:31:20 +00002828 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
Alexey Bataev412254a2019-05-09 18:44:53 +00002829 if (VD->hasGlobalStorage() && CS && !CS->capturesVariable(VD) &&
Gheorghe-Teodor Bercea5254f0a2019-06-14 17:58:26 +00002830 (Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
2831 !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link))
Alexey Bataevafe50572017-10-06 17:00:28 +00002832 return;
2833
Alexey Bataeve3727102018-04-18 15:57:46 +00002834 SourceLocation ELoc = E->getExprLoc();
2835 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002836 // The default(none) clause requires that each variable that is referenced
2837 // in the construct, and does not have a predetermined data-sharing
2838 // attribute, must have its data-sharing attribute explicitly determined
2839 // by being listed in a data-sharing attribute clause.
2840 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev7e6803e2019-01-09 15:58:05 +00002841 isImplicitOrExplicitTaskingRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002842 VarsWithInheritedDSA.count(VD) == 0) {
2843 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002844 return;
2845 }
2846
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002847 if (isOpenMPTargetExecutionDirective(DKind) &&
2848 !Stack->isLoopControlVariable(VD).first) {
2849 if (!Stack->checkMappableExprComponentListsForDecl(
2850 VD, /*CurrentRegionOnly=*/true,
2851 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2852 StackComponents,
2853 OpenMPClauseKind) {
2854 // Variable is used if it has been marked as an array, array
2855 // section or the variable iself.
2856 return StackComponents.size() == 1 ||
2857 std::all_of(
2858 std::next(StackComponents.rbegin()),
2859 StackComponents.rend(),
2860 [](const OMPClauseMappableExprCommon::
2861 MappableComponent &MC) {
2862 return MC.getAssociatedDeclaration() ==
2863 nullptr &&
2864 (isa<OMPArraySectionExpr>(
2865 MC.getAssociatedExpression()) ||
2866 isa<ArraySubscriptExpr>(
2867 MC.getAssociatedExpression()));
2868 });
2869 })) {
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002870 bool IsFirstprivate = false;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002871 // By default lambdas are captured as firstprivates.
2872 if (const auto *RD =
2873 VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002874 IsFirstprivate = RD->isLambda();
2875 IsFirstprivate =
2876 IsFirstprivate ||
2877 (VD->getType().getNonReferenceType()->isScalarType() &&
Alexey Bataev92327c52018-03-26 16:40:55 +00002878 Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res);
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002879 if (IsFirstprivate)
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002880 ImplicitFirstprivate.emplace_back(E);
2881 else
2882 ImplicitMap.emplace_back(E);
2883 return;
2884 }
2885 }
2886
Alexey Bataev758e55e2013-09-06 18:03:48 +00002887 // OpenMP [2.9.3.6, Restrictions, p.2]
2888 // A list item that appears in a reduction clause of the innermost
2889 // enclosing worksharing or parallel construct may not be accessed in an
2890 // explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002891 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002892 VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2893 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002894 return isOpenMPParallelDirective(K) ||
2895 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2896 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00002897 /*FromParent=*/true);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002898 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00002899 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002900 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002901 reportOriginalDsa(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002902 return;
2903 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002904
2905 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002906 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002907 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
Alexey Bataeva495c642019-03-11 19:51:42 +00002908 !Stack->isLoopControlVariable(VD).first) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002909 ImplicitFirstprivate.push_back(E);
Alexey Bataeva495c642019-03-11 19:51:42 +00002910 return;
2911 }
2912
2913 // Store implicitly used globals with declare target link for parent
2914 // target.
2915 if (!isOpenMPTargetExecutionDirective(DKind) && Res &&
2916 *Res == OMPDeclareTargetDeclAttr::MT_Link) {
2917 Stack->addToParentTargetRegionLinkGlobals(E);
2918 return;
2919 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002920 }
2921 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002922 void VisitMemberExpr(MemberExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00002923 if (E->isTypeDependent() || E->isValueDependent() ||
2924 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2925 return;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002926 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002927 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Patrick Lystere13b1e32019-01-02 19:28:48 +00002928 if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParens())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002929 if (!FD)
2930 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002931 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002932 // Check if the variable has explicit DSA set and stop analysis if it
2933 // so.
2934 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
2935 return;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002936
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002937 if (isOpenMPTargetExecutionDirective(DKind) &&
2938 !Stack->isLoopControlVariable(FD).first &&
2939 !Stack->checkMappableExprComponentListsForDecl(
2940 FD, /*CurrentRegionOnly=*/true,
2941 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2942 StackComponents,
2943 OpenMPClauseKind) {
2944 return isa<CXXThisExpr>(
2945 cast<MemberExpr>(
2946 StackComponents.back().getAssociatedExpression())
2947 ->getBase()
2948 ->IgnoreParens());
2949 })) {
2950 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
2951 // A bit-field cannot appear in a map clause.
2952 //
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002953 if (FD->isBitField())
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002954 return;
Patrick Lystere13b1e32019-01-02 19:28:48 +00002955
2956 // Check to see if the member expression is referencing a class that
2957 // has already been explicitly mapped
2958 if (Stack->isClassPreviouslyMapped(TE->getType()))
2959 return;
2960
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002961 ImplicitMap.emplace_back(E);
2962 return;
2963 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002964
Alexey Bataeve3727102018-04-18 15:57:46 +00002965 SourceLocation ELoc = E->getExprLoc();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002966 // OpenMP [2.9.3.6, Restrictions, p.2]
2967 // A list item that appears in a reduction clause of the innermost
2968 // enclosing worksharing or parallel construct may not be accessed in
2969 // an explicit task.
2970 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002971 FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2972 [](OpenMPDirectiveKind K) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002973 return isOpenMPParallelDirective(K) ||
2974 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2975 },
2976 /*FromParent=*/true);
2977 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2978 ErrorFound = true;
2979 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002980 reportOriginalDsa(SemaRef, Stack, FD, DVar);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002981 return;
2982 }
2983
2984 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002985 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002986 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
Alexey Bataevb40e05202018-10-24 18:53:12 +00002987 !Stack->isLoopControlVariable(FD).first) {
2988 // Check if there is a captured expression for the current field in the
2989 // region. Do not mark it as firstprivate unless there is no captured
2990 // expression.
2991 // TODO: try to make it firstprivate.
2992 if (DVar.CKind != OMPC_unknown)
2993 ImplicitFirstprivate.push_back(E);
2994 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002995 return;
2996 }
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002997 if (isOpenMPTargetExecutionDirective(DKind)) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002998 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
Alexey Bataeve3727102018-04-18 15:57:46 +00002999 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
Alexey Bataevb7a9b742017-12-05 19:20:09 +00003000 /*NoDiagnose=*/true))
Alexey Bataev27041fa2017-12-05 15:22:49 +00003001 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00003002 const auto *VD = cast<ValueDecl>(
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003003 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
3004 if (!Stack->checkMappableExprComponentListsForDecl(
3005 VD, /*CurrentRegionOnly=*/true,
3006 [&CurComponents](
3007 OMPClauseMappableExprCommon::MappableExprComponentListRef
3008 StackComponents,
3009 OpenMPClauseKind) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003010 auto CCI = CurComponents.rbegin();
Alexey Bataev5ec38932017-09-26 16:19:04 +00003011 auto CCE = CurComponents.rend();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003012 for (const auto &SC : llvm::reverse(StackComponents)) {
3013 // Do both expressions have the same kind?
3014 if (CCI->getAssociatedExpression()->getStmtClass() !=
3015 SC.getAssociatedExpression()->getStmtClass())
3016 if (!(isa<OMPArraySectionExpr>(
3017 SC.getAssociatedExpression()) &&
3018 isa<ArraySubscriptExpr>(
3019 CCI->getAssociatedExpression())))
3020 return false;
3021
Alexey Bataeve3727102018-04-18 15:57:46 +00003022 const Decl *CCD = CCI->getAssociatedDeclaration();
3023 const Decl *SCD = SC.getAssociatedDeclaration();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003024 CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
3025 SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
3026 if (SCD != CCD)
3027 return false;
3028 std::advance(CCI, 1);
Alexey Bataev5ec38932017-09-26 16:19:04 +00003029 if (CCI == CCE)
3030 break;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003031 }
3032 return true;
3033 })) {
3034 Visit(E->getBase());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003035 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003036 } else {
Alexey Bataev7fcacd82016-11-28 15:55:15 +00003037 Visit(E->getBase());
Alexey Bataeve3727102018-04-18 15:57:46 +00003038 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003039 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003040 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003041 for (OMPClause *C : S->clauses()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003042 // Skip analysis of arguments of implicitly defined firstprivate clause
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003043 // for task|target directives.
3044 // Skip analysis of arguments of implicitly defined map clause for target
3045 // directives.
3046 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
3047 C->isImplicit())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003048 for (Stmt *CC : C->children()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003049 if (CC)
3050 Visit(CC);
3051 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003052 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003053 }
Alexey Bataevf07946e2018-10-29 20:17:42 +00003054 // Check implicitly captured variables.
3055 VisitSubCaptures(S);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003056 }
3057 void VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003058 for (Stmt *C : S->children()) {
Alexey Bataev8fc7b5f2018-10-25 15:35:27 +00003059 if (C) {
Joel E. Denny0fdf5a92018-12-19 15:59:47 +00003060 // Check implicitly captured variables in the task-based directives to
3061 // check if they must be firstprivatized.
3062 Visit(C);
Alexey Bataev8fc7b5f2018-10-25 15:35:27 +00003063 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003064 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003065 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003066
Alexey Bataev1242d8f2019-06-28 20:45:14 +00003067 void visitSubCaptures(CapturedStmt *S) {
3068 for (const CapturedStmt::Capture &Cap : S->captures()) {
3069 if (!Cap.capturesVariable() && !Cap.capturesVariableByCopy())
3070 continue;
3071 VarDecl *VD = Cap.getCapturedVar();
3072 // Do not try to map the variable if it or its sub-component was mapped
3073 // already.
3074 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
3075 Stack->checkMappableExprComponentListsForDecl(
3076 VD, /*CurrentRegionOnly=*/true,
3077 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
3078 OpenMPClauseKind) { return true; }))
3079 continue;
3080 DeclRefExpr *DRE = buildDeclRefExpr(
3081 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context),
3082 Cap.getLocation(), /*RefersToCapture=*/true);
3083 Visit(DRE);
3084 }
3085 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003086 bool isErrorFound() const { return ErrorFound; }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003087 ArrayRef<Expr *> getImplicitFirstprivate() const {
3088 return ImplicitFirstprivate;
3089 }
3090 ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; }
Alexey Bataeve3727102018-04-18 15:57:46 +00003091 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003092 return VarsWithInheritedDSA;
3093 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003094
Alexey Bataev7ff55242014-06-19 09:13:45 +00003095 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
Alexey Bataeva495c642019-03-11 19:51:42 +00003096 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {
3097 // Process declare target link variables for the target directives.
3098 if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) {
3099 for (DeclRefExpr *E : Stack->getLinkGlobals())
3100 Visit(E);
3101 }
3102 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003103};
Alexey Bataeved09d242014-05-28 05:53:51 +00003104} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00003105
Alexey Bataevbae9a792014-06-27 10:37:06 +00003106void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00003107 switch (DKind) {
Kelvin Li70a12c52016-07-13 21:51:49 +00003108 case OMPD_parallel:
3109 case OMPD_parallel_for:
3110 case OMPD_parallel_for_simd:
3111 case OMPD_parallel_sections:
Carlo Bertolliba1487b2017-10-04 14:12:09 +00003112 case OMPD_teams:
Alexey Bataev999277a2017-12-06 14:31:09 +00003113 case OMPD_teams_distribute:
3114 case OMPD_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003115 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Alexey Bataev2377fe92015-09-10 08:12:02 +00003116 QualType KmpInt32PtrTy =
3117 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003118 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00003119 std::make_pair(".global_tid.", KmpInt32PtrTy),
3120 std::make_pair(".bound_tid.", KmpInt32PtrTy),
3121 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00003122 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00003123 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3124 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00003125 break;
3126 }
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00003127 case OMPD_target_teams:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00003128 case OMPD_target_parallel:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00003129 case OMPD_target_parallel_for:
Alexey Bataevdfa430f2017-12-08 15:03:50 +00003130 case OMPD_target_parallel_for_simd:
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00003131 case OMPD_target_teams_distribute:
3132 case OMPD_target_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003133 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3134 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3135 QualType KmpInt32PtrTy =
3136 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3137 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00003138 FunctionProtoType::ExtProtoInfo EPI;
3139 EPI.Variadic = true;
3140 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3141 Sema::CapturedParamNameType Params[] = {
3142 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003143 std::make_pair(".part_id.", KmpInt32PtrTy),
3144 std::make_pair(".privates.", VoidPtrTy),
3145 std::make_pair(
3146 ".copy_fn.",
3147 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00003148 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3149 std::make_pair(StringRef(), QualType()) // __context with shared vars
3150 };
3151 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Joel E. Denny7d5bc552019-08-22 03:34:30 +00003152 Params, /*OpenMPCaptureLevel=*/0);
Alexey Bataev0c869ef2018-01-16 15:57:07 +00003153 // Mark this captured region as inlined, because we don't use outlined
3154 // function directly.
3155 getCurCapturedRegion()->TheCapturedDecl->addAttr(
3156 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003157 Context, AlwaysInlineAttr::Keyword_forceinline));
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003158 Sema::CapturedParamNameType ParamsTarget[] = {
3159 std::make_pair(StringRef(), QualType()) // __context with shared vars
3160 };
3161 // Start a captured region for 'target' with no implicit parameters.
3162 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Joel E. Denny7d5bc552019-08-22 03:34:30 +00003163 ParamsTarget, /*OpenMPCaptureLevel=*/1);
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00003164 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003165 std::make_pair(".global_tid.", KmpInt32PtrTy),
3166 std::make_pair(".bound_tid.", KmpInt32PtrTy),
3167 std::make_pair(StringRef(), QualType()) // __context with shared vars
3168 };
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00003169 // Start a captured region for 'teams' or 'parallel'. Both regions have
3170 // the same implicit parameters.
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003171 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Joel E. Denny7d5bc552019-08-22 03:34:30 +00003172 ParamsTeamsOrParallel, /*OpenMPCaptureLevel=*/2);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003173 break;
3174 }
Alexey Bataev8451efa2018-01-15 19:06:12 +00003175 case OMPD_target:
3176 case OMPD_target_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003177 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3178 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3179 QualType KmpInt32PtrTy =
3180 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3181 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00003182 FunctionProtoType::ExtProtoInfo EPI;
3183 EPI.Variadic = true;
3184 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3185 Sema::CapturedParamNameType Params[] = {
3186 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003187 std::make_pair(".part_id.", KmpInt32PtrTy),
3188 std::make_pair(".privates.", VoidPtrTy),
3189 std::make_pair(
3190 ".copy_fn.",
3191 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00003192 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3193 std::make_pair(StringRef(), QualType()) // __context with shared vars
3194 };
3195 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Joel E. Denny7d5bc552019-08-22 03:34:30 +00003196 Params, /*OpenMPCaptureLevel=*/0);
Alexey Bataev8451efa2018-01-15 19:06:12 +00003197 // Mark this captured region as inlined, because we don't use outlined
3198 // function directly.
3199 getCurCapturedRegion()->TheCapturedDecl->addAttr(
3200 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003201 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev8451efa2018-01-15 19:06:12 +00003202 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Joel E. Denny7d5bc552019-08-22 03:34:30 +00003203 std::make_pair(StringRef(), QualType()),
3204 /*OpenMPCaptureLevel=*/1);
Alexey Bataev8451efa2018-01-15 19:06:12 +00003205 break;
3206 }
Kelvin Li70a12c52016-07-13 21:51:49 +00003207 case OMPD_simd:
3208 case OMPD_for:
3209 case OMPD_for_simd:
3210 case OMPD_sections:
3211 case OMPD_section:
3212 case OMPD_single:
3213 case OMPD_master:
3214 case OMPD_critical:
Kelvin Lia579b912016-07-14 02:54:56 +00003215 case OMPD_taskgroup:
3216 case OMPD_distribute:
Alexey Bataev46506272017-12-05 17:41:34 +00003217 case OMPD_distribute_simd:
Kelvin Li70a12c52016-07-13 21:51:49 +00003218 case OMPD_ordered:
3219 case OMPD_atomic:
Alexey Bataev8451efa2018-01-15 19:06:12 +00003220 case OMPD_target_data: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003221 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00003222 std::make_pair(StringRef(), QualType()) // __context with shared vars
3223 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00003224 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3225 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003226 break;
3227 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003228 case OMPD_task: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003229 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3230 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3231 QualType KmpInt32PtrTy =
3232 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3233 QualType Args[] = {VoidPtrTy};
Alexey Bataev3ae88e22015-05-22 08:56:35 +00003234 FunctionProtoType::ExtProtoInfo EPI;
3235 EPI.Variadic = true;
3236 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003237 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00003238 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003239 std::make_pair(".part_id.", KmpInt32PtrTy),
3240 std::make_pair(".privates.", VoidPtrTy),
3241 std::make_pair(
3242 ".copy_fn.",
3243 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev48591dd2016-04-20 04:01:36 +00003244 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003245 std::make_pair(StringRef(), QualType()) // __context with shared vars
3246 };
3247 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3248 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00003249 // Mark this captured region as inlined, because we don't use outlined
3250 // function directly.
3251 getCurCapturedRegion()->TheCapturedDecl->addAttr(
3252 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003253 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003254 break;
3255 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00003256 case OMPD_taskloop:
3257 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00003258 QualType KmpInt32Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003259 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
3260 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00003261 QualType KmpUInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003262 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
3263 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00003264 QualType KmpInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003265 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
3266 .withConst();
3267 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3268 QualType KmpInt32PtrTy =
3269 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3270 QualType Args[] = {VoidPtrTy};
Alexey Bataev7292c292016-04-25 12:22:29 +00003271 FunctionProtoType::ExtProtoInfo EPI;
3272 EPI.Variadic = true;
3273 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00003274 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00003275 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003276 std::make_pair(".part_id.", KmpInt32PtrTy),
3277 std::make_pair(".privates.", VoidPtrTy),
Alexey Bataev7292c292016-04-25 12:22:29 +00003278 std::make_pair(
3279 ".copy_fn.",
3280 Context.getPointerType(CopyFnType).withConst().withRestrict()),
3281 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3282 std::make_pair(".lb.", KmpUInt64Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003283 std::make_pair(".ub.", KmpUInt64Ty),
3284 std::make_pair(".st.", KmpInt64Ty),
Alexey Bataev7292c292016-04-25 12:22:29 +00003285 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003286 std::make_pair(".reductions.", VoidPtrTy),
Alexey Bataev49f6e782015-12-01 04:18:41 +00003287 std::make_pair(StringRef(), QualType()) // __context with shared vars
3288 };
3289 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3290 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00003291 // Mark this captured region as inlined, because we don't use outlined
3292 // function directly.
3293 getCurCapturedRegion()->TheCapturedDecl->addAttr(
3294 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003295 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev49f6e782015-12-01 04:18:41 +00003296 break;
3297 }
Kelvin Li4a39add2016-07-05 05:00:15 +00003298 case OMPD_distribute_parallel_for_simd:
Alexey Bataev647dd842018-01-15 20:59:40 +00003299 case OMPD_distribute_parallel_for: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003300 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli9925f152016-06-27 14:55:37 +00003301 QualType KmpInt32PtrTy =
3302 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3303 Sema::CapturedParamNameType Params[] = {
3304 std::make_pair(".global_tid.", KmpInt32PtrTy),
3305 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003306 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
3307 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli9925f152016-06-27 14:55:37 +00003308 std::make_pair(StringRef(), QualType()) // __context with shared vars
3309 };
3310 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3311 Params);
3312 break;
3313 }
Alexey Bataev647dd842018-01-15 20:59:40 +00003314 case OMPD_target_teams_distribute_parallel_for:
3315 case OMPD_target_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003316 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli52978c32018-01-03 21:12:44 +00003317 QualType KmpInt32PtrTy =
3318 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003319 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
Carlo Bertolli52978c32018-01-03 21:12:44 +00003320
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003321 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00003322 FunctionProtoType::ExtProtoInfo EPI;
3323 EPI.Variadic = true;
3324 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3325 Sema::CapturedParamNameType Params[] = {
3326 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003327 std::make_pair(".part_id.", KmpInt32PtrTy),
3328 std::make_pair(".privates.", VoidPtrTy),
3329 std::make_pair(
3330 ".copy_fn.",
3331 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00003332 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3333 std::make_pair(StringRef(), QualType()) // __context with shared vars
3334 };
3335 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Joel E. Denny7d5bc552019-08-22 03:34:30 +00003336 Params, /*OpenMPCaptureLevel=*/0);
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00003337 // Mark this captured region as inlined, because we don't use outlined
3338 // function directly.
3339 getCurCapturedRegion()->TheCapturedDecl->addAttr(
3340 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003341 Context, AlwaysInlineAttr::Keyword_forceinline));
Carlo Bertolli52978c32018-01-03 21:12:44 +00003342 Sema::CapturedParamNameType ParamsTarget[] = {
3343 std::make_pair(StringRef(), QualType()) // __context with shared vars
3344 };
3345 // Start a captured region for 'target' with no implicit parameters.
3346 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Joel E. Denny7d5bc552019-08-22 03:34:30 +00003347 ParamsTarget, /*OpenMPCaptureLevel=*/1);
Carlo Bertolli52978c32018-01-03 21:12:44 +00003348
3349 Sema::CapturedParamNameType ParamsTeams[] = {
3350 std::make_pair(".global_tid.", KmpInt32PtrTy),
3351 std::make_pair(".bound_tid.", KmpInt32PtrTy),
3352 std::make_pair(StringRef(), QualType()) // __context with shared vars
3353 };
3354 // Start a captured region for 'target' with no implicit parameters.
3355 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Joel E. Denny7d5bc552019-08-22 03:34:30 +00003356 ParamsTeams, /*OpenMPCaptureLevel=*/2);
Carlo Bertolli52978c32018-01-03 21:12:44 +00003357
3358 Sema::CapturedParamNameType ParamsParallel[] = {
3359 std::make_pair(".global_tid.", KmpInt32PtrTy),
3360 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003361 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
3362 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli52978c32018-01-03 21:12:44 +00003363 std::make_pair(StringRef(), QualType()) // __context with shared vars
3364 };
3365 // Start a captured region for 'teams' or 'parallel'. Both regions have
3366 // the same implicit parameters.
3367 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Joel E. Denny7d5bc552019-08-22 03:34:30 +00003368 ParamsParallel, /*OpenMPCaptureLevel=*/3);
Carlo Bertolli52978c32018-01-03 21:12:44 +00003369 break;
3370 }
3371
Alexey Bataev46506272017-12-05 17:41:34 +00003372 case OMPD_teams_distribute_parallel_for:
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00003373 case OMPD_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003374 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli62fae152017-11-20 20:46:39 +00003375 QualType KmpInt32PtrTy =
3376 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3377
3378 Sema::CapturedParamNameType ParamsTeams[] = {
3379 std::make_pair(".global_tid.", KmpInt32PtrTy),
3380 std::make_pair(".bound_tid.", KmpInt32PtrTy),
3381 std::make_pair(StringRef(), QualType()) // __context with shared vars
3382 };
3383 // Start a captured region for 'target' with no implicit parameters.
3384 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Joel E. Denny7d5bc552019-08-22 03:34:30 +00003385 ParamsTeams, /*OpenMPCaptureLevel=*/0);
Carlo Bertolli62fae152017-11-20 20:46:39 +00003386
3387 Sema::CapturedParamNameType ParamsParallel[] = {
3388 std::make_pair(".global_tid.", KmpInt32PtrTy),
3389 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003390 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
3391 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli62fae152017-11-20 20:46:39 +00003392 std::make_pair(StringRef(), QualType()) // __context with shared vars
3393 };
3394 // Start a captured region for 'teams' or 'parallel'. Both regions have
3395 // the same implicit parameters.
3396 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Joel E. Denny7d5bc552019-08-22 03:34:30 +00003397 ParamsParallel, /*OpenMPCaptureLevel=*/1);
Carlo Bertolli62fae152017-11-20 20:46:39 +00003398 break;
3399 }
Alexey Bataev7828b252017-11-21 17:08:48 +00003400 case OMPD_target_update:
3401 case OMPD_target_enter_data:
3402 case OMPD_target_exit_data: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003403 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3404 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3405 QualType KmpInt32PtrTy =
3406 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3407 QualType Args[] = {VoidPtrTy};
Alexey Bataev7828b252017-11-21 17:08:48 +00003408 FunctionProtoType::ExtProtoInfo EPI;
3409 EPI.Variadic = true;
3410 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3411 Sema::CapturedParamNameType Params[] = {
3412 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003413 std::make_pair(".part_id.", KmpInt32PtrTy),
3414 std::make_pair(".privates.", VoidPtrTy),
3415 std::make_pair(
3416 ".copy_fn.",
3417 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev7828b252017-11-21 17:08:48 +00003418 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3419 std::make_pair(StringRef(), QualType()) // __context with shared vars
3420 };
3421 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3422 Params);
3423 // Mark this captured region as inlined, because we don't use outlined
3424 // function directly.
3425 getCurCapturedRegion()->TheCapturedDecl->addAttr(
3426 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003427 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev7828b252017-11-21 17:08:48 +00003428 break;
3429 }
Alexey Bataev9959db52014-05-06 10:08:46 +00003430 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00003431 case OMPD_allocate:
Alexey Bataevee9af452014-11-21 11:33:46 +00003432 case OMPD_taskyield:
3433 case OMPD_barrier:
3434 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003435 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00003436 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00003437 case OMPD_flush:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003438 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +00003439 case OMPD_declare_mapper:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003440 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003441 case OMPD_declare_target:
3442 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00003443 case OMPD_requires:
Alexey Bataev9959db52014-05-06 10:08:46 +00003444 llvm_unreachable("OpenMP Directive is not allowed");
3445 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00003446 llvm_unreachable("Unknown OpenMP directive");
3447 }
3448}
3449
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003450int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
3451 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
3452 getOpenMPCaptureRegions(CaptureRegions, DKind);
3453 return CaptureRegions.size();
3454}
3455
Alexey Bataev3392d762016-02-16 11:18:12 +00003456static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00003457 Expr *CaptureExpr, bool WithInit,
3458 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00003459 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00003460 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00003461 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00003462 QualType Ty = Init->getType();
3463 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00003464 if (S.getLangOpts().CPlusPlus) {
Alexey Bataev4244be22016-02-11 05:35:55 +00003465 Ty = C.getLValueReferenceType(Ty);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00003466 } else {
Alexey Bataev4244be22016-02-11 05:35:55 +00003467 Ty = C.getPointerType(Ty);
3468 ExprResult Res =
3469 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
3470 if (!Res.isUsable())
3471 return nullptr;
3472 Init = Res.get();
3473 }
Alexey Bataev61205072016-03-02 04:57:40 +00003474 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00003475 }
Alexey Bataeva7206b92016-12-20 16:51:02 +00003476 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003477 CaptureExpr->getBeginLoc());
Alexey Bataev2bbf7212016-03-03 03:52:24 +00003478 if (!WithInit)
Alexey Bataeve3727102018-04-18 15:57:46 +00003479 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
Alexey Bataev4244be22016-02-11 05:35:55 +00003480 S.CurContext->addHiddenDecl(CED);
Richard Smith3beb7c62017-01-12 02:27:38 +00003481 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00003482 return CED;
3483}
3484
Alexey Bataev61205072016-03-02 04:57:40 +00003485static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
3486 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00003487 OMPCapturedExprDecl *CD;
Alexey Bataeve3727102018-04-18 15:57:46 +00003488 if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
Alexey Bataevb7a34b62016-02-25 03:59:29 +00003489 CD = cast<OMPCapturedExprDecl>(VD);
Alexey Bataeve3727102018-04-18 15:57:46 +00003490 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00003491 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
3492 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00003493 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00003494 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00003495}
3496
Alexey Bataev5a3af132016-03-29 08:58:54 +00003497static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00003498 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00003499 if (!Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00003500 OMPCapturedExprDecl *CD = buildCaptureDecl(
3501 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
3502 /*WithInit=*/true, /*AsExpression=*/true);
Alexey Bataev5a3af132016-03-29 08:58:54 +00003503 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
3504 CaptureExpr->getExprLoc());
3505 }
3506 ExprResult Res = Ref;
3507 if (!S.getLangOpts().CPlusPlus &&
3508 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
Alexey Bataev8e769ee2017-12-22 21:01:52 +00003509 Ref->getType()->isPointerType()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00003510 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00003511 if (!Res.isUsable())
3512 return ExprError();
3513 }
3514 return S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00003515}
3516
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003517namespace {
3518// OpenMP directives parsed in this section are represented as a
3519// CapturedStatement with an associated statement. If a syntax error
3520// is detected during the parsing of the associated statement, the
3521// compiler must abort processing and close the CapturedStatement.
3522//
3523// Combined directives such as 'target parallel' have more than one
3524// nested CapturedStatements. This RAII ensures that we unwind out
3525// of all the nested CapturedStatements when an error is found.
3526class CaptureRegionUnwinderRAII {
3527private:
3528 Sema &S;
3529 bool &ErrorFound;
Alexey Bataeve3727102018-04-18 15:57:46 +00003530 OpenMPDirectiveKind DKind = OMPD_unknown;
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003531
3532public:
3533 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
3534 OpenMPDirectiveKind DKind)
3535 : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
3536 ~CaptureRegionUnwinderRAII() {
3537 if (ErrorFound) {
3538 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
3539 while (--ThisCaptureLevel >= 0)
3540 S.ActOnCapturedRegionError();
3541 }
3542 }
3543};
3544} // namespace
3545
Alexey Bataevb600ae32019-07-01 17:46:52 +00003546void Sema::tryCaptureOpenMPLambdas(ValueDecl *V) {
3547 // Capture variables captured by reference in lambdas for target-based
3548 // directives.
3549 if (!CurContext->isDependentContext() &&
3550 (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) ||
3551 isOpenMPTargetDataManagementDirective(
3552 DSAStack->getCurrentDirective()))) {
3553 QualType Type = V->getType();
3554 if (const auto *RD = Type.getCanonicalType()
3555 .getNonReferenceType()
3556 ->getAsCXXRecordDecl()) {
3557 bool SavedForceCaptureByReferenceInTargetExecutable =
3558 DSAStack->isForceCaptureByReferenceInTargetExecutable();
3559 DSAStack->setForceCaptureByReferenceInTargetExecutable(
3560 /*V=*/true);
3561 if (RD->isLambda()) {
3562 llvm::DenseMap<const VarDecl *, FieldDecl *> Captures;
3563 FieldDecl *ThisCapture;
3564 RD->getCaptureFields(Captures, ThisCapture);
3565 for (const LambdaCapture &LC : RD->captures()) {
3566 if (LC.getCaptureKind() == LCK_ByRef) {
3567 VarDecl *VD = LC.getCapturedVar();
3568 DeclContext *VDC = VD->getDeclContext();
3569 if (!VDC->Encloses(CurContext))
3570 continue;
3571 MarkVariableReferenced(LC.getLocation(), VD);
3572 } else if (LC.getCaptureKind() == LCK_This) {
3573 QualType ThisTy = getCurrentThisType();
3574 if (!ThisTy.isNull() &&
3575 Context.typesAreCompatible(ThisTy, ThisCapture->getType()))
3576 CheckCXXThisCapture(LC.getLocation());
3577 }
3578 }
3579 }
3580 DSAStack->setForceCaptureByReferenceInTargetExecutable(
3581 SavedForceCaptureByReferenceInTargetExecutable);
3582 }
3583 }
3584}
3585
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003586StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
3587 ArrayRef<OMPClause *> Clauses) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003588 bool ErrorFound = false;
3589 CaptureRegionUnwinderRAII CaptureRegionUnwinder(
3590 *this, ErrorFound, DSAStack->getCurrentDirective());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003591 if (!S.isUsable()) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003592 ErrorFound = true;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003593 return StmtError();
3594 }
Alexey Bataev993d2802015-12-28 06:23:08 +00003595
Alexey Bataev2ba67042017-11-28 21:11:44 +00003596 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
3597 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
Alexey Bataev993d2802015-12-28 06:23:08 +00003598 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00003599 OMPScheduleClause *SC = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00003600 SmallVector<const OMPLinearClause *, 4> LCs;
3601 SmallVector<const OMPClauseWithPreInit *, 4> PICs;
Alexey Bataev040d5402015-05-12 08:35:28 +00003602 // This is required for proper codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003603 for (OMPClause *Clause : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00003604 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
3605 Clause->getClauseKind() == OMPC_in_reduction) {
3606 // Capture taskgroup task_reduction descriptors inside the tasking regions
3607 // with the corresponding in_reduction items.
3608 auto *IRC = cast<OMPInReductionClause>(Clause);
Alexey Bataeve3727102018-04-18 15:57:46 +00003609 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00003610 if (E)
3611 MarkDeclarationsReferencedInExpr(E);
3612 }
Alexey Bataev16dc7b62015-05-20 03:46:04 +00003613 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00003614 Clause->getClauseKind() == OMPC_copyprivate ||
3615 (getLangOpts().OpenMPUseTLS &&
3616 getASTContext().getTargetInfo().isTLSSupported() &&
3617 Clause->getClauseKind() == OMPC_copyin)) {
3618 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00003619 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeve3727102018-04-18 15:57:46 +00003620 for (Stmt *VarRef : Clause->children()) {
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003621 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00003622 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003623 }
3624 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00003625 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev2ba67042017-11-28 21:11:44 +00003626 } else if (CaptureRegions.size() > 1 ||
3627 CaptureRegions.back() != OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003628 if (auto *C = OMPClauseWithPreInit::get(Clause))
3629 PICs.push_back(C);
Alexey Bataev005248a2016-02-25 05:25:57 +00003630 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003631 if (Expr *E = C->getPostUpdateExpr())
Alexey Bataev005248a2016-02-25 05:25:57 +00003632 MarkDeclarationsReferencedInExpr(E);
3633 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003634 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00003635 if (Clause->getClauseKind() == OMPC_schedule)
3636 SC = cast<OMPScheduleClause>(Clause);
3637 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00003638 OC = cast<OMPOrderedClause>(Clause);
3639 else if (Clause->getClauseKind() == OMPC_linear)
3640 LCs.push_back(cast<OMPLinearClause>(Clause));
3641 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00003642 // OpenMP, 2.7.1 Loop Construct, Restrictions
3643 // The nonmonotonic modifier cannot be specified if an ordered clause is
3644 // specified.
3645 if (SC &&
3646 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
3647 SC->getSecondScheduleModifier() ==
3648 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
3649 OC) {
3650 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
3651 ? SC->getFirstScheduleModifierLoc()
3652 : SC->getSecondScheduleModifierLoc(),
3653 diag::err_omp_schedule_nonmonotonic_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00003654 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev6402bca2015-12-28 07:25:51 +00003655 ErrorFound = true;
3656 }
Alexey Bataev993d2802015-12-28 06:23:08 +00003657 if (!LCs.empty() && OC && OC->getNumForLoops()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003658 for (const OMPLinearClause *C : LCs) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003659 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00003660 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev993d2802015-12-28 06:23:08 +00003661 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00003662 ErrorFound = true;
3663 }
Alexey Bataev113438c2015-12-30 12:06:23 +00003664 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
3665 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
3666 OC->getNumForLoops()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003667 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
Alexey Bataev113438c2015-12-30 12:06:23 +00003668 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
3669 ErrorFound = true;
3670 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00003671 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00003672 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003673 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003674 StmtResult SR = S;
Richard Smith0621a8f2019-05-31 00:45:10 +00003675 unsigned CompletedRegions = 0;
Alexey Bataev2ba67042017-11-28 21:11:44 +00003676 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003677 // Mark all variables in private list clauses as used in inner region.
3678 // Required for proper codegen of combined directives.
3679 // TODO: add processing for other clauses.
Alexey Bataev2ba67042017-11-28 21:11:44 +00003680 if (ThisCaptureRegion != OMPD_unknown) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003681 for (const clang::OMPClauseWithPreInit *C : PICs) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003682 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
3683 // Find the particular capture region for the clause if the
3684 // directive is a combined one with multiple capture regions.
3685 // If the directive is not a combined one, the capture region
3686 // associated with the clause is OMPD_unknown and is generated
3687 // only once.
3688 if (CaptureRegion == ThisCaptureRegion ||
3689 CaptureRegion == OMPD_unknown) {
3690 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003691 for (Decl *D : DS->decls())
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003692 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
3693 }
3694 }
3695 }
3696 }
Richard Smith0621a8f2019-05-31 00:45:10 +00003697 if (++CompletedRegions == CaptureRegions.size())
3698 DSAStack->setBodyComplete();
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003699 SR = ActOnCapturedRegionEnd(SR.get());
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003700 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003701 return SR;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003702}
3703
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00003704static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
3705 OpenMPDirectiveKind CancelRegion,
3706 SourceLocation StartLoc) {
3707 // CancelRegion is only needed for cancel and cancellation_point.
3708 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
3709 return false;
3710
3711 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
3712 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
3713 return false;
3714
3715 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
3716 << getOpenMPDirectiveName(CancelRegion);
3717 return true;
3718}
3719
Alexey Bataeve3727102018-04-18 15:57:46 +00003720static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003721 OpenMPDirectiveKind CurrentRegion,
3722 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003723 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003724 SourceLocation StartLoc) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003725 if (Stack->getCurScope()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003726 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
3727 OpenMPDirectiveKind OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003728 bool NestingProhibited = false;
3729 bool CloseNesting = true;
David Majnemer9d168222016-08-05 17:44:54 +00003730 bool OrphanSeen = false;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003731 enum {
3732 NoRecommend,
3733 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00003734 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003735 ShouldBeInTargetRegion,
3736 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003737 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00003738 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003739 // OpenMP [2.16, Nesting of Regions]
3740 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003741 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00003742 // An ordered construct with the simd clause is the only OpenMP
3743 // construct that can appear in the simd region.
David Majnemer9d168222016-08-05 17:44:54 +00003744 // Allowing a SIMD construct nested in another SIMD construct is an
Kelvin Lifd8b5742016-07-01 14:30:25 +00003745 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3746 // message.
3747 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3748 ? diag::err_omp_prohibited_region_simd
3749 : diag::warn_omp_nesting_simd);
3750 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00003751 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003752 if (ParentRegion == OMPD_atomic) {
3753 // OpenMP [2.16, Nesting of Regions]
3754 // OpenMP constructs may not be nested inside an atomic region.
3755 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3756 return true;
3757 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003758 if (CurrentRegion == OMPD_section) {
3759 // OpenMP [2.7.2, sections Construct, Restrictions]
3760 // Orphaned section directives are prohibited. That is, the section
3761 // directives must appear within the sections construct and must not be
3762 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003763 if (ParentRegion != OMPD_sections &&
3764 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003765 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3766 << (ParentRegion != OMPD_unknown)
3767 << getOpenMPDirectiveName(ParentRegion);
3768 return true;
3769 }
3770 return false;
3771 }
Alexey Bataev185e88d2019-01-08 15:53:42 +00003772 // Allow some constructs (except teams and cancellation constructs) to be
3773 // orphaned (they could be used in functions, called from OpenMP regions
3774 // with the required preconditions).
Kelvin Libf594a52016-12-17 05:48:59 +00003775 if (ParentRegion == OMPD_unknown &&
Alexey Bataev185e88d2019-01-08 15:53:42 +00003776 !isOpenMPNestingTeamsDirective(CurrentRegion) &&
3777 CurrentRegion != OMPD_cancellation_point &&
3778 CurrentRegion != OMPD_cancel)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003779 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00003780 if (CurrentRegion == OMPD_cancellation_point ||
3781 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003782 // OpenMP [2.16, Nesting of Regions]
3783 // A cancellation point construct for which construct-type-clause is
3784 // taskgroup must be nested inside a task construct. A cancellation
3785 // point construct for which construct-type-clause is not taskgroup must
3786 // be closely nested inside an OpenMP construct that matches the type
3787 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00003788 // A cancel construct for which construct-type-clause is taskgroup must be
3789 // nested inside a task construct. A cancel construct for which
3790 // construct-type-clause is not taskgroup must be closely nested inside an
3791 // OpenMP construct that matches the type specified in
3792 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003793 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003794 !((CancelRegion == OMPD_parallel &&
3795 (ParentRegion == OMPD_parallel ||
3796 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00003797 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003798 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00003799 ParentRegion == OMPD_target_parallel_for ||
3800 ParentRegion == OMPD_distribute_parallel_for ||
Alexey Bataev16e79882017-11-22 21:12:03 +00003801 ParentRegion == OMPD_teams_distribute_parallel_for ||
3802 ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003803 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3804 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00003805 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3806 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev185e88d2019-01-08 15:53:42 +00003807 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003808 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00003809 // OpenMP [2.16, Nesting of Regions]
3810 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003811 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00003812 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003813 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003814 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3815 // OpenMP [2.16, Nesting of Regions]
3816 // A critical region may not be nested (closely or otherwise) inside a
3817 // critical region with the same name. Note that this restriction is not
3818 // sufficient to prevent deadlock.
3819 SourceLocation PreviousCriticalLoc;
David Majnemer9d168222016-08-05 17:44:54 +00003820 bool DeadLock = Stack->hasDirective(
3821 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
3822 const DeclarationNameInfo &DNI,
Alexey Bataeve3727102018-04-18 15:57:46 +00003823 SourceLocation Loc) {
David Majnemer9d168222016-08-05 17:44:54 +00003824 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
3825 PreviousCriticalLoc = Loc;
3826 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003827 }
3828 return false;
David Majnemer9d168222016-08-05 17:44:54 +00003829 },
3830 false /* skip top directive */);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003831 if (DeadLock) {
3832 SemaRef.Diag(StartLoc,
3833 diag::err_omp_prohibited_region_critical_same_name)
3834 << CurrentName.getName();
3835 if (PreviousCriticalLoc.isValid())
3836 SemaRef.Diag(PreviousCriticalLoc,
3837 diag::note_omp_previous_critical_region);
3838 return true;
3839 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003840 } else if (CurrentRegion == OMPD_barrier) {
3841 // OpenMP [2.16, Nesting of Regions]
3842 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003843 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003844 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3845 isOpenMPTaskingDirective(ParentRegion) ||
3846 ParentRegion == OMPD_master ||
3847 ParentRegion == OMPD_critical ||
3848 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00003849 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00003850 !isOpenMPParallelDirective(CurrentRegion) &&
3851 !isOpenMPTeamsDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003852 // OpenMP [2.16, Nesting of Regions]
3853 // A worksharing region may not be closely nested inside a worksharing,
3854 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003855 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3856 isOpenMPTaskingDirective(ParentRegion) ||
3857 ParentRegion == OMPD_master ||
3858 ParentRegion == OMPD_critical ||
3859 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003860 Recommend = ShouldBeInParallelRegion;
3861 } else if (CurrentRegion == OMPD_ordered) {
3862 // OpenMP [2.16, Nesting of Regions]
3863 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00003864 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003865 // An ordered region must be closely nested inside a loop region (or
3866 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003867 // OpenMP [2.8.1,simd Construct, Restrictions]
3868 // An ordered construct with the simd clause is the only OpenMP construct
3869 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003870 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003871 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003872 !(isOpenMPSimdDirective(ParentRegion) ||
3873 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003874 Recommend = ShouldBeInOrderedRegion;
Kelvin Libf594a52016-12-17 05:48:59 +00003875 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00003876 // OpenMP [2.16, Nesting of Regions]
3877 // If specified, a teams construct must be contained within a target
3878 // construct.
Alexey Bataev7a54d762019-09-10 20:19:58 +00003879 NestingProhibited =
3880 (SemaRef.LangOpts.OpenMP <= 45 && ParentRegion != OMPD_target) ||
3881 (SemaRef.LangOpts.OpenMP >= 50 && ParentRegion != OMPD_unknown &&
3882 ParentRegion != OMPD_target);
Kelvin Li2b51f722016-07-26 04:32:50 +00003883 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003884 Recommend = ShouldBeInTargetRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003885 }
Kelvin Libf594a52016-12-17 05:48:59 +00003886 if (!NestingProhibited &&
3887 !isOpenMPTargetExecutionDirective(CurrentRegion) &&
3888 !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
3889 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00003890 // OpenMP [2.16, Nesting of Regions]
3891 // distribute, parallel, parallel sections, parallel workshare, and the
3892 // parallel loop and parallel loop SIMD constructs are the only OpenMP
3893 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003894 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3895 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00003896 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003897 }
David Majnemer9d168222016-08-05 17:44:54 +00003898 if (!NestingProhibited &&
Kelvin Li02532872016-08-05 14:37:37 +00003899 isOpenMPNestingDistributeDirective(CurrentRegion)) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003900 // OpenMP 4.5 [2.17 Nesting of Regions]
3901 // The region associated with the distribute construct must be strictly
3902 // nested inside a teams region
Kelvin Libf594a52016-12-17 05:48:59 +00003903 NestingProhibited =
3904 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003905 Recommend = ShouldBeInTeamsRegion;
3906 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003907 if (!NestingProhibited &&
3908 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3909 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3910 // OpenMP 4.5 [2.17 Nesting of Regions]
3911 // If a target, target update, target data, target enter data, or
3912 // target exit data construct is encountered during execution of a
3913 // target region, the behavior is unspecified.
3914 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00003915 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
Alexey Bataeve3727102018-04-18 15:57:46 +00003916 SourceLocation) {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003917 if (isOpenMPTargetExecutionDirective(K)) {
3918 OffendingRegion = K;
3919 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003920 }
3921 return false;
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003922 },
3923 false /* don't skip top directive */);
3924 CloseNesting = false;
3925 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003926 if (NestingProhibited) {
Kelvin Li2b51f722016-07-26 04:32:50 +00003927 if (OrphanSeen) {
3928 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
3929 << getOpenMPDirectiveName(CurrentRegion) << Recommend;
3930 } else {
3931 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
3932 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3933 << Recommend << getOpenMPDirectiveName(CurrentRegion);
3934 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003935 return true;
3936 }
3937 }
3938 return false;
3939}
3940
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003941static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3942 ArrayRef<OMPClause *> Clauses,
3943 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3944 bool ErrorFound = false;
3945 unsigned NamedModifiersNumber = 0;
3946 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3947 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003948 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00003949 for (const OMPClause *C : Clauses) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003950 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3951 // At most one if clause without a directive-name-modifier can appear on
3952 // the directive.
3953 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3954 if (FoundNameModifiers[CurNM]) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003955 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003956 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3957 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3958 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003959 } else if (CurNM != OMPD_unknown) {
3960 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003961 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003962 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003963 FoundNameModifiers[CurNM] = IC;
3964 if (CurNM == OMPD_unknown)
3965 continue;
3966 // Check if the specified name modifier is allowed for the current
3967 // directive.
3968 // At most one if clause with the particular directive-name-modifier can
3969 // appear on the directive.
3970 bool MatchFound = false;
3971 for (auto NM : AllowedNameModifiers) {
3972 if (CurNM == NM) {
3973 MatchFound = true;
3974 break;
3975 }
3976 }
3977 if (!MatchFound) {
3978 S.Diag(IC->getNameModifierLoc(),
3979 diag::err_omp_wrong_if_directive_name_modifier)
3980 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3981 ErrorFound = true;
3982 }
3983 }
3984 }
3985 // If any if clause on the directive includes a directive-name-modifier then
3986 // all if clauses on the directive must include a directive-name-modifier.
3987 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3988 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003989 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003990 diag::err_omp_no_more_if_clause);
3991 } else {
3992 std::string Values;
3993 std::string Sep(", ");
3994 unsigned AllowedCnt = 0;
3995 unsigned TotalAllowedNum =
3996 AllowedNameModifiers.size() - NamedModifiersNumber;
3997 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3998 ++Cnt) {
3999 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
4000 if (!FoundNameModifiers[NM]) {
4001 Values += "'";
4002 Values += getOpenMPDirectiveName(NM);
4003 Values += "'";
4004 if (AllowedCnt + 2 == TotalAllowedNum)
4005 Values += " or ";
4006 else if (AllowedCnt + 1 != TotalAllowedNum)
4007 Values += Sep;
4008 ++AllowedCnt;
4009 }
4010 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004011 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004012 diag::err_omp_unnamed_if_clause)
4013 << (TotalAllowedNum > 1) << Values;
4014 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004015 for (SourceLocation Loc : NameModifierLoc) {
Alexey Bataevecb156a2015-09-15 17:23:56 +00004016 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
4017 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004018 ErrorFound = true;
4019 }
4020 return ErrorFound;
4021}
4022
Alexey Bataeve106f252019-04-01 14:25:31 +00004023static std::pair<ValueDecl *, bool>
4024getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
4025 SourceRange &ERange, bool AllowArraySection = false) {
4026 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
4027 RefExpr->containsUnexpandedParameterPack())
4028 return std::make_pair(nullptr, true);
4029
4030 // OpenMP [3.1, C/C++]
4031 // A list item is a variable name.
4032 // OpenMP [2.9.3.3, Restrictions, p.1]
4033 // A variable that is part of another variable (as an array or
4034 // structure element) cannot appear in a private clause.
4035 RefExpr = RefExpr->IgnoreParens();
4036 enum {
4037 NoArrayExpr = -1,
4038 ArraySubscript = 0,
4039 OMPArraySection = 1
4040 } IsArrayExpr = NoArrayExpr;
4041 if (AllowArraySection) {
4042 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
4043 Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
4044 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
4045 Base = TempASE->getBase()->IgnoreParenImpCasts();
4046 RefExpr = Base;
4047 IsArrayExpr = ArraySubscript;
4048 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
4049 Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
4050 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
4051 Base = TempOASE->getBase()->IgnoreParenImpCasts();
4052 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
4053 Base = TempASE->getBase()->IgnoreParenImpCasts();
4054 RefExpr = Base;
4055 IsArrayExpr = OMPArraySection;
4056 }
4057 }
4058 ELoc = RefExpr->getExprLoc();
4059 ERange = RefExpr->getSourceRange();
4060 RefExpr = RefExpr->IgnoreParenImpCasts();
4061 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
4062 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
4063 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
4064 (S.getCurrentThisType().isNull() || !ME ||
4065 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
4066 !isa<FieldDecl>(ME->getMemberDecl()))) {
4067 if (IsArrayExpr != NoArrayExpr) {
4068 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
4069 << ERange;
4070 } else {
4071 S.Diag(ELoc,
4072 AllowArraySection
4073 ? diag::err_omp_expected_var_name_member_expr_or_array_item
4074 : diag::err_omp_expected_var_name_member_expr)
4075 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
4076 }
4077 return std::make_pair(nullptr, false);
4078 }
4079 return std::make_pair(
4080 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
4081}
4082
4083static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
Alexey Bataev471171c2019-03-28 19:15:36 +00004084 ArrayRef<OMPClause *> Clauses) {
4085 assert(!S.CurContext->isDependentContext() &&
4086 "Expected non-dependent context.");
Alexey Bataev471171c2019-03-28 19:15:36 +00004087 auto AllocateRange =
4088 llvm::make_filter_range(Clauses, OMPAllocateClause::classof);
Alexey Bataeve106f252019-04-01 14:25:31 +00004089 llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>>
4090 DeclToCopy;
4091 auto PrivateRange = llvm::make_filter_range(Clauses, [](const OMPClause *C) {
4092 return isOpenMPPrivate(C->getClauseKind());
4093 });
4094 for (OMPClause *Cl : PrivateRange) {
4095 MutableArrayRef<Expr *>::iterator I, It, Et;
4096 if (Cl->getClauseKind() == OMPC_private) {
4097 auto *PC = cast<OMPPrivateClause>(Cl);
4098 I = PC->private_copies().begin();
4099 It = PC->varlist_begin();
4100 Et = PC->varlist_end();
4101 } else if (Cl->getClauseKind() == OMPC_firstprivate) {
4102 auto *PC = cast<OMPFirstprivateClause>(Cl);
4103 I = PC->private_copies().begin();
4104 It = PC->varlist_begin();
4105 Et = PC->varlist_end();
4106 } else if (Cl->getClauseKind() == OMPC_lastprivate) {
4107 auto *PC = cast<OMPLastprivateClause>(Cl);
4108 I = PC->private_copies().begin();
4109 It = PC->varlist_begin();
4110 Et = PC->varlist_end();
4111 } else if (Cl->getClauseKind() == OMPC_linear) {
4112 auto *PC = cast<OMPLinearClause>(Cl);
4113 I = PC->privates().begin();
4114 It = PC->varlist_begin();
4115 Et = PC->varlist_end();
4116 } else if (Cl->getClauseKind() == OMPC_reduction) {
4117 auto *PC = cast<OMPReductionClause>(Cl);
4118 I = PC->privates().begin();
4119 It = PC->varlist_begin();
4120 Et = PC->varlist_end();
4121 } else if (Cl->getClauseKind() == OMPC_task_reduction) {
4122 auto *PC = cast<OMPTaskReductionClause>(Cl);
4123 I = PC->privates().begin();
4124 It = PC->varlist_begin();
4125 Et = PC->varlist_end();
4126 } else if (Cl->getClauseKind() == OMPC_in_reduction) {
4127 auto *PC = cast<OMPInReductionClause>(Cl);
4128 I = PC->privates().begin();
4129 It = PC->varlist_begin();
4130 Et = PC->varlist_end();
4131 } else {
4132 llvm_unreachable("Expected private clause.");
4133 }
4134 for (Expr *E : llvm::make_range(It, Et)) {
4135 if (!*I) {
4136 ++I;
4137 continue;
4138 }
4139 SourceLocation ELoc;
4140 SourceRange ERange;
4141 Expr *SimpleRefExpr = E;
4142 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
4143 /*AllowArraySection=*/true);
4144 DeclToCopy.try_emplace(Res.first,
4145 cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()));
4146 ++I;
4147 }
4148 }
Alexey Bataev471171c2019-03-28 19:15:36 +00004149 for (OMPClause *C : AllocateRange) {
4150 auto *AC = cast<OMPAllocateClause>(C);
4151 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
4152 getAllocatorKind(S, Stack, AC->getAllocator());
4153 // OpenMP, 2.11.4 allocate Clause, Restrictions.
4154 // For task, taskloop or target directives, allocation requests to memory
4155 // allocators with the trait access set to thread result in unspecified
4156 // behavior.
4157 if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc &&
4158 (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
4159 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()))) {
4160 S.Diag(AC->getAllocator()->getExprLoc(),
4161 diag::warn_omp_allocate_thread_on_task_target_directive)
4162 << getOpenMPDirectiveName(Stack->getCurrentDirective());
Alexey Bataeve106f252019-04-01 14:25:31 +00004163 }
4164 for (Expr *E : AC->varlists()) {
4165 SourceLocation ELoc;
4166 SourceRange ERange;
4167 Expr *SimpleRefExpr = E;
4168 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange);
4169 ValueDecl *VD = Res.first;
4170 DSAStackTy::DSAVarData Data = Stack->getTopDSA(VD, /*FromParent=*/false);
4171 if (!isOpenMPPrivate(Data.CKind)) {
4172 S.Diag(E->getExprLoc(),
4173 diag::err_omp_expected_private_copy_for_allocate);
4174 continue;
4175 }
4176 VarDecl *PrivateVD = DeclToCopy[VD];
4177 if (checkPreviousOMPAllocateAttribute(S, Stack, E, PrivateVD,
4178 AllocatorKind, AC->getAllocator()))
4179 continue;
4180 applyOMPAllocateAttribute(S, PrivateVD, AllocatorKind, AC->getAllocator(),
4181 E->getSourceRange());
Alexey Bataev471171c2019-03-28 19:15:36 +00004182 }
4183 }
Alexey Bataev471171c2019-03-28 19:15:36 +00004184}
4185
Alexey Bataev6d4ed052015-07-01 06:57:41 +00004186StmtResult Sema::ActOnOpenMPExecutableDirective(
4187 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
4188 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
4189 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004190 StmtResult Res = StmtError();
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00004191 // First check CancelRegion which is then used in checkNestingOfRegions.
4192 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
4193 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00004194 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00004195 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00004196
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004197 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataeve3727102018-04-18 15:57:46 +00004198 VarsWithInheritedDSAType VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004199 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00004200 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00004201 if (AStmt && !CurContext->isDependentContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00004202 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4203
4204 // Check default data sharing attributes for referenced variables.
4205 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
Arpith Chacko Jacob1f46b702017-01-23 15:38:49 +00004206 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
4207 Stmt *S = AStmt;
4208 while (--ThisCaptureLevel >= 0)
4209 S = cast<CapturedStmt>(S)->getCapturedStmt();
4210 DSAChecker.Visit(S);
Alexey Bataev1242d8f2019-06-28 20:45:14 +00004211 if (!isOpenMPTargetDataManagementDirective(Kind) &&
4212 !isOpenMPTaskingDirective(Kind)) {
4213 // Visit subcaptures to generate implicit clauses for captured vars.
4214 auto *CS = cast<CapturedStmt>(AStmt);
4215 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
4216 getOpenMPCaptureRegions(CaptureRegions, Kind);
4217 // Ignore outer tasking regions for target directives.
4218 if (CaptureRegions.size() > 1 && CaptureRegions.front() == OMPD_task)
4219 CS = cast<CapturedStmt>(CS->getCapturedStmt());
4220 DSAChecker.visitSubCaptures(CS);
4221 }
Alexey Bataev68446b72014-07-18 07:47:19 +00004222 if (DSAChecker.isErrorFound())
4223 return StmtError();
4224 // Generate list of implicitly defined firstprivate variables.
4225 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00004226
Alexey Bataev88202be2017-07-27 13:20:36 +00004227 SmallVector<Expr *, 4> ImplicitFirstprivates(
4228 DSAChecker.getImplicitFirstprivate().begin(),
4229 DSAChecker.getImplicitFirstprivate().end());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00004230 SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
4231 DSAChecker.getImplicitMap().end());
Alexey Bataev88202be2017-07-27 13:20:36 +00004232 // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +00004233 for (OMPClause *C : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00004234 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004235 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00004236 if (E)
4237 ImplicitFirstprivates.emplace_back(E);
4238 }
4239 }
4240 if (!ImplicitFirstprivates.empty()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00004241 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
Alexey Bataev88202be2017-07-27 13:20:36 +00004242 ImplicitFirstprivates, SourceLocation(), SourceLocation(),
4243 SourceLocation())) {
Alexey Bataev68446b72014-07-18 07:47:19 +00004244 ClausesWithImplicit.push_back(Implicit);
4245 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataev88202be2017-07-27 13:20:36 +00004246 ImplicitFirstprivates.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00004247 } else {
Alexey Bataev68446b72014-07-18 07:47:19 +00004248 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00004249 }
Alexey Bataev68446b72014-07-18 07:47:19 +00004250 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00004251 if (!ImplicitMaps.empty()) {
Michael Kruse4304e9d2019-02-19 16:38:20 +00004252 CXXScopeSpec MapperIdScopeSpec;
4253 DeclarationNameInfo MapperId;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00004254 if (OMPClause *Implicit = ActOnOpenMPMapClause(
Michael Kruse4304e9d2019-02-19 16:38:20 +00004255 llvm::None, llvm::None, MapperIdScopeSpec, MapperId,
4256 OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true, SourceLocation(),
4257 SourceLocation(), ImplicitMaps, OMPVarListLocTy())) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00004258 ClausesWithImplicit.emplace_back(Implicit);
4259 ErrorFound |=
4260 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00004261 } else {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00004262 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00004263 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00004264 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004265 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00004266
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004267 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004268 switch (Kind) {
4269 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00004270 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
4271 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004272 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004273 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004274 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00004275 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
4276 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004277 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00004278 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00004279 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
4280 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004281 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00004282 case OMPD_for_simd:
4283 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
4284 EndLoc, VarsWithInheritedDSA);
4285 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004286 case OMPD_sections:
4287 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
4288 EndLoc);
4289 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004290 case OMPD_section:
4291 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00004292 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004293 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
4294 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004295 case OMPD_single:
4296 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
4297 EndLoc);
4298 break;
Alexander Musman80c22892014-07-17 08:54:58 +00004299 case OMPD_master:
4300 assert(ClausesWithImplicit.empty() &&
4301 "No clauses are allowed for 'omp master' directive");
4302 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
4303 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004304 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00004305 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
4306 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004307 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00004308 case OMPD_parallel_for:
4309 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
4310 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004311 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004312 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00004313 case OMPD_parallel_for_simd:
4314 Res = ActOnOpenMPParallelForSimdDirective(
4315 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004316 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004317 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004318 case OMPD_parallel_sections:
4319 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
4320 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004321 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004322 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004323 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004324 Res =
4325 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004326 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004327 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00004328 case OMPD_taskyield:
4329 assert(ClausesWithImplicit.empty() &&
4330 "No clauses are allowed for 'omp taskyield' directive");
4331 assert(AStmt == nullptr &&
4332 "No associated statement allowed for 'omp taskyield' directive");
4333 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
4334 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004335 case OMPD_barrier:
4336 assert(ClausesWithImplicit.empty() &&
4337 "No clauses are allowed for 'omp barrier' directive");
4338 assert(AStmt == nullptr &&
4339 "No associated statement allowed for 'omp barrier' directive");
4340 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
4341 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00004342 case OMPD_taskwait:
4343 assert(ClausesWithImplicit.empty() &&
4344 "No clauses are allowed for 'omp taskwait' directive");
4345 assert(AStmt == nullptr &&
4346 "No associated statement allowed for 'omp taskwait' directive");
4347 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
4348 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004349 case OMPD_taskgroup:
Alexey Bataev169d96a2017-07-18 20:17:46 +00004350 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
4351 EndLoc);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004352 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00004353 case OMPD_flush:
4354 assert(AStmt == nullptr &&
4355 "No associated statement allowed for 'omp flush' directive");
4356 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
4357 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004358 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00004359 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
4360 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004361 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00004362 case OMPD_atomic:
4363 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
4364 EndLoc);
4365 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00004366 case OMPD_teams:
4367 Res =
4368 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
4369 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004370 case OMPD_target:
4371 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
4372 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004373 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004374 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00004375 case OMPD_target_parallel:
4376 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
4377 StartLoc, EndLoc);
4378 AllowedNameModifiers.push_back(OMPD_target);
4379 AllowedNameModifiers.push_back(OMPD_parallel);
4380 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00004381 case OMPD_target_parallel_for:
4382 Res = ActOnOpenMPTargetParallelForDirective(
4383 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4384 AllowedNameModifiers.push_back(OMPD_target);
4385 AllowedNameModifiers.push_back(OMPD_parallel);
4386 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00004387 case OMPD_cancellation_point:
4388 assert(ClausesWithImplicit.empty() &&
4389 "No clauses are allowed for 'omp cancellation point' directive");
4390 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
4391 "cancellation point' directive");
4392 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
4393 break;
Alexey Bataev80909872015-07-02 11:25:17 +00004394 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00004395 assert(AStmt == nullptr &&
4396 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00004397 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
4398 CancelRegion);
4399 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00004400 break;
Michael Wong65f367f2015-07-21 13:44:28 +00004401 case OMPD_target_data:
4402 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
4403 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004404 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00004405 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00004406 case OMPD_target_enter_data:
4407 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00004408 EndLoc, AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00004409 AllowedNameModifiers.push_back(OMPD_target_enter_data);
4410 break;
Samuel Antao72590762016-01-19 20:04:50 +00004411 case OMPD_target_exit_data:
4412 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00004413 EndLoc, AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00004414 AllowedNameModifiers.push_back(OMPD_target_exit_data);
4415 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00004416 case OMPD_taskloop:
4417 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
4418 EndLoc, VarsWithInheritedDSA);
4419 AllowedNameModifiers.push_back(OMPD_taskloop);
4420 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004421 case OMPD_taskloop_simd:
4422 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
4423 EndLoc, VarsWithInheritedDSA);
4424 AllowedNameModifiers.push_back(OMPD_taskloop);
4425 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004426 case OMPD_distribute:
4427 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
4428 EndLoc, VarsWithInheritedDSA);
4429 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00004430 case OMPD_target_update:
Alexey Bataev7828b252017-11-21 17:08:48 +00004431 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
4432 EndLoc, AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00004433 AllowedNameModifiers.push_back(OMPD_target_update);
4434 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00004435 case OMPD_distribute_parallel_for:
4436 Res = ActOnOpenMPDistributeParallelForDirective(
4437 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4438 AllowedNameModifiers.push_back(OMPD_parallel);
4439 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00004440 case OMPD_distribute_parallel_for_simd:
4441 Res = ActOnOpenMPDistributeParallelForSimdDirective(
4442 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4443 AllowedNameModifiers.push_back(OMPD_parallel);
4444 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00004445 case OMPD_distribute_simd:
4446 Res = ActOnOpenMPDistributeSimdDirective(
4447 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4448 break;
Kelvin Lia579b912016-07-14 02:54:56 +00004449 case OMPD_target_parallel_for_simd:
4450 Res = ActOnOpenMPTargetParallelForSimdDirective(
4451 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4452 AllowedNameModifiers.push_back(OMPD_target);
4453 AllowedNameModifiers.push_back(OMPD_parallel);
4454 break;
Kelvin Li986330c2016-07-20 22:57:10 +00004455 case OMPD_target_simd:
4456 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
4457 EndLoc, VarsWithInheritedDSA);
4458 AllowedNameModifiers.push_back(OMPD_target);
4459 break;
Kelvin Li02532872016-08-05 14:37:37 +00004460 case OMPD_teams_distribute:
David Majnemer9d168222016-08-05 17:44:54 +00004461 Res = ActOnOpenMPTeamsDistributeDirective(
4462 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Kelvin Li02532872016-08-05 14:37:37 +00004463 break;
Kelvin Li4e325f72016-10-25 12:50:55 +00004464 case OMPD_teams_distribute_simd:
4465 Res = ActOnOpenMPTeamsDistributeSimdDirective(
4466 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4467 break;
Kelvin Li579e41c2016-11-30 23:51:03 +00004468 case OMPD_teams_distribute_parallel_for_simd:
4469 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
4470 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4471 AllowedNameModifiers.push_back(OMPD_parallel);
4472 break;
Kelvin Li7ade93f2016-12-09 03:24:30 +00004473 case OMPD_teams_distribute_parallel_for:
4474 Res = ActOnOpenMPTeamsDistributeParallelForDirective(
4475 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4476 AllowedNameModifiers.push_back(OMPD_parallel);
4477 break;
Kelvin Libf594a52016-12-17 05:48:59 +00004478 case OMPD_target_teams:
4479 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
4480 EndLoc);
4481 AllowedNameModifiers.push_back(OMPD_target);
4482 break;
Kelvin Li83c451e2016-12-25 04:52:54 +00004483 case OMPD_target_teams_distribute:
4484 Res = ActOnOpenMPTargetTeamsDistributeDirective(
4485 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4486 AllowedNameModifiers.push_back(OMPD_target);
4487 break;
Kelvin Li80e8f562016-12-29 22:16:30 +00004488 case OMPD_target_teams_distribute_parallel_for:
4489 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
4490 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4491 AllowedNameModifiers.push_back(OMPD_target);
4492 AllowedNameModifiers.push_back(OMPD_parallel);
4493 break;
Kelvin Li1851df52017-01-03 05:23:48 +00004494 case OMPD_target_teams_distribute_parallel_for_simd:
4495 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
4496 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4497 AllowedNameModifiers.push_back(OMPD_target);
4498 AllowedNameModifiers.push_back(OMPD_parallel);
4499 break;
Kelvin Lida681182017-01-10 18:08:18 +00004500 case OMPD_target_teams_distribute_simd:
4501 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
4502 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4503 AllowedNameModifiers.push_back(OMPD_target);
4504 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00004505 case OMPD_declare_target:
4506 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004507 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00004508 case OMPD_allocate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00004509 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +00004510 case OMPD_declare_mapper:
Alexey Bataev587e1de2016-03-30 10:43:55 +00004511 case OMPD_declare_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00004512 case OMPD_requires:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004513 llvm_unreachable("OpenMP Directive is not allowed");
4514 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004515 llvm_unreachable("Unknown OpenMP directive");
4516 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004517
Roman Lebedevb5700602019-03-20 16:32:36 +00004518 ErrorFound = Res.isInvalid() || ErrorFound;
4519
Alexey Bataev412254a2019-05-09 18:44:53 +00004520 // Check variables in the clauses if default(none) was specified.
4521 if (DSAStack->getDefaultDSA() == DSA_none) {
4522 DSAAttrChecker DSAChecker(DSAStack, *this, nullptr);
4523 for (OMPClause *C : Clauses) {
4524 switch (C->getClauseKind()) {
4525 case OMPC_num_threads:
4526 case OMPC_dist_schedule:
4527 // Do not analyse if no parent teams directive.
4528 if (isOpenMPTeamsDirective(DSAStack->getCurrentDirective()))
4529 break;
4530 continue;
4531 case OMPC_if:
4532 if (isOpenMPTeamsDirective(DSAStack->getCurrentDirective()) &&
4533 cast<OMPIfClause>(C)->getNameModifier() != OMPD_target)
4534 break;
4535 continue;
4536 case OMPC_schedule:
4537 break;
4538 case OMPC_ordered:
4539 case OMPC_device:
4540 case OMPC_num_teams:
4541 case OMPC_thread_limit:
4542 case OMPC_priority:
4543 case OMPC_grainsize:
4544 case OMPC_num_tasks:
4545 case OMPC_hint:
4546 case OMPC_collapse:
4547 case OMPC_safelen:
4548 case OMPC_simdlen:
4549 case OMPC_final:
4550 case OMPC_default:
4551 case OMPC_proc_bind:
4552 case OMPC_private:
4553 case OMPC_firstprivate:
4554 case OMPC_lastprivate:
4555 case OMPC_shared:
4556 case OMPC_reduction:
4557 case OMPC_task_reduction:
4558 case OMPC_in_reduction:
4559 case OMPC_linear:
4560 case OMPC_aligned:
4561 case OMPC_copyin:
4562 case OMPC_copyprivate:
4563 case OMPC_nowait:
4564 case OMPC_untied:
4565 case OMPC_mergeable:
4566 case OMPC_allocate:
4567 case OMPC_read:
4568 case OMPC_write:
4569 case OMPC_update:
4570 case OMPC_capture:
4571 case OMPC_seq_cst:
4572 case OMPC_depend:
4573 case OMPC_threads:
4574 case OMPC_simd:
4575 case OMPC_map:
4576 case OMPC_nogroup:
4577 case OMPC_defaultmap:
4578 case OMPC_to:
4579 case OMPC_from:
4580 case OMPC_use_device_ptr:
4581 case OMPC_is_device_ptr:
4582 continue;
4583 case OMPC_allocator:
4584 case OMPC_flush:
4585 case OMPC_threadprivate:
4586 case OMPC_uniform:
4587 case OMPC_unknown:
4588 case OMPC_unified_address:
4589 case OMPC_unified_shared_memory:
4590 case OMPC_reverse_offload:
4591 case OMPC_dynamic_allocators:
4592 case OMPC_atomic_default_mem_order:
Alexey Bataev729e2422019-08-23 16:11:14 +00004593 case OMPC_device_type:
Alexey Bataev412254a2019-05-09 18:44:53 +00004594 llvm_unreachable("Unexpected clause");
4595 }
4596 for (Stmt *CC : C->children()) {
4597 if (CC)
4598 DSAChecker.Visit(CC);
4599 }
4600 }
4601 for (auto &P : DSAChecker.getVarsWithInheritedDSA())
4602 VarsWithInheritedDSA[P.getFirst()] = P.getSecond();
4603 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004604 for (const auto &P : VarsWithInheritedDSA) {
Alexey Bataev1242d8f2019-06-28 20:45:14 +00004605 if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(P.getFirst()))
4606 continue;
4607 ErrorFound = true;
Alexey Bataev4acb8592014-07-07 13:01:15 +00004608 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
4609 << P.first << P.second->getSourceRange();
Alexey Bataev41ebe0c2019-05-09 18:14:57 +00004610 Diag(DSAStack->getDefaultDSALocation(), diag::note_omp_default_dsa_none);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004611 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004612
4613 if (!AllowedNameModifiers.empty())
4614 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
4615 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00004616
Alexey Bataeved09d242014-05-28 05:53:51 +00004617 if (ErrorFound)
4618 return StmtError();
Roman Lebedevb5700602019-03-20 16:32:36 +00004619
4620 if (!(Res.getAs<OMPExecutableDirective>()->isStandaloneDirective())) {
4621 Res.getAs<OMPExecutableDirective>()
4622 ->getStructuredBlock()
4623 ->setIsOMPStructuredBlock(true);
4624 }
4625
Gheorghe-Teodor Bercea411a6242019-04-18 19:53:43 +00004626 if (!CurContext->isDependentContext() &&
4627 isOpenMPTargetExecutionDirective(Kind) &&
4628 !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
4629 DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() ||
4630 DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() ||
4631 DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) {
4632 // Register target to DSA Stack.
4633 DSAStack->addTargetDirLocation(StartLoc);
4634 }
4635
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004636 return Res;
4637}
4638
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00004639Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
4640 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00004641 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00004642 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
4643 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00004644 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00004645 assert(Linears.size() == LinModifiers.size());
4646 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00004647 if (!DG || DG.get().isNull())
4648 return DeclGroupPtrTy();
4649
4650 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00004651 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00004652 return DG;
4653 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004654 Decl *ADecl = DG.get().getSingleDecl();
Alexey Bataev587e1de2016-03-30 10:43:55 +00004655 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
4656 ADecl = FTD->getTemplatedDecl();
4657
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00004658 auto *FD = dyn_cast<FunctionDecl>(ADecl);
4659 if (!FD) {
4660 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00004661 return DeclGroupPtrTy();
4662 }
4663
Alexey Bataev2af33e32016-04-07 12:45:37 +00004664 // OpenMP [2.8.2, declare simd construct, Description]
4665 // The parameter of the simdlen clause must be a constant positive integer
4666 // expression.
4667 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00004668 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00004669 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00004670 // OpenMP [2.8.2, declare simd construct, Description]
4671 // The special this pointer can be used as if was one of the arguments to the
4672 // function in any of the linear, aligned, or uniform clauses.
4673 // The uniform clause declares one or more arguments to have an invariant
4674 // value for all concurrent invocations of the function in the execution of a
4675 // single SIMD loop.
Alexey Bataeve3727102018-04-18 15:57:46 +00004676 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
4677 const Expr *UniformedLinearThis = nullptr;
4678 for (const Expr *E : Uniforms) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00004679 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00004680 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
4681 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00004682 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
4683 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00004684 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
Alexey Bataev43a919f2018-04-13 17:48:43 +00004685 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00004686 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00004687 }
4688 if (isa<CXXThisExpr>(E)) {
4689 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00004690 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00004691 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00004692 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
4693 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00004694 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00004695 // OpenMP [2.8.2, declare simd construct, Description]
4696 // The aligned clause declares that the object to which each list item points
4697 // is aligned to the number of bytes expressed in the optional parameter of
4698 // the aligned clause.
4699 // The special this pointer can be used as if was one of the arguments to the
4700 // function in any of the linear, aligned, or uniform clauses.
4701 // The type of list items appearing in the aligned clause must be array,
4702 // pointer, reference to array, or reference to pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +00004703 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
4704 const Expr *AlignedThis = nullptr;
4705 for (const Expr *E : Aligneds) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00004706 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00004707 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
4708 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
4709 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevd93d3762016-04-12 09:35:56 +00004710 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
4711 FD->getParamDecl(PVD->getFunctionScopeIndex())
4712 ->getCanonicalDecl() == CanonPVD) {
4713 // OpenMP [2.8.1, simd construct, Restrictions]
4714 // A list-item cannot appear in more than one aligned clause.
4715 if (AlignedArgs.count(CanonPVD) > 0) {
4716 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
4717 << 1 << E->getSourceRange();
4718 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
4719 diag::note_omp_explicit_dsa)
4720 << getOpenMPClauseName(OMPC_aligned);
4721 continue;
4722 }
4723 AlignedArgs[CanonPVD] = E;
4724 QualType QTy = PVD->getType()
4725 .getNonReferenceType()
4726 .getUnqualifiedType()
4727 .getCanonicalType();
4728 const Type *Ty = QTy.getTypePtrOrNull();
4729 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
4730 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
4731 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
4732 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
4733 }
4734 continue;
4735 }
4736 }
4737 if (isa<CXXThisExpr>(E)) {
4738 if (AlignedThis) {
4739 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
4740 << 2 << E->getSourceRange();
4741 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
4742 << getOpenMPClauseName(OMPC_aligned);
4743 }
4744 AlignedThis = E;
4745 continue;
4746 }
4747 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
4748 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
4749 }
4750 // The optional parameter of the aligned clause, alignment, must be a constant
4751 // positive integer expression. If no optional parameter is specified,
4752 // implementation-defined default alignments for SIMD instructions on the
4753 // target platforms are assumed.
Alexey Bataeve3727102018-04-18 15:57:46 +00004754 SmallVector<const Expr *, 4> NewAligns;
4755 for (Expr *E : Alignments) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00004756 ExprResult Align;
4757 if (E)
4758 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
4759 NewAligns.push_back(Align.get());
4760 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00004761 // OpenMP [2.8.2, declare simd construct, Description]
4762 // The linear clause declares one or more list items to be private to a SIMD
4763 // lane and to have a linear relationship with respect to the iteration space
4764 // of a loop.
4765 // The special this pointer can be used as if was one of the arguments to the
4766 // function in any of the linear, aligned, or uniform clauses.
4767 // When a linear-step expression is specified in a linear clause it must be
4768 // either a constant integer expression or an integer-typed parameter that is
4769 // specified in a uniform clause on the directive.
Alexey Bataeve3727102018-04-18 15:57:46 +00004770 llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
Alexey Bataevecba70f2016-04-12 11:02:11 +00004771 const bool IsUniformedThis = UniformedLinearThis != nullptr;
4772 auto MI = LinModifiers.begin();
Alexey Bataeve3727102018-04-18 15:57:46 +00004773 for (const Expr *E : Linears) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00004774 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
4775 ++MI;
4776 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 Bataevecba70f2016-04-12 11:02:11 +00004780 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
4781 FD->getParamDecl(PVD->getFunctionScopeIndex())
4782 ->getCanonicalDecl() == CanonPVD) {
4783 // OpenMP [2.15.3.7, linear Clause, Restrictions]
4784 // A list-item cannot appear in more than one linear clause.
4785 if (LinearArgs.count(CanonPVD) > 0) {
4786 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
4787 << getOpenMPClauseName(OMPC_linear)
4788 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
4789 Diag(LinearArgs[CanonPVD]->getExprLoc(),
4790 diag::note_omp_explicit_dsa)
4791 << getOpenMPClauseName(OMPC_linear);
4792 continue;
4793 }
4794 // Each argument can appear in at most one uniform or linear clause.
4795 if (UniformedArgs.count(CanonPVD) > 0) {
4796 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
4797 << getOpenMPClauseName(OMPC_linear)
4798 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
4799 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
4800 diag::note_omp_explicit_dsa)
4801 << getOpenMPClauseName(OMPC_uniform);
4802 continue;
4803 }
4804 LinearArgs[CanonPVD] = E;
4805 if (E->isValueDependent() || E->isTypeDependent() ||
4806 E->isInstantiationDependent() ||
4807 E->containsUnexpandedParameterPack())
4808 continue;
4809 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
4810 PVD->getOriginalType());
4811 continue;
4812 }
4813 }
4814 if (isa<CXXThisExpr>(E)) {
4815 if (UniformedLinearThis) {
4816 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
4817 << getOpenMPClauseName(OMPC_linear)
4818 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
4819 << E->getSourceRange();
4820 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
4821 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
4822 : OMPC_linear);
4823 continue;
4824 }
4825 UniformedLinearThis = E;
4826 if (E->isValueDependent() || E->isTypeDependent() ||
4827 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
4828 continue;
4829 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
4830 E->getType());
4831 continue;
4832 }
4833 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
4834 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
4835 }
4836 Expr *Step = nullptr;
4837 Expr *NewStep = nullptr;
4838 SmallVector<Expr *, 4> NewSteps;
Alexey Bataeve3727102018-04-18 15:57:46 +00004839 for (Expr *E : Steps) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00004840 // Skip the same step expression, it was checked already.
4841 if (Step == E || !E) {
4842 NewSteps.push_back(E ? NewStep : nullptr);
4843 continue;
4844 }
4845 Step = E;
Alexey Bataeve3727102018-04-18 15:57:46 +00004846 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step))
4847 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
4848 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00004849 if (UniformedArgs.count(CanonPVD) == 0) {
4850 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
4851 << Step->getSourceRange();
4852 } else if (E->isValueDependent() || E->isTypeDependent() ||
4853 E->isInstantiationDependent() ||
4854 E->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00004855 CanonPVD->getType()->hasIntegerRepresentation()) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00004856 NewSteps.push_back(Step);
Alexey Bataeve3727102018-04-18 15:57:46 +00004857 } else {
Alexey Bataevecba70f2016-04-12 11:02:11 +00004858 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
4859 << Step->getSourceRange();
4860 }
4861 continue;
4862 }
4863 NewStep = Step;
4864 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
4865 !Step->isInstantiationDependent() &&
4866 !Step->containsUnexpandedParameterPack()) {
4867 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
4868 .get();
4869 if (NewStep)
4870 NewStep = VerifyIntegerConstantExpression(NewStep).get();
4871 }
4872 NewSteps.push_back(NewStep);
4873 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00004874 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
4875 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00004876 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00004877 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
4878 const_cast<Expr **>(Linears.data()), Linears.size(),
4879 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
4880 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00004881 ADecl->addAttr(NewAttr);
4882 return ConvertDeclToDeclGroup(ADecl);
4883}
4884
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004885StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
4886 Stmt *AStmt,
4887 SourceLocation StartLoc,
4888 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004889 if (!AStmt)
4890 return StmtError();
4891
Alexey Bataeve3727102018-04-18 15:57:46 +00004892 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9959db52014-05-06 10:08:46 +00004893 // 1.2.2 OpenMP Language Terminology
4894 // Structured block - An executable statement with a single entry at the
4895 // top and a single exit at the bottom.
4896 // The point of exit cannot be a branch out of the structured block.
4897 // longjmp() and throw() must not violate the entry/exit criteria.
4898 CS->getCapturedDecl()->setNothrow();
4899
Reid Kleckner87a31802018-03-12 21:43:02 +00004900 setFunctionHasBranchProtectedScope();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004901
Alexey Bataev25e5b442015-09-15 12:52:43 +00004902 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4903 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004904}
4905
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004906namespace {
Alexey Bataevf8be4762019-08-14 19:30:06 +00004907/// Iteration space of a single for loop.
4908struct LoopIterationSpace final {
4909 /// True if the condition operator is the strict compare operator (<, > or
4910 /// !=).
4911 bool IsStrictCompare = false;
4912 /// Condition of the loop.
4913 Expr *PreCond = nullptr;
4914 /// This expression calculates the number of iterations in the loop.
4915 /// It is always possible to calculate it before starting the loop.
4916 Expr *NumIterations = nullptr;
4917 /// The loop counter variable.
4918 Expr *CounterVar = nullptr;
4919 /// Private loop counter variable.
4920 Expr *PrivateCounterVar = nullptr;
4921 /// This is initializer for the initial value of #CounterVar.
4922 Expr *CounterInit = nullptr;
4923 /// This is step for the #CounterVar used to generate its update:
4924 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
4925 Expr *CounterStep = nullptr;
4926 /// Should step be subtracted?
4927 bool Subtract = false;
4928 /// Source range of the loop init.
4929 SourceRange InitSrcRange;
4930 /// Source range of the loop condition.
4931 SourceRange CondSrcRange;
4932 /// Source range of the loop increment.
4933 SourceRange IncSrcRange;
4934 /// Minimum value that can have the loop control variable. Used to support
4935 /// non-rectangular loops. Applied only for LCV with the non-iterator types,
4936 /// since only such variables can be used in non-loop invariant expressions.
4937 Expr *MinValue = nullptr;
4938 /// Maximum value that can have the loop control variable. Used to support
4939 /// non-rectangular loops. Applied only for LCV with the non-iterator type,
4940 /// since only such variables can be used in non-loop invariant expressions.
4941 Expr *MaxValue = nullptr;
4942 /// true, if the lower bound depends on the outer loop control var.
4943 bool IsNonRectangularLB = false;
4944 /// true, if the upper bound depends on the outer loop control var.
4945 bool IsNonRectangularUB = false;
4946 /// Index of the loop this loop depends on and forms non-rectangular loop
4947 /// nest.
4948 unsigned LoopDependentIdx = 0;
4949 /// Final condition for the non-rectangular loop nest support. It is used to
4950 /// check that the number of iterations for this particular counter must be
4951 /// finished.
4952 Expr *FinalCondition = nullptr;
4953};
4954
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004955/// Helper class for checking canonical form of the OpenMP loops and
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004956/// extracting iteration space of each loop in the loop nest, that will be used
4957/// for IR generation.
4958class OpenMPIterationSpaceChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004959 /// Reference to Sema.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004960 Sema &SemaRef;
Alexey Bataev622af1d2019-04-24 19:58:30 +00004961 /// Data-sharing stack.
4962 DSAStackTy &Stack;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004963 /// A location for diagnostics (when there is no some better location).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004964 SourceLocation DefaultLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004965 /// A location for diagnostics (when increment is not compatible).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004966 SourceLocation ConditionLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004967 /// A source location for referring to loop init later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004968 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004969 /// A source location for referring to condition later.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004970 SourceRange ConditionSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004971 /// A source location for referring to increment later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004972 SourceRange IncrementSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004973 /// Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004974 ValueDecl *LCDecl = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004975 /// Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004976 Expr *LCRef = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004977 /// Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004978 Expr *LB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004979 /// Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004980 Expr *UB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004981 /// Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004982 Expr *Step = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004983 /// This flag is true when condition is one of:
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004984 /// Var < UB
4985 /// Var <= UB
4986 /// UB > Var
4987 /// UB >= Var
Kelvin Liefbe4af2018-11-21 19:10:48 +00004988 /// This will have no value when the condition is !=
4989 llvm::Optional<bool> TestIsLessOp;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004990 /// This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004991 bool TestIsStrictOp = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004992 /// This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004993 bool SubtractStep = false;
Alexey Bataev5ddc6d12019-04-26 19:28:37 +00004994 /// The outer loop counter this loop depends on (if any).
4995 const ValueDecl *DepDecl = nullptr;
4996 /// Contains number of loop (starts from 1) on which loop counter init
4997 /// expression of this loop depends on.
4998 Optional<unsigned> InitDependOnLC;
4999 /// Contains number of loop (starts from 1) on which loop counter condition
5000 /// expression of this loop depends on.
5001 Optional<unsigned> CondDependOnLC;
Alexey Bataev622af1d2019-04-24 19:58:30 +00005002 /// Checks if the provide statement depends on the loop counter.
Alexey Bataev5ddc6d12019-04-26 19:28:37 +00005003 Optional<unsigned> doesDependOnLoopCounter(const Stmt *S, bool IsInitializer);
Alexey Bataevf8be4762019-08-14 19:30:06 +00005004 /// Original condition required for checking of the exit condition for
5005 /// non-rectangular loop.
5006 Expr *Condition = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005007
5008public:
Alexey Bataev622af1d2019-04-24 19:58:30 +00005009 OpenMPIterationSpaceChecker(Sema &SemaRef, DSAStackTy &Stack,
5010 SourceLocation DefaultLoc)
5011 : SemaRef(SemaRef), Stack(Stack), DefaultLoc(DefaultLoc),
5012 ConditionLoc(DefaultLoc) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005013 /// Check init-expr for canonical loop form and save loop counter
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005014 /// variable - #Var and its initialization value - #LB.
Alexey Bataeve3727102018-04-18 15:57:46 +00005015 bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005016 /// Check test-expr for canonical form, save upper-bound (#UB), flags
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005017 /// for less/greater and for strict/non-strict comparison.
Alexey Bataeve3727102018-04-18 15:57:46 +00005018 bool checkAndSetCond(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005019 /// Check incr-expr for canonical loop form and return true if it
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005020 /// does not conform, otherwise save loop step (#Step).
Alexey Bataeve3727102018-04-18 15:57:46 +00005021 bool checkAndSetInc(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005022 /// Return the loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00005023 ValueDecl *getLoopDecl() const { return LCDecl; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005024 /// Return the reference expression to loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00005025 Expr *getLoopDeclRefExpr() const { return LCRef; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005026 /// Source range of the loop init.
Alexey Bataeve3727102018-04-18 15:57:46 +00005027 SourceRange getInitSrcRange() const { return InitSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005028 /// Source range of the loop condition.
Alexey Bataeve3727102018-04-18 15:57:46 +00005029 SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005030 /// Source range of the loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00005031 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005032 /// True if the step should be subtracted.
Alexey Bataeve3727102018-04-18 15:57:46 +00005033 bool shouldSubtractStep() const { return SubtractStep; }
Alexey Bataev316ccf62019-01-29 18:51:58 +00005034 /// True, if the compare operator is strict (<, > or !=).
5035 bool isStrictTestOp() const { return TestIsStrictOp; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005036 /// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00005037 Expr *buildNumIterations(
Alexey Bataevf8be4762019-08-14 19:30:06 +00005038 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType,
Alexey Bataeve3727102018-04-18 15:57:46 +00005039 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005040 /// Build the precondition expression for the loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00005041 Expr *
5042 buildPreCond(Scope *S, Expr *Cond,
5043 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005044 /// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005045 DeclRefExpr *
5046 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
5047 DSAStackTy &DSA) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005048 /// Build reference expression to the private counter be used for
Alexey Bataeva8899172015-08-06 12:30:57 +00005049 /// codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005050 Expr *buildPrivateCounterVar() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005051 /// Build initialization of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005052 Expr *buildCounterInit() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005053 /// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005054 Expr *buildCounterStep() const;
Alexey Bataevf138fda2018-08-13 19:04:24 +00005055 /// Build loop data with counter value for depend clauses in ordered
5056 /// directives.
5057 Expr *
5058 buildOrderedLoopData(Scope *S, Expr *Counter,
5059 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
5060 SourceLocation Loc, Expr *Inc = nullptr,
5061 OverloadedOperatorKind OOK = OO_Amp);
Alexey Bataevf8be4762019-08-14 19:30:06 +00005062 /// Builds the minimum value for the loop counter.
5063 std::pair<Expr *, Expr *> buildMinMaxValues(
5064 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
5065 /// Builds final condition for the non-rectangular loops.
5066 Expr *buildFinalCondition(Scope *S) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005067 /// Return true if any expression is dependent.
Alexey Bataeve3727102018-04-18 15:57:46 +00005068 bool dependent() const;
Alexey Bataevf8be4762019-08-14 19:30:06 +00005069 /// Returns true if the initializer forms non-rectangular loop.
5070 bool doesInitDependOnLC() const { return InitDependOnLC.hasValue(); }
5071 /// Returns true if the condition forms non-rectangular loop.
5072 bool doesCondDependOnLC() const { return CondDependOnLC.hasValue(); }
5073 /// Returns index of the loop we depend on (starting from 1), or 0 otherwise.
5074 unsigned getLoopDependentIdx() const {
5075 return InitDependOnLC.getValueOr(CondDependOnLC.getValueOr(0));
5076 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005077
5078private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005079 /// Check the right-hand side of an assignment in the increment
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005080 /// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +00005081 bool checkAndSetIncRHS(Expr *RHS);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005082 /// Helper to set loop counter variable and its initializer.
Alexey Bataev622af1d2019-04-24 19:58:30 +00005083 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB,
5084 bool EmitDiags);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005085 /// Helper to set upper bound.
Kelvin Liefbe4af2018-11-21 19:10:48 +00005086 bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp,
5087 SourceRange SR, SourceLocation SL);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005088 /// Helper to set loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00005089 bool setStep(Expr *NewStep, bool Subtract);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005090};
5091
Alexey Bataeve3727102018-04-18 15:57:46 +00005092bool OpenMPIterationSpaceChecker::dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005093 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005094 assert(!LB && !UB && !Step);
5095 return false;
5096 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005097 return LCDecl->getType()->isDependentType() ||
5098 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
5099 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005100}
5101
Alexey Bataeve3727102018-04-18 15:57:46 +00005102bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005103 Expr *NewLCRefExpr,
Alexey Bataev622af1d2019-04-24 19:58:30 +00005104 Expr *NewLB, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005105 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005106 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00005107 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005108 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005109 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005110 LCDecl = getCanonicalDecl(NewLCDecl);
5111 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005112 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
5113 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00005114 if ((Ctor->isCopyOrMoveConstructor() ||
5115 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
5116 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005117 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005118 LB = NewLB;
Alexey Bataev622af1d2019-04-24 19:58:30 +00005119 if (EmitDiags)
Alexey Bataev5ddc6d12019-04-26 19:28:37 +00005120 InitDependOnLC = doesDependOnLoopCounter(LB, /*IsInitializer=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005121 return false;
5122}
5123
Alexey Bataev316ccf62019-01-29 18:51:58 +00005124bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB,
5125 llvm::Optional<bool> LessOp,
Kelvin Liefbe4af2018-11-21 19:10:48 +00005126 bool StrictOp, SourceRange SR,
5127 SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005128 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005129 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
5130 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005131 if (!NewUB)
5132 return true;
5133 UB = NewUB;
Kelvin Liefbe4af2018-11-21 19:10:48 +00005134 if (LessOp)
5135 TestIsLessOp = LessOp;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005136 TestIsStrictOp = StrictOp;
5137 ConditionSrcRange = SR;
5138 ConditionLoc = SL;
Alexey Bataev5ddc6d12019-04-26 19:28:37 +00005139 CondDependOnLC = doesDependOnLoopCounter(UB, /*IsInitializer=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005140 return false;
5141}
5142
Alexey Bataeve3727102018-04-18 15:57:46 +00005143bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005144 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005145 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005146 if (!NewStep)
5147 return true;
5148 if (!NewStep->isValueDependent()) {
5149 // Check that the step is integer expression.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005150 SourceLocation StepLoc = NewStep->getBeginLoc();
Alexey Bataev5372fb82017-08-31 23:06:52 +00005151 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
5152 StepLoc, getExprAsWritten(NewStep));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005153 if (Val.isInvalid())
5154 return true;
5155 NewStep = Val.get();
5156
5157 // OpenMP [2.6, Canonical Loop Form, Restrictions]
5158 // If test-expr is of form var relational-op b and relational-op is < or
5159 // <= then incr-expr must cause var to increase on each iteration of the
5160 // loop. If test-expr is of form var relational-op b and relational-op is
5161 // > or >= then incr-expr must cause var to decrease on each iteration of
5162 // the loop.
5163 // If test-expr is of form b relational-op var and relational-op is < or
5164 // <= then incr-expr must cause var to decrease on each iteration of the
5165 // loop. If test-expr is of form b relational-op var and relational-op is
5166 // > or >= then incr-expr must cause var to increase on each iteration of
5167 // the loop.
5168 llvm::APSInt Result;
5169 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
5170 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
5171 bool IsConstNeg =
5172 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005173 bool IsConstPos =
5174 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005175 bool IsConstZero = IsConstant && !Result.getBoolValue();
Kelvin Liefbe4af2018-11-21 19:10:48 +00005176
5177 // != with increment is treated as <; != with decrement is treated as >
5178 if (!TestIsLessOp.hasValue())
5179 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005180 if (UB && (IsConstZero ||
Gheorghe-Teodor Berceaa3afcf22019-01-09 20:38:35 +00005181 (TestIsLessOp.getValue() ?
Kelvin Liefbe4af2018-11-21 19:10:48 +00005182 (IsConstNeg || (IsUnsigned && Subtract)) :
5183 (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005184 SemaRef.Diag(NewStep->getExprLoc(),
5185 diag::err_omp_loop_incr_not_compatible)
Kelvin Liefbe4af2018-11-21 19:10:48 +00005186 << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005187 SemaRef.Diag(ConditionLoc,
5188 diag::note_omp_loop_cond_requres_compatible_incr)
Kelvin Liefbe4af2018-11-21 19:10:48 +00005189 << TestIsLessOp.getValue() << ConditionSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005190 return true;
5191 }
Kelvin Liefbe4af2018-11-21 19:10:48 +00005192 if (TestIsLessOp.getValue() == Subtract) {
David Majnemer9d168222016-08-05 17:44:54 +00005193 NewStep =
5194 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
5195 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005196 Subtract = !Subtract;
5197 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005198 }
5199
5200 Step = NewStep;
5201 SubtractStep = Subtract;
5202 return false;
5203}
5204
Alexey Bataev622af1d2019-04-24 19:58:30 +00005205namespace {
5206/// Checker for the non-rectangular loops. Checks if the initializer or
5207/// condition expression references loop counter variable.
5208class LoopCounterRefChecker final
5209 : public ConstStmtVisitor<LoopCounterRefChecker, bool> {
5210 Sema &SemaRef;
5211 DSAStackTy &Stack;
5212 const ValueDecl *CurLCDecl = nullptr;
Alexey Bataev2f9ef332019-04-25 16:21:13 +00005213 const ValueDecl *DepDecl = nullptr;
Alexey Bataev5ddc6d12019-04-26 19:28:37 +00005214 const ValueDecl *PrevDepDecl = nullptr;
Alexey Bataev622af1d2019-04-24 19:58:30 +00005215 bool IsInitializer = true;
Alexey Bataev5ddc6d12019-04-26 19:28:37 +00005216 unsigned BaseLoopId = 0;
5217 bool checkDecl(const Expr *E, const ValueDecl *VD) {
5218 if (getCanonicalDecl(VD) == getCanonicalDecl(CurLCDecl)) {
5219 SemaRef.Diag(E->getExprLoc(), diag::err_omp_stmt_depends_on_loop_counter)
5220 << (IsInitializer ? 0 : 1);
5221 return false;
5222 }
5223 const auto &&Data = Stack.isLoopControlVariable(VD);
5224 // OpenMP, 2.9.1 Canonical Loop Form, Restrictions.
5225 // The type of the loop iterator on which we depend may not have a random
5226 // access iterator type.
5227 if (Data.first && VD->getType()->isRecordType()) {
5228 SmallString<128> Name;
5229 llvm::raw_svector_ostream OS(Name);
5230 VD->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(),
5231 /*Qualified=*/true);
5232 SemaRef.Diag(E->getExprLoc(),
5233 diag::err_omp_wrong_dependency_iterator_type)
5234 << OS.str();
5235 SemaRef.Diag(VD->getLocation(), diag::note_previous_decl) << VD;
5236 return false;
5237 }
5238 if (Data.first &&
5239 (DepDecl || (PrevDepDecl &&
5240 getCanonicalDecl(VD) != getCanonicalDecl(PrevDepDecl)))) {
5241 if (!DepDecl && PrevDepDecl)
5242 DepDecl = PrevDepDecl;
5243 SmallString<128> Name;
5244 llvm::raw_svector_ostream OS(Name);
5245 DepDecl->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(),
5246 /*Qualified=*/true);
5247 SemaRef.Diag(E->getExprLoc(),
5248 diag::err_omp_invariant_or_linear_dependency)
5249 << OS.str();
5250 return false;
5251 }
5252 if (Data.first) {
5253 DepDecl = VD;
5254 BaseLoopId = Data.first;
5255 }
5256 return Data.first;
5257 }
Alexey Bataev622af1d2019-04-24 19:58:30 +00005258
5259public:
5260 bool VisitDeclRefExpr(const DeclRefExpr *E) {
5261 const ValueDecl *VD = E->getDecl();
Alexey Bataev5ddc6d12019-04-26 19:28:37 +00005262 if (isa<VarDecl>(VD))
5263 return checkDecl(E, VD);
Alexey Bataev622af1d2019-04-24 19:58:30 +00005264 return false;
5265 }
5266 bool VisitMemberExpr(const MemberExpr *E) {
5267 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
5268 const ValueDecl *VD = E->getMemberDecl();
Mike Rice552c2c02019-07-17 15:18:45 +00005269 if (isa<VarDecl>(VD) || isa<FieldDecl>(VD))
5270 return checkDecl(E, VD);
Alexey Bataev622af1d2019-04-24 19:58:30 +00005271 }
5272 return false;
5273 }
5274 bool VisitStmt(const Stmt *S) {
Alexey Bataevf8be4762019-08-14 19:30:06 +00005275 bool Res = false;
Alexey Bataev2f9ef332019-04-25 16:21:13 +00005276 for (const Stmt *Child : S->children())
Alexey Bataevf8be4762019-08-14 19:30:06 +00005277 Res = (Child && Visit(Child)) || Res;
Alexey Bataev2f9ef332019-04-25 16:21:13 +00005278 return Res;
Alexey Bataev622af1d2019-04-24 19:58:30 +00005279 }
5280 explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack,
Alexey Bataev5ddc6d12019-04-26 19:28:37 +00005281 const ValueDecl *CurLCDecl, bool IsInitializer,
5282 const ValueDecl *PrevDepDecl = nullptr)
Alexey Bataev622af1d2019-04-24 19:58:30 +00005283 : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl),
Alexey Bataev5ddc6d12019-04-26 19:28:37 +00005284 PrevDepDecl(PrevDepDecl), IsInitializer(IsInitializer) {}
5285 unsigned getBaseLoopId() const {
5286 assert(CurLCDecl && "Expected loop dependency.");
5287 return BaseLoopId;
5288 }
5289 const ValueDecl *getDepDecl() const {
5290 assert(CurLCDecl && "Expected loop dependency.");
5291 return DepDecl;
5292 }
Alexey Bataev622af1d2019-04-24 19:58:30 +00005293};
5294} // namespace
5295
Alexey Bataev5ddc6d12019-04-26 19:28:37 +00005296Optional<unsigned>
5297OpenMPIterationSpaceChecker::doesDependOnLoopCounter(const Stmt *S,
5298 bool IsInitializer) {
Alexey Bataev622af1d2019-04-24 19:58:30 +00005299 // Check for the non-rectangular loops.
Alexey Bataev5ddc6d12019-04-26 19:28:37 +00005300 LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer,
5301 DepDecl);
5302 if (LoopStmtChecker.Visit(S)) {
5303 DepDecl = LoopStmtChecker.getDepDecl();
5304 return LoopStmtChecker.getBaseLoopId();
5305 }
5306 return llvm::None;
Alexey Bataev622af1d2019-04-24 19:58:30 +00005307}
5308
Alexey Bataeve3727102018-04-18 15:57:46 +00005309bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005310 // Check init-expr for canonical loop form and save loop counter
5311 // variable - #Var and its initialization value - #LB.
5312 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
5313 // var = lb
5314 // integer-type var = lb
5315 // random-access-iterator-type var = lb
5316 // pointer-type var = lb
5317 //
5318 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00005319 if (EmitDiags) {
5320 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
5321 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005322 return true;
5323 }
Tim Shen4a05bb82016-06-21 20:29:17 +00005324 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
5325 if (!ExprTemp->cleanupsHaveSideEffects())
5326 S = ExprTemp->getSubExpr();
5327
Alexander Musmana5f070a2014-10-01 06:03:56 +00005328 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005329 if (Expr *E = dyn_cast<Expr>(S))
5330 S = E->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00005331 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005332 if (BO->getOpcode() == BO_Assign) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005333 Expr *LHS = BO->getLHS()->IgnoreParens();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005334 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
5335 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
5336 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataev622af1d2019-04-24 19:58:30 +00005337 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
5338 EmitDiags);
5339 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS(), EmitDiags);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005340 }
5341 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
5342 if (ME->isArrow() &&
5343 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataev622af1d2019-04-24 19:58:30 +00005344 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
5345 EmitDiags);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005346 }
5347 }
David Majnemer9d168222016-08-05 17:44:54 +00005348 } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005349 if (DS->isSingleDecl()) {
David Majnemer9d168222016-08-05 17:44:54 +00005350 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00005351 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005352 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00005353 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005354 SemaRef.Diag(S->getBeginLoc(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005355 diag::ext_omp_loop_not_canonical_init)
5356 << S->getSourceRange();
Alexey Bataevf138fda2018-08-13 19:04:24 +00005357 return setLCDeclAndLB(
5358 Var,
5359 buildDeclRefExpr(SemaRef, Var,
5360 Var->getType().getNonReferenceType(),
5361 DS->getBeginLoc()),
Alexey Bataev622af1d2019-04-24 19:58:30 +00005362 Var->getInit(), EmitDiags);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005363 }
5364 }
5365 }
David Majnemer9d168222016-08-05 17:44:54 +00005366 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005367 if (CE->getOperator() == OO_Equal) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005368 Expr *LHS = CE->getArg(0);
David Majnemer9d168222016-08-05 17:44:54 +00005369 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005370 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
5371 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataev622af1d2019-04-24 19:58:30 +00005372 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
5373 EmitDiags);
5374 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1), EmitDiags);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005375 }
5376 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
5377 if (ME->isArrow() &&
5378 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataev622af1d2019-04-24 19:58:30 +00005379 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
5380 EmitDiags);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005381 }
5382 }
5383 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005384
Alexey Bataeve3727102018-04-18 15:57:46 +00005385 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005386 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00005387 if (EmitDiags) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005388 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init)
Alexey Bataev9c821032015-04-30 04:23:23 +00005389 << S->getSourceRange();
5390 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005391 return true;
5392}
5393
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005394/// Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005395/// variable (which may be the loop variable) if possible.
Alexey Bataeve3727102018-04-18 15:57:46 +00005396static const ValueDecl *getInitLCDecl(const Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005397 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00005398 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005399 E = getExprAsWritten(E);
Alexey Bataeve3727102018-04-18 15:57:46 +00005400 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005401 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00005402 if ((Ctor->isCopyOrMoveConstructor() ||
5403 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
5404 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005405 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00005406 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
5407 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005408 return getCanonicalDecl(VD);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005409 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005410 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005411 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
5412 return getCanonicalDecl(ME->getMemberDecl());
5413 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005414}
5415
Alexey Bataeve3727102018-04-18 15:57:46 +00005416bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005417 // Check test-expr for canonical form, save upper-bound UB, flags for
5418 // less/greater and for strict/non-strict comparison.
Alexey Bataev1be63402019-09-11 15:44:06 +00005419 // OpenMP [2.9] Canonical loop form. Test-expr may be one of the following:
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005420 // var relational-op b
5421 // b relational-op var
5422 //
Alexey Bataev1be63402019-09-11 15:44:06 +00005423 bool IneqCondIsCanonical = SemaRef.getLangOpts().OpenMP >= 50;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005424 if (!S) {
Alexey Bataev1be63402019-09-11 15:44:06 +00005425 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond)
5426 << (IneqCondIsCanonical ? 1 : 0) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005427 return true;
5428 }
Alexey Bataevf8be4762019-08-14 19:30:06 +00005429 Condition = S;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005430 S = getExprAsWritten(S);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005431 SourceLocation CondLoc = S->getBeginLoc();
David Majnemer9d168222016-08-05 17:44:54 +00005432 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005433 if (BO->isRelationalOp()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005434 if (getInitLCDecl(BO->getLHS()) == LCDecl)
5435 return setUB(BO->getRHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005436 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
5437 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
5438 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00005439 if (getInitLCDecl(BO->getRHS()) == LCDecl)
5440 return setUB(BO->getLHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005441 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
5442 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
5443 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataev1be63402019-09-11 15:44:06 +00005444 } else if (IneqCondIsCanonical && BO->getOpcode() == BO_NE)
5445 return setUB(
5446 getInitLCDecl(BO->getLHS()) == LCDecl ? BO->getRHS() : BO->getLHS(),
5447 /*LessOp=*/llvm::None,
5448 /*StrictOp=*/true, BO->getSourceRange(), BO->getOperatorLoc());
David Majnemer9d168222016-08-05 17:44:54 +00005449 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005450 if (CE->getNumArgs() == 2) {
5451 auto Op = CE->getOperator();
5452 switch (Op) {
5453 case OO_Greater:
5454 case OO_GreaterEqual:
5455 case OO_Less:
5456 case OO_LessEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00005457 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
5458 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005459 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
5460 CE->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00005461 if (getInitLCDecl(CE->getArg(1)) == LCDecl)
5462 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005463 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
5464 CE->getOperatorLoc());
5465 break;
Gheorghe-Teodor Berceaa3afcf22019-01-09 20:38:35 +00005466 case OO_ExclaimEqual:
Alexey Bataev1be63402019-09-11 15:44:06 +00005467 if (IneqCondIsCanonical)
5468 return setUB(getInitLCDecl(CE->getArg(0)) == LCDecl ? CE->getArg(1)
5469 : CE->getArg(0),
5470 /*LessOp=*/llvm::None,
5471 /*StrictOp=*/true, CE->getSourceRange(),
5472 CE->getOperatorLoc());
Kelvin Liefbe4af2018-11-21 19:10:48 +00005473 break;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005474 default:
5475 break;
5476 }
5477 }
5478 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005479 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005480 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005481 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataev1be63402019-09-11 15:44:06 +00005482 << (IneqCondIsCanonical ? 1 : 0) << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005483 return true;
5484}
5485
Alexey Bataeve3727102018-04-18 15:57:46 +00005486bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005487 // RHS of canonical loop form increment can be:
5488 // var + incr
5489 // incr + var
5490 // var - incr
5491 //
5492 RHS = RHS->IgnoreParenImpCasts();
David Majnemer9d168222016-08-05 17:44:54 +00005493 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005494 if (BO->isAdditiveOp()) {
5495 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataeve3727102018-04-18 15:57:46 +00005496 if (getInitLCDecl(BO->getLHS()) == LCDecl)
5497 return setStep(BO->getRHS(), !IsAdd);
5498 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl)
5499 return setStep(BO->getLHS(), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005500 }
David Majnemer9d168222016-08-05 17:44:54 +00005501 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005502 bool IsAdd = CE->getOperator() == OO_Plus;
5503 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005504 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
5505 return setStep(CE->getArg(1), !IsAdd);
5506 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl)
5507 return setStep(CE->getArg(0), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005508 }
5509 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005510 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005511 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005512 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005513 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005514 return true;
5515}
5516
Alexey Bataeve3727102018-04-18 15:57:46 +00005517bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005518 // Check incr-expr for canonical loop form and return true if it
5519 // does not conform.
5520 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
5521 // ++var
5522 // var++
5523 // --var
5524 // var--
5525 // var += incr
5526 // var -= incr
5527 // var = var + incr
5528 // var = incr + var
5529 // var = var - incr
5530 //
5531 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005532 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005533 return true;
5534 }
Tim Shen4a05bb82016-06-21 20:29:17 +00005535 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
5536 if (!ExprTemp->cleanupsHaveSideEffects())
5537 S = ExprTemp->getSubExpr();
5538
Alexander Musmana5f070a2014-10-01 06:03:56 +00005539 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005540 S = S->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00005541 if (auto *UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005542 if (UO->isIncrementDecrementOp() &&
Alexey Bataeve3727102018-04-18 15:57:46 +00005543 getInitLCDecl(UO->getSubExpr()) == LCDecl)
5544 return setStep(SemaRef
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005545 .ActOnIntegerConstant(UO->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00005546 (UO->isDecrementOp() ? -1 : 1))
5547 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00005548 /*Subtract=*/false);
David Majnemer9d168222016-08-05 17:44:54 +00005549 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005550 switch (BO->getOpcode()) {
5551 case BO_AddAssign:
5552 case BO_SubAssign:
Alexey Bataeve3727102018-04-18 15:57:46 +00005553 if (getInitLCDecl(BO->getLHS()) == LCDecl)
5554 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005555 break;
5556 case BO_Assign:
Alexey Bataeve3727102018-04-18 15:57:46 +00005557 if (getInitLCDecl(BO->getLHS()) == LCDecl)
5558 return checkAndSetIncRHS(BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005559 break;
5560 default:
5561 break;
5562 }
David Majnemer9d168222016-08-05 17:44:54 +00005563 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005564 switch (CE->getOperator()) {
5565 case OO_PlusPlus:
5566 case OO_MinusMinus:
Alexey Bataeve3727102018-04-18 15:57:46 +00005567 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
5568 return setStep(SemaRef
David Majnemer9d168222016-08-05 17:44:54 +00005569 .ActOnIntegerConstant(
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005570 CE->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00005571 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
5572 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00005573 /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005574 break;
5575 case OO_PlusEqual:
5576 case OO_MinusEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00005577 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
5578 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005579 break;
5580 case OO_Equal:
Alexey Bataeve3727102018-04-18 15:57:46 +00005581 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
5582 return checkAndSetIncRHS(CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005583 break;
5584 default:
5585 break;
5586 }
5587 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005588 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005589 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005590 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005591 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005592 return true;
5593}
Alexander Musmana5f070a2014-10-01 06:03:56 +00005594
Alexey Bataev5a3af132016-03-29 08:58:54 +00005595static ExprResult
5596tryBuildCapture(Sema &SemaRef, Expr *Capture,
Alexey Bataeve3727102018-04-18 15:57:46 +00005597 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00005598 if (SemaRef.CurContext->isDependentContext())
5599 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00005600 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
5601 return SemaRef.PerformImplicitConversion(
5602 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
5603 /*AllowExplicit=*/true);
5604 auto I = Captures.find(Capture);
5605 if (I != Captures.end())
5606 return buildCapture(SemaRef, Capture, I->second);
5607 DeclRefExpr *Ref = nullptr;
5608 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
5609 Captures[Capture] = Ref;
5610 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005611}
5612
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005613/// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00005614Expr *OpenMPIterationSpaceChecker::buildNumIterations(
Alexey Bataevf8be4762019-08-14 19:30:06 +00005615 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType,
Alexey Bataeve3727102018-04-18 15:57:46 +00005616 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00005617 ExprResult Diff;
Alexey Bataeve3727102018-04-18 15:57:46 +00005618 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005619 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00005620 SemaRef.getLangOpts().CPlusPlus) {
Alexey Bataevf8be4762019-08-14 19:30:06 +00005621 Expr *LBVal = LB;
5622 Expr *UBVal = UB;
5623 // LB = TestIsLessOp.getValue() ? min(LB(MinVal), LB(MaxVal)) :
5624 // max(LB(MinVal), LB(MaxVal))
5625 if (InitDependOnLC) {
5626 const LoopIterationSpace &IS =
5627 ResultIterSpaces[ResultIterSpaces.size() - 1 -
5628 InitDependOnLC.getValueOr(
5629 CondDependOnLC.getValueOr(0))];
5630 if (!IS.MinValue || !IS.MaxValue)
5631 return nullptr;
5632 // OuterVar = Min
5633 ExprResult MinValue =
5634 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue);
5635 if (!MinValue.isUsable())
5636 return nullptr;
5637
5638 ExprResult LBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
5639 IS.CounterVar, MinValue.get());
5640 if (!LBMinVal.isUsable())
5641 return nullptr;
5642 // OuterVar = Min, LBVal
5643 LBMinVal =
5644 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMinVal.get(), LBVal);
5645 if (!LBMinVal.isUsable())
5646 return nullptr;
5647 // (OuterVar = Min, LBVal)
5648 LBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMinVal.get());
5649 if (!LBMinVal.isUsable())
5650 return nullptr;
5651
5652 // OuterVar = Max
5653 ExprResult MaxValue =
5654 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue);
5655 if (!MaxValue.isUsable())
5656 return nullptr;
5657
5658 ExprResult LBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
5659 IS.CounterVar, MaxValue.get());
5660 if (!LBMaxVal.isUsable())
5661 return nullptr;
5662 // OuterVar = Max, LBVal
5663 LBMaxVal =
5664 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMaxVal.get(), LBVal);
5665 if (!LBMaxVal.isUsable())
5666 return nullptr;
5667 // (OuterVar = Max, LBVal)
5668 LBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMaxVal.get());
5669 if (!LBMaxVal.isUsable())
5670 return nullptr;
5671
5672 Expr *LBMin = tryBuildCapture(SemaRef, LBMinVal.get(), Captures).get();
5673 Expr *LBMax = tryBuildCapture(SemaRef, LBMaxVal.get(), Captures).get();
5674 if (!LBMin || !LBMax)
5675 return nullptr;
5676 // LB(MinVal) < LB(MaxVal)
5677 ExprResult MinLessMaxRes =
5678 SemaRef.BuildBinOp(S, DefaultLoc, BO_LT, LBMin, LBMax);
5679 if (!MinLessMaxRes.isUsable())
5680 return nullptr;
5681 Expr *MinLessMax =
5682 tryBuildCapture(SemaRef, MinLessMaxRes.get(), Captures).get();
5683 if (!MinLessMax)
5684 return nullptr;
5685 if (TestIsLessOp.getValue()) {
5686 // LB(MinVal) < LB(MaxVal) ? LB(MinVal) : LB(MaxVal) - min(LB(MinVal),
5687 // LB(MaxVal))
5688 ExprResult MinLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc,
5689 MinLessMax, LBMin, LBMax);
5690 if (!MinLB.isUsable())
5691 return nullptr;
5692 LBVal = MinLB.get();
5693 } else {
5694 // LB(MinVal) < LB(MaxVal) ? LB(MaxVal) : LB(MinVal) - max(LB(MinVal),
5695 // LB(MaxVal))
5696 ExprResult MaxLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc,
5697 MinLessMax, LBMax, LBMin);
5698 if (!MaxLB.isUsable())
5699 return nullptr;
5700 LBVal = MaxLB.get();
5701 }
5702 }
5703 // UB = TestIsLessOp.getValue() ? max(UB(MinVal), UB(MaxVal)) :
5704 // min(UB(MinVal), UB(MaxVal))
5705 if (CondDependOnLC) {
5706 const LoopIterationSpace &IS =
5707 ResultIterSpaces[ResultIterSpaces.size() - 1 -
5708 InitDependOnLC.getValueOr(
5709 CondDependOnLC.getValueOr(0))];
5710 if (!IS.MinValue || !IS.MaxValue)
5711 return nullptr;
5712 // OuterVar = Min
5713 ExprResult MinValue =
5714 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue);
5715 if (!MinValue.isUsable())
5716 return nullptr;
5717
5718 ExprResult UBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
5719 IS.CounterVar, MinValue.get());
5720 if (!UBMinVal.isUsable())
5721 return nullptr;
5722 // OuterVar = Min, UBVal
5723 UBMinVal =
5724 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMinVal.get(), UBVal);
5725 if (!UBMinVal.isUsable())
5726 return nullptr;
5727 // (OuterVar = Min, UBVal)
5728 UBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMinVal.get());
5729 if (!UBMinVal.isUsable())
5730 return nullptr;
5731
5732 // OuterVar = Max
5733 ExprResult MaxValue =
5734 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue);
5735 if (!MaxValue.isUsable())
5736 return nullptr;
5737
5738 ExprResult UBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
5739 IS.CounterVar, MaxValue.get());
5740 if (!UBMaxVal.isUsable())
5741 return nullptr;
5742 // OuterVar = Max, UBVal
5743 UBMaxVal =
5744 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMaxVal.get(), UBVal);
5745 if (!UBMaxVal.isUsable())
5746 return nullptr;
5747 // (OuterVar = Max, UBVal)
5748 UBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMaxVal.get());
5749 if (!UBMaxVal.isUsable())
5750 return nullptr;
5751
5752 Expr *UBMin = tryBuildCapture(SemaRef, UBMinVal.get(), Captures).get();
5753 Expr *UBMax = tryBuildCapture(SemaRef, UBMaxVal.get(), Captures).get();
5754 if (!UBMin || !UBMax)
5755 return nullptr;
5756 // UB(MinVal) > UB(MaxVal)
5757 ExprResult MinGreaterMaxRes =
5758 SemaRef.BuildBinOp(S, DefaultLoc, BO_GT, UBMin, UBMax);
5759 if (!MinGreaterMaxRes.isUsable())
5760 return nullptr;
5761 Expr *MinGreaterMax =
5762 tryBuildCapture(SemaRef, MinGreaterMaxRes.get(), Captures).get();
5763 if (!MinGreaterMax)
5764 return nullptr;
5765 if (TestIsLessOp.getValue()) {
5766 // UB(MinVal) > UB(MaxVal) ? UB(MinVal) : UB(MaxVal) - max(UB(MinVal),
5767 // UB(MaxVal))
5768 ExprResult MaxUB = SemaRef.ActOnConditionalOp(
5769 DefaultLoc, DefaultLoc, MinGreaterMax, UBMin, UBMax);
5770 if (!MaxUB.isUsable())
5771 return nullptr;
5772 UBVal = MaxUB.get();
5773 } else {
5774 // UB(MinVal) > UB(MaxVal) ? UB(MaxVal) : UB(MinVal) - min(UB(MinVal),
5775 // UB(MaxVal))
5776 ExprResult MinUB = SemaRef.ActOnConditionalOp(
5777 DefaultLoc, DefaultLoc, MinGreaterMax, UBMax, UBMin);
5778 if (!MinUB.isUsable())
5779 return nullptr;
5780 UBVal = MinUB.get();
5781 }
5782 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005783 // Upper - Lower
Alexey Bataevf8be4762019-08-14 19:30:06 +00005784 Expr *UBExpr = TestIsLessOp.getValue() ? UBVal : LBVal;
5785 Expr *LBExpr = TestIsLessOp.getValue() ? LBVal : UBVal;
Alexey Bataev5a3af132016-03-29 08:58:54 +00005786 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
5787 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005788 if (!Upper || !Lower)
5789 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005790
5791 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
5792
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005793 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00005794 // BuildBinOp already emitted error, this one is to point user to upper
5795 // and lower bound, and to tell what is passed to 'operator-'.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005796 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
Alexander Musmana5f070a2014-10-01 06:03:56 +00005797 << Upper->getSourceRange() << Lower->getSourceRange();
5798 return nullptr;
5799 }
5800 }
5801
5802 if (!Diff.isUsable())
5803 return nullptr;
5804
5805 // Upper - Lower [- 1]
5806 if (TestIsStrictOp)
5807 Diff = SemaRef.BuildBinOp(
5808 S, DefaultLoc, BO_Sub, Diff.get(),
5809 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5810 if (!Diff.isUsable())
5811 return nullptr;
5812
5813 // Upper - Lower [- 1] + Step
Alexey Bataeve3727102018-04-18 15:57:46 +00005814 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00005815 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005816 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005817 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005818 if (!Diff.isUsable())
5819 return nullptr;
5820
5821 // Parentheses (for dumping/debugging purposes only).
5822 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
5823 if (!Diff.isUsable())
5824 return nullptr;
5825
5826 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005827 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005828 if (!Diff.isUsable())
5829 return nullptr;
5830
Alexander Musman174b3ca2014-10-06 11:16:29 +00005831 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005832 QualType Type = Diff.get()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +00005833 ASTContext &C = SemaRef.Context;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005834 bool UseVarType = VarType->hasIntegerRepresentation() &&
5835 C.getTypeSize(Type) > C.getTypeSize(VarType);
5836 if (!Type->isIntegerType() || UseVarType) {
5837 unsigned NewSize =
5838 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
5839 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
5840 : Type->hasSignedIntegerRepresentation();
5841 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00005842 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
5843 Diff = SemaRef.PerformImplicitConversion(
5844 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
5845 if (!Diff.isUsable())
5846 return nullptr;
5847 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005848 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00005849 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00005850 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
5851 if (NewSize != C.getTypeSize(Type)) {
5852 if (NewSize < C.getTypeSize(Type)) {
5853 assert(NewSize == 64 && "incorrect loop var size");
5854 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
5855 << InitSrcRange << ConditionSrcRange;
5856 }
5857 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005858 NewSize, Type->hasSignedIntegerRepresentation() ||
5859 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00005860 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
5861 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
5862 Sema::AA_Converting, true);
5863 if (!Diff.isUsable())
5864 return nullptr;
5865 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00005866 }
5867 }
5868
Alexander Musmana5f070a2014-10-01 06:03:56 +00005869 return Diff.get();
5870}
5871
Alexey Bataevf8be4762019-08-14 19:30:06 +00005872std::pair<Expr *, Expr *> OpenMPIterationSpaceChecker::buildMinMaxValues(
5873 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
5874 // Do not build for iterators, they cannot be used in non-rectangular loop
5875 // nests.
5876 if (LCDecl->getType()->isRecordType())
5877 return std::make_pair(nullptr, nullptr);
5878 // If we subtract, the min is in the condition, otherwise the min is in the
5879 // init value.
5880 Expr *MinExpr = nullptr;
5881 Expr *MaxExpr = nullptr;
5882 Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB;
5883 Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB;
5884 bool LBNonRect = TestIsLessOp.getValue() ? InitDependOnLC.hasValue()
5885 : CondDependOnLC.hasValue();
5886 bool UBNonRect = TestIsLessOp.getValue() ? CondDependOnLC.hasValue()
5887 : InitDependOnLC.hasValue();
5888 Expr *Lower =
5889 LBNonRect ? LBExpr : tryBuildCapture(SemaRef, LBExpr, Captures).get();
5890 Expr *Upper =
5891 UBNonRect ? UBExpr : tryBuildCapture(SemaRef, UBExpr, Captures).get();
5892 if (!Upper || !Lower)
5893 return std::make_pair(nullptr, nullptr);
5894
5895 if (TestIsLessOp.getValue())
5896 MinExpr = Lower;
5897 else
5898 MaxExpr = Upper;
5899
5900 // Build minimum/maximum value based on number of iterations.
5901 ExprResult Diff;
5902 QualType VarType = LCDecl->getType().getNonReferenceType();
5903
5904 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
5905 if (!Diff.isUsable())
5906 return std::make_pair(nullptr, nullptr);
5907
5908 // Upper - Lower [- 1]
5909 if (TestIsStrictOp)
5910 Diff = SemaRef.BuildBinOp(
5911 S, DefaultLoc, BO_Sub, Diff.get(),
5912 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5913 if (!Diff.isUsable())
5914 return std::make_pair(nullptr, nullptr);
5915
5916 // Upper - Lower [- 1] + Step
5917 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
5918 if (!NewStep.isUsable())
5919 return std::make_pair(nullptr, nullptr);
5920
5921 // Parentheses (for dumping/debugging purposes only).
5922 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
5923 if (!Diff.isUsable())
5924 return std::make_pair(nullptr, nullptr);
5925
5926 // (Upper - Lower [- 1]) / Step
5927 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
5928 if (!Diff.isUsable())
5929 return std::make_pair(nullptr, nullptr);
5930
5931 // ((Upper - Lower [- 1]) / Step) * Step
5932 // Parentheses (for dumping/debugging purposes only).
5933 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
5934 if (!Diff.isUsable())
5935 return std::make_pair(nullptr, nullptr);
5936
5937 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Mul, Diff.get(), NewStep.get());
5938 if (!Diff.isUsable())
5939 return std::make_pair(nullptr, nullptr);
5940
5941 // Convert to the original type or ptrdiff_t, if original type is pointer.
5942 if (!VarType->isAnyPointerType() &&
5943 !SemaRef.Context.hasSameType(Diff.get()->getType(), VarType)) {
5944 Diff = SemaRef.PerformImplicitConversion(
5945 Diff.get(), VarType, Sema::AA_Converting, /*AllowExplicit=*/true);
5946 } else if (VarType->isAnyPointerType() &&
5947 !SemaRef.Context.hasSameType(
5948 Diff.get()->getType(),
5949 SemaRef.Context.getUnsignedPointerDiffType())) {
5950 Diff = SemaRef.PerformImplicitConversion(
5951 Diff.get(), SemaRef.Context.getUnsignedPointerDiffType(),
5952 Sema::AA_Converting, /*AllowExplicit=*/true);
5953 }
5954 if (!Diff.isUsable())
5955 return std::make_pair(nullptr, nullptr);
5956
5957 // Parentheses (for dumping/debugging purposes only).
5958 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
5959 if (!Diff.isUsable())
5960 return std::make_pair(nullptr, nullptr);
5961
5962 if (TestIsLessOp.getValue()) {
5963 // MinExpr = Lower;
5964 // MaxExpr = Lower + (((Upper - Lower [- 1]) / Step) * Step)
5965 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Lower, Diff.get());
5966 if (!Diff.isUsable())
5967 return std::make_pair(nullptr, nullptr);
5968 Diff = SemaRef.ActOnFinishFullExpr(Diff.get(), /*DiscardedValue*/ false);
5969 if (!Diff.isUsable())
5970 return std::make_pair(nullptr, nullptr);
5971 MaxExpr = Diff.get();
5972 } else {
5973 // MaxExpr = Upper;
5974 // MinExpr = Upper - (((Upper - Lower [- 1]) / Step) * Step)
5975 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Diff.get());
5976 if (!Diff.isUsable())
5977 return std::make_pair(nullptr, nullptr);
5978 Diff = SemaRef.ActOnFinishFullExpr(Diff.get(), /*DiscardedValue*/ false);
5979 if (!Diff.isUsable())
5980 return std::make_pair(nullptr, nullptr);
5981 MinExpr = Diff.get();
5982 }
5983
5984 return std::make_pair(MinExpr, MaxExpr);
5985}
5986
5987Expr *OpenMPIterationSpaceChecker::buildFinalCondition(Scope *S) const {
5988 if (InitDependOnLC || CondDependOnLC)
5989 return Condition;
5990 return nullptr;
5991}
5992
Alexey Bataeve3727102018-04-18 15:57:46 +00005993Expr *OpenMPIterationSpaceChecker::buildPreCond(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005994 Scope *S, Expr *Cond,
Alexey Bataeve3727102018-04-18 15:57:46 +00005995 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00005996 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
Richard Smith2e3ed4a2019-08-16 19:53:22 +00005997 Sema::TentativeAnalysisScope Trap(SemaRef);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005998
Alexey Bataevf8be4762019-08-14 19:30:06 +00005999 ExprResult NewLB =
6000 InitDependOnLC ? LB : tryBuildCapture(SemaRef, LB, Captures);
6001 ExprResult NewUB =
6002 CondDependOnLC ? UB : tryBuildCapture(SemaRef, UB, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00006003 if (!NewLB.isUsable() || !NewUB.isUsable())
6004 return nullptr;
6005
Alexey Bataeve3727102018-04-18 15:57:46 +00006006 ExprResult CondExpr =
6007 SemaRef.BuildBinOp(S, DefaultLoc,
Gheorghe-Teodor Berceaa3afcf22019-01-09 20:38:35 +00006008 TestIsLessOp.getValue() ?
Kelvin Liefbe4af2018-11-21 19:10:48 +00006009 (TestIsStrictOp ? BO_LT : BO_LE) :
6010 (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataeve3727102018-04-18 15:57:46 +00006011 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00006012 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00006013 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
6014 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00006015 CondExpr = SemaRef.PerformImplicitConversion(
6016 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
6017 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00006018 }
Richard Smith2e3ed4a2019-08-16 19:53:22 +00006019
Sergi Mateo Bellidof3e00fe2019-02-01 08:39:01 +00006020 // Otherwise use original loop condition and evaluate it in runtime.
Alexey Bataev62dbb972015-04-22 11:59:37 +00006021 return CondExpr.isUsable() ? CondExpr.get() : Cond;
6022}
6023
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006024/// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006025DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
Alexey Bataevf138fda2018-08-13 19:04:24 +00006026 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
6027 DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006028 auto *VD = dyn_cast<VarDecl>(LCDecl);
6029 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006030 VD = SemaRef.isOpenMPCapturedDecl(LCDecl);
6031 DeclRefExpr *Ref = buildDeclRefExpr(
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006032 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00006033 const DSAStackTy::DSAVarData Data =
6034 DSA.getTopDSA(LCDecl, /*FromParent=*/false);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006035 // If the loop control decl is explicitly marked as private, do not mark it
6036 // as captured again.
6037 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
6038 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006039 return Ref;
6040 }
Alexey Bataev0d8fcdf2019-03-14 20:36:00 +00006041 return cast<DeclRefExpr>(LCRef);
Alexey Bataeva8899172015-08-06 12:30:57 +00006042}
6043
Alexey Bataeve3727102018-04-18 15:57:46 +00006044Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006045 if (LCDecl && !LCDecl->isInvalidDecl()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006046 QualType Type = LCDecl->getType().getNonReferenceType();
6047 VarDecl *PrivateVar = buildVarDecl(
Alexey Bataev63cc8e92018-03-20 14:45:59 +00006048 SemaRef, DefaultLoc, Type, LCDecl->getName(),
6049 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
6050 isa<VarDecl>(LCDecl)
6051 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
6052 : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00006053 if (PrivateVar->isInvalidDecl())
6054 return nullptr;
6055 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
6056 }
6057 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00006058}
6059
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006060/// Build initialization of the counter to be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006061Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00006062
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006063/// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006064Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00006065
Alexey Bataevf138fda2018-08-13 19:04:24 +00006066Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
6067 Scope *S, Expr *Counter,
6068 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
6069 Expr *Inc, OverloadedOperatorKind OOK) {
6070 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get();
6071 if (!Cnt)
6072 return nullptr;
6073 if (Inc) {
6074 assert((OOK == OO_Plus || OOK == OO_Minus) &&
6075 "Expected only + or - operations for depend clauses.");
6076 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
6077 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get();
6078 if (!Cnt)
6079 return nullptr;
6080 }
6081 ExprResult Diff;
6082 QualType VarType = LCDecl->getType().getNonReferenceType();
6083 if (VarType->isIntegerType() || VarType->isPointerType() ||
6084 SemaRef.getLangOpts().CPlusPlus) {
6085 // Upper - Lower
Alexey Bataev316ccf62019-01-29 18:51:58 +00006086 Expr *Upper = TestIsLessOp.getValue()
6087 ? Cnt
6088 : tryBuildCapture(SemaRef, UB, Captures).get();
6089 Expr *Lower = TestIsLessOp.getValue()
6090 ? tryBuildCapture(SemaRef, LB, Captures).get()
6091 : Cnt;
Alexey Bataevf138fda2018-08-13 19:04:24 +00006092 if (!Upper || !Lower)
6093 return nullptr;
6094
6095 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
6096
6097 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
6098 // BuildBinOp already emitted error, this one is to point user to upper
6099 // and lower bound, and to tell what is passed to 'operator-'.
6100 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
6101 << Upper->getSourceRange() << Lower->getSourceRange();
6102 return nullptr;
6103 }
6104 }
6105
6106 if (!Diff.isUsable())
6107 return nullptr;
6108
6109 // Parentheses (for dumping/debugging purposes only).
6110 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
6111 if (!Diff.isUsable())
6112 return nullptr;
6113
6114 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
6115 if (!NewStep.isUsable())
6116 return nullptr;
6117 // (Upper - Lower) / Step
6118 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
6119 if (!Diff.isUsable())
6120 return nullptr;
6121
6122 return Diff.get();
6123}
Alexey Bataev23b69422014-06-18 07:08:49 +00006124} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006125
Alexey Bataev9c821032015-04-30 04:23:23 +00006126void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
6127 assert(getLangOpts().OpenMP && "OpenMP is not active.");
6128 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006129 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
6130 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00006131 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevce901812018-12-19 18:16:37 +00006132 DSAStack->loopStart();
Alexey Bataev622af1d2019-04-24 19:58:30 +00006133 OpenMPIterationSpaceChecker ISC(*this, *DSAStack, ForLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00006134 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) {
6135 if (ValueDecl *D = ISC.getLoopDecl()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006136 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev05be1da2019-07-18 17:49:13 +00006137 DeclRefExpr *PrivateRef = nullptr;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006138 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006139 if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006140 VD = Private;
Alexey Bataeve3727102018-04-18 15:57:46 +00006141 } else {
Alexey Bataev05be1da2019-07-18 17:49:13 +00006142 PrivateRef = buildCapture(*this, D, ISC.getLoopDeclRefExpr(),
6143 /*WithInit=*/false);
6144 VD = cast<VarDecl>(PrivateRef->getDecl());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006145 }
6146 }
6147 DSAStack->addLoopControlVariable(D, VD);
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00006148 const Decl *LD = DSAStack->getPossiblyLoopCunter();
6149 if (LD != D->getCanonicalDecl()) {
6150 DSAStack->resetPossibleLoopCounter();
6151 if (auto *Var = dyn_cast_or_null<VarDecl>(LD))
6152 MarkDeclarationsReferencedInExpr(
6153 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var),
6154 Var->getType().getNonLValueExprType(Context),
6155 ForLoc, /*RefersToCapture=*/true));
6156 }
Alexey Bataev05be1da2019-07-18 17:49:13 +00006157 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
6158 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables
6159 // Referenced in a Construct, C/C++]. The loop iteration variable in the
6160 // associated for-loop of a simd construct with just one associated
6161 // for-loop may be listed in a linear clause with a constant-linear-step
6162 // that is the increment of the associated for-loop. The loop iteration
6163 // variable(s) in the associated for-loop(s) of a for or parallel for
6164 // construct may be listed in a private or lastprivate clause.
6165 DSAStackTy::DSAVarData DVar =
6166 DSAStack->getTopDSA(D, /*FromParent=*/false);
6167 // If LoopVarRefExpr is nullptr it means the corresponding loop variable
6168 // is declared in the loop and it is predetermined as a private.
6169 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
6170 OpenMPClauseKind PredeterminedCKind =
6171 isOpenMPSimdDirective(DKind)
6172 ? (DSAStack->hasMutipleLoops() ? OMPC_lastprivate : OMPC_linear)
6173 : OMPC_private;
6174 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
6175 DVar.CKind != PredeterminedCKind && DVar.RefExpr &&
6176 (LangOpts.OpenMP <= 45 || (DVar.CKind != OMPC_lastprivate &&
6177 DVar.CKind != OMPC_private))) ||
6178 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
6179 isOpenMPDistributeDirective(DKind)) &&
6180 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
6181 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
6182 (DVar.CKind != OMPC_private || DVar.RefExpr)) {
6183 Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa)
6184 << getOpenMPClauseName(DVar.CKind)
6185 << getOpenMPDirectiveName(DKind)
6186 << getOpenMPClauseName(PredeterminedCKind);
6187 if (DVar.RefExpr == nullptr)
6188 DVar.CKind = PredeterminedCKind;
6189 reportOriginalDsa(*this, DSAStack, D, DVar,
6190 /*IsLoopIterVar=*/true);
6191 } else if (LoopDeclRefExpr) {
6192 // Make the loop iteration variable private (for worksharing
6193 // constructs), linear (for simd directives with the only one
6194 // associated loop) or lastprivate (for simd directives with several
6195 // collapsed or ordered loops).
6196 if (DVar.CKind == OMPC_unknown)
6197 DSAStack->addDSA(D, LoopDeclRefExpr, PredeterminedCKind,
6198 PrivateRef);
6199 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006200 }
6201 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006202 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00006203 }
6204}
6205
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006206/// Called on a for stmt to check and extract its iteration space
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006207/// for further processing (such as collapsing).
Alexey Bataeve3727102018-04-18 15:57:46 +00006208static bool checkOpenMPIterationSpace(
Alexey Bataev4acb8592014-07-07 13:01:15 +00006209 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
6210 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataevf138fda2018-08-13 19:04:24 +00006211 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
6212 Expr *OrderedLoopCountExpr,
Alexey Bataeve3727102018-04-18 15:57:46 +00006213 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexey Bataevf8be4762019-08-14 19:30:06 +00006214 llvm::MutableArrayRef<LoopIterationSpace> ResultIterSpaces,
Alexey Bataeve3727102018-04-18 15:57:46 +00006215 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006216 // OpenMP [2.6, Canonical Loop Form]
6217 // for (init-expr; test-expr; incr-expr) structured-block
David Majnemer9d168222016-08-05 17:44:54 +00006218 auto *For = dyn_cast_or_null<ForStmt>(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006219 if (!For) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006220 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00006221 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
Alexey Bataevf138fda2018-08-13 19:04:24 +00006222 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount
Alexey Bataev10e775f2015-07-30 11:36:16 +00006223 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
Alexey Bataevf138fda2018-08-13 19:04:24 +00006224 if (TotalNestedLoopCount > 1) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006225 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
6226 SemaRef.Diag(DSA.getConstructLoc(),
6227 diag::note_omp_collapse_ordered_expr)
6228 << 2 << CollapseLoopCountExpr->getSourceRange()
6229 << OrderedLoopCountExpr->getSourceRange();
6230 else if (CollapseLoopCountExpr)
6231 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
6232 diag::note_omp_collapse_ordered_expr)
6233 << 0 << CollapseLoopCountExpr->getSourceRange();
6234 else
6235 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
6236 diag::note_omp_collapse_ordered_expr)
6237 << 1 << OrderedLoopCountExpr->getSourceRange();
6238 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006239 return true;
6240 }
6241 assert(For->getBody());
6242
Alexey Bataev622af1d2019-04-24 19:58:30 +00006243 OpenMPIterationSpaceChecker ISC(SemaRef, DSA, For->getForLoc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006244
6245 // Check init.
Alexey Bataeve3727102018-04-18 15:57:46 +00006246 Stmt *Init = For->getInit();
6247 if (ISC.checkAndSetInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006248 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006249
6250 bool HasErrors = false;
6251
6252 // Check loop variable's type.
Alexey Bataeve3727102018-04-18 15:57:46 +00006253 if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006254 // OpenMP [2.6, Canonical Loop Form]
6255 // Var is one of the following:
6256 // A variable of signed or unsigned integer type.
6257 // For C++, a variable of a random access iterator type.
6258 // For C, a variable of a pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +00006259 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006260 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
6261 !VarType->isPointerType() &&
6262 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006263 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006264 << SemaRef.getLangOpts().CPlusPlus;
6265 HasErrors = true;
6266 }
6267
6268 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
6269 // a Construct
6270 // The loop iteration variable(s) in the associated for-loop(s) of a for or
6271 // parallel for construct is (are) private.
6272 // The loop iteration variable in the associated for-loop of a simd
6273 // construct with just one associated for-loop is linear with a
6274 // constant-linear-step that is the increment of the associated for-loop.
6275 // Exclude loop var from the list of variables with implicitly defined data
6276 // sharing attributes.
6277 VarsWithImplicitDSA.erase(LCDecl);
6278
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006279 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
6280
6281 // Check test-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00006282 HasErrors |= ISC.checkAndSetCond(For->getCond());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00006283
6284 // Check incr-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00006285 HasErrors |= ISC.checkAndSetInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006286 }
6287
Alexey Bataeve3727102018-04-18 15:57:46 +00006288 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006289 return HasErrors;
6290
Alexander Musmana5f070a2014-10-01 06:03:56 +00006291 // Build the loop's iteration space representation.
Alexey Bataevf8be4762019-08-14 19:30:06 +00006292 ResultIterSpaces[CurrentNestedLoopCount].PreCond =
Alexey Bataeve3727102018-04-18 15:57:46 +00006293 ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexey Bataevf8be4762019-08-14 19:30:06 +00006294 ResultIterSpaces[CurrentNestedLoopCount].NumIterations =
6295 ISC.buildNumIterations(DSA.getCurScope(), ResultIterSpaces,
6296 (isOpenMPWorksharingDirective(DKind) ||
6297 isOpenMPTaskLoopDirective(DKind) ||
6298 isOpenMPDistributeDirective(DKind)),
6299 Captures);
6300 ResultIterSpaces[CurrentNestedLoopCount].CounterVar =
6301 ISC.buildCounterVar(Captures, DSA);
6302 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar =
6303 ISC.buildPrivateCounterVar();
6304 ResultIterSpaces[CurrentNestedLoopCount].CounterInit = ISC.buildCounterInit();
6305 ResultIterSpaces[CurrentNestedLoopCount].CounterStep = ISC.buildCounterStep();
6306 ResultIterSpaces[CurrentNestedLoopCount].InitSrcRange = ISC.getInitSrcRange();
6307 ResultIterSpaces[CurrentNestedLoopCount].CondSrcRange =
6308 ISC.getConditionSrcRange();
6309 ResultIterSpaces[CurrentNestedLoopCount].IncSrcRange =
6310 ISC.getIncrementSrcRange();
6311 ResultIterSpaces[CurrentNestedLoopCount].Subtract = ISC.shouldSubtractStep();
6312 ResultIterSpaces[CurrentNestedLoopCount].IsStrictCompare =
6313 ISC.isStrictTestOp();
6314 std::tie(ResultIterSpaces[CurrentNestedLoopCount].MinValue,
6315 ResultIterSpaces[CurrentNestedLoopCount].MaxValue) =
6316 ISC.buildMinMaxValues(DSA.getCurScope(), Captures);
6317 ResultIterSpaces[CurrentNestedLoopCount].FinalCondition =
6318 ISC.buildFinalCondition(DSA.getCurScope());
6319 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularLB =
6320 ISC.doesInitDependOnLC();
6321 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularUB =
6322 ISC.doesCondDependOnLC();
6323 ResultIterSpaces[CurrentNestedLoopCount].LoopDependentIdx =
6324 ISC.getLoopDependentIdx();
Alexander Musmana5f070a2014-10-01 06:03:56 +00006325
Alexey Bataevf8be4762019-08-14 19:30:06 +00006326 HasErrors |=
6327 (ResultIterSpaces[CurrentNestedLoopCount].PreCond == nullptr ||
6328 ResultIterSpaces[CurrentNestedLoopCount].NumIterations == nullptr ||
6329 ResultIterSpaces[CurrentNestedLoopCount].CounterVar == nullptr ||
6330 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar == nullptr ||
6331 ResultIterSpaces[CurrentNestedLoopCount].CounterInit == nullptr ||
6332 ResultIterSpaces[CurrentNestedLoopCount].CounterStep == nullptr);
Alexey Bataevf138fda2018-08-13 19:04:24 +00006333 if (!HasErrors && DSA.isOrderedRegion()) {
6334 if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
6335 if (CurrentNestedLoopCount <
6336 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
6337 DSA.getOrderedRegionParam().second->setLoopNumIterations(
Alexey Bataevf8be4762019-08-14 19:30:06 +00006338 CurrentNestedLoopCount,
6339 ResultIterSpaces[CurrentNestedLoopCount].NumIterations);
Alexey Bataevf138fda2018-08-13 19:04:24 +00006340 DSA.getOrderedRegionParam().second->setLoopCounter(
Alexey Bataevf8be4762019-08-14 19:30:06 +00006341 CurrentNestedLoopCount,
6342 ResultIterSpaces[CurrentNestedLoopCount].CounterVar);
Alexey Bataevf138fda2018-08-13 19:04:24 +00006343 }
6344 }
6345 for (auto &Pair : DSA.getDoacrossDependClauses()) {
6346 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) {
6347 // Erroneous case - clause has some problems.
6348 continue;
6349 }
6350 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink &&
6351 Pair.second.size() <= CurrentNestedLoopCount) {
6352 // Erroneous case - clause has some problems.
6353 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr);
6354 continue;
6355 }
6356 Expr *CntValue;
6357 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
6358 CntValue = ISC.buildOrderedLoopData(
Alexey Bataevf8be4762019-08-14 19:30:06 +00006359 DSA.getCurScope(),
6360 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
Alexey Bataevf138fda2018-08-13 19:04:24 +00006361 Pair.first->getDependencyLoc());
6362 else
6363 CntValue = ISC.buildOrderedLoopData(
Alexey Bataevf8be4762019-08-14 19:30:06 +00006364 DSA.getCurScope(),
6365 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
Alexey Bataevf138fda2018-08-13 19:04:24 +00006366 Pair.first->getDependencyLoc(),
6367 Pair.second[CurrentNestedLoopCount].first,
6368 Pair.second[CurrentNestedLoopCount].second);
6369 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue);
6370 }
6371 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00006372
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006373 return HasErrors;
6374}
6375
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006376/// Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00006377static ExprResult
Alexey Bataeve3727102018-04-18 15:57:46 +00006378buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
Alexey Bataevf8be4762019-08-14 19:30:06 +00006379 ExprResult Start, bool IsNonRectangularLB,
Alexey Bataeve3727102018-04-18 15:57:46 +00006380 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006381 // Build 'VarRef = Start.
Alexey Bataevf8be4762019-08-14 19:30:06 +00006382 ExprResult NewStart = IsNonRectangularLB
6383 ? Start.get()
6384 : tryBuildCapture(SemaRef, Start.get(), Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00006385 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006386 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00006387 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00006388 VarRef.get()->getType())) {
6389 NewStart = SemaRef.PerformImplicitConversion(
6390 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
6391 /*AllowExplicit=*/true);
6392 if (!NewStart.isUsable())
6393 return ExprError();
6394 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006395
Alexey Bataeve3727102018-04-18 15:57:46 +00006396 ExprResult Init =
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006397 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
6398 return Init;
6399}
6400
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006401/// Build 'VarRef = Start + Iter * Step'.
Alexey Bataeve3727102018-04-18 15:57:46 +00006402static ExprResult buildCounterUpdate(
6403 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
6404 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
Alexey Bataevf8be4762019-08-14 19:30:06 +00006405 bool IsNonRectangularLB,
Alexey Bataeve3727102018-04-18 15:57:46 +00006406 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00006407 // Add parentheses (for debugging purposes only).
6408 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
6409 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
6410 !Step.isUsable())
6411 return ExprError();
6412
Alexey Bataev5a3af132016-03-29 08:58:54 +00006413 ExprResult NewStep = Step;
6414 if (Captures)
6415 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006416 if (NewStep.isInvalid())
6417 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006418 ExprResult Update =
6419 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00006420 if (!Update.isUsable())
6421 return ExprError();
6422
Alexey Bataevc0214e02016-02-16 12:13:49 +00006423 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
6424 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataevf8be4762019-08-14 19:30:06 +00006425 if (!Start.isUsable())
6426 return ExprError();
6427 ExprResult NewStart = SemaRef.ActOnParenExpr(Loc, Loc, Start.get());
6428 if (!NewStart.isUsable())
6429 return ExprError();
6430 if (Captures && !IsNonRectangularLB)
Alexey Bataev5a3af132016-03-29 08:58:54 +00006431 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006432 if (NewStart.isInvalid())
6433 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00006434
Alexey Bataevc0214e02016-02-16 12:13:49 +00006435 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
6436 ExprResult SavedUpdate = Update;
6437 ExprResult UpdateVal;
6438 if (VarRef.get()->getType()->isOverloadableType() ||
6439 NewStart.get()->getType()->isOverloadableType() ||
6440 Update.get()->getType()->isOverloadableType()) {
Richard Smith2e3ed4a2019-08-16 19:53:22 +00006441 Sema::TentativeAnalysisScope Trap(SemaRef);
6442
Alexey Bataevc0214e02016-02-16 12:13:49 +00006443 Update =
6444 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
6445 if (Update.isUsable()) {
6446 UpdateVal =
6447 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
6448 VarRef.get(), SavedUpdate.get());
6449 if (UpdateVal.isUsable()) {
6450 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
6451 UpdateVal.get());
6452 }
6453 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00006454 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00006455
Alexey Bataevc0214e02016-02-16 12:13:49 +00006456 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
6457 if (!Update.isUsable() || !UpdateVal.isUsable()) {
6458 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
6459 NewStart.get(), SavedUpdate.get());
6460 if (!Update.isUsable())
6461 return ExprError();
6462
Alexey Bataev11481f52016-02-17 10:29:05 +00006463 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
6464 VarRef.get()->getType())) {
6465 Update = SemaRef.PerformImplicitConversion(
6466 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
6467 if (!Update.isUsable())
6468 return ExprError();
6469 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00006470
6471 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
6472 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00006473 return Update;
6474}
6475
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006476/// Convert integer expression \a E to make it have at least \a Bits
Alexander Musmana5f070a2014-10-01 06:03:56 +00006477/// bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00006478static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00006479 if (E == nullptr)
6480 return ExprError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006481 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00006482 QualType OldType = E->getType();
6483 unsigned HasBits = C.getTypeSize(OldType);
6484 if (HasBits >= Bits)
6485 return ExprResult(E);
6486 // OK to convert to signed, because new type has more bits than old.
6487 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
6488 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
6489 true);
6490}
6491
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006492/// Check if the given expression \a E is a constant integer that fits
Alexander Musmana5f070a2014-10-01 06:03:56 +00006493/// into \a Bits bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00006494static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00006495 if (E == nullptr)
6496 return false;
6497 llvm::APSInt Result;
6498 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
6499 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
6500 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006501}
6502
Alexey Bataev5a3af132016-03-29 08:58:54 +00006503/// Build preinits statement for the given declarations.
6504static Stmt *buildPreInits(ASTContext &Context,
Alexey Bataevc5514062017-10-25 15:44:52 +00006505 MutableArrayRef<Decl *> PreInits) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00006506 if (!PreInits.empty()) {
6507 return new (Context) DeclStmt(
6508 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
6509 SourceLocation(), SourceLocation());
6510 }
6511 return nullptr;
6512}
6513
6514/// Build preinits statement for the given declarations.
Alexey Bataevc5514062017-10-25 15:44:52 +00006515static Stmt *
6516buildPreInits(ASTContext &Context,
Alexey Bataeve3727102018-04-18 15:57:46 +00006517 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00006518 if (!Captures.empty()) {
6519 SmallVector<Decl *, 16> PreInits;
Alexey Bataeve3727102018-04-18 15:57:46 +00006520 for (const auto &Pair : Captures)
Alexey Bataev5a3af132016-03-29 08:58:54 +00006521 PreInits.push_back(Pair.second->getDecl());
6522 return buildPreInits(Context, PreInits);
6523 }
6524 return nullptr;
6525}
6526
6527/// Build postupdate expression for the given list of postupdates expressions.
6528static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
6529 Expr *PostUpdate = nullptr;
6530 if (!PostUpdates.empty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006531 for (Expr *E : PostUpdates) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00006532 Expr *ConvE = S.BuildCStyleCastExpr(
6533 E->getExprLoc(),
6534 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
6535 E->getExprLoc(), E)
6536 .get();
6537 PostUpdate = PostUpdate
6538 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
6539 PostUpdate, ConvE)
6540 .get()
6541 : ConvE;
6542 }
6543 }
6544 return PostUpdate;
6545}
6546
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006547/// Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00006548/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
6549/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00006550static unsigned
Alexey Bataeve3727102018-04-18 15:57:46 +00006551checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
Alexey Bataev10e775f2015-07-30 11:36:16 +00006552 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
6553 DSAStackTy &DSA,
Alexey Bataeve3727102018-04-18 15:57:46 +00006554 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00006555 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00006556 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006557 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00006558 // Found 'collapse' clause - calculate collapse number.
Fangrui Song407659a2018-11-30 23:41:18 +00006559 Expr::EvalResult Result;
Dmitri Gribenko04323c22019-05-17 17:16:53 +00006560 if (!CollapseLoopCountExpr->isValueDependent() &&
6561 CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
Fangrui Song407659a2018-11-30 23:41:18 +00006562 NestedLoopCount = Result.Val.getInt().getLimitedValue();
Dmitri Gribenko04323c22019-05-17 17:16:53 +00006563 } else {
Rui Ueyama49a3ad22019-07-16 04:46:31 +00006564 Built.clear(/*Size=*/1);
Dmitri Gribenko04323c22019-05-17 17:16:53 +00006565 return 1;
6566 }
Alexey Bataev10e775f2015-07-30 11:36:16 +00006567 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00006568 unsigned OrderedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006569 if (OrderedLoopCountExpr) {
6570 // Found 'ordered' clause - calculate collapse number.
Fangrui Song407659a2018-11-30 23:41:18 +00006571 Expr::EvalResult EVResult;
Dmitri Gribenko04323c22019-05-17 17:16:53 +00006572 if (!OrderedLoopCountExpr->isValueDependent() &&
6573 OrderedLoopCountExpr->EvaluateAsInt(EVResult,
6574 SemaRef.getASTContext())) {
Fangrui Song407659a2018-11-30 23:41:18 +00006575 llvm::APSInt Result = EVResult.Val.getInt();
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006576 if (Result.getLimitedValue() < NestedLoopCount) {
6577 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
6578 diag::err_omp_wrong_ordered_loop_count)
6579 << OrderedLoopCountExpr->getSourceRange();
6580 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
6581 diag::note_collapse_loop_count)
6582 << CollapseLoopCountExpr->getSourceRange();
6583 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00006584 OrderedLoopCount = Result.getLimitedValue();
Dmitri Gribenko04323c22019-05-17 17:16:53 +00006585 } else {
Rui Ueyama49a3ad22019-07-16 04:46:31 +00006586 Built.clear(/*Size=*/1);
Dmitri Gribenko04323c22019-05-17 17:16:53 +00006587 return 1;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006588 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00006589 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006590 // This is helper routine for loop directives (e.g., 'for', 'simd',
6591 // 'for simd', etc.).
Alexey Bataeve3727102018-04-18 15:57:46 +00006592 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev316ccf62019-01-29 18:51:58 +00006593 SmallVector<LoopIterationSpace, 4> IterSpaces(
6594 std::max(OrderedLoopCount, NestedLoopCount));
Alexander Musmana5f070a2014-10-01 06:03:56 +00006595 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006596 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00006597 if (checkOpenMPIterationSpace(
6598 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
6599 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
Alexey Bataevf8be4762019-08-14 19:30:06 +00006600 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces, Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00006601 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006602 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00006603 // OpenMP [2.8.1, simd construct, Restrictions]
6604 // All loops associated with the construct must be perfectly nested; that
6605 // is, there must be no intervening code nor any OpenMP directive between
6606 // any two loops.
6607 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006608 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00006609 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) {
6610 if (checkOpenMPIterationSpace(
6611 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
6612 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
Alexey Bataevf8be4762019-08-14 19:30:06 +00006613 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces, Captures))
Alexey Bataevf138fda2018-08-13 19:04:24 +00006614 return 0;
6615 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) {
6616 // Handle initialization of captured loop iterator variables.
6617 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar);
6618 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) {
6619 Captures[DRE] = DRE;
6620 }
6621 }
6622 // Move on to the next nested for loop, or to the loop body.
6623 // OpenMP [2.8.1, simd construct, Restrictions]
6624 // All loops associated with the construct must be perfectly nested; that
6625 // is, there must be no intervening code nor any OpenMP directive between
6626 // any two loops.
6627 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
6628 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006629
Alexander Musmana5f070a2014-10-01 06:03:56 +00006630 Built.clear(/* size */ NestedLoopCount);
6631
6632 if (SemaRef.CurContext->isDependentContext())
6633 return NestedLoopCount;
6634
6635 // An example of what is generated for the following code:
6636 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00006637 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00006638 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00006639 // for (k = 0; k < NK; ++k)
6640 // for (j = J0; j < NJ; j+=2) {
6641 // <loop body>
6642 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00006643 //
6644 // We generate the code below.
6645 // Note: the loop body may be outlined in CodeGen.
6646 // Note: some counters may be C++ classes, operator- is used to find number of
6647 // iterations and operator+= to calculate counter value.
6648 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
6649 // or i64 is currently supported).
6650 //
6651 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
6652 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
6653 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
6654 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
6655 // // similar updates for vars in clauses (e.g. 'linear')
6656 // <loop body (using local i and j)>
6657 // }
6658 // i = NI; // assign final values of counters
6659 // j = NJ;
6660 //
6661
6662 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
6663 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00006664 // Precondition tests if there is at least one iteration (all conditions are
6665 // true).
6666 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexey Bataeve3727102018-04-18 15:57:46 +00006667 Expr *N0 = IterSpaces[0].NumIterations;
6668 ExprResult LastIteration32 =
6669 widenIterationCount(/*Bits=*/32,
6670 SemaRef
6671 .PerformImplicitConversion(
6672 N0->IgnoreImpCasts(), N0->getType(),
6673 Sema::AA_Converting, /*AllowExplicit=*/true)
6674 .get(),
6675 SemaRef);
6676 ExprResult LastIteration64 = widenIterationCount(
6677 /*Bits=*/64,
6678 SemaRef
6679 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(),
6680 Sema::AA_Converting,
6681 /*AllowExplicit=*/true)
6682 .get(),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006683 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00006684
6685 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
6686 return NestedLoopCount;
6687
Alexey Bataeve3727102018-04-18 15:57:46 +00006688 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00006689 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
6690
6691 Scope *CurScope = DSA.getCurScope();
6692 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00006693 if (PreCond.isUsable()) {
Alexey Bataeva7206b92016-12-20 16:51:02 +00006694 PreCond =
6695 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
6696 PreCond.get(), IterSpaces[Cnt].PreCond);
Alexey Bataev62dbb972015-04-22 11:59:37 +00006697 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006698 Expr *N = IterSpaces[Cnt].NumIterations;
Alexey Bataeva7206b92016-12-20 16:51:02 +00006699 SourceLocation Loc = N->getExprLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00006700 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
6701 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006702 LastIteration32 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00006703 CurScope, Loc, BO_Mul, LastIteration32.get(),
David Majnemer9d168222016-08-05 17:44:54 +00006704 SemaRef
6705 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
6706 Sema::AA_Converting,
6707 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006708 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00006709 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006710 LastIteration64 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00006711 CurScope, Loc, BO_Mul, LastIteration64.get(),
David Majnemer9d168222016-08-05 17:44:54 +00006712 SemaRef
6713 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
6714 Sema::AA_Converting,
6715 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00006716 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00006717 }
6718
6719 // Choose either the 32-bit or 64-bit version.
6720 ExprResult LastIteration = LastIteration64;
Gheorghe-Teodor Berceaa3afcf22019-01-09 20:38:35 +00006721 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse ||
6722 (LastIteration32.isUsable() &&
6723 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
6724 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
6725 fitsInto(
6726 /*Bits=*/32,
6727 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
6728 LastIteration64.get(), SemaRef))))
Alexander Musmana5f070a2014-10-01 06:03:56 +00006729 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00006730 QualType VType = LastIteration.get()->getType();
6731 QualType RealVType = VType;
6732 QualType StrideVType = VType;
6733 if (isOpenMPTaskLoopDirective(DKind)) {
6734 VType =
6735 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
6736 StrideVType =
6737 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
6738 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00006739
6740 if (!LastIteration.isUsable())
6741 return 0;
6742
6743 // Save the number of iterations.
6744 ExprResult NumIterations = LastIteration;
6745 {
6746 LastIteration = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00006747 CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
6748 LastIteration.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00006749 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
6750 if (!LastIteration.isUsable())
6751 return 0;
6752 }
6753
6754 // Calculate the last iteration number beforehand instead of doing this on
6755 // each iteration. Do not do this if the number of iterations may be kfold-ed.
6756 llvm::APSInt Result;
6757 bool IsConstant =
6758 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
6759 ExprResult CalcLastIteration;
6760 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00006761 ExprResult SaveRef =
6762 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00006763 LastIteration = SaveRef;
6764
6765 // Prepare SaveRef + 1.
6766 NumIterations = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00006767 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00006768 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
6769 if (!NumIterations.isUsable())
6770 return 0;
6771 }
6772
6773 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
6774
David Majnemer9d168222016-08-05 17:44:54 +00006775 // Build variables passed into runtime, necessary for worksharing directives.
Carlo Bertolliffafe102017-04-20 00:39:39 +00006776 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006777 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
6778 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00006779 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00006780 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
6781 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00006782 SemaRef.AddInitializerToDecl(LBDecl,
6783 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
6784 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00006785
6786 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00006787 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
6788 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00006789 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00006790 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00006791
6792 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
6793 // This will be used to implement clause 'lastprivate'.
6794 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006795 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
6796 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00006797 SemaRef.AddInitializerToDecl(ILDecl,
6798 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
6799 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00006800
6801 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00006802 VarDecl *STDecl =
6803 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
6804 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00006805 SemaRef.AddInitializerToDecl(STDecl,
6806 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
6807 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00006808
6809 // Build expression: UB = min(UB, LastIteration)
David Majnemer9d168222016-08-05 17:44:54 +00006810 // It is necessary for CodeGen of directives with static scheduling.
Alexander Musmanc6388682014-12-15 07:07:06 +00006811 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
6812 UB.get(), LastIteration.get());
6813 ExprResult CondOp = SemaRef.ActOnConditionalOp(
Alexey Bataev86ec3fe2018-07-25 14:40:26 +00006814 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(),
6815 LastIteration.get(), UB.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00006816 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
6817 CondOp.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00006818 EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false);
Carlo Bertolli9925f152016-06-27 14:55:37 +00006819
6820 // If we have a combined directive that combines 'distribute', 'for' or
6821 // 'simd' we need to be able to access the bounds of the schedule of the
6822 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
6823 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
6824 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Carlo Bertolliffafe102017-04-20 00:39:39 +00006825 // Lower bound variable, initialized with zero.
6826 VarDecl *CombLBDecl =
6827 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
6828 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
6829 SemaRef.AddInitializerToDecl(
6830 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
6831 /*DirectInit*/ false);
6832
6833 // Upper bound variable, initialized with last iteration number.
6834 VarDecl *CombUBDecl =
6835 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
6836 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
6837 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
6838 /*DirectInit*/ false);
6839
6840 ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
6841 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
6842 ExprResult CombCondOp =
6843 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
6844 LastIteration.get(), CombUB.get());
6845 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
6846 CombCondOp.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00006847 CombEUB =
6848 SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00006849
Alexey Bataeve3727102018-04-18 15:57:46 +00006850 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
Carlo Bertolli9925f152016-06-27 14:55:37 +00006851 // We expect to have at least 2 more parameters than the 'parallel'
6852 // directive does - the lower and upper bounds of the previous schedule.
6853 assert(CD->getNumParams() >= 4 &&
6854 "Unexpected number of parameters in loop combined directive");
6855
6856 // Set the proper type for the bounds given what we learned from the
6857 // enclosed loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00006858 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
6859 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
Carlo Bertolli9925f152016-06-27 14:55:37 +00006860
6861 // Previous lower and upper bounds are obtained from the region
6862 // parameters.
6863 PrevLB =
6864 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
6865 PrevUB =
6866 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
6867 }
Alexander Musmanc6388682014-12-15 07:07:06 +00006868 }
6869
6870 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00006871 ExprResult IV;
Carlo Bertolliffafe102017-04-20 00:39:39 +00006872 ExprResult Init, CombInit;
Alexander Musmana5f070a2014-10-01 06:03:56 +00006873 {
Alexey Bataev7292c292016-04-25 12:22:29 +00006874 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
6875 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
David Majnemer9d168222016-08-05 17:44:54 +00006876 Expr *RHS =
6877 (isOpenMPWorksharingDirective(DKind) ||
6878 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
6879 ? LB.get()
6880 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
Alexander Musmanc6388682014-12-15 07:07:06 +00006881 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00006882 Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00006883
6884 if (isOpenMPLoopBoundSharingDirective(DKind)) {
6885 Expr *CombRHS =
6886 (isOpenMPWorksharingDirective(DKind) ||
6887 isOpenMPTaskLoopDirective(DKind) ||
6888 isOpenMPDistributeDirective(DKind))
6889 ? CombLB.get()
6890 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
6891 CombInit =
6892 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00006893 CombInit =
6894 SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00006895 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00006896 }
6897
Alexey Bataev316ccf62019-01-29 18:51:58 +00006898 bool UseStrictCompare =
6899 RealVType->hasUnsignedIntegerRepresentation() &&
6900 llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) {
6901 return LIS.IsStrictCompare;
6902 });
6903 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for
6904 // unsigned IV)) for worksharing loops.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006905 SourceLocation CondLoc = AStmt->getBeginLoc();
Alexey Bataev316ccf62019-01-29 18:51:58 +00006906 Expr *BoundUB = UB.get();
6907 if (UseStrictCompare) {
6908 BoundUB =
6909 SemaRef
6910 .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB,
6911 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
6912 .get();
6913 BoundUB =
6914 SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get();
6915 }
Alexander Musmanc6388682014-12-15 07:07:06 +00006916 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006917 (isOpenMPWorksharingDirective(DKind) ||
6918 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexey Bataev316ccf62019-01-29 18:51:58 +00006919 ? SemaRef.BuildBinOp(CurScope, CondLoc,
6920 UseStrictCompare ? BO_LT : BO_LE, IV.get(),
6921 BoundUB)
Alexander Musmanc6388682014-12-15 07:07:06 +00006922 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
6923 NumIterations.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00006924 ExprResult CombDistCond;
6925 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Alexey Bataev316ccf62019-01-29 18:51:58 +00006926 CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
6927 NumIterations.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00006928 }
6929
Carlo Bertolliffafe102017-04-20 00:39:39 +00006930 ExprResult CombCond;
6931 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Alexey Bataev316ccf62019-01-29 18:51:58 +00006932 Expr *BoundCombUB = CombUB.get();
6933 if (UseStrictCompare) {
6934 BoundCombUB =
6935 SemaRef
6936 .BuildBinOp(
6937 CurScope, CondLoc, BO_Add, BoundCombUB,
6938 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
6939 .get();
6940 BoundCombUB =
6941 SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false)
6942 .get();
6943 }
Carlo Bertolliffafe102017-04-20 00:39:39 +00006944 CombCond =
Alexey Bataev316ccf62019-01-29 18:51:58 +00006945 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE,
6946 IV.get(), BoundCombUB);
Carlo Bertolliffafe102017-04-20 00:39:39 +00006947 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00006948 // Loop increment (IV = IV + 1)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006949 SourceLocation IncLoc = AStmt->getBeginLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00006950 ExprResult Inc =
6951 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
6952 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
6953 if (!Inc.isUsable())
6954 return 0;
6955 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00006956 Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00006957 if (!Inc.isUsable())
6958 return 0;
6959
6960 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
6961 // Used for directives with static scheduling.
Carlo Bertolliffafe102017-04-20 00:39:39 +00006962 // In combined construct, add combined version that use CombLB and CombUB
6963 // base variables for the update
6964 ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006965 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
6966 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00006967 // LB + ST
6968 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
6969 if (!NextLB.isUsable())
6970 return 0;
6971 // LB = LB + ST
6972 NextLB =
6973 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00006974 NextLB =
6975 SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00006976 if (!NextLB.isUsable())
6977 return 0;
6978 // UB + ST
6979 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
6980 if (!NextUB.isUsable())
6981 return 0;
6982 // UB = UB + ST
6983 NextUB =
6984 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00006985 NextUB =
6986 SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00006987 if (!NextUB.isUsable())
6988 return 0;
Carlo Bertolliffafe102017-04-20 00:39:39 +00006989 if (isOpenMPLoopBoundSharingDirective(DKind)) {
6990 CombNextLB =
6991 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
6992 if (!NextLB.isUsable())
6993 return 0;
6994 // LB = LB + ST
6995 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
6996 CombNextLB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00006997 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(),
6998 /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00006999 if (!CombNextLB.isUsable())
7000 return 0;
7001 // UB + ST
7002 CombNextUB =
7003 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
7004 if (!CombNextUB.isUsable())
7005 return 0;
7006 // UB = UB + ST
7007 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
7008 CombNextUB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00007009 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(),
7010 /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00007011 if (!CombNextUB.isUsable())
7012 return 0;
7013 }
Alexander Musmanc6388682014-12-15 07:07:06 +00007014 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00007015
Carlo Bertolliffafe102017-04-20 00:39:39 +00007016 // Create increment expression for distribute loop when combined in a same
Carlo Bertolli8429d812017-02-17 21:29:13 +00007017 // directive with for as IV = IV + ST; ensure upper bound expression based
7018 // on PrevUB instead of NumIterations - used to implement 'for' when found
7019 // in combination with 'distribute', like in 'distribute parallel for'
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007020 SourceLocation DistIncLoc = AStmt->getBeginLoc();
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00007021 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond;
Carlo Bertolli8429d812017-02-17 21:29:13 +00007022 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Alexey Bataev316ccf62019-01-29 18:51:58 +00007023 DistCond = SemaRef.BuildBinOp(
7024 CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB);
Carlo Bertolli8429d812017-02-17 21:29:13 +00007025 assert(DistCond.isUsable() && "distribute cond expr was not built");
7026
7027 DistInc =
7028 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
7029 assert(DistInc.isUsable() && "distribute inc expr was not built");
7030 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
7031 DistInc.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00007032 DistInc =
7033 SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false);
Carlo Bertolli8429d812017-02-17 21:29:13 +00007034 assert(DistInc.isUsable() && "distribute inc expr was not built");
7035
7036 // Build expression: UB = min(UB, prevUB) for #for in composite or combined
7037 // construct
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007038 SourceLocation DistEUBLoc = AStmt->getBeginLoc();
Carlo Bertolli8429d812017-02-17 21:29:13 +00007039 ExprResult IsUBGreater =
7040 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
7041 ExprResult CondOp = SemaRef.ActOnConditionalOp(
7042 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
7043 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
7044 CondOp.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00007045 PrevEUB =
7046 SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false);
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00007047
Alexey Bataev316ccf62019-01-29 18:51:58 +00007048 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in
7049 // parallel for is in combination with a distribute directive with
7050 // schedule(static, 1)
7051 Expr *BoundPrevUB = PrevUB.get();
7052 if (UseStrictCompare) {
7053 BoundPrevUB =
7054 SemaRef
7055 .BuildBinOp(
7056 CurScope, CondLoc, BO_Add, BoundPrevUB,
7057 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
7058 .get();
7059 BoundPrevUB =
7060 SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false)
7061 .get();
7062 }
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00007063 ParForInDistCond =
Alexey Bataev316ccf62019-01-29 18:51:58 +00007064 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE,
7065 IV.get(), BoundPrevUB);
Carlo Bertolli8429d812017-02-17 21:29:13 +00007066 }
7067
Alexander Musmana5f070a2014-10-01 06:03:56 +00007068 // Build updates and final values of the loop counters.
7069 bool HasErrors = false;
7070 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00007071 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00007072 Built.Updates.resize(NestedLoopCount);
7073 Built.Finals.resize(NestedLoopCount);
Alexey Bataevf8be4762019-08-14 19:30:06 +00007074 Built.DependentCounters.resize(NestedLoopCount);
7075 Built.DependentInits.resize(NestedLoopCount);
7076 Built.FinalsConditions.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00007077 {
Gheorghe-Teodor Bercea677960642019-01-09 20:45:26 +00007078 // We implement the following algorithm for obtaining the
7079 // original loop iteration variable values based on the
7080 // value of the collapsed loop iteration variable IV.
7081 //
7082 // Let n+1 be the number of collapsed loops in the nest.
7083 // Iteration variables (I0, I1, .... In)
7084 // Iteration counts (N0, N1, ... Nn)
7085 //
7086 // Acc = IV;
7087 //
7088 // To compute Ik for loop k, 0 <= k <= n, generate:
7089 // Prod = N(k+1) * N(k+2) * ... * Nn;
7090 // Ik = Acc / Prod;
7091 // Acc -= Ik * Prod;
7092 //
7093 ExprResult Acc = IV;
7094 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00007095 LoopIterationSpace &IS = IterSpaces[Cnt];
7096 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
Alexander Musmana5f070a2014-10-01 06:03:56 +00007097 ExprResult Iter;
Alexander Musmana5f070a2014-10-01 06:03:56 +00007098
Gheorghe-Teodor Bercea677960642019-01-09 20:45:26 +00007099 // Compute prod
7100 ExprResult Prod =
7101 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
7102 for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K)
7103 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(),
7104 IterSpaces[K].NumIterations);
7105
7106 // Iter = Acc / Prod
7107 // If there is at least one more inner loop to avoid
7108 // multiplication by 1.
7109 if (Cnt + 1 < NestedLoopCount)
7110 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div,
7111 Acc.get(), Prod.get());
7112 else
7113 Iter = Acc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00007114 if (!Iter.isUsable()) {
7115 HasErrors = true;
7116 break;
7117 }
7118
Gheorghe-Teodor Bercea677960642019-01-09 20:45:26 +00007119 // Update Acc:
7120 // Acc -= Iter * Prod
7121 // Check if there is at least one more inner loop to avoid
7122 // multiplication by 1.
7123 if (Cnt + 1 < NestedLoopCount)
7124 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul,
7125 Iter.get(), Prod.get());
7126 else
7127 Prod = Iter;
7128 Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub,
7129 Acc.get(), Prod.get());
7130
Alexey Bataev39f915b82015-05-08 10:41:21 +00007131 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007132 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00007133 DeclRefExpr *CounterVar = buildDeclRefExpr(
7134 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
7135 /*RefersToCapture=*/true);
Alexey Bataevf8be4762019-08-14 19:30:06 +00007136 ExprResult Init =
7137 buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
7138 IS.CounterInit, IS.IsNonRectangularLB, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00007139 if (!Init.isUsable()) {
7140 HasErrors = true;
7141 break;
7142 }
Alexey Bataeve3727102018-04-18 15:57:46 +00007143 ExprResult Update = buildCounterUpdate(
Alexey Bataev5a3af132016-03-29 08:58:54 +00007144 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
Alexey Bataevf8be4762019-08-14 19:30:06 +00007145 IS.CounterStep, IS.Subtract, IS.IsNonRectangularLB, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00007146 if (!Update.isUsable()) {
7147 HasErrors = true;
7148 break;
7149 }
7150
7151 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
Alexey Bataevf8be4762019-08-14 19:30:06 +00007152 ExprResult Final =
7153 buildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
7154 IS.CounterInit, IS.NumIterations, IS.CounterStep,
7155 IS.Subtract, IS.IsNonRectangularLB, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00007156 if (!Final.isUsable()) {
7157 HasErrors = true;
7158 break;
7159 }
7160
Alexander Musmana5f070a2014-10-01 06:03:56 +00007161 if (!Update.isUsable() || !Final.isUsable()) {
7162 HasErrors = true;
7163 break;
7164 }
7165 // Save results
7166 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00007167 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00007168 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00007169 Built.Updates[Cnt] = Update.get();
7170 Built.Finals[Cnt] = Final.get();
Alexey Bataevf8be4762019-08-14 19:30:06 +00007171 Built.DependentCounters[Cnt] = nullptr;
7172 Built.DependentInits[Cnt] = nullptr;
7173 Built.FinalsConditions[Cnt] = nullptr;
7174 if (IS.IsNonRectangularLB) {
7175 Built.DependentCounters[Cnt] =
7176 Built.Counters[NestedLoopCount - 1 - IS.LoopDependentIdx];
7177 Built.DependentInits[Cnt] =
7178 Built.Inits[NestedLoopCount - 1 - IS.LoopDependentIdx];
7179 Built.FinalsConditions[Cnt] = IS.FinalCondition;
7180 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00007181 }
7182 }
7183
7184 if (HasErrors)
7185 return 0;
7186
7187 // Save results
7188 Built.IterationVarRef = IV.get();
7189 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00007190 Built.NumIterations = NumIterations.get();
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00007191 Built.CalcLastIteration = SemaRef
7192 .ActOnFinishFullExpr(CalcLastIteration.get(),
Alexey Bataevf8be4762019-08-14 19:30:06 +00007193 /*DiscardedValue=*/false)
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00007194 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00007195 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00007196 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00007197 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00007198 Built.Init = Init.get();
7199 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00007200 Built.LB = LB.get();
7201 Built.UB = UB.get();
7202 Built.IL = IL.get();
7203 Built.ST = ST.get();
7204 Built.EUB = EUB.get();
7205 Built.NLB = NextLB.get();
7206 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00007207 Built.PrevLB = PrevLB.get();
7208 Built.PrevUB = PrevUB.get();
Carlo Bertolli8429d812017-02-17 21:29:13 +00007209 Built.DistInc = DistInc.get();
7210 Built.PrevEUB = PrevEUB.get();
Carlo Bertolliffafe102017-04-20 00:39:39 +00007211 Built.DistCombinedFields.LB = CombLB.get();
7212 Built.DistCombinedFields.UB = CombUB.get();
7213 Built.DistCombinedFields.EUB = CombEUB.get();
7214 Built.DistCombinedFields.Init = CombInit.get();
7215 Built.DistCombinedFields.Cond = CombCond.get();
7216 Built.DistCombinedFields.NLB = CombNextLB.get();
7217 Built.DistCombinedFields.NUB = CombNextUB.get();
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00007218 Built.DistCombinedFields.DistCond = CombDistCond.get();
7219 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00007220
Alexey Bataevabfc0692014-06-25 06:52:00 +00007221 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007222}
7223
Alexey Bataev10e775f2015-07-30 11:36:16 +00007224static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00007225 auto CollapseClauses =
7226 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
7227 if (CollapseClauses.begin() != CollapseClauses.end())
7228 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00007229 return nullptr;
7230}
7231
Alexey Bataev10e775f2015-07-30 11:36:16 +00007232static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00007233 auto OrderedClauses =
7234 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
7235 if (OrderedClauses.begin() != OrderedClauses.end())
7236 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00007237 return nullptr;
7238}
7239
Kelvin Lic5609492016-07-15 04:39:07 +00007240static bool checkSimdlenSafelenSpecified(Sema &S,
7241 const ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007242 const OMPSafelenClause *Safelen = nullptr;
7243 const OMPSimdlenClause *Simdlen = nullptr;
Kelvin Lic5609492016-07-15 04:39:07 +00007244
Alexey Bataeve3727102018-04-18 15:57:46 +00007245 for (const OMPClause *Clause : Clauses) {
Kelvin Lic5609492016-07-15 04:39:07 +00007246 if (Clause->getClauseKind() == OMPC_safelen)
7247 Safelen = cast<OMPSafelenClause>(Clause);
7248 else if (Clause->getClauseKind() == OMPC_simdlen)
7249 Simdlen = cast<OMPSimdlenClause>(Clause);
7250 if (Safelen && Simdlen)
7251 break;
7252 }
7253
7254 if (Simdlen && Safelen) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007255 const Expr *SimdlenLength = Simdlen->getSimdlen();
7256 const Expr *SafelenLength = Safelen->getSafelen();
Kelvin Lic5609492016-07-15 04:39:07 +00007257 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
7258 SimdlenLength->isInstantiationDependent() ||
7259 SimdlenLength->containsUnexpandedParameterPack())
7260 return false;
7261 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
7262 SafelenLength->isInstantiationDependent() ||
7263 SafelenLength->containsUnexpandedParameterPack())
7264 return false;
Fangrui Song407659a2018-11-30 23:41:18 +00007265 Expr::EvalResult SimdlenResult, SafelenResult;
7266 SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context);
7267 SafelenLength->EvaluateAsInt(SafelenResult, S.Context);
7268 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt();
7269 llvm::APSInt SafelenRes = SafelenResult.Val.getInt();
Kelvin Lic5609492016-07-15 04:39:07 +00007270 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
7271 // If both simdlen and safelen clauses are specified, the value of the
7272 // simdlen parameter must be less than or equal to the value of the safelen
7273 // parameter.
7274 if (SimdlenRes > SafelenRes) {
7275 S.Diag(SimdlenLength->getExprLoc(),
7276 diag::err_omp_wrong_simdlen_safelen_values)
7277 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
7278 return true;
7279 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00007280 }
7281 return false;
7282}
7283
Alexey Bataeve3727102018-04-18 15:57:46 +00007284StmtResult
7285Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
7286 SourceLocation StartLoc, SourceLocation EndLoc,
7287 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007288 if (!AStmt)
7289 return StmtError();
7290
7291 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00007292 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007293 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7294 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007295 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00007296 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
7297 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00007298 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00007299 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00007300
Alexander Musmana5f070a2014-10-01 06:03:56 +00007301 assert((CurContext->isDependentContext() || B.builtAll()) &&
7302 "omp simd loop exprs were not built");
7303
Alexander Musman3276a272015-03-21 10:12:56 +00007304 if (!CurContext->isDependentContext()) {
7305 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007306 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007307 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexander Musman3276a272015-03-21 10:12:56 +00007308 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007309 B.NumIterations, *this, CurScope,
7310 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00007311 return StmtError();
7312 }
7313 }
7314
Kelvin Lic5609492016-07-15 04:39:07 +00007315 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00007316 return StmtError();
7317
Reid Kleckner87a31802018-03-12 21:43:02 +00007318 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00007319 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
7320 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00007321}
7322
Alexey Bataeve3727102018-04-18 15:57:46 +00007323StmtResult
7324Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
7325 SourceLocation StartLoc, SourceLocation EndLoc,
7326 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007327 if (!AStmt)
7328 return StmtError();
7329
7330 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00007331 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007332 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7333 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007334 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00007335 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
7336 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00007337 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00007338 return StmtError();
7339
Alexander Musmana5f070a2014-10-01 06:03:56 +00007340 assert((CurContext->isDependentContext() || B.builtAll()) &&
7341 "omp for loop exprs were not built");
7342
Alexey Bataev54acd402015-08-04 11:18:19 +00007343 if (!CurContext->isDependentContext()) {
7344 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007345 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007346 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00007347 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007348 B.NumIterations, *this, CurScope,
7349 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00007350 return StmtError();
7351 }
7352 }
7353
Reid Kleckner87a31802018-03-12 21:43:02 +00007354 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00007355 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00007356 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00007357}
7358
Alexander Musmanf82886e2014-09-18 05:12:34 +00007359StmtResult Sema::ActOnOpenMPForSimdDirective(
7360 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007361 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007362 if (!AStmt)
7363 return StmtError();
7364
7365 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00007366 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007367 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7368 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00007369 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007370 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00007371 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7372 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00007373 if (NestedLoopCount == 0)
7374 return StmtError();
7375
Alexander Musmanc6388682014-12-15 07:07:06 +00007376 assert((CurContext->isDependentContext() || B.builtAll()) &&
7377 "omp for simd loop exprs were not built");
7378
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00007379 if (!CurContext->isDependentContext()) {
7380 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007381 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007382 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00007383 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007384 B.NumIterations, *this, CurScope,
7385 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00007386 return StmtError();
7387 }
7388 }
7389
Kelvin Lic5609492016-07-15 04:39:07 +00007390 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00007391 return StmtError();
7392
Reid Kleckner87a31802018-03-12 21:43:02 +00007393 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00007394 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
7395 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00007396}
7397
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00007398StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
7399 Stmt *AStmt,
7400 SourceLocation StartLoc,
7401 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007402 if (!AStmt)
7403 return StmtError();
7404
7405 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00007406 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00007407 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00007408 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00007409 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00007410 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00007411 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00007412 return StmtError();
7413 // All associated statements must be '#pragma omp section' except for
7414 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00007415 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00007416 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
7417 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007418 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev1e0498a2014-06-26 08:21:58 +00007419 diag::err_omp_sections_substmt_not_section);
7420 return StmtError();
7421 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00007422 cast<OMPSectionDirective>(SectionStmt)
7423 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00007424 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00007425 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007426 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00007427 return StmtError();
7428 }
7429
Reid Kleckner87a31802018-03-12 21:43:02 +00007430 setFunctionHasBranchProtectedScope();
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00007431
Alexey Bataev25e5b442015-09-15 12:52:43 +00007432 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
7433 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00007434}
7435
Alexey Bataev1e0498a2014-06-26 08:21:58 +00007436StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
7437 SourceLocation StartLoc,
7438 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007439 if (!AStmt)
7440 return StmtError();
7441
7442 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00007443
Reid Kleckner87a31802018-03-12 21:43:02 +00007444 setFunctionHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00007445 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00007446
Alexey Bataev25e5b442015-09-15 12:52:43 +00007447 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
7448 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00007449}
7450
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00007451StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
7452 Stmt *AStmt,
7453 SourceLocation StartLoc,
7454 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007455 if (!AStmt)
7456 return StmtError();
7457
7458 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00007459
Reid Kleckner87a31802018-03-12 21:43:02 +00007460 setFunctionHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00007461
Alexey Bataev3255bf32015-01-19 05:20:46 +00007462 // OpenMP [2.7.3, single Construct, Restrictions]
7463 // The copyprivate clause must not be used with the nowait clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00007464 const OMPClause *Nowait = nullptr;
7465 const OMPClause *Copyprivate = nullptr;
7466 for (const OMPClause *Clause : Clauses) {
Alexey Bataev3255bf32015-01-19 05:20:46 +00007467 if (Clause->getClauseKind() == OMPC_nowait)
7468 Nowait = Clause;
7469 else if (Clause->getClauseKind() == OMPC_copyprivate)
7470 Copyprivate = Clause;
7471 if (Copyprivate && Nowait) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007472 Diag(Copyprivate->getBeginLoc(),
Alexey Bataev3255bf32015-01-19 05:20:46 +00007473 diag::err_omp_single_copyprivate_with_nowait);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007474 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here);
Alexey Bataev3255bf32015-01-19 05:20:46 +00007475 return StmtError();
7476 }
7477 }
7478
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00007479 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7480}
7481
Alexander Musman80c22892014-07-17 08:54:58 +00007482StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
7483 SourceLocation StartLoc,
7484 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007485 if (!AStmt)
7486 return StmtError();
7487
7488 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00007489
Reid Kleckner87a31802018-03-12 21:43:02 +00007490 setFunctionHasBranchProtectedScope();
Alexander Musman80c22892014-07-17 08:54:58 +00007491
7492 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
7493}
7494
Alexey Bataev28c75412015-12-15 08:19:24 +00007495StmtResult Sema::ActOnOpenMPCriticalDirective(
7496 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
7497 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007498 if (!AStmt)
7499 return StmtError();
7500
7501 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00007502
Alexey Bataev28c75412015-12-15 08:19:24 +00007503 bool ErrorFound = false;
7504 llvm::APSInt Hint;
7505 SourceLocation HintLoc;
7506 bool DependentHint = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00007507 for (const OMPClause *C : Clauses) {
Alexey Bataev28c75412015-12-15 08:19:24 +00007508 if (C->getClauseKind() == OMPC_hint) {
7509 if (!DirName.getName()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007510 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name);
Alexey Bataev28c75412015-12-15 08:19:24 +00007511 ErrorFound = true;
7512 }
7513 Expr *E = cast<OMPHintClause>(C)->getHint();
7514 if (E->isTypeDependent() || E->isValueDependent() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00007515 E->isInstantiationDependent()) {
Alexey Bataev28c75412015-12-15 08:19:24 +00007516 DependentHint = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00007517 } else {
Alexey Bataev28c75412015-12-15 08:19:24 +00007518 Hint = E->EvaluateKnownConstInt(Context);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007519 HintLoc = C->getBeginLoc();
Alexey Bataev28c75412015-12-15 08:19:24 +00007520 }
7521 }
7522 }
7523 if (ErrorFound)
7524 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00007525 const auto Pair = DSAStack->getCriticalWithHint(DirName);
Alexey Bataev28c75412015-12-15 08:19:24 +00007526 if (Pair.first && DirName.getName() && !DependentHint) {
7527 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
7528 Diag(StartLoc, diag::err_omp_critical_with_hint);
Alexey Bataeve3727102018-04-18 15:57:46 +00007529 if (HintLoc.isValid())
Alexey Bataev28c75412015-12-15 08:19:24 +00007530 Diag(HintLoc, diag::note_omp_critical_hint_here)
7531 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00007532 else
Alexey Bataev28c75412015-12-15 08:19:24 +00007533 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
Alexey Bataeve3727102018-04-18 15:57:46 +00007534 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007535 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here)
Alexey Bataev28c75412015-12-15 08:19:24 +00007536 << 1
7537 << C->getHint()->EvaluateKnownConstInt(Context).toString(
7538 /*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00007539 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007540 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1;
Alexey Bataeve3727102018-04-18 15:57:46 +00007541 }
Alexey Bataev28c75412015-12-15 08:19:24 +00007542 }
7543 }
7544
Reid Kleckner87a31802018-03-12 21:43:02 +00007545 setFunctionHasBranchProtectedScope();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00007546
Alexey Bataev28c75412015-12-15 08:19:24 +00007547 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
7548 Clauses, AStmt);
7549 if (!Pair.first && DirName.getName() && !DependentHint)
7550 DSAStack->addCriticalWithHint(Dir, Hint);
7551 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00007552}
7553
Alexey Bataev4acb8592014-07-07 13:01:15 +00007554StmtResult Sema::ActOnOpenMPParallelForDirective(
7555 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007556 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007557 if (!AStmt)
7558 return StmtError();
7559
Alexey Bataeve3727102018-04-18 15:57:46 +00007560 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev4acb8592014-07-07 13:01:15 +00007561 // 1.2.2 OpenMP Language Terminology
7562 // Structured block - An executable statement with a single entry at the
7563 // top and a single exit at the bottom.
7564 // The point of exit cannot be a branch out of the structured block.
7565 // longjmp() and throw() must not violate the entry/exit criteria.
7566 CS->getCapturedDecl()->setNothrow();
7567
Alexander Musmanc6388682014-12-15 07:07:06 +00007568 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007569 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7570 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00007571 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007572 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00007573 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7574 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00007575 if (NestedLoopCount == 0)
7576 return StmtError();
7577
Alexander Musmana5f070a2014-10-01 06:03:56 +00007578 assert((CurContext->isDependentContext() || B.builtAll()) &&
7579 "omp parallel for loop exprs were not built");
7580
Alexey Bataev54acd402015-08-04 11:18:19 +00007581 if (!CurContext->isDependentContext()) {
7582 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007583 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007584 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00007585 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007586 B.NumIterations, *this, CurScope,
7587 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00007588 return StmtError();
7589 }
7590 }
7591
Reid Kleckner87a31802018-03-12 21:43:02 +00007592 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00007593 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00007594 NestedLoopCount, Clauses, AStmt, B,
7595 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00007596}
7597
Alexander Musmane4e893b2014-09-23 09:33:00 +00007598StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
7599 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007600 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007601 if (!AStmt)
7602 return StmtError();
7603
Alexey Bataeve3727102018-04-18 15:57:46 +00007604 auto *CS = cast<CapturedStmt>(AStmt);
Alexander Musmane4e893b2014-09-23 09:33:00 +00007605 // 1.2.2 OpenMP Language Terminology
7606 // Structured block - An executable statement with a single entry at the
7607 // top and a single exit at the bottom.
7608 // The point of exit cannot be a branch out of the structured block.
7609 // longjmp() and throw() must not violate the entry/exit criteria.
7610 CS->getCapturedDecl()->setNothrow();
7611
Alexander Musmanc6388682014-12-15 07:07:06 +00007612 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007613 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7614 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00007615 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007616 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00007617 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7618 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00007619 if (NestedLoopCount == 0)
7620 return StmtError();
7621
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00007622 if (!CurContext->isDependentContext()) {
7623 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007624 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007625 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00007626 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007627 B.NumIterations, *this, CurScope,
7628 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00007629 return StmtError();
7630 }
7631 }
7632
Kelvin Lic5609492016-07-15 04:39:07 +00007633 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00007634 return StmtError();
7635
Reid Kleckner87a31802018-03-12 21:43:02 +00007636 setFunctionHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00007637 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00007638 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00007639}
7640
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00007641StmtResult
7642Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
7643 Stmt *AStmt, SourceLocation StartLoc,
7644 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007645 if (!AStmt)
7646 return StmtError();
7647
7648 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00007649 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00007650 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00007651 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00007652 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00007653 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00007654 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00007655 return StmtError();
7656 // All associated statements must be '#pragma omp section' except for
7657 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00007658 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00007659 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
7660 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007661 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00007662 diag::err_omp_parallel_sections_substmt_not_section);
7663 return StmtError();
7664 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00007665 cast<OMPSectionDirective>(SectionStmt)
7666 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00007667 }
7668 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007669 Diag(AStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00007670 diag::err_omp_parallel_sections_not_compound_stmt);
7671 return StmtError();
7672 }
7673
Reid Kleckner87a31802018-03-12 21:43:02 +00007674 setFunctionHasBranchProtectedScope();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00007675
Alexey Bataev25e5b442015-09-15 12:52:43 +00007676 return OMPParallelSectionsDirective::Create(
7677 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00007678}
7679
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007680StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
7681 Stmt *AStmt, SourceLocation StartLoc,
7682 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007683 if (!AStmt)
7684 return StmtError();
7685
David Majnemer9d168222016-08-05 17:44:54 +00007686 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007687 // 1.2.2 OpenMP Language Terminology
7688 // Structured block - An executable statement with a single entry at the
7689 // top and a single exit at the bottom.
7690 // The point of exit cannot be a branch out of the structured block.
7691 // longjmp() and throw() must not violate the entry/exit criteria.
7692 CS->getCapturedDecl()->setNothrow();
7693
Reid Kleckner87a31802018-03-12 21:43:02 +00007694 setFunctionHasBranchProtectedScope();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007695
Alexey Bataev25e5b442015-09-15 12:52:43 +00007696 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
7697 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007698}
7699
Alexey Bataev68446b72014-07-18 07:47:19 +00007700StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
7701 SourceLocation EndLoc) {
7702 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
7703}
7704
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00007705StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
7706 SourceLocation EndLoc) {
7707 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
7708}
7709
Alexey Bataev2df347a2014-07-18 10:17:07 +00007710StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
7711 SourceLocation EndLoc) {
7712 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
7713}
7714
Alexey Bataev169d96a2017-07-18 20:17:46 +00007715StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
7716 Stmt *AStmt,
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00007717 SourceLocation StartLoc,
7718 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007719 if (!AStmt)
7720 return StmtError();
7721
7722 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00007723
Reid Kleckner87a31802018-03-12 21:43:02 +00007724 setFunctionHasBranchProtectedScope();
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00007725
Alexey Bataev169d96a2017-07-18 20:17:46 +00007726 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
Alexey Bataev3b1b8952017-07-25 15:53:26 +00007727 AStmt,
7728 DSAStack->getTaskgroupReductionRef());
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00007729}
7730
Alexey Bataev6125da92014-07-21 11:26:11 +00007731StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
7732 SourceLocation StartLoc,
7733 SourceLocation EndLoc) {
7734 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
7735 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
7736}
7737
Alexey Bataev346265e2015-09-25 10:37:12 +00007738StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
7739 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00007740 SourceLocation StartLoc,
7741 SourceLocation EndLoc) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007742 const OMPClause *DependFound = nullptr;
7743 const OMPClause *DependSourceClause = nullptr;
7744 const OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00007745 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00007746 const OMPThreadsClause *TC = nullptr;
7747 const OMPSIMDClause *SC = nullptr;
7748 for (const OMPClause *C : Clauses) {
Alexey Bataeveb482352015-12-18 05:05:56 +00007749 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
7750 DependFound = C;
7751 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
7752 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007753 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataeveb482352015-12-18 05:05:56 +00007754 << getOpenMPDirectiveName(OMPD_ordered)
7755 << getOpenMPClauseName(OMPC_depend) << 2;
7756 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00007757 } else {
Alexey Bataeveb482352015-12-18 05:05:56 +00007758 DependSourceClause = C;
Alexey Bataeve3727102018-04-18 15:57:46 +00007759 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007760 if (DependSinkClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007761 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007762 << 0;
7763 ErrorFound = true;
7764 }
7765 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
7766 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007767 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007768 << 1;
7769 ErrorFound = true;
7770 }
7771 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00007772 }
Alexey Bataeve3727102018-04-18 15:57:46 +00007773 } else if (C->getClauseKind() == OMPC_threads) {
Alexey Bataev346265e2015-09-25 10:37:12 +00007774 TC = cast<OMPThreadsClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00007775 } else if (C->getClauseKind() == OMPC_simd) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007776 SC = cast<OMPSIMDClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00007777 }
Alexey Bataev346265e2015-09-25 10:37:12 +00007778 }
Alexey Bataeveb482352015-12-18 05:05:56 +00007779 if (!ErrorFound && !SC &&
7780 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007781 // OpenMP [2.8.1,simd Construct, Restrictions]
7782 // An ordered construct with the simd clause is the only OpenMP construct
7783 // that can appear in the simd region.
7784 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00007785 ErrorFound = true;
7786 } else if (DependFound && (TC || SC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007787 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd)
Alexey Bataeveb482352015-12-18 05:05:56 +00007788 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
7789 ErrorFound = true;
Alexey Bataevf138fda2018-08-13 19:04:24 +00007790 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007791 Diag(DependFound->getBeginLoc(),
Alexey Bataeveb482352015-12-18 05:05:56 +00007792 diag::err_omp_ordered_directive_without_param);
7793 ErrorFound = true;
7794 } else if (TC || Clauses.empty()) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00007795 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007796 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
Alexey Bataeveb482352015-12-18 05:05:56 +00007797 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
7798 << (TC != nullptr);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007799 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param);
Alexey Bataeveb482352015-12-18 05:05:56 +00007800 ErrorFound = true;
7801 }
7802 }
7803 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007804 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00007805
7806 if (AStmt) {
7807 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7808
Reid Kleckner87a31802018-03-12 21:43:02 +00007809 setFunctionHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007810 }
Alexey Bataev346265e2015-09-25 10:37:12 +00007811
7812 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00007813}
7814
Alexey Bataev1d160b12015-03-13 12:27:31 +00007815namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007816/// Helper class for checking expression in 'omp atomic [update]'
Alexey Bataev1d160b12015-03-13 12:27:31 +00007817/// construct.
7818class OpenMPAtomicUpdateChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007819 /// Error results for atomic update expressions.
Alexey Bataev1d160b12015-03-13 12:27:31 +00007820 enum ExprAnalysisErrorCode {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007821 /// A statement is not an expression statement.
Alexey Bataev1d160b12015-03-13 12:27:31 +00007822 NotAnExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007823 /// Expression is not builtin binary or unary operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00007824 NotABinaryOrUnaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007825 /// Unary operation is not post-/pre- increment/decrement operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00007826 NotAnUnaryIncDecExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007827 /// An expression is not of scalar type.
Alexey Bataev1d160b12015-03-13 12:27:31 +00007828 NotAScalarType,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007829 /// A binary operation is not an assignment operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00007830 NotAnAssignmentOp,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007831 /// RHS part of the binary operation is not a binary expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00007832 NotABinaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007833 /// RHS part is not additive/multiplicative/shift/biwise binary
Alexey Bataev1d160b12015-03-13 12:27:31 +00007834 /// expression.
7835 NotABinaryOperator,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007836 /// RHS binary operation does not have reference to the updated LHS
Alexey Bataev1d160b12015-03-13 12:27:31 +00007837 /// part.
7838 NotAnUpdateExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007839 /// No errors is found.
Alexey Bataev1d160b12015-03-13 12:27:31 +00007840 NoError
7841 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007842 /// Reference to Sema.
Alexey Bataev1d160b12015-03-13 12:27:31 +00007843 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007844 /// A location for note diagnostics (when error is found).
Alexey Bataev1d160b12015-03-13 12:27:31 +00007845 SourceLocation NoteLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007846 /// 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00007847 Expr *X;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007848 /// 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00007849 Expr *E;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007850 /// Helper expression of the form
Alexey Bataevb4505a72015-03-30 05:20:59 +00007851 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
7852 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
7853 Expr *UpdateExpr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007854 /// Is 'x' a LHS in a RHS part of full update expression. It is
Alexey Bataevb4505a72015-03-30 05:20:59 +00007855 /// important for non-associative operations.
7856 bool IsXLHSInRHSPart;
7857 BinaryOperatorKind Op;
7858 SourceLocation OpLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007859 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00007860 /// if it is a prefix unary operation.
7861 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00007862
7863public:
7864 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00007865 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00007866 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007867 /// Check specified statement that it is suitable for 'atomic update'
Alexey Bataev1d160b12015-03-13 12:27:31 +00007868 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00007869 /// expression. If DiagId and NoteId == 0, then only check is performed
7870 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00007871 /// \param DiagId Diagnostic which should be emitted if error is found.
7872 /// \param NoteId Diagnostic note for the main error message.
7873 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00007874 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007875 /// Return the 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00007876 Expr *getX() const { return X; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007877 /// Return the 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00007878 Expr *getExpr() const { return E; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007879 /// Return the update expression used in calculation of the updated
Alexey Bataevb4505a72015-03-30 05:20:59 +00007880 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
7881 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
7882 Expr *getUpdateExpr() const { return UpdateExpr; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007883 /// Return true if 'x' is LHS in RHS part of full update expression,
Alexey Bataevb4505a72015-03-30 05:20:59 +00007884 /// false otherwise.
7885 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
7886
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007887 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00007888 /// if it is a prefix unary operation.
7889 bool isPostfixUpdate() const { return IsPostfixUpdate; }
7890
Alexey Bataev1d160b12015-03-13 12:27:31 +00007891private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00007892 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
7893 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00007894};
7895} // namespace
7896
7897bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
7898 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
7899 ExprAnalysisErrorCode ErrorFound = NoError;
7900 SourceLocation ErrorLoc, NoteLoc;
7901 SourceRange ErrorRange, NoteRange;
7902 // Allowed constructs are:
7903 // x = x binop expr;
7904 // x = expr binop x;
7905 if (AtomicBinOp->getOpcode() == BO_Assign) {
7906 X = AtomicBinOp->getLHS();
Alexey Bataeve3727102018-04-18 15:57:46 +00007907 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00007908 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
7909 if (AtomicInnerBinOp->isMultiplicativeOp() ||
7910 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
7911 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00007912 Op = AtomicInnerBinOp->getOpcode();
7913 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +00007914 Expr *LHS = AtomicInnerBinOp->getLHS();
7915 Expr *RHS = AtomicInnerBinOp->getRHS();
Alexey Bataev1d160b12015-03-13 12:27:31 +00007916 llvm::FoldingSetNodeID XId, LHSId, RHSId;
7917 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
7918 /*Canonical=*/true);
7919 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
7920 /*Canonical=*/true);
7921 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
7922 /*Canonical=*/true);
7923 if (XId == LHSId) {
7924 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00007925 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00007926 } else if (XId == RHSId) {
7927 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00007928 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00007929 } else {
7930 ErrorLoc = AtomicInnerBinOp->getExprLoc();
7931 ErrorRange = AtomicInnerBinOp->getSourceRange();
7932 NoteLoc = X->getExprLoc();
7933 NoteRange = X->getSourceRange();
7934 ErrorFound = NotAnUpdateExpression;
7935 }
7936 } else {
7937 ErrorLoc = AtomicInnerBinOp->getExprLoc();
7938 ErrorRange = AtomicInnerBinOp->getSourceRange();
7939 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
7940 NoteRange = SourceRange(NoteLoc, NoteLoc);
7941 ErrorFound = NotABinaryOperator;
7942 }
7943 } else {
7944 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
7945 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
7946 ErrorFound = NotABinaryExpression;
7947 }
7948 } else {
7949 ErrorLoc = AtomicBinOp->getExprLoc();
7950 ErrorRange = AtomicBinOp->getSourceRange();
7951 NoteLoc = AtomicBinOp->getOperatorLoc();
7952 NoteRange = SourceRange(NoteLoc, NoteLoc);
7953 ErrorFound = NotAnAssignmentOp;
7954 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00007955 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00007956 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
7957 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
7958 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00007959 }
7960 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00007961 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00007962 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00007963}
7964
7965bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
7966 unsigned NoteId) {
7967 ExprAnalysisErrorCode ErrorFound = NoError;
7968 SourceLocation ErrorLoc, NoteLoc;
7969 SourceRange ErrorRange, NoteRange;
7970 // Allowed constructs are:
7971 // x++;
7972 // x--;
7973 // ++x;
7974 // --x;
7975 // x binop= expr;
7976 // x = x binop expr;
7977 // x = expr binop x;
7978 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
7979 AtomicBody = AtomicBody->IgnoreParenImpCasts();
7980 if (AtomicBody->getType()->isScalarType() ||
7981 AtomicBody->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007982 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00007983 AtomicBody->IgnoreParenImpCasts())) {
7984 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00007985 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00007986 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00007987 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00007988 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00007989 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00007990 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00007991 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
7992 AtomicBody->IgnoreParenImpCasts())) {
7993 // Check for Binary Operation
David Majnemer9d168222016-08-05 17:44:54 +00007994 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
Alexey Bataevb4505a72015-03-30 05:20:59 +00007995 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00007996 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
David Majnemer9d168222016-08-05 17:44:54 +00007997 AtomicBody->IgnoreParenImpCasts())) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00007998 // Check for Unary Operation
7999 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00008000 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00008001 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
8002 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00008003 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00008004 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
8005 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00008006 } else {
8007 ErrorFound = NotAnUnaryIncDecExpression;
8008 ErrorLoc = AtomicUnaryOp->getExprLoc();
8009 ErrorRange = AtomicUnaryOp->getSourceRange();
8010 NoteLoc = AtomicUnaryOp->getOperatorLoc();
8011 NoteRange = SourceRange(NoteLoc, NoteLoc);
8012 }
Alexey Bataev5a195472015-09-04 12:55:50 +00008013 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00008014 ErrorFound = NotABinaryOrUnaryExpression;
8015 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
8016 NoteRange = ErrorRange = AtomicBody->getSourceRange();
8017 }
8018 } else {
8019 ErrorFound = NotAScalarType;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008020 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00008021 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
8022 }
8023 } else {
8024 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008025 NoteLoc = ErrorLoc = S->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00008026 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
8027 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00008028 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00008029 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
8030 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
8031 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00008032 }
8033 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00008034 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00008035 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00008036 // Build an update expression of form 'OpaqueValueExpr(x) binop
8037 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
8038 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
8039 auto *OVEX = new (SemaRef.getASTContext())
8040 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
8041 auto *OVEExpr = new (SemaRef.getASTContext())
8042 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
Alexey Bataeve3727102018-04-18 15:57:46 +00008043 ExprResult Update =
Alexey Bataevb4505a72015-03-30 05:20:59 +00008044 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
8045 IsXLHSInRHSPart ? OVEExpr : OVEX);
8046 if (Update.isInvalid())
8047 return true;
8048 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
8049 Sema::AA_Casting);
8050 if (Update.isInvalid())
8051 return true;
8052 UpdateExpr = Update.get();
8053 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00008054 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00008055}
8056
Alexey Bataev0162e452014-07-22 10:10:35 +00008057StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
8058 Stmt *AStmt,
8059 SourceLocation StartLoc,
8060 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008061 if (!AStmt)
8062 return StmtError();
8063
David Majnemer9d168222016-08-05 17:44:54 +00008064 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00008065 // 1.2.2 OpenMP Language Terminology
8066 // Structured block - An executable statement with a single entry at the
8067 // top and a single exit at the bottom.
8068 // The point of exit cannot be a branch out of the structured block.
8069 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00008070 OpenMPClauseKind AtomicKind = OMPC_unknown;
8071 SourceLocation AtomicKindLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00008072 for (const OMPClause *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00008073 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00008074 C->getClauseKind() == OMPC_update ||
8075 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00008076 if (AtomicKind != OMPC_unknown) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008077 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00008078 << SourceRange(C->getBeginLoc(), C->getEndLoc());
Alexey Bataevdea47612014-07-23 07:46:59 +00008079 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
8080 << getOpenMPClauseName(AtomicKind);
8081 } else {
8082 AtomicKind = C->getClauseKind();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008083 AtomicKindLoc = C->getBeginLoc();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008084 }
8085 }
8086 }
Alexey Bataev62cec442014-11-18 10:14:22 +00008087
Alexey Bataeve3727102018-04-18 15:57:46 +00008088 Stmt *Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00008089 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
8090 Body = EWC->getSubExpr();
8091
Alexey Bataev62cec442014-11-18 10:14:22 +00008092 Expr *X = nullptr;
8093 Expr *V = nullptr;
8094 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00008095 Expr *UE = nullptr;
8096 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00008097 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00008098 // OpenMP [2.12.6, atomic Construct]
8099 // In the next expressions:
8100 // * x and v (as applicable) are both l-value expressions with scalar type.
8101 // * During the execution of an atomic region, multiple syntactic
8102 // occurrences of x must designate the same storage location.
8103 // * Neither of v and expr (as applicable) may access the storage location
8104 // designated by x.
8105 // * Neither of x and expr (as applicable) may access the storage location
8106 // designated by v.
8107 // * expr is an expression with scalar type.
8108 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
8109 // * binop, binop=, ++, and -- are not overloaded operators.
8110 // * The expression x binop expr must be numerically equivalent to x binop
8111 // (expr). This requirement is satisfied if the operators in expr have
8112 // precedence greater than binop, or by using parentheses around expr or
8113 // subexpressions of expr.
8114 // * The expression expr binop x must be numerically equivalent to (expr)
8115 // binop x. This requirement is satisfied if the operators in expr have
8116 // precedence equal to or greater than binop, or by using parentheses around
8117 // expr or subexpressions of expr.
8118 // * For forms that allow multiple occurrences of x, the number of times
8119 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00008120 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00008121 enum {
8122 NotAnExpression,
8123 NotAnAssignmentOp,
8124 NotAScalarType,
8125 NotAnLValue,
8126 NoError
8127 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00008128 SourceLocation ErrorLoc, NoteLoc;
8129 SourceRange ErrorRange, NoteRange;
8130 // If clause is read:
8131 // v = x;
Alexey Bataeve3727102018-04-18 15:57:46 +00008132 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
8133 const auto *AtomicBinOp =
Alexey Bataev62cec442014-11-18 10:14:22 +00008134 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
8135 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
8136 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
8137 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
8138 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
8139 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
8140 if (!X->isLValue() || !V->isLValue()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00008141 const Expr *NotLValueExpr = X->isLValue() ? V : X;
Alexey Bataev62cec442014-11-18 10:14:22 +00008142 ErrorFound = NotAnLValue;
8143 ErrorLoc = AtomicBinOp->getExprLoc();
8144 ErrorRange = AtomicBinOp->getSourceRange();
8145 NoteLoc = NotLValueExpr->getExprLoc();
8146 NoteRange = NotLValueExpr->getSourceRange();
8147 }
8148 } else if (!X->isInstantiationDependent() ||
8149 !V->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00008150 const Expr *NotScalarExpr =
Alexey Bataev62cec442014-11-18 10:14:22 +00008151 (X->isInstantiationDependent() || X->getType()->isScalarType())
8152 ? V
8153 : X;
8154 ErrorFound = NotAScalarType;
8155 ErrorLoc = AtomicBinOp->getExprLoc();
8156 ErrorRange = AtomicBinOp->getSourceRange();
8157 NoteLoc = NotScalarExpr->getExprLoc();
8158 NoteRange = NotScalarExpr->getSourceRange();
8159 }
Alexey Bataev5a195472015-09-04 12:55:50 +00008160 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00008161 ErrorFound = NotAnAssignmentOp;
8162 ErrorLoc = AtomicBody->getExprLoc();
8163 ErrorRange = AtomicBody->getSourceRange();
8164 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
8165 : AtomicBody->getExprLoc();
8166 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
8167 : AtomicBody->getSourceRange();
8168 }
8169 } else {
8170 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008171 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataev62cec442014-11-18 10:14:22 +00008172 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00008173 }
Alexey Bataev62cec442014-11-18 10:14:22 +00008174 if (ErrorFound != NoError) {
8175 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
8176 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00008177 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
8178 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00008179 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00008180 }
8181 if (CurContext->isDependentContext())
Alexey Bataev62cec442014-11-18 10:14:22 +00008182 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00008183 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00008184 enum {
8185 NotAnExpression,
8186 NotAnAssignmentOp,
8187 NotAScalarType,
8188 NotAnLValue,
8189 NoError
8190 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00008191 SourceLocation ErrorLoc, NoteLoc;
8192 SourceRange ErrorRange, NoteRange;
8193 // If clause is write:
8194 // x = expr;
Alexey Bataeve3727102018-04-18 15:57:46 +00008195 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
8196 const auto *AtomicBinOp =
Alexey Bataevf33eba62014-11-28 07:21:40 +00008197 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
8198 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00008199 X = AtomicBinOp->getLHS();
8200 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00008201 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
8202 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
8203 if (!X->isLValue()) {
8204 ErrorFound = NotAnLValue;
8205 ErrorLoc = AtomicBinOp->getExprLoc();
8206 ErrorRange = AtomicBinOp->getSourceRange();
8207 NoteLoc = X->getExprLoc();
8208 NoteRange = X->getSourceRange();
8209 }
8210 } else if (!X->isInstantiationDependent() ||
8211 !E->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00008212 const Expr *NotScalarExpr =
Alexey Bataevf33eba62014-11-28 07:21:40 +00008213 (X->isInstantiationDependent() || X->getType()->isScalarType())
8214 ? E
8215 : X;
8216 ErrorFound = NotAScalarType;
8217 ErrorLoc = AtomicBinOp->getExprLoc();
8218 ErrorRange = AtomicBinOp->getSourceRange();
8219 NoteLoc = NotScalarExpr->getExprLoc();
8220 NoteRange = NotScalarExpr->getSourceRange();
8221 }
Alexey Bataev5a195472015-09-04 12:55:50 +00008222 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00008223 ErrorFound = NotAnAssignmentOp;
8224 ErrorLoc = AtomicBody->getExprLoc();
8225 ErrorRange = AtomicBody->getSourceRange();
8226 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
8227 : AtomicBody->getExprLoc();
8228 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
8229 : AtomicBody->getSourceRange();
8230 }
8231 } else {
8232 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008233 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevf33eba62014-11-28 07:21:40 +00008234 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00008235 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00008236 if (ErrorFound != NoError) {
8237 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
8238 << ErrorRange;
8239 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
8240 << NoteRange;
8241 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00008242 }
8243 if (CurContext->isDependentContext())
Alexey Bataevf33eba62014-11-28 07:21:40 +00008244 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00008245 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00008246 // If clause is update:
8247 // x++;
8248 // x--;
8249 // ++x;
8250 // --x;
8251 // x binop= expr;
8252 // x = x binop expr;
8253 // x = expr binop x;
8254 OpenMPAtomicUpdateChecker Checker(*this);
8255 if (Checker.checkStatement(
8256 Body, (AtomicKind == OMPC_update)
8257 ? diag::err_omp_atomic_update_not_expression_statement
8258 : diag::err_omp_atomic_not_expression_statement,
8259 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00008260 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00008261 if (!CurContext->isDependentContext()) {
8262 E = Checker.getExpr();
8263 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00008264 UE = Checker.getUpdateExpr();
8265 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00008266 }
Alexey Bataev459dec02014-07-24 06:46:57 +00008267 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00008268 enum {
8269 NotAnAssignmentOp,
8270 NotACompoundStatement,
8271 NotTwoSubstatements,
8272 NotASpecificExpression,
8273 NoError
8274 } ErrorFound = NoError;
8275 SourceLocation ErrorLoc, NoteLoc;
8276 SourceRange ErrorRange, NoteRange;
Alexey Bataeve3727102018-04-18 15:57:46 +00008277 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00008278 // If clause is a capture:
8279 // v = x++;
8280 // v = x--;
8281 // v = ++x;
8282 // v = --x;
8283 // v = x binop= expr;
8284 // v = x = x binop expr;
8285 // v = x = expr binop x;
Alexey Bataeve3727102018-04-18 15:57:46 +00008286 const auto *AtomicBinOp =
Alexey Bataevb78ca832015-04-01 03:33:17 +00008287 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
8288 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
8289 V = AtomicBinOp->getLHS();
8290 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
8291 OpenMPAtomicUpdateChecker Checker(*this);
8292 if (Checker.checkStatement(
8293 Body, diag::err_omp_atomic_capture_not_expression_statement,
8294 diag::note_omp_atomic_update))
8295 return StmtError();
8296 E = Checker.getExpr();
8297 X = Checker.getX();
8298 UE = Checker.getUpdateExpr();
8299 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
8300 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00008301 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00008302 ErrorLoc = AtomicBody->getExprLoc();
8303 ErrorRange = AtomicBody->getSourceRange();
8304 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
8305 : AtomicBody->getExprLoc();
8306 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
8307 : AtomicBody->getSourceRange();
8308 ErrorFound = NotAnAssignmentOp;
8309 }
8310 if (ErrorFound != NoError) {
8311 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
8312 << ErrorRange;
8313 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
8314 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00008315 }
Alexey Bataeve3727102018-04-18 15:57:46 +00008316 if (CurContext->isDependentContext())
8317 UE = V = E = X = nullptr;
Alexey Bataevb78ca832015-04-01 03:33:17 +00008318 } else {
8319 // If clause is a capture:
8320 // { v = x; x = expr; }
8321 // { v = x; x++; }
8322 // { v = x; x--; }
8323 // { v = x; ++x; }
8324 // { v = x; --x; }
8325 // { v = x; x binop= expr; }
8326 // { v = x; x = x binop expr; }
8327 // { v = x; x = expr binop x; }
8328 // { x++; v = x; }
8329 // { x--; v = x; }
8330 // { ++x; v = x; }
8331 // { --x; v = x; }
8332 // { x binop= expr; v = x; }
8333 // { x = x binop expr; v = x; }
8334 // { x = expr binop x; v = x; }
8335 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
8336 // Check that this is { expr1; expr2; }
8337 if (CS->size() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00008338 Stmt *First = CS->body_front();
8339 Stmt *Second = CS->body_back();
Alexey Bataevb78ca832015-04-01 03:33:17 +00008340 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
8341 First = EWC->getSubExpr()->IgnoreParenImpCasts();
8342 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
8343 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
8344 // Need to find what subexpression is 'v' and what is 'x'.
8345 OpenMPAtomicUpdateChecker Checker(*this);
8346 bool IsUpdateExprFound = !Checker.checkStatement(Second);
8347 BinaryOperator *BinOp = nullptr;
8348 if (IsUpdateExprFound) {
8349 BinOp = dyn_cast<BinaryOperator>(First);
8350 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
8351 }
8352 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
8353 // { v = x; x++; }
8354 // { v = x; x--; }
8355 // { v = x; ++x; }
8356 // { v = x; --x; }
8357 // { v = x; x binop= expr; }
8358 // { v = x; x = x binop expr; }
8359 // { v = x; x = expr binop x; }
8360 // Check that the first expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00008361 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00008362 llvm::FoldingSetNodeID XId, PossibleXId;
8363 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
8364 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
8365 IsUpdateExprFound = XId == PossibleXId;
8366 if (IsUpdateExprFound) {
8367 V = BinOp->getLHS();
8368 X = Checker.getX();
8369 E = Checker.getExpr();
8370 UE = Checker.getUpdateExpr();
8371 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00008372 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00008373 }
8374 }
8375 if (!IsUpdateExprFound) {
8376 IsUpdateExprFound = !Checker.checkStatement(First);
8377 BinOp = nullptr;
8378 if (IsUpdateExprFound) {
8379 BinOp = dyn_cast<BinaryOperator>(Second);
8380 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
8381 }
8382 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
8383 // { x++; v = x; }
8384 // { x--; v = x; }
8385 // { ++x; v = x; }
8386 // { --x; v = x; }
8387 // { x binop= expr; v = x; }
8388 // { x = x binop expr; v = x; }
8389 // { x = expr binop x; v = x; }
8390 // Check that the second expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00008391 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00008392 llvm::FoldingSetNodeID XId, PossibleXId;
8393 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
8394 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
8395 IsUpdateExprFound = XId == PossibleXId;
8396 if (IsUpdateExprFound) {
8397 V = BinOp->getLHS();
8398 X = Checker.getX();
8399 E = Checker.getExpr();
8400 UE = Checker.getUpdateExpr();
8401 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00008402 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00008403 }
8404 }
8405 }
8406 if (!IsUpdateExprFound) {
8407 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00008408 auto *FirstExpr = dyn_cast<Expr>(First);
8409 auto *SecondExpr = dyn_cast<Expr>(Second);
8410 if (!FirstExpr || !SecondExpr ||
8411 !(FirstExpr->isInstantiationDependent() ||
8412 SecondExpr->isInstantiationDependent())) {
8413 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
8414 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00008415 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00008416 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008417 : First->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00008418 NoteRange = ErrorRange = FirstBinOp
8419 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00008420 : SourceRange(ErrorLoc, ErrorLoc);
8421 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00008422 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
8423 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
8424 ErrorFound = NotAnAssignmentOp;
8425 NoteLoc = ErrorLoc = SecondBinOp
8426 ? SecondBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008427 : Second->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00008428 NoteRange = ErrorRange =
8429 SecondBinOp ? SecondBinOp->getSourceRange()
8430 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00008431 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00008432 Expr *PossibleXRHSInFirst =
Alexey Bataev5a195472015-09-04 12:55:50 +00008433 FirstBinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00008434 Expr *PossibleXLHSInSecond =
Alexey Bataev5a195472015-09-04 12:55:50 +00008435 SecondBinOp->getLHS()->IgnoreParenImpCasts();
8436 llvm::FoldingSetNodeID X1Id, X2Id;
8437 PossibleXRHSInFirst->Profile(X1Id, Context,
8438 /*Canonical=*/true);
8439 PossibleXLHSInSecond->Profile(X2Id, Context,
8440 /*Canonical=*/true);
8441 IsUpdateExprFound = X1Id == X2Id;
8442 if (IsUpdateExprFound) {
8443 V = FirstBinOp->getLHS();
8444 X = SecondBinOp->getLHS();
8445 E = SecondBinOp->getRHS();
8446 UE = nullptr;
8447 IsXLHSInRHSPart = false;
8448 IsPostfixUpdate = true;
8449 } else {
8450 ErrorFound = NotASpecificExpression;
8451 ErrorLoc = FirstBinOp->getExprLoc();
8452 ErrorRange = FirstBinOp->getSourceRange();
8453 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
8454 NoteRange = SecondBinOp->getRHS()->getSourceRange();
8455 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00008456 }
8457 }
8458 }
8459 }
8460 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008461 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00008462 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008463 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00008464 ErrorFound = NotTwoSubstatements;
8465 }
8466 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008467 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00008468 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008469 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00008470 ErrorFound = NotACompoundStatement;
8471 }
8472 if (ErrorFound != NoError) {
8473 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
8474 << ErrorRange;
8475 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
8476 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00008477 }
Alexey Bataeve3727102018-04-18 15:57:46 +00008478 if (CurContext->isDependentContext())
8479 UE = V = E = X = nullptr;
Alexey Bataev459dec02014-07-24 06:46:57 +00008480 }
Alexey Bataevdea47612014-07-23 07:46:59 +00008481 }
Alexey Bataev0162e452014-07-22 10:10:35 +00008482
Reid Kleckner87a31802018-03-12 21:43:02 +00008483 setFunctionHasBranchProtectedScope();
Alexey Bataev0162e452014-07-22 10:10:35 +00008484
Alexey Bataev62cec442014-11-18 10:14:22 +00008485 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00008486 X, V, E, UE, IsXLHSInRHSPart,
8487 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00008488}
8489
Alexey Bataev0bd520b2014-09-19 08:19:49 +00008490StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
8491 Stmt *AStmt,
8492 SourceLocation StartLoc,
8493 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008494 if (!AStmt)
8495 return StmtError();
8496
Alexey Bataeve3727102018-04-18 15:57:46 +00008497 auto *CS = cast<CapturedStmt>(AStmt);
Samuel Antao4af1b7b2015-12-02 17:44:43 +00008498 // 1.2.2 OpenMP Language Terminology
8499 // Structured block - An executable statement with a single entry at the
8500 // top and a single exit at the bottom.
8501 // The point of exit cannot be a branch out of the structured block.
8502 // longjmp() and throw() must not violate the entry/exit criteria.
8503 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00008504 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
8505 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8506 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8507 // 1.2.2 OpenMP Language Terminology
8508 // Structured block - An executable statement with a single entry at the
8509 // top and a single exit at the bottom.
8510 // The point of exit cannot be a branch out of the structured block.
8511 // longjmp() and throw() must not violate the entry/exit criteria.
8512 CS->getCapturedDecl()->setNothrow();
8513 }
Alexey Bataev0bd520b2014-09-19 08:19:49 +00008514
Alexey Bataev13314bf2014-10-09 04:18:56 +00008515 // OpenMP [2.16, Nesting of Regions]
8516 // If specified, a teams construct must be contained within a target
8517 // construct. That target construct must contain no statements or directives
8518 // outside of the teams construct.
8519 if (DSAStack->hasInnerTeamsRegion()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00008520 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
Alexey Bataev13314bf2014-10-09 04:18:56 +00008521 bool OMPTeamsFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00008522 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00008523 auto I = CS->body_begin();
8524 while (I != CS->body_end()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00008525 const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
Kelvin Li620ba602019-02-05 16:43:00 +00008526 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) ||
8527 OMPTeamsFound) {
8528
Alexey Bataev13314bf2014-10-09 04:18:56 +00008529 OMPTeamsFound = false;
8530 break;
8531 }
8532 ++I;
8533 }
8534 assert(I != CS->body_end() && "Not found statement");
8535 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00008536 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00008537 const auto *OED = dyn_cast<OMPExecutableDirective>(S);
Kelvin Li3834dce2016-06-27 19:15:43 +00008538 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00008539 }
8540 if (!OMPTeamsFound) {
8541 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
8542 Diag(DSAStack->getInnerTeamsRegionLoc(),
8543 diag::note_omp_nested_teams_construct_here);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008544 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here)
Alexey Bataev13314bf2014-10-09 04:18:56 +00008545 << isa<OMPExecutableDirective>(S);
8546 return StmtError();
8547 }
8548 }
8549
Reid Kleckner87a31802018-03-12 21:43:02 +00008550 setFunctionHasBranchProtectedScope();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00008551
8552 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
8553}
8554
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00008555StmtResult
8556Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
8557 Stmt *AStmt, SourceLocation StartLoc,
8558 SourceLocation EndLoc) {
8559 if (!AStmt)
8560 return StmtError();
8561
Alexey Bataeve3727102018-04-18 15:57:46 +00008562 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00008563 // 1.2.2 OpenMP Language Terminology
8564 // Structured block - An executable statement with a single entry at the
8565 // top and a single exit at the bottom.
8566 // The point of exit cannot be a branch out of the structured block.
8567 // longjmp() and throw() must not violate the entry/exit criteria.
8568 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00008569 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
8570 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8571 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8572 // 1.2.2 OpenMP Language Terminology
8573 // Structured block - An executable statement with a single entry at the
8574 // top and a single exit at the bottom.
8575 // The point of exit cannot be a branch out of the structured block.
8576 // longjmp() and throw() must not violate the entry/exit criteria.
8577 CS->getCapturedDecl()->setNothrow();
8578 }
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00008579
Reid Kleckner87a31802018-03-12 21:43:02 +00008580 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00008581
8582 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
8583 AStmt);
8584}
8585
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00008586StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
8587 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008588 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00008589 if (!AStmt)
8590 return StmtError();
8591
Alexey Bataeve3727102018-04-18 15:57:46 +00008592 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00008593 // 1.2.2 OpenMP Language Terminology
8594 // Structured block - An executable statement with a single entry at the
8595 // top and a single exit at the bottom.
8596 // The point of exit cannot be a branch out of the structured block.
8597 // longjmp() and throw() must not violate the entry/exit criteria.
8598 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008599 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
8600 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8601 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8602 // 1.2.2 OpenMP Language Terminology
8603 // Structured block - An executable statement with a single entry at the
8604 // top and a single exit at the bottom.
8605 // The point of exit cannot be a branch out of the structured block.
8606 // longjmp() and throw() must not violate the entry/exit criteria.
8607 CS->getCapturedDecl()->setNothrow();
8608 }
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00008609
8610 OMPLoopDirective::HelperExprs B;
8611 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
8612 // define the nested loops number.
8613 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00008614 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008615 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00008616 VarsWithImplicitDSA, B);
8617 if (NestedLoopCount == 0)
8618 return StmtError();
8619
8620 assert((CurContext->isDependentContext() || B.builtAll()) &&
8621 "omp target parallel for loop exprs were not built");
8622
8623 if (!CurContext->isDependentContext()) {
8624 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008625 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00008626 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00008627 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00008628 B.NumIterations, *this, CurScope,
8629 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00008630 return StmtError();
8631 }
8632 }
8633
Reid Kleckner87a31802018-03-12 21:43:02 +00008634 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00008635 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
8636 NestedLoopCount, Clauses, AStmt,
8637 B, DSAStack->isCancelRegion());
8638}
8639
Alexey Bataev95b64a92017-05-30 16:00:04 +00008640/// Check for existence of a map clause in the list of clauses.
8641static bool hasClauses(ArrayRef<OMPClause *> Clauses,
8642 const OpenMPClauseKind K) {
8643 return llvm::any_of(
8644 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
8645}
Samuel Antaodf67fc42016-01-19 19:15:56 +00008646
Alexey Bataev95b64a92017-05-30 16:00:04 +00008647template <typename... Params>
8648static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
8649 const Params... ClauseTypes) {
8650 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
Samuel Antaodf67fc42016-01-19 19:15:56 +00008651}
8652
Michael Wong65f367f2015-07-21 13:44:28 +00008653StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
8654 Stmt *AStmt,
8655 SourceLocation StartLoc,
8656 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008657 if (!AStmt)
8658 return StmtError();
8659
8660 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8661
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00008662 // OpenMP [2.10.1, Restrictions, p. 97]
8663 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00008664 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
8665 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
8666 << "'map' or 'use_device_ptr'"
David Majnemer9d168222016-08-05 17:44:54 +00008667 << getOpenMPDirectiveName(OMPD_target_data);
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00008668 return StmtError();
8669 }
8670
Reid Kleckner87a31802018-03-12 21:43:02 +00008671 setFunctionHasBranchProtectedScope();
Michael Wong65f367f2015-07-21 13:44:28 +00008672
8673 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
8674 AStmt);
8675}
8676
Samuel Antaodf67fc42016-01-19 19:15:56 +00008677StmtResult
8678Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
8679 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00008680 SourceLocation EndLoc, Stmt *AStmt) {
8681 if (!AStmt)
8682 return StmtError();
8683
Alexey Bataeve3727102018-04-18 15:57:46 +00008684 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00008685 // 1.2.2 OpenMP Language Terminology
8686 // Structured block - An executable statement with a single entry at the
8687 // top and a single exit at the bottom.
8688 // The point of exit cannot be a branch out of the structured block.
8689 // longjmp() and throw() must not violate the entry/exit criteria.
8690 CS->getCapturedDecl()->setNothrow();
8691 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
8692 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8693 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8694 // 1.2.2 OpenMP Language Terminology
8695 // Structured block - An executable statement with a single entry at the
8696 // top and a single exit at the bottom.
8697 // The point of exit cannot be a branch out of the structured block.
8698 // longjmp() and throw() must not violate the entry/exit criteria.
8699 CS->getCapturedDecl()->setNothrow();
8700 }
8701
Samuel Antaodf67fc42016-01-19 19:15:56 +00008702 // OpenMP [2.10.2, Restrictions, p. 99]
8703 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00008704 if (!hasClauses(Clauses, OMPC_map)) {
8705 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
8706 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
Samuel Antaodf67fc42016-01-19 19:15:56 +00008707 return StmtError();
8708 }
8709
Alexey Bataev7828b252017-11-21 17:08:48 +00008710 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
8711 AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00008712}
8713
Samuel Antao72590762016-01-19 20:04:50 +00008714StmtResult
8715Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
8716 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00008717 SourceLocation EndLoc, Stmt *AStmt) {
8718 if (!AStmt)
8719 return StmtError();
8720
Alexey Bataeve3727102018-04-18 15:57:46 +00008721 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00008722 // 1.2.2 OpenMP Language Terminology
8723 // Structured block - An executable statement with a single entry at the
8724 // top and a single exit at the bottom.
8725 // The point of exit cannot be a branch out of the structured block.
8726 // longjmp() and throw() must not violate the entry/exit criteria.
8727 CS->getCapturedDecl()->setNothrow();
8728 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
8729 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8730 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8731 // 1.2.2 OpenMP Language Terminology
8732 // Structured block - An executable statement with a single entry at the
8733 // top and a single exit at the bottom.
8734 // The point of exit cannot be a branch out of the structured block.
8735 // longjmp() and throw() must not violate the entry/exit criteria.
8736 CS->getCapturedDecl()->setNothrow();
8737 }
8738
Samuel Antao72590762016-01-19 20:04:50 +00008739 // OpenMP [2.10.3, Restrictions, p. 102]
8740 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00008741 if (!hasClauses(Clauses, OMPC_map)) {
8742 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
8743 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
Samuel Antao72590762016-01-19 20:04:50 +00008744 return StmtError();
8745 }
8746
Alexey Bataev7828b252017-11-21 17:08:48 +00008747 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
8748 AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00008749}
8750
Samuel Antao686c70c2016-05-26 17:30:50 +00008751StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
8752 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00008753 SourceLocation EndLoc,
8754 Stmt *AStmt) {
8755 if (!AStmt)
8756 return StmtError();
8757
Alexey Bataeve3727102018-04-18 15:57:46 +00008758 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00008759 // 1.2.2 OpenMP Language Terminology
8760 // Structured block - An executable statement with a single entry at the
8761 // top and a single exit at the bottom.
8762 // The point of exit cannot be a branch out of the structured block.
8763 // longjmp() and throw() must not violate the entry/exit criteria.
8764 CS->getCapturedDecl()->setNothrow();
8765 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
8766 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8767 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8768 // 1.2.2 OpenMP Language Terminology
8769 // Structured block - An executable statement with a single entry at the
8770 // top and a single exit at the bottom.
8771 // The point of exit cannot be a branch out of the structured block.
8772 // longjmp() and throw() must not violate the entry/exit criteria.
8773 CS->getCapturedDecl()->setNothrow();
8774 }
8775
Alexey Bataev95b64a92017-05-30 16:00:04 +00008776 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
Samuel Antao686c70c2016-05-26 17:30:50 +00008777 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
8778 return StmtError();
8779 }
Alexey Bataev7828b252017-11-21 17:08:48 +00008780 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
8781 AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00008782}
8783
Alexey Bataev13314bf2014-10-09 04:18:56 +00008784StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
8785 Stmt *AStmt, SourceLocation StartLoc,
8786 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008787 if (!AStmt)
8788 return StmtError();
8789
Alexey Bataeve3727102018-04-18 15:57:46 +00008790 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev13314bf2014-10-09 04:18:56 +00008791 // 1.2.2 OpenMP Language Terminology
8792 // Structured block - An executable statement with a single entry at the
8793 // top and a single exit at the bottom.
8794 // The point of exit cannot be a branch out of the structured block.
8795 // longjmp() and throw() must not violate the entry/exit criteria.
8796 CS->getCapturedDecl()->setNothrow();
8797
Reid Kleckner87a31802018-03-12 21:43:02 +00008798 setFunctionHasBranchProtectedScope();
Alexey Bataev13314bf2014-10-09 04:18:56 +00008799
Alexey Bataevceabd412017-11-30 18:01:54 +00008800 DSAStack->setParentTeamsRegionLoc(StartLoc);
8801
Alexey Bataev13314bf2014-10-09 04:18:56 +00008802 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
8803}
8804
Alexey Bataev6d4ed052015-07-01 06:57:41 +00008805StmtResult
8806Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
8807 SourceLocation EndLoc,
8808 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00008809 if (DSAStack->isParentNowaitRegion()) {
8810 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
8811 return StmtError();
8812 }
8813 if (DSAStack->isParentOrderedRegion()) {
8814 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
8815 return StmtError();
8816 }
8817 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
8818 CancelRegion);
8819}
8820
Alexey Bataev87933c72015-09-18 08:07:34 +00008821StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
8822 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00008823 SourceLocation EndLoc,
8824 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev80909872015-07-02 11:25:17 +00008825 if (DSAStack->isParentNowaitRegion()) {
8826 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
8827 return StmtError();
8828 }
8829 if (DSAStack->isParentOrderedRegion()) {
8830 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
8831 return StmtError();
8832 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00008833 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00008834 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
8835 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00008836}
8837
Alexey Bataev382967a2015-12-08 12:06:20 +00008838static bool checkGrainsizeNumTasksClauses(Sema &S,
8839 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00008840 const OMPClause *PrevClause = nullptr;
Alexey Bataev382967a2015-12-08 12:06:20 +00008841 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00008842 for (const OMPClause *C : Clauses) {
Alexey Bataev382967a2015-12-08 12:06:20 +00008843 if (C->getClauseKind() == OMPC_grainsize ||
8844 C->getClauseKind() == OMPC_num_tasks) {
8845 if (!PrevClause)
8846 PrevClause = C;
8847 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008848 S.Diag(C->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00008849 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
8850 << getOpenMPClauseName(C->getClauseKind())
8851 << getOpenMPClauseName(PrevClause->getClauseKind());
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008852 S.Diag(PrevClause->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00008853 diag::note_omp_previous_grainsize_num_tasks)
8854 << getOpenMPClauseName(PrevClause->getClauseKind());
8855 ErrorFound = true;
8856 }
8857 }
8858 }
8859 return ErrorFound;
8860}
8861
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00008862static bool checkReductionClauseWithNogroup(Sema &S,
8863 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00008864 const OMPClause *ReductionClause = nullptr;
8865 const OMPClause *NogroupClause = nullptr;
8866 for (const OMPClause *C : Clauses) {
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00008867 if (C->getClauseKind() == OMPC_reduction) {
8868 ReductionClause = C;
8869 if (NogroupClause)
8870 break;
8871 continue;
8872 }
8873 if (C->getClauseKind() == OMPC_nogroup) {
8874 NogroupClause = C;
8875 if (ReductionClause)
8876 break;
8877 continue;
8878 }
8879 }
8880 if (ReductionClause && NogroupClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008881 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup)
8882 << SourceRange(NogroupClause->getBeginLoc(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00008883 NogroupClause->getEndLoc());
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00008884 return true;
8885 }
8886 return false;
8887}
8888
Alexey Bataev49f6e782015-12-01 04:18:41 +00008889StmtResult Sema::ActOnOpenMPTaskLoopDirective(
8890 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008891 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00008892 if (!AStmt)
8893 return StmtError();
8894
8895 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8896 OMPLoopDirective::HelperExprs B;
8897 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
8898 // define the nested loops number.
8899 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00008900 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00008901 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00008902 VarsWithImplicitDSA, B);
8903 if (NestedLoopCount == 0)
8904 return StmtError();
8905
8906 assert((CurContext->isDependentContext() || B.builtAll()) &&
8907 "omp for loop exprs were not built");
8908
Alexey Bataev382967a2015-12-08 12:06:20 +00008909 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
8910 // The grainsize clause and num_tasks clause are mutually exclusive and may
8911 // not appear on the same taskloop directive.
8912 if (checkGrainsizeNumTasksClauses(*this, Clauses))
8913 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00008914 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
8915 // If a reduction clause is present on the taskloop directive, the nogroup
8916 // clause must not be specified.
8917 if (checkReductionClauseWithNogroup(*this, Clauses))
8918 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00008919
Reid Kleckner87a31802018-03-12 21:43:02 +00008920 setFunctionHasBranchProtectedScope();
Alexey Bataev49f6e782015-12-01 04:18:41 +00008921 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
8922 NestedLoopCount, Clauses, AStmt, B);
8923}
8924
Alexey Bataev0a6ed842015-12-03 09:40:15 +00008925StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
8926 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008927 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00008928 if (!AStmt)
8929 return StmtError();
8930
8931 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8932 OMPLoopDirective::HelperExprs B;
8933 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
8934 // define the nested loops number.
8935 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00008936 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00008937 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
8938 VarsWithImplicitDSA, B);
8939 if (NestedLoopCount == 0)
8940 return StmtError();
8941
8942 assert((CurContext->isDependentContext() || B.builtAll()) &&
8943 "omp for loop exprs were not built");
8944
Alexey Bataev5a3af132016-03-29 08:58:54 +00008945 if (!CurContext->isDependentContext()) {
8946 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008947 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00008948 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev5a3af132016-03-29 08:58:54 +00008949 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00008950 B.NumIterations, *this, CurScope,
8951 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00008952 return StmtError();
8953 }
8954 }
8955
Alexey Bataev382967a2015-12-08 12:06:20 +00008956 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
8957 // The grainsize clause and num_tasks clause are mutually exclusive and may
8958 // not appear on the same taskloop directive.
8959 if (checkGrainsizeNumTasksClauses(*this, Clauses))
8960 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00008961 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
8962 // If a reduction clause is present on the taskloop directive, the nogroup
8963 // clause must not be specified.
8964 if (checkReductionClauseWithNogroup(*this, Clauses))
8965 return StmtError();
Alexey Bataev438388c2017-11-22 18:34:02 +00008966 if (checkSimdlenSafelenSpecified(*this, Clauses))
8967 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00008968
Reid Kleckner87a31802018-03-12 21:43:02 +00008969 setFunctionHasBranchProtectedScope();
Alexey Bataev0a6ed842015-12-03 09:40:15 +00008970 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
8971 NestedLoopCount, Clauses, AStmt, B);
8972}
8973
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008974StmtResult Sema::ActOnOpenMPDistributeDirective(
8975 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008976 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008977 if (!AStmt)
8978 return StmtError();
8979
8980 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8981 OMPLoopDirective::HelperExprs B;
8982 // In presence of clause 'collapse' with number of loops, it will
8983 // define the nested loops number.
8984 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00008985 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008986 nullptr /*ordered not a clause on distribute*/, AStmt,
8987 *this, *DSAStack, VarsWithImplicitDSA, B);
8988 if (NestedLoopCount == 0)
8989 return StmtError();
8990
8991 assert((CurContext->isDependentContext() || B.builtAll()) &&
8992 "omp for loop exprs were not built");
8993
Reid Kleckner87a31802018-03-12 21:43:02 +00008994 setFunctionHasBranchProtectedScope();
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008995 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
8996 NestedLoopCount, Clauses, AStmt, B);
8997}
8998
Carlo Bertolli9925f152016-06-27 14:55:37 +00008999StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
9000 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009001 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli9925f152016-06-27 14:55:37 +00009002 if (!AStmt)
9003 return StmtError();
9004
Alexey Bataeve3727102018-04-18 15:57:46 +00009005 auto *CS = cast<CapturedStmt>(AStmt);
Carlo Bertolli9925f152016-06-27 14:55:37 +00009006 // 1.2.2 OpenMP Language Terminology
9007 // Structured block - An executable statement with a single entry at the
9008 // top and a single exit at the bottom.
9009 // The point of exit cannot be a branch out of the structured block.
9010 // longjmp() and throw() must not violate the entry/exit criteria.
9011 CS->getCapturedDecl()->setNothrow();
Alexey Bataev7f96c372017-11-22 17:19:31 +00009012 for (int ThisCaptureLevel =
9013 getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
9014 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9015 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9016 // 1.2.2 OpenMP Language Terminology
9017 // Structured block - An executable statement with a single entry at the
9018 // top and a single exit at the bottom.
9019 // The point of exit cannot be a branch out of the structured block.
9020 // longjmp() and throw() must not violate the entry/exit criteria.
9021 CS->getCapturedDecl()->setNothrow();
9022 }
Carlo Bertolli9925f152016-06-27 14:55:37 +00009023
9024 OMPLoopDirective::HelperExprs B;
9025 // In presence of clause 'collapse' with number of loops, it will
9026 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00009027 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli9925f152016-06-27 14:55:37 +00009028 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev7f96c372017-11-22 17:19:31 +00009029 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Carlo Bertolli9925f152016-06-27 14:55:37 +00009030 VarsWithImplicitDSA, B);
9031 if (NestedLoopCount == 0)
9032 return StmtError();
9033
9034 assert((CurContext->isDependentContext() || B.builtAll()) &&
9035 "omp for loop exprs were not built");
9036
Reid Kleckner87a31802018-03-12 21:43:02 +00009037 setFunctionHasBranchProtectedScope();
Carlo Bertolli9925f152016-06-27 14:55:37 +00009038 return OMPDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00009039 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
9040 DSAStack->isCancelRegion());
Carlo Bertolli9925f152016-06-27 14:55:37 +00009041}
9042
Kelvin Li4a39add2016-07-05 05:00:15 +00009043StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
9044 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009045 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4a39add2016-07-05 05:00:15 +00009046 if (!AStmt)
9047 return StmtError();
9048
Alexey Bataeve3727102018-04-18 15:57:46 +00009049 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4a39add2016-07-05 05:00:15 +00009050 // 1.2.2 OpenMP Language Terminology
9051 // Structured block - An executable statement with a single entry at the
9052 // top and a single exit at the bottom.
9053 // The point of exit cannot be a branch out of the structured block.
9054 // longjmp() and throw() must not violate the entry/exit criteria.
9055 CS->getCapturedDecl()->setNothrow();
Alexey Bataev974acd62017-11-27 19:38:52 +00009056 for (int ThisCaptureLevel =
9057 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
9058 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9059 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9060 // 1.2.2 OpenMP Language Terminology
9061 // Structured block - An executable statement with a single entry at the
9062 // top and a single exit at the bottom.
9063 // The point of exit cannot be a branch out of the structured block.
9064 // longjmp() and throw() must not violate the entry/exit criteria.
9065 CS->getCapturedDecl()->setNothrow();
9066 }
Kelvin Li4a39add2016-07-05 05:00:15 +00009067
9068 OMPLoopDirective::HelperExprs B;
9069 // In presence of clause 'collapse' with number of loops, it will
9070 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00009071 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li4a39add2016-07-05 05:00:15 +00009072 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev974acd62017-11-27 19:38:52 +00009073 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li4a39add2016-07-05 05:00:15 +00009074 VarsWithImplicitDSA, B);
9075 if (NestedLoopCount == 0)
9076 return StmtError();
9077
9078 assert((CurContext->isDependentContext() || B.builtAll()) &&
9079 "omp for loop exprs were not built");
9080
Alexey Bataev438388c2017-11-22 18:34:02 +00009081 if (!CurContext->isDependentContext()) {
9082 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00009083 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00009084 if (auto *LC = dyn_cast<OMPLinearClause>(C))
9085 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9086 B.NumIterations, *this, CurScope,
9087 DSAStack))
9088 return StmtError();
9089 }
9090 }
9091
Kelvin Lic5609492016-07-15 04:39:07 +00009092 if (checkSimdlenSafelenSpecified(*this, Clauses))
9093 return StmtError();
9094
Reid Kleckner87a31802018-03-12 21:43:02 +00009095 setFunctionHasBranchProtectedScope();
Kelvin Li4a39add2016-07-05 05:00:15 +00009096 return OMPDistributeParallelForSimdDirective::Create(
9097 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9098}
9099
Kelvin Li787f3fc2016-07-06 04:45:38 +00009100StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
9101 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009102 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li787f3fc2016-07-06 04:45:38 +00009103 if (!AStmt)
9104 return StmtError();
9105
Alexey Bataeve3727102018-04-18 15:57:46 +00009106 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li787f3fc2016-07-06 04:45:38 +00009107 // 1.2.2 OpenMP Language Terminology
9108 // Structured block - An executable statement with a single entry at the
9109 // top and a single exit at the bottom.
9110 // The point of exit cannot be a branch out of the structured block.
9111 // longjmp() and throw() must not violate the entry/exit criteria.
9112 CS->getCapturedDecl()->setNothrow();
Alexey Bataev617db5f2017-12-04 15:38:33 +00009113 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
9114 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9115 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9116 // 1.2.2 OpenMP Language Terminology
9117 // Structured block - An executable statement with a single entry at the
9118 // top and a single exit at the bottom.
9119 // The point of exit cannot be a branch out of the structured block.
9120 // longjmp() and throw() must not violate the entry/exit criteria.
9121 CS->getCapturedDecl()->setNothrow();
9122 }
Kelvin Li787f3fc2016-07-06 04:45:38 +00009123
9124 OMPLoopDirective::HelperExprs B;
9125 // In presence of clause 'collapse' with number of loops, it will
9126 // define the nested loops number.
9127 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00009128 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev617db5f2017-12-04 15:38:33 +00009129 nullptr /*ordered not a clause on distribute*/, CS, *this,
9130 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li787f3fc2016-07-06 04:45:38 +00009131 if (NestedLoopCount == 0)
9132 return StmtError();
9133
9134 assert((CurContext->isDependentContext() || B.builtAll()) &&
9135 "omp for loop exprs were not built");
9136
Alexey Bataev438388c2017-11-22 18:34:02 +00009137 if (!CurContext->isDependentContext()) {
9138 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00009139 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00009140 if (auto *LC = dyn_cast<OMPLinearClause>(C))
9141 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9142 B.NumIterations, *this, CurScope,
9143 DSAStack))
9144 return StmtError();
9145 }
9146 }
9147
Kelvin Lic5609492016-07-15 04:39:07 +00009148 if (checkSimdlenSafelenSpecified(*this, Clauses))
9149 return StmtError();
9150
Reid Kleckner87a31802018-03-12 21:43:02 +00009151 setFunctionHasBranchProtectedScope();
Kelvin Li787f3fc2016-07-06 04:45:38 +00009152 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
9153 NestedLoopCount, Clauses, AStmt, B);
9154}
9155
Kelvin Lia579b912016-07-14 02:54:56 +00009156StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
9157 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009158 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lia579b912016-07-14 02:54:56 +00009159 if (!AStmt)
9160 return StmtError();
9161
Alexey Bataeve3727102018-04-18 15:57:46 +00009162 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Lia579b912016-07-14 02:54:56 +00009163 // 1.2.2 OpenMP Language Terminology
9164 // Structured block - An executable statement with a single entry at the
9165 // top and a single exit at the bottom.
9166 // The point of exit cannot be a branch out of the structured block.
9167 // longjmp() and throw() must not violate the entry/exit criteria.
9168 CS->getCapturedDecl()->setNothrow();
Alexey Bataev5d7edca2017-11-09 17:32:15 +00009169 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
9170 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9171 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9172 // 1.2.2 OpenMP Language Terminology
9173 // Structured block - An executable statement with a single entry at the
9174 // top and a single exit at the bottom.
9175 // The point of exit cannot be a branch out of the structured block.
9176 // longjmp() and throw() must not violate the entry/exit criteria.
9177 CS->getCapturedDecl()->setNothrow();
9178 }
Kelvin Lia579b912016-07-14 02:54:56 +00009179
9180 OMPLoopDirective::HelperExprs B;
9181 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
9182 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00009183 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lia579b912016-07-14 02:54:56 +00009184 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev5d7edca2017-11-09 17:32:15 +00009185 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Lia579b912016-07-14 02:54:56 +00009186 VarsWithImplicitDSA, B);
9187 if (NestedLoopCount == 0)
9188 return StmtError();
9189
9190 assert((CurContext->isDependentContext() || B.builtAll()) &&
9191 "omp target parallel for simd loop exprs were not built");
9192
9193 if (!CurContext->isDependentContext()) {
9194 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00009195 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00009196 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Lia579b912016-07-14 02:54:56 +00009197 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9198 B.NumIterations, *this, CurScope,
9199 DSAStack))
9200 return StmtError();
9201 }
9202 }
Kelvin Lic5609492016-07-15 04:39:07 +00009203 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00009204 return StmtError();
9205
Reid Kleckner87a31802018-03-12 21:43:02 +00009206 setFunctionHasBranchProtectedScope();
Kelvin Lia579b912016-07-14 02:54:56 +00009207 return OMPTargetParallelForSimdDirective::Create(
9208 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9209}
9210
Kelvin Li986330c2016-07-20 22:57:10 +00009211StmtResult Sema::ActOnOpenMPTargetSimdDirective(
9212 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009213 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li986330c2016-07-20 22:57:10 +00009214 if (!AStmt)
9215 return StmtError();
9216
Alexey Bataeve3727102018-04-18 15:57:46 +00009217 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li986330c2016-07-20 22:57:10 +00009218 // 1.2.2 OpenMP Language Terminology
9219 // Structured block - An executable statement with a single entry at the
9220 // top and a single exit at the bottom.
9221 // The point of exit cannot be a branch out of the structured block.
9222 // longjmp() and throw() must not violate the entry/exit criteria.
9223 CS->getCapturedDecl()->setNothrow();
Alexey Bataevf8365372017-11-17 17:57:25 +00009224 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
9225 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9226 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9227 // 1.2.2 OpenMP Language Terminology
9228 // Structured block - An executable statement with a single entry at the
9229 // top and a single exit at the bottom.
9230 // The point of exit cannot be a branch out of the structured block.
9231 // longjmp() and throw() must not violate the entry/exit criteria.
9232 CS->getCapturedDecl()->setNothrow();
9233 }
9234
Kelvin Li986330c2016-07-20 22:57:10 +00009235 OMPLoopDirective::HelperExprs B;
9236 // In presence of clause 'collapse' with number of loops, it will define the
9237 // nested loops number.
David Majnemer9d168222016-08-05 17:44:54 +00009238 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00009239 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevf8365372017-11-17 17:57:25 +00009240 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Li986330c2016-07-20 22:57:10 +00009241 VarsWithImplicitDSA, B);
9242 if (NestedLoopCount == 0)
9243 return StmtError();
9244
9245 assert((CurContext->isDependentContext() || B.builtAll()) &&
9246 "omp target simd loop exprs were not built");
9247
9248 if (!CurContext->isDependentContext()) {
9249 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00009250 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00009251 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Li986330c2016-07-20 22:57:10 +00009252 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9253 B.NumIterations, *this, CurScope,
9254 DSAStack))
9255 return StmtError();
9256 }
9257 }
9258
9259 if (checkSimdlenSafelenSpecified(*this, Clauses))
9260 return StmtError();
9261
Reid Kleckner87a31802018-03-12 21:43:02 +00009262 setFunctionHasBranchProtectedScope();
Kelvin Li986330c2016-07-20 22:57:10 +00009263 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
9264 NestedLoopCount, Clauses, AStmt, B);
9265}
9266
Kelvin Li02532872016-08-05 14:37:37 +00009267StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
9268 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009269 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li02532872016-08-05 14:37:37 +00009270 if (!AStmt)
9271 return StmtError();
9272
Alexey Bataeve3727102018-04-18 15:57:46 +00009273 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li02532872016-08-05 14:37:37 +00009274 // 1.2.2 OpenMP Language Terminology
9275 // Structured block - An executable statement with a single entry at the
9276 // top and a single exit at the bottom.
9277 // The point of exit cannot be a branch out of the structured block.
9278 // longjmp() and throw() must not violate the entry/exit criteria.
9279 CS->getCapturedDecl()->setNothrow();
Alexey Bataev95c6dd42017-11-29 15:14:16 +00009280 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
9281 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9282 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9283 // 1.2.2 OpenMP Language Terminology
9284 // Structured block - An executable statement with a single entry at the
9285 // top and a single exit at the bottom.
9286 // The point of exit cannot be a branch out of the structured block.
9287 // longjmp() and throw() must not violate the entry/exit criteria.
9288 CS->getCapturedDecl()->setNothrow();
9289 }
Kelvin Li02532872016-08-05 14:37:37 +00009290
9291 OMPLoopDirective::HelperExprs B;
9292 // In presence of clause 'collapse' with number of loops, it will
9293 // define the nested loops number.
9294 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00009295 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
Alexey Bataev95c6dd42017-11-29 15:14:16 +00009296 nullptr /*ordered not a clause on distribute*/, CS, *this,
9297 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li02532872016-08-05 14:37:37 +00009298 if (NestedLoopCount == 0)
9299 return StmtError();
9300
9301 assert((CurContext->isDependentContext() || B.builtAll()) &&
9302 "omp teams distribute loop exprs were not built");
9303
Reid Kleckner87a31802018-03-12 21:43:02 +00009304 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00009305
9306 DSAStack->setParentTeamsRegionLoc(StartLoc);
9307
David Majnemer9d168222016-08-05 17:44:54 +00009308 return OMPTeamsDistributeDirective::Create(
9309 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Kelvin Li02532872016-08-05 14:37:37 +00009310}
9311
Kelvin Li4e325f72016-10-25 12:50:55 +00009312StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
9313 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009314 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4e325f72016-10-25 12:50:55 +00009315 if (!AStmt)
9316 return StmtError();
9317
Alexey Bataeve3727102018-04-18 15:57:46 +00009318 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4e325f72016-10-25 12:50:55 +00009319 // 1.2.2 OpenMP Language Terminology
9320 // Structured block - An executable statement with a single entry at the
9321 // top and a single exit at the bottom.
9322 // The point of exit cannot be a branch out of the structured block.
9323 // longjmp() and throw() must not violate the entry/exit criteria.
9324 CS->getCapturedDecl()->setNothrow();
Alexey Bataev999277a2017-12-06 14:31:09 +00009325 for (int ThisCaptureLevel =
9326 getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
9327 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9328 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9329 // 1.2.2 OpenMP Language Terminology
9330 // Structured block - An executable statement with a single entry at the
9331 // top and a single exit at the bottom.
9332 // The point of exit cannot be a branch out of the structured block.
9333 // longjmp() and throw() must not violate the entry/exit criteria.
9334 CS->getCapturedDecl()->setNothrow();
9335 }
9336
Kelvin Li4e325f72016-10-25 12:50:55 +00009337
9338 OMPLoopDirective::HelperExprs B;
9339 // In presence of clause 'collapse' with number of loops, it will
9340 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00009341 unsigned NestedLoopCount = checkOpenMPLoop(
Samuel Antao4c8035b2016-12-12 18:00:20 +00009342 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev999277a2017-12-06 14:31:09 +00009343 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Samuel Antao4c8035b2016-12-12 18:00:20 +00009344 VarsWithImplicitDSA, B);
Kelvin Li4e325f72016-10-25 12:50:55 +00009345
9346 if (NestedLoopCount == 0)
9347 return StmtError();
9348
9349 assert((CurContext->isDependentContext() || B.builtAll()) &&
9350 "omp teams distribute simd loop exprs were not built");
9351
9352 if (!CurContext->isDependentContext()) {
9353 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00009354 for (OMPClause *C : Clauses) {
Kelvin Li4e325f72016-10-25 12:50:55 +00009355 if (auto *LC = dyn_cast<OMPLinearClause>(C))
9356 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9357 B.NumIterations, *this, CurScope,
9358 DSAStack))
9359 return StmtError();
9360 }
9361 }
9362
9363 if (checkSimdlenSafelenSpecified(*this, Clauses))
9364 return StmtError();
9365
Reid Kleckner87a31802018-03-12 21:43:02 +00009366 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00009367
9368 DSAStack->setParentTeamsRegionLoc(StartLoc);
9369
Kelvin Li4e325f72016-10-25 12:50:55 +00009370 return OMPTeamsDistributeSimdDirective::Create(
9371 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9372}
9373
Kelvin Li579e41c2016-11-30 23:51:03 +00009374StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
9375 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009376 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li579e41c2016-11-30 23:51:03 +00009377 if (!AStmt)
9378 return StmtError();
9379
Alexey Bataeve3727102018-04-18 15:57:46 +00009380 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li579e41c2016-11-30 23:51:03 +00009381 // 1.2.2 OpenMP Language Terminology
9382 // Structured block - An executable statement with a single entry at the
9383 // top and a single exit at the bottom.
9384 // The point of exit cannot be a branch out of the structured block.
9385 // longjmp() and throw() must not violate the entry/exit criteria.
9386 CS->getCapturedDecl()->setNothrow();
9387
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00009388 for (int ThisCaptureLevel =
9389 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
9390 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9391 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9392 // 1.2.2 OpenMP Language Terminology
9393 // Structured block - An executable statement with a single entry at the
9394 // top and a single exit at the bottom.
9395 // The point of exit cannot be a branch out of the structured block.
9396 // longjmp() and throw() must not violate the entry/exit criteria.
9397 CS->getCapturedDecl()->setNothrow();
9398 }
9399
Kelvin Li579e41c2016-11-30 23:51:03 +00009400 OMPLoopDirective::HelperExprs B;
9401 // In presence of clause 'collapse' with number of loops, it will
9402 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00009403 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li579e41c2016-11-30 23:51:03 +00009404 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00009405 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li579e41c2016-11-30 23:51:03 +00009406 VarsWithImplicitDSA, B);
9407
9408 if (NestedLoopCount == 0)
9409 return StmtError();
9410
9411 assert((CurContext->isDependentContext() || B.builtAll()) &&
9412 "omp for loop exprs were not built");
9413
9414 if (!CurContext->isDependentContext()) {
9415 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00009416 for (OMPClause *C : Clauses) {
Kelvin Li579e41c2016-11-30 23:51:03 +00009417 if (auto *LC = dyn_cast<OMPLinearClause>(C))
9418 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9419 B.NumIterations, *this, CurScope,
9420 DSAStack))
9421 return StmtError();
9422 }
9423 }
9424
9425 if (checkSimdlenSafelenSpecified(*this, Clauses))
9426 return StmtError();
9427
Reid Kleckner87a31802018-03-12 21:43:02 +00009428 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00009429
9430 DSAStack->setParentTeamsRegionLoc(StartLoc);
9431
Kelvin Li579e41c2016-11-30 23:51:03 +00009432 return OMPTeamsDistributeParallelForSimdDirective::Create(
9433 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9434}
9435
Kelvin Li7ade93f2016-12-09 03:24:30 +00009436StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
9437 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009438 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li7ade93f2016-12-09 03:24:30 +00009439 if (!AStmt)
9440 return StmtError();
9441
Alexey Bataeve3727102018-04-18 15:57:46 +00009442 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li7ade93f2016-12-09 03:24:30 +00009443 // 1.2.2 OpenMP Language Terminology
9444 // Structured block - An executable statement with a single entry at the
9445 // top and a single exit at the bottom.
9446 // The point of exit cannot be a branch out of the structured block.
9447 // longjmp() and throw() must not violate the entry/exit criteria.
9448 CS->getCapturedDecl()->setNothrow();
9449
Carlo Bertolli62fae152017-11-20 20:46:39 +00009450 for (int ThisCaptureLevel =
9451 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
9452 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9453 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9454 // 1.2.2 OpenMP Language Terminology
9455 // Structured block - An executable statement with a single entry at the
9456 // top and a single exit at the bottom.
9457 // The point of exit cannot be a branch out of the structured block.
9458 // longjmp() and throw() must not violate the entry/exit criteria.
9459 CS->getCapturedDecl()->setNothrow();
9460 }
9461
Kelvin Li7ade93f2016-12-09 03:24:30 +00009462 OMPLoopDirective::HelperExprs B;
9463 // In presence of clause 'collapse' with number of loops, it will
9464 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00009465 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li7ade93f2016-12-09 03:24:30 +00009466 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Carlo Bertolli62fae152017-11-20 20:46:39 +00009467 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li7ade93f2016-12-09 03:24:30 +00009468 VarsWithImplicitDSA, B);
9469
9470 if (NestedLoopCount == 0)
9471 return StmtError();
9472
9473 assert((CurContext->isDependentContext() || B.builtAll()) &&
9474 "omp for loop exprs were not built");
9475
Reid Kleckner87a31802018-03-12 21:43:02 +00009476 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00009477
9478 DSAStack->setParentTeamsRegionLoc(StartLoc);
9479
Kelvin Li7ade93f2016-12-09 03:24:30 +00009480 return OMPTeamsDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00009481 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
9482 DSAStack->isCancelRegion());
Kelvin Li7ade93f2016-12-09 03:24:30 +00009483}
9484
Kelvin Libf594a52016-12-17 05:48:59 +00009485StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
9486 Stmt *AStmt,
9487 SourceLocation StartLoc,
9488 SourceLocation EndLoc) {
9489 if (!AStmt)
9490 return StmtError();
9491
Alexey Bataeve3727102018-04-18 15:57:46 +00009492 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Libf594a52016-12-17 05:48:59 +00009493 // 1.2.2 OpenMP Language Terminology
9494 // Structured block - An executable statement with a single entry at the
9495 // top and a single exit at the bottom.
9496 // The point of exit cannot be a branch out of the structured block.
9497 // longjmp() and throw() must not violate the entry/exit criteria.
9498 CS->getCapturedDecl()->setNothrow();
9499
Alexey Bataevf9fc42e2017-11-22 14:25:55 +00009500 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
9501 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9502 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9503 // 1.2.2 OpenMP Language Terminology
9504 // Structured block - An executable statement with a single entry at the
9505 // top and a single exit at the bottom.
9506 // The point of exit cannot be a branch out of the structured block.
9507 // longjmp() and throw() must not violate the entry/exit criteria.
9508 CS->getCapturedDecl()->setNothrow();
9509 }
Reid Kleckner87a31802018-03-12 21:43:02 +00009510 setFunctionHasBranchProtectedScope();
Kelvin Libf594a52016-12-17 05:48:59 +00009511
9512 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
9513 AStmt);
9514}
9515
Kelvin Li83c451e2016-12-25 04:52:54 +00009516StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
9517 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009518 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li83c451e2016-12-25 04:52:54 +00009519 if (!AStmt)
9520 return StmtError();
9521
Alexey Bataeve3727102018-04-18 15:57:46 +00009522 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li83c451e2016-12-25 04:52:54 +00009523 // 1.2.2 OpenMP Language Terminology
9524 // Structured block - An executable statement with a single entry at the
9525 // top and a single exit at the bottom.
9526 // The point of exit cannot be a branch out of the structured block.
9527 // longjmp() and throw() must not violate the entry/exit criteria.
9528 CS->getCapturedDecl()->setNothrow();
Alexey Bataevdfa430f2017-12-08 15:03:50 +00009529 for (int ThisCaptureLevel =
9530 getOpenMPCaptureLevels(OMPD_target_teams_distribute);
9531 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9532 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9533 // 1.2.2 OpenMP Language Terminology
9534 // Structured block - An executable statement with a single entry at the
9535 // top and a single exit at the bottom.
9536 // The point of exit cannot be a branch out of the structured block.
9537 // longjmp() and throw() must not violate the entry/exit criteria.
9538 CS->getCapturedDecl()->setNothrow();
9539 }
Kelvin Li83c451e2016-12-25 04:52:54 +00009540
9541 OMPLoopDirective::HelperExprs B;
9542 // In presence of clause 'collapse' with number of loops, it will
9543 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00009544 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataevdfa430f2017-12-08 15:03:50 +00009545 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
9546 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li83c451e2016-12-25 04:52:54 +00009547 VarsWithImplicitDSA, B);
9548 if (NestedLoopCount == 0)
9549 return StmtError();
9550
9551 assert((CurContext->isDependentContext() || B.builtAll()) &&
9552 "omp target teams distribute loop exprs were not built");
9553
Reid Kleckner87a31802018-03-12 21:43:02 +00009554 setFunctionHasBranchProtectedScope();
Kelvin Li83c451e2016-12-25 04:52:54 +00009555 return OMPTargetTeamsDistributeDirective::Create(
9556 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9557}
9558
Kelvin Li80e8f562016-12-29 22:16:30 +00009559StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
9560 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009561 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li80e8f562016-12-29 22:16:30 +00009562 if (!AStmt)
9563 return StmtError();
9564
Alexey Bataeve3727102018-04-18 15:57:46 +00009565 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li80e8f562016-12-29 22:16:30 +00009566 // 1.2.2 OpenMP Language Terminology
9567 // Structured block - An executable statement with a single entry at the
9568 // top and a single exit at the bottom.
9569 // The point of exit cannot be a branch out of the structured block.
9570 // longjmp() and throw() must not violate the entry/exit criteria.
9571 CS->getCapturedDecl()->setNothrow();
Carlo Bertolli52978c32018-01-03 21:12:44 +00009572 for (int ThisCaptureLevel =
9573 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
9574 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9575 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9576 // 1.2.2 OpenMP Language Terminology
9577 // Structured block - An executable statement with a single entry at the
9578 // top and a single exit at the bottom.
9579 // The point of exit cannot be a branch out of the structured block.
9580 // longjmp() and throw() must not violate the entry/exit criteria.
9581 CS->getCapturedDecl()->setNothrow();
9582 }
9583
Kelvin Li80e8f562016-12-29 22:16:30 +00009584 OMPLoopDirective::HelperExprs B;
9585 // In presence of clause 'collapse' with number of loops, it will
9586 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00009587 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli52978c32018-01-03 21:12:44 +00009588 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
9589 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li80e8f562016-12-29 22:16:30 +00009590 VarsWithImplicitDSA, B);
9591 if (NestedLoopCount == 0)
9592 return StmtError();
9593
9594 assert((CurContext->isDependentContext() || B.builtAll()) &&
9595 "omp target teams distribute parallel for loop exprs were not built");
9596
Alexey Bataev647dd842018-01-15 20:59:40 +00009597 if (!CurContext->isDependentContext()) {
9598 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00009599 for (OMPClause *C : Clauses) {
Alexey Bataev647dd842018-01-15 20:59:40 +00009600 if (auto *LC = dyn_cast<OMPLinearClause>(C))
9601 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9602 B.NumIterations, *this, CurScope,
9603 DSAStack))
9604 return StmtError();
9605 }
9606 }
9607
Reid Kleckner87a31802018-03-12 21:43:02 +00009608 setFunctionHasBranchProtectedScope();
Kelvin Li80e8f562016-12-29 22:16:30 +00009609 return OMPTargetTeamsDistributeParallelForDirective::Create(
Alexey Bataev16e79882017-11-22 21:12:03 +00009610 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
9611 DSAStack->isCancelRegion());
Kelvin Li80e8f562016-12-29 22:16:30 +00009612}
9613
Kelvin Li1851df52017-01-03 05:23:48 +00009614StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
9615 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009616 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li1851df52017-01-03 05:23:48 +00009617 if (!AStmt)
9618 return StmtError();
9619
Alexey Bataeve3727102018-04-18 15:57:46 +00009620 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li1851df52017-01-03 05:23:48 +00009621 // 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();
Alexey Bataev647dd842018-01-15 20:59:40 +00009627 for (int ThisCaptureLevel = getOpenMPCaptureLevels(
9628 OMPD_target_teams_distribute_parallel_for_simd);
9629 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9630 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9631 // 1.2.2 OpenMP Language Terminology
9632 // Structured block - An executable statement with a single entry at the
9633 // top and a single exit at the bottom.
9634 // The point of exit cannot be a branch out of the structured block.
9635 // longjmp() and throw() must not violate the entry/exit criteria.
9636 CS->getCapturedDecl()->setNothrow();
9637 }
Kelvin Li1851df52017-01-03 05:23:48 +00009638
9639 OMPLoopDirective::HelperExprs B;
9640 // In presence of clause 'collapse' with number of loops, it will
9641 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00009642 unsigned NestedLoopCount =
9643 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
Alexey Bataev647dd842018-01-15 20:59:40 +00009644 getCollapseNumberExpr(Clauses),
9645 nullptr /*ordered not a clause on distribute*/, CS, *this,
9646 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li1851df52017-01-03 05:23:48 +00009647 if (NestedLoopCount == 0)
9648 return StmtError();
9649
9650 assert((CurContext->isDependentContext() || B.builtAll()) &&
9651 "omp target teams distribute parallel for simd loop exprs were not "
9652 "built");
9653
9654 if (!CurContext->isDependentContext()) {
9655 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00009656 for (OMPClause *C : Clauses) {
Kelvin Li1851df52017-01-03 05:23:48 +00009657 if (auto *LC = dyn_cast<OMPLinearClause>(C))
9658 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9659 B.NumIterations, *this, CurScope,
9660 DSAStack))
9661 return StmtError();
9662 }
9663 }
9664
Alexey Bataev438388c2017-11-22 18:34:02 +00009665 if (checkSimdlenSafelenSpecified(*this, Clauses))
9666 return StmtError();
9667
Reid Kleckner87a31802018-03-12 21:43:02 +00009668 setFunctionHasBranchProtectedScope();
Kelvin Li1851df52017-01-03 05:23:48 +00009669 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
9670 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9671}
9672
Kelvin Lida681182017-01-10 18:08:18 +00009673StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
9674 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00009675 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lida681182017-01-10 18:08:18 +00009676 if (!AStmt)
9677 return StmtError();
9678
9679 auto *CS = cast<CapturedStmt>(AStmt);
9680 // 1.2.2 OpenMP Language Terminology
9681 // Structured block - An executable statement with a single entry at the
9682 // top and a single exit at the bottom.
9683 // The point of exit cannot be a branch out of the structured block.
9684 // longjmp() and throw() must not violate the entry/exit criteria.
9685 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00009686 for (int ThisCaptureLevel =
9687 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
9688 ThisCaptureLevel > 1; --ThisCaptureLevel) {
9689 CS = cast<CapturedStmt>(CS->getCapturedStmt());
9690 // 1.2.2 OpenMP Language Terminology
9691 // Structured block - An executable statement with a single entry at the
9692 // top and a single exit at the bottom.
9693 // The point of exit cannot be a branch out of the structured block.
9694 // longjmp() and throw() must not violate the entry/exit criteria.
9695 CS->getCapturedDecl()->setNothrow();
9696 }
Kelvin Lida681182017-01-10 18:08:18 +00009697
9698 OMPLoopDirective::HelperExprs B;
9699 // In presence of clause 'collapse' with number of loops, it will
9700 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00009701 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lida681182017-01-10 18:08:18 +00009702 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00009703 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Lida681182017-01-10 18:08:18 +00009704 VarsWithImplicitDSA, B);
9705 if (NestedLoopCount == 0)
9706 return StmtError();
9707
9708 assert((CurContext->isDependentContext() || B.builtAll()) &&
9709 "omp target teams distribute simd loop exprs were not built");
9710
Alexey Bataev438388c2017-11-22 18:34:02 +00009711 if (!CurContext->isDependentContext()) {
9712 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00009713 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00009714 if (auto *LC = dyn_cast<OMPLinearClause>(C))
9715 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9716 B.NumIterations, *this, CurScope,
9717 DSAStack))
9718 return StmtError();
9719 }
9720 }
9721
9722 if (checkSimdlenSafelenSpecified(*this, Clauses))
9723 return StmtError();
9724
Reid Kleckner87a31802018-03-12 21:43:02 +00009725 setFunctionHasBranchProtectedScope();
Kelvin Lida681182017-01-10 18:08:18 +00009726 return OMPTargetTeamsDistributeSimdDirective::Create(
9727 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9728}
9729
Alexey Bataeved09d242014-05-28 05:53:51 +00009730OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009731 SourceLocation StartLoc,
9732 SourceLocation LParenLoc,
9733 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009734 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009735 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00009736 case OMPC_final:
9737 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
9738 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00009739 case OMPC_num_threads:
9740 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
9741 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00009742 case OMPC_safelen:
9743 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
9744 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00009745 case OMPC_simdlen:
9746 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
9747 break;
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00009748 case OMPC_allocator:
9749 Res = ActOnOpenMPAllocatorClause(Expr, StartLoc, LParenLoc, EndLoc);
9750 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00009751 case OMPC_collapse:
9752 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
9753 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00009754 case OMPC_ordered:
9755 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
9756 break;
Michael Wonge710d542015-08-07 16:16:36 +00009757 case OMPC_device:
9758 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
9759 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009760 case OMPC_num_teams:
9761 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
9762 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009763 case OMPC_thread_limit:
9764 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
9765 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00009766 case OMPC_priority:
9767 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
9768 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009769 case OMPC_grainsize:
9770 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
9771 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00009772 case OMPC_num_tasks:
9773 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
9774 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00009775 case OMPC_hint:
9776 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
9777 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009778 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009779 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009780 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009781 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009782 case OMPC_private:
9783 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00009784 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009785 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00009786 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009787 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009788 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00009789 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009790 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009791 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009792 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00009793 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009794 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009795 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009796 case OMPC_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00009797 case OMPC_allocate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009798 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009799 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009800 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009801 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009802 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009803 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009804 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00009805 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009806 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009807 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00009808 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009809 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009810 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009811 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009812 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009813 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009814 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009815 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009816 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00009817 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009818 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009819 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009820 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009821 case OMPC_atomic_default_mem_order:
Alexey Bataev729e2422019-08-23 16:11:14 +00009822 case OMPC_device_type:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009823 llvm_unreachable("Clause is not allowed.");
9824 }
9825 return Res;
9826}
9827
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009828// An OpenMP directive such as 'target parallel' has two captured regions:
9829// for the 'target' and 'parallel' respectively. This function returns
9830// the region in which to capture expressions associated with a clause.
9831// A return value of OMPD_unknown signifies that the expression should not
9832// be captured.
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00009833static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
9834 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
9835 OpenMPDirectiveKind NameModifier = OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009836 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009837 switch (CKind) {
9838 case OMPC_if:
9839 switch (DKind) {
9840 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00009841 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00009842 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009843 // If this clause applies to the nested 'parallel' region, capture within
9844 // the 'target' region, otherwise do not capture.
9845 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
9846 CaptureRegion = OMPD_target;
9847 break;
Carlo Bertolli52978c32018-01-03 21:12:44 +00009848 case OMPD_target_teams_distribute_parallel_for:
9849 case OMPD_target_teams_distribute_parallel_for_simd:
9850 // If this clause applies to the nested 'parallel' region, capture within
9851 // the 'teams' region, otherwise do not capture.
9852 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
9853 CaptureRegion = OMPD_teams;
9854 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00009855 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00009856 case OMPD_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00009857 CaptureRegion = OMPD_teams;
9858 break;
Alexey Bataevd2202ca2017-12-27 17:58:32 +00009859 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00009860 case OMPD_target_enter_data:
9861 case OMPD_target_exit_data:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00009862 CaptureRegion = OMPD_task;
9863 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009864 case OMPD_cancel:
9865 case OMPD_parallel:
9866 case OMPD_parallel_sections:
9867 case OMPD_parallel_for:
9868 case OMPD_parallel_for_simd:
9869 case OMPD_target:
9870 case OMPD_target_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009871 case OMPD_target_teams:
9872 case OMPD_target_teams_distribute:
9873 case OMPD_target_teams_distribute_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009874 case OMPD_distribute_parallel_for:
9875 case OMPD_distribute_parallel_for_simd:
9876 case OMPD_task:
9877 case OMPD_taskloop:
9878 case OMPD_taskloop_simd:
9879 case OMPD_target_data:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009880 // Do not capture if-clause expressions.
9881 break;
9882 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00009883 case OMPD_allocate:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009884 case OMPD_taskyield:
9885 case OMPD_barrier:
9886 case OMPD_taskwait:
9887 case OMPD_cancellation_point:
9888 case OMPD_flush:
9889 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +00009890 case OMPD_declare_mapper:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009891 case OMPD_declare_simd:
9892 case OMPD_declare_target:
9893 case OMPD_end_declare_target:
9894 case OMPD_teams:
9895 case OMPD_simd:
9896 case OMPD_for:
9897 case OMPD_for_simd:
9898 case OMPD_sections:
9899 case OMPD_section:
9900 case OMPD_single:
9901 case OMPD_master:
9902 case OMPD_critical:
9903 case OMPD_taskgroup:
9904 case OMPD_distribute:
9905 case OMPD_ordered:
9906 case OMPD_atomic:
9907 case OMPD_distribute_simd:
9908 case OMPD_teams_distribute:
9909 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00009910 case OMPD_requires:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009911 llvm_unreachable("Unexpected OpenMP directive with if-clause");
9912 case OMPD_unknown:
9913 llvm_unreachable("Unknown OpenMP directive");
9914 }
9915 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00009916 case OMPC_num_threads:
9917 switch (DKind) {
9918 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00009919 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00009920 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00009921 CaptureRegion = OMPD_target;
9922 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00009923 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00009924 case OMPD_teams_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00009925 case OMPD_target_teams_distribute_parallel_for:
9926 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00009927 CaptureRegion = OMPD_teams;
9928 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00009929 case OMPD_parallel:
9930 case OMPD_parallel_sections:
9931 case OMPD_parallel_for:
9932 case OMPD_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00009933 case OMPD_distribute_parallel_for:
9934 case OMPD_distribute_parallel_for_simd:
9935 // Do not capture num_threads-clause expressions.
9936 break;
9937 case OMPD_target_data:
9938 case OMPD_target_enter_data:
9939 case OMPD_target_exit_data:
9940 case OMPD_target_update:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00009941 case OMPD_target:
9942 case OMPD_target_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00009943 case OMPD_target_teams:
9944 case OMPD_target_teams_distribute:
9945 case OMPD_target_teams_distribute_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00009946 case OMPD_cancel:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00009947 case OMPD_task:
9948 case OMPD_taskloop:
9949 case OMPD_taskloop_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00009950 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00009951 case OMPD_allocate:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00009952 case OMPD_taskyield:
9953 case OMPD_barrier:
9954 case OMPD_taskwait:
9955 case OMPD_cancellation_point:
9956 case OMPD_flush:
9957 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +00009958 case OMPD_declare_mapper:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00009959 case OMPD_declare_simd:
9960 case OMPD_declare_target:
9961 case OMPD_end_declare_target:
9962 case OMPD_teams:
9963 case OMPD_simd:
9964 case OMPD_for:
9965 case OMPD_for_simd:
9966 case OMPD_sections:
9967 case OMPD_section:
9968 case OMPD_single:
9969 case OMPD_master:
9970 case OMPD_critical:
9971 case OMPD_taskgroup:
9972 case OMPD_distribute:
9973 case OMPD_ordered:
9974 case OMPD_atomic:
9975 case OMPD_distribute_simd:
9976 case OMPD_teams_distribute:
9977 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00009978 case OMPD_requires:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00009979 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
9980 case OMPD_unknown:
9981 llvm_unreachable("Unknown OpenMP directive");
9982 }
9983 break;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00009984 case OMPC_num_teams:
9985 switch (DKind) {
9986 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00009987 case OMPD_target_teams_distribute:
9988 case OMPD_target_teams_distribute_simd:
9989 case OMPD_target_teams_distribute_parallel_for:
9990 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00009991 CaptureRegion = OMPD_target;
9992 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00009993 case OMPD_teams_distribute_parallel_for:
9994 case OMPD_teams_distribute_parallel_for_simd:
9995 case OMPD_teams:
9996 case OMPD_teams_distribute:
9997 case OMPD_teams_distribute_simd:
9998 // Do not capture num_teams-clause expressions.
9999 break;
10000 case OMPD_distribute_parallel_for:
10001 case OMPD_distribute_parallel_for_simd:
10002 case OMPD_task:
10003 case OMPD_taskloop:
10004 case OMPD_taskloop_simd:
10005 case OMPD_target_data:
10006 case OMPD_target_enter_data:
10007 case OMPD_target_exit_data:
10008 case OMPD_target_update:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000010009 case OMPD_cancel:
10010 case OMPD_parallel:
10011 case OMPD_parallel_sections:
10012 case OMPD_parallel_for:
10013 case OMPD_parallel_for_simd:
10014 case OMPD_target:
10015 case OMPD_target_simd:
10016 case OMPD_target_parallel:
10017 case OMPD_target_parallel_for:
10018 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000010019 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000010020 case OMPD_allocate:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000010021 case OMPD_taskyield:
10022 case OMPD_barrier:
10023 case OMPD_taskwait:
10024 case OMPD_cancellation_point:
10025 case OMPD_flush:
10026 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +000010027 case OMPD_declare_mapper:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000010028 case OMPD_declare_simd:
10029 case OMPD_declare_target:
10030 case OMPD_end_declare_target:
10031 case OMPD_simd:
10032 case OMPD_for:
10033 case OMPD_for_simd:
10034 case OMPD_sections:
10035 case OMPD_section:
10036 case OMPD_single:
10037 case OMPD_master:
10038 case OMPD_critical:
10039 case OMPD_taskgroup:
10040 case OMPD_distribute:
10041 case OMPD_ordered:
10042 case OMPD_atomic:
10043 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +000010044 case OMPD_requires:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000010045 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
10046 case OMPD_unknown:
10047 llvm_unreachable("Unknown OpenMP directive");
10048 }
10049 break;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000010050 case OMPC_thread_limit:
10051 switch (DKind) {
10052 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +000010053 case OMPD_target_teams_distribute:
10054 case OMPD_target_teams_distribute_simd:
10055 case OMPD_target_teams_distribute_parallel_for:
10056 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000010057 CaptureRegion = OMPD_target;
10058 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +000010059 case OMPD_teams_distribute_parallel_for:
10060 case OMPD_teams_distribute_parallel_for_simd:
10061 case OMPD_teams:
10062 case OMPD_teams_distribute:
10063 case OMPD_teams_distribute_simd:
10064 // Do not capture thread_limit-clause expressions.
10065 break;
10066 case OMPD_distribute_parallel_for:
10067 case OMPD_distribute_parallel_for_simd:
10068 case OMPD_task:
10069 case OMPD_taskloop:
10070 case OMPD_taskloop_simd:
10071 case OMPD_target_data:
10072 case OMPD_target_enter_data:
10073 case OMPD_target_exit_data:
10074 case OMPD_target_update:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000010075 case OMPD_cancel:
10076 case OMPD_parallel:
10077 case OMPD_parallel_sections:
10078 case OMPD_parallel_for:
10079 case OMPD_parallel_for_simd:
10080 case OMPD_target:
10081 case OMPD_target_simd:
10082 case OMPD_target_parallel:
10083 case OMPD_target_parallel_for:
10084 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000010085 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000010086 case OMPD_allocate:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000010087 case OMPD_taskyield:
10088 case OMPD_barrier:
10089 case OMPD_taskwait:
10090 case OMPD_cancellation_point:
10091 case OMPD_flush:
10092 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +000010093 case OMPD_declare_mapper:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000010094 case OMPD_declare_simd:
10095 case OMPD_declare_target:
10096 case OMPD_end_declare_target:
10097 case OMPD_simd:
10098 case OMPD_for:
10099 case OMPD_for_simd:
10100 case OMPD_sections:
10101 case OMPD_section:
10102 case OMPD_single:
10103 case OMPD_master:
10104 case OMPD_critical:
10105 case OMPD_taskgroup:
10106 case OMPD_distribute:
10107 case OMPD_ordered:
10108 case OMPD_atomic:
10109 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +000010110 case OMPD_requires:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000010111 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
10112 case OMPD_unknown:
10113 llvm_unreachable("Unknown OpenMP directive");
10114 }
10115 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010116 case OMPC_schedule:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +000010117 switch (DKind) {
Alexey Bataev2ba67042017-11-28 21:11:44 +000010118 case OMPD_parallel_for:
10119 case OMPD_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +000010120 case OMPD_distribute_parallel_for:
Alexey Bataev974acd62017-11-27 19:38:52 +000010121 case OMPD_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +000010122 case OMPD_teams_distribute_parallel_for:
10123 case OMPD_teams_distribute_parallel_for_simd:
10124 case OMPD_target_parallel_for:
10125 case OMPD_target_parallel_for_simd:
10126 case OMPD_target_teams_distribute_parallel_for:
10127 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +000010128 CaptureRegion = OMPD_parallel;
10129 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +000010130 case OMPD_for:
10131 case OMPD_for_simd:
10132 // Do not capture schedule-clause expressions.
Alexey Bataevfb0ebec2017-11-08 20:16:14 +000010133 break;
10134 case OMPD_task:
10135 case OMPD_taskloop:
10136 case OMPD_taskloop_simd:
10137 case OMPD_target_data:
10138 case OMPD_target_enter_data:
10139 case OMPD_target_exit_data:
10140 case OMPD_target_update:
10141 case OMPD_teams:
10142 case OMPD_teams_distribute:
10143 case OMPD_teams_distribute_simd:
10144 case OMPD_target_teams_distribute:
10145 case OMPD_target_teams_distribute_simd:
10146 case OMPD_target:
10147 case OMPD_target_simd:
10148 case OMPD_target_parallel:
10149 case OMPD_cancel:
10150 case OMPD_parallel:
10151 case OMPD_parallel_sections:
10152 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000010153 case OMPD_allocate:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +000010154 case OMPD_taskyield:
10155 case OMPD_barrier:
10156 case OMPD_taskwait:
10157 case OMPD_cancellation_point:
10158 case OMPD_flush:
10159 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +000010160 case OMPD_declare_mapper:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +000010161 case OMPD_declare_simd:
10162 case OMPD_declare_target:
10163 case OMPD_end_declare_target:
10164 case OMPD_simd:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +000010165 case OMPD_sections:
10166 case OMPD_section:
10167 case OMPD_single:
10168 case OMPD_master:
10169 case OMPD_critical:
10170 case OMPD_taskgroup:
10171 case OMPD_distribute:
10172 case OMPD_ordered:
10173 case OMPD_atomic:
10174 case OMPD_distribute_simd:
10175 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +000010176 case OMPD_requires:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +000010177 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
10178 case OMPD_unknown:
10179 llvm_unreachable("Unknown OpenMP directive");
10180 }
10181 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010182 case OMPC_dist_schedule:
Carlo Bertolli62fae152017-11-20 20:46:39 +000010183 switch (DKind) {
10184 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +000010185 case OMPD_teams_distribute_parallel_for_simd:
10186 case OMPD_teams_distribute:
10187 case OMPD_teams_distribute_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +000010188 case OMPD_target_teams_distribute_parallel_for:
10189 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +000010190 case OMPD_target_teams_distribute:
10191 case OMPD_target_teams_distribute_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +000010192 CaptureRegion = OMPD_teams;
Alexey Bataev2ba67042017-11-28 21:11:44 +000010193 break;
10194 case OMPD_distribute_parallel_for:
10195 case OMPD_distribute_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +000010196 case OMPD_distribute:
Carlo Bertolli62fae152017-11-20 20:46:39 +000010197 case OMPD_distribute_simd:
10198 // Do not capture thread_limit-clause expressions.
10199 break;
10200 case OMPD_parallel_for:
10201 case OMPD_parallel_for_simd:
10202 case OMPD_target_parallel_for_simd:
10203 case OMPD_target_parallel_for:
10204 case OMPD_task:
10205 case OMPD_taskloop:
10206 case OMPD_taskloop_simd:
10207 case OMPD_target_data:
10208 case OMPD_target_enter_data:
10209 case OMPD_target_exit_data:
10210 case OMPD_target_update:
10211 case OMPD_teams:
10212 case OMPD_target:
10213 case OMPD_target_simd:
10214 case OMPD_target_parallel:
10215 case OMPD_cancel:
10216 case OMPD_parallel:
10217 case OMPD_parallel_sections:
10218 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000010219 case OMPD_allocate:
Carlo Bertolli62fae152017-11-20 20:46:39 +000010220 case OMPD_taskyield:
10221 case OMPD_barrier:
10222 case OMPD_taskwait:
10223 case OMPD_cancellation_point:
10224 case OMPD_flush:
10225 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +000010226 case OMPD_declare_mapper:
Carlo Bertolli62fae152017-11-20 20:46:39 +000010227 case OMPD_declare_simd:
10228 case OMPD_declare_target:
10229 case OMPD_end_declare_target:
10230 case OMPD_simd:
10231 case OMPD_for:
10232 case OMPD_for_simd:
10233 case OMPD_sections:
10234 case OMPD_section:
10235 case OMPD_single:
10236 case OMPD_master:
10237 case OMPD_critical:
10238 case OMPD_taskgroup:
Carlo Bertolli62fae152017-11-20 20:46:39 +000010239 case OMPD_ordered:
10240 case OMPD_atomic:
10241 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +000010242 case OMPD_requires:
Carlo Bertolli62fae152017-11-20 20:46:39 +000010243 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
10244 case OMPD_unknown:
10245 llvm_unreachable("Unknown OpenMP directive");
10246 }
10247 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +000010248 case OMPC_device:
10249 switch (DKind) {
Alexey Bataevd2202ca2017-12-27 17:58:32 +000010250 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +000010251 case OMPD_target_enter_data:
10252 case OMPD_target_exit_data:
Alexey Bataev8451efa2018-01-15 19:06:12 +000010253 case OMPD_target:
Alexey Bataevf41c88f2018-01-16 15:05:16 +000010254 case OMPD_target_simd:
Alexey Bataev0c869ef2018-01-16 15:57:07 +000010255 case OMPD_target_teams:
Alexey Bataev54d5c7d2018-01-16 16:27:49 +000010256 case OMPD_target_parallel:
Alexey Bataev79df7562018-01-16 16:46:46 +000010257 case OMPD_target_teams_distribute:
Alexey Bataev8d16a432018-01-16 17:22:50 +000010258 case OMPD_target_teams_distribute_simd:
Alexey Bataev8ed895512018-01-16 17:41:04 +000010259 case OMPD_target_parallel_for:
Alexey Bataevd60d1ba2018-01-16 17:55:15 +000010260 case OMPD_target_parallel_for_simd:
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +000010261 case OMPD_target_teams_distribute_parallel_for:
Alexey Bataev9350fc32018-01-16 19:18:24 +000010262 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataevd2202ca2017-12-27 17:58:32 +000010263 CaptureRegion = OMPD_task;
10264 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +000010265 case OMPD_target_data:
Alexey Bataev2ba67042017-11-28 21:11:44 +000010266 // Do not capture device-clause expressions.
10267 break;
10268 case OMPD_teams_distribute_parallel_for:
10269 case OMPD_teams_distribute_parallel_for_simd:
10270 case OMPD_teams:
10271 case OMPD_teams_distribute:
10272 case OMPD_teams_distribute_simd:
10273 case OMPD_distribute_parallel_for:
10274 case OMPD_distribute_parallel_for_simd:
10275 case OMPD_task:
10276 case OMPD_taskloop:
10277 case OMPD_taskloop_simd:
10278 case OMPD_cancel:
10279 case OMPD_parallel:
10280 case OMPD_parallel_sections:
10281 case OMPD_parallel_for:
10282 case OMPD_parallel_for_simd:
10283 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000010284 case OMPD_allocate:
Alexey Bataev2ba67042017-11-28 21:11:44 +000010285 case OMPD_taskyield:
10286 case OMPD_barrier:
10287 case OMPD_taskwait:
10288 case OMPD_cancellation_point:
10289 case OMPD_flush:
10290 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +000010291 case OMPD_declare_mapper:
Alexey Bataev2ba67042017-11-28 21:11:44 +000010292 case OMPD_declare_simd:
10293 case OMPD_declare_target:
10294 case OMPD_end_declare_target:
10295 case OMPD_simd:
10296 case OMPD_for:
10297 case OMPD_for_simd:
10298 case OMPD_sections:
10299 case OMPD_section:
10300 case OMPD_single:
10301 case OMPD_master:
10302 case OMPD_critical:
10303 case OMPD_taskgroup:
10304 case OMPD_distribute:
10305 case OMPD_ordered:
10306 case OMPD_atomic:
10307 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +000010308 case OMPD_requires:
Alexey Bataev2ba67042017-11-28 21:11:44 +000010309 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
10310 case OMPD_unknown:
10311 llvm_unreachable("Unknown OpenMP directive");
10312 }
10313 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010314 case OMPC_firstprivate:
10315 case OMPC_lastprivate:
10316 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +000010317 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +000010318 case OMPC_in_reduction:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010319 case OMPC_linear:
10320 case OMPC_default:
10321 case OMPC_proc_bind:
10322 case OMPC_final:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010323 case OMPC_safelen:
10324 case OMPC_simdlen:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000010325 case OMPC_allocator:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010326 case OMPC_collapse:
10327 case OMPC_private:
10328 case OMPC_shared:
10329 case OMPC_aligned:
10330 case OMPC_copyin:
10331 case OMPC_copyprivate:
10332 case OMPC_ordered:
10333 case OMPC_nowait:
10334 case OMPC_untied:
10335 case OMPC_mergeable:
10336 case OMPC_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000010337 case OMPC_allocate:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010338 case OMPC_flush:
10339 case OMPC_read:
10340 case OMPC_write:
10341 case OMPC_update:
10342 case OMPC_capture:
10343 case OMPC_seq_cst:
10344 case OMPC_depend:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010345 case OMPC_threads:
10346 case OMPC_simd:
10347 case OMPC_map:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010348 case OMPC_priority:
10349 case OMPC_grainsize:
10350 case OMPC_nogroup:
10351 case OMPC_num_tasks:
10352 case OMPC_hint:
10353 case OMPC_defaultmap:
10354 case OMPC_unknown:
10355 case OMPC_uniform:
10356 case OMPC_to:
10357 case OMPC_from:
10358 case OMPC_use_device_ptr:
10359 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +000010360 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +000010361 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +000010362 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +000010363 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +000010364 case OMPC_atomic_default_mem_order:
Alexey Bataev729e2422019-08-23 16:11:14 +000010365 case OMPC_device_type:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010366 llvm_unreachable("Unexpected OpenMP clause.");
10367 }
10368 return CaptureRegion;
10369}
10370
Alexey Bataev6b8046a2015-09-03 07:23:48 +000010371OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
10372 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +000010373 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +000010374 SourceLocation NameModifierLoc,
10375 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +000010376 SourceLocation EndLoc) {
10377 Expr *ValExpr = Condition;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010378 Stmt *HelperValStmt = nullptr;
10379 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Alexey Bataevaadd52e2014-02-13 05:29:23 +000010380 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
10381 !Condition->isInstantiationDependent() &&
10382 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +000010383 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +000010384 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000010385 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +000010386
Alexey Bataev8e769ee2017-12-22 21:01:52 +000010387 ValExpr = Val.get();
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010388
10389 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
10390 CaptureRegion =
10391 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
Alexey Bataev2ba67042017-11-28 21:11:44 +000010392 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000010393 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000010394 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010395 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
10396 HelperValStmt = buildPreInits(Context, Captures);
10397 }
Alexey Bataevaadd52e2014-02-13 05:29:23 +000010398 }
10399
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000010400 return new (Context)
10401 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
10402 LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +000010403}
10404
Alexey Bataev3778b602014-07-17 07:32:53 +000010405OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
10406 SourceLocation StartLoc,
10407 SourceLocation LParenLoc,
10408 SourceLocation EndLoc) {
10409 Expr *ValExpr = Condition;
10410 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
10411 !Condition->isInstantiationDependent() &&
10412 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +000010413 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +000010414 if (Val.isInvalid())
10415 return nullptr;
10416
Richard Smith03a4aa32016-06-23 19:02:52 +000010417 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +000010418 }
10419
10420 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10421}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000010422ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
10423 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +000010424 if (!Op)
10425 return ExprError();
10426
10427 class IntConvertDiagnoser : public ICEConvertDiagnoser {
10428 public:
10429 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +000010430 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +000010431 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
10432 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +000010433 return S.Diag(Loc, diag::err_omp_not_integral) << T;
10434 }
Alexey Bataeved09d242014-05-28 05:53:51 +000010435 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
10436 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +000010437 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
10438 }
Alexey Bataeved09d242014-05-28 05:53:51 +000010439 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
10440 QualType T,
10441 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +000010442 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
10443 }
Alexey Bataeved09d242014-05-28 05:53:51 +000010444 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
10445 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +000010446 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +000010447 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +000010448 }
Alexey Bataeved09d242014-05-28 05:53:51 +000010449 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
10450 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +000010451 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
10452 }
Alexey Bataeved09d242014-05-28 05:53:51 +000010453 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
10454 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +000010455 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +000010456 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +000010457 }
Alexey Bataeved09d242014-05-28 05:53:51 +000010458 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
10459 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +000010460 llvm_unreachable("conversion functions are permitted");
10461 }
10462 } ConvertDiagnoser;
10463 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
10464}
10465
Alexey Bataeve3727102018-04-18 15:57:46 +000010466static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +000010467 OpenMPClauseKind CKind,
10468 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010469 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
10470 !ValExpr->isInstantiationDependent()) {
10471 SourceLocation Loc = ValExpr->getExprLoc();
10472 ExprResult Value =
10473 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
10474 if (Value.isInvalid())
10475 return false;
10476
10477 ValExpr = Value.get();
10478 // The expression must evaluate to a non-negative integer value.
10479 llvm::APSInt Result;
10480 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +000010481 Result.isSigned() &&
10482 !((!StrictlyPositive && Result.isNonNegative()) ||
10483 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010484 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +000010485 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
10486 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010487 return false;
10488 }
10489 }
10490 return true;
10491}
10492
Alexey Bataev568a8332014-03-06 06:15:19 +000010493OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
10494 SourceLocation StartLoc,
10495 SourceLocation LParenLoc,
10496 SourceLocation EndLoc) {
10497 Expr *ValExpr = NumThreads;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +000010498 Stmt *HelperValStmt = nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +000010499
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010500 // OpenMP [2.5, Restrictions]
10501 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000010502 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
Alexey Bataeva0569352015-12-01 10:17:31 +000010503 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010504 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +000010505
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +000010506 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000010507 OpenMPDirectiveKind CaptureRegion =
10508 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
10509 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000010510 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000010511 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +000010512 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
10513 HelperValStmt = buildPreInits(Context, Captures);
10514 }
10515
10516 return new (Context) OMPNumThreadsClause(
10517 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +000010518}
10519
Alexey Bataev62c87d22014-03-21 04:51:18 +000010520ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010521 OpenMPClauseKind CKind,
10522 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +000010523 if (!E)
10524 return ExprError();
10525 if (E->isValueDependent() || E->isTypeDependent() ||
10526 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +000010527 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +000010528 llvm::APSInt Result;
10529 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
10530 if (ICE.isInvalid())
10531 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010532 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
10533 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +000010534 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010535 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
10536 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +000010537 return ExprError();
10538 }
Alexander Musman09184fe2014-09-30 05:29:28 +000010539 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
10540 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
10541 << E->getSourceRange();
10542 return ExprError();
10543 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010544 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
10545 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +000010546 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010547 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +000010548 return ICE;
10549}
10550
10551OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
10552 SourceLocation LParenLoc,
10553 SourceLocation EndLoc) {
10554 // OpenMP [2.8.1, simd construct, Description]
10555 // The parameter of the safelen clause must be a constant
10556 // positive integer expression.
10557 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
10558 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000010559 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +000010560 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +000010561 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +000010562}
10563
Alexey Bataev66b15b52015-08-21 11:14:16 +000010564OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
10565 SourceLocation LParenLoc,
10566 SourceLocation EndLoc) {
10567 // OpenMP [2.8.1, simd construct, Description]
10568 // The parameter of the simdlen clause must be a constant
10569 // positive integer expression.
10570 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
10571 if (Simdlen.isInvalid())
10572 return nullptr;
10573 return new (Context)
10574 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
10575}
10576
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000010577/// Tries to find omp_allocator_handle_t type.
Alexey Bataev27ef9512019-03-20 20:14:22 +000010578static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc,
10579 DSAStackTy *Stack) {
10580 QualType OMPAllocatorHandleT = Stack->getOMPAllocatorHandleT();
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000010581 if (!OMPAllocatorHandleT.isNull())
10582 return true;
Alexey Bataev27ef9512019-03-20 20:14:22 +000010583 // Build the predefined allocator expressions.
10584 bool ErrorFound = false;
10585 for (int I = OMPAllocateDeclAttr::OMPDefaultMemAlloc;
10586 I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
10587 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
10588 StringRef Allocator =
10589 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind);
10590 DeclarationName AllocatorName = &S.getASTContext().Idents.get(Allocator);
10591 auto *VD = dyn_cast_or_null<ValueDecl>(
10592 S.LookupSingleName(S.TUScope, AllocatorName, Loc, Sema::LookupAnyName));
10593 if (!VD) {
10594 ErrorFound = true;
10595 break;
10596 }
10597 QualType AllocatorType =
10598 VD->getType().getNonLValueExprType(S.getASTContext());
10599 ExprResult Res = S.BuildDeclRefExpr(VD, AllocatorType, VK_LValue, Loc);
10600 if (!Res.isUsable()) {
10601 ErrorFound = true;
10602 break;
10603 }
10604 if (OMPAllocatorHandleT.isNull())
10605 OMPAllocatorHandleT = AllocatorType;
10606 if (!S.getASTContext().hasSameType(OMPAllocatorHandleT, AllocatorType)) {
10607 ErrorFound = true;
10608 break;
10609 }
10610 Stack->setAllocator(AllocatorKind, Res.get());
10611 }
10612 if (ErrorFound) {
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000010613 S.Diag(Loc, diag::err_implied_omp_allocator_handle_t_not_found);
10614 return false;
10615 }
Alexey Bataev27ef9512019-03-20 20:14:22 +000010616 OMPAllocatorHandleT.addConst();
10617 Stack->setOMPAllocatorHandleT(OMPAllocatorHandleT);
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000010618 return true;
10619}
10620
10621OMPClause *Sema::ActOnOpenMPAllocatorClause(Expr *A, SourceLocation StartLoc,
10622 SourceLocation LParenLoc,
10623 SourceLocation EndLoc) {
10624 // OpenMP [2.11.3, allocate Directive, Description]
10625 // allocator is an expression of omp_allocator_handle_t type.
Alexey Bataev27ef9512019-03-20 20:14:22 +000010626 if (!findOMPAllocatorHandleT(*this, A->getExprLoc(), DSAStack))
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000010627 return nullptr;
10628
10629 ExprResult Allocator = DefaultLvalueConversion(A);
10630 if (Allocator.isInvalid())
10631 return nullptr;
Alexey Bataev27ef9512019-03-20 20:14:22 +000010632 Allocator = PerformImplicitConversion(Allocator.get(),
10633 DSAStack->getOMPAllocatorHandleT(),
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000010634 Sema::AA_Initializing,
10635 /*AllowExplicit=*/true);
10636 if (Allocator.isInvalid())
10637 return nullptr;
10638 return new (Context)
10639 OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc);
10640}
10641
Alexander Musman64d33f12014-06-04 07:53:32 +000010642OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
10643 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +000010644 SourceLocation LParenLoc,
10645 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +000010646 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +000010647 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +000010648 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +000010649 // The parameter of the collapse clause must be a constant
10650 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +000010651 ExprResult NumForLoopsResult =
10652 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
10653 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +000010654 return nullptr;
10655 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +000010656 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +000010657}
10658
Alexey Bataev10e775f2015-07-30 11:36:16 +000010659OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
10660 SourceLocation EndLoc,
10661 SourceLocation LParenLoc,
10662 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +000010663 // OpenMP [2.7.1, loop construct, Description]
10664 // OpenMP [2.8.1, simd construct, Description]
10665 // OpenMP [2.9.6, distribute construct, Description]
10666 // The parameter of the ordered clause must be a constant
10667 // positive integer expression if any.
10668 if (NumForLoops && LParenLoc.isValid()) {
10669 ExprResult NumForLoopsResult =
10670 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
10671 if (NumForLoopsResult.isInvalid())
10672 return nullptr;
10673 NumForLoops = NumForLoopsResult.get();
Alexey Bataeve3727102018-04-18 15:57:46 +000010674 } else {
Alexey Bataev346265e2015-09-25 10:37:12 +000010675 NumForLoops = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010676 }
Alexey Bataevf138fda2018-08-13 19:04:24 +000010677 auto *Clause = OMPOrderedClause::Create(
10678 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0,
10679 StartLoc, LParenLoc, EndLoc);
10680 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause);
10681 return Clause;
Alexey Bataev10e775f2015-07-30 11:36:16 +000010682}
10683
Alexey Bataeved09d242014-05-28 05:53:51 +000010684OMPClause *Sema::ActOnOpenMPSimpleClause(
10685 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
10686 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +000010687 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010688 switch (Kind) {
10689 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +000010690 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +000010691 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
10692 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010693 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +000010694 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +000010695 Res = ActOnOpenMPProcBindClause(
10696 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
10697 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +000010698 break;
Patrick Lyster7a2a27c2018-11-02 12:18:11 +000010699 case OMPC_atomic_default_mem_order:
10700 Res = ActOnOpenMPAtomicDefaultMemOrderClause(
10701 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument),
10702 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
10703 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +000010704 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +000010705 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +000010706 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +000010707 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +000010708 case OMPC_simdlen:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000010709 case OMPC_allocator:
Alexander Musman8bd31e62014-05-27 15:12:19 +000010710 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +000010711 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010712 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010713 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +000010714 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +000010715 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +000010716 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +000010717 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +000010718 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +000010719 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010720 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010721 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +000010722 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +000010723 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +000010724 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +000010725 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +000010726 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010727 case OMPC_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000010728 case OMPC_allocate:
Alexey Bataev6125da92014-07-21 11:26:11 +000010729 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +000010730 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +000010731 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +000010732 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +000010733 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +000010734 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010735 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +000010736 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +000010737 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +000010738 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +000010739 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +000010740 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010741 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +000010742 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000010743 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +000010744 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +000010745 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +000010746 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +000010747 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000010748 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010749 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +000010750 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +000010751 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +000010752 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +000010753 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +000010754 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +000010755 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +000010756 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +000010757 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +000010758 case OMPC_dynamic_allocators:
Alexey Bataev729e2422019-08-23 16:11:14 +000010759 case OMPC_device_type:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010760 llvm_unreachable("Clause is not allowed.");
10761 }
10762 return Res;
10763}
10764
Alexey Bataev6402bca2015-12-28 07:25:51 +000010765static std::string
10766getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
10767 ArrayRef<unsigned> Exclude = llvm::None) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010768 SmallString<256> Buffer;
10769 llvm::raw_svector_ostream Out(Buffer);
Alexey Bataev6402bca2015-12-28 07:25:51 +000010770 unsigned Bound = Last >= 2 ? Last - 2 : 0;
10771 unsigned Skipped = Exclude.size();
10772 auto S = Exclude.begin(), E = Exclude.end();
Alexey Bataeve3727102018-04-18 15:57:46 +000010773 for (unsigned I = First; I < Last; ++I) {
10774 if (std::find(S, E, I) != E) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000010775 --Skipped;
10776 continue;
10777 }
Alexey Bataeve3727102018-04-18 15:57:46 +000010778 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'";
10779 if (I == Bound - Skipped)
10780 Out << " or ";
10781 else if (I != Bound + 1 - Skipped)
10782 Out << ", ";
Alexey Bataev6402bca2015-12-28 07:25:51 +000010783 }
Alexey Bataeve3727102018-04-18 15:57:46 +000010784 return Out.str();
Alexey Bataev6402bca2015-12-28 07:25:51 +000010785}
10786
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010787OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
10788 SourceLocation KindKwLoc,
10789 SourceLocation StartLoc,
10790 SourceLocation LParenLoc,
10791 SourceLocation EndLoc) {
10792 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +000010793 static_assert(OMPC_DEFAULT_unknown > 0,
10794 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010795 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010796 << getListOfPossibleValues(OMPC_default, /*First=*/0,
10797 /*Last=*/OMPC_DEFAULT_unknown)
10798 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +000010799 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010800 }
Alexey Bataev758e55e2013-09-06 18:03:48 +000010801 switch (Kind) {
10802 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +000010803 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010804 break;
10805 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +000010806 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010807 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +000010808 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +000010809 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +000010810 break;
10811 }
Alexey Bataeved09d242014-05-28 05:53:51 +000010812 return new (Context)
10813 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010814}
10815
Alexey Bataevbcbadb62014-05-06 06:04:14 +000010816OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
10817 SourceLocation KindKwLoc,
10818 SourceLocation StartLoc,
10819 SourceLocation LParenLoc,
10820 SourceLocation EndLoc) {
10821 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +000010822 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010823 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
10824 /*Last=*/OMPC_PROC_BIND_unknown)
10825 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +000010826 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +000010827 }
Alexey Bataeved09d242014-05-28 05:53:51 +000010828 return new (Context)
10829 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +000010830}
10831
Patrick Lyster7a2a27c2018-11-02 12:18:11 +000010832OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause(
10833 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc,
10834 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
10835 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) {
10836 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
10837 << getListOfPossibleValues(
10838 OMPC_atomic_default_mem_order, /*First=*/0,
10839 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown)
10840 << getOpenMPClauseName(OMPC_atomic_default_mem_order);
10841 return nullptr;
10842 }
10843 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc,
10844 LParenLoc, EndLoc);
10845}
10846
Alexey Bataev56dafe82014-06-20 07:16:17 +000010847OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +000010848 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +000010849 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +000010850 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +000010851 SourceLocation EndLoc) {
10852 OMPClause *Res = nullptr;
10853 switch (Kind) {
10854 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +000010855 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
10856 assert(Argument.size() == NumberOfElements &&
10857 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +000010858 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +000010859 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
10860 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
10861 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
10862 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
10863 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +000010864 break;
10865 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +000010866 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
10867 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
10868 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
10869 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +000010870 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +000010871 case OMPC_dist_schedule:
10872 Res = ActOnOpenMPDistScheduleClause(
10873 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
10874 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
10875 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000010876 case OMPC_defaultmap:
10877 enum { Modifier, DefaultmapKind };
10878 Res = ActOnOpenMPDefaultmapClause(
10879 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
10880 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
David Majnemer9d168222016-08-05 17:44:54 +000010881 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
10882 EndLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000010883 break;
Alexey Bataev3778b602014-07-17 07:32:53 +000010884 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +000010885 case OMPC_num_threads:
10886 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +000010887 case OMPC_simdlen:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000010888 case OMPC_allocator:
Alexey Bataev56dafe82014-06-20 07:16:17 +000010889 case OMPC_collapse:
10890 case OMPC_default:
10891 case OMPC_proc_bind:
10892 case OMPC_private:
10893 case OMPC_firstprivate:
10894 case OMPC_lastprivate:
10895 case OMPC_shared:
10896 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +000010897 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +000010898 case OMPC_in_reduction:
Alexey Bataev56dafe82014-06-20 07:16:17 +000010899 case OMPC_linear:
10900 case OMPC_aligned:
10901 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +000010902 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +000010903 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +000010904 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +000010905 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +000010906 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +000010907 case OMPC_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000010908 case OMPC_allocate:
Alexey Bataev6125da92014-07-21 11:26:11 +000010909 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +000010910 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +000010911 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +000010912 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +000010913 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +000010914 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010915 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +000010916 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +000010917 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +000010918 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +000010919 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +000010920 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010921 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +000010922 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000010923 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +000010924 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +000010925 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +000010926 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +000010927 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +000010928 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +000010929 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +000010930 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +000010931 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +000010932 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +000010933 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +000010934 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +000010935 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +000010936 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +000010937 case OMPC_atomic_default_mem_order:
Alexey Bataev729e2422019-08-23 16:11:14 +000010938 case OMPC_device_type:
Alexey Bataev56dafe82014-06-20 07:16:17 +000010939 llvm_unreachable("Clause is not allowed.");
10940 }
10941 return Res;
10942}
10943
Alexey Bataev6402bca2015-12-28 07:25:51 +000010944static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
10945 OpenMPScheduleClauseModifier M2,
10946 SourceLocation M1Loc, SourceLocation M2Loc) {
10947 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
10948 SmallVector<unsigned, 2> Excluded;
10949 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
10950 Excluded.push_back(M2);
10951 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
10952 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
10953 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
10954 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
10955 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
10956 << getListOfPossibleValues(OMPC_schedule,
10957 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
10958 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
10959 Excluded)
10960 << getOpenMPClauseName(OMPC_schedule);
10961 return true;
10962 }
10963 return false;
10964}
10965
Alexey Bataev56dafe82014-06-20 07:16:17 +000010966OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +000010967 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +000010968 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +000010969 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
10970 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
10971 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
10972 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
10973 return nullptr;
10974 // OpenMP, 2.7.1, Loop Construct, Restrictions
10975 // Either the monotonic modifier or the nonmonotonic modifier can be specified
10976 // but not both.
10977 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
10978 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
10979 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
10980 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
10981 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
10982 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
10983 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
10984 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
10985 return nullptr;
10986 }
Alexey Bataev56dafe82014-06-20 07:16:17 +000010987 if (Kind == OMPC_SCHEDULE_unknown) {
10988 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +000010989 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
10990 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
10991 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
10992 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
10993 Exclude);
10994 } else {
10995 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
10996 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +000010997 }
10998 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
10999 << Values << getOpenMPClauseName(OMPC_schedule);
11000 return nullptr;
11001 }
Alexey Bataev6402bca2015-12-28 07:25:51 +000011002 // OpenMP, 2.7.1, Loop Construct, Restrictions
11003 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
11004 // schedule(guided).
11005 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
11006 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
11007 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
11008 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
11009 diag::err_omp_schedule_nonmonotonic_static);
11010 return nullptr;
11011 }
Alexey Bataev56dafe82014-06-20 07:16:17 +000011012 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000011013 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +000011014 if (ChunkSize) {
11015 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
11016 !ChunkSize->isInstantiationDependent() &&
11017 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011018 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Alexey Bataev56dafe82014-06-20 07:16:17 +000011019 ExprResult Val =
11020 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
11021 if (Val.isInvalid())
11022 return nullptr;
11023
11024 ValExpr = Val.get();
11025
11026 // OpenMP [2.7.1, Restrictions]
11027 // chunk_size must be a loop invariant integer expression with a positive
11028 // value.
11029 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +000011030 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
11031 if (Result.isSigned() && !Result.isStrictlyPositive()) {
11032 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +000011033 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +000011034 return nullptr;
11035 }
Alexey Bataev2ba67042017-11-28 21:11:44 +000011036 } else if (getOpenMPCaptureRegionForClause(
11037 DSAStack->getCurrentDirective(), OMPC_schedule) !=
11038 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +000011039 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000011040 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000011041 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +000011042 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11043 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +000011044 }
11045 }
11046 }
11047
Alexey Bataev6402bca2015-12-28 07:25:51 +000011048 return new (Context)
11049 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +000011050 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +000011051}
11052
Alexey Bataev142e1fc2014-06-20 09:44:06 +000011053OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
11054 SourceLocation StartLoc,
11055 SourceLocation EndLoc) {
11056 OMPClause *Res = nullptr;
11057 switch (Kind) {
11058 case OMPC_ordered:
11059 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
11060 break;
Alexey Bataev236070f2014-06-20 11:19:47 +000011061 case OMPC_nowait:
11062 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
11063 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +000011064 case OMPC_untied:
11065 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
11066 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +000011067 case OMPC_mergeable:
11068 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
11069 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +000011070 case OMPC_read:
11071 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
11072 break;
Alexey Bataevdea47612014-07-23 07:46:59 +000011073 case OMPC_write:
11074 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
11075 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +000011076 case OMPC_update:
11077 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
11078 break;
Alexey Bataev459dec02014-07-24 06:46:57 +000011079 case OMPC_capture:
11080 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
11081 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +000011082 case OMPC_seq_cst:
11083 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
11084 break;
Alexey Bataev346265e2015-09-25 10:37:12 +000011085 case OMPC_threads:
11086 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
11087 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +000011088 case OMPC_simd:
11089 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
11090 break;
Alexey Bataevb825de12015-12-07 10:51:44 +000011091 case OMPC_nogroup:
11092 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
11093 break;
Kelvin Li1408f912018-09-26 04:28:39 +000011094 case OMPC_unified_address:
11095 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
11096 break;
Patrick Lyster4a370b92018-10-01 13:47:43 +000011097 case OMPC_unified_shared_memory:
11098 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
11099 break;
Patrick Lyster6bdf63b2018-10-03 20:07:58 +000011100 case OMPC_reverse_offload:
11101 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc);
11102 break;
Patrick Lyster3fe9e392018-10-11 14:41:10 +000011103 case OMPC_dynamic_allocators:
11104 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
11105 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +000011106 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +000011107 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +000011108 case OMPC_num_threads:
11109 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +000011110 case OMPC_simdlen:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000011111 case OMPC_allocator:
Alexey Bataev142e1fc2014-06-20 09:44:06 +000011112 case OMPC_collapse:
11113 case OMPC_schedule:
11114 case OMPC_private:
11115 case OMPC_firstprivate:
11116 case OMPC_lastprivate:
11117 case OMPC_shared:
11118 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +000011119 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +000011120 case OMPC_in_reduction:
Alexey Bataev142e1fc2014-06-20 09:44:06 +000011121 case OMPC_linear:
11122 case OMPC_aligned:
11123 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +000011124 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +000011125 case OMPC_default:
11126 case OMPC_proc_bind:
11127 case OMPC_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +000011128 case OMPC_allocate:
Alexey Bataev6125da92014-07-21 11:26:11 +000011129 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011130 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +000011131 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +000011132 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +000011133 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011134 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +000011135 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000011136 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +000011137 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +000011138 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +000011139 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011140 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +000011141 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +000011142 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +000011143 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +000011144 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +000011145 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +000011146 case OMPC_is_device_ptr:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +000011147 case OMPC_atomic_default_mem_order:
Alexey Bataev729e2422019-08-23 16:11:14 +000011148 case OMPC_device_type:
Alexey Bataev142e1fc2014-06-20 09:44:06 +000011149 llvm_unreachable("Clause is not allowed.");
11150 }
11151 return Res;
11152}
11153
Alexey Bataev236070f2014-06-20 11:19:47 +000011154OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
11155 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +000011156 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +000011157 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
11158}
11159
Alexey Bataev7aea99a2014-07-17 12:19:31 +000011160OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
11161 SourceLocation EndLoc) {
11162 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
11163}
11164
Alexey Bataev74ba3a52014-07-17 12:47:03 +000011165OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
11166 SourceLocation EndLoc) {
11167 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
11168}
11169
Alexey Bataevf98b00c2014-07-23 02:27:21 +000011170OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
11171 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +000011172 return new (Context) OMPReadClause(StartLoc, EndLoc);
11173}
11174
Alexey Bataevdea47612014-07-23 07:46:59 +000011175OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
11176 SourceLocation EndLoc) {
11177 return new (Context) OMPWriteClause(StartLoc, EndLoc);
11178}
11179
Alexey Bataev67a4f222014-07-23 10:25:33 +000011180OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
11181 SourceLocation EndLoc) {
11182 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
11183}
11184
Alexey Bataev459dec02014-07-24 06:46:57 +000011185OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
11186 SourceLocation EndLoc) {
11187 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
11188}
11189
Alexey Bataev82bad8b2014-07-24 08:55:34 +000011190OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
11191 SourceLocation EndLoc) {
11192 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
11193}
11194
Alexey Bataev346265e2015-09-25 10:37:12 +000011195OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
11196 SourceLocation EndLoc) {
11197 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
11198}
11199
Alexey Bataevd14d1e62015-09-28 06:39:35 +000011200OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
11201 SourceLocation EndLoc) {
11202 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
11203}
11204
Alexey Bataevb825de12015-12-07 10:51:44 +000011205OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
11206 SourceLocation EndLoc) {
11207 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
11208}
11209
Kelvin Li1408f912018-09-26 04:28:39 +000011210OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
11211 SourceLocation EndLoc) {
11212 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc);
11213}
11214
Patrick Lyster4a370b92018-10-01 13:47:43 +000011215OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
11216 SourceLocation EndLoc) {
11217 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
11218}
11219
Patrick Lyster6bdf63b2018-10-03 20:07:58 +000011220OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc,
11221 SourceLocation EndLoc) {
11222 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc);
11223}
11224
Patrick Lyster3fe9e392018-10-11 14:41:10 +000011225OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
11226 SourceLocation EndLoc) {
11227 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
11228}
11229
Alexey Bataevc5e02582014-06-16 07:08:35 +000011230OMPClause *Sema::ActOnOpenMPVarListClause(
11231 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
Michael Kruse4304e9d2019-02-19 16:38:20 +000011232 const OMPVarListLocTy &Locs, SourceLocation ColonLoc,
11233 CXXScopeSpec &ReductionOrMapperIdScopeSpec,
11234 DeclarationNameInfo &ReductionOrMapperId, OpenMPDependClauseKind DepKind,
Kelvin Lief579432018-12-18 22:18:41 +000011235 OpenMPLinearClauseKind LinKind,
11236 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
Michael Kruse4304e9d2019-02-19 16:38:20 +000011237 ArrayRef<SourceLocation> MapTypeModifiersLoc, OpenMPMapClauseKind MapType,
11238 bool IsMapTypeImplicit, SourceLocation DepLinMapLoc) {
11239 SourceLocation StartLoc = Locs.StartLoc;
11240 SourceLocation LParenLoc = Locs.LParenLoc;
11241 SourceLocation EndLoc = Locs.EndLoc;
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011242 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011243 switch (Kind) {
11244 case OMPC_private:
11245 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
11246 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +000011247 case OMPC_firstprivate:
11248 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
11249 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +000011250 case OMPC_lastprivate:
11251 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
11252 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +000011253 case OMPC_shared:
11254 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
11255 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011256 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +000011257 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
Michael Kruse4304e9d2019-02-19 16:38:20 +000011258 EndLoc, ReductionOrMapperIdScopeSpec,
11259 ReductionOrMapperId);
Alexey Bataevc5e02582014-06-16 07:08:35 +000011260 break;
Alexey Bataev169d96a2017-07-18 20:17:46 +000011261 case OMPC_task_reduction:
11262 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
Michael Kruse4304e9d2019-02-19 16:38:20 +000011263 EndLoc, ReductionOrMapperIdScopeSpec,
11264 ReductionOrMapperId);
Alexey Bataev169d96a2017-07-18 20:17:46 +000011265 break;
Alexey Bataevfa312f32017-07-21 18:48:21 +000011266 case OMPC_in_reduction:
Michael Kruse4304e9d2019-02-19 16:38:20 +000011267 Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
11268 EndLoc, ReductionOrMapperIdScopeSpec,
11269 ReductionOrMapperId);
Alexey Bataevfa312f32017-07-21 18:48:21 +000011270 break;
Alexander Musman8dba6642014-04-22 13:09:42 +000011271 case OMPC_linear:
11272 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +000011273 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +000011274 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011275 case OMPC_aligned:
11276 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
11277 ColonLoc, EndLoc);
11278 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011279 case OMPC_copyin:
11280 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
11281 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011282 case OMPC_copyprivate:
11283 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
11284 break;
Alexey Bataev6125da92014-07-21 11:26:11 +000011285 case OMPC_flush:
11286 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
11287 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011288 case OMPC_depend:
David Majnemer9d168222016-08-05 17:44:54 +000011289 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
Kelvin Li0bff7af2015-11-23 05:32:03 +000011290 StartLoc, LParenLoc, EndLoc);
11291 break;
11292 case OMPC_map:
Michael Kruse4304e9d2019-02-19 16:38:20 +000011293 Res = ActOnOpenMPMapClause(MapTypeModifiers, MapTypeModifiersLoc,
11294 ReductionOrMapperIdScopeSpec,
11295 ReductionOrMapperId, MapType, IsMapTypeImplicit,
11296 DepLinMapLoc, ColonLoc, VarList, Locs);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011297 break;
Samuel Antao661c0902016-05-26 17:39:58 +000011298 case OMPC_to:
Michael Kruse01f670d2019-02-22 22:29:42 +000011299 Res = ActOnOpenMPToClause(VarList, ReductionOrMapperIdScopeSpec,
11300 ReductionOrMapperId, Locs);
Samuel Antao661c0902016-05-26 17:39:58 +000011301 break;
Samuel Antaoec172c62016-05-26 17:49:04 +000011302 case OMPC_from:
Michael Kruse0336c752019-02-25 20:34:15 +000011303 Res = ActOnOpenMPFromClause(VarList, ReductionOrMapperIdScopeSpec,
11304 ReductionOrMapperId, Locs);
Samuel Antaoec172c62016-05-26 17:49:04 +000011305 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +000011306 case OMPC_use_device_ptr:
Michael Kruse4304e9d2019-02-19 16:38:20 +000011307 Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs);
Carlo Bertolli2404b172016-07-13 15:37:16 +000011308 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +000011309 case OMPC_is_device_ptr:
Michael Kruse4304e9d2019-02-19 16:38:20 +000011310 Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs);
Carlo Bertolli70594e92016-07-13 17:16:49 +000011311 break;
Alexey Bataeve04483e2019-03-27 14:14:31 +000011312 case OMPC_allocate:
11313 Res = ActOnOpenMPAllocateClause(TailExpr, VarList, StartLoc, LParenLoc,
11314 ColonLoc, EndLoc);
11315 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +000011316 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +000011317 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +000011318 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +000011319 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +000011320 case OMPC_simdlen:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +000011321 case OMPC_allocator:
Alexander Musman8bd31e62014-05-27 15:12:19 +000011322 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011323 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +000011324 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +000011325 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +000011326 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +000011327 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +000011328 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +000011329 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011330 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +000011331 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +000011332 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +000011333 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +000011334 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +000011335 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +000011336 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +000011337 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +000011338 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +000011339 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011340 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +000011341 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000011342 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +000011343 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +000011344 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +000011345 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +000011346 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011347 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011348 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +000011349 case OMPC_uniform:
Kelvin Li1408f912018-09-26 04:28:39 +000011350 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +000011351 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +000011352 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +000011353 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +000011354 case OMPC_atomic_default_mem_order:
Alexey Bataev729e2422019-08-23 16:11:14 +000011355 case OMPC_device_type:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011356 llvm_unreachable("Clause is not allowed.");
11357 }
11358 return Res;
11359}
11360
Alexey Bataev90c228f2016-02-08 09:29:13 +000011361ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +000011362 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +000011363 ExprResult Res = BuildDeclRefExpr(
11364 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
11365 if (!Res.isUsable())
11366 return ExprError();
11367 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
11368 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
11369 if (!Res.isUsable())
11370 return ExprError();
11371 }
11372 if (VK != VK_LValue && Res.get()->isGLValue()) {
11373 Res = DefaultLvalueConversion(Res.get());
11374 if (!Res.isUsable())
11375 return ExprError();
11376 }
11377 return Res;
11378}
11379
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011380OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
11381 SourceLocation StartLoc,
11382 SourceLocation LParenLoc,
11383 SourceLocation EndLoc) {
11384 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +000011385 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +000011386 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011387 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000011388 SourceLocation ELoc;
11389 SourceRange ERange;
11390 Expr *SimpleRefExpr = RefExpr;
11391 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +000011392 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011393 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011394 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +000011395 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011396 }
Alexey Bataevd985eda2016-02-10 11:29:16 +000011397 ValueDecl *D = Res.first;
11398 if (!D)
11399 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011400
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000011401 QualType Type = D->getType();
11402 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011403
11404 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
11405 // A variable that appears in a private clause must not have an incomplete
11406 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000011407 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011408 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011409 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011410
Joel E. Dennye6234d1422019-01-04 22:11:31 +000011411 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
11412 // A variable that is privatized must not have a const-qualified type
11413 // unless it is of class type with a mutable member. This restriction does
11414 // not apply to the firstprivate clause.
11415 //
11416 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions]
11417 // A variable that appears in a private clause must not have a
11418 // const-qualified type unless it is of class type with a mutable member.
Joel E. Dennyd2649292019-01-04 22:11:56 +000011419 if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc))
Joel E. Dennye6234d1422019-01-04 22:11:31 +000011420 continue;
11421
Alexey Bataev758e55e2013-09-06 18:03:48 +000011422 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
11423 // in a Construct]
11424 // Variables with the predetermined data-sharing attributes may not be
11425 // listed in data-sharing attributes clauses, except for the cases
11426 // listed below. For these exceptions only, listing a predetermined
11427 // variable in a data-sharing attribute clause is allowed and overrides
11428 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +000011429 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev758e55e2013-09-06 18:03:48 +000011430 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011431 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
11432 << getOpenMPClauseName(OMPC_private);
Alexey Bataeve3727102018-04-18 15:57:46 +000011433 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +000011434 continue;
11435 }
11436
Alexey Bataeve3727102018-04-18 15:57:46 +000011437 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011438 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000011439 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Kelvin Libf594a52016-12-17 05:48:59 +000011440 isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011441 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
11442 << getOpenMPClauseName(OMPC_private) << Type
Kelvin Libf594a52016-12-17 05:48:59 +000011443 << getOpenMPDirectiveName(CurrDir);
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011444 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000011445 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011446 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000011447 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011448 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000011449 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011450 continue;
11451 }
11452
Carlo Bertollib74bfc82016-03-18 21:43:32 +000011453 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
11454 // A list item cannot appear in both a map clause and a data-sharing
11455 // attribute clause on the same construct
Joel E. Denny7d5bc552019-08-22 03:34:30 +000011456 //
11457 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
11458 // A list item cannot appear in both a map clause and a data-sharing
11459 // attribute clause on the same construct unless the construct is a
11460 // combined construct.
11461 if ((LangOpts.OpenMP <= 45 && isOpenMPTargetExecutionDirective(CurrDir)) ||
11462 CurrDir == OMPD_target) {
Samuel Antao6890b092016-07-28 14:25:09 +000011463 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +000011464 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000011465 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +000011466 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
11467 OpenMPClauseKind WhereFoundClauseKind) -> bool {
11468 ConflictKind = WhereFoundClauseKind;
11469 return true;
11470 })) {
11471 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +000011472 << getOpenMPClauseName(OMPC_private)
Samuel Antao6890b092016-07-28 14:25:09 +000011473 << getOpenMPClauseName(ConflictKind)
Kelvin Libf594a52016-12-17 05:48:59 +000011474 << getOpenMPDirectiveName(CurrDir);
Alexey Bataeve3727102018-04-18 15:57:46 +000011475 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000011476 continue;
11477 }
11478 }
11479
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011480 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
11481 // A variable of class type (or array thereof) that appears in a private
11482 // clause requires an accessible, unambiguous default constructor for the
11483 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +000011484 // Generate helper private variable and initialize it with the default
11485 // value. The address of the original variable is replaced by the address of
11486 // the new private variable in CodeGen. This new variable is not added to
11487 // IdResolver, so the code in the OpenMP region uses original variable for
11488 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011489 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011490 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000011491 buildVarDecl(*this, ELoc, Type, D->getName(),
11492 D->hasAttrs() ? &D->getAttrs() : nullptr,
11493 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Richard Smith3beb7c62017-01-12 02:27:38 +000011494 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev03b340a2014-10-21 03:16:40 +000011495 if (VDPrivate->isInvalidDecl())
11496 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +000011497 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000011498 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +000011499
Alexey Bataev90c228f2016-02-08 09:29:13 +000011500 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011501 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +000011502 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +000011503 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011504 Vars.push_back((VD || CurContext->isDependentContext())
11505 ? RefExpr->IgnoreParens()
11506 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +000011507 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011508 }
11509
Alexey Bataeved09d242014-05-28 05:53:51 +000011510 if (Vars.empty())
11511 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011512
Alexey Bataev03b340a2014-10-21 03:16:40 +000011513 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
11514 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000011515}
11516
Alexey Bataev4a5bb772014-10-08 14:01:46 +000011517namespace {
11518class DiagsUninitializedSeveretyRAII {
11519private:
11520 DiagnosticsEngine &Diags;
11521 SourceLocation SavedLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +000011522 bool IsIgnored = false;
Alexey Bataev4a5bb772014-10-08 14:01:46 +000011523
11524public:
11525 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
11526 bool IsIgnored)
11527 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
11528 if (!IsIgnored) {
11529 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
11530 /*Map*/ diag::Severity::Ignored, Loc);
11531 }
11532 }
11533 ~DiagsUninitializedSeveretyRAII() {
11534 if (!IsIgnored)
11535 Diags.popMappings(SavedLoc);
11536 }
11537};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000011538}
Alexey Bataev4a5bb772014-10-08 14:01:46 +000011539
Alexey Bataevd5af8e42013-10-01 05:32:34 +000011540OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
11541 SourceLocation StartLoc,
11542 SourceLocation LParenLoc,
11543 SourceLocation EndLoc) {
11544 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +000011545 SmallVector<Expr *, 8> PrivateCopies;
11546 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +000011547 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000011548 bool IsImplicitClause =
11549 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
Alexey Bataeve3727102018-04-18 15:57:46 +000011550 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000011551
Alexey Bataeve3727102018-04-18 15:57:46 +000011552 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011553 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000011554 SourceLocation ELoc;
11555 SourceRange ERange;
11556 Expr *SimpleRefExpr = RefExpr;
11557 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +000011558 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +000011559 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011560 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000011561 PrivateCopies.push_back(nullptr);
11562 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000011563 }
Alexey Bataevd985eda2016-02-10 11:29:16 +000011564 ValueDecl *D = Res.first;
11565 if (!D)
11566 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +000011567
Alexey Bataev60da77e2016-02-29 05:54:20 +000011568 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +000011569 QualType Type = D->getType();
11570 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000011571
11572 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
11573 // A variable that appears in a private clause must not have an incomplete
11574 // type or a reference type.
11575 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +000011576 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +000011577 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011578 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +000011579
11580 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
11581 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +000011582 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +000011583 // class type.
Alexey Bataeve3727102018-04-18 15:57:46 +000011584 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +000011585
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000011586 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +000011587 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000011588 if (!IsImplicitClause) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011589 DSAStackTy::DSAVarData DVar =
11590 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +000011591 TopDVar = DVar;
Alexey Bataeveffbdf12017-07-21 17:24:30 +000011592 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011593 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000011594 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
11595 // A list item that specifies a given variable may not appear in more
11596 // than one clause on the same directive, except that a variable may be
11597 // specified in both firstprivate and lastprivate clauses.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000011598 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
11599 // A list item may appear in a firstprivate or lastprivate clause but not
11600 // both.
Alexey Bataevd5af8e42013-10-01 05:32:34 +000011601 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +000011602 (isOpenMPDistributeDirective(CurrDir) ||
11603 DVar.CKind != OMPC_lastprivate) &&
Alexey Bataeveffbdf12017-07-21 17:24:30 +000011604 DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +000011605 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +000011606 << getOpenMPClauseName(DVar.CKind)
11607 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000011608 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000011609 continue;
11610 }
11611
11612 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
11613 // in a Construct]
11614 // Variables with the predetermined data-sharing attributes may not be
11615 // listed in data-sharing attributes clauses, except for the cases
11616 // listed below. For these exceptions only, listing a predetermined
11617 // variable in a data-sharing attribute clause is allowed and overrides
11618 // the variable's predetermined data-sharing attributes.
11619 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
11620 // in a Construct, C/C++, p.2]
11621 // Variables with const-qualified type having no mutable member may be
11622 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +000011623 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +000011624 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
11625 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +000011626 << getOpenMPClauseName(DVar.CKind)
11627 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000011628 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000011629 continue;
11630 }
11631
11632 // OpenMP [2.9.3.4, Restrictions, p.2]
11633 // A list item that is private within a parallel region must not appear
11634 // in a firstprivate clause on a worksharing construct if any of the
11635 // worksharing regions arising from the worksharing construct ever bind
11636 // to any of the parallel regions arising from the parallel construct.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000011637 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
11638 // A list item that is private within a teams region must not appear in a
11639 // firstprivate clause on a distribute construct if any of the distribute
11640 // regions arising from the distribute construct ever bind to any of the
11641 // teams regions arising from the teams construct.
11642 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
11643 // A list item that appears in a reduction clause of a teams construct
11644 // must not appear in a firstprivate clause on a distribute construct if
11645 // any of the distribute regions arising from the distribute construct
11646 // ever bind to any of the teams regions arising from the teams construct.
11647 if ((isOpenMPWorksharingDirective(CurrDir) ||
11648 isOpenMPDistributeDirective(CurrDir)) &&
Kelvin Li579e41c2016-11-30 23:51:03 +000011649 !isOpenMPParallelDirective(CurrDir) &&
11650 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +000011651 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000011652 if (DVar.CKind != OMPC_shared &&
11653 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +000011654 isOpenMPTeamsDirective(DVar.DKind) ||
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000011655 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +000011656 Diag(ELoc, diag::err_omp_required_access)
11657 << getOpenMPClauseName(OMPC_firstprivate)
11658 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000011659 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +000011660 continue;
11661 }
11662 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +000011663 // OpenMP [2.9.3.4, Restrictions, p.3]
11664 // A list item that appears in a reduction clause of a parallel construct
11665 // must not appear in a firstprivate clause on a worksharing or task
11666 // construct if any of the worksharing or task regions arising from the
11667 // worksharing or task construct ever bind to any of the parallel regions
11668 // arising from the parallel construct.
11669 // OpenMP [2.9.3.4, Restrictions, p.4]
11670 // A list item that appears in a reduction clause in worksharing
11671 // construct must not appear in a firstprivate clause in a task construct
11672 // encountered during execution of any of the worksharing regions arising
11673 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +000011674 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000011675 DVar = DSAStack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +000011676 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
11677 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000011678 return isOpenMPParallelDirective(K) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +000011679 isOpenMPWorksharingDirective(K) ||
11680 isOpenMPTeamsDirective(K);
Alexey Bataev7ace49d2016-05-17 08:55:33 +000011681 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +000011682 /*FromParent=*/true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000011683 if (DVar.CKind == OMPC_reduction &&
11684 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +000011685 isOpenMPWorksharingDirective(DVar.DKind) ||
11686 isOpenMPTeamsDirective(DVar.DKind))) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000011687 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
11688 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataeve3727102018-04-18 15:57:46 +000011689 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000011690 continue;
11691 }
11692 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +000011693
Carlo Bertollib74bfc82016-03-18 21:43:32 +000011694 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
11695 // A list item cannot appear in both a map clause and a data-sharing
11696 // attribute clause on the same construct
Joel E. Denny7d5bc552019-08-22 03:34:30 +000011697 //
11698 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
11699 // A list item cannot appear in both a map clause and a data-sharing
11700 // attribute clause on the same construct unless the construct is a
11701 // combined construct.
11702 if ((LangOpts.OpenMP <= 45 &&
11703 isOpenMPTargetExecutionDirective(CurrDir)) ||
11704 CurrDir == OMPD_target) {
Samuel Antao6890b092016-07-28 14:25:09 +000011705 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +000011706 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000011707 VD, /*CurrentRegionOnly=*/true,
Alexey Bataeve3727102018-04-18 15:57:46 +000011708 [&ConflictKind](
11709 OMPClauseMappableExprCommon::MappableExprComponentListRef,
11710 OpenMPClauseKind WhereFoundClauseKind) {
Samuel Antao6890b092016-07-28 14:25:09 +000011711 ConflictKind = WhereFoundClauseKind;
11712 return true;
11713 })) {
11714 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +000011715 << getOpenMPClauseName(OMPC_firstprivate)
Samuel Antao6890b092016-07-28 14:25:09 +000011716 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +000011717 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000011718 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000011719 continue;
11720 }
11721 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +000011722 }
11723
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011724 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000011725 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +000011726 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011727 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
11728 << getOpenMPClauseName(OMPC_firstprivate) << Type
11729 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
11730 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +000011731 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011732 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +000011733 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011734 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +000011735 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011736 continue;
11737 }
11738
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011739 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011740 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000011741 buildVarDecl(*this, ELoc, Type, D->getName(),
11742 D->hasAttrs() ? &D->getAttrs() : nullptr,
11743 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000011744 // Generate helper private variable and initialize it with the value of the
11745 // original variable. The address of the original variable is replaced by
11746 // the address of the new private variable in the CodeGen. This new variable
11747 // is not added to IdResolver, so the code in the OpenMP region uses
11748 // original variable for proper diagnostics and variable capturing.
11749 Expr *VDInitRefExpr = nullptr;
11750 // For arrays generate initializer for single element and replace it by the
11751 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011752 if (Type->isArrayType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011753 VarDecl *VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +000011754 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011755 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +000011756 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011757 ElemType = ElemType.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011758 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
11759 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +000011760 InitializedEntity Entity =
11761 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000011762 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
11763
11764 InitializationSequence InitSeq(*this, Entity, Kind, Init);
11765 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
11766 if (Result.isInvalid())
11767 VDPrivate->setInvalidDecl();
11768 else
11769 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011770 // Remove temp variable declaration.
11771 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000011772 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +000011773 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
11774 ".firstprivate.temp");
Alexey Bataevd985eda2016-02-10 11:29:16 +000011775 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
11776 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +000011777 AddInitializerToDecl(VDPrivate,
11778 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000011779 /*DirectInit=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000011780 }
11781 if (VDPrivate->isInvalidDecl()) {
11782 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +000011783 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +000011784 diag::note_omp_task_predetermined_firstprivate_here);
11785 }
11786 continue;
11787 }
11788 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000011789 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +000011790 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
11791 RefExpr->getExprLoc());
11792 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011793 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011794 if (TopDVar.CKind == OMPC_lastprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000011795 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000011796 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000011797 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000011798 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000011799 ExprCaptures.push_back(Ref->getDecl());
11800 }
Alexey Bataev417089f2016-02-17 13:19:37 +000011801 }
Alexey Bataevd985eda2016-02-10 11:29:16 +000011802 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011803 Vars.push_back((VD || CurContext->isDependentContext())
11804 ? RefExpr->IgnoreParens()
11805 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000011806 PrivateCopies.push_back(VDPrivateRefExpr);
11807 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000011808 }
11809
Alexey Bataeved09d242014-05-28 05:53:51 +000011810 if (Vars.empty())
11811 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +000011812
11813 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +000011814 Vars, PrivateCopies, Inits,
11815 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +000011816}
11817
Alexander Musman1bb328c2014-06-04 13:06:39 +000011818OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
11819 SourceLocation StartLoc,
11820 SourceLocation LParenLoc,
11821 SourceLocation EndLoc) {
11822 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +000011823 SmallVector<Expr *, 8> SrcExprs;
11824 SmallVector<Expr *, 8> DstExprs;
11825 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +000011826 SmallVector<Decl *, 4> ExprCaptures;
11827 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeve3727102018-04-18 15:57:46 +000011828 for (Expr *RefExpr : VarList) {
Alexander Musman1bb328c2014-06-04 13:06:39 +000011829 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000011830 SourceLocation ELoc;
11831 SourceRange ERange;
11832 Expr *SimpleRefExpr = RefExpr;
11833 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +000011834 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +000011835 // It will be analyzed later.
11836 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +000011837 SrcExprs.push_back(nullptr);
11838 DstExprs.push_back(nullptr);
11839 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +000011840 }
Alexey Bataev74caaf22016-02-20 04:09:36 +000011841 ValueDecl *D = Res.first;
11842 if (!D)
11843 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000011844
Alexey Bataev74caaf22016-02-20 04:09:36 +000011845 QualType Type = D->getType();
11846 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +000011847
11848 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
11849 // A variable that appears in a lastprivate clause must not have an
11850 // incomplete type or a reference type.
11851 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +000011852 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +000011853 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011854 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +000011855
Joel E. Dennye6234d1422019-01-04 22:11:31 +000011856 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
11857 // A variable that is privatized must not have a const-qualified type
11858 // unless it is of class type with a mutable member. This restriction does
11859 // not apply to the firstprivate clause.
11860 //
11861 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions]
11862 // A variable that appears in a lastprivate clause must not have a
11863 // const-qualified type unless it is of class type with a mutable member.
Joel E. Dennyd2649292019-01-04 22:11:56 +000011864 if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc))
Joel E. Dennye6234d1422019-01-04 22:11:31 +000011865 continue;
11866
Alexey Bataeveffbdf12017-07-21 17:24:30 +000011867 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexander Musman1bb328c2014-06-04 13:06:39 +000011868 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
11869 // in a Construct]
11870 // Variables with the predetermined data-sharing attributes may not be
11871 // listed in data-sharing attributes clauses, except for the cases
11872 // listed below.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000011873 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
11874 // A list item may appear in a firstprivate or lastprivate clause but not
11875 // both.
Alexey Bataeve3727102018-04-18 15:57:46 +000011876 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman1bb328c2014-06-04 13:06:39 +000011877 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +000011878 (isOpenMPDistributeDirective(CurrDir) ||
11879 DVar.CKind != OMPC_firstprivate) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +000011880 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
11881 Diag(ELoc, diag::err_omp_wrong_dsa)
11882 << getOpenMPClauseName(DVar.CKind)
11883 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000011884 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +000011885 continue;
11886 }
11887
Alexey Bataevf29276e2014-06-18 04:14:57 +000011888 // OpenMP [2.14.3.5, Restrictions, p.2]
11889 // A list item that is private within a parallel region, or that appears in
11890 // the reduction clause of a parallel construct, must not appear in a
11891 // lastprivate clause on a worksharing construct if any of the corresponding
11892 // worksharing regions ever binds to any of the corresponding parallel
11893 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +000011894 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +000011895 if (isOpenMPWorksharingDirective(CurrDir) &&
Kelvin Li579e41c2016-11-30 23:51:03 +000011896 !isOpenMPParallelDirective(CurrDir) &&
11897 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +000011898 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +000011899 if (DVar.CKind != OMPC_shared) {
11900 Diag(ELoc, diag::err_omp_required_access)
11901 << getOpenMPClauseName(OMPC_lastprivate)
11902 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000011903 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +000011904 continue;
11905 }
11906 }
Alexey Bataev74caaf22016-02-20 04:09:36 +000011907
Alexander Musman1bb328c2014-06-04 13:06:39 +000011908 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +000011909 // A variable of class type (or array thereof) that appears in a
11910 // lastprivate clause requires an accessible, unambiguous default
11911 // constructor for the class type, unless the list item is also specified
11912 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +000011913 // A variable of class type (or array thereof) that appears in a
11914 // lastprivate clause requires an accessible, unambiguous copy assignment
11915 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +000011916 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011917 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(),
11918 Type.getUnqualifiedType(), ".lastprivate.src",
11919 D->hasAttrs() ? &D->getAttrs() : nullptr);
11920 DeclRefExpr *PseudoSrcExpr =
Alexey Bataev74caaf22016-02-20 04:09:36 +000011921 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +000011922 VarDecl *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +000011923 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +000011924 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011925 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +000011926 // For arrays generate assignment operation for single element and replace
11927 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000011928 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
11929 PseudoDstExpr, PseudoSrcExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +000011930 if (AssignmentOp.isInvalid())
11931 continue;
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000011932 AssignmentOp =
11933 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev38e89532015-04-16 04:54:05 +000011934 if (AssignmentOp.isInvalid())
11935 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000011936
Alexey Bataev74caaf22016-02-20 04:09:36 +000011937 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011938 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011939 if (TopDVar.CKind == OMPC_firstprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000011940 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000011941 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000011942 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011943 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000011944 ExprCaptures.push_back(Ref->getDecl());
11945 }
11946 if (TopDVar.CKind == OMPC_firstprivate ||
Alexey Bataeve3727102018-04-18 15:57:46 +000011947 (!isOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011948 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +000011949 ExprResult RefRes = DefaultLvalueConversion(Ref);
11950 if (!RefRes.isUsable())
11951 continue;
11952 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +000011953 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
11954 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +000011955 if (!PostUpdateRes.isUsable())
11956 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +000011957 ExprPostUpdates.push_back(
11958 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +000011959 }
11960 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +000011961 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011962 Vars.push_back((VD || CurContext->isDependentContext())
11963 ? RefExpr->IgnoreParens()
11964 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +000011965 SrcExprs.push_back(PseudoSrcExpr);
11966 DstExprs.push_back(PseudoDstExpr);
11967 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +000011968 }
11969
11970 if (Vars.empty())
11971 return nullptr;
11972
11973 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +000011974 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +000011975 buildPreInits(Context, ExprCaptures),
11976 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +000011977}
11978
Alexey Bataev758e55e2013-09-06 18:03:48 +000011979OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
11980 SourceLocation StartLoc,
11981 SourceLocation LParenLoc,
11982 SourceLocation EndLoc) {
11983 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000011984 for (Expr *RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +000011985 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000011986 SourceLocation ELoc;
11987 SourceRange ERange;
11988 Expr *SimpleRefExpr = RefExpr;
11989 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000011990 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +000011991 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011992 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +000011993 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +000011994 ValueDecl *D = Res.first;
11995 if (!D)
11996 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +000011997
Alexey Bataevb7a34b62016-02-25 03:59:29 +000011998 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +000011999 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
12000 // in a Construct]
12001 // Variables with the predetermined data-sharing attributes may not be
12002 // listed in data-sharing attributes clauses, except for the cases
12003 // listed below. For these exceptions only, listing a predetermined
12004 // variable in a data-sharing attribute clause is allowed and overrides
12005 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +000012006 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeved09d242014-05-28 05:53:51 +000012007 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
12008 DVar.RefExpr) {
12009 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
12010 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000012011 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +000012012 continue;
12013 }
12014
Alexey Bataevb7a34b62016-02-25 03:59:29 +000012015 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000012016 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +000012017 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000012018 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000012019 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
12020 ? RefExpr->IgnoreParens()
12021 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +000012022 }
12023
Alexey Bataeved09d242014-05-28 05:53:51 +000012024 if (Vars.empty())
12025 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000012026
12027 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
12028}
12029
Alexey Bataevc5e02582014-06-16 07:08:35 +000012030namespace {
12031class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
12032 DSAStackTy *Stack;
12033
12034public:
12035 bool VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012036 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
12037 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevc5e02582014-06-16 07:08:35 +000012038 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
12039 return false;
12040 if (DVar.CKind != OMPC_unknown)
12041 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000012042 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +000012043 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; },
Alexey Bataeveffbdf12017-07-21 17:24:30 +000012044 /*FromParent=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000012045 return DVarPrivate.CKind != OMPC_unknown;
Alexey Bataevc5e02582014-06-16 07:08:35 +000012046 }
12047 return false;
12048 }
12049 bool VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012050 for (Stmt *Child : S->children()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000012051 if (Child && Visit(Child))
12052 return true;
12053 }
12054 return false;
12055 }
Alexey Bataev23b69422014-06-18 07:08:49 +000012056 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +000012057};
Alexey Bataev23b69422014-06-18 07:08:49 +000012058} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +000012059
Alexey Bataev60da77e2016-02-29 05:54:20 +000012060namespace {
12061// Transform MemberExpression for specified FieldDecl of current class to
12062// DeclRefExpr to specified OMPCapturedExprDecl.
12063class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
12064 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
Alexey Bataeve3727102018-04-18 15:57:46 +000012065 ValueDecl *Field = nullptr;
12066 DeclRefExpr *CapturedExpr = nullptr;
Alexey Bataev60da77e2016-02-29 05:54:20 +000012067
12068public:
12069 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
12070 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
12071
12072 ExprResult TransformMemberExpr(MemberExpr *E) {
12073 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
12074 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +000012075 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +000012076 return CapturedExpr;
12077 }
12078 return BaseTransform::TransformMemberExpr(E);
12079 }
12080 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
12081};
12082} // namespace
12083
Alexey Bataev97d18bf2018-04-11 19:21:00 +000012084template <typename T, typename U>
Michael Kruse4304e9d2019-02-19 16:38:20 +000012085static T filterLookupForUDReductionAndMapper(
12086 SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012087 for (U &Set : Lookups) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012088 for (auto *D : Set) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012089 if (T Res = Gen(cast<ValueDecl>(D)))
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012090 return Res;
12091 }
12092 }
12093 return T();
12094}
12095
Alexey Bataev43b90b72018-09-12 16:31:59 +000012096static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
12097 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
12098
12099 for (auto RD : D->redecls()) {
12100 // Don't bother with extra checks if we already know this one isn't visible.
12101 if (RD == D)
12102 continue;
12103
12104 auto ND = cast<NamedDecl>(RD);
12105 if (LookupResult::isVisible(SemaRef, ND))
12106 return ND;
12107 }
12108
12109 return nullptr;
12110}
12111
12112static void
Michael Kruse4304e9d2019-02-19 16:38:20 +000012113argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id,
Alexey Bataev43b90b72018-09-12 16:31:59 +000012114 SourceLocation Loc, QualType Ty,
12115 SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
12116 // Find all of the associated namespaces and classes based on the
12117 // arguments we have.
12118 Sema::AssociatedNamespaceSet AssociatedNamespaces;
12119 Sema::AssociatedClassSet AssociatedClasses;
12120 OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
12121 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces,
12122 AssociatedClasses);
12123
12124 // C++ [basic.lookup.argdep]p3:
12125 // Let X be the lookup set produced by unqualified lookup (3.4.1)
12126 // and let Y be the lookup set produced by argument dependent
12127 // lookup (defined as follows). If X contains [...] then Y is
12128 // empty. Otherwise Y is the set of declarations found in the
12129 // namespaces associated with the argument types as described
12130 // below. The set of declarations found by the lookup of the name
12131 // is the union of X and Y.
12132 //
12133 // Here, we compute Y and add its members to the overloaded
12134 // candidate set.
12135 for (auto *NS : AssociatedNamespaces) {
12136 // When considering an associated namespace, the lookup is the
12137 // same as the lookup performed when the associated namespace is
12138 // used as a qualifier (3.4.3.2) except that:
12139 //
12140 // -- Any using-directives in the associated namespace are
12141 // ignored.
12142 //
12143 // -- Any namespace-scope friend functions declared in
12144 // associated classes are visible within their respective
12145 // namespaces even if they are not visible during an ordinary
12146 // lookup (11.4).
Michael Kruse4304e9d2019-02-19 16:38:20 +000012147 DeclContext::lookup_result R = NS->lookup(Id.getName());
Alexey Bataev43b90b72018-09-12 16:31:59 +000012148 for (auto *D : R) {
12149 auto *Underlying = D;
12150 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
12151 Underlying = USD->getTargetDecl();
12152
Michael Kruse4304e9d2019-02-19 16:38:20 +000012153 if (!isa<OMPDeclareReductionDecl>(Underlying) &&
12154 !isa<OMPDeclareMapperDecl>(Underlying))
Alexey Bataev43b90b72018-09-12 16:31:59 +000012155 continue;
12156
12157 if (!SemaRef.isVisible(D)) {
12158 D = findAcceptableDecl(SemaRef, D);
12159 if (!D)
12160 continue;
12161 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
12162 Underlying = USD->getTargetDecl();
12163 }
12164 Lookups.emplace_back();
12165 Lookups.back().addDecl(Underlying);
12166 }
12167 }
12168}
12169
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012170static ExprResult
12171buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
12172 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
12173 const DeclarationNameInfo &ReductionId, QualType Ty,
12174 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
12175 if (ReductionIdScopeSpec.isInvalid())
12176 return ExprError();
12177 SmallVector<UnresolvedSet<8>, 4> Lookups;
12178 if (S) {
12179 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
12180 Lookup.suppressDiagnostics();
12181 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012182 NamedDecl *D = Lookup.getRepresentativeDecl();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012183 do {
12184 S = S->getParent();
12185 } while (S && !S->isDeclScope(D));
12186 if (S)
12187 S = S->getParent();
Alexey Bataev43b90b72018-09-12 16:31:59 +000012188 Lookups.emplace_back();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012189 Lookups.back().append(Lookup.begin(), Lookup.end());
12190 Lookup.clear();
12191 }
12192 } else if (auto *ULE =
12193 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
12194 Lookups.push_back(UnresolvedSet<8>());
12195 Decl *PrevD = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000012196 for (NamedDecl *D : ULE->decls()) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012197 if (D == PrevD)
12198 Lookups.push_back(UnresolvedSet<8>());
Don Hintonf170dff2019-03-19 06:14:14 +000012199 else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(D))
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012200 Lookups.back().addDecl(DRD);
12201 PrevD = D;
12202 }
12203 }
Alexey Bataevfdc20352017-08-25 15:43:55 +000012204 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
12205 Ty->isInstantiationDependentType() ||
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012206 Ty->containsUnexpandedParameterPack() ||
Michael Kruse4304e9d2019-02-19 16:38:20 +000012207 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012208 return !D->isInvalidDecl() &&
12209 (D->getType()->isDependentType() ||
12210 D->getType()->isInstantiationDependentType() ||
12211 D->getType()->containsUnexpandedParameterPack());
12212 })) {
12213 UnresolvedSet<8> ResSet;
Alexey Bataeve3727102018-04-18 15:57:46 +000012214 for (const UnresolvedSet<8> &Set : Lookups) {
Alexey Bataev43b90b72018-09-12 16:31:59 +000012215 if (Set.empty())
12216 continue;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012217 ResSet.append(Set.begin(), Set.end());
12218 // The last item marks the end of all declarations at the specified scope.
12219 ResSet.addDecl(Set[Set.size() - 1]);
12220 }
12221 return UnresolvedLookupExpr::Create(
12222 SemaRef.Context, /*NamingClass=*/nullptr,
12223 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
12224 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
12225 }
Alexey Bataev43b90b72018-09-12 16:31:59 +000012226 // Lookup inside the classes.
12227 // C++ [over.match.oper]p3:
12228 // For a unary operator @ with an operand of a type whose
12229 // cv-unqualified version is T1, and for a binary operator @ with
12230 // a left operand of a type whose cv-unqualified version is T1 and
12231 // a right operand of a type whose cv-unqualified version is T2,
12232 // three sets of candidate functions, designated member
12233 // candidates, non-member candidates and built-in candidates, are
12234 // constructed as follows:
12235 // -- If T1 is a complete class type or a class currently being
12236 // defined, the set of member candidates is the result of the
12237 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
12238 // the set of member candidates is empty.
12239 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
12240 Lookup.suppressDiagnostics();
12241 if (const auto *TyRec = Ty->getAs<RecordType>()) {
12242 // Complete the type if it can be completed.
12243 // If the type is neither complete nor being defined, bail out now.
12244 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() ||
12245 TyRec->getDecl()->getDefinition()) {
12246 Lookup.clear();
12247 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl());
12248 if (Lookup.empty()) {
12249 Lookups.emplace_back();
12250 Lookups.back().append(Lookup.begin(), Lookup.end());
12251 }
12252 }
12253 }
12254 // Perform ADL.
Alexey Bataev09232662019-04-04 17:28:22 +000012255 if (SemaRef.getLangOpts().CPlusPlus)
Alexey Bataev74a04e82019-03-13 19:31:34 +000012256 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups);
Alexey Bataev09232662019-04-04 17:28:22 +000012257 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
12258 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
12259 if (!D->isInvalidDecl() &&
12260 SemaRef.Context.hasSameType(D->getType(), Ty))
12261 return D;
12262 return nullptr;
12263 }))
12264 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(),
12265 VK_LValue, Loc);
12266 if (SemaRef.getLangOpts().CPlusPlus) {
Alexey Bataev74a04e82019-03-13 19:31:34 +000012267 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
12268 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
12269 if (!D->isInvalidDecl() &&
12270 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
12271 !Ty.isMoreQualifiedThan(D->getType()))
12272 return D;
12273 return nullptr;
12274 })) {
12275 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
12276 /*DetectVirtual=*/false);
12277 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
12278 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
12279 VD->getType().getUnqualifiedType()))) {
12280 if (SemaRef.CheckBaseClassAccess(
12281 Loc, VD->getType(), Ty, Paths.front(),
12282 /*DiagID=*/0) != Sema::AR_inaccessible) {
12283 SemaRef.BuildBasePathArray(Paths, BasePath);
12284 return SemaRef.BuildDeclRefExpr(
12285 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc);
12286 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012287 }
12288 }
12289 }
12290 }
12291 if (ReductionIdScopeSpec.isSet()) {
12292 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
12293 return ExprError();
12294 }
12295 return ExprEmpty();
12296}
12297
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012298namespace {
12299/// Data for the reduction-based clauses.
12300struct ReductionData {
12301 /// List of original reduction items.
12302 SmallVector<Expr *, 8> Vars;
12303 /// List of private copies of the reduction items.
12304 SmallVector<Expr *, 8> Privates;
12305 /// LHS expressions for the reduction_op expressions.
12306 SmallVector<Expr *, 8> LHSs;
12307 /// RHS expressions for the reduction_op expressions.
12308 SmallVector<Expr *, 8> RHSs;
12309 /// Reduction operation expression.
12310 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev88202be2017-07-27 13:20:36 +000012311 /// Taskgroup descriptors for the corresponding reduction items in
12312 /// in_reduction clauses.
12313 SmallVector<Expr *, 8> TaskgroupDescriptors;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012314 /// List of captures for clause.
12315 SmallVector<Decl *, 4> ExprCaptures;
12316 /// List of postupdate expressions.
12317 SmallVector<Expr *, 4> ExprPostUpdates;
12318 ReductionData() = delete;
12319 /// Reserves required memory for the reduction data.
12320 ReductionData(unsigned Size) {
12321 Vars.reserve(Size);
12322 Privates.reserve(Size);
12323 LHSs.reserve(Size);
12324 RHSs.reserve(Size);
12325 ReductionOps.reserve(Size);
Alexey Bataev88202be2017-07-27 13:20:36 +000012326 TaskgroupDescriptors.reserve(Size);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012327 ExprCaptures.reserve(Size);
12328 ExprPostUpdates.reserve(Size);
12329 }
12330 /// Stores reduction item and reduction operation only (required for dependent
12331 /// reduction item).
12332 void push(Expr *Item, Expr *ReductionOp) {
12333 Vars.emplace_back(Item);
12334 Privates.emplace_back(nullptr);
12335 LHSs.emplace_back(nullptr);
12336 RHSs.emplace_back(nullptr);
12337 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000012338 TaskgroupDescriptors.emplace_back(nullptr);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012339 }
12340 /// Stores reduction data.
Alexey Bataev88202be2017-07-27 13:20:36 +000012341 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
12342 Expr *TaskgroupDescriptor) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012343 Vars.emplace_back(Item);
12344 Privates.emplace_back(Private);
12345 LHSs.emplace_back(LHS);
12346 RHSs.emplace_back(RHS);
12347 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000012348 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012349 }
12350};
12351} // namespace
12352
Alexey Bataeve3727102018-04-18 15:57:46 +000012353static bool checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000012354 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
12355 SmallVectorImpl<llvm::APSInt> &ArraySizes) {
12356 const Expr *Length = OASE->getLength();
12357 if (Length == nullptr) {
12358 // For array sections of the form [1:] or [:], we would need to analyze
12359 // the lower bound...
12360 if (OASE->getColonLoc().isValid())
12361 return false;
12362
12363 // This is an array subscript which has implicit length 1!
12364 SingleElement = true;
12365 ArraySizes.push_back(llvm::APSInt::get(1));
12366 } else {
Fangrui Song407659a2018-11-30 23:41:18 +000012367 Expr::EvalResult Result;
12368 if (!Length->EvaluateAsInt(Result, Context))
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000012369 return false;
12370
Fangrui Song407659a2018-11-30 23:41:18 +000012371 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000012372 SingleElement = (ConstantLengthValue.getSExtValue() == 1);
12373 ArraySizes.push_back(ConstantLengthValue);
12374 }
12375
12376 // Get the base of this array section and walk up from there.
12377 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
12378
12379 // We require length = 1 for all array sections except the right-most to
12380 // guarantee that the memory region is contiguous and has no holes in it.
12381 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
12382 Length = TempOASE->getLength();
12383 if (Length == nullptr) {
12384 // For array sections of the form [1:] or [:], we would need to analyze
12385 // the lower bound...
12386 if (OASE->getColonLoc().isValid())
12387 return false;
12388
12389 // This is an array subscript which has implicit length 1!
12390 ArraySizes.push_back(llvm::APSInt::get(1));
12391 } else {
Fangrui Song407659a2018-11-30 23:41:18 +000012392 Expr::EvalResult Result;
12393 if (!Length->EvaluateAsInt(Result, Context))
12394 return false;
12395
12396 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
12397 if (ConstantLengthValue.getSExtValue() != 1)
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000012398 return false;
12399
12400 ArraySizes.push_back(ConstantLengthValue);
12401 }
12402 Base = TempOASE->getBase()->IgnoreParenImpCasts();
12403 }
12404
12405 // If we have a single element, we don't need to add the implicit lengths.
12406 if (!SingleElement) {
12407 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
12408 // Has implicit length 1!
12409 ArraySizes.push_back(llvm::APSInt::get(1));
12410 Base = TempASE->getBase()->IgnoreParenImpCasts();
12411 }
12412 }
12413
12414 // This array section can be privatized as a single value or as a constant
12415 // sized array.
12416 return true;
12417}
12418
Alexey Bataeve3727102018-04-18 15:57:46 +000012419static bool actOnOMPReductionKindClause(
Alexey Bataev169d96a2017-07-18 20:17:46 +000012420 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
12421 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
12422 SourceLocation ColonLoc, SourceLocation EndLoc,
12423 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012424 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012425 DeclarationName DN = ReductionId.getName();
12426 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
Alexey Bataevc5e02582014-06-16 07:08:35 +000012427 BinaryOperatorKind BOK = BO_Comma;
12428
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012429 ASTContext &Context = S.Context;
Alexey Bataevc5e02582014-06-16 07:08:35 +000012430 // OpenMP [2.14.3.6, reduction clause]
12431 // C
12432 // reduction-identifier is either an identifier or one of the following
12433 // operators: +, -, *, &, |, ^, && and ||
12434 // C++
12435 // reduction-identifier is either an id-expression or one of the following
12436 // operators: +, -, *, &, |, ^, && and ||
Alexey Bataevc5e02582014-06-16 07:08:35 +000012437 switch (OOK) {
12438 case OO_Plus:
12439 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000012440 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +000012441 break;
12442 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000012443 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +000012444 break;
12445 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000012446 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +000012447 break;
12448 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000012449 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +000012450 break;
12451 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000012452 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +000012453 break;
12454 case OO_AmpAmp:
12455 BOK = BO_LAnd;
12456 break;
12457 case OO_PipePipe:
12458 BOK = BO_LOr;
12459 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000012460 case OO_New:
12461 case OO_Delete:
12462 case OO_Array_New:
12463 case OO_Array_Delete:
12464 case OO_Slash:
12465 case OO_Percent:
12466 case OO_Tilde:
12467 case OO_Exclaim:
12468 case OO_Equal:
12469 case OO_Less:
12470 case OO_Greater:
12471 case OO_LessEqual:
12472 case OO_GreaterEqual:
12473 case OO_PlusEqual:
12474 case OO_MinusEqual:
12475 case OO_StarEqual:
12476 case OO_SlashEqual:
12477 case OO_PercentEqual:
12478 case OO_CaretEqual:
12479 case OO_AmpEqual:
12480 case OO_PipeEqual:
12481 case OO_LessLess:
12482 case OO_GreaterGreater:
12483 case OO_LessLessEqual:
12484 case OO_GreaterGreaterEqual:
12485 case OO_EqualEqual:
12486 case OO_ExclaimEqual:
Richard Smithd30b23d2017-12-01 02:13:10 +000012487 case OO_Spaceship:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000012488 case OO_PlusPlus:
12489 case OO_MinusMinus:
12490 case OO_Comma:
12491 case OO_ArrowStar:
12492 case OO_Arrow:
12493 case OO_Call:
12494 case OO_Subscript:
12495 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +000012496 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000012497 case NUM_OVERLOADED_OPERATORS:
12498 llvm_unreachable("Unexpected reduction identifier");
12499 case OO_None:
Alexey Bataeve3727102018-04-18 15:57:46 +000012500 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000012501 if (II->isStr("max"))
12502 BOK = BO_GT;
12503 else if (II->isStr("min"))
12504 BOK = BO_LT;
12505 }
12506 break;
12507 }
12508 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012509 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +000012510 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataev4d4624c2017-07-20 16:47:47 +000012511 else
12512 ReductionIdRange.setBegin(ReductionId.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000012513 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000012514
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012515 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
12516 bool FirstIter = true;
Alexey Bataeve3727102018-04-18 15:57:46 +000012517 for (Expr *RefExpr : VarList) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000012518 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +000012519 // OpenMP [2.1, C/C++]
12520 // A list item is a variable or array section, subject to the restrictions
12521 // specified in Section 2.4 on page 42 and in each of the sections
12522 // describing clauses and directives for which a list appears.
12523 // OpenMP [2.14.3.3, Restrictions, p.1]
12524 // A variable that is part of another variable (as an array or
12525 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012526 if (!FirstIter && IR != ER)
12527 ++IR;
12528 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +000012529 SourceLocation ELoc;
12530 SourceRange ERange;
12531 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012532 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
Alexey Bataev60da77e2016-02-29 05:54:20 +000012533 /*AllowArraySection=*/true);
12534 if (Res.second) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012535 // Try to find 'declare reduction' corresponding construct before using
12536 // builtin/overloaded operators.
12537 QualType Type = Context.DependentTy;
12538 CXXCastPath BasePath;
12539 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012540 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012541 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012542 Expr *ReductionOp = nullptr;
12543 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012544 (DeclareReductionRef.isUnset() ||
12545 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012546 ReductionOp = DeclareReductionRef.get();
12547 // It will be analyzed later.
12548 RD.push(RefExpr, ReductionOp);
Alexey Bataevc5e02582014-06-16 07:08:35 +000012549 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000012550 ValueDecl *D = Res.first;
12551 if (!D)
12552 continue;
12553
Alexey Bataev88202be2017-07-27 13:20:36 +000012554 Expr *TaskgroupDescriptor = nullptr;
Alexey Bataeva1764212015-09-30 09:22:36 +000012555 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +000012556 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
12557 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +000012558 if (ASE) {
Alexey Bataev31300ed2016-02-04 11:27:03 +000012559 Type = ASE->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000012560 } else if (OASE) {
12561 QualType BaseType =
12562 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
12563 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
Alexey Bataeva1764212015-09-30 09:22:36 +000012564 Type = ATy->getElementType();
12565 else
12566 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +000012567 Type = Type.getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000012568 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +000012569 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
Alexey Bataeve3727102018-04-18 15:57:46 +000012570 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000012571 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +000012572
Alexey Bataevc5e02582014-06-16 07:08:35 +000012573 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
12574 // A variable that appears in a private clause must not have an incomplete
12575 // type or a reference type.
Joel E. Denny3cabf732018-06-28 19:54:49 +000012576 if (S.RequireCompleteType(ELoc, D->getType(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012577 diag::err_omp_reduction_incomplete_type))
Alexey Bataevc5e02582014-06-16 07:08:35 +000012578 continue;
12579 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +000012580 // A list item that appears in a reduction clause must not be
12581 // const-qualified.
Joel E. Dennyd2649292019-01-04 22:11:56 +000012582 if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc,
12583 /*AcceptIfMutable*/ false, ASE || OASE))
Alexey Bataevc5e02582014-06-16 07:08:35 +000012584 continue;
Alexey Bataevbc529672018-09-28 19:33:14 +000012585
12586 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
Alexey Bataevc5e02582014-06-16 07:08:35 +000012587 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
12588 // If a list-item is a reference type then it must bind to the same object
12589 // for all threads of the team.
Alexey Bataevbc529672018-09-28 19:33:14 +000012590 if (!ASE && !OASE) {
12591 if (VD) {
12592 VarDecl *VDDef = VD->getDefinition();
12593 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
12594 DSARefChecker Check(Stack);
12595 if (Check.Visit(VDDef->getInit())) {
12596 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
12597 << getOpenMPClauseName(ClauseKind) << ERange;
12598 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
12599 continue;
12600 }
Alexey Bataeva1764212015-09-30 09:22:36 +000012601 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000012602 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012603
Alexey Bataevbc529672018-09-28 19:33:14 +000012604 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
12605 // in a Construct]
12606 // Variables with the predetermined data-sharing attributes may not be
12607 // listed in data-sharing attributes clauses, except for the cases
12608 // listed below. For these exceptions only, listing a predetermined
12609 // variable in a data-sharing attribute clause is allowed and overrides
12610 // the variable's predetermined data-sharing attributes.
12611 // OpenMP [2.14.3.6, Restrictions, p.3]
12612 // Any number of reduction clauses can be specified on the directive,
12613 // but a list item can appear only once in the reduction clauses for that
12614 // directive.
12615 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
12616 if (DVar.CKind == OMPC_reduction) {
12617 S.Diag(ELoc, diag::err_omp_once_referenced)
12618 << getOpenMPClauseName(ClauseKind);
12619 if (DVar.RefExpr)
12620 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
12621 continue;
12622 }
12623 if (DVar.CKind != OMPC_unknown) {
12624 S.Diag(ELoc, diag::err_omp_wrong_dsa)
12625 << getOpenMPClauseName(DVar.CKind)
12626 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataeve3727102018-04-18 15:57:46 +000012627 reportOriginalDsa(S, Stack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000012628 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +000012629 }
Alexey Bataevbc529672018-09-28 19:33:14 +000012630
12631 // OpenMP [2.14.3.6, Restrictions, p.1]
12632 // A list item that appears in a reduction clause of a worksharing
12633 // construct must be shared in the parallel regions to which any of the
12634 // worksharing regions arising from the worksharing construct bind.
12635 if (isOpenMPWorksharingDirective(CurrDir) &&
12636 !isOpenMPParallelDirective(CurrDir) &&
12637 !isOpenMPTeamsDirective(CurrDir)) {
12638 DVar = Stack->getImplicitDSA(D, true);
12639 if (DVar.CKind != OMPC_shared) {
12640 S.Diag(ELoc, diag::err_omp_required_access)
12641 << getOpenMPClauseName(OMPC_reduction)
12642 << getOpenMPClauseName(OMPC_shared);
12643 reportOriginalDsa(S, Stack, D, DVar);
12644 continue;
12645 }
12646 }
Alexey Bataevf29276e2014-06-18 04:14:57 +000012647 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000012648
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012649 // Try to find 'declare reduction' corresponding construct before using
12650 // builtin/overloaded operators.
12651 CXXCastPath BasePath;
12652 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012653 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012654 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
12655 if (DeclareReductionRef.isInvalid())
12656 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012657 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012658 (DeclareReductionRef.isUnset() ||
12659 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012660 RD.push(RefExpr, DeclareReductionRef.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012661 continue;
12662 }
12663 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
12664 // Not allowed reduction identifier is found.
Stephen Kellyf2ceec42018-08-09 21:08:08 +000012665 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012666 diag::err_omp_unknown_reduction_identifier)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012667 << Type << ReductionIdRange;
12668 continue;
12669 }
12670
12671 // OpenMP [2.14.3.6, reduction clause, Restrictions]
12672 // The type of a list item that appears in a reduction clause must be valid
12673 // for the reduction-identifier. For a max or min reduction in C, the type
12674 // of the list item must be an allowed arithmetic data type: char, int,
12675 // float, double, or _Bool, possibly modified with long, short, signed, or
12676 // unsigned. For a max or min reduction in C++, the type of the list item
12677 // must be an allowed arithmetic data type: char, wchar_t, int, float,
12678 // double, or bool, possibly modified with long, short, signed, or unsigned.
12679 if (DeclareReductionRef.isUnset()) {
12680 if ((BOK == BO_GT || BOK == BO_LT) &&
12681 !(Type->isScalarType() ||
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012682 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
12683 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
Alexey Bataev169d96a2017-07-18 20:17:46 +000012684 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012685 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012686 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
12687 VarDecl::DeclarationOnly;
12688 S.Diag(D->getLocation(),
12689 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012690 << D;
12691 }
12692 continue;
12693 }
12694 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012695 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000012696 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
12697 << getOpenMPClauseName(ClauseKind);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012698 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012699 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
12700 VarDecl::DeclarationOnly;
12701 S.Diag(D->getLocation(),
12702 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012703 << D;
12704 }
12705 continue;
12706 }
12707 }
12708
Alexey Bataev794ba0d2015-04-10 10:43:45 +000012709 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000012710 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
12711 D->hasAttrs() ? &D->getAttrs() : nullptr);
12712 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
12713 D->hasAttrs() ? &D->getAttrs() : nullptr);
12714 QualType PrivateTy = Type;
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000012715
12716 // Try if we can determine constant lengths for all array sections and avoid
12717 // the VLA.
12718 bool ConstantLengthOASE = false;
12719 if (OASE) {
12720 bool SingleElement;
12721 llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
Alexey Bataeve3727102018-04-18 15:57:46 +000012722 ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000012723 Context, OASE, SingleElement, ArraySizes);
12724
12725 // If we don't have a single element, we must emit a constant array type.
12726 if (ConstantLengthOASE && !SingleElement) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012727 for (llvm::APSInt &Size : ArraySizes)
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000012728 PrivateTy = Context.getConstantArrayType(
12729 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0);
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000012730 }
12731 }
12732
12733 if ((OASE && !ConstantLengthOASE) ||
Jonas Hahnfeld96087f32017-11-02 13:30:42 +000012734 (!OASE && !ASE &&
Alexey Bataev60da77e2016-02-29 05:54:20 +000012735 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev85260312019-07-11 20:35:31 +000012736 if (!Context.getTargetInfo().isVLASupported()) {
12737 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective())) {
12738 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
12739 S.Diag(ELoc, diag::note_vla_unsupported);
12740 } else {
12741 S.targetDiag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
12742 S.targetDiag(ELoc, diag::note_vla_unsupported);
12743 }
Jonas Hahnfeld87d44262017-11-18 21:00:46 +000012744 continue;
12745 }
David Majnemer9d168222016-08-05 17:44:54 +000012746 // For arrays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +000012747 // Create pseudo array type for private copy. The size for this array will
12748 // be generated during codegen.
12749 // For array subscripts or single variables Private Ty is the same as Type
12750 // (type of the variable or single array element).
12751 PrivateTy = Context.getVariableArrayType(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012752 Type,
Alexey Bataevd070a582017-10-25 15:54:04 +000012753 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
Alexey Bataevf24e7b12015-10-08 09:10:53 +000012754 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +000012755 } else if (!ASE && !OASE &&
Alexey Bataeve3727102018-04-18 15:57:46 +000012756 Context.getAsArrayType(D->getType().getNonReferenceType())) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000012757 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000012758 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000012759 // Private copy.
Alexey Bataeve3727102018-04-18 15:57:46 +000012760 VarDecl *PrivateVD =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000012761 buildVarDecl(S, ELoc, PrivateTy, D->getName(),
12762 D->hasAttrs() ? &D->getAttrs() : nullptr,
12763 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000012764 // Add initializer for private variable.
12765 Expr *Init = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000012766 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
12767 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012768 if (DeclareReductionRef.isUsable()) {
12769 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
12770 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
12771 if (DRD->getInitializer()) {
12772 Init = DRDRef;
12773 RHSVD->setInit(DRDRef);
12774 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000012775 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012776 } else {
12777 switch (BOK) {
12778 case BO_Add:
12779 case BO_Xor:
12780 case BO_Or:
12781 case BO_LOr:
12782 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
12783 if (Type->isScalarType() || Type->isAnyComplexType())
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012784 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012785 break;
12786 case BO_Mul:
12787 case BO_LAnd:
12788 if (Type->isScalarType() || Type->isAnyComplexType()) {
12789 // '*' and '&&' reduction ops - initializer is '1'.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012790 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +000012791 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012792 break;
12793 case BO_And: {
12794 // '&' reduction op - initializer is '~0'.
12795 QualType OrigType = Type;
12796 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
12797 Type = ComplexTy->getElementType();
12798 if (Type->isRealFloatingType()) {
12799 llvm::APFloat InitValue =
12800 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
12801 /*isIEEE=*/true);
12802 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
12803 Type, ELoc);
12804 } else if (Type->isScalarType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012805 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012806 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
12807 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
12808 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
12809 }
12810 if (Init && OrigType->isAnyComplexType()) {
12811 // Init = 0xFFFF + 0xFFFFi;
12812 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012813 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012814 }
12815 Type = OrigType;
12816 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000012817 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012818 case BO_LT:
12819 case BO_GT: {
12820 // 'min' reduction op - initializer is 'Largest representable number in
12821 // the reduction list item type'.
12822 // 'max' reduction op - initializer is 'Least representable number in
12823 // the reduction list item type'.
12824 if (Type->isIntegerType() || Type->isPointerType()) {
12825 bool IsSigned = Type->hasSignedIntegerRepresentation();
Alexey Bataeve3727102018-04-18 15:57:46 +000012826 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012827 QualType IntTy =
12828 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
12829 llvm::APInt InitValue =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012830 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
12831 : llvm::APInt::getMinValue(Size)
12832 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
12833 : llvm::APInt::getMaxValue(Size);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012834 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
12835 if (Type->isPointerType()) {
12836 // Cast to pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +000012837 ExprResult CastExpr = S.BuildCStyleCastExpr(
Alexey Bataevd070a582017-10-25 15:54:04 +000012838 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012839 if (CastExpr.isInvalid())
12840 continue;
12841 Init = CastExpr.get();
12842 }
12843 } else if (Type->isRealFloatingType()) {
12844 llvm::APFloat InitValue = llvm::APFloat::getLargest(
12845 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
12846 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
12847 Type, ELoc);
12848 }
12849 break;
12850 }
12851 case BO_PtrMemD:
12852 case BO_PtrMemI:
12853 case BO_MulAssign:
12854 case BO_Div:
12855 case BO_Rem:
12856 case BO_Sub:
12857 case BO_Shl:
12858 case BO_Shr:
12859 case BO_LE:
12860 case BO_GE:
12861 case BO_EQ:
12862 case BO_NE:
Richard Smithc70f1d62017-12-14 15:16:18 +000012863 case BO_Cmp:
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012864 case BO_AndAssign:
12865 case BO_XorAssign:
12866 case BO_OrAssign:
12867 case BO_Assign:
12868 case BO_AddAssign:
12869 case BO_SubAssign:
12870 case BO_DivAssign:
12871 case BO_RemAssign:
12872 case BO_ShlAssign:
12873 case BO_ShrAssign:
12874 case BO_Comma:
12875 llvm_unreachable("Unexpected reduction operation");
12876 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +000012877 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012878 if (Init && DeclareReductionRef.isUnset())
12879 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
12880 else if (!Init)
12881 S.ActOnUninitializedDecl(RHSVD);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012882 if (RHSVD->isInvalidDecl())
12883 continue;
Alexey Bataev09232662019-04-04 17:28:22 +000012884 if (!RHSVD->hasInit() &&
12885 (DeclareReductionRef.isUnset() || !S.LangOpts.CPlusPlus)) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012886 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
12887 << Type << ReductionIdRange;
12888 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
12889 VarDecl::DeclarationOnly;
12890 S.Diag(D->getLocation(),
12891 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000012892 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000012893 continue;
12894 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000012895 // Store initializer for single element in private copy. Will be used during
12896 // codegen.
12897 PrivateVD->setInit(RHSVD->getInit());
12898 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataeve3727102018-04-18 15:57:46 +000012899 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012900 ExprResult ReductionOp;
12901 if (DeclareReductionRef.isUsable()) {
12902 QualType RedTy = DeclareReductionRef.get()->getType();
12903 QualType PtrRedTy = Context.getPointerType(RedTy);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012904 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
12905 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012906 if (!BasePath.empty()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012907 LHS = S.DefaultLvalueConversion(LHS.get());
12908 RHS = S.DefaultLvalueConversion(RHS.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012909 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
12910 CK_UncheckedDerivedToBase, LHS.get(),
12911 &BasePath, LHS.get()->getValueKind());
12912 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
12913 CK_UncheckedDerivedToBase, RHS.get(),
12914 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +000012915 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012916 FunctionProtoType::ExtProtoInfo EPI;
12917 QualType Params[] = {PtrRedTy, PtrRedTy};
12918 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
12919 auto *OVE = new (Context) OpaqueValueExpr(
12920 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012921 S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012922 Expr *Args[] = {LHS.get(), RHS.get()};
Bruno Riccic5885cf2018-12-21 15:20:32 +000012923 ReductionOp =
12924 CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012925 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012926 ReductionOp = S.BuildBinOp(
Stephen Kellyf2ceec42018-08-09 21:08:08 +000012927 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012928 if (ReductionOp.isUsable()) {
12929 if (BOK != BO_LT && BOK != BO_GT) {
12930 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000012931 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012932 BO_Assign, LHSDRE, ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012933 } else {
Alexey Bataevd070a582017-10-25 15:54:04 +000012934 auto *ConditionalOp = new (Context)
12935 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
12936 Type, VK_LValue, OK_Ordinary);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012937 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000012938 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000012939 BO_Assign, LHSDRE, ConditionalOp);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012940 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000012941 if (ReductionOp.isUsable())
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000012942 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(),
12943 /*DiscardedValue*/ false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012944 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000012945 if (!ReductionOp.isUsable())
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012946 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +000012947 }
12948
Alexey Bataevfa312f32017-07-21 18:48:21 +000012949 // OpenMP [2.15.4.6, Restrictions, p.2]
12950 // A list item that appears in an in_reduction clause of a task construct
12951 // must appear in a task_reduction clause of a construct associated with a
12952 // taskgroup region that includes the participating task in its taskgroup
12953 // set. The construct associated with the innermost region that meets this
12954 // condition must specify the same reduction-identifier as the in_reduction
12955 // clause.
12956 if (ClauseKind == OMPC_in_reduction) {
Alexey Bataevfa312f32017-07-21 18:48:21 +000012957 SourceRange ParentSR;
12958 BinaryOperatorKind ParentBOK;
12959 const Expr *ParentReductionOp;
Alexey Bataev88202be2017-07-27 13:20:36 +000012960 Expr *ParentBOKTD, *ParentReductionOpTD;
Alexey Bataevf189cb72017-07-24 14:52:13 +000012961 DSAStackTy::DSAVarData ParentBOKDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000012962 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
12963 ParentBOKTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000012964 DSAStackTy::DSAVarData ParentReductionOpDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000012965 Stack->getTopMostTaskgroupReductionData(
12966 D, ParentSR, ParentReductionOp, ParentReductionOpTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000012967 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
12968 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
12969 if (!IsParentBOK && !IsParentReductionOp) {
12970 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
12971 continue;
12972 }
Alexey Bataevfa312f32017-07-21 18:48:21 +000012973 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
12974 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
12975 IsParentReductionOp) {
12976 bool EmitError = true;
12977 if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
12978 llvm::FoldingSetNodeID RedId, ParentRedId;
12979 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
12980 DeclareReductionRef.get()->Profile(RedId, Context,
12981 /*Canonical=*/true);
12982 EmitError = RedId != ParentRedId;
12983 }
12984 if (EmitError) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000012985 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfa312f32017-07-21 18:48:21 +000012986 diag::err_omp_reduction_identifier_mismatch)
12987 << ReductionIdRange << RefExpr->getSourceRange();
12988 S.Diag(ParentSR.getBegin(),
12989 diag::note_omp_previous_reduction_identifier)
Alexey Bataevf189cb72017-07-24 14:52:13 +000012990 << ParentSR
12991 << (IsParentBOK ? ParentBOKDSA.RefExpr
12992 : ParentReductionOpDSA.RefExpr)
12993 ->getSourceRange();
Alexey Bataevfa312f32017-07-21 18:48:21 +000012994 continue;
12995 }
12996 }
Alexey Bataev88202be2017-07-27 13:20:36 +000012997 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
12998 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
Alexey Bataevfa312f32017-07-21 18:48:21 +000012999 }
13000
Alexey Bataev60da77e2016-02-29 05:54:20 +000013001 DeclRefExpr *Ref = nullptr;
13002 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013003 if (!VD && !S.CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000013004 if (ASE || OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013005 TransformExprToCaptures RebuildToCapture(S, D);
Alexey Bataev60da77e2016-02-29 05:54:20 +000013006 VarsExpr =
13007 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
13008 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +000013009 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013010 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +000013011 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013012 if (!S.isOpenMPCapturedDecl(D)) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013013 RD.ExprCaptures.emplace_back(Ref->getDecl());
Alexey Bataev5a3af132016-03-29 08:58:54 +000013014 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013015 ExprResult RefRes = S.DefaultLvalueConversion(Ref);
Alexey Bataev5a3af132016-03-29 08:58:54 +000013016 if (!RefRes.isUsable())
13017 continue;
13018 ExprResult PostUpdateRes =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013019 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
13020 RefRes.get());
Alexey Bataev5a3af132016-03-29 08:58:54 +000013021 if (!PostUpdateRes.isUsable())
13022 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013023 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
13024 Stack->getCurrentDirective() == OMPD_taskgroup) {
13025 S.Diag(RefExpr->getExprLoc(),
13026 diag::err_omp_reduction_non_addressable_expression)
Alexey Bataevbcd0ae02017-07-11 19:16:44 +000013027 << RefExpr->getSourceRange();
13028 continue;
13029 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013030 RD.ExprPostUpdates.emplace_back(
13031 S.IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +000013032 }
13033 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000013034 }
Alexey Bataev169d96a2017-07-18 20:17:46 +000013035 // All reduction items are still marked as reduction (to do not increase
13036 // code base size).
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013037 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
Alexey Bataevf189cb72017-07-24 14:52:13 +000013038 if (CurrDir == OMPD_taskgroup) {
13039 if (DeclareReductionRef.isUsable())
Alexey Bataev3b1b8952017-07-25 15:53:26 +000013040 Stack->addTaskgroupReductionData(D, ReductionIdRange,
13041 DeclareReductionRef.get());
Alexey Bataevf189cb72017-07-24 14:52:13 +000013042 else
Alexey Bataev3b1b8952017-07-25 15:53:26 +000013043 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
Alexey Bataevf189cb72017-07-24 14:52:13 +000013044 }
Alexey Bataev88202be2017-07-27 13:20:36 +000013045 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
13046 TaskgroupDescriptor);
Alexey Bataevc5e02582014-06-16 07:08:35 +000013047 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013048 return RD.Vars.empty();
13049}
Alexey Bataevc5e02582014-06-16 07:08:35 +000013050
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013051OMPClause *Sema::ActOnOpenMPReductionClause(
13052 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
13053 SourceLocation ColonLoc, SourceLocation EndLoc,
13054 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
13055 ArrayRef<Expr *> UnresolvedReductions) {
13056 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000013057 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
Alexey Bataev169d96a2017-07-18 20:17:46 +000013058 StartLoc, LParenLoc, ColonLoc, EndLoc,
13059 ReductionIdScopeSpec, ReductionId,
13060 UnresolvedReductions, RD))
Alexey Bataevc5e02582014-06-16 07:08:35 +000013061 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +000013062
Alexey Bataevc5e02582014-06-16 07:08:35 +000013063 return OMPReductionClause::Create(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000013064 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
13065 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
13066 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
13067 buildPreInits(Context, RD.ExprCaptures),
13068 buildPostUpdate(*this, RD.ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +000013069}
13070
Alexey Bataev169d96a2017-07-18 20:17:46 +000013071OMPClause *Sema::ActOnOpenMPTaskReductionClause(
13072 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
13073 SourceLocation ColonLoc, SourceLocation EndLoc,
13074 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
13075 ArrayRef<Expr *> UnresolvedReductions) {
13076 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000013077 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList,
13078 StartLoc, LParenLoc, ColonLoc, EndLoc,
13079 ReductionIdScopeSpec, ReductionId,
Alexey Bataev169d96a2017-07-18 20:17:46 +000013080 UnresolvedReductions, RD))
13081 return nullptr;
13082
13083 return OMPTaskReductionClause::Create(
13084 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
13085 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
13086 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
13087 buildPreInits(Context, RD.ExprCaptures),
13088 buildPostUpdate(*this, RD.ExprPostUpdates));
13089}
13090
Alexey Bataevfa312f32017-07-21 18:48:21 +000013091OMPClause *Sema::ActOnOpenMPInReductionClause(
13092 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
13093 SourceLocation ColonLoc, SourceLocation EndLoc,
13094 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
13095 ArrayRef<Expr *> UnresolvedReductions) {
13096 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000013097 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
Alexey Bataevfa312f32017-07-21 18:48:21 +000013098 StartLoc, LParenLoc, ColonLoc, EndLoc,
13099 ReductionIdScopeSpec, ReductionId,
13100 UnresolvedReductions, RD))
13101 return nullptr;
13102
13103 return OMPInReductionClause::Create(
13104 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
13105 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
Alexey Bataev88202be2017-07-27 13:20:36 +000013106 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
Alexey Bataevfa312f32017-07-21 18:48:21 +000013107 buildPreInits(Context, RD.ExprCaptures),
13108 buildPostUpdate(*this, RD.ExprPostUpdates));
13109}
13110
Alexey Bataevecba70f2016-04-12 11:02:11 +000013111bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
13112 SourceLocation LinLoc) {
13113 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
13114 LinKind == OMPC_LINEAR_unknown) {
13115 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
13116 return true;
13117 }
13118 return false;
13119}
13120
Alexey Bataeve3727102018-04-18 15:57:46 +000013121bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
Alexey Bataevecba70f2016-04-12 11:02:11 +000013122 OpenMPLinearClauseKind LinKind,
13123 QualType Type) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013124 const auto *VD = dyn_cast_or_null<VarDecl>(D);
Alexey Bataevecba70f2016-04-12 11:02:11 +000013125 // A variable must not have an incomplete type or a reference type.
13126 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
13127 return true;
13128 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
13129 !Type->isReferenceType()) {
13130 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
13131 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
13132 return true;
13133 }
13134 Type = Type.getNonReferenceType();
13135
Joel E. Dennybae586f2019-01-04 22:12:13 +000013136 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
13137 // A variable that is privatized must not have a const-qualified type
13138 // unless it is of class type with a mutable member. This restriction does
13139 // not apply to the firstprivate clause.
13140 if (rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc))
Alexey Bataevecba70f2016-04-12 11:02:11 +000013141 return true;
Alexey Bataevecba70f2016-04-12 11:02:11 +000013142
13143 // A list item must be of integral or pointer type.
13144 Type = Type.getUnqualifiedType().getCanonicalType();
13145 const auto *Ty = Type.getTypePtrOrNull();
13146 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
13147 !Ty->isPointerType())) {
13148 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
13149 if (D) {
13150 bool IsDecl =
13151 !VD ||
13152 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
13153 Diag(D->getLocation(),
13154 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
13155 << D;
13156 }
13157 return true;
13158 }
13159 return false;
13160}
13161
Alexey Bataev182227b2015-08-20 10:54:39 +000013162OMPClause *Sema::ActOnOpenMPLinearClause(
13163 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
13164 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
13165 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +000013166 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000013167 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +000013168 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +000013169 SmallVector<Decl *, 4> ExprCaptures;
13170 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +000013171 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +000013172 LinKind = OMPC_LINEAR_val;
Alexey Bataeve3727102018-04-18 15:57:46 +000013173 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000013174 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +000013175 SourceLocation ELoc;
13176 SourceRange ERange;
13177 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000013178 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000013179 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +000013180 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000013181 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000013182 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +000013183 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +000013184 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +000013185 ValueDecl *D = Res.first;
13186 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +000013187 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +000013188
Alexey Bataev2bbf7212016-03-03 03:52:24 +000013189 QualType Type = D->getType();
13190 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +000013191
13192 // OpenMP [2.14.3.7, linear clause]
13193 // A list-item cannot appear in more than one linear clause.
13194 // A list-item that appears in a linear clause cannot appear in any
13195 // other data-sharing attribute clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000013196 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman8dba6642014-04-22 13:09:42 +000013197 if (DVar.RefExpr) {
13198 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
13199 << getOpenMPClauseName(OMPC_linear);
Alexey Bataeve3727102018-04-18 15:57:46 +000013200 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +000013201 continue;
13202 }
13203
Alexey Bataevecba70f2016-04-12 11:02:11 +000013204 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +000013205 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +000013206 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +000013207
Alexey Bataevbd9fec12015-08-18 06:47:21 +000013208 // Build private copy of original var.
Alexey Bataeve3727102018-04-18 15:57:46 +000013209 VarDecl *Private =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000013210 buildVarDecl(*this, ELoc, Type, D->getName(),
13211 D->hasAttrs() ? &D->getAttrs() : nullptr,
13212 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000013213 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +000013214 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +000013215 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000013216 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +000013217 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000013218 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000013219 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000013220 if (!isOpenMPCapturedDecl(D)) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000013221 ExprCaptures.push_back(Ref->getDecl());
13222 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
13223 ExprResult RefRes = DefaultLvalueConversion(Ref);
13224 if (!RefRes.isUsable())
13225 continue;
13226 ExprResult PostUpdateRes =
13227 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
13228 SimpleRefExpr, RefRes.get());
13229 if (!PostUpdateRes.isUsable())
13230 continue;
13231 ExprPostUpdates.push_back(
13232 IgnoredValueConversions(PostUpdateRes.get()).get());
13233 }
13234 }
13235 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000013236 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +000013237 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000013238 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +000013239 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000013240 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000013241 /*DirectInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000013242 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000013243
13244 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000013245 Vars.push_back((VD || CurContext->isDependentContext())
13246 ? RefExpr->IgnoreParens()
13247 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000013248 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +000013249 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +000013250 }
13251
13252 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000013253 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000013254
13255 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +000013256 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000013257 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
13258 !Step->isInstantiationDependent() &&
13259 !Step->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013260 SourceLocation StepLoc = Step->getBeginLoc();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000013261 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +000013262 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000013263 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +000013264 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +000013265
Alexander Musman3276a272015-03-21 10:12:56 +000013266 // Build var to save the step value.
13267 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +000013268 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +000013269 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +000013270 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +000013271 ExprResult CalcStep =
13272 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000013273 CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false);
Alexander Musman3276a272015-03-21 10:12:56 +000013274
Alexander Musman8dba6642014-04-22 13:09:42 +000013275 // Warn about zero linear step (it would be probably better specified as
13276 // making corresponding variables 'const').
13277 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +000013278 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
13279 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +000013280 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
13281 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +000013282 if (!IsConstant && CalcStep.isUsable()) {
13283 // Calculate the step beforehand instead of doing this on each iteration.
13284 // (This is not used if the number of iterations may be kfold-ed).
13285 CalcStepExpr = CalcStep.get();
13286 }
Alexander Musman8dba6642014-04-22 13:09:42 +000013287 }
13288
Alexey Bataev182227b2015-08-20 10:54:39 +000013289 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
13290 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +000013291 StepExpr, CalcStepExpr,
13292 buildPreInits(Context, ExprCaptures),
13293 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +000013294}
13295
Alexey Bataev5dff95c2016-04-22 03:56:56 +000013296static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
13297 Expr *NumIterations, Sema &SemaRef,
13298 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +000013299 // Walk the vars and build update/final expressions for the CodeGen.
13300 SmallVector<Expr *, 8> Updates;
13301 SmallVector<Expr *, 8> Finals;
Alexey Bataev195ae902019-08-08 13:42:45 +000013302 SmallVector<Expr *, 8> UsedExprs;
Alexander Musman3276a272015-03-21 10:12:56 +000013303 Expr *Step = Clause.getStep();
13304 Expr *CalcStep = Clause.getCalcStep();
13305 // OpenMP [2.14.3.7, linear clause]
13306 // If linear-step is not specified it is assumed to be 1.
Alexey Bataeve3727102018-04-18 15:57:46 +000013307 if (!Step)
Alexander Musman3276a272015-03-21 10:12:56 +000013308 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013309 else if (CalcStep)
Alexander Musman3276a272015-03-21 10:12:56 +000013310 Step = cast<BinaryOperator>(CalcStep)->getLHS();
13311 bool HasErrors = false;
13312 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +000013313 auto CurPrivate = Clause.privates().begin();
Alexey Bataeve3727102018-04-18 15:57:46 +000013314 OpenMPLinearClauseKind LinKind = Clause.getModifier();
13315 for (Expr *RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +000013316 SourceLocation ELoc;
13317 SourceRange ERange;
13318 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000013319 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000013320 ValueDecl *D = Res.first;
13321 if (Res.second || !D) {
13322 Updates.push_back(nullptr);
13323 Finals.push_back(nullptr);
13324 HasErrors = true;
13325 continue;
13326 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +000013327 auto &&Info = Stack->isLoopControlVariable(D);
Alexey Bataev2b86f212017-11-29 21:31:48 +000013328 // OpenMP [2.15.11, distribute simd Construct]
13329 // A list item may not appear in a linear clause, unless it is the loop
13330 // iteration variable.
13331 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
13332 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
13333 SemaRef.Diag(ELoc,
13334 diag::err_omp_linear_distribute_var_non_loop_iteration);
13335 Updates.push_back(nullptr);
13336 Finals.push_back(nullptr);
13337 HasErrors = true;
13338 continue;
13339 }
Alexander Musman3276a272015-03-21 10:12:56 +000013340 Expr *InitExpr = *CurInit;
13341
13342 // Build privatized reference to the current linear var.
David Majnemer9d168222016-08-05 17:44:54 +000013343 auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000013344 Expr *CapturedRef;
13345 if (LinKind == OMPC_LINEAR_uval)
13346 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
13347 else
13348 CapturedRef =
13349 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
13350 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
13351 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000013352
13353 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000013354 ExprResult Update;
Alexey Bataeve3727102018-04-18 15:57:46 +000013355 if (!Info.first)
Alexey Bataevf8be4762019-08-14 19:30:06 +000013356 Update = buildCounterUpdate(
13357 SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, InitExpr, IV, Step,
13358 /*Subtract=*/false, /*IsNonRectangularLB=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000013359 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000013360 Update = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013361 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000013362 /*DiscardedValue*/ false);
Alexander Musman3276a272015-03-21 10:12:56 +000013363
13364 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000013365 ExprResult Final;
Alexey Bataeve3727102018-04-18 15:57:46 +000013366 if (!Info.first)
13367 Final =
13368 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataevf8be4762019-08-14 19:30:06 +000013369 InitExpr, NumIterations, Step, /*Subtract=*/false,
13370 /*IsNonRectangularLB=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000013371 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000013372 Final = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013373 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000013374 /*DiscardedValue*/ false);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000013375
Alexander Musman3276a272015-03-21 10:12:56 +000013376 if (!Update.isUsable() || !Final.isUsable()) {
13377 Updates.push_back(nullptr);
13378 Finals.push_back(nullptr);
Alexey Bataev195ae902019-08-08 13:42:45 +000013379 UsedExprs.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +000013380 HasErrors = true;
13381 } else {
13382 Updates.push_back(Update.get());
13383 Finals.push_back(Final.get());
Alexey Bataev195ae902019-08-08 13:42:45 +000013384 if (!Info.first)
13385 UsedExprs.push_back(SimpleRefExpr);
Alexander Musman3276a272015-03-21 10:12:56 +000013386 }
Richard Trieucc3949d2016-02-18 22:34:54 +000013387 ++CurInit;
13388 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +000013389 }
Alexey Bataev195ae902019-08-08 13:42:45 +000013390 if (Expr *S = Clause.getStep())
13391 UsedExprs.push_back(S);
13392 // Fill the remaining part with the nullptr.
13393 UsedExprs.append(Clause.varlist_size() + 1 - UsedExprs.size(), nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +000013394 Clause.setUpdates(Updates);
13395 Clause.setFinals(Finals);
Alexey Bataev195ae902019-08-08 13:42:45 +000013396 Clause.setUsedExprs(UsedExprs);
Alexander Musman3276a272015-03-21 10:12:56 +000013397 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +000013398}
13399
Alexander Musmanf0d76e72014-05-29 14:36:25 +000013400OMPClause *Sema::ActOnOpenMPAlignedClause(
13401 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
13402 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000013403 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000013404 for (Expr *RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +000013405 assert(RefExpr && "NULL expr in OpenMP linear clause.");
13406 SourceLocation ELoc;
13407 SourceRange ERange;
13408 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000013409 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev1efd1662016-03-29 10:59:56 +000013410 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000013411 // It will be analyzed later.
13412 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000013413 }
Alexey Bataev1efd1662016-03-29 10:59:56 +000013414 ValueDecl *D = Res.first;
13415 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000013416 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000013417
Alexey Bataev1efd1662016-03-29 10:59:56 +000013418 QualType QType = D->getType();
13419 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000013420
13421 // OpenMP [2.8.1, simd construct, Restrictions]
13422 // The type of list items appearing in the aligned clause must be
13423 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000013424 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +000013425 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +000013426 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000013427 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +000013428 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000013429 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +000013430 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +000013431 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +000013432 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +000013433 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +000013434 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000013435 continue;
13436 }
13437
13438 // OpenMP [2.8.1, simd construct, Restrictions]
13439 // A list-item cannot appear in more than one aligned clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000013440 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +000013441 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000013442 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
13443 << getOpenMPClauseName(OMPC_aligned);
13444 continue;
13445 }
13446
Alexey Bataev1efd1662016-03-29 10:59:56 +000013447 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000013448 if (!VD && isOpenMPCapturedDecl(D))
Alexey Bataev1efd1662016-03-29 10:59:56 +000013449 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
13450 Vars.push_back(DefaultFunctionArrayConversion(
13451 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
13452 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +000013453 }
13454
13455 // OpenMP [2.8.1, simd construct, Description]
13456 // The parameter of the aligned clause, alignment, must be a constant
13457 // positive integer expression.
13458 // If no optional parameter is specified, implementation-defined default
13459 // alignments for SIMD instructions on the target platforms are assumed.
13460 if (Alignment != nullptr) {
13461 ExprResult AlignResult =
13462 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
13463 if (AlignResult.isInvalid())
13464 return nullptr;
13465 Alignment = AlignResult.get();
13466 }
13467 if (Vars.empty())
13468 return nullptr;
13469
13470 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
13471 EndLoc, Vars, Alignment);
13472}
13473
Alexey Bataevd48bcd82014-03-31 03:36:38 +000013474OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
13475 SourceLocation StartLoc,
13476 SourceLocation LParenLoc,
13477 SourceLocation EndLoc) {
13478 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +000013479 SmallVector<Expr *, 8> SrcExprs;
13480 SmallVector<Expr *, 8> DstExprs;
13481 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000013482 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000013483 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
13484 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +000013485 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000013486 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000013487 SrcExprs.push_back(nullptr);
13488 DstExprs.push_back(nullptr);
13489 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000013490 continue;
13491 }
13492
Alexey Bataeved09d242014-05-28 05:53:51 +000013493 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000013494 // OpenMP [2.1, C/C++]
13495 // A list item is a variable name.
13496 // OpenMP [2.14.4.1, Restrictions, p.1]
13497 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +000013498 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000013499 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000013500 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
13501 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000013502 continue;
13503 }
13504
13505 Decl *D = DE->getDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000013506 auto *VD = cast<VarDecl>(D);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000013507
13508 QualType Type = VD->getType();
13509 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
13510 // It will be analyzed later.
13511 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000013512 SrcExprs.push_back(nullptr);
13513 DstExprs.push_back(nullptr);
13514 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000013515 continue;
13516 }
13517
13518 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
13519 // A list item that appears in a copyin clause must be threadprivate.
13520 if (!DSAStack->isThreadPrivate(VD)) {
13521 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +000013522 << getOpenMPClauseName(OMPC_copyin)
13523 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000013524 continue;
13525 }
13526
13527 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
13528 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +000013529 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +000013530 // operator for the class type.
Alexey Bataeve3727102018-04-18 15:57:46 +000013531 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
13532 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013533 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000013534 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000013535 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +000013536 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +000013537 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013538 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst",
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000013539 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000013540 DeclRefExpr *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +000013541 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +000013542 // For arrays generate assignment operation for single element and replace
13543 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000013544 ExprResult AssignmentOp =
13545 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr,
13546 PseudoSrcExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000013547 if (AssignmentOp.isInvalid())
13548 continue;
13549 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000013550 /*DiscardedValue*/ false);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000013551 if (AssignmentOp.isInvalid())
13552 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000013553
13554 DSAStack->addDSA(VD, DE, OMPC_copyin);
13555 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000013556 SrcExprs.push_back(PseudoSrcExpr);
13557 DstExprs.push_back(PseudoDstExpr);
13558 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +000013559 }
13560
Alexey Bataeved09d242014-05-28 05:53:51 +000013561 if (Vars.empty())
13562 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000013563
Alexey Bataevf56f98c2015-04-16 05:39:01 +000013564 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
13565 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000013566}
13567
Alexey Bataevbae9a792014-06-27 10:37:06 +000013568OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
13569 SourceLocation StartLoc,
13570 SourceLocation LParenLoc,
13571 SourceLocation EndLoc) {
13572 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +000013573 SmallVector<Expr *, 8> SrcExprs;
13574 SmallVector<Expr *, 8> DstExprs;
13575 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000013576 for (Expr *RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +000013577 assert(RefExpr && "NULL expr in OpenMP linear clause.");
13578 SourceLocation ELoc;
13579 SourceRange ERange;
13580 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000013581 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataeve122da12016-03-17 10:50:17 +000013582 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000013583 // It will be analyzed later.
13584 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000013585 SrcExprs.push_back(nullptr);
13586 DstExprs.push_back(nullptr);
13587 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +000013588 }
Alexey Bataeve122da12016-03-17 10:50:17 +000013589 ValueDecl *D = Res.first;
13590 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +000013591 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000013592
Alexey Bataeve122da12016-03-17 10:50:17 +000013593 QualType Type = D->getType();
13594 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +000013595
13596 // OpenMP [2.14.4.2, Restrictions, p.2]
13597 // A list item that appears in a copyprivate clause may not appear in a
13598 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +000013599 if (!VD || !DSAStack->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013600 DSAStackTy::DSAVarData DVar =
13601 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000013602 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
13603 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000013604 Diag(ELoc, diag::err_omp_wrong_dsa)
13605 << getOpenMPClauseName(DVar.CKind)
13606 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000013607 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000013608 continue;
13609 }
13610
13611 // OpenMP [2.11.4.2, Restrictions, p.1]
13612 // All list items that appear in a copyprivate clause must be either
13613 // threadprivate or private in the enclosing context.
13614 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +000013615 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000013616 if (DVar.CKind == OMPC_shared) {
13617 Diag(ELoc, diag::err_omp_required_access)
13618 << getOpenMPClauseName(OMPC_copyprivate)
13619 << "threadprivate or private in the enclosing context";
Alexey Bataeve3727102018-04-18 15:57:46 +000013620 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000013621 continue;
13622 }
13623 }
13624 }
13625
Alexey Bataev7a3e5852015-05-19 08:19:24 +000013626 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000013627 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000013628 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000013629 << getOpenMPClauseName(OMPC_copyprivate) << Type
13630 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000013631 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000013632 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000013633 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000013634 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000013635 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000013636 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000013637 continue;
13638 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000013639
Alexey Bataevbae9a792014-06-27 10:37:06 +000013640 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
13641 // A variable of class type (or array thereof) that appears in a
13642 // copyin clause requires an accessible, unambiguous copy assignment
13643 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000013644 Type = Context.getBaseElementType(Type.getNonReferenceType())
13645 .getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000013646 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013647 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src",
Alexey Bataeve122da12016-03-17 10:50:17 +000013648 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000013649 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
13650 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013651 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst",
Alexey Bataeve122da12016-03-17 10:50:17 +000013652 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000013653 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
13654 ExprResult AssignmentOp = BuildBinOp(
13655 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000013656 if (AssignmentOp.isInvalid())
13657 continue;
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000013658 AssignmentOp =
13659 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000013660 if (AssignmentOp.isInvalid())
13661 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000013662
13663 // No need to mark vars as copyprivate, they are already threadprivate or
13664 // implicitly private.
Alexey Bataeve3727102018-04-18 15:57:46 +000013665 assert(VD || isOpenMPCapturedDecl(D));
Alexey Bataeve122da12016-03-17 10:50:17 +000013666 Vars.push_back(
13667 VD ? RefExpr->IgnoreParens()
13668 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000013669 SrcExprs.push_back(PseudoSrcExpr);
13670 DstExprs.push_back(PseudoDstExpr);
13671 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000013672 }
13673
13674 if (Vars.empty())
13675 return nullptr;
13676
Alexey Bataeva63048e2015-03-23 06:18:07 +000013677 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13678 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000013679}
13680
Alexey Bataev6125da92014-07-21 11:26:11 +000013681OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
13682 SourceLocation StartLoc,
13683 SourceLocation LParenLoc,
13684 SourceLocation EndLoc) {
13685 if (VarList.empty())
13686 return nullptr;
13687
13688 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
13689}
Alexey Bataevdea47612014-07-23 07:46:59 +000013690
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000013691OMPClause *
13692Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
13693 SourceLocation DepLoc, SourceLocation ColonLoc,
13694 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
13695 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000013696 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000013697 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000013698 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000013699 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000013700 return nullptr;
13701 }
13702 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000013703 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
13704 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000013705 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000013706 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000013707 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
13708 /*Last=*/OMPC_DEPEND_unknown, Except)
13709 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000013710 return nullptr;
13711 }
13712 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000013713 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000013714 llvm::APSInt DepCounter(/*BitWidth=*/32);
13715 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
Alexey Bataevf138fda2018-08-13 19:04:24 +000013716 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
13717 if (const Expr *OrderedCountExpr =
13718 DSAStack->getParentOrderedRegionParam().first) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000013719 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
13720 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000013721 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000013722 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013723 for (Expr *RefExpr : VarList) {
Alexey Bataev17daedf2018-02-15 22:42:57 +000013724 assert(RefExpr && "NULL expr in OpenMP shared clause.");
13725 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
13726 // It will be analyzed later.
13727 Vars.push_back(RefExpr);
13728 continue;
13729 }
13730
13731 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +000013732 Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev17daedf2018-02-15 22:42:57 +000013733 if (DepKind == OMPC_DEPEND_sink) {
Alexey Bataevf138fda2018-08-13 19:04:24 +000013734 if (DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000013735 DepCounter >= TotalDepCount) {
13736 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
13737 continue;
13738 }
13739 ++DepCounter;
13740 // OpenMP [2.13.9, Summary]
13741 // depend(dependence-type : vec), where dependence-type is:
13742 // 'sink' and where vec is the iteration vector, which has the form:
13743 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
13744 // where n is the value specified by the ordered clause in the loop
13745 // directive, xi denotes the loop iteration variable of the i-th nested
13746 // loop associated with the loop directive, and di is a constant
13747 // non-negative integer.
13748 if (CurContext->isDependentContext()) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000013749 // It will be analyzed later.
13750 Vars.push_back(RefExpr);
13751 continue;
13752 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000013753 SimpleExpr = SimpleExpr->IgnoreImplicit();
13754 OverloadedOperatorKind OOK = OO_None;
13755 SourceLocation OOLoc;
13756 Expr *LHS = SimpleExpr;
13757 Expr *RHS = nullptr;
13758 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
13759 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
13760 OOLoc = BO->getOperatorLoc();
13761 LHS = BO->getLHS()->IgnoreParenImpCasts();
13762 RHS = BO->getRHS()->IgnoreParenImpCasts();
13763 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
13764 OOK = OCE->getOperator();
13765 OOLoc = OCE->getOperatorLoc();
13766 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
13767 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
13768 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
13769 OOK = MCE->getMethodDecl()
13770 ->getNameInfo()
13771 .getName()
13772 .getCXXOverloadedOperator();
13773 OOLoc = MCE->getCallee()->getExprLoc();
13774 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
13775 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000013776 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000013777 SourceLocation ELoc;
13778 SourceRange ERange;
Alexey Bataevbc529672018-09-28 19:33:14 +000013779 auto Res = getPrivateItem(*this, LHS, ELoc, ERange);
Alexey Bataev17daedf2018-02-15 22:42:57 +000013780 if (Res.second) {
13781 // It will be analyzed later.
13782 Vars.push_back(RefExpr);
13783 }
13784 ValueDecl *D = Res.first;
13785 if (!D)
13786 continue;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000013787
Alexey Bataev17daedf2018-02-15 22:42:57 +000013788 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
13789 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
13790 continue;
13791 }
13792 if (RHS) {
13793 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
13794 RHS, OMPC_depend, /*StrictlyPositive=*/false);
13795 if (RHSRes.isInvalid())
13796 continue;
13797 }
13798 if (!CurContext->isDependentContext() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000013799 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000013800 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013801 const ValueDecl *VD =
Alexey Bataev17daedf2018-02-15 22:42:57 +000013802 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
Alexey Bataeve3727102018-04-18 15:57:46 +000013803 if (VD)
Alexey Bataev17daedf2018-02-15 22:42:57 +000013804 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
13805 << 1 << VD;
Alexey Bataeve3727102018-04-18 15:57:46 +000013806 else
Alexey Bataev17daedf2018-02-15 22:42:57 +000013807 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
Alexey Bataev17daedf2018-02-15 22:42:57 +000013808 continue;
13809 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013810 OpsOffs.emplace_back(RHS, OOK);
Alexey Bataev17daedf2018-02-15 22:42:57 +000013811 } else {
13812 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
13813 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
13814 (ASE &&
13815 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() &&
13816 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
13817 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
13818 << RefExpr->getSourceRange();
13819 continue;
13820 }
Richard Smith2e3ed4a2019-08-16 19:53:22 +000013821
13822 ExprResult Res;
13823 {
13824 Sema::TentativeAnalysisScope Trap(*this);
13825 Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf,
13826 RefExpr->IgnoreParenImpCasts());
13827 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000013828 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
13829 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
13830 << RefExpr->getSourceRange();
13831 continue;
13832 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000013833 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000013834 Vars.push_back(RefExpr->IgnoreParenImpCasts());
Alexey Bataeva636c7f2015-12-23 10:27:45 +000013835 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000013836
13837 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
13838 TotalDepCount > VarList.size() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000013839 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000013840 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
13841 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
13842 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
13843 }
13844 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
13845 Vars.empty())
13846 return nullptr;
13847
Alexey Bataev8b427062016-05-25 12:36:08 +000013848 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataevf138fda2018-08-13 19:04:24 +000013849 DepKind, DepLoc, ColonLoc, Vars,
13850 TotalDepCount.getZExtValue());
Alexey Bataev17daedf2018-02-15 22:42:57 +000013851 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
13852 DSAStack->isParentOrderedRegion())
Alexey Bataev8b427062016-05-25 12:36:08 +000013853 DSAStack->addDoacrossDependClause(C, OpsOffs);
13854 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000013855}
Michael Wonge710d542015-08-07 16:16:36 +000013856
13857OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
13858 SourceLocation LParenLoc,
13859 SourceLocation EndLoc) {
13860 Expr *ValExpr = Device;
Alexey Bataev931e19b2017-10-02 16:32:39 +000013861 Stmt *HelperValStmt = nullptr;
Michael Wonge710d542015-08-07 16:16:36 +000013862
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013863 // OpenMP [2.9.1, Restrictions]
13864 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000013865 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
Alexey Bataeva0569352015-12-01 10:17:31 +000013866 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013867 return nullptr;
13868
Alexey Bataev931e19b2017-10-02 16:32:39 +000013869 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000013870 OpenMPDirectiveKind CaptureRegion =
13871 getOpenMPCaptureRegionForClause(DKind, OMPC_device);
13872 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013873 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013874 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev931e19b2017-10-02 16:32:39 +000013875 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13876 HelperValStmt = buildPreInits(Context, Captures);
13877 }
13878
Alexey Bataev8451efa2018-01-15 19:06:12 +000013879 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion,
13880 StartLoc, LParenLoc, EndLoc);
Michael Wonge710d542015-08-07 16:16:36 +000013881}
Kelvin Li0bff7af2015-11-23 05:32:03 +000013882
Alexey Bataeve3727102018-04-18 15:57:46 +000013883static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
Alexey Bataev95c23e72018-02-27 21:31:11 +000013884 DSAStackTy *Stack, QualType QTy,
13885 bool FullCheck = true) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000013886 NamedDecl *ND;
13887 if (QTy->isIncompleteType(&ND)) {
13888 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
13889 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +000013890 }
Alexey Bataev95c23e72018-02-27 21:31:11 +000013891 if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
13892 !QTy.isTrivialType(SemaRef.Context))
13893 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
Kelvin Li0bff7af2015-11-23 05:32:03 +000013894 return true;
13895}
13896
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000013897/// Return true if it can be proven that the provided array expression
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000013898/// (array section or array subscript) does NOT specify the whole size of the
13899/// array whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000013900static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000013901 const Expr *E,
13902 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013903 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000013904
13905 // If this is an array subscript, it refers to the whole size if the size of
13906 // the dimension is constant and equals 1. Also, an array section assumes the
13907 // format of an array subscript if no colon is used.
13908 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013909 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000013910 return ATy->getSize().getSExtValue() != 1;
13911 // Size can't be evaluated statically.
13912 return false;
13913 }
13914
13915 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000013916 const Expr *LowerBound = OASE->getLowerBound();
13917 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000013918
13919 // If there is a lower bound that does not evaluates to zero, we are not
David Majnemer9d168222016-08-05 17:44:54 +000013920 // covering the whole dimension.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000013921 if (LowerBound) {
Fangrui Song407659a2018-11-30 23:41:18 +000013922 Expr::EvalResult Result;
13923 if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000013924 return false; // Can't get the integer value as a constant.
Fangrui Song407659a2018-11-30 23:41:18 +000013925
13926 llvm::APSInt ConstLowerBound = Result.Val.getInt();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000013927 if (ConstLowerBound.getSExtValue())
13928 return true;
13929 }
13930
13931 // If we don't have a length we covering the whole dimension.
13932 if (!Length)
13933 return false;
13934
13935 // If the base is a pointer, we don't have a way to get the size of the
13936 // pointee.
13937 if (BaseQTy->isPointerType())
13938 return false;
13939
13940 // We can only check if the length is the same as the size of the dimension
13941 // if we have a constant array.
Alexey Bataeve3727102018-04-18 15:57:46 +000013942 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000013943 if (!CATy)
13944 return false;
13945
Fangrui Song407659a2018-11-30 23:41:18 +000013946 Expr::EvalResult Result;
13947 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000013948 return false; // Can't get the integer value as a constant.
13949
Fangrui Song407659a2018-11-30 23:41:18 +000013950 llvm::APSInt ConstLength = Result.Val.getInt();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000013951 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
13952}
13953
13954// Return true if it can be proven that the provided array expression (array
13955// section or array subscript) does NOT specify a single element of the array
13956// whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000013957static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
David Majnemer9d168222016-08-05 17:44:54 +000013958 const Expr *E,
13959 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013960 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000013961
13962 // An array subscript always refer to a single element. Also, an array section
13963 // assumes the format of an array subscript if no colon is used.
13964 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
13965 return false;
13966
13967 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000013968 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000013969
13970 // If we don't have a length we have to check if the array has unitary size
13971 // for this dimension. Also, we should always expect a length if the base type
13972 // is pointer.
13973 if (!Length) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013974 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000013975 return ATy->getSize().getSExtValue() != 1;
13976 // We cannot assume anything.
13977 return false;
13978 }
13979
13980 // Check if the length evaluates to 1.
Fangrui Song407659a2018-11-30 23:41:18 +000013981 Expr::EvalResult Result;
13982 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000013983 return false; // Can't get the integer value as a constant.
13984
Fangrui Song407659a2018-11-30 23:41:18 +000013985 llvm::APSInt ConstLength = Result.Val.getInt();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000013986 return ConstLength.getSExtValue() != 1;
13987}
13988
Samuel Antao661c0902016-05-26 17:39:58 +000013989// Return the expression of the base of the mappable expression or null if it
13990// cannot be determined and do all the necessary checks to see if the expression
13991// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000013992// components of the expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013993static const Expr *checkMapClauseExpressionBase(
Samuel Antao90927002016-04-26 14:54:23 +000013994 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000013995 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
Alexey Bataevb7a9b742017-12-05 19:20:09 +000013996 OpenMPClauseKind CKind, bool NoDiagnose) {
Samuel Antao5de996e2016-01-22 20:21:36 +000013997 SourceLocation ELoc = E->getExprLoc();
13998 SourceRange ERange = E->getSourceRange();
13999
14000 // The base of elements of list in a map clause have to be either:
14001 // - a reference to variable or field.
14002 // - a member expression.
14003 // - an array expression.
14004 //
14005 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
14006 // reference to 'r'.
14007 //
14008 // If we have:
14009 //
14010 // struct SS {
14011 // Bla S;
14012 // foo() {
14013 // #pragma omp target map (S.Arr[:12]);
14014 // }
14015 // }
14016 //
14017 // We want to retrieve the member expression 'this->S';
14018
Alexey Bataeve3727102018-04-18 15:57:46 +000014019 const Expr *RelevantExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000014020
Samuel Antao5de996e2016-01-22 20:21:36 +000014021 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
14022 // If a list item is an array section, it must specify contiguous storage.
14023 //
14024 // For this restriction it is sufficient that we make sure only references
14025 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014026 // exist except in the rightmost expression (unless they cover the whole
14027 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000014028 //
14029 // r.ArrS[3:5].Arr[6:7]
14030 //
14031 // r.ArrS[3:5].x
14032 //
14033 // but these would be valid:
14034 // r.ArrS[3].Arr[6:7]
14035 //
14036 // r.ArrS[3].x
14037
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014038 bool AllowUnitySizeArraySection = true;
14039 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000014040
Dmitry Polukhin644a9252016-03-11 07:58:34 +000014041 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000014042 E = E->IgnoreParenImpCasts();
14043
14044 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
14045 if (!isa<VarDecl>(CurE->getDecl()))
Alexey Bataev27041fa2017-12-05 15:22:49 +000014046 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000014047
14048 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014049
14050 // If we got a reference to a declaration, we should not expect any array
14051 // section before that.
14052 AllowUnitySizeArraySection = false;
14053 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000014054
14055 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000014056 CurComponents.emplace_back(CurE, CurE->getDecl());
14057 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000014058 Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts();
Samuel Antao5de996e2016-01-22 20:21:36 +000014059
14060 if (isa<CXXThisExpr>(BaseE))
14061 // We found a base expression: this->Val.
14062 RelevantExpr = CurE;
14063 else
14064 E = BaseE;
14065
14066 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000014067 if (!NoDiagnose) {
14068 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
14069 << CurE->getSourceRange();
14070 return nullptr;
14071 }
14072 if (RelevantExpr)
14073 return nullptr;
14074 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000014075 }
14076
14077 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
14078
14079 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
14080 // A bit-field cannot appear in a map clause.
14081 //
14082 if (FD->isBitField()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000014083 if (!NoDiagnose) {
14084 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
14085 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
14086 return nullptr;
14087 }
14088 if (RelevantExpr)
14089 return nullptr;
14090 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000014091 }
14092
14093 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
14094 // If the type of a list item is a reference to a type T then the type
14095 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014096 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000014097
14098 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
14099 // A list item cannot be a variable that is a member of a structure with
14100 // a union type.
14101 //
Alexey Bataeve3727102018-04-18 15:57:46 +000014102 if (CurType->isUnionType()) {
14103 if (!NoDiagnose) {
14104 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
14105 << CurE->getSourceRange();
14106 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000014107 }
Alexey Bataeve3727102018-04-18 15:57:46 +000014108 continue;
Alexey Bataevb7a9b742017-12-05 19:20:09 +000014109 }
Samuel Antao5de996e2016-01-22 20:21:36 +000014110
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014111 // If we got a member expression, we should not expect any array section
14112 // before that:
14113 //
14114 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
14115 // If a list item is an element of a structure, only the rightmost symbol
14116 // of the variable reference can be an array section.
14117 //
14118 AllowUnitySizeArraySection = false;
14119 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000014120
14121 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000014122 CurComponents.emplace_back(CurE, FD);
14123 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000014124 E = CurE->getBase()->IgnoreParenImpCasts();
14125
14126 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000014127 if (!NoDiagnose) {
14128 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
14129 << 0 << CurE->getSourceRange();
14130 return nullptr;
14131 }
14132 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000014133 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014134
14135 // If we got an array subscript that express the whole dimension we
14136 // can have any array expressions before. If it only expressing part of
14137 // the dimension, we can only have unitary-size array expressions.
Alexey Bataeve3727102018-04-18 15:57:46 +000014138 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014139 E->getType()))
14140 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000014141
Patrick Lystere13b1e32019-01-02 19:28:48 +000014142 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) {
14143 Expr::EvalResult Result;
14144 if (CurE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext())) {
14145 if (!Result.Val.getInt().isNullValue()) {
14146 SemaRef.Diag(CurE->getIdx()->getExprLoc(),
14147 diag::err_omp_invalid_map_this_expr);
14148 SemaRef.Diag(CurE->getIdx()->getExprLoc(),
14149 diag::note_omp_invalid_subscript_on_this_ptr_map);
14150 }
14151 }
14152 RelevantExpr = TE;
14153 }
14154
Samuel Antao90927002016-04-26 14:54:23 +000014155 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000014156 CurComponents.emplace_back(CurE, nullptr);
14157 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000014158 assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
Samuel Antao5de996e2016-01-22 20:21:36 +000014159 E = CurE->getBase()->IgnoreParenImpCasts();
14160
Alexey Bataev27041fa2017-12-05 15:22:49 +000014161 QualType CurType =
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014162 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
14163
Samuel Antao5de996e2016-01-22 20:21:36 +000014164 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
14165 // If the type of a list item is a reference to a type T then the type
14166 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000014167 if (CurType->isReferenceType())
14168 CurType = CurType->getPointeeType();
14169
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014170 bool IsPointer = CurType->isAnyPointerType();
14171
14172 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000014173 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
14174 << 0 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000014175 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000014176 }
14177
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014178 bool NotWhole =
Alexey Bataeve3727102018-04-18 15:57:46 +000014179 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014180 bool NotUnity =
Alexey Bataeve3727102018-04-18 15:57:46 +000014181 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014182
Samuel Antaodab51bb2016-07-18 23:22:11 +000014183 if (AllowWholeSizeArraySection) {
14184 // Any array section is currently allowed. Allowing a whole size array
14185 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014186 //
14187 // If this array section refers to the whole dimension we can still
14188 // accept other array sections before this one, except if the base is a
14189 // pointer. Otherwise, only unitary sections are accepted.
14190 if (NotWhole || IsPointer)
14191 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +000014192 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014193 // A unity or whole array section is not allowed and that is not
14194 // compatible with the properties of the current array section.
14195 SemaRef.Diag(
14196 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
14197 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000014198 return nullptr;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000014199 }
Samuel Antao90927002016-04-26 14:54:23 +000014200
Patrick Lystere13b1e32019-01-02 19:28:48 +000014201 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) {
14202 Expr::EvalResult ResultR;
14203 Expr::EvalResult ResultL;
14204 if (CurE->getLength()->EvaluateAsInt(ResultR,
14205 SemaRef.getASTContext())) {
14206 if (!ResultR.Val.getInt().isOneValue()) {
14207 SemaRef.Diag(CurE->getLength()->getExprLoc(),
14208 diag::err_omp_invalid_map_this_expr);
14209 SemaRef.Diag(CurE->getLength()->getExprLoc(),
14210 diag::note_omp_invalid_length_on_this_ptr_mapping);
14211 }
14212 }
14213 if (CurE->getLowerBound() && CurE->getLowerBound()->EvaluateAsInt(
14214 ResultL, SemaRef.getASTContext())) {
14215 if (!ResultL.Val.getInt().isNullValue()) {
14216 SemaRef.Diag(CurE->getLowerBound()->getExprLoc(),
14217 diag::err_omp_invalid_map_this_expr);
14218 SemaRef.Diag(CurE->getLowerBound()->getExprLoc(),
14219 diag::note_omp_invalid_lower_bound_on_this_ptr_mapping);
14220 }
14221 }
14222 RelevantExpr = TE;
14223 }
14224
Samuel Antao90927002016-04-26 14:54:23 +000014225 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000014226 CurComponents.emplace_back(CurE, nullptr);
14227 } else {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000014228 if (!NoDiagnose) {
14229 // If nothing else worked, this is not a valid map clause expression.
14230 SemaRef.Diag(
14231 ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
14232 << ERange;
14233 }
Alexey Bataev27041fa2017-12-05 15:22:49 +000014234 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000014235 }
Samuel Antao5de996e2016-01-22 20:21:36 +000014236 }
14237
14238 return RelevantExpr;
14239}
14240
14241// Return true if expression E associated with value VD has conflicts with other
14242// map information.
Alexey Bataeve3727102018-04-18 15:57:46 +000014243static bool checkMapConflicts(
14244 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
Samuel Antao90927002016-04-26 14:54:23 +000014245 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000014246 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
14247 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000014248 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000014249 SourceLocation ELoc = E->getExprLoc();
14250 SourceRange ERange = E->getSourceRange();
14251
14252 // In order to easily check the conflicts we need to match each component of
14253 // the expression under test with the components of the expressions that are
14254 // already in the stack.
14255
Samuel Antao5de996e2016-01-22 20:21:36 +000014256 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000014257 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000014258 "Map clause expression with unexpected base!");
14259
14260 // Variables to help detecting enclosing problems in data environment nests.
14261 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000014262 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000014263
Samuel Antao90927002016-04-26 14:54:23 +000014264 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
14265 VD, CurrentRegionOnly,
Alexey Bataeve3727102018-04-18 15:57:46 +000014266 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
14267 ERange, CKind, &EnclosingExpr,
14268 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
14269 StackComponents,
14270 OpenMPClauseKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000014271 assert(!StackComponents.empty() &&
14272 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000014273 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000014274 "Map clause expression with unexpected base!");
Fangrui Song16fe49a2018-04-18 19:32:01 +000014275 (void)VD;
Samuel Antao5de996e2016-01-22 20:21:36 +000014276
Samuel Antao90927002016-04-26 14:54:23 +000014277 // The whole expression in the stack.
Alexey Bataeve3727102018-04-18 15:57:46 +000014278 const Expr *RE = StackComponents.front().getAssociatedExpression();
Samuel Antao90927002016-04-26 14:54:23 +000014279
Samuel Antao5de996e2016-01-22 20:21:36 +000014280 // Expressions must start from the same base. Here we detect at which
14281 // point both expressions diverge from each other and see if we can
14282 // detect if the memory referred to both expressions is contiguous and
14283 // do not overlap.
14284 auto CI = CurComponents.rbegin();
14285 auto CE = CurComponents.rend();
14286 auto SI = StackComponents.rbegin();
14287 auto SE = StackComponents.rend();
14288 for (; CI != CE && SI != SE; ++CI, ++SI) {
14289
14290 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
14291 // At most one list item can be an array item derived from a given
14292 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000014293 if (CurrentRegionOnly &&
14294 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
14295 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
14296 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
14297 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
14298 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000014299 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000014300 << CI->getAssociatedExpression()->getSourceRange();
14301 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
14302 diag::note_used_here)
14303 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000014304 return true;
14305 }
14306
14307 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000014308 if (CI->getAssociatedExpression()->getStmtClass() !=
14309 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000014310 break;
14311
14312 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000014313 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000014314 break;
14315 }
Kelvin Li9f645ae2016-07-18 22:49:16 +000014316 // Check if the extra components of the expressions in the enclosing
14317 // data environment are redundant for the current base declaration.
14318 // If they are, the maps completely overlap, which is legal.
14319 for (; SI != SE; ++SI) {
14320 QualType Type;
Alexey Bataeve3727102018-04-18 15:57:46 +000014321 if (const auto *ASE =
David Majnemer9d168222016-08-05 17:44:54 +000014322 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +000014323 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +000014324 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(
David Majnemer9d168222016-08-05 17:44:54 +000014325 SI->getAssociatedExpression())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000014326 const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
Kelvin Li9f645ae2016-07-18 22:49:16 +000014327 Type =
14328 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
14329 }
14330 if (Type.isNull() || Type->isAnyPointerType() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000014331 checkArrayExpressionDoesNotReferToWholeSize(
Kelvin Li9f645ae2016-07-18 22:49:16 +000014332 SemaRef, SI->getAssociatedExpression(), Type))
14333 break;
14334 }
Samuel Antao5de996e2016-01-22 20:21:36 +000014335
14336 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
14337 // List items of map clauses in the same construct must not share
14338 // original storage.
14339 //
14340 // If the expressions are exactly the same or one is a subset of the
14341 // other, it means they are sharing storage.
14342 if (CI == CE && SI == SE) {
14343 if (CurrentRegionOnly) {
Alexey Bataeve3727102018-04-18 15:57:46 +000014344 if (CKind == OMPC_map) {
Samuel Antao661c0902016-05-26 17:39:58 +000014345 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000014346 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000014347 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000014348 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
14349 << ERange;
14350 }
Samuel Antao5de996e2016-01-22 20:21:36 +000014351 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
14352 << RE->getSourceRange();
14353 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000014354 }
Alexey Bataeve3727102018-04-18 15:57:46 +000014355 // If we find the same expression in the enclosing data environment,
14356 // that is legal.
14357 IsEnclosedByDataEnvironmentExpr = true;
14358 return false;
Samuel Antao5de996e2016-01-22 20:21:36 +000014359 }
14360
Samuel Antao90927002016-04-26 14:54:23 +000014361 QualType DerivedType =
14362 std::prev(CI)->getAssociatedDeclaration()->getType();
14363 SourceLocation DerivedLoc =
14364 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000014365
14366 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
14367 // If the type of a list item is a reference to a type T then the type
14368 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000014369 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000014370
14371 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
14372 // A variable for which the type is pointer and an array section
14373 // derived from that variable must not appear as list items of map
14374 // clauses of the same construct.
14375 //
14376 // Also, cover one of the cases in:
14377 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
14378 // If any part of the original storage of a list item has corresponding
14379 // storage in the device data environment, all of the original storage
14380 // must have corresponding storage in the device data environment.
14381 //
14382 if (DerivedType->isAnyPointerType()) {
14383 if (CI == CE || SI == SE) {
14384 SemaRef.Diag(
14385 DerivedLoc,
14386 diag::err_omp_pointer_mapped_along_with_derived_section)
14387 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000014388 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
14389 << RE->getSourceRange();
14390 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +000014391 }
14392 if (CI->getAssociatedExpression()->getStmtClass() !=
Alexey Bataev2819260b2018-02-27 17:42:00 +000014393 SI->getAssociatedExpression()->getStmtClass() ||
14394 CI->getAssociatedDeclaration()->getCanonicalDecl() ==
14395 SI->getAssociatedDeclaration()->getCanonicalDecl()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000014396 assert(CI != CE && SI != SE);
Alexey Bataev2819260b2018-02-27 17:42:00 +000014397 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
Samuel Antao5de996e2016-01-22 20:21:36 +000014398 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000014399 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
14400 << RE->getSourceRange();
14401 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000014402 }
Samuel Antao5de996e2016-01-22 20:21:36 +000014403 }
14404
14405 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
14406 // List items of map clauses in the same construct must not share
14407 // original storage.
14408 //
14409 // An expression is a subset of the other.
14410 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000014411 if (CKind == OMPC_map) {
Alexey Bataeve82445f2018-09-20 13:54:02 +000014412 if (CI != CE || SI != SE) {
14413 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
14414 // a pointer.
14415 auto Begin =
14416 CI != CE ? CurComponents.begin() : StackComponents.begin();
14417 auto End = CI != CE ? CurComponents.end() : StackComponents.end();
14418 auto It = Begin;
14419 while (It != End && !It->getAssociatedDeclaration())
14420 std::advance(It, 1);
14421 assert(It != End &&
14422 "Expected at least one component with the declaration.");
14423 if (It != Begin && It->getAssociatedDeclaration()
14424 ->getType()
14425 .getCanonicalType()
14426 ->isAnyPointerType()) {
14427 IsEnclosedByDataEnvironmentExpr = false;
14428 EnclosingExpr = nullptr;
14429 return false;
14430 }
14431 }
Samuel Antao661c0902016-05-26 17:39:58 +000014432 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000014433 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000014434 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000014435 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
14436 << ERange;
14437 }
Samuel Antao5de996e2016-01-22 20:21:36 +000014438 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
14439 << RE->getSourceRange();
14440 return true;
14441 }
14442
14443 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000014444 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000014445 if (!CurrentRegionOnly && SI != SE)
14446 EnclosingExpr = RE;
14447
14448 // The current expression is a subset of the expression in the data
14449 // environment.
14450 IsEnclosedByDataEnvironmentExpr |=
14451 (!CurrentRegionOnly && CI != CE && SI == SE);
14452
14453 return false;
14454 });
14455
14456 if (CurrentRegionOnly)
14457 return FoundError;
14458
14459 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
14460 // If any part of the original storage of a list item has corresponding
14461 // storage in the device data environment, all of the original storage must
14462 // have corresponding storage in the device data environment.
14463 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
14464 // If a list item is an element of a structure, and a different element of
14465 // the structure has a corresponding list item in the device data environment
14466 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000014467 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000014468 // data environment prior to the task encountering the construct.
14469 //
14470 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
14471 SemaRef.Diag(ELoc,
14472 diag::err_omp_original_storage_is_shared_and_does_not_contain)
14473 << ERange;
14474 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
14475 << EnclosingExpr->getSourceRange();
14476 return true;
14477 }
14478
14479 return FoundError;
14480}
14481
Michael Kruse4304e9d2019-02-19 16:38:20 +000014482// Look up the user-defined mapper given the mapper name and mapped type, and
14483// build a reference to it.
Benjamin Kramerba2ea932019-03-28 17:18:42 +000014484static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S,
14485 CXXScopeSpec &MapperIdScopeSpec,
14486 const DeclarationNameInfo &MapperId,
14487 QualType Type,
14488 Expr *UnresolvedMapper) {
Michael Kruse4304e9d2019-02-19 16:38:20 +000014489 if (MapperIdScopeSpec.isInvalid())
14490 return ExprError();
14491 // Find all user-defined mappers with the given MapperId.
14492 SmallVector<UnresolvedSet<8>, 4> Lookups;
14493 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName);
14494 Lookup.suppressDiagnostics();
14495 if (S) {
14496 while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) {
14497 NamedDecl *D = Lookup.getRepresentativeDecl();
14498 while (S && !S->isDeclScope(D))
14499 S = S->getParent();
14500 if (S)
14501 S = S->getParent();
14502 Lookups.emplace_back();
14503 Lookups.back().append(Lookup.begin(), Lookup.end());
14504 Lookup.clear();
14505 }
14506 } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) {
14507 // Extract the user-defined mappers with the given MapperId.
14508 Lookups.push_back(UnresolvedSet<8>());
14509 for (NamedDecl *D : ULE->decls()) {
14510 auto *DMD = cast<OMPDeclareMapperDecl>(D);
14511 assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation.");
14512 Lookups.back().addDecl(DMD);
14513 }
14514 }
14515 // Defer the lookup for dependent types. The results will be passed through
14516 // UnresolvedMapper on instantiation.
14517 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() ||
14518 Type->isInstantiationDependentType() ||
14519 Type->containsUnexpandedParameterPack() ||
14520 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) {
14521 return !D->isInvalidDecl() &&
14522 (D->getType()->isDependentType() ||
14523 D->getType()->isInstantiationDependentType() ||
14524 D->getType()->containsUnexpandedParameterPack());
14525 })) {
14526 UnresolvedSet<8> URS;
14527 for (const UnresolvedSet<8> &Set : Lookups) {
14528 if (Set.empty())
14529 continue;
14530 URS.append(Set.begin(), Set.end());
14531 }
14532 return UnresolvedLookupExpr::Create(
14533 SemaRef.Context, /*NamingClass=*/nullptr,
14534 MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId,
14535 /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end());
14536 }
14537 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
14538 // The type must be of struct, union or class type in C and C++
14539 if (!Type->isStructureOrClassType() && !Type->isUnionType())
14540 return ExprEmpty();
14541 SourceLocation Loc = MapperId.getLoc();
14542 // Perform argument dependent lookup.
14543 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet())
14544 argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups);
14545 // Return the first user-defined mapper with the desired type.
14546 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
14547 Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * {
14548 if (!D->isInvalidDecl() &&
14549 SemaRef.Context.hasSameType(D->getType(), Type))
14550 return D;
14551 return nullptr;
14552 }))
14553 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc);
14554 // Find the first user-defined mapper with a type derived from the desired
14555 // type.
14556 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
14557 Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * {
14558 if (!D->isInvalidDecl() &&
14559 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) &&
14560 !Type.isMoreQualifiedThan(D->getType()))
14561 return D;
14562 return nullptr;
14563 })) {
14564 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
14565 /*DetectVirtual=*/false);
14566 if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) {
14567 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
14568 VD->getType().getUnqualifiedType()))) {
14569 if (SemaRef.CheckBaseClassAccess(
14570 Loc, VD->getType(), Type, Paths.front(),
14571 /*DiagID=*/0) != Sema::AR_inaccessible) {
14572 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc);
14573 }
14574 }
14575 }
14576 }
14577 // Report error if a mapper is specified, but cannot be found.
14578 if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") {
14579 SemaRef.Diag(Loc, diag::err_omp_invalid_mapper)
14580 << Type << MapperId.getName();
14581 return ExprError();
14582 }
14583 return ExprEmpty();
14584}
14585
Samuel Antao661c0902016-05-26 17:39:58 +000014586namespace {
14587// Utility struct that gathers all the related lists associated with a mappable
14588// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000014589struct MappableVarListInfo {
Samuel Antao661c0902016-05-26 17:39:58 +000014590 // The list of expressions.
14591 ArrayRef<Expr *> VarList;
14592 // The list of processed expressions.
14593 SmallVector<Expr *, 16> ProcessedVarList;
14594 // The mappble components for each expression.
14595 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
14596 // The base declaration of the variable.
14597 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
Michael Kruse4304e9d2019-02-19 16:38:20 +000014598 // The reference to the user-defined mapper associated with every expression.
14599 SmallVector<Expr *, 16> UDMapperList;
Samuel Antao661c0902016-05-26 17:39:58 +000014600
14601 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
14602 // We have a list of components and base declarations for each entry in the
14603 // variable list.
14604 VarComponents.reserve(VarList.size());
14605 VarBaseDeclarations.reserve(VarList.size());
14606 }
14607};
14608}
14609
14610// Check the validity of the provided variable list for the provided clause kind
Michael Kruse4304e9d2019-02-19 16:38:20 +000014611// \a CKind. In the check process the valid expressions, mappable expression
14612// components, variables, and user-defined mappers are extracted and used to
14613// fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a
14614// UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec,
14615// and \a MapperId are expected to be valid if the clause kind is 'map'.
14616static void checkMappableExpressionList(
14617 Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind,
14618 MappableVarListInfo &MVLI, SourceLocation StartLoc,
Michael Kruse01f670d2019-02-22 22:29:42 +000014619 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId,
14620 ArrayRef<Expr *> UnresolvedMappers,
Michael Kruse4304e9d2019-02-19 16:38:20 +000014621 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
Michael Kruse01f670d2019-02-22 22:29:42 +000014622 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000014623 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
14624 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000014625 "Unexpected clause kind with mappable expressions!");
Michael Kruse01f670d2019-02-22 22:29:42 +000014626
14627 // If the identifier of user-defined mapper is not specified, it is "default".
14628 // We do not change the actual name in this clause to distinguish whether a
14629 // mapper is specified explicitly, i.e., it is not explicitly specified when
14630 // MapperId.getName() is empty.
14631 if (!MapperId.getName() || MapperId.getName().isEmpty()) {
14632 auto &DeclNames = SemaRef.getASTContext().DeclarationNames;
14633 MapperId.setName(DeclNames.getIdentifier(
14634 &SemaRef.getASTContext().Idents.get("default")));
14635 }
Michael Kruse4304e9d2019-02-19 16:38:20 +000014636
14637 // Iterators to find the current unresolved mapper expression.
14638 auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end();
14639 bool UpdateUMIt = false;
14640 Expr *UnresolvedMapper = nullptr;
Kelvin Li0bff7af2015-11-23 05:32:03 +000014641
Samuel Antao90927002016-04-26 14:54:23 +000014642 // Keep track of the mappable components and base declarations in this clause.
14643 // Each entry in the list is going to have a list of components associated. We
14644 // record each set of the components so that we can build the clause later on.
14645 // In the end we should have the same amount of declarations and component
14646 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000014647
Alexey Bataeve3727102018-04-18 15:57:46 +000014648 for (Expr *RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000014649 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000014650 SourceLocation ELoc = RE->getExprLoc();
14651
Michael Kruse4304e9d2019-02-19 16:38:20 +000014652 // Find the current unresolved mapper expression.
14653 if (UpdateUMIt && UMIt != UMEnd) {
14654 UMIt++;
14655 assert(
14656 UMIt != UMEnd &&
14657 "Expect the size of UnresolvedMappers to match with that of VarList");
14658 }
14659 UpdateUMIt = true;
14660 if (UMIt != UMEnd)
14661 UnresolvedMapper = *UMIt;
14662
Alexey Bataeve3727102018-04-18 15:57:46 +000014663 const Expr *VE = RE->IgnoreParenLValueCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000014664
14665 if (VE->isValueDependent() || VE->isTypeDependent() ||
14666 VE->isInstantiationDependent() ||
14667 VE->containsUnexpandedParameterPack()) {
Michael Kruse0336c752019-02-25 20:34:15 +000014668 // Try to find the associated user-defined mapper.
14669 ExprResult ER = buildUserDefinedMapperRef(
14670 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
14671 VE->getType().getCanonicalType(), UnresolvedMapper);
14672 if (ER.isInvalid())
14673 continue;
14674 MVLI.UDMapperList.push_back(ER.get());
Samuel Antao5de996e2016-01-22 20:21:36 +000014675 // We can only analyze this information once the missing information is
14676 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000014677 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000014678 continue;
14679 }
14680
Alexey Bataeve3727102018-04-18 15:57:46 +000014681 Expr *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000014682
Samuel Antao5de996e2016-01-22 20:21:36 +000014683 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000014684 SemaRef.Diag(ELoc,
14685 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000014686 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000014687 continue;
14688 }
14689
Samuel Antao90927002016-04-26 14:54:23 +000014690 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
14691 ValueDecl *CurDeclaration = nullptr;
14692
14693 // Obtain the array or member expression bases if required. Also, fill the
14694 // components array with all the components identified in the process.
Alexey Bataeve3727102018-04-18 15:57:46 +000014695 const Expr *BE = checkMapClauseExpressionBase(
14696 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false);
Samuel Antao5de996e2016-01-22 20:21:36 +000014697 if (!BE)
14698 continue;
14699
Samuel Antao90927002016-04-26 14:54:23 +000014700 assert(!CurComponents.empty() &&
14701 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000014702
Patrick Lystere13b1e32019-01-02 19:28:48 +000014703 if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) {
14704 // Add store "this" pointer to class in DSAStackTy for future checking
14705 DSAS->addMappedClassesQualTypes(TE->getType());
Michael Kruse0336c752019-02-25 20:34:15 +000014706 // Try to find the associated user-defined mapper.
14707 ExprResult ER = buildUserDefinedMapperRef(
14708 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
14709 VE->getType().getCanonicalType(), UnresolvedMapper);
14710 if (ER.isInvalid())
14711 continue;
14712 MVLI.UDMapperList.push_back(ER.get());
Patrick Lystere13b1e32019-01-02 19:28:48 +000014713 // Skip restriction checking for variable or field declarations
14714 MVLI.ProcessedVarList.push_back(RE);
14715 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
14716 MVLI.VarComponents.back().append(CurComponents.begin(),
14717 CurComponents.end());
14718 MVLI.VarBaseDeclarations.push_back(nullptr);
14719 continue;
14720 }
14721
Samuel Antao90927002016-04-26 14:54:23 +000014722 // For the following checks, we rely on the base declaration which is
14723 // expected to be associated with the last component. The declaration is
14724 // expected to be a variable or a field (if 'this' is being mapped).
14725 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
14726 assert(CurDeclaration && "Null decl on map clause.");
14727 assert(
14728 CurDeclaration->isCanonicalDecl() &&
14729 "Expecting components to have associated only canonical declarations.");
14730
14731 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
Alexey Bataeve3727102018-04-18 15:57:46 +000014732 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000014733
14734 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000014735 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000014736
14737 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000014738 // threadprivate variables cannot appear in a map clause.
14739 // OpenMP 4.5 [2.10.5, target update Construct]
14740 // threadprivate variables cannot appear in a from clause.
14741 if (VD && DSAS->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000014742 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000014743 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
14744 << getOpenMPClauseName(CKind);
Alexey Bataeve3727102018-04-18 15:57:46 +000014745 reportOriginalDsa(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000014746 continue;
14747 }
14748
Samuel Antao5de996e2016-01-22 20:21:36 +000014749 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
14750 // A list item cannot appear in both a map clause and a data-sharing
14751 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000014752
Samuel Antao5de996e2016-01-22 20:21:36 +000014753 // Check conflicts with other map clause expressions. We check the conflicts
14754 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000014755 // environment, because the restrictions are different. We only have to
14756 // check conflicts across regions for the map clauses.
Alexey Bataeve3727102018-04-18 15:57:46 +000014757 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000014758 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000014759 break;
Samuel Antao661c0902016-05-26 17:39:58 +000014760 if (CKind == OMPC_map &&
Alexey Bataeve3727102018-04-18 15:57:46 +000014761 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000014762 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000014763 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000014764
Samuel Antao661c0902016-05-26 17:39:58 +000014765 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000014766 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
14767 // If the type of a list item is a reference to a type T then the type will
14768 // be considered to be T for all purposes of this clause.
Alexey Bataev354df2e2018-05-02 18:44:10 +000014769 auto I = llvm::find_if(
14770 CurComponents,
14771 [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
14772 return MC.getAssociatedDeclaration();
14773 });
14774 assert(I != CurComponents.end() && "Null decl on map clause.");
14775 QualType Type =
14776 I->getAssociatedDeclaration()->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000014777
Samuel Antao661c0902016-05-26 17:39:58 +000014778 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
14779 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000014780 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000014781 // A list item must have a mappable type.
Alexey Bataeve3727102018-04-18 15:57:46 +000014782 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
Samuel Antao661c0902016-05-26 17:39:58 +000014783 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000014784 continue;
14785
Samuel Antao661c0902016-05-26 17:39:58 +000014786 if (CKind == OMPC_map) {
14787 // target enter data
14788 // OpenMP [2.10.2, Restrictions, p. 99]
14789 // A map-type must be specified in all map clauses and must be either
14790 // to or alloc.
14791 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
14792 if (DKind == OMPD_target_enter_data &&
14793 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
14794 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
14795 << (IsMapTypeImplicit ? 1 : 0)
14796 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
14797 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000014798 continue;
14799 }
Samuel Antao661c0902016-05-26 17:39:58 +000014800
14801 // target exit_data
14802 // OpenMP [2.10.3, Restrictions, p. 102]
14803 // A map-type must be specified in all map clauses and must be either
14804 // from, release, or delete.
14805 if (DKind == OMPD_target_exit_data &&
14806 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
14807 MapType == OMPC_MAP_delete)) {
14808 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
14809 << (IsMapTypeImplicit ? 1 : 0)
14810 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
14811 << getOpenMPDirectiveName(DKind);
14812 continue;
14813 }
14814
14815 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
14816 // A list item cannot appear in both a map clause and a data-sharing
14817 // attribute clause on the same construct
Joel E. Denny7d5bc552019-08-22 03:34:30 +000014818 //
14819 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
14820 // A list item cannot appear in both a map clause and a data-sharing
14821 // attribute clause on the same construct unless the construct is a
14822 // combined construct.
14823 if (VD && ((SemaRef.LangOpts.OpenMP <= 45 &&
14824 isOpenMPTargetExecutionDirective(DKind)) ||
14825 DKind == OMPD_target)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000014826 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000014827 if (isOpenMPPrivate(DVar.CKind)) {
Samuel Antao6890b092016-07-28 14:25:09 +000014828 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Samuel Antao661c0902016-05-26 17:39:58 +000014829 << getOpenMPClauseName(DVar.CKind)
Samuel Antao6890b092016-07-28 14:25:09 +000014830 << getOpenMPClauseName(OMPC_map)
Samuel Antao661c0902016-05-26 17:39:58 +000014831 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000014832 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar);
Samuel Antao661c0902016-05-26 17:39:58 +000014833 continue;
14834 }
14835 }
Michael Kruse01f670d2019-02-22 22:29:42 +000014836 }
Michael Kruse4304e9d2019-02-19 16:38:20 +000014837
Michael Kruse01f670d2019-02-22 22:29:42 +000014838 // Try to find the associated user-defined mapper.
Michael Kruse0336c752019-02-25 20:34:15 +000014839 ExprResult ER = buildUserDefinedMapperRef(
14840 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
14841 Type.getCanonicalType(), UnresolvedMapper);
14842 if (ER.isInvalid())
14843 continue;
14844 MVLI.UDMapperList.push_back(ER.get());
Carlo Bertollib74bfc82016-03-18 21:43:32 +000014845
Samuel Antao90927002016-04-26 14:54:23 +000014846 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000014847 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000014848
14849 // Store the components in the stack so that they can be used to check
14850 // against other clauses later on.
Samuel Antao6890b092016-07-28 14:25:09 +000014851 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
14852 /*WhereFoundClauseKind=*/OMPC_map);
Samuel Antao90927002016-04-26 14:54:23 +000014853
14854 // Save the components and declaration to create the clause. For purposes of
14855 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000014856 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000014857 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
14858 MVLI.VarComponents.back().append(CurComponents.begin(),
14859 CurComponents.end());
14860 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
14861 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000014862 }
Samuel Antao661c0902016-05-26 17:39:58 +000014863}
14864
Michael Kruse4304e9d2019-02-19 16:38:20 +000014865OMPClause *Sema::ActOnOpenMPMapClause(
14866 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
14867 ArrayRef<SourceLocation> MapTypeModifiersLoc,
14868 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
14869 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc,
14870 SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
14871 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) {
14872 OpenMPMapModifierKind Modifiers[] = {OMPC_MAP_MODIFIER_unknown,
14873 OMPC_MAP_MODIFIER_unknown,
14874 OMPC_MAP_MODIFIER_unknown};
Kelvin Lief579432018-12-18 22:18:41 +000014875 SourceLocation ModifiersLoc[OMPMapClause::NumberOfModifiers];
14876
14877 // Process map-type-modifiers, flag errors for duplicate modifiers.
14878 unsigned Count = 0;
14879 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) {
14880 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown &&
14881 llvm::find(Modifiers, MapTypeModifiers[I]) != std::end(Modifiers)) {
14882 Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier);
14883 continue;
14884 }
14885 assert(Count < OMPMapClause::NumberOfModifiers &&
Gheorghe-Teodor Berceaa3afcf22019-01-09 20:38:35 +000014886 "Modifiers exceed the allowed number of map type modifiers");
Kelvin Lief579432018-12-18 22:18:41 +000014887 Modifiers[Count] = MapTypeModifiers[I];
14888 ModifiersLoc[Count] = MapTypeModifiersLoc[I];
14889 ++Count;
14890 }
14891
Michael Kruse4304e9d2019-02-19 16:38:20 +000014892 MappableVarListInfo MVLI(VarList);
14893 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc,
Michael Kruse01f670d2019-02-22 22:29:42 +000014894 MapperIdScopeSpec, MapperId, UnresolvedMappers,
14895 MapType, IsMapTypeImplicit);
Michael Kruse4304e9d2019-02-19 16:38:20 +000014896
Samuel Antao5de996e2016-01-22 20:21:36 +000014897 // We need to produce a map clause even if we don't have variables so that
14898 // other diagnostics related with non-existing map clauses are accurate.
Michael Kruse4304e9d2019-02-19 16:38:20 +000014899 return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList,
14900 MVLI.VarBaseDeclarations, MVLI.VarComponents,
14901 MVLI.UDMapperList, Modifiers, ModifiersLoc,
14902 MapperIdScopeSpec.getWithLocInContext(Context),
14903 MapperId, MapType, IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000014904}
Kelvin Li099bb8c2015-11-24 20:50:12 +000014905
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000014906QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
14907 TypeResult ParsedType) {
14908 assert(ParsedType.isUsable());
14909
14910 QualType ReductionType = GetTypeFromParser(ParsedType.get());
14911 if (ReductionType.isNull())
14912 return QualType();
14913
14914 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
14915 // A type name in a declare reduction directive cannot be a function type, an
14916 // array type, a reference type, or a type qualified with const, volatile or
14917 // restrict.
14918 if (ReductionType.hasQualifiers()) {
14919 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
14920 return QualType();
14921 }
14922
14923 if (ReductionType->isFunctionType()) {
14924 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
14925 return QualType();
14926 }
14927 if (ReductionType->isReferenceType()) {
14928 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
14929 return QualType();
14930 }
14931 if (ReductionType->isArrayType()) {
14932 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
14933 return QualType();
14934 }
14935 return ReductionType;
14936}
14937
14938Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
14939 Scope *S, DeclContext *DC, DeclarationName Name,
14940 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
14941 AccessSpecifier AS, Decl *PrevDeclInScope) {
14942 SmallVector<Decl *, 8> Decls;
14943 Decls.reserve(ReductionTypes.size());
14944
14945 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
Richard Smithbecb92d2017-10-10 22:33:17 +000014946 forRedeclarationInCurContext());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000014947 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
14948 // A reduction-identifier may not be re-declared in the current scope for the
14949 // same type or for a type that is compatible according to the base language
14950 // rules.
14951 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
14952 OMPDeclareReductionDecl *PrevDRD = nullptr;
14953 bool InCompoundScope = true;
14954 if (S != nullptr) {
14955 // Find previous declaration with the same name not referenced in other
14956 // declarations.
14957 FunctionScopeInfo *ParentFn = getEnclosingFunction();
14958 InCompoundScope =
14959 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
14960 LookupName(Lookup, S);
14961 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
14962 /*AllowInlineNamespace=*/false);
14963 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
Alexey Bataeve3727102018-04-18 15:57:46 +000014964 LookupResult::Filter Filter = Lookup.makeFilter();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000014965 while (Filter.hasNext()) {
14966 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
14967 if (InCompoundScope) {
14968 auto I = UsedAsPrevious.find(PrevDecl);
14969 if (I == UsedAsPrevious.end())
14970 UsedAsPrevious[PrevDecl] = false;
Alexey Bataeve3727102018-04-18 15:57:46 +000014971 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000014972 UsedAsPrevious[D] = true;
14973 }
14974 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
14975 PrevDecl->getLocation();
14976 }
14977 Filter.done();
14978 if (InCompoundScope) {
Alexey Bataeve3727102018-04-18 15:57:46 +000014979 for (const auto &PrevData : UsedAsPrevious) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000014980 if (!PrevData.second) {
14981 PrevDRD = PrevData.first;
14982 break;
14983 }
14984 }
14985 }
14986 } else if (PrevDeclInScope != nullptr) {
14987 auto *PrevDRDInScope = PrevDRD =
14988 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
14989 do {
14990 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
14991 PrevDRDInScope->getLocation();
14992 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
14993 } while (PrevDRDInScope != nullptr);
14994 }
Alexey Bataeve3727102018-04-18 15:57:46 +000014995 for (const auto &TyData : ReductionTypes) {
14996 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000014997 bool Invalid = false;
14998 if (I != PreviousRedeclTypes.end()) {
14999 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
15000 << TyData.first;
15001 Diag(I->second, diag::note_previous_definition);
15002 Invalid = true;
15003 }
15004 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
15005 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
15006 Name, TyData.first, PrevDRD);
15007 DC->addDecl(DRD);
15008 DRD->setAccess(AS);
15009 Decls.push_back(DRD);
15010 if (Invalid)
15011 DRD->setInvalidDecl();
15012 else
15013 PrevDRD = DRD;
15014 }
15015
15016 return DeclGroupPtrTy::make(
15017 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
15018}
15019
15020void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
15021 auto *DRD = cast<OMPDeclareReductionDecl>(D);
15022
15023 // Enter new function scope.
15024 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000015025 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015026 getCurFunction()->setHasOMPDeclareReductionCombiner();
15027
15028 if (S != nullptr)
15029 PushDeclContext(S, DRD);
15030 else
15031 CurContext = DRD;
15032
Faisal Valid143a0c2017-04-01 21:30:49 +000015033 PushExpressionEvaluationContext(
15034 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015035
15036 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000015037 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
15038 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
15039 // uses semantics of argument handles by value, but it should be passed by
15040 // reference. C lang does not support references, so pass all parameters as
15041 // pointers.
15042 // Create 'T omp_in;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000015043 VarDecl *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000015044 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015045 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
15046 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
15047 // uses semantics of argument handles by value, but it should be passed by
15048 // reference. C lang does not support references, so pass all parameters as
15049 // pointers.
15050 // Create 'T omp_out;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000015051 VarDecl *OmpOutParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015052 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
15053 if (S != nullptr) {
15054 PushOnScopeChains(OmpInParm, S);
15055 PushOnScopeChains(OmpOutParm, S);
15056 } else {
15057 DRD->addDecl(OmpInParm);
15058 DRD->addDecl(OmpOutParm);
15059 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000015060 Expr *InE =
15061 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation());
15062 Expr *OutE =
15063 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation());
15064 DRD->setCombinerData(InE, OutE);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015065}
15066
15067void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
15068 auto *DRD = cast<OMPDeclareReductionDecl>(D);
15069 DiscardCleanupsInEvaluationContext();
15070 PopExpressionEvaluationContext();
15071
15072 PopDeclContext();
15073 PopFunctionScopeInfo();
15074
15075 if (Combiner != nullptr)
15076 DRD->setCombiner(Combiner);
15077 else
15078 DRD->setInvalidDecl();
15079}
15080
Alexey Bataev070f43a2017-09-06 14:49:58 +000015081VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015082 auto *DRD = cast<OMPDeclareReductionDecl>(D);
15083
15084 // Enter new function scope.
15085 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000015086 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015087
15088 if (S != nullptr)
15089 PushDeclContext(S, DRD);
15090 else
15091 CurContext = DRD;
15092
Faisal Valid143a0c2017-04-01 21:30:49 +000015093 PushExpressionEvaluationContext(
15094 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015095
15096 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015097 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
15098 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
15099 // uses semantics of argument handles by value, but it should be passed by
15100 // reference. C lang does not support references, so pass all parameters as
15101 // pointers.
15102 // Create 'T omp_priv;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000015103 VarDecl *OmpPrivParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015104 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000015105 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
15106 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
15107 // uses semantics of argument handles by value, but it should be passed by
15108 // reference. C lang does not support references, so pass all parameters as
15109 // pointers.
15110 // Create 'T omp_orig;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000015111 VarDecl *OmpOrigParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000015112 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015113 if (S != nullptr) {
15114 PushOnScopeChains(OmpPrivParm, S);
15115 PushOnScopeChains(OmpOrigParm, S);
15116 } else {
15117 DRD->addDecl(OmpPrivParm);
15118 DRD->addDecl(OmpOrigParm);
15119 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000015120 Expr *OrigE =
15121 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation());
15122 Expr *PrivE =
15123 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation());
15124 DRD->setInitializerData(OrigE, PrivE);
Alexey Bataev070f43a2017-09-06 14:49:58 +000015125 return OmpPrivParm;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015126}
15127
Alexey Bataev070f43a2017-09-06 14:49:58 +000015128void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
15129 VarDecl *OmpPrivParm) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015130 auto *DRD = cast<OMPDeclareReductionDecl>(D);
15131 DiscardCleanupsInEvaluationContext();
15132 PopExpressionEvaluationContext();
15133
15134 PopDeclContext();
15135 PopFunctionScopeInfo();
15136
Alexey Bataev070f43a2017-09-06 14:49:58 +000015137 if (Initializer != nullptr) {
15138 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
15139 } else if (OmpPrivParm->hasInit()) {
15140 DRD->setInitializer(OmpPrivParm->getInit(),
15141 OmpPrivParm->isDirectInit()
15142 ? OMPDeclareReductionDecl::DirectInit
15143 : OMPDeclareReductionDecl::CopyInit);
15144 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015145 DRD->setInvalidDecl();
Alexey Bataev070f43a2017-09-06 14:49:58 +000015146 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015147}
15148
15149Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
15150 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000015151 for (Decl *D : DeclReductions.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015152 if (IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000015153 if (S)
15154 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S,
15155 /*AddToContext=*/false);
15156 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015157 D->setInvalidDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000015158 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000015159 }
15160 return DeclReductions;
15161}
15162
Michael Kruse251e1482019-02-01 20:25:04 +000015163TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) {
15164 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
15165 QualType T = TInfo->getType();
15166 if (D.isInvalidType())
15167 return true;
15168
15169 if (getLangOpts().CPlusPlus) {
15170 // Check that there are no default arguments (C++ only).
15171 CheckExtraCXXDefaultArguments(D);
15172 }
15173
15174 return CreateParsedType(T, TInfo);
15175}
15176
15177QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc,
15178 TypeResult ParsedType) {
15179 assert(ParsedType.isUsable() && "Expect usable parsed mapper type");
15180
15181 QualType MapperType = GetTypeFromParser(ParsedType.get());
15182 assert(!MapperType.isNull() && "Expect valid mapper type");
15183
15184 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
15185 // The type must be of struct, union or class type in C and C++
15186 if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) {
15187 Diag(TyLoc, diag::err_omp_mapper_wrong_type);
15188 return QualType();
15189 }
15190 return MapperType;
15191}
15192
15193OMPDeclareMapperDecl *Sema::ActOnOpenMPDeclareMapperDirectiveStart(
15194 Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType,
15195 SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS,
15196 Decl *PrevDeclInScope) {
15197 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName,
15198 forRedeclarationInCurContext());
15199 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
15200 // A mapper-identifier may not be redeclared in the current scope for the
15201 // same type or for a type that is compatible according to the base language
15202 // rules.
15203 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
15204 OMPDeclareMapperDecl *PrevDMD = nullptr;
15205 bool InCompoundScope = true;
15206 if (S != nullptr) {
15207 // Find previous declaration with the same name not referenced in other
15208 // declarations.
15209 FunctionScopeInfo *ParentFn = getEnclosingFunction();
15210 InCompoundScope =
15211 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
15212 LookupName(Lookup, S);
15213 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
15214 /*AllowInlineNamespace=*/false);
15215 llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious;
15216 LookupResult::Filter Filter = Lookup.makeFilter();
15217 while (Filter.hasNext()) {
15218 auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next());
15219 if (InCompoundScope) {
15220 auto I = UsedAsPrevious.find(PrevDecl);
15221 if (I == UsedAsPrevious.end())
15222 UsedAsPrevious[PrevDecl] = false;
15223 if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope())
15224 UsedAsPrevious[D] = true;
15225 }
15226 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
15227 PrevDecl->getLocation();
15228 }
15229 Filter.done();
15230 if (InCompoundScope) {
15231 for (const auto &PrevData : UsedAsPrevious) {
15232 if (!PrevData.second) {
15233 PrevDMD = PrevData.first;
15234 break;
15235 }
15236 }
15237 }
15238 } else if (PrevDeclInScope) {
15239 auto *PrevDMDInScope = PrevDMD =
15240 cast<OMPDeclareMapperDecl>(PrevDeclInScope);
15241 do {
15242 PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] =
15243 PrevDMDInScope->getLocation();
15244 PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope();
15245 } while (PrevDMDInScope != nullptr);
15246 }
15247 const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType());
15248 bool Invalid = false;
15249 if (I != PreviousRedeclTypes.end()) {
15250 Diag(StartLoc, diag::err_omp_declare_mapper_redefinition)
15251 << MapperType << Name;
15252 Diag(I->second, diag::note_previous_definition);
15253 Invalid = true;
15254 }
15255 auto *DMD = OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name,
15256 MapperType, VN, PrevDMD);
15257 DC->addDecl(DMD);
15258 DMD->setAccess(AS);
15259 if (Invalid)
15260 DMD->setInvalidDecl();
15261
15262 // Enter new function scope.
15263 PushFunctionScope();
15264 setFunctionHasBranchProtectedScope();
15265
15266 CurContext = DMD;
15267
15268 return DMD;
15269}
15270
15271void Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(OMPDeclareMapperDecl *DMD,
15272 Scope *S,
15273 QualType MapperType,
15274 SourceLocation StartLoc,
15275 DeclarationName VN) {
15276 VarDecl *VD = buildVarDecl(*this, StartLoc, MapperType, VN.getAsString());
15277 if (S)
15278 PushOnScopeChains(VD, S);
15279 else
15280 DMD->addDecl(VD);
15281 Expr *MapperVarRefExpr = buildDeclRefExpr(*this, VD, MapperType, StartLoc);
15282 DMD->setMapperVarRef(MapperVarRefExpr);
15283}
15284
15285Sema::DeclGroupPtrTy
15286Sema::ActOnOpenMPDeclareMapperDirectiveEnd(OMPDeclareMapperDecl *D, Scope *S,
15287 ArrayRef<OMPClause *> ClauseList) {
15288 PopDeclContext();
15289 PopFunctionScopeInfo();
15290
15291 if (D) {
15292 if (S)
15293 PushOnScopeChains(D, S, /*AddToContext=*/false);
15294 D->CreateClauses(Context, ClauseList);
15295 }
15296
15297 return DeclGroupPtrTy::make(DeclGroupRef(D));
15298}
15299
David Majnemer9d168222016-08-05 17:44:54 +000015300OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
Kelvin Li099bb8c2015-11-24 20:50:12 +000015301 SourceLocation StartLoc,
15302 SourceLocation LParenLoc,
15303 SourceLocation EndLoc) {
15304 Expr *ValExpr = NumTeams;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000015305 Stmt *HelperValStmt = nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000015306
Kelvin Lia15fb1a2015-11-27 18:47:36 +000015307 // OpenMP [teams Constrcut, Restrictions]
15308 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000015309 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
Alexey Bataeva0569352015-12-01 10:17:31 +000015310 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000015311 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000015312
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000015313 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000015314 OpenMPDirectiveKind CaptureRegion =
15315 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
15316 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000015317 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000015318 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000015319 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
15320 HelperValStmt = buildPreInits(Context, Captures);
15321 }
15322
15323 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
15324 StartLoc, LParenLoc, EndLoc);
Kelvin Li099bb8c2015-11-24 20:50:12 +000015325}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000015326
15327OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
15328 SourceLocation StartLoc,
15329 SourceLocation LParenLoc,
15330 SourceLocation EndLoc) {
15331 Expr *ValExpr = ThreadLimit;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000015332 Stmt *HelperValStmt = nullptr;
Kelvin Lia15fb1a2015-11-27 18:47:36 +000015333
15334 // OpenMP [teams Constrcut, Restrictions]
15335 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000015336 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
Alexey Bataeva0569352015-12-01 10:17:31 +000015337 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000015338 return nullptr;
15339
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000015340 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000015341 OpenMPDirectiveKind CaptureRegion =
15342 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
15343 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000015344 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000015345 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000015346 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
15347 HelperValStmt = buildPreInits(Context, Captures);
15348 }
15349
15350 return new (Context) OMPThreadLimitClause(
15351 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Kelvin Lia15fb1a2015-11-27 18:47:36 +000015352}
Alexey Bataeva0569352015-12-01 10:17:31 +000015353
15354OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
15355 SourceLocation StartLoc,
15356 SourceLocation LParenLoc,
15357 SourceLocation EndLoc) {
15358 Expr *ValExpr = Priority;
15359
15360 // OpenMP [2.9.1, task Constrcut]
15361 // The priority-value is a non-negative numerical scalar expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000015362 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
Alexey Bataeva0569352015-12-01 10:17:31 +000015363 /*StrictlyPositive=*/false))
15364 return nullptr;
15365
15366 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
15367}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000015368
15369OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
15370 SourceLocation StartLoc,
15371 SourceLocation LParenLoc,
15372 SourceLocation EndLoc) {
15373 Expr *ValExpr = Grainsize;
15374
15375 // OpenMP [2.9.2, taskloop Constrcut]
15376 // The parameter of the grainsize clause must be a positive integer
15377 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000015378 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000015379 /*StrictlyPositive=*/true))
15380 return nullptr;
15381
15382 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
15383}
Alexey Bataev382967a2015-12-08 12:06:20 +000015384
15385OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
15386 SourceLocation StartLoc,
15387 SourceLocation LParenLoc,
15388 SourceLocation EndLoc) {
15389 Expr *ValExpr = NumTasks;
15390
15391 // OpenMP [2.9.2, taskloop Constrcut]
15392 // The parameter of the num_tasks clause must be a positive integer
15393 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000015394 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
Alexey Bataev382967a2015-12-08 12:06:20 +000015395 /*StrictlyPositive=*/true))
15396 return nullptr;
15397
15398 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
15399}
15400
Alexey Bataev28c75412015-12-15 08:19:24 +000015401OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
15402 SourceLocation LParenLoc,
15403 SourceLocation EndLoc) {
15404 // OpenMP [2.13.2, critical construct, Description]
15405 // ... where hint-expression is an integer constant expression that evaluates
15406 // to a valid lock hint.
15407 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
15408 if (HintExpr.isInvalid())
15409 return nullptr;
15410 return new (Context)
15411 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
15412}
15413
Carlo Bertollib4adf552016-01-15 18:50:31 +000015414OMPClause *Sema::ActOnOpenMPDistScheduleClause(
15415 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
15416 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
15417 SourceLocation EndLoc) {
15418 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
15419 std::string Values;
15420 Values += "'";
15421 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
15422 Values += "'";
15423 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
15424 << Values << getOpenMPClauseName(OMPC_dist_schedule);
15425 return nullptr;
15426 }
15427 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000015428 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000015429 if (ChunkSize) {
15430 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
15431 !ChunkSize->isInstantiationDependent() &&
15432 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000015433 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Carlo Bertollib4adf552016-01-15 18:50:31 +000015434 ExprResult Val =
15435 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
15436 if (Val.isInvalid())
15437 return nullptr;
15438
15439 ValExpr = Val.get();
15440
15441 // OpenMP [2.7.1, Restrictions]
15442 // chunk_size must be a loop invariant integer expression with a positive
15443 // value.
15444 llvm::APSInt Result;
15445 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
15446 if (Result.isSigned() && !Result.isStrictlyPositive()) {
15447 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
15448 << "dist_schedule" << ChunkSize->getSourceRange();
15449 return nullptr;
15450 }
Alexey Bataev2ba67042017-11-28 21:11:44 +000015451 } else if (getOpenMPCaptureRegionForClause(
15452 DSAStack->getCurrentDirective(), OMPC_dist_schedule) !=
15453 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +000015454 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000015455 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000015456 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +000015457 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
15458 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000015459 }
15460 }
15461 }
15462
15463 return new (Context)
15464 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000015465 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000015466}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000015467
15468OMPClause *Sema::ActOnOpenMPDefaultmapClause(
15469 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
15470 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
15471 SourceLocation KindLoc, SourceLocation EndLoc) {
15472 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
David Majnemer9d168222016-08-05 17:44:54 +000015473 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000015474 std::string Value;
15475 SourceLocation Loc;
15476 Value += "'";
15477 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
15478 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000015479 OMPC_DEFAULTMAP_MODIFIER_tofrom);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000015480 Loc = MLoc;
15481 } else {
15482 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000015483 OMPC_DEFAULTMAP_scalar);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000015484 Loc = KindLoc;
15485 }
15486 Value += "'";
15487 Diag(Loc, diag::err_omp_unexpected_clause_value)
15488 << Value << getOpenMPClauseName(OMPC_defaultmap);
15489 return nullptr;
15490 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +000015491 DSAStack->setDefaultDMAToFromScalar(StartLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000015492
15493 return new (Context)
15494 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
15495}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000015496
15497bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
15498 DeclContext *CurLexicalContext = getCurLexicalContext();
15499 if (!CurLexicalContext->isFileContext() &&
15500 !CurLexicalContext->isExternCContext() &&
Alexey Bataev502ec492017-10-03 20:00:00 +000015501 !CurLexicalContext->isExternCXXContext() &&
15502 !isa<CXXRecordDecl>(CurLexicalContext) &&
15503 !isa<ClassTemplateDecl>(CurLexicalContext) &&
15504 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
15505 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000015506 Diag(Loc, diag::err_omp_region_not_file_context);
15507 return false;
15508 }
Kelvin Libc38e632018-09-10 02:07:09 +000015509 ++DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000015510 return true;
15511}
15512
15513void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
Kelvin Libc38e632018-09-10 02:07:09 +000015514 assert(DeclareTargetNestingLevel > 0 &&
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000015515 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
Kelvin Libc38e632018-09-10 02:07:09 +000015516 --DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000015517}
15518
Alexey Bataev729e2422019-08-23 16:11:14 +000015519NamedDecl *
15520Sema::lookupOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec,
15521 const DeclarationNameInfo &Id,
15522 NamedDeclSetType &SameDirectiveDecls) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000015523 LookupResult Lookup(*this, Id, LookupOrdinaryName);
15524 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
15525
15526 if (Lookup.isAmbiguous())
Alexey Bataev729e2422019-08-23 16:11:14 +000015527 return nullptr;
Dmitry Polukhind69b5052016-05-09 14:59:13 +000015528 Lookup.suppressDiagnostics();
15529
15530 if (!Lookup.isSingleResult()) {
Bruno Ricci70ad3962019-03-25 17:08:51 +000015531 VarOrFuncDeclFilterCCC CCC(*this);
Dmitry Polukhind69b5052016-05-09 14:59:13 +000015532 if (TypoCorrection Corrected =
Bruno Ricci70ad3962019-03-25 17:08:51 +000015533 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC,
Dmitry Polukhind69b5052016-05-09 14:59:13 +000015534 CTK_ErrorRecovery)) {
15535 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
15536 << Id.getName());
15537 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
Alexey Bataev729e2422019-08-23 16:11:14 +000015538 return nullptr;
Dmitry Polukhind69b5052016-05-09 14:59:13 +000015539 }
15540
15541 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
Alexey Bataev729e2422019-08-23 16:11:14 +000015542 return nullptr;
Dmitry Polukhind69b5052016-05-09 14:59:13 +000015543 }
15544
15545 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
Alexey Bataev729e2422019-08-23 16:11:14 +000015546 if (!isa<VarDecl>(ND) && !isa<FunctionDecl>(ND) &&
15547 !isa<FunctionTemplateDecl>(ND)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000015548 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
Alexey Bataev729e2422019-08-23 16:11:14 +000015549 return nullptr;
15550 }
15551 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
15552 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
15553 return ND;
15554}
15555
15556void Sema::ActOnOpenMPDeclareTargetName(
15557 NamedDecl *ND, SourceLocation Loc, OMPDeclareTargetDeclAttr::MapTypeTy MT,
15558 OMPDeclareTargetDeclAttr::DevTypeTy DT) {
15559 assert((isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
15560 isa<FunctionTemplateDecl>(ND)) &&
15561 "Expected variable, function or function template.");
15562
15563 // Diagnose marking after use as it may lead to incorrect diagnosis and
15564 // codegen.
15565 if (LangOpts.OpenMP >= 50 &&
15566 (ND->isUsed(/*CheckUsedAttr=*/false) || ND->isReferenced()))
15567 Diag(Loc, diag::warn_omp_declare_target_after_first_use);
15568
15569 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
15570 OMPDeclareTargetDeclAttr::getDeviceType(cast<ValueDecl>(ND));
15571 if (DevTy.hasValue() && *DevTy != DT) {
15572 Diag(Loc, diag::err_omp_device_type_mismatch)
15573 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(DT)
15574 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(*DevTy);
15575 return;
15576 }
15577 Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
15578 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(cast<ValueDecl>(ND));
15579 if (!Res) {
15580 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT, DT,
15581 SourceRange(Loc, Loc));
15582 ND->addAttr(A);
15583 if (ASTMutationListener *ML = Context.getASTMutationListener())
15584 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
15585 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Loc);
15586 } else if (*Res != MT) {
15587 Diag(Loc, diag::err_omp_declare_target_to_and_link) << ND;
Alexey Bataeve3727102018-04-18 15:57:46 +000015588 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +000015589}
15590
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000015591static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
15592 Sema &SemaRef, Decl *D) {
Alexey Bataev30a78212018-09-11 13:59:10 +000015593 if (!D || !isa<VarDecl>(D))
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000015594 return;
Alexey Bataev30a78212018-09-11 13:59:10 +000015595 auto *VD = cast<VarDecl>(D);
Alexey Bataev217ff1e2019-08-16 20:15:02 +000015596 Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy =
15597 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
15598 if (SemaRef.LangOpts.OpenMP >= 50 &&
15599 (SemaRef.getCurLambda(/*IgnoreNonLambdaCapturingScope=*/true) ||
15600 SemaRef.getCurBlock() || SemaRef.getCurCapturedRegion()) &&
15601 VD->hasGlobalStorage()) {
15602 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy =
15603 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
15604 if (!MapTy || *MapTy != OMPDeclareTargetDeclAttr::MT_To) {
15605 // OpenMP 5.0, 2.12.7 declare target Directive, Restrictions
15606 // If a lambda declaration and definition appears between a
15607 // declare target directive and the matching end declare target
15608 // directive, all variables that are captured by the lambda
15609 // expression must also appear in a to clause.
15610 SemaRef.Diag(VD->getLocation(),
Alexey Bataevc4299552019-08-20 17:50:13 +000015611 diag::err_omp_lambda_capture_in_declare_target_not_to);
Alexey Bataev217ff1e2019-08-16 20:15:02 +000015612 SemaRef.Diag(SL, diag::note_var_explicitly_captured_here)
15613 << VD << 0 << SR;
15614 return;
15615 }
15616 }
15617 if (MapTy.hasValue())
Alexey Bataev30a78212018-09-11 13:59:10 +000015618 return;
15619 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context);
15620 SemaRef.Diag(SL, diag::note_used_here) << SR;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000015621}
15622
15623static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
15624 Sema &SemaRef, DSAStackTy *Stack,
15625 ValueDecl *VD) {
Alexey Bataevebcfc9e2019-08-22 16:48:26 +000015626 return OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) ||
Alexey Bataeve3727102018-04-18 15:57:46 +000015627 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
15628 /*FullCheck=*/false);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000015629}
15630
Kelvin Li1ce87c72017-12-12 20:08:12 +000015631void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
15632 SourceLocation IdLoc) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000015633 if (!D || D->isInvalidDecl())
15634 return;
15635 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
Stephen Kellyf2ceec42018-08-09 21:08:08 +000015636 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
Alexey Bataeve3727102018-04-18 15:57:46 +000015637 if (auto *VD = dyn_cast<VarDecl>(D)) {
Alexey Bataevc1943e72018-07-09 19:58:08 +000015638 // Only global variables can be marked as declare target.
Alexey Bataev30a78212018-09-11 13:59:10 +000015639 if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
15640 !VD->isStaticDataMember())
Alexey Bataevc1943e72018-07-09 19:58:08 +000015641 return;
15642 // 2.10.6: threadprivate variable cannot appear in a declare target
15643 // directive.
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000015644 if (DSAStack->isThreadPrivate(VD)) {
15645 Diag(SL, diag::err_omp_threadprivate_in_target);
Alexey Bataeve3727102018-04-18 15:57:46 +000015646 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000015647 return;
15648 }
15649 }
Alexey Bataev97b72212018-08-14 18:31:20 +000015650 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
15651 D = FTD->getTemplatedDecl();
Alexey Bataev9fd495b2019-08-20 19:50:13 +000015652 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
Alexey Bataev30a78212018-09-11 13:59:10 +000015653 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
15654 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD);
Alexey Bataev9fd495b2019-08-20 19:50:13 +000015655 if (IdLoc.isValid() && Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
Kelvin Li1ce87c72017-12-12 20:08:12 +000015656 Diag(IdLoc, diag::err_omp_function_in_link_clause);
15657 Diag(FD->getLocation(), diag::note_defined_here) << FD;
15658 return;
15659 }
Alexey Bataev9fd495b2019-08-20 19:50:13 +000015660 // Mark the function as must be emitted for the device.
Alexey Bataev729e2422019-08-23 16:11:14 +000015661 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
15662 OMPDeclareTargetDeclAttr::getDeviceType(FD);
15663 if (LangOpts.OpenMPIsDevice && Res.hasValue() && IdLoc.isValid() &&
15664 *DevTy != OMPDeclareTargetDeclAttr::DT_Host)
Alexey Bataev9fd495b2019-08-20 19:50:13 +000015665 checkOpenMPDeviceFunction(IdLoc, FD, /*CheckForDelayedContext=*/false);
Alexey Bataev729e2422019-08-23 16:11:14 +000015666 if (!LangOpts.OpenMPIsDevice && Res.hasValue() && IdLoc.isValid() &&
15667 *DevTy != OMPDeclareTargetDeclAttr::DT_NoHost)
15668 checkOpenMPHostFunction(IdLoc, FD, /*CheckCaller=*/false);
Kelvin Li1ce87c72017-12-12 20:08:12 +000015669 }
Alexey Bataev30a78212018-09-11 13:59:10 +000015670 if (auto *VD = dyn_cast<ValueDecl>(D)) {
15671 // Problem if any with var declared with incomplete type will be reported
15672 // as normal, so no need to check it here.
15673 if ((E || !VD->getType()->isIncompleteType()) &&
15674 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD))
15675 return;
15676 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
15677 // Checking declaration inside declare target region.
15678 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
15679 isa<FunctionTemplateDecl>(D)) {
15680 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
Alexey Bataev729e2422019-08-23 16:11:14 +000015681 Context, OMPDeclareTargetDeclAttr::MT_To,
15682 OMPDeclareTargetDeclAttr::DT_Any, SourceRange(IdLoc, IdLoc));
Alexey Bataev30a78212018-09-11 13:59:10 +000015683 D->addAttr(A);
15684 if (ASTMutationListener *ML = Context.getASTMutationListener())
15685 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
15686 }
15687 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000015688 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000015689 }
Alexey Bataev30a78212018-09-11 13:59:10 +000015690 if (!E)
15691 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000015692 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
15693}
Samuel Antao661c0902016-05-26 17:39:58 +000015694
15695OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
Michael Kruse01f670d2019-02-22 22:29:42 +000015696 CXXScopeSpec &MapperIdScopeSpec,
15697 DeclarationNameInfo &MapperId,
15698 const OMPVarListLocTy &Locs,
15699 ArrayRef<Expr *> UnresolvedMappers) {
Samuel Antao661c0902016-05-26 17:39:58 +000015700 MappableVarListInfo MVLI(VarList);
Michael Kruse01f670d2019-02-22 22:29:42 +000015701 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc,
15702 MapperIdScopeSpec, MapperId, UnresolvedMappers);
Samuel Antao661c0902016-05-26 17:39:58 +000015703 if (MVLI.ProcessedVarList.empty())
15704 return nullptr;
15705
Michael Kruse01f670d2019-02-22 22:29:42 +000015706 return OMPToClause::Create(
15707 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
15708 MVLI.VarComponents, MVLI.UDMapperList,
15709 MapperIdScopeSpec.getWithLocInContext(Context), MapperId);
Samuel Antao661c0902016-05-26 17:39:58 +000015710}
Samuel Antaoec172c62016-05-26 17:49:04 +000015711
15712OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
Michael Kruse0336c752019-02-25 20:34:15 +000015713 CXXScopeSpec &MapperIdScopeSpec,
15714 DeclarationNameInfo &MapperId,
15715 const OMPVarListLocTy &Locs,
15716 ArrayRef<Expr *> UnresolvedMappers) {
Samuel Antaoec172c62016-05-26 17:49:04 +000015717 MappableVarListInfo MVLI(VarList);
Michael Kruse01f670d2019-02-22 22:29:42 +000015718 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc,
15719 MapperIdScopeSpec, MapperId, UnresolvedMappers);
Samuel Antaoec172c62016-05-26 17:49:04 +000015720 if (MVLI.ProcessedVarList.empty())
15721 return nullptr;
15722
Michael Kruse0336c752019-02-25 20:34:15 +000015723 return OMPFromClause::Create(
15724 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
15725 MVLI.VarComponents, MVLI.UDMapperList,
15726 MapperIdScopeSpec.getWithLocInContext(Context), MapperId);
Samuel Antaoec172c62016-05-26 17:49:04 +000015727}
Carlo Bertolli2404b172016-07-13 15:37:16 +000015728
15729OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
Michael Kruse4304e9d2019-02-19 16:38:20 +000015730 const OMPVarListLocTy &Locs) {
Samuel Antaocc10b852016-07-28 14:23:26 +000015731 MappableVarListInfo MVLI(VarList);
15732 SmallVector<Expr *, 8> PrivateCopies;
15733 SmallVector<Expr *, 8> Inits;
15734
Alexey Bataeve3727102018-04-18 15:57:46 +000015735 for (Expr *RefExpr : VarList) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000015736 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
15737 SourceLocation ELoc;
15738 SourceRange ERange;
15739 Expr *SimpleRefExpr = RefExpr;
15740 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
15741 if (Res.second) {
15742 // It will be analyzed later.
Samuel Antaocc10b852016-07-28 14:23:26 +000015743 MVLI.ProcessedVarList.push_back(RefExpr);
15744 PrivateCopies.push_back(nullptr);
15745 Inits.push_back(nullptr);
Carlo Bertolli2404b172016-07-13 15:37:16 +000015746 }
15747 ValueDecl *D = Res.first;
15748 if (!D)
15749 continue;
15750
15751 QualType Type = D->getType();
Samuel Antaocc10b852016-07-28 14:23:26 +000015752 Type = Type.getNonReferenceType().getUnqualifiedType();
15753
15754 auto *VD = dyn_cast<VarDecl>(D);
15755
15756 // Item should be a pointer or reference to pointer.
15757 if (!Type->isPointerType()) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000015758 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
15759 << 0 << RefExpr->getSourceRange();
15760 continue;
15761 }
Samuel Antaocc10b852016-07-28 14:23:26 +000015762
15763 // Build the private variable and the expression that refers to it.
Alexey Bataev63cc8e92018-03-20 14:45:59 +000015764 auto VDPrivate =
15765 buildVarDecl(*this, ELoc, Type, D->getName(),
15766 D->hasAttrs() ? &D->getAttrs() : nullptr,
15767 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Samuel Antaocc10b852016-07-28 14:23:26 +000015768 if (VDPrivate->isInvalidDecl())
15769 continue;
15770
15771 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000015772 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Samuel Antaocc10b852016-07-28 14:23:26 +000015773 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
15774
15775 // Add temporary variable to initialize the private copy of the pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +000015776 VarDecl *VDInit =
Samuel Antaocc10b852016-07-28 14:23:26 +000015777 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
Alexey Bataeve3727102018-04-18 15:57:46 +000015778 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
15779 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc());
Samuel Antaocc10b852016-07-28 14:23:26 +000015780 AddInitializerToDecl(VDPrivate,
15781 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000015782 /*DirectInit=*/false);
Samuel Antaocc10b852016-07-28 14:23:26 +000015783
15784 // If required, build a capture to implement the privatization initialized
15785 // with the current list item value.
15786 DeclRefExpr *Ref = nullptr;
15787 if (!VD)
15788 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
15789 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
15790 PrivateCopies.push_back(VDPrivateRefExpr);
15791 Inits.push_back(VDInitRefExpr);
15792
15793 // We need to add a data sharing attribute for this variable to make sure it
15794 // is correctly captured. A variable that shows up in a use_device_ptr has
15795 // similar properties of a first private variable.
15796 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
15797
15798 // Create a mappable component for the list item. List items in this clause
15799 // only need a component.
15800 MVLI.VarBaseDeclarations.push_back(D);
15801 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
15802 MVLI.VarComponents.back().push_back(
15803 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
Carlo Bertolli2404b172016-07-13 15:37:16 +000015804 }
15805
Samuel Antaocc10b852016-07-28 14:23:26 +000015806 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli2404b172016-07-13 15:37:16 +000015807 return nullptr;
15808
Samuel Antaocc10b852016-07-28 14:23:26 +000015809 return OMPUseDevicePtrClause::Create(
Michael Kruse4304e9d2019-02-19 16:38:20 +000015810 Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits,
15811 MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +000015812}
Carlo Bertolli70594e92016-07-13 17:16:49 +000015813
15814OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
Michael Kruse4304e9d2019-02-19 16:38:20 +000015815 const OMPVarListLocTy &Locs) {
Samuel Antao6890b092016-07-28 14:25:09 +000015816 MappableVarListInfo MVLI(VarList);
Alexey Bataeve3727102018-04-18 15:57:46 +000015817 for (Expr *RefExpr : VarList) {
Kelvin Li84376252016-12-14 15:39:58 +000015818 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
Carlo Bertolli70594e92016-07-13 17:16:49 +000015819 SourceLocation ELoc;
15820 SourceRange ERange;
15821 Expr *SimpleRefExpr = RefExpr;
15822 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
15823 if (Res.second) {
15824 // It will be analyzed later.
Samuel Antao6890b092016-07-28 14:25:09 +000015825 MVLI.ProcessedVarList.push_back(RefExpr);
Carlo Bertolli70594e92016-07-13 17:16:49 +000015826 }
15827 ValueDecl *D = Res.first;
15828 if (!D)
15829 continue;
15830
15831 QualType Type = D->getType();
15832 // item should be a pointer or array or reference to pointer or array
15833 if (!Type.getNonReferenceType()->isPointerType() &&
15834 !Type.getNonReferenceType()->isArrayType()) {
15835 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
15836 << 0 << RefExpr->getSourceRange();
15837 continue;
15838 }
Samuel Antao6890b092016-07-28 14:25:09 +000015839
15840 // Check if the declaration in the clause does not show up in any data
15841 // sharing attribute.
Alexey Bataeve3727102018-04-18 15:57:46 +000015842 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Samuel Antao6890b092016-07-28 14:25:09 +000015843 if (isOpenMPPrivate(DVar.CKind)) {
15844 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
15845 << getOpenMPClauseName(DVar.CKind)
15846 << getOpenMPClauseName(OMPC_is_device_ptr)
15847 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000015848 reportOriginalDsa(*this, DSAStack, D, DVar);
Samuel Antao6890b092016-07-28 14:25:09 +000015849 continue;
15850 }
15851
Alexey Bataeve3727102018-04-18 15:57:46 +000015852 const Expr *ConflictExpr;
Samuel Antao6890b092016-07-28 14:25:09 +000015853 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000015854 D, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +000015855 [&ConflictExpr](
15856 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
15857 OpenMPClauseKind) -> bool {
15858 ConflictExpr = R.front().getAssociatedExpression();
15859 return true;
15860 })) {
15861 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
15862 Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
15863 << ConflictExpr->getSourceRange();
15864 continue;
15865 }
15866
15867 // Store the components in the stack so that they can be used to check
15868 // against other clauses later on.
15869 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
15870 DSAStack->addMappableExpressionComponents(
15871 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
15872
15873 // Record the expression we've just processed.
15874 MVLI.ProcessedVarList.push_back(SimpleRefExpr);
15875
15876 // Create a mappable component for the list item. List items in this clause
15877 // only need a component. We use a null declaration to signal fields in
15878 // 'this'.
15879 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
15880 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
15881 "Unexpected device pointer expression!");
15882 MVLI.VarBaseDeclarations.push_back(
15883 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
15884 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
15885 MVLI.VarComponents.back().push_back(MC);
Carlo Bertolli70594e92016-07-13 17:16:49 +000015886 }
15887
Samuel Antao6890b092016-07-28 14:25:09 +000015888 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli70594e92016-07-13 17:16:49 +000015889 return nullptr;
15890
Michael Kruse4304e9d2019-02-19 16:38:20 +000015891 return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList,
15892 MVLI.VarBaseDeclarations,
15893 MVLI.VarComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +000015894}
Alexey Bataeve04483e2019-03-27 14:14:31 +000015895
15896OMPClause *Sema::ActOnOpenMPAllocateClause(
15897 Expr *Allocator, ArrayRef<Expr *> VarList, SourceLocation StartLoc,
15898 SourceLocation ColonLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
15899 if (Allocator) {
15900 // OpenMP [2.11.4 allocate Clause, Description]
15901 // allocator is an expression of omp_allocator_handle_t type.
15902 if (!findOMPAllocatorHandleT(*this, Allocator->getExprLoc(), DSAStack))
15903 return nullptr;
15904
15905 ExprResult AllocatorRes = DefaultLvalueConversion(Allocator);
15906 if (AllocatorRes.isInvalid())
15907 return nullptr;
15908 AllocatorRes = PerformImplicitConversion(AllocatorRes.get(),
15909 DSAStack->getOMPAllocatorHandleT(),
15910 Sema::AA_Initializing,
15911 /*AllowExplicit=*/true);
15912 if (AllocatorRes.isInvalid())
15913 return nullptr;
15914 Allocator = AllocatorRes.get();
Alexey Bataev84c8bae2019-04-01 16:56:59 +000015915 } else {
15916 // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions.
15917 // allocate clauses that appear on a target construct or on constructs in a
15918 // target region must specify an allocator expression unless a requires
15919 // directive with the dynamic_allocators clause is present in the same
15920 // compilation unit.
15921 if (LangOpts.OpenMPIsDevice &&
15922 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
15923 targetDiag(StartLoc, diag::err_expected_allocator_expression);
Alexey Bataeve04483e2019-03-27 14:14:31 +000015924 }
15925 // Analyze and build list of variables.
15926 SmallVector<Expr *, 8> Vars;
15927 for (Expr *RefExpr : VarList) {
15928 assert(RefExpr && "NULL expr in OpenMP private clause.");
15929 SourceLocation ELoc;
15930 SourceRange ERange;
15931 Expr *SimpleRefExpr = RefExpr;
15932 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
15933 if (Res.second) {
15934 // It will be analyzed later.
15935 Vars.push_back(RefExpr);
15936 }
15937 ValueDecl *D = Res.first;
15938 if (!D)
15939 continue;
15940
15941 auto *VD = dyn_cast<VarDecl>(D);
15942 DeclRefExpr *Ref = nullptr;
15943 if (!VD && !CurContext->isDependentContext())
15944 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
15945 Vars.push_back((VD || CurContext->isDependentContext())
15946 ? RefExpr->IgnoreParens()
15947 : Ref);
15948 }
15949
15950 if (Vars.empty())
15951 return nullptr;
15952
15953 return OMPAllocateClause::Create(Context, StartLoc, LParenLoc, Allocator,
15954 ColonLoc, EndLoc, Vars);
15955}