clang  9.0.0svn
SemaOpenMP.cpp
Go to the documentation of this file.
1 //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 /// This file implements semantic analysis for OpenMP directives and
10 /// clauses.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #include "TreeTransform.h"
15 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/DeclCXX.h"
20 #include "clang/AST/DeclOpenMP.h"
21 #include "clang/AST/StmtCXX.h"
22 #include "clang/AST/StmtOpenMP.h"
23 #include "clang/AST/StmtVisitor.h"
24 #include "clang/AST/TypeOrdering.h"
27 #include "clang/Sema/Lookup.h"
28 #include "clang/Sema/Scope.h"
29 #include "clang/Sema/ScopeInfo.h"
31 #include "llvm/ADT/PointerEmbeddedInt.h"
32 using namespace clang;
33 
34 //===----------------------------------------------------------------------===//
35 // Stack of data-sharing attributes for variables
36 //===----------------------------------------------------------------------===//
37 
39  Sema &SemaRef, Expr *E,
41  OpenMPClauseKind CKind, bool NoDiagnose);
42 
43 namespace {
44 /// Default data sharing attributes, which can be applied to directive.
46  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'.
49 };
50 
51 /// Attributes of the defaultmap clause.
53  DMA_unspecified, /// Default mapping is not specified.
54  DMA_tofrom_scalar, /// Default mapping is 'tofrom:scalar'.
55 };
56 
57 /// Stack for tracking declarations used in OpenMP directives and
58 /// clauses and their data-sharing attributes.
59 class DSAStackTy {
60 public:
61  struct DSAVarData {
64  const Expr *RefExpr = nullptr;
65  DeclRefExpr *PrivateCopy = nullptr;
66  SourceLocation ImplicitDSALoc;
67  DSAVarData() = default;
68  DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
69  const Expr *RefExpr, DeclRefExpr *PrivateCopy,
70  SourceLocation ImplicitDSALoc)
71  : DKind(DKind), CKind(CKind), RefExpr(RefExpr),
72  PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {}
73  };
74  using OperatorOffsetTy =
76  using DoacrossDependMapTy =
77  llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>;
78 
79 private:
80  struct DSAInfo {
81  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).
84  llvm::PointerIntPair<const Expr *, 1, bool> RefExpr;
85  DeclRefExpr *PrivateCopy = nullptr;
86  };
87  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>;
92  /// Struct that associates a component with the clause kind where they are
93  /// found.
94  struct MappedExprComponentTy {
97  };
98  using MappedExprComponentsTy =
99  llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>;
100  using CriticalsWithHintsTy =
101  llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>;
102  struct ReductionData {
103  using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>;
104  SourceRange ReductionRange;
105  llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp;
106  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  };
116  using DeclReductionMapTy =
117  llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>;
118 
119  struct SharingMapTy {
120  DeclSAMapTy SharingMap;
121  DeclReductionMapTy ReductionMap;
122  AlignedMapTy AlignedMap;
123  MappedExprComponentsTy MappedExprComponents;
124  LoopControlVariablesMapTy LCVMap;
125  DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
126  SourceLocation DefaultAttrLoc;
127  DefaultMapAttributes DefaultMapAttr = DMA_unspecified;
128  SourceLocation DefaultMapAttrLoc;
130  DeclarationNameInfo DirectiveName;
131  Scope *CurScope = nullptr;
132  SourceLocation ConstructLoc;
133  /// 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;
137  /// First argument (Expr *) contains optional argument of the
138  /// 'ordered' clause, the second one is true if the regions has 'ordered'
139  /// clause, false otherwise.
141  unsigned AssociatedLoops = 1;
142  const Decl *PossiblyLoopCounter = nullptr;
143  bool NowaitRegion = false;
144  bool CancelRegion = false;
145  bool LoopStart = false;
146  SourceLocation InnerTeamsRegionLoc;
147  /// Reference to the taskgroup task_reduction reference expression.
148  Expr *TaskgroupReductionRef = nullptr;
149  llvm::DenseSet<QualType> MappedClassesQualTypes;
150  /// List of globals marked as declare target link in this target region
151  /// (isOpenMPTargetExecutionDirective(Directive) == true).
152  llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls;
153  SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
154  Scope *CurScope, SourceLocation Loc)
155  : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
156  ConstructLoc(Loc) {}
157  SharingMapTy() = default;
158  };
159 
160  using StackTy = SmallVector<SharingMapTy, 4>;
161 
162  /// Stack of used declaration and their data-sharing attributes.
163  DeclSAMapTy Threadprivates;
164  const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
166  /// true, if check for DSA must be from parent directive, false, if
167  /// from current directive.
168  OpenMPClauseKind ClauseKindMode = OMPC_unknown;
169  Sema &SemaRef;
170  bool ForceCapturing = false;
171  /// true if all the vaiables in the target executable directives must be
172  /// captured by reference.
173  bool ForceCaptureByReferenceInTargetExecutable = false;
174  CriticalsWithHintsTy Criticals;
175 
176  using iterator = StackTy::const_reverse_iterator;
177 
178  DSAVarData getDSA(iterator &Iter, ValueDecl *D) const;
179 
180  /// Checks if the variable is a local for OpenMP region.
181  bool isOpenMPLocal(VarDecl *D, iterator Iter) const;
182 
183  bool isStackEmpty() const {
184  return Stack.empty() ||
185  Stack.back().second != CurrentNonCapturingFunctionScope ||
186  Stack.back().first.empty();
187  }
188 
189  /// Vector of previously declared requires directives
191  /// omp_allocator_handle_t type.
192  QualType OMPAllocatorHandleT;
193  /// Expression for the predefined allocators.
194  Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = {
195  nullptr};
196  /// Vector of previously encountered target directives
197  SmallVector<SourceLocation, 2> TargetLocations;
198 
199 public:
200  explicit DSAStackTy(Sema &S) : SemaRef(S) {}
201 
202  /// Sets omp_allocator_handle_t type.
203  void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; }
204  /// Gets omp_allocator_handle_t type.
205  QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; }
206  /// Sets the given default allocator.
207  void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
208  Expr *Allocator) {
209  OMPPredefinedAllocators[AllocatorKind] = Allocator;
210  }
211  /// Returns the specified default allocator.
212  Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const {
213  return OMPPredefinedAllocators[AllocatorKind];
214  }
215 
216  bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
217  OpenMPClauseKind getClauseParsingMode() const {
218  assert(isClauseParsingMode() && "Must be in clause parsing mode.");
219  return ClauseKindMode;
220  }
221  void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
222 
223  bool isForceVarCapturing() const { return ForceCapturing; }
224  void setForceVarCapturing(bool V) { ForceCapturing = V; }
225 
226  void setForceCaptureByReferenceInTargetExecutable(bool V) {
227  ForceCaptureByReferenceInTargetExecutable = V;
228  }
229  bool isForceCaptureByReferenceInTargetExecutable() const {
230  return ForceCaptureByReferenceInTargetExecutable;
231  }
232 
233  void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
234  Scope *CurScope, SourceLocation Loc) {
235  if (Stack.empty() ||
236  Stack.back().second != CurrentNonCapturingFunctionScope)
237  Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope);
238  Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc);
239  Stack.back().first.back().DefaultAttrLoc = Loc;
240  }
241 
242  void pop() {
243  assert(!Stack.back().first.empty() &&
244  "Data-sharing attributes stack is empty!");
245  Stack.back().first.pop_back();
246  }
247 
248  /// Marks that we're started loop parsing.
249  void loopInit() {
250  assert(isOpenMPLoopDirective(getCurrentDirective()) &&
251  "Expected loop-based directive.");
252  Stack.back().first.back().LoopStart = true;
253  }
254  /// Start capturing of the variables in the loop context.
255  void loopStart() {
256  assert(isOpenMPLoopDirective(getCurrentDirective()) &&
257  "Expected loop-based directive.");
258  Stack.back().first.back().LoopStart = false;
259  }
260  /// true, if variables are captured, false otherwise.
261  bool isLoopStarted() const {
262  assert(isOpenMPLoopDirective(getCurrentDirective()) &&
263  "Expected loop-based directive.");
264  return !Stack.back().first.back().LoopStart;
265  }
266  /// Marks (or clears) declaration as possibly loop counter.
267  void resetPossibleLoopCounter(const Decl *D = nullptr) {
268  Stack.back().first.back().PossiblyLoopCounter =
269  D ? D->getCanonicalDecl() : D;
270  }
271  /// Gets the possible loop counter decl.
272  const Decl *getPossiblyLoopCunter() const {
273  return Stack.back().first.back().PossiblyLoopCounter;
274  }
275  /// Start new OpenMP region stack in new non-capturing function.
276  void pushFunction() {
277  const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
278  assert(!isa<CapturingScopeInfo>(CurFnScope));
279  CurrentNonCapturingFunctionScope = CurFnScope;
280  }
281  /// Pop region stack for non-capturing function.
282  void popFunction(const FunctionScopeInfo *OldFSI) {
283  if (!Stack.empty() && Stack.back().second == OldFSI) {
284  assert(Stack.back().first.empty());
285  Stack.pop_back();
286  }
287  CurrentNonCapturingFunctionScope = nullptr;
288  for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) {
289  if (!isa<CapturingScopeInfo>(FSI)) {
290  CurrentNonCapturingFunctionScope = FSI;
291  break;
292  }
293  }
294  }
295 
296  void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) {
297  Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint);
298  }
299  const std::pair<const OMPCriticalDirective *, llvm::APSInt>
300  getCriticalWithHint(const DeclarationNameInfo &Name) const {
301  auto I = Criticals.find(Name.getAsString());
302  if (I != Criticals.end())
303  return I->second;
304  return std::make_pair(nullptr, llvm::APSInt());
305  }
306  /// If 'aligned' declaration for given variable \a D was not seen yet,
307  /// add it and return NULL; otherwise return previous occurrence's expression
308  /// for diagnostics.
309  const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE);
310 
311  /// Register specified variable as loop control variable.
312  void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture);
313  /// Check if the specified variable is a loop control variable for
314  /// current region.
315  /// \return The index of the loop control variable in the list of associated
316  /// for-loops (from outer to inner).
317  const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const;
318  /// Check if the specified variable is a loop control variable for
319  /// parent region.
320  /// \return The index of the loop control variable in the list of associated
321  /// for-loops (from outer to inner).
322  const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const;
323  /// Get the loop control variable for the I-th loop (or nullptr) in
324  /// parent directive.
325  const ValueDecl *getParentLoopControlVariable(unsigned I) const;
326 
327  /// Adds explicit data sharing attribute to the specified declaration.
328  void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
329  DeclRefExpr *PrivateCopy = nullptr);
330 
331  /// Adds additional information for the reduction items with the reduction id
332  /// represented as an operator.
333  void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
334  BinaryOperatorKind BOK);
335  /// Adds additional information for the reduction items with the reduction id
336  /// represented as reduction identifier.
337  void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
338  const Expr *ReductionRef);
339  /// Returns the location and reduction operation from the innermost parent
340  /// region for the given \p D.
341  const DSAVarData
342  getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
343  BinaryOperatorKind &BOK,
344  Expr *&TaskgroupDescriptor) const;
345  /// Returns the location and reduction operation from the innermost parent
346  /// region for the given \p D.
347  const DSAVarData
348  getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
349  const Expr *&ReductionRef,
350  Expr *&TaskgroupDescriptor) const;
351  /// Return reduction reference expression for the current taskgroup.
352  Expr *getTaskgroupReductionRef() const {
353  assert(Stack.back().first.back().Directive == OMPD_taskgroup &&
354  "taskgroup reference expression requested for non taskgroup "
355  "directive.");
356  return Stack.back().first.back().TaskgroupReductionRef;
357  }
358  /// Checks if the given \p VD declaration is actually a taskgroup reduction
359  /// descriptor variable at the \p Level of OpenMP regions.
360  bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const {
361  return Stack.back().first[Level].TaskgroupReductionRef &&
362  cast<DeclRefExpr>(Stack.back().first[Level].TaskgroupReductionRef)
363  ->getDecl() == VD;
364  }
365 
366  /// Returns data sharing attributes from top of the stack for the
367  /// specified declaration.
368  const DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
369  /// Returns data-sharing attributes for the specified declaration.
370  const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const;
371  /// Checks if the specified variables has data-sharing attributes which
372  /// match specified \a CPred predicate in any directive which matches \a DPred
373  /// predicate.
374  const DSAVarData
375  hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
376  const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
377  bool FromParent) const;
378  /// Checks if the specified variables has data-sharing attributes which
379  /// match specified \a CPred predicate in any innermost directive which
380  /// matches \a DPred predicate.
381  const DSAVarData
382  hasInnermostDSA(ValueDecl *D,
383  const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
384  const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
385  bool FromParent) const;
386  /// Checks if the specified variables has explicit data-sharing
387  /// attributes which match specified \a CPred predicate at the specified
388  /// OpenMP region.
389  bool hasExplicitDSA(const ValueDecl *D,
390  const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
391  unsigned Level, bool NotLastprivate = false) const;
392 
393  /// Returns true if the directive at level \Level matches in the
394  /// specified \a DPred predicate.
395  bool hasExplicitDirective(
396  const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
397  unsigned Level) const;
398 
399  /// Finds a directive which matches specified \a DPred predicate.
400  bool hasDirective(
401  const llvm::function_ref<bool(
403  DPred,
404  bool FromParent) const;
405 
406  /// Returns currently analyzed directive.
407  OpenMPDirectiveKind getCurrentDirective() const {
408  return isStackEmpty() ? OMPD_unknown : Stack.back().first.back().Directive;
409  }
410  /// Returns directive kind at specified level.
411  OpenMPDirectiveKind getDirective(unsigned Level) const {
412  assert(!isStackEmpty() && "No directive at specified level.");
413  return Stack.back().first[Level].Directive;
414  }
415  /// Returns parent directive.
416  OpenMPDirectiveKind getParentDirective() const {
417  if (isStackEmpty() || Stack.back().first.size() == 1)
418  return OMPD_unknown;
419  return std::next(Stack.back().first.rbegin())->Directive;
420  }
421 
422  /// Add requires decl to internal vector
423  void addRequiresDecl(OMPRequiresDecl *RD) {
424  RequiresDecls.push_back(RD);
425  }
426 
427  /// Checks if the defined 'requires' directive has specified type of clause.
428  template <typename ClauseType>
429  bool hasRequiresDeclWithClause() {
430  return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
431  return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
432  return isa<ClauseType>(C);
433  });
434  });
435  }
436 
437  /// Checks for a duplicate clause amongst previously declared requires
438  /// directives
439  bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const {
440  bool IsDuplicate = false;
441  for (OMPClause *CNew : ClauseList) {
442  for (const OMPRequiresDecl *D : RequiresDecls) {
443  for (const OMPClause *CPrev : D->clauselists()) {
444  if (CNew->getClauseKind() == CPrev->getClauseKind()) {
445  SemaRef.Diag(CNew->getBeginLoc(),
446  diag::err_omp_requires_clause_redeclaration)
447  << getOpenMPClauseName(CNew->getClauseKind());
448  SemaRef.Diag(CPrev->getBeginLoc(),
449  diag::note_omp_requires_previous_clause)
450  << getOpenMPClauseName(CPrev->getClauseKind());
451  IsDuplicate = true;
452  }
453  }
454  }
455  }
456  return IsDuplicate;
457  }
458 
459  /// Add location of previously encountered target to internal vector
460  void addTargetDirLocation(SourceLocation LocStart) {
461  TargetLocations.push_back(LocStart);
462  }
463 
464  // Return previously encountered target region locations.
465  ArrayRef<SourceLocation> getEncounteredTargetLocs() const {
466  return TargetLocations;
467  }
468 
469  /// Set default data sharing attribute to none.
470  void setDefaultDSANone(SourceLocation Loc) {
471  assert(!isStackEmpty());
472  Stack.back().first.back().DefaultAttr = DSA_none;
473  Stack.back().first.back().DefaultAttrLoc = Loc;
474  }
475  /// Set default data sharing attribute to shared.
476  void setDefaultDSAShared(SourceLocation Loc) {
477  assert(!isStackEmpty());
478  Stack.back().first.back().DefaultAttr = DSA_shared;
479  Stack.back().first.back().DefaultAttrLoc = Loc;
480  }
481  /// Set default data mapping attribute to 'tofrom:scalar'.
482  void setDefaultDMAToFromScalar(SourceLocation Loc) {
483  assert(!isStackEmpty());
484  Stack.back().first.back().DefaultMapAttr = DMA_tofrom_scalar;
485  Stack.back().first.back().DefaultMapAttrLoc = Loc;
486  }
487 
488  DefaultDataSharingAttributes getDefaultDSA() const {
489  return isStackEmpty() ? DSA_unspecified
490  : Stack.back().first.back().DefaultAttr;
491  }
492  SourceLocation getDefaultDSALocation() const {
493  return isStackEmpty() ? SourceLocation()
494  : Stack.back().first.back().DefaultAttrLoc;
495  }
496  DefaultMapAttributes getDefaultDMA() const {
497  return isStackEmpty() ? DMA_unspecified
498  : Stack.back().first.back().DefaultMapAttr;
499  }
500  DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const {
501  return Stack.back().first[Level].DefaultMapAttr;
502  }
503  SourceLocation getDefaultDMALocation() const {
504  return isStackEmpty() ? SourceLocation()
505  : Stack.back().first.back().DefaultMapAttrLoc;
506  }
507 
508  /// Checks if the specified variable is a threadprivate.
509  bool isThreadPrivate(VarDecl *D) {
510  const DSAVarData DVar = getTopDSA(D, false);
511  return isOpenMPThreadPrivate(DVar.CKind);
512  }
513 
514  /// Marks current region as ordered (it has an 'ordered' clause).
515  void setOrderedRegion(bool IsOrdered, const Expr *Param,
516  OMPOrderedClause *Clause) {
517  assert(!isStackEmpty());
518  if (IsOrdered)
519  Stack.back().first.back().OrderedRegion.emplace(Param, Clause);
520  else
521  Stack.back().first.back().OrderedRegion.reset();
522  }
523  /// Returns true, if region is ordered (has associated 'ordered' clause),
524  /// false - otherwise.
525  bool isOrderedRegion() const {
526  if (isStackEmpty())
527  return false;
528  return Stack.back().first.rbegin()->OrderedRegion.hasValue();
529  }
530  /// Returns optional parameter for the ordered region.
531  std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const {
532  if (isStackEmpty() ||
533  !Stack.back().first.rbegin()->OrderedRegion.hasValue())
534  return std::make_pair(nullptr, nullptr);
535  return Stack.back().first.rbegin()->OrderedRegion.getValue();
536  }
537  /// Returns true, if parent region is ordered (has associated
538  /// 'ordered' clause), false - otherwise.
539  bool isParentOrderedRegion() const {
540  if (isStackEmpty() || Stack.back().first.size() == 1)
541  return false;
542  return std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue();
543  }
544  /// Returns optional parameter for the ordered region.
545  std::pair<const Expr *, OMPOrderedClause *>
546  getParentOrderedRegionParam() const {
547  if (isStackEmpty() || Stack.back().first.size() == 1 ||
548  !std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue())
549  return std::make_pair(nullptr, nullptr);
550  return std::next(Stack.back().first.rbegin())->OrderedRegion.getValue();
551  }
552  /// Marks current region as nowait (it has a 'nowait' clause).
553  void setNowaitRegion(bool IsNowait = true) {
554  assert(!isStackEmpty());
555  Stack.back().first.back().NowaitRegion = IsNowait;
556  }
557  /// Returns true, if parent region is nowait (has associated
558  /// 'nowait' clause), false - otherwise.
559  bool isParentNowaitRegion() const {
560  if (isStackEmpty() || Stack.back().first.size() == 1)
561  return false;
562  return std::next(Stack.back().first.rbegin())->NowaitRegion;
563  }
564  /// Marks parent region as cancel region.
565  void setParentCancelRegion(bool Cancel = true) {
566  if (!isStackEmpty() && Stack.back().first.size() > 1) {
567  auto &StackElemRef = *std::next(Stack.back().first.rbegin());
568  StackElemRef.CancelRegion |= StackElemRef.CancelRegion || Cancel;
569  }
570  }
571  /// Return true if current region has inner cancel construct.
572  bool isCancelRegion() const {
573  return isStackEmpty() ? false : Stack.back().first.back().CancelRegion;
574  }
575 
576  /// Set collapse value for the region.
577  void setAssociatedLoops(unsigned Val) {
578  assert(!isStackEmpty());
579  Stack.back().first.back().AssociatedLoops = Val;
580  }
581  /// Return collapse value for region.
582  unsigned getAssociatedLoops() const {
583  return isStackEmpty() ? 0 : Stack.back().first.back().AssociatedLoops;
584  }
585 
586  /// Marks current target region as one with closely nested teams
587  /// region.
588  void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
589  if (!isStackEmpty() && Stack.back().first.size() > 1) {
590  std::next(Stack.back().first.rbegin())->InnerTeamsRegionLoc =
591  TeamsRegionLoc;
592  }
593  }
594  /// Returns true, if current region has closely nested teams region.
595  bool hasInnerTeamsRegion() const {
596  return getInnerTeamsRegionLoc().isValid();
597  }
598  /// Returns location of the nested teams region (if any).
599  SourceLocation getInnerTeamsRegionLoc() const {
600  return isStackEmpty() ? SourceLocation()
601  : Stack.back().first.back().InnerTeamsRegionLoc;
602  }
603 
604  Scope *getCurScope() const {
605  return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope;
606  }
607  SourceLocation getConstructLoc() const {
608  return isStackEmpty() ? SourceLocation()
609  : Stack.back().first.back().ConstructLoc;
610  }
611 
612  /// Do the check specified in \a Check to all component lists and return true
613  /// if any issue is found.
614  bool checkMappableExprComponentListsForDecl(
615  const ValueDecl *VD, bool CurrentRegionOnly,
616  const llvm::function_ref<
619  Check) const {
620  if (isStackEmpty())
621  return false;
622  auto SI = Stack.back().first.rbegin();
623  auto SE = Stack.back().first.rend();
624 
625  if (SI == SE)
626  return false;
627 
628  if (CurrentRegionOnly)
629  SE = std::next(SI);
630  else
631  std::advance(SI, 1);
632 
633  for (; SI != SE; ++SI) {
634  auto MI = SI->MappedExprComponents.find(VD);
635  if (MI != SI->MappedExprComponents.end())
637  MI->second.Components)
638  if (Check(L, MI->second.Kind))
639  return true;
640  }
641  return false;
642  }
643 
644  /// Do the check specified in \a Check to all component lists at a given level
645  /// and return true if any issue is found.
646  bool checkMappableExprComponentListsForDeclAtLevel(
647  const ValueDecl *VD, unsigned Level,
648  const llvm::function_ref<
651  Check) const {
652  if (isStackEmpty())
653  return false;
654 
655  auto StartI = Stack.back().first.begin();
656  auto EndI = Stack.back().first.end();
657  if (std::distance(StartI, EndI) <= (int)Level)
658  return false;
659  std::advance(StartI, Level);
660 
661  auto MI = StartI->MappedExprComponents.find(VD);
662  if (MI != StartI->MappedExprComponents.end())
664  MI->second.Components)
665  if (Check(L, MI->second.Kind))
666  return true;
667  return false;
668  }
669 
670  /// Create a new mappable expression component list associated with a given
671  /// declaration and initialize it with the provided list of components.
672  void addMappableExpressionComponents(
673  const ValueDecl *VD,
675  OpenMPClauseKind WhereFoundClauseKind) {
676  assert(!isStackEmpty() &&
677  "Not expecting to retrieve components from a empty stack!");
678  MappedExprComponentTy &MEC =
679  Stack.back().first.back().MappedExprComponents[VD];
680  // Create new entry and append the new components there.
681  MEC.Components.resize(MEC.Components.size() + 1);
682  MEC.Components.back().append(Components.begin(), Components.end());
683  MEC.Kind = WhereFoundClauseKind;
684  }
685 
686  unsigned getNestingLevel() const {
687  assert(!isStackEmpty());
688  return Stack.back().first.size() - 1;
689  }
690  void addDoacrossDependClause(OMPDependClause *C,
691  const OperatorOffsetTy &OpsOffs) {
692  assert(!isStackEmpty() && Stack.back().first.size() > 1);
693  SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
694  assert(isOpenMPWorksharingDirective(StackElem.Directive));
695  StackElem.DoacrossDepends.try_emplace(C, OpsOffs);
696  }
697  llvm::iterator_range<DoacrossDependMapTy::const_iterator>
698  getDoacrossDependClauses() const {
699  assert(!isStackEmpty());
700  const SharingMapTy &StackElem = Stack.back().first.back();
701  if (isOpenMPWorksharingDirective(StackElem.Directive)) {
702  const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends;
703  return llvm::make_range(Ref.begin(), Ref.end());
704  }
705  return llvm::make_range(StackElem.DoacrossDepends.end(),
706  StackElem.DoacrossDepends.end());
707  }
708 
709  // Store types of classes which have been explicitly mapped
710  void addMappedClassesQualTypes(QualType QT) {
711  SharingMapTy &StackElem = Stack.back().first.back();
712  StackElem.MappedClassesQualTypes.insert(QT);
713  }
714 
715  // Return set of mapped classes types
716  bool isClassPreviouslyMapped(QualType QT) const {
717  const SharingMapTy &StackElem = Stack.back().first.back();
718  return StackElem.MappedClassesQualTypes.count(QT) != 0;
719  }
720 
721  /// Adds global declare target to the parent target region.
722  void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) {
723  assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
724  E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link &&
725  "Expected declare target link global.");
726  if (isStackEmpty())
727  return;
728  auto It = Stack.back().first.rbegin();
729  while (It != Stack.back().first.rend() &&
730  !isOpenMPTargetExecutionDirective(It->Directive))
731  ++It;
732  if (It != Stack.back().first.rend()) {
733  assert(isOpenMPTargetExecutionDirective(It->Directive) &&
734  "Expected target executable directive.");
735  It->DeclareTargetLinkVarDecls.push_back(E);
736  }
737  }
738 
739  /// Returns the list of globals with declare target link if current directive
740  /// is target.
741  ArrayRef<DeclRefExpr *> getLinkGlobals() const {
742  assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) &&
743  "Expected target executable directive.");
744  return Stack.back().first.back().DeclareTargetLinkVarDecls;
745  }
746 };
747 
748 bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) {
749  return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind);
750 }
751 
752 bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) {
753  return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) || DKind == OMPD_unknown;
754 }
755 
756 } // namespace
757 
758 static const Expr *getExprAsWritten(const Expr *E) {
759  if (const auto *FE = dyn_cast<FullExpr>(E))
760  E = FE->getSubExpr();
761 
762  if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
763  E = MTE->GetTemporaryExpr();
764 
765  while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
766  E = Binder->getSubExpr();
767 
768  if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
769  E = ICE->getSubExprAsWritten();
770  return E->IgnoreParens();
771 }
772 
774  return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E)));
775 }
776 
777 static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
778  if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
779  if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
780  D = ME->getMemberDecl();
781  const auto *VD = dyn_cast<VarDecl>(D);
782  const auto *FD = dyn_cast<FieldDecl>(D);
783  if (VD != nullptr) {
784  VD = VD->getCanonicalDecl();
785  D = VD;
786  } else {
787  assert(FD);
788  FD = FD->getCanonicalDecl();
789  D = FD;
790  }
791  return D;
792 }
793 
795  return const_cast<ValueDecl *>(
796  getCanonicalDecl(const_cast<const ValueDecl *>(D)));
797 }
798 
799 DSAStackTy::DSAVarData DSAStackTy::getDSA(iterator &Iter,
800  ValueDecl *D) const {
801  D = getCanonicalDecl(D);
802  auto *VD = dyn_cast<VarDecl>(D);
803  const auto *FD = dyn_cast<FieldDecl>(D);
804  DSAVarData DVar;
805  if (isStackEmpty() || Iter == Stack.back().first.rend()) {
806  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
807  // in a region but not in construct]
808  // File-scope or namespace-scope variables referenced in called routines
809  // in the region are shared unless they appear in a threadprivate
810  // directive.
811  if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD))
812  DVar.CKind = OMPC_shared;
813 
814  // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
815  // in a region but not in construct]
816  // Variables with static storage duration that are declared in called
817  // routines in the region are shared.
818  if (VD && VD->hasGlobalStorage())
819  DVar.CKind = OMPC_shared;
820 
821  // Non-static data members are shared by default.
822  if (FD)
823  DVar.CKind = OMPC_shared;
824 
825  return DVar;
826  }
827 
828  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
829  // in a Construct, C/C++, predetermined, p.1]
830  // Variables with automatic storage duration that are declared in a scope
831  // inside the construct are private.
832  if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
833  (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
834  DVar.CKind = OMPC_private;
835  return DVar;
836  }
837 
838  DVar.DKind = Iter->Directive;
839  // Explicitly specified attributes and local variables with predetermined
840  // attributes.
841  if (Iter->SharingMap.count(D)) {
842  const DSAInfo &Data = Iter->SharingMap.lookup(D);
843  DVar.RefExpr = Data.RefExpr.getPointer();
844  DVar.PrivateCopy = Data.PrivateCopy;
845  DVar.CKind = Data.Attributes;
846  DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
847  return DVar;
848  }
849 
850  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
851  // in a Construct, C/C++, implicitly determined, p.1]
852  // In a parallel or task construct, the data-sharing attributes of these
853  // variables are determined by the default clause, if present.
854  switch (Iter->DefaultAttr) {
855  case DSA_shared:
856  DVar.CKind = OMPC_shared;
857  DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
858  return DVar;
859  case DSA_none:
860  return DVar;
861  case DSA_unspecified:
862  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
863  // in a Construct, implicitly determined, p.2]
864  // In a parallel construct, if no default clause is present, these
865  // variables are shared.
866  DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
867  if (isOpenMPParallelDirective(DVar.DKind) ||
868  isOpenMPTeamsDirective(DVar.DKind)) {
869  DVar.CKind = OMPC_shared;
870  return DVar;
871  }
872 
873  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
874  // in a Construct, implicitly determined, p.4]
875  // In a task construct, if no default clause is present, a variable that in
876  // the enclosing context is determined to be shared by all implicit tasks
877  // bound to the current team is shared.
878  if (isOpenMPTaskingDirective(DVar.DKind)) {
879  DSAVarData DVarTemp;
880  iterator I = Iter, E = Stack.back().first.rend();
881  do {
882  ++I;
883  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
884  // Referenced in a Construct, implicitly determined, p.6]
885  // In a task construct, if no default clause is present, a variable
886  // whose data-sharing attribute is not determined by the rules above is
887  // firstprivate.
888  DVarTemp = getDSA(I, D);
889  if (DVarTemp.CKind != OMPC_shared) {
890  DVar.RefExpr = nullptr;
891  DVar.CKind = OMPC_firstprivate;
892  return DVar;
893  }
894  } while (I != E && !isImplicitTaskingRegion(I->Directive));
895  DVar.CKind =
896  (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
897  return DVar;
898  }
899  }
900  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
901  // in a Construct, implicitly determined, p.3]
902  // For constructs other than task, if no default clause is present, these
903  // variables inherit their data-sharing attributes from the enclosing
904  // context.
905  return getDSA(++Iter, D);
906 }
907 
908 const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
909  const Expr *NewDE) {
910  assert(!isStackEmpty() && "Data sharing attributes stack is empty");
911  D = getCanonicalDecl(D);
912  SharingMapTy &StackElem = Stack.back().first.back();
913  auto It = StackElem.AlignedMap.find(D);
914  if (It == StackElem.AlignedMap.end()) {
915  assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
916  StackElem.AlignedMap[D] = NewDE;
917  return nullptr;
918  }
919  assert(It->second && "Unexpected nullptr expr in the aligned map");
920  return It->second;
921 }
922 
923 void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
924  assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
925  D = getCanonicalDecl(D);
926  SharingMapTy &StackElem = Stack.back().first.back();
927  StackElem.LCVMap.try_emplace(
928  D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
929 }
930 
931 const DSAStackTy::LCDeclInfo
932 DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
933  assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
934  D = getCanonicalDecl(D);
935  const SharingMapTy &StackElem = Stack.back().first.back();
936  auto It = StackElem.LCVMap.find(D);
937  if (It != StackElem.LCVMap.end())
938  return It->second;
939  return {0, nullptr};
940 }
941 
942 const DSAStackTy::LCDeclInfo
943 DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
944  assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
945  "Data-sharing attributes stack is empty");
946  D = getCanonicalDecl(D);
947  const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
948  auto It = StackElem.LCVMap.find(D);
949  if (It != StackElem.LCVMap.end())
950  return It->second;
951  return {0, nullptr};
952 }
953 
954 const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
955  assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
956  "Data-sharing attributes stack is empty");
957  const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
958  if (StackElem.LCVMap.size() < I)
959  return nullptr;
960  for (const auto &Pair : StackElem.LCVMap)
961  if (Pair.second.first == I)
962  return Pair.first;
963  return nullptr;
964 }
965 
966 void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
967  DeclRefExpr *PrivateCopy) {
968  D = getCanonicalDecl(D);
969  if (A == OMPC_threadprivate) {
970  DSAInfo &Data = Threadprivates[D];
971  Data.Attributes = A;
972  Data.RefExpr.setPointer(E);
973  Data.PrivateCopy = nullptr;
974  } else {
975  assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
976  DSAInfo &Data = Stack.back().first.back().SharingMap[D];
977  assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
978  (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
979  (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
980  (isLoopControlVariable(D).first && A == OMPC_private));
981  if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
982  Data.RefExpr.setInt(/*IntVal=*/true);
983  return;
984  }
985  const bool IsLastprivate =
986  A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
987  Data.Attributes = A;
988  Data.RefExpr.setPointerAndInt(E, IsLastprivate);
989  Data.PrivateCopy = PrivateCopy;
990  if (PrivateCopy) {
991  DSAInfo &Data =
992  Stack.back().first.back().SharingMap[PrivateCopy->getDecl()];
993  Data.Attributes = A;
994  Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
995  Data.PrivateCopy = nullptr;
996  }
997  }
998 }
999 
1000 /// Build a variable declaration for OpenMP loop iteration variable.
1002  StringRef Name, const AttrVec *Attrs = nullptr,
1003  DeclRefExpr *OrigRef = nullptr) {
1004  DeclContext *DC = SemaRef.CurContext;
1005  IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
1006  TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
1007  auto *Decl =
1008  VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
1009  if (Attrs) {
1010  for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
1011  I != E; ++I)
1012  Decl->addAttr(*I);
1013  }
1014  Decl->setImplicit();
1015  if (OrigRef) {
1016  Decl->addAttr(
1017  OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
1018  }
1019  return Decl;
1020 }
1021 
1023  SourceLocation Loc,
1024  bool RefersToCapture = false) {
1025  D->setReferenced();
1026  D->markUsed(S.Context);
1028  SourceLocation(), D, RefersToCapture, Loc, Ty,
1029  VK_LValue);
1030 }
1031 
1032 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1033  BinaryOperatorKind BOK) {
1034  D = getCanonicalDecl(D);
1035  assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1036  assert(
1037  Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
1038  "Additional reduction info may be specified only for reduction items.");
1039  ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
1040  assert(ReductionData.ReductionRange.isInvalid() &&
1041  Stack.back().first.back().Directive == OMPD_taskgroup &&
1042  "Additional reduction info may be specified only once for reduction "
1043  "items.");
1044  ReductionData.set(BOK, SR);
1045  Expr *&TaskgroupReductionRef =
1046  Stack.back().first.back().TaskgroupReductionRef;
1047  if (!TaskgroupReductionRef) {
1048  VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1049  SemaRef.Context.VoidPtrTy, ".task_red.");
1050  TaskgroupReductionRef =
1051  buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
1052  }
1053 }
1054 
1055 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1056  const Expr *ReductionRef) {
1057  D = getCanonicalDecl(D);
1058  assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1059  assert(
1060  Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
1061  "Additional reduction info may be specified only for reduction items.");
1062  ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
1063  assert(ReductionData.ReductionRange.isInvalid() &&
1064  Stack.back().first.back().Directive == OMPD_taskgroup &&
1065  "Additional reduction info may be specified only once for reduction "
1066  "items.");
1067  ReductionData.set(ReductionRef, SR);
1068  Expr *&TaskgroupReductionRef =
1069  Stack.back().first.back().TaskgroupReductionRef;
1070  if (!TaskgroupReductionRef) {
1071  VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1072  SemaRef.Context.VoidPtrTy, ".task_red.");
1073  TaskgroupReductionRef =
1074  buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
1075  }
1076 }
1077 
1078 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1079  const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
1080  Expr *&TaskgroupDescriptor) const {
1081  D = getCanonicalDecl(D);
1082  assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1083  if (Stack.back().first.empty())
1084  return DSAVarData();
1085  for (iterator I = std::next(Stack.back().first.rbegin(), 1),
1086  E = Stack.back().first.rend();
1087  I != E; std::advance(I, 1)) {
1088  const DSAInfo &Data = I->SharingMap.lookup(D);
1089  if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
1090  continue;
1091  const ReductionData &ReductionData = I->ReductionMap.lookup(D);
1092  if (!ReductionData.ReductionOp ||
1093  ReductionData.ReductionOp.is<const Expr *>())
1094  return DSAVarData();
1095  SR = ReductionData.ReductionRange;
1096  BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
1097  assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1098  "expression for the descriptor is not "
1099  "set.");
1100  TaskgroupDescriptor = I->TaskgroupReductionRef;
1101  return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1102  Data.PrivateCopy, I->DefaultAttrLoc);
1103  }
1104  return DSAVarData();
1105 }
1106 
1107 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1108  const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
1109  Expr *&TaskgroupDescriptor) const {
1110  D = getCanonicalDecl(D);
1111  assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1112  if (Stack.back().first.empty())
1113  return DSAVarData();
1114  for (iterator I = std::next(Stack.back().first.rbegin(), 1),
1115  E = Stack.back().first.rend();
1116  I != E; std::advance(I, 1)) {
1117  const DSAInfo &Data = I->SharingMap.lookup(D);
1118  if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
1119  continue;
1120  const ReductionData &ReductionData = I->ReductionMap.lookup(D);
1121  if (!ReductionData.ReductionOp ||
1122  !ReductionData.ReductionOp.is<const Expr *>())
1123  return DSAVarData();
1124  SR = ReductionData.ReductionRange;
1125  ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
1126  assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1127  "expression for the descriptor is not "
1128  "set.");
1129  TaskgroupDescriptor = I->TaskgroupReductionRef;
1130  return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1131  Data.PrivateCopy, I->DefaultAttrLoc);
1132  }
1133  return DSAVarData();
1134 }
1135 
1136 bool DSAStackTy::isOpenMPLocal(VarDecl *D, iterator Iter) const {
1137  D = D->getCanonicalDecl();
1138  if (!isStackEmpty()) {
1139  iterator I = Iter, E = Stack.back().first.rend();
1140  Scope *TopScope = nullptr;
1141  while (I != E && !isImplicitOrExplicitTaskingRegion(I->Directive) &&
1142  !isOpenMPTargetExecutionDirective(I->Directive))
1143  ++I;
1144  if (I == E)
1145  return false;
1146  TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
1147  Scope *CurScope = getCurScope();
1148  while (CurScope != TopScope && !CurScope->isDeclScope(D))
1149  CurScope = CurScope->getParent();
1150  return CurScope != TopScope;
1151  }
1152  return false;
1153 }
1154 
1155 static bool isConstNotMutableType(Sema &SemaRef, QualType Type,
1156  bool AcceptIfMutable = true,
1157  bool *IsClassType = nullptr) {
1158  ASTContext &Context = SemaRef.getASTContext();
1159  Type = Type.getNonReferenceType().getCanonicalType();
1160  bool IsConstant = Type.isConstant(Context);
1161  Type = Context.getBaseElementType(Type);
1162  const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus
1163  ? Type->getAsCXXRecordDecl()
1164  : nullptr;
1165  if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1166  if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
1167  RD = CTD->getTemplatedDecl();
1168  if (IsClassType)
1169  *IsClassType = RD;
1170  return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD &&
1171  RD->hasDefinition() && RD->hasMutableFields());
1172 }
1173 
1174 static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D,
1176  SourceLocation ELoc,
1177  bool AcceptIfMutable = true,
1178  bool ListItemNotVar = false) {
1179  ASTContext &Context = SemaRef.getASTContext();
1180  bool IsClassType;
1181  if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) {
1182  unsigned Diag = ListItemNotVar
1183  ? diag::err_omp_const_list_item
1184  : IsClassType ? diag::err_omp_const_not_mutable_variable
1185  : diag::err_omp_const_variable;
1186  SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind);
1187  if (!ListItemNotVar && D) {
1188  const VarDecl *VD = dyn_cast<VarDecl>(D);
1189  bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
1191  SemaRef.Diag(D->getLocation(),
1192  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1193  << D;
1194  }
1195  return true;
1196  }
1197  return false;
1198 }
1199 
1200 const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1201  bool FromParent) {
1202  D = getCanonicalDecl(D);
1203  DSAVarData DVar;
1204 
1205  auto *VD = dyn_cast<VarDecl>(D);
1206  auto TI = Threadprivates.find(D);
1207  if (TI != Threadprivates.end()) {
1208  DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
1209  DVar.CKind = OMPC_threadprivate;
1210  return DVar;
1211  }
1212  if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
1213  DVar.RefExpr = buildDeclRefExpr(
1214  SemaRef, VD, D->getType().getNonReferenceType(),
1215  VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1216  DVar.CKind = OMPC_threadprivate;
1217  addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1218  return DVar;
1219  }
1220  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1221  // in a Construct, C/C++, predetermined, p.1]
1222  // Variables appearing in threadprivate directives are threadprivate.
1223  if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
1224  !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1225  SemaRef.getLangOpts().OpenMPUseTLS &&
1226  SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
1227  (VD && VD->getStorageClass() == SC_Register &&
1228  VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
1229  DVar.RefExpr = buildDeclRefExpr(
1230  SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
1231  DVar.CKind = OMPC_threadprivate;
1232  addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1233  return DVar;
1234  }
1235  if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
1236  VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
1237  !isLoopControlVariable(D).first) {
1238  iterator IterTarget =
1239  std::find_if(Stack.back().first.rbegin(), Stack.back().first.rend(),
1240  [](const SharingMapTy &Data) {
1241  return isOpenMPTargetExecutionDirective(Data.Directive);
1242  });
1243  if (IterTarget != Stack.back().first.rend()) {
1244  iterator ParentIterTarget = std::next(IterTarget, 1);
1245  for (iterator Iter = Stack.back().first.rbegin();
1246  Iter != ParentIterTarget; std::advance(Iter, 1)) {
1247  if (isOpenMPLocal(VD, Iter)) {
1248  DVar.RefExpr =
1249  buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1250  D->getLocation());
1251  DVar.CKind = OMPC_threadprivate;
1252  return DVar;
1253  }
1254  }
1255  if (!isClauseParsingMode() || IterTarget != Stack.back().first.rbegin()) {
1256  auto DSAIter = IterTarget->SharingMap.find(D);
1257  if (DSAIter != IterTarget->SharingMap.end() &&
1258  isOpenMPPrivate(DSAIter->getSecond().Attributes)) {
1259  DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1260  DVar.CKind = OMPC_threadprivate;
1261  return DVar;
1262  }
1263  iterator End = Stack.back().first.rend();
1264  if (!SemaRef.isOpenMPCapturedByRef(
1265  D, std::distance(ParentIterTarget, End))) {
1266  DVar.RefExpr =
1267  buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1268  IterTarget->ConstructLoc);
1269  DVar.CKind = OMPC_threadprivate;
1270  return DVar;
1271  }
1272  }
1273  }
1274  }
1275 
1276  if (isStackEmpty())
1277  // Not in OpenMP execution region and top scope was already checked.
1278  return DVar;
1279 
1280  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1281  // in a Construct, C/C++, predetermined, p.4]
1282  // Static data members are shared.
1283  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1284  // in a Construct, C/C++, predetermined, p.7]
1285  // Variables with static storage duration that are declared in a scope
1286  // inside the construct are shared.
1287  auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
1288  if (VD && VD->isStaticDataMember()) {
1289  DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
1290  if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
1291  return DVar;
1292 
1293  DVar.CKind = OMPC_shared;
1294  return DVar;
1295  }
1296 
1297  // The predetermined shared attribute for const-qualified types having no
1298  // mutable members was removed after OpenMP 3.1.
1299  if (SemaRef.LangOpts.OpenMP <= 31) {
1300  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1301  // in a Construct, C/C++, predetermined, p.6]
1302  // Variables with const qualified type having no mutable member are
1303  // shared.
1304  if (isConstNotMutableType(SemaRef, D->getType())) {
1305  // Variables with const-qualified type having no mutable member may be
1306  // listed in a firstprivate clause, even if they are static data members.
1307  DSAVarData DVarTemp = hasInnermostDSA(
1308  D,
1309  [](OpenMPClauseKind C) {
1310  return C == OMPC_firstprivate || C == OMPC_shared;
1311  },
1312  MatchesAlways, FromParent);
1313  if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
1314  return DVarTemp;
1315 
1316  DVar.CKind = OMPC_shared;
1317  return DVar;
1318  }
1319  }
1320 
1321  // Explicitly specified attributes and local variables with predetermined
1322  // attributes.
1323  iterator I = Stack.back().first.rbegin();
1324  iterator EndI = Stack.back().first.rend();
1325  if (FromParent && I != EndI)
1326  std::advance(I, 1);
1327  auto It = I->SharingMap.find(D);
1328  if (It != I->SharingMap.end()) {
1329  const DSAInfo &Data = It->getSecond();
1330  DVar.RefExpr = Data.RefExpr.getPointer();
1331  DVar.PrivateCopy = Data.PrivateCopy;
1332  DVar.CKind = Data.Attributes;
1333  DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1334  DVar.DKind = I->Directive;
1335  }
1336 
1337  return DVar;
1338 }
1339 
1340 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1341  bool FromParent) const {
1342  if (isStackEmpty()) {
1343  iterator I;
1344  return getDSA(I, D);
1345  }
1346  D = getCanonicalDecl(D);
1347  iterator StartI = Stack.back().first.rbegin();
1348  iterator EndI = Stack.back().first.rend();
1349  if (FromParent && StartI != EndI)
1350  std::advance(StartI, 1);
1351  return getDSA(StartI, D);
1352 }
1353 
1354 const DSAStackTy::DSAVarData
1355 DSAStackTy::hasDSA(ValueDecl *D,
1356  const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1357  const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1358  bool FromParent) const {
1359  if (isStackEmpty())
1360  return {};
1361  D = getCanonicalDecl(D);
1362  iterator I = Stack.back().first.rbegin();
1363  iterator EndI = Stack.back().first.rend();
1364  if (FromParent && I != EndI)
1365  std::advance(I, 1);
1366  for (; I != EndI; std::advance(I, 1)) {
1367  if (!DPred(I->Directive) && !isImplicitOrExplicitTaskingRegion(I->Directive))
1368  continue;
1369  iterator NewI = I;
1370  DSAVarData DVar = getDSA(NewI, D);
1371  if (I == NewI && CPred(DVar.CKind))
1372  return DVar;
1373  }
1374  return {};
1375 }
1376 
1377 const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
1378  ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1379  const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1380  bool FromParent) const {
1381  if (isStackEmpty())
1382  return {};
1383  D = getCanonicalDecl(D);
1384  iterator StartI = Stack.back().first.rbegin();
1385  iterator EndI = Stack.back().first.rend();
1386  if (FromParent && StartI != EndI)
1387  std::advance(StartI, 1);
1388  if (StartI == EndI || !DPred(StartI->Directive))
1389  return {};
1390  iterator NewI = StartI;
1391  DSAVarData DVar = getDSA(NewI, D);
1392  return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData();
1393 }
1394 
1395 bool DSAStackTy::hasExplicitDSA(
1396  const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1397  unsigned Level, bool NotLastprivate) const {
1398  if (isStackEmpty())
1399  return false;
1400  D = getCanonicalDecl(D);
1401  auto StartI = Stack.back().first.begin();
1402  auto EndI = Stack.back().first.end();
1403  if (std::distance(StartI, EndI) <= (int)Level)
1404  return false;
1405  std::advance(StartI, Level);
1406  auto I = StartI->SharingMap.find(D);
1407  if ((I != StartI->SharingMap.end()) &&
1408  I->getSecond().RefExpr.getPointer() &&
1409  CPred(I->getSecond().Attributes) &&
1410  (!NotLastprivate || !I->getSecond().RefExpr.getInt()))
1411  return true;
1412  // Check predetermined rules for the loop control variables.
1413  auto LI = StartI->LCVMap.find(D);
1414  if (LI != StartI->LCVMap.end())
1415  return CPred(OMPC_private);
1416  return false;
1417 }
1418 
1419 bool DSAStackTy::hasExplicitDirective(
1420  const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1421  unsigned Level) const {
1422  if (isStackEmpty())
1423  return false;
1424  auto StartI = Stack.back().first.begin();
1425  auto EndI = Stack.back().first.end();
1426  if (std::distance(StartI, EndI) <= (int)Level)
1427  return false;
1428  std::advance(StartI, Level);
1429  return DPred(StartI->Directive);
1430 }
1431 
1432 bool DSAStackTy::hasDirective(
1433  const llvm::function_ref<bool(OpenMPDirectiveKind,
1435  DPred,
1436  bool FromParent) const {
1437  // We look only in the enclosing region.
1438  if (isStackEmpty())
1439  return false;
1440  auto StartI = std::next(Stack.back().first.rbegin());
1441  auto EndI = Stack.back().first.rend();
1442  if (FromParent && StartI != EndI)
1443  StartI = std::next(StartI);
1444  for (auto I = StartI, EE = EndI; I != EE; ++I) {
1445  if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1446  return true;
1447  }
1448  return false;
1449 }
1450 
1451 void Sema::InitDataSharingAttributesStack() {
1452  VarDataSharingAttributesStack = new DSAStackTy(*this);
1453 }
1454 
1455 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1456 
1457 void Sema::pushOpenMPFunctionRegion() {
1458  DSAStack->pushFunction();
1459 }
1460 
1461 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1462  DSAStack->popFunction(OldFSI);
1463 }
1464 
1466  assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice &&
1467  "Expected OpenMP device compilation.");
1468  return !S.isInOpenMPTargetExecutionDirective() &&
1470 }
1471 
1472 /// Do we know that we will eventually codegen the given function?
1473 static bool isKnownEmitted(Sema &S, FunctionDecl *FD) {
1474  assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice &&
1475  "Expected OpenMP device compilation.");
1476  // Templates are emitted when they're instantiated.
1477  if (FD->isDependentContext())
1478  return false;
1479 
1480  if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
1481  FD->getCanonicalDecl()))
1482  return true;
1483 
1484  // Otherwise, the function is known-emitted if it's in our set of
1485  // known-emitted functions.
1486  return S.DeviceKnownEmittedFns.count(FD) > 0;
1487 }
1488 
1490  unsigned DiagID) {
1491  assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice &&
1492  "Expected OpenMP device compilation.");
1494  !isKnownEmitted(*this, getCurFunctionDecl()))
1495  ? DeviceDiagBuilder::K_Deferred
1496  : DeviceDiagBuilder::K_Immediate,
1497  Loc, DiagID, getCurFunctionDecl(), *this);
1498 }
1499 
1500 void Sema::checkOpenMPDeviceFunction(SourceLocation Loc, FunctionDecl *Callee) {
1501  assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice &&
1502  "Expected OpenMP device compilation.");
1503  assert(Callee && "Callee may not be null.");
1504  FunctionDecl *Caller = getCurFunctionDecl();
1505 
1506  // If the caller is known-emitted, mark the callee as known-emitted.
1507  // Otherwise, mark the call in our call graph so we can traverse it later.
1508  if (!isOpenMPDeviceDelayedContext(*this) ||
1509  (Caller && isKnownEmitted(*this, Caller)))
1510  markKnownEmitted(*this, Caller, Callee, Loc, isKnownEmitted);
1511  else if (Caller)
1512  DeviceCallGraph[Caller].insert({Callee, Loc});
1513 }
1514 
1515 void Sema::checkOpenMPDeviceExpr(const Expr *E) {
1516  assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice &&
1517  "OpenMP device compilation mode is expected.");
1518  QualType Ty = E->getType();
1519  if ((Ty->isFloat16Type() && !Context.getTargetInfo().hasFloat16Type()) ||
1520  (Ty->isFloat128Type() && !Context.getTargetInfo().hasFloat128Type()) ||
1521  (Ty->isIntegerType() && Context.getTypeSize(Ty) == 128 &&
1522  !Context.getTargetInfo().hasInt128Type()))
1523  targetDiag(E->getExprLoc(), diag::err_type_unsupported)
1524  << Ty << E->getSourceRange();
1525 }
1526 
1527 bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level) const {
1528  assert(LangOpts.OpenMP && "OpenMP is not allowed");
1529 
1530  ASTContext &Ctx = getASTContext();
1531  bool IsByRef = true;
1532 
1533  // Find the directive that is associated with the provided scope.
1534  D = cast<ValueDecl>(D->getCanonicalDecl());
1535  QualType Ty = D->getType();
1536 
1537  if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
1538  // This table summarizes how a given variable should be passed to the device
1539  // given its type and the clauses where it appears. This table is based on
1540  // the description in OpenMP 4.5 [2.10.4, target Construct] and
1541  // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1542  //
1543  // =========================================================================
1544  // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
1545  // | |(tofrom:scalar)| | pvt | | | |
1546  // =========================================================================
1547  // | scl | | | | - | | bycopy|
1548  // | scl | | - | x | - | - | bycopy|
1549  // | scl | | x | - | - | - | null |
1550  // | scl | x | | | - | | byref |
1551  // | scl | x | - | x | - | - | bycopy|
1552  // | scl | x | x | - | - | - | null |
1553  // | scl | | - | - | - | x | byref |
1554  // | scl | x | - | - | - | x | byref |
1555  //
1556  // | agg | n.a. | | | - | | byref |
1557  // | agg | n.a. | - | x | - | - | byref |
1558  // | agg | n.a. | x | - | - | - | null |
1559  // | agg | n.a. | - | - | - | x | byref |
1560  // | agg | n.a. | - | - | - | x[] | byref |
1561  //
1562  // | ptr | n.a. | | | - | | bycopy|
1563  // | ptr | n.a. | - | x | - | - | bycopy|
1564  // | ptr | n.a. | x | - | - | - | null |
1565  // | ptr | n.a. | - | - | - | x | byref |
1566  // | ptr | n.a. | - | - | - | x[] | bycopy|
1567  // | ptr | n.a. | - | - | x | | bycopy|
1568  // | ptr | n.a. | - | - | x | x | bycopy|
1569  // | ptr | n.a. | - | - | x | x[] | bycopy|
1570  // =========================================================================
1571  // Legend:
1572  // scl - scalar
1573  // ptr - pointer
1574  // agg - aggregate
1575  // x - applies
1576  // - - invalid in this combination
1577  // [] - mapped with an array section
1578  // byref - should be mapped by reference
1579  // byval - should be mapped by value
1580  // null - initialize a local variable to null on the device
1581  //
1582  // Observations:
1583  // - All scalar declarations that show up in a map clause have to be passed
1584  // by reference, because they may have been mapped in the enclosing data
1585  // environment.
1586  // - If the scalar value does not fit the size of uintptr, it has to be
1587  // passed by reference, regardless the result in the table above.
1588  // - For pointers mapped by value that have either an implicit map or an
1589  // array section, the runtime library may pass the NULL value to the
1590  // device instead of the value passed to it by the compiler.
1591 
1592  if (Ty->isReferenceType())
1593  Ty = Ty->castAs<ReferenceType>()->getPointeeType();
1594 
1595  // Locate map clauses and see if the variable being captured is referred to
1596  // in any of those clauses. Here we only care about variables, not fields,
1597  // because fields are part of aggregates.
1598  bool IsVariableUsedInMapClause = false;
1599  bool IsVariableAssociatedWithSection = false;
1600 
1601  DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1602  D, Level,
1603  [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D](
1605  MapExprComponents,
1606  OpenMPClauseKind WhereFoundClauseKind) {
1607  // Only the map clause information influences how a variable is
1608  // captured. E.g. is_device_ptr does not require changing the default
1609  // behavior.
1610  if (WhereFoundClauseKind != OMPC_map)
1611  return false;
1612 
1613  auto EI = MapExprComponents.rbegin();
1614  auto EE = MapExprComponents.rend();
1615 
1616  assert(EI != EE && "Invalid map expression!");
1617 
1618  if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
1619  IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
1620 
1621  ++EI;
1622  if (EI == EE)
1623  return false;
1624 
1625  if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
1626  isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
1627  isa<MemberExpr>(EI->getAssociatedExpression())) {
1628  IsVariableAssociatedWithSection = true;
1629  // There is nothing more we need to know about this variable.
1630  return true;
1631  }
1632 
1633  // Keep looking for more map info.
1634  return false;
1635  });
1636 
1637  if (IsVariableUsedInMapClause) {
1638  // If variable is identified in a map clause it is always captured by
1639  // reference except if it is a pointer that is dereferenced somehow.
1640  IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
1641  } else {
1642  // By default, all the data that has a scalar type is mapped by copy
1643  // (except for reduction variables).
1644  IsByRef =
1645  (DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
1646  !Ty->isAnyPointerType()) ||
1647  !Ty->isScalarType() ||
1648  DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar ||
1649  DSAStack->hasExplicitDSA(
1650  D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level);
1651  }
1652  }
1653 
1654  if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
1655  IsByRef =
1656  ((DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
1657  !Ty->isAnyPointerType()) ||
1658  !DSAStack->hasExplicitDSA(
1659  D,
1660  [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
1661  Level, /*NotLastprivate=*/true)) &&
1662  // If the variable is artificial and must be captured by value - try to
1663  // capture by value.
1664  !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
1665  !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue());
1666  }
1667 
1668  // When passing data by copy, we need to make sure it fits the uintptr size
1669  // and alignment, because the runtime library only deals with uintptr types.
1670  // If it does not fit the uintptr size, we need to pass the data by reference
1671  // instead.
1672  if (!IsByRef &&
1673  (Ctx.getTypeSizeInChars(Ty) >
1674  Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
1675  Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
1676  IsByRef = true;
1677  }
1678 
1679  return IsByRef;
1680 }
1681 
1682 unsigned Sema::getOpenMPNestingLevel() const {
1683  assert(getLangOpts().OpenMP);
1684  return DSAStack->getNestingLevel();
1685 }
1686 
1688  return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
1689  !DSAStack->isClauseParsingMode()) ||
1690  DSAStack->hasDirective(
1692  SourceLocation) -> bool {
1693  return isOpenMPTargetExecutionDirective(K);
1694  },
1695  false);
1696 }
1697 
1699  assert(LangOpts.OpenMP && "OpenMP is not allowed");
1700  D = getCanonicalDecl(D);
1701 
1702  // If we are attempting to capture a global variable in a directive with
1703  // 'target' we return true so that this global is also mapped to the device.
1704  //
1705  auto *VD = dyn_cast<VarDecl>(D);
1706  if (VD && !VD->hasLocalStorage()) {
1707  if (isInOpenMPDeclareTargetContext() &&
1708  (getCurCapturedRegion() || getCurBlock() || getCurLambda())) {
1709  // Try to mark variable as declare target if it is used in capturing
1710  // regions.
1711  if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
1712  checkDeclIsAllowedInOpenMPTarget(nullptr, VD);
1713  return nullptr;
1714  } else if (isInOpenMPTargetExecutionDirective()) {
1715  // If the declaration is enclosed in a 'declare target' directive,
1716  // then it should not be captured.
1717  //
1718  if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
1719  return nullptr;
1720  return VD;
1721  }
1722  }
1723  // Capture variables captured by reference in lambdas for target-based
1724  // directives.
1725  if (VD && !DSAStack->isClauseParsingMode()) {
1726  if (const auto *RD = VD->getType()
1727  .getCanonicalType()
1728  .getNonReferenceType()
1729  ->getAsCXXRecordDecl()) {
1730  bool SavedForceCaptureByReferenceInTargetExecutable =
1731  DSAStack->isForceCaptureByReferenceInTargetExecutable();
1732  DSAStack->setForceCaptureByReferenceInTargetExecutable(/*V=*/true);
1733  if (RD->isLambda()) {
1734  llvm::DenseMap<const VarDecl *, FieldDecl *> Captures;
1735  FieldDecl *ThisCapture;
1736  RD->getCaptureFields(Captures, ThisCapture);
1737  for (const LambdaCapture &LC : RD->captures()) {
1738  if (LC.getCaptureKind() == LCK_ByRef) {
1739  VarDecl *VD = LC.getCapturedVar();
1740  DeclContext *VDC = VD->getDeclContext();
1741  if (!VDC->Encloses(CurContext))
1742  continue;
1743  DSAStackTy::DSAVarData DVarPrivate =
1744  DSAStack->getTopDSA(VD, /*FromParent=*/false);
1745  // Do not capture already captured variables.
1746  if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) &&
1747  DVarPrivate.CKind == OMPC_unknown &&
1748  !DSAStack->checkMappableExprComponentListsForDecl(
1749  D, /*CurrentRegionOnly=*/true,
1751  MappableExprComponentListRef,
1752  OpenMPClauseKind) { return true; }))
1753  MarkVariableReferenced(LC.getLocation(), LC.getCapturedVar());
1754  } else if (LC.getCaptureKind() == LCK_This) {
1755  QualType ThisTy = getCurrentThisType();
1756  if (!ThisTy.isNull() &&
1757  Context.typesAreCompatible(ThisTy, ThisCapture->getType()))
1758  CheckCXXThisCapture(LC.getLocation());
1759  }
1760  }
1761  }
1762  DSAStack->setForceCaptureByReferenceInTargetExecutable(
1763  SavedForceCaptureByReferenceInTargetExecutable);
1764  }
1765  }
1766 
1767  if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1768  (!DSAStack->isClauseParsingMode() ||
1769  DSAStack->getParentDirective() != OMPD_unknown)) {
1770  auto &&Info = DSAStack->isLoopControlVariable(D);
1771  if (Info.first ||
1772  (VD && VD->hasLocalStorage() &&
1773  isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) ||
1774  (VD && DSAStack->isForceVarCapturing()))
1775  return VD ? VD : Info.second;
1776  DSAStackTy::DSAVarData DVarPrivate =
1777  DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
1778  if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
1779  return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
1780  DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate,
1781  [](OpenMPDirectiveKind) { return true; },
1782  DSAStack->isClauseParsingMode());
1783  if (DVarPrivate.CKind != OMPC_unknown)
1784  return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
1785  }
1786  return nullptr;
1787 }
1788 
1789 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
1790  unsigned Level) const {
1792  getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
1793  FunctionScopesIndex -= Regions.size();
1794 }
1795 
1797  assert(LangOpts.OpenMP && "OpenMP must be enabled.");
1798  if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
1799  DSAStack->loopInit();
1800 }
1801 
1802 bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const {
1803  assert(LangOpts.OpenMP && "OpenMP is not allowed");
1804  if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
1805  if (DSAStack->getAssociatedLoops() > 0 &&
1806  !DSAStack->isLoopStarted()) {
1807  DSAStack->resetPossibleLoopCounter(D);
1808  DSAStack->loopStart();
1809  return true;
1810  }
1811  if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() ||
1812  DSAStack->isLoopControlVariable(D).first) &&
1813  !DSAStack->hasExplicitDSA(
1814  D, [](OpenMPClauseKind K) { return K != OMPC_private; }, Level) &&
1815  !isOpenMPSimdDirective(DSAStack->getCurrentDirective()))
1816  return true;
1817  }
1818  return DSAStack->hasExplicitDSA(
1819  D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) ||
1820  (DSAStack->isClauseParsingMode() &&
1821  DSAStack->getClauseParsingMode() == OMPC_private) ||
1822  // Consider taskgroup reduction descriptor variable a private to avoid
1823  // possible capture in the region.
1824  (DSAStack->hasExplicitDirective(
1825  [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; },
1826  Level) &&
1827  DSAStack->isTaskgroupReductionRef(D, Level));
1828 }
1829 
1831  unsigned Level) {
1832  assert(LangOpts.OpenMP && "OpenMP is not allowed");
1833  D = getCanonicalDecl(D);
1835  for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
1836  const unsigned NewLevel = I - 1;
1837  if (DSAStack->hasExplicitDSA(D,
1838  [&OMPC](const OpenMPClauseKind K) {
1839  if (isOpenMPPrivate(K)) {
1840  OMPC = K;
1841  return true;
1842  }
1843  return false;
1844  },
1845  NewLevel))
1846  break;
1847  if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1848  D, NewLevel,
1850  OpenMPClauseKind) { return true; })) {
1851  OMPC = OMPC_map;
1852  break;
1853  }
1854  if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1855  NewLevel)) {
1856  OMPC = OMPC_map;
1857  if (D->getType()->isScalarType() &&
1858  DSAStack->getDefaultDMAAtLevel(NewLevel) !=
1859  DefaultMapAttributes::DMA_tofrom_scalar)
1860  OMPC = OMPC_firstprivate;
1861  break;
1862  }
1863  }
1864  if (OMPC != OMPC_unknown)
1865  FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC));
1866 }
1867 
1869  unsigned Level) const {
1870  assert(LangOpts.OpenMP && "OpenMP is not allowed");
1871  // Return true if the current level is no longer enclosed in a target region.
1872 
1873  const auto *VD = dyn_cast<VarDecl>(D);
1874  return VD && !VD->hasLocalStorage() &&
1875  DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1876  Level);
1877 }
1878 
1879 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
1880 
1882  const DeclarationNameInfo &DirName,
1883  Scope *CurScope, SourceLocation Loc) {
1884  DSAStack->push(DKind, DirName, CurScope, Loc);
1885  PushExpressionEvaluationContext(
1886  ExpressionEvaluationContext::PotentiallyEvaluated);
1887 }
1888 
1890  DSAStack->setClauseParsingMode(K);
1891 }
1892 
1894  DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
1895 }
1896 
1897 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
1898  ArrayRef<OMPClause *> Clauses);
1899 
1900 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
1901  // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1902  // A variable of class type (or array thereof) that appears in a lastprivate
1903  // clause requires an accessible, unambiguous default constructor for the
1904  // class type, unless the list item is also specified in a firstprivate
1905  // clause.
1906  if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
1907  for (OMPClause *C : D->clauses()) {
1908  if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1909  SmallVector<Expr *, 8> PrivateCopies;
1910  for (Expr *DE : Clause->varlists()) {
1911  if (DE->isValueDependent() || DE->isTypeDependent()) {
1912  PrivateCopies.push_back(nullptr);
1913  continue;
1914  }
1915  auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
1916  auto *VD = cast<VarDecl>(DRE->getDecl());
1917  QualType Type = VD->getType().getNonReferenceType();
1918  const DSAStackTy::DSAVarData DVar =
1919  DSAStack->getTopDSA(VD, /*FromParent=*/false);
1920  if (DVar.CKind == OMPC_lastprivate) {
1921  // Generate helper private variable and initialize it with the
1922  // default value. The address of the original variable is replaced
1923  // by the address of the new private variable in CodeGen. This new
1924  // variable is not added to IdResolver, so the code in the OpenMP
1925  // region uses original variable for proper diagnostics.
1926  VarDecl *VDPrivate = buildVarDecl(
1927  *this, DE->getExprLoc(), Type.getUnqualifiedType(),
1928  VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE);
1929  ActOnUninitializedDecl(VDPrivate);
1930  if (VDPrivate->isInvalidDecl()) {
1931  PrivateCopies.push_back(nullptr);
1932  continue;
1933  }
1934  PrivateCopies.push_back(buildDeclRefExpr(
1935  *this, VDPrivate, DE->getType(), DE->getExprLoc()));
1936  } else {
1937  // The variable is also a firstprivate, so initialization sequence
1938  // for private copy is generated already.
1939  PrivateCopies.push_back(nullptr);
1940  }
1941  }
1942  Clause->setPrivateCopies(PrivateCopies);
1943  }
1944  }
1945  // Check allocate clauses.
1946  if (!CurContext->isDependentContext())
1947  checkAllocateClauses(*this, DSAStack, D->clauses());
1948  }
1949 
1950  DSAStack->pop();
1951  DiscardCleanupsInEvaluationContext();
1952  PopExpressionEvaluationContext();
1953 }
1954 
1955 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1956  Expr *NumIterations, Sema &SemaRef,
1957  Scope *S, DSAStackTy *Stack);
1958 
1959 namespace {
1960 
1961 class VarDeclFilterCCC final : public CorrectionCandidateCallback {
1962 private:
1963  Sema &SemaRef;
1964 
1965 public:
1966  explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
1967  bool ValidateCandidate(const TypoCorrection &Candidate) override {
1968  NamedDecl *ND = Candidate.getCorrectionDecl();
1969  if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
1970  return VD->hasGlobalStorage() &&
1971  SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1972  SemaRef.getCurScope());
1973  }
1974  return false;
1975  }
1976 
1977  std::unique_ptr<CorrectionCandidateCallback> clone() override {
1978  return llvm::make_unique<VarDeclFilterCCC>(*this);
1979  }
1980 
1981 };
1982 
1983 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
1984 private:
1985  Sema &SemaRef;
1986 
1987 public:
1988  explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1989  bool ValidateCandidate(const TypoCorrection &Candidate) override {
1990  NamedDecl *ND = Candidate.getCorrectionDecl();
1991  if (ND && ((isa<VarDecl>(ND) && ND->getKind() == Decl::Var) ||
1992  isa<FunctionDecl>(ND))) {
1993  return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1994  SemaRef.getCurScope());
1995  }
1996  return false;
1997  }
1998 
1999  std::unique_ptr<CorrectionCandidateCallback> clone() override {
2000  return llvm::make_unique<VarOrFuncDeclFilterCCC>(*this);
2001  }
2002 };
2003 
2004 } // namespace
2005 
2007  CXXScopeSpec &ScopeSpec,
2008  const DeclarationNameInfo &Id,
2010  LookupResult Lookup(*this, Id, LookupOrdinaryName);
2011  LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
2012 
2013  if (Lookup.isAmbiguous())
2014  return ExprError();
2015 
2016  VarDecl *VD;
2017  if (!Lookup.isSingleResult()) {
2018  VarDeclFilterCCC CCC(*this);
2019  if (TypoCorrection Corrected =
2020  CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC,
2021  CTK_ErrorRecovery)) {
2022  diagnoseTypo(Corrected,
2023  PDiag(Lookup.empty()
2024  ? diag::err_undeclared_var_use_suggest
2025  : diag::err_omp_expected_var_arg_suggest)
2026  << Id.getName());
2027  VD = Corrected.getCorrectionDeclAs<VarDecl>();
2028  } else {
2029  Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
2030  : diag::err_omp_expected_var_arg)
2031  << Id.getName();
2032  return ExprError();
2033  }
2034  } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
2035  Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
2036  Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
2037  return ExprError();
2038  }
2039  Lookup.suppressDiagnostics();
2040 
2041  // OpenMP [2.9.2, Syntax, C/C++]
2042  // Variables must be file-scope, namespace-scope, or static block-scope.
2043  if (Kind == OMPD_threadprivate && !VD->hasGlobalStorage()) {
2044  Diag(Id.getLoc(), diag::err_omp_global_var_arg)
2045  << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal();
2046  bool IsDecl =
2048  Diag(VD->getLocation(),
2049  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2050  << VD;
2051  return ExprError();
2052  }
2053 
2054  VarDecl *CanonicalVD = VD->getCanonicalDecl();
2055  NamedDecl *ND = CanonicalVD;
2056  // OpenMP [2.9.2, Restrictions, C/C++, p.2]
2057  // A threadprivate directive for file-scope variables must appear outside
2058  // any definition or declaration.
2059  if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
2060  !getCurLexicalContext()->isTranslationUnit()) {
2061  Diag(Id.getLoc(), diag::err_omp_var_scope)
2062  << getOpenMPDirectiveName(Kind) << VD;
2063  bool IsDecl =
2065  Diag(VD->getLocation(),
2066  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2067  << VD;
2068  return ExprError();
2069  }
2070  // OpenMP [2.9.2, Restrictions, C/C++, p.3]
2071  // A threadprivate directive for static class member variables must appear
2072  // in the class definition, in the same scope in which the member
2073  // variables are declared.
2074  if (CanonicalVD->isStaticDataMember() &&
2075  !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
2076  Diag(Id.getLoc(), diag::err_omp_var_scope)
2077  << getOpenMPDirectiveName(Kind) << VD;
2078  bool IsDecl =
2080  Diag(VD->getLocation(),
2081  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2082  << VD;
2083  return ExprError();
2084  }
2085  // OpenMP [2.9.2, Restrictions, C/C++, p.4]
2086  // A threadprivate directive for namespace-scope variables must appear
2087  // outside any definition or declaration other than the namespace
2088  // definition itself.
2089  if (CanonicalVD->getDeclContext()->isNamespace() &&
2090  (!getCurLexicalContext()->isFileContext() ||
2091  !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
2092  Diag(Id.getLoc(), diag::err_omp_var_scope)
2093  << getOpenMPDirectiveName(Kind) << VD;
2094  bool IsDecl =
2096  Diag(VD->getLocation(),
2097  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2098  << VD;
2099  return ExprError();
2100  }
2101  // OpenMP [2.9.2, Restrictions, C/C++, p.6]
2102  // A threadprivate directive for static block-scope variables must appear
2103  // in the scope of the variable and not in a nested scope.
2104  if (CanonicalVD->isLocalVarDecl() && CurScope &&
2105  !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
2106  Diag(Id.getLoc(), diag::err_omp_var_scope)
2107  << getOpenMPDirectiveName(Kind) << VD;
2108  bool IsDecl =
2110  Diag(VD->getLocation(),
2111  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2112  << VD;
2113  return ExprError();
2114  }
2115 
2116  // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
2117  // A threadprivate directive must lexically precede all references to any
2118  // of the variables in its list.
2119  if (Kind == OMPD_threadprivate && VD->isUsed() &&
2120  !DSAStack->isThreadPrivate(VD)) {
2121  Diag(Id.getLoc(), diag::err_omp_var_used)
2122  << getOpenMPDirectiveName(Kind) << VD;
2123  return ExprError();
2124  }
2125 
2126  QualType ExprType = VD->getType().getNonReferenceType();
2127  return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
2128  SourceLocation(), VD,
2129  /*RefersToEnclosingVariableOrCapture=*/false,
2130  Id.getLoc(), ExprType, VK_LValue);
2131 }
2132 
2135  ArrayRef<Expr *> VarList) {
2136  if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
2137  CurContext->addDecl(D);
2138  return DeclGroupPtrTy::make(DeclGroupRef(D));
2139  }
2140  return nullptr;
2141 }
2142 
2143 namespace {
2144 class LocalVarRefChecker final
2145  : public ConstStmtVisitor<LocalVarRefChecker, bool> {
2146  Sema &SemaRef;
2147 
2148 public:
2149  bool VisitDeclRefExpr(const DeclRefExpr *E) {
2150  if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
2151  if (VD->hasLocalStorage()) {
2152  SemaRef.Diag(E->getBeginLoc(),
2153  diag::err_omp_local_var_in_threadprivate_init)
2154  << E->getSourceRange();
2155  SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
2156  << VD << VD->getSourceRange();
2157  return true;
2158  }
2159  }
2160  return false;
2161  }
2162  bool VisitStmt(const Stmt *S) {
2163  for (const Stmt *Child : S->children()) {
2164  if (Child && Visit(Child))
2165  return true;
2166  }
2167  return false;
2168  }
2169  explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
2170 };
2171 } // namespace
2172 
2176  for (Expr *RefExpr : VarList) {
2177  auto *DE = cast<DeclRefExpr>(RefExpr);
2178  auto *VD = cast<VarDecl>(DE->getDecl());
2179  SourceLocation ILoc = DE->getExprLoc();
2180 
2181  // Mark variable as used.
2182  VD->setReferenced();
2183  VD->markUsed(Context);
2184 
2185  QualType QType = VD->getType();
2186  if (QType->isDependentType() || QType->isInstantiationDependentType()) {
2187  // It will be analyzed later.
2188  Vars.push_back(DE);
2189  continue;
2190  }
2191 
2192  // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2193  // A threadprivate variable must not have an incomplete type.
2194  if (RequireCompleteType(ILoc, VD->getType(),
2195  diag::err_omp_threadprivate_incomplete_type)) {
2196  continue;
2197  }
2198 
2199  // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2200  // A threadprivate variable must not have a reference type.
2201  if (VD->getType()->isReferenceType()) {
2202  Diag(ILoc, diag::err_omp_ref_type_arg)
2203  << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
2204  bool IsDecl =
2205  VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2206  Diag(VD->getLocation(),
2207  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2208  << VD;
2209  continue;
2210  }
2211 
2212  // Check if this is a TLS variable. If TLS is not being supported, produce
2213  // the corresponding diagnostic.
2214  if ((VD->getTLSKind() != VarDecl::TLS_None &&
2215  !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
2216  getLangOpts().OpenMPUseTLS &&
2217  getASTContext().getTargetInfo().isTLSSupported())) ||
2218  (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
2219  !VD->isLocalVarDecl())) {
2220  Diag(ILoc, diag::err_omp_var_thread_local)
2221  << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
2222  bool IsDecl =
2223  VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2224  Diag(VD->getLocation(),
2225  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2226  << VD;
2227  continue;
2228  }
2229 
2230  // Check if initial value of threadprivate variable reference variable with
2231  // local storage (it is not supported by runtime).
2232  if (const Expr *Init = VD->getAnyInitializer()) {
2233  LocalVarRefChecker Checker(*this);
2234  if (Checker.Visit(Init))
2235  continue;
2236  }
2237 
2238  Vars.push_back(RefExpr);
2239  DSAStack->addDSA(VD, DE, OMPC_threadprivate);
2240  VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
2241  Context, SourceRange(Loc, Loc)));
2242  if (ASTMutationListener *ML = Context.getASTMutationListener())
2243  ML->DeclarationMarkedOpenMPThreadPrivate(VD);
2244  }
2245  OMPThreadPrivateDecl *D = nullptr;
2246  if (!Vars.empty()) {
2247  D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
2248  Vars);
2249  D->setAccess(AS_public);
2250  }
2251  return D;
2252 }
2253 
2254 static OMPAllocateDeclAttr::AllocatorTypeTy
2255 getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) {
2256  if (!Allocator)
2257  return OMPAllocateDeclAttr::OMPDefaultMemAlloc;
2258  if (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
2259  Allocator->isInstantiationDependent() ||
2260  Allocator->containsUnexpandedParameterPack())
2261  return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
2262  auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
2263  const Expr *AE = Allocator->IgnoreParenImpCasts();
2264  for (int I = OMPAllocateDeclAttr::OMPDefaultMemAlloc;
2265  I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
2266  auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
2267  const Expr *DefAllocator = Stack->getAllocator(AllocatorKind);
2268  llvm::FoldingSetNodeID AEId, DAEId;
2269  AE->Profile(AEId, S.getASTContext(), /*Canonical=*/true);
2270  DefAllocator->Profile(DAEId, S.getASTContext(), /*Canonical=*/true);
2271  if (AEId == DAEId) {
2272  AllocatorKindRes = AllocatorKind;
2273  break;
2274  }
2275  }
2276  return AllocatorKindRes;
2277 }
2278 
2280  Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD,
2281  OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) {
2282  if (!VD->hasAttr<OMPAllocateDeclAttr>())
2283  return false;
2284  const auto *A = VD->getAttr<OMPAllocateDeclAttr>();
2285  Expr *PrevAllocator = A->getAllocator();
2286  OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind =
2287  getAllocatorKind(S, Stack, PrevAllocator);
2288  bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind;
2289  if (AllocatorsMatch &&
2290  AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc &&
2291  Allocator && PrevAllocator) {
2292  const Expr *AE = Allocator->IgnoreParenImpCasts();
2293  const Expr *PAE = PrevAllocator->IgnoreParenImpCasts();
2294  llvm::FoldingSetNodeID AEId, PAEId;
2295  AE->Profile(AEId, S.Context, /*Canonical=*/true);
2296  PAE->Profile(PAEId, S.Context, /*Canonical=*/true);
2297  AllocatorsMatch = AEId == PAEId;
2298  }
2299  if (!AllocatorsMatch) {
2300  SmallString<256> AllocatorBuffer;
2301  llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer);
2302  if (Allocator)
2303  Allocator->printPretty(AllocatorStream, nullptr, S.getPrintingPolicy());
2304  SmallString<256> PrevAllocatorBuffer;
2305  llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer);
2306  if (PrevAllocator)
2307  PrevAllocator->printPretty(PrevAllocatorStream, nullptr,
2308  S.getPrintingPolicy());
2309 
2310  SourceLocation AllocatorLoc =
2311  Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc();
2312  SourceRange AllocatorRange =
2313  Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange();
2314  SourceLocation PrevAllocatorLoc =
2315  PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation();
2316  SourceRange PrevAllocatorRange =
2317  PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange();
2318  S.Diag(AllocatorLoc, diag::warn_omp_used_different_allocator)
2319  << (Allocator ? 1 : 0) << AllocatorStream.str()
2320  << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str()
2321  << AllocatorRange;
2322  S.Diag(PrevAllocatorLoc, diag::note_omp_previous_allocator)
2323  << PrevAllocatorRange;
2324  return true;
2325  }
2326  return false;
2327 }
2328 
2329 static void
2331  OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
2332  Expr *Allocator, SourceRange SR) {
2333  if (VD->hasAttr<OMPAllocateDeclAttr>())
2334  return;
2335  if (Allocator &&
2336  (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
2337  Allocator->isInstantiationDependent() ||
2338  Allocator->containsUnexpandedParameterPack()))
2339  return;
2340  auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind,
2341  Allocator, SR);
2342  VD->addAttr(A);
2344  ML->DeclarationMarkedOpenMPAllocate(VD, A);
2345 }
2346 
2348  SourceLocation Loc, ArrayRef<Expr *> VarList,
2349  ArrayRef<OMPClause *> Clauses, DeclContext *Owner) {
2350  assert(Clauses.size() <= 1 && "Expected at most one clause.");
2351  Expr *Allocator = nullptr;
2352  if (Clauses.empty()) {
2353  // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions.
2354  // allocate directives that appear in a target region must specify an
2355  // allocator clause unless a requires directive with the dynamic_allocators
2356  // clause is present in the same compilation unit.
2357  if (LangOpts.OpenMPIsDevice &&
2358  !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
2359  targetDiag(Loc, diag::err_expected_allocator_clause);
2360  } else {
2361  Allocator = cast<OMPAllocatorClause>(Clauses.back())->getAllocator();
2362  }
2363  OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
2364  getAllocatorKind(*this, DSAStack, Allocator);
2366  for (Expr *RefExpr : VarList) {
2367  auto *DE = cast<DeclRefExpr>(RefExpr);
2368  auto *VD = cast<VarDecl>(DE->getDecl());
2369 
2370  // Check if this is a TLS variable or global register.
2371  if (VD->getTLSKind() != VarDecl::TLS_None ||
2372  VD->hasAttr<OMPThreadPrivateDeclAttr>() ||
2373  (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
2374  !VD->isLocalVarDecl()))
2375  continue;
2376 
2377  // If the used several times in the allocate directive, the same allocator
2378  // must be used.
2379  if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD,
2380  AllocatorKind, Allocator))
2381  continue;
2382 
2383  // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++
2384  // If a list item has a static storage type, the allocator expression in the
2385  // allocator clause must be a constant expression that evaluates to one of
2386  // the predefined memory allocator values.
2387  if (Allocator && VD->hasGlobalStorage()) {
2388  if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) {
2389  Diag(Allocator->getExprLoc(),
2390  diag::err_omp_expected_predefined_allocator)
2391  << Allocator->getSourceRange();
2392  bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2394  Diag(VD->getLocation(),
2395  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2396  << VD;
2397  continue;
2398  }
2399  }
2400 
2401  Vars.push_back(RefExpr);
2402  applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator,
2403  DE->getSourceRange());
2404  }
2405  if (Vars.empty())
2406  return nullptr;
2407  if (!Owner)
2408  Owner = getCurLexicalContext();
2409  auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses);
2410  D->setAccess(AS_public);
2411  Owner->addDecl(D);
2412  return DeclGroupPtrTy::make(DeclGroupRef(D));
2413 }
2414 
2417  ArrayRef<OMPClause *> ClauseList) {
2418  OMPRequiresDecl *D = nullptr;
2419  if (!CurContext->isFileContext()) {
2420  Diag(Loc, diag::err_omp_invalid_scope) << "requires";
2421  } else {
2422  D = CheckOMPRequiresDecl(Loc, ClauseList);
2423  if (D) {
2424  CurContext->addDecl(D);
2425  DSAStack->addRequiresDecl(D);
2426  }
2427  }
2428  return DeclGroupPtrTy::make(DeclGroupRef(D));
2429 }
2430 
2432  ArrayRef<OMPClause *> ClauseList) {
2433  /// For target specific clauses, the requires directive cannot be
2434  /// specified after the handling of any of the target regions in the
2435  /// current compilation unit.
2436  ArrayRef<SourceLocation> TargetLocations =
2437  DSAStack->getEncounteredTargetLocs();
2438  if (!TargetLocations.empty()) {
2439  for (const OMPClause *CNew : ClauseList) {
2440  // Check if any of the requires clauses affect target regions.
2441  if (isa<OMPUnifiedSharedMemoryClause>(CNew) ||
2442  isa<OMPUnifiedAddressClause>(CNew) ||
2443  isa<OMPReverseOffloadClause>(CNew) ||
2444  isa<OMPDynamicAllocatorsClause>(CNew)) {
2445  Diag(Loc, diag::err_omp_target_before_requires)
2446  << getOpenMPClauseName(CNew->getClauseKind());
2447  for (SourceLocation TargetLoc : TargetLocations) {
2448  Diag(TargetLoc, diag::note_omp_requires_encountered_target);
2449  }
2450  }
2451  }
2452  }
2453 
2454  if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
2455  return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc,
2456  ClauseList);
2457  return nullptr;
2458 }
2459 
2460 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
2461  const ValueDecl *D,
2462  const DSAStackTy::DSAVarData &DVar,
2463  bool IsLoopIterVar = false) {
2464  if (DVar.RefExpr) {
2465  SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2466  << getOpenMPClauseName(DVar.CKind);
2467  return;
2468  }
2469  enum {
2470  PDSA_StaticMemberShared,
2471  PDSA_StaticLocalVarShared,
2472  PDSA_LoopIterVarPrivate,
2473  PDSA_LoopIterVarLinear,
2474  PDSA_LoopIterVarLastprivate,
2475  PDSA_ConstVarShared,
2476  PDSA_GlobalVarShared,
2477  PDSA_TaskVarFirstprivate,
2478  PDSA_LocalVarPrivate,
2479  PDSA_Implicit
2480  } Reason = PDSA_Implicit;
2481  bool ReportHint = false;
2482  auto ReportLoc = D->getLocation();
2483  auto *VD = dyn_cast<VarDecl>(D);
2484  if (IsLoopIterVar) {
2485  if (DVar.CKind == OMPC_private)
2486  Reason = PDSA_LoopIterVarPrivate;
2487  else if (DVar.CKind == OMPC_lastprivate)
2488  Reason = PDSA_LoopIterVarLastprivate;
2489  else
2490  Reason = PDSA_LoopIterVarLinear;
2491  } else if (isOpenMPTaskingDirective(DVar.DKind) &&
2492  DVar.CKind == OMPC_firstprivate) {
2493  Reason = PDSA_TaskVarFirstprivate;
2494  ReportLoc = DVar.ImplicitDSALoc;
2495  } else if (VD && VD->isStaticLocal())
2496  Reason = PDSA_StaticLocalVarShared;
2497  else if (VD && VD->isStaticDataMember())
2498  Reason = PDSA_StaticMemberShared;
2499  else if (VD && VD->isFileVarDecl())
2500  Reason = PDSA_GlobalVarShared;
2501  else if (D->getType().isConstant(SemaRef.getASTContext()))
2502  Reason = PDSA_ConstVarShared;
2503  else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
2504  ReportHint = true;
2505  Reason = PDSA_LocalVarPrivate;
2506  }
2507  if (Reason != PDSA_Implicit) {
2508  SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
2509  << Reason << ReportHint
2510  << getOpenMPDirectiveName(Stack->getCurrentDirective());
2511  } else if (DVar.ImplicitDSALoc.isValid()) {
2512  SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
2513  << getOpenMPClauseName(DVar.CKind);
2514  }
2515 }
2516 
2517 namespace {
2518 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
2519  DSAStackTy *Stack;
2520  Sema &SemaRef;
2521  bool ErrorFound = false;
2522  CapturedStmt *CS = nullptr;
2523  llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
2524  llvm::SmallVector<Expr *, 4> ImplicitMap;
2525  Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
2526  llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
2527 
2528  void VisitSubCaptures(OMPExecutableDirective *S) {
2529  // Check implicitly captured variables.
2530  if (!S->hasAssociatedStmt() || !S->getAssociatedStmt())
2531  return;
2532  for (const CapturedStmt::Capture &Cap :
2534  if (!Cap.capturesVariable())
2535  continue;
2536  VarDecl *VD = Cap.getCapturedVar();
2537  // Do not try to map the variable if it or its sub-component was mapped
2538  // already.
2539  if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
2540  Stack->checkMappableExprComponentListsForDecl(
2541  VD, /*CurrentRegionOnly=*/true,
2543  OpenMPClauseKind) { return true; }))
2544  continue;
2546  SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context),
2547  Cap.getLocation(), /*RefersToCapture=*/true);
2548  Visit(DRE);
2549  }
2550  }
2551 
2552 public:
2553  void VisitDeclRefExpr(DeclRefExpr *E) {
2554  if (E->isTypeDependent() || E->isValueDependent() ||
2556  return;
2557  if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
2558  VD = VD->getCanonicalDecl();
2559  // Skip internally declared variables.
2560  if (VD->hasLocalStorage() && !CS->capturesVariable(VD))
2561  return;
2562 
2563  DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
2564  // Check if the variable has explicit DSA set and stop analysis if it so.
2565  if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
2566  return;
2567 
2568  // Skip internally declared static variables.
2570  OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
2571  if (VD->hasGlobalStorage() && !CS->capturesVariable(VD) &&
2572  (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link))
2573  return;
2574 
2575  SourceLocation ELoc = E->getExprLoc();
2576  OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
2577  // The default(none) clause requires that each variable that is referenced
2578  // in the construct, and does not have a predetermined data-sharing
2579  // attribute, must have its data-sharing attribute explicitly determined
2580  // by being listed in a data-sharing attribute clause.
2581  if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
2582  isImplicitOrExplicitTaskingRegion(DKind) &&
2583  VarsWithInheritedDSA.count(VD) == 0) {
2584  VarsWithInheritedDSA[VD] = E;
2585  return;
2586  }
2587 
2588  if (isOpenMPTargetExecutionDirective(DKind) &&
2589  !Stack->isLoopControlVariable(VD).first) {
2590  if (!Stack->checkMappableExprComponentListsForDecl(
2591  VD, /*CurrentRegionOnly=*/true,
2593  StackComponents,
2594  OpenMPClauseKind) {
2595  // Variable is used if it has been marked as an array, array
2596  // section or the variable iself.
2597  return StackComponents.size() == 1 ||
2598  std::all_of(
2599  std::next(StackComponents.rbegin()),
2600  StackComponents.rend(),
2601  [](const OMPClauseMappableExprCommon::
2602  MappableComponent &MC) {
2603  return MC.getAssociatedDeclaration() ==
2604  nullptr &&
2605  (isa<OMPArraySectionExpr>(
2606  MC.getAssociatedExpression()) ||
2607  isa<ArraySubscriptExpr>(
2608  MC.getAssociatedExpression()));
2609  });
2610  })) {
2611  bool IsFirstprivate = false;
2612  // By default lambdas are captured as firstprivates.
2613  if (const auto *RD =
2614  VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
2615  IsFirstprivate = RD->isLambda();
2616  IsFirstprivate =
2617  IsFirstprivate ||
2618  (VD->getType().getNonReferenceType()->isScalarType() &&
2619  Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res);
2620  if (IsFirstprivate)
2621  ImplicitFirstprivate.emplace_back(E);
2622  else
2623  ImplicitMap.emplace_back(E);
2624  return;
2625  }
2626  }
2627 
2628  // OpenMP [2.9.3.6, Restrictions, p.2]
2629  // A list item that appears in a reduction clause of the innermost
2630  // enclosing worksharing or parallel construct may not be accessed in an
2631  // explicit task.
2632  DVar = Stack->hasInnermostDSA(
2633  VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2634  [](OpenMPDirectiveKind K) {
2635  return isOpenMPParallelDirective(K) ||
2637  },
2638  /*FromParent=*/true);
2639  if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2640  ErrorFound = true;
2641  SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
2642  reportOriginalDsa(SemaRef, Stack, VD, DVar);
2643  return;
2644  }
2645 
2646  // Define implicit data-sharing attributes for task.
2647  DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
2648  if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
2649  !Stack->isLoopControlVariable(VD).first) {
2650  ImplicitFirstprivate.push_back(E);
2651  return;
2652  }
2653 
2654  // Store implicitly used globals with declare target link for parent
2655  // target.
2656  if (!isOpenMPTargetExecutionDirective(DKind) && Res &&
2657  *Res == OMPDeclareTargetDeclAttr::MT_Link) {
2658  Stack->addToParentTargetRegionLinkGlobals(E);
2659  return;
2660  }
2661  }
2662  }
2663  void VisitMemberExpr(MemberExpr *E) {
2664  if (E->isTypeDependent() || E->isValueDependent() ||
2666  return;
2667  auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
2668  OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
2669  if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParens())) {
2670  if (!FD)
2671  return;
2672  DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
2673  // Check if the variable has explicit DSA set and stop analysis if it
2674  // so.
2675  if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
2676  return;
2677 
2678  if (isOpenMPTargetExecutionDirective(DKind) &&
2679  !Stack->isLoopControlVariable(FD).first &&
2680  !Stack->checkMappableExprComponentListsForDecl(
2681  FD, /*CurrentRegionOnly=*/true,
2683  StackComponents,
2684  OpenMPClauseKind) {
2685  return isa<CXXThisExpr>(
2686  cast<MemberExpr>(
2687  StackComponents.back().getAssociatedExpression())
2688  ->getBase()
2689  ->IgnoreParens());
2690  })) {
2691  // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
2692  // A bit-field cannot appear in a map clause.
2693  //
2694  if (FD->isBitField())
2695  return;
2696 
2697  // Check to see if the member expression is referencing a class that
2698  // has already been explicitly mapped
2699  if (Stack->isClassPreviouslyMapped(TE->getType()))
2700  return;
2701 
2702  ImplicitMap.emplace_back(E);
2703  return;
2704  }
2705 
2706  SourceLocation ELoc = E->getExprLoc();
2707  // OpenMP [2.9.3.6, Restrictions, p.2]
2708  // A list item that appears in a reduction clause of the innermost
2709  // enclosing worksharing or parallel construct may not be accessed in
2710  // an explicit task.
2711  DVar = Stack->hasInnermostDSA(
2712  FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2713  [](OpenMPDirectiveKind K) {
2714  return isOpenMPParallelDirective(K) ||
2716  },
2717  /*FromParent=*/true);
2718  if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2719  ErrorFound = true;
2720  SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
2721  reportOriginalDsa(SemaRef, Stack, FD, DVar);
2722  return;
2723  }
2724 
2725  // Define implicit data-sharing attributes for task.
2726  DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
2727  if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
2728  !Stack->isLoopControlVariable(FD).first) {
2729  // Check if there is a captured expression for the current field in the
2730  // region. Do not mark it as firstprivate unless there is no captured
2731  // expression.
2732  // TODO: try to make it firstprivate.
2733  if (DVar.CKind != OMPC_unknown)
2734  ImplicitFirstprivate.push_back(E);
2735  }
2736  return;
2737  }
2738  if (isOpenMPTargetExecutionDirective(DKind)) {
2740  if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
2741  /*NoDiagnose=*/true))
2742  return;
2743  const auto *VD = cast<ValueDecl>(
2744  CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
2745  if (!Stack->checkMappableExprComponentListsForDecl(
2746  VD, /*CurrentRegionOnly=*/true,
2747  [&CurComponents](
2749  StackComponents,
2750  OpenMPClauseKind) {
2751  auto CCI = CurComponents.rbegin();
2752  auto CCE = CurComponents.rend();
2753  for (const auto &SC : llvm::reverse(StackComponents)) {
2754  // Do both expressions have the same kind?
2755  if (CCI->getAssociatedExpression()->getStmtClass() !=
2756  SC.getAssociatedExpression()->getStmtClass())
2757  if (!(isa<OMPArraySectionExpr>(
2758  SC.getAssociatedExpression()) &&
2759  isa<ArraySubscriptExpr>(
2760  CCI->getAssociatedExpression())))
2761  return false;
2762 
2763  const Decl *CCD = CCI->getAssociatedDeclaration();
2764  const Decl *SCD = SC.getAssociatedDeclaration();
2765  CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
2766  SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
2767  if (SCD != CCD)
2768  return false;
2769  std::advance(CCI, 1);
2770  if (CCI == CCE)
2771  break;
2772  }
2773  return true;
2774  })) {
2775  Visit(E->getBase());
2776  }
2777  } else {
2778  Visit(E->getBase());
2779  }
2780  }
2781  void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
2782  for (OMPClause *C : S->clauses()) {
2783  // Skip analysis of arguments of implicitly defined firstprivate clause
2784  // for task|target directives.
2785  // Skip analysis of arguments of implicitly defined map clause for target
2786  // directives.
2787  if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
2788  C->isImplicit())) {
2789  for (Stmt *CC : C->children()) {
2790  if (CC)
2791  Visit(CC);
2792  }
2793  }
2794  }
2795  // Check implicitly captured variables.
2796  VisitSubCaptures(S);
2797  }
2798  void VisitStmt(Stmt *S) {
2799  for (Stmt *C : S->children()) {
2800  if (C) {
2801  // Check implicitly captured variables in the task-based directives to
2802  // check if they must be firstprivatized.
2803  Visit(C);
2804  }
2805  }
2806  }
2807 
2808  bool isErrorFound() const { return ErrorFound; }
2809  ArrayRef<Expr *> getImplicitFirstprivate() const {
2810  return ImplicitFirstprivate;
2811  }
2812  ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; }
2813  const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
2814  return VarsWithInheritedDSA;
2815  }
2816 
2817  DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
2818  : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {
2819  // Process declare target link variables for the target directives.
2820  if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) {
2821  for (DeclRefExpr *E : Stack->getLinkGlobals())
2822  Visit(E);
2823  }
2824  }
2825 };
2826 } // namespace
2827 
2829  switch (DKind) {
2830  case OMPD_parallel:
2831  case OMPD_parallel_for:
2832  case OMPD_parallel_for_simd:
2833  case OMPD_parallel_sections:
2834  case OMPD_teams:
2835  case OMPD_teams_distribute:
2836  case OMPD_teams_distribute_simd: {
2837  QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2838  QualType KmpInt32PtrTy =
2839  Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2840  Sema::CapturedParamNameType Params[] = {
2841  std::make_pair(".global_tid.", KmpInt32PtrTy),
2842  std::make_pair(".bound_tid.", KmpInt32PtrTy),
2843  std::make_pair(StringRef(), QualType()) // __context with shared vars
2844  };
2845  ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2846  Params);
2847  break;
2848  }
2849  case OMPD_target_teams:
2850  case OMPD_target_parallel:
2851  case OMPD_target_parallel_for:
2852  case OMPD_target_parallel_for_simd:
2853  case OMPD_target_teams_distribute:
2854  case OMPD_target_teams_distribute_simd: {
2855  QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2856  QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2857  QualType KmpInt32PtrTy =
2858  Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2859  QualType Args[] = {VoidPtrTy};
2861  EPI.Variadic = true;
2862  QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2863  Sema::CapturedParamNameType Params[] = {
2864  std::make_pair(".global_tid.", KmpInt32Ty),
2865  std::make_pair(".part_id.", KmpInt32PtrTy),
2866  std::make_pair(".privates.", VoidPtrTy),
2867  std::make_pair(
2868  ".copy_fn.",
2869  Context.getPointerType(CopyFnType).withConst().withRestrict()),
2870  std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2871  std::make_pair(StringRef(), QualType()) // __context with shared vars
2872  };
2873  ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2874  Params);
2875  // Mark this captured region as inlined, because we don't use outlined
2876  // function directly.
2877  getCurCapturedRegion()->TheCapturedDecl->addAttr(
2878  AlwaysInlineAttr::CreateImplicit(
2879  Context, AlwaysInlineAttr::Keyword_forceinline));
2880  Sema::CapturedParamNameType ParamsTarget[] = {
2881  std::make_pair(StringRef(), QualType()) // __context with shared vars
2882  };
2883  // Start a captured region for 'target' with no implicit parameters.
2884  ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2885  ParamsTarget);
2886  Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
2887  std::make_pair(".global_tid.", KmpInt32PtrTy),
2888  std::make_pair(".bound_tid.", KmpInt32PtrTy),
2889  std::make_pair(StringRef(), QualType()) // __context with shared vars
2890  };
2891  // Start a captured region for 'teams' or 'parallel'. Both regions have
2892  // the same implicit parameters.
2893  ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2894  ParamsTeamsOrParallel);
2895  break;
2896  }
2897  case OMPD_target:
2898  case OMPD_target_simd: {
2899  QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2900  QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2901  QualType KmpInt32PtrTy =
2902  Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2903  QualType Args[] = {VoidPtrTy};
2905  EPI.Variadic = true;
2906  QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2907  Sema::CapturedParamNameType Params[] = {
2908  std::make_pair(".global_tid.", KmpInt32Ty),
2909  std::make_pair(".part_id.", KmpInt32PtrTy),
2910  std::make_pair(".privates.", VoidPtrTy),
2911  std::make_pair(
2912  ".copy_fn.",
2913  Context.getPointerType(CopyFnType).withConst().withRestrict()),
2914  std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2915  std::make_pair(StringRef(), QualType()) // __context with shared vars
2916  };
2917  ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2918  Params);
2919  // Mark this captured region as inlined, because we don't use outlined
2920  // function directly.
2921  getCurCapturedRegion()->TheCapturedDecl->addAttr(
2922  AlwaysInlineAttr::CreateImplicit(
2923  Context, AlwaysInlineAttr::Keyword_forceinline));
2924  ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2925  std::make_pair(StringRef(), QualType()));
2926  break;
2927  }
2928  case OMPD_simd:
2929  case OMPD_for:
2930  case OMPD_for_simd:
2931  case OMPD_sections:
2932  case OMPD_section:
2933  case OMPD_single:
2934  case OMPD_master:
2935  case OMPD_critical:
2936  case OMPD_taskgroup:
2937  case OMPD_distribute:
2938  case OMPD_distribute_simd:
2939  case OMPD_ordered:
2940  case OMPD_atomic:
2941  case OMPD_target_data: {
2942  Sema::CapturedParamNameType Params[] = {
2943  std::make_pair(StringRef(), QualType()) // __context with shared vars
2944  };
2945  ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2946  Params);
2947  break;
2948  }
2949  case OMPD_task: {
2950  QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2951  QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2952  QualType KmpInt32PtrTy =
2953  Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2954  QualType Args[] = {VoidPtrTy};
2956  EPI.Variadic = true;
2957  QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2958  Sema::CapturedParamNameType Params[] = {
2959  std::make_pair(".global_tid.", KmpInt32Ty),
2960  std::make_pair(".part_id.", KmpInt32PtrTy),
2961  std::make_pair(".privates.", VoidPtrTy),
2962  std::make_pair(
2963  ".copy_fn.",
2964  Context.getPointerType(CopyFnType).withConst().withRestrict()),
2965  std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2966  std::make_pair(StringRef(), QualType()) // __context with shared vars
2967  };
2968  ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2969  Params);
2970  // Mark this captured region as inlined, because we don't use outlined
2971  // function directly.
2972  getCurCapturedRegion()->TheCapturedDecl->addAttr(
2973  AlwaysInlineAttr::CreateImplicit(
2974  Context, AlwaysInlineAttr::Keyword_forceinline));
2975  break;
2976  }
2977  case OMPD_taskloop:
2978  case OMPD_taskloop_simd: {
2979  QualType KmpInt32Ty =
2980  Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
2981  .withConst();
2982  QualType KmpUInt64Ty =
2983  Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
2984  .withConst();
2985  QualType KmpInt64Ty =
2986  Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
2987  .withConst();
2988  QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2989  QualType KmpInt32PtrTy =
2990  Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2991  QualType Args[] = {VoidPtrTy};
2993  EPI.Variadic = true;
2994  QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2995  Sema::CapturedParamNameType Params[] = {
2996  std::make_pair(".global_tid.", KmpInt32Ty),
2997  std::make_pair(".part_id.", KmpInt32PtrTy),
2998  std::make_pair(".privates.", VoidPtrTy),
2999  std::make_pair(
3000  ".copy_fn.",
3001  Context.getPointerType(CopyFnType).withConst().withRestrict()),
3002  std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3003  std::make_pair(".lb.", KmpUInt64Ty),
3004  std::make_pair(".ub.", KmpUInt64Ty),
3005  std::make_pair(".st.", KmpInt64Ty),
3006  std::make_pair(".liter.", KmpInt32Ty),
3007  std::make_pair(".reductions.", VoidPtrTy),
3008  std::make_pair(StringRef(), QualType()) // __context with shared vars
3009  };
3010  ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3011  Params);
3012  // Mark this captured region as inlined, because we don't use outlined
3013  // function directly.
3014  getCurCapturedRegion()->TheCapturedDecl->addAttr(
3015  AlwaysInlineAttr::CreateImplicit(
3016  Context, AlwaysInlineAttr::Keyword_forceinline));
3017  break;
3018  }
3019  case OMPD_distribute_parallel_for_simd:
3020  case OMPD_distribute_parallel_for: {
3021  QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3022  QualType KmpInt32PtrTy =
3023  Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3024  Sema::CapturedParamNameType Params[] = {
3025  std::make_pair(".global_tid.", KmpInt32PtrTy),
3026  std::make_pair(".bound_tid.", KmpInt32PtrTy),
3027  std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
3028  std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
3029  std::make_pair(StringRef(), QualType()) // __context with shared vars
3030  };
3031  ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3032  Params);
3033  break;
3034  }
3035  case OMPD_target_teams_distribute_parallel_for:
3036  case OMPD_target_teams_distribute_parallel_for_simd: {
3037  QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3038  QualType KmpInt32PtrTy =
3039  Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3040  QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3041 
3042  QualType Args[] = {VoidPtrTy};
3044  EPI.Variadic = true;
3045  QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3046  Sema::CapturedParamNameType Params[] = {
3047  std::make_pair(".global_tid.", KmpInt32Ty),
3048  std::make_pair(".part_id.", KmpInt32PtrTy),
3049  std::make_pair(".privates.", VoidPtrTy),
3050  std::make_pair(
3051  ".copy_fn.",
3052  Context.getPointerType(CopyFnType).withConst().withRestrict()),
3053  std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3054  std::make_pair(StringRef(), QualType()) // __context with shared vars
3055  };
3056  ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3057  Params);
3058  // Mark this captured region as inlined, because we don't use outlined
3059  // function directly.
3060  getCurCapturedRegion()->TheCapturedDecl->addAttr(
3061  AlwaysInlineAttr::CreateImplicit(
3062  Context, AlwaysInlineAttr::Keyword_forceinline));
3063  Sema::CapturedParamNameType ParamsTarget[] = {
3064  std::make_pair(StringRef(), QualType()) // __context with shared vars
3065  };
3066  // Start a captured region for 'target' with no implicit parameters.
3067  ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3068  ParamsTarget);
3069 
3070  Sema::CapturedParamNameType ParamsTeams[] = {
3071  std::make_pair(".global_tid.", KmpInt32PtrTy),
3072  std::make_pair(".bound_tid.", KmpInt32PtrTy),
3073  std::make_pair(StringRef(), QualType()) // __context with shared vars
3074  };
3075  // Start a captured region for 'target' with no implicit parameters.
3076  ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3077  ParamsTeams);
3078 
3079  Sema::CapturedParamNameType ParamsParallel[] = {
3080  std::make_pair(".global_tid.", KmpInt32PtrTy),
3081  std::make_pair(".bound_tid.", KmpInt32PtrTy),
3082  std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
3083  std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
3084  std::make_pair(StringRef(), QualType()) // __context with shared vars
3085  };
3086  // Start a captured region for 'teams' or 'parallel'. Both regions have
3087  // the same implicit parameters.
3088  ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3089  ParamsParallel);
3090  break;
3091  }
3092 
3093  case OMPD_teams_distribute_parallel_for:
3094  case OMPD_teams_distribute_parallel_for_simd: {
3095  QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3096  QualType KmpInt32PtrTy =
3097  Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3098 
3099  Sema::CapturedParamNameType ParamsTeams[] = {
3100  std::make_pair(".global_tid.", KmpInt32PtrTy),
3101  std::make_pair(".bound_tid.", KmpInt32PtrTy),
3102  std::make_pair(StringRef(), QualType()) // __context with shared vars
3103  };
3104  // Start a captured region for 'target' with no implicit parameters.
3105  ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3106  ParamsTeams);
3107 
3108  Sema::CapturedParamNameType ParamsParallel[] = {
3109  std::make_pair(".global_tid.", KmpInt32PtrTy),
3110  std::make_pair(".bound_tid.", KmpInt32PtrTy),
3111  std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
3112  std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
3113  std::make_pair(StringRef(), QualType()) // __context with shared vars
3114  };
3115  // Start a captured region for 'teams' or 'parallel'. Both regions have
3116  // the same implicit parameters.
3117  ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3118  ParamsParallel);
3119  break;
3120  }
3121  case OMPD_target_update:
3122  case OMPD_target_enter_data:
3123  case OMPD_target_exit_data: {
3124  QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3125  QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3126  QualType KmpInt32PtrTy =
3127  Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3128  QualType Args[] = {VoidPtrTy};
3130  EPI.Variadic = true;
3131  QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3132  Sema::CapturedParamNameType Params[] = {
3133  std::make_pair(".global_tid.", KmpInt32Ty),
3134  std::make_pair(".part_id.", KmpInt32PtrTy),
3135  std::make_pair(".privates.", VoidPtrTy),
3136  std::make_pair(
3137  ".copy_fn.",
3138  Context.getPointerType(CopyFnType).withConst().withRestrict()),
3139  std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3140  std::make_pair(StringRef(), QualType()) // __context with shared vars
3141  };
3142  ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3143  Params);
3144  // Mark this captured region as inlined, because we don't use outlined
3145  // function directly.
3146  getCurCapturedRegion()->TheCapturedDecl->addAttr(
3147  AlwaysInlineAttr::CreateImplicit(
3148  Context, AlwaysInlineAttr::Keyword_forceinline));
3149  break;
3150  }
3151  case OMPD_threadprivate:
3152  case OMPD_allocate:
3153  case OMPD_taskyield:
3154  case OMPD_barrier:
3155  case OMPD_taskwait:
3156  case OMPD_cancellation_point:
3157  case OMPD_cancel:
3158  case OMPD_flush:
3159  case OMPD_declare_reduction:
3160  case OMPD_declare_mapper:
3161  case OMPD_declare_simd:
3162  case OMPD_declare_target:
3163  case OMPD_end_declare_target:
3164  case OMPD_requires:
3165  llvm_unreachable("OpenMP Directive is not allowed");
3166  case OMPD_unknown:
3167  llvm_unreachable("Unknown OpenMP directive");
3168  }
3169 }
3170 
3172  SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
3173  getOpenMPCaptureRegions(CaptureRegions, DKind);
3174  return CaptureRegions.size();
3175 }
3176 
3178  Expr *CaptureExpr, bool WithInit,
3179  bool AsExpression) {
3180  assert(CaptureExpr);
3181  ASTContext &C = S.getASTContext();
3182  Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
3183  QualType Ty = Init->getType();
3184  if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
3185  if (S.getLangOpts().CPlusPlus) {
3186  Ty = C.getLValueReferenceType(Ty);
3187  } else {
3188  Ty = C.getPointerType(Ty);
3189  ExprResult Res =
3190  S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
3191  if (!Res.isUsable())
3192  return nullptr;
3193  Init = Res.get();
3194  }
3195  WithInit = true;
3196  }
3197  auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
3198  CaptureExpr->getBeginLoc());
3199  if (!WithInit)
3200  CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
3201  S.CurContext->addHiddenDecl(CED);
3202  S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
3203  return CED;
3204 }
3205 
3206 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
3207  bool WithInit) {
3208  OMPCapturedExprDecl *CD;
3209  if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
3210  CD = cast<OMPCapturedExprDecl>(VD);
3211  else
3212  CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
3213  /*AsExpression=*/false);
3214  return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
3215  CaptureExpr->getExprLoc());
3216 }
3217 
3218 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
3219  CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
3220  if (!Ref) {
3222  S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
3223  /*WithInit=*/true, /*AsExpression=*/true);
3224  Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
3225  CaptureExpr->getExprLoc());
3226  }
3227  ExprResult Res = Ref;
3228  if (!S.getLangOpts().CPlusPlus &&
3229  CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
3230  Ref->getType()->isPointerType()) {
3231  Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
3232  if (!Res.isUsable())
3233  return ExprError();
3234  }
3235  return S.DefaultLvalueConversion(Res.get());
3236 }
3237 
3238 namespace {
3239 // OpenMP directives parsed in this section are represented as a
3240 // CapturedStatement with an associated statement. If a syntax error
3241 // is detected during the parsing of the associated statement, the
3242 // compiler must abort processing and close the CapturedStatement.
3243 //
3244 // Combined directives such as 'target parallel' have more than one
3245 // nested CapturedStatements. This RAII ensures that we unwind out
3246 // of all the nested CapturedStatements when an error is found.
3247 class CaptureRegionUnwinderRAII {
3248 private:
3249  Sema &S;
3250  bool &ErrorFound;
3252 
3253 public:
3254  CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
3255  OpenMPDirectiveKind DKind)
3256  : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
3257  ~CaptureRegionUnwinderRAII() {
3258  if (ErrorFound) {
3259  int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
3260  while (--ThisCaptureLevel >= 0)
3262  }
3263  }
3264 };
3265 } // namespace
3266 
3268  ArrayRef<OMPClause *> Clauses) {
3269  bool ErrorFound = false;
3270  CaptureRegionUnwinderRAII CaptureRegionUnwinder(
3271  *this, ErrorFound, DSAStack->getCurrentDirective());
3272  if (!S.isUsable()) {
3273  ErrorFound = true;
3274  return StmtError();
3275  }
3276 
3277  SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
3278  getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
3279  OMPOrderedClause *OC = nullptr;
3280  OMPScheduleClause *SC = nullptr;
3283  // This is required for proper codegen.
3284  for (OMPClause *Clause : Clauses) {
3285  if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
3286  Clause->getClauseKind() == OMPC_in_reduction) {
3287  // Capture taskgroup task_reduction descriptors inside the tasking regions
3288  // with the corresponding in_reduction items.
3289  auto *IRC = cast<OMPInReductionClause>(Clause);
3290  for (Expr *E : IRC->taskgroup_descriptors())
3291  if (E)
3292  MarkDeclarationsReferencedInExpr(E);
3293  }
3294  if (isOpenMPPrivate(Clause->getClauseKind()) ||
3295  Clause->getClauseKind() == OMPC_copyprivate ||
3296  (getLangOpts().OpenMPUseTLS &&
3297  getASTContext().getTargetInfo().isTLSSupported() &&
3298  Clause->getClauseKind() == OMPC_copyin)) {
3299  DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
3300  // Mark all variables in private list clauses as used in inner region.
3301  for (Stmt *VarRef : Clause->children()) {
3302  if (auto *E = cast_or_null<Expr>(VarRef)) {
3303  MarkDeclarationsReferencedInExpr(E);
3304  }
3305  }
3306  DSAStack->setForceVarCapturing(/*V=*/false);
3307  } else if (CaptureRegions.size() > 1 ||
3308  CaptureRegions.back() != OMPD_unknown) {
3309  if (auto *C = OMPClauseWithPreInit::get(Clause))
3310  PICs.push_back(C);
3311  if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
3312  if (Expr *E = C->getPostUpdateExpr())
3313  MarkDeclarationsReferencedInExpr(E);
3314  }
3315  }
3316  if (Clause->getClauseKind() == OMPC_schedule)
3317  SC = cast<OMPScheduleClause>(Clause);
3318  else if (Clause->getClauseKind() == OMPC_ordered)
3319  OC = cast<OMPOrderedClause>(Clause);
3320  else if (Clause->getClauseKind() == OMPC_linear)
3321  LCs.push_back(cast<OMPLinearClause>(Clause));
3322  }
3323  // OpenMP, 2.7.1 Loop Construct, Restrictions
3324  // The nonmonotonic modifier cannot be specified if an ordered clause is
3325  // specified.
3326  if (SC &&
3327  (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
3328  SC->getSecondScheduleModifier() ==
3329  OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
3330  OC) {
3331  Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
3334  diag::err_omp_schedule_nonmonotonic_ordered)
3335  << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
3336  ErrorFound = true;
3337  }
3338  if (!LCs.empty() && OC && OC->getNumForLoops()) {
3339  for (const OMPLinearClause *C : LCs) {
3340  Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
3341  << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
3342  }
3343  ErrorFound = true;
3344  }
3345  if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
3346  isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
3347  OC->getNumForLoops()) {
3348  Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
3349  << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
3350  ErrorFound = true;
3351  }
3352  if (ErrorFound) {
3353  return StmtError();
3354  }
3355  StmtResult SR = S;
3356  for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
3357  // Mark all variables in private list clauses as used in inner region.
3358  // Required for proper codegen of combined directives.
3359  // TODO: add processing for other clauses.
3360  if (ThisCaptureRegion != OMPD_unknown) {
3361  for (const clang::OMPClauseWithPreInit *C : PICs) {
3362  OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
3363  // Find the particular capture region for the clause if the
3364  // directive is a combined one with multiple capture regions.
3365  // If the directive is not a combined one, the capture region
3366  // associated with the clause is OMPD_unknown and is generated
3367  // only once.
3368  if (CaptureRegion == ThisCaptureRegion ||
3369  CaptureRegion == OMPD_unknown) {
3370  if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
3371  for (Decl *D : DS->decls())
3372  MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
3373  }
3374  }
3375  }
3376  }
3377  SR = ActOnCapturedRegionEnd(SR.get());
3378  }
3379  return SR;
3380 }
3381 
3382 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
3383  OpenMPDirectiveKind CancelRegion,
3384  SourceLocation StartLoc) {
3385  // CancelRegion is only needed for cancel and cancellation_point.
3386  if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
3387  return false;
3388 
3389  if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
3390  CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
3391  return false;
3392 
3393  SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
3394  << getOpenMPDirectiveName(CancelRegion);
3395  return true;
3396 }
3397 
3398 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
3399  OpenMPDirectiveKind CurrentRegion,
3400  const DeclarationNameInfo &CurrentName,
3401  OpenMPDirectiveKind CancelRegion,
3402  SourceLocation StartLoc) {
3403  if (Stack->getCurScope()) {
3404  OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
3405  OpenMPDirectiveKind OffendingRegion = ParentRegion;
3406  bool NestingProhibited = false;
3407  bool CloseNesting = true;
3408  bool OrphanSeen = false;
3409  enum {
3410  NoRecommend,
3411  ShouldBeInParallelRegion,
3412  ShouldBeInOrderedRegion,
3413  ShouldBeInTargetRegion,
3414  ShouldBeInTeamsRegion
3415  } Recommend = NoRecommend;
3416  if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
3417  // OpenMP [2.16, Nesting of Regions]
3418  // OpenMP constructs may not be nested inside a simd region.
3419  // OpenMP [2.8.1,simd Construct, Restrictions]
3420  // An ordered construct with the simd clause is the only OpenMP
3421  // construct that can appear in the simd region.
3422  // Allowing a SIMD construct nested in another SIMD construct is an
3423  // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3424  // message.
3425  SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3426  ? diag::err_omp_prohibited_region_simd
3427  : diag::warn_omp_nesting_simd);
3428  return CurrentRegion != OMPD_simd;
3429  }
3430  if (ParentRegion == OMPD_atomic) {
3431  // OpenMP [2.16, Nesting of Regions]
3432  // OpenMP constructs may not be nested inside an atomic region.
3433  SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3434  return true;
3435  }
3436  if (CurrentRegion == OMPD_section) {
3437  // OpenMP [2.7.2, sections Construct, Restrictions]
3438  // Orphaned section directives are prohibited. That is, the section
3439  // directives must appear within the sections construct and must not be
3440  // encountered elsewhere in the sections region.
3441  if (ParentRegion != OMPD_sections &&
3442  ParentRegion != OMPD_parallel_sections) {
3443  SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3444  << (ParentRegion != OMPD_unknown)
3445  << getOpenMPDirectiveName(ParentRegion);
3446  return true;
3447  }
3448  return false;
3449  }
3450  // Allow some constructs (except teams and cancellation constructs) to be
3451  // orphaned (they could be used in functions, called from OpenMP regions
3452  // with the required preconditions).
3453  if (ParentRegion == OMPD_unknown &&
3454  !isOpenMPNestingTeamsDirective(CurrentRegion) &&
3455  CurrentRegion != OMPD_cancellation_point &&
3456  CurrentRegion != OMPD_cancel)
3457  return false;
3458  if (CurrentRegion == OMPD_cancellation_point ||
3459  CurrentRegion == OMPD_cancel) {
3460  // OpenMP [2.16, Nesting of Regions]
3461  // A cancellation point construct for which construct-type-clause is
3462  // taskgroup must be nested inside a task construct. A cancellation
3463  // point construct for which construct-type-clause is not taskgroup must
3464  // be closely nested inside an OpenMP construct that matches the type
3465  // specified in construct-type-clause.
3466  // A cancel construct for which construct-type-clause is taskgroup must be
3467  // nested inside a task construct. A cancel construct for which
3468  // construct-type-clause is not taskgroup must be closely nested inside an
3469  // OpenMP construct that matches the type specified in
3470  // construct-type-clause.
3471  NestingProhibited =
3472  !((CancelRegion == OMPD_parallel &&
3473  (ParentRegion == OMPD_parallel ||
3474  ParentRegion == OMPD_target_parallel)) ||
3475  (CancelRegion == OMPD_for &&
3476  (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
3477  ParentRegion == OMPD_target_parallel_for ||
3478  ParentRegion == OMPD_distribute_parallel_for ||
3479  ParentRegion == OMPD_teams_distribute_parallel_for ||
3480  ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
3481  (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3482  (CancelRegion == OMPD_sections &&
3483  (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3484  ParentRegion == OMPD_parallel_sections)));
3485  OrphanSeen = ParentRegion == OMPD_unknown;
3486  } else if (CurrentRegion == OMPD_master) {
3487  // OpenMP [2.16, Nesting of Regions]
3488  // A master region may not be closely nested inside a worksharing,
3489  // atomic, or explicit task region.
3490  NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3491  isOpenMPTaskingDirective(ParentRegion);
3492  } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3493  // OpenMP [2.16, Nesting of Regions]
3494  // A critical region may not be nested (closely or otherwise) inside a
3495  // critical region with the same name. Note that this restriction is not
3496  // sufficient to prevent deadlock.
3497  SourceLocation PreviousCriticalLoc;
3498  bool DeadLock = Stack->hasDirective(
3499  [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
3500  const DeclarationNameInfo &DNI,
3501  SourceLocation Loc) {
3502  if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
3503  PreviousCriticalLoc = Loc;
3504  return true;
3505  }
3506  return false;
3507  },
3508  false /* skip top directive */);
3509  if (DeadLock) {
3510  SemaRef.Diag(StartLoc,
3511  diag::err_omp_prohibited_region_critical_same_name)
3512  << CurrentName.getName();
3513  if (PreviousCriticalLoc.isValid())
3514  SemaRef.Diag(PreviousCriticalLoc,
3515  diag::note_omp_previous_critical_region);
3516  return true;
3517  }
3518  } else if (CurrentRegion == OMPD_barrier) {
3519  // OpenMP [2.16, Nesting of Regions]
3520  // A barrier region may not be closely nested inside a worksharing,
3521  // explicit task, critical, ordered, atomic, or master region.
3522  NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3523  isOpenMPTaskingDirective(ParentRegion) ||
3524  ParentRegion == OMPD_master ||
3525  ParentRegion == OMPD_critical ||
3526  ParentRegion == OMPD_ordered;
3527  } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
3528  !isOpenMPParallelDirective(CurrentRegion) &&
3529  !isOpenMPTeamsDirective(CurrentRegion)) {
3530  // OpenMP [2.16, Nesting of Regions]
3531  // A worksharing region may not be closely nested inside a worksharing,
3532  // explicit task, critical, ordered, atomic, or master region.
3533  NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3534  isOpenMPTaskingDirective(ParentRegion) ||
3535  ParentRegion == OMPD_master ||
3536  ParentRegion == OMPD_critical ||
3537  ParentRegion == OMPD_ordered;
3538  Recommend = ShouldBeInParallelRegion;
3539  } else if (CurrentRegion == OMPD_ordered) {
3540  // OpenMP [2.16, Nesting of Regions]
3541  // An ordered region may not be closely nested inside a critical,
3542  // atomic, or explicit task region.
3543  // An ordered region must be closely nested inside a loop region (or
3544  // parallel loop region) with an ordered clause.
3545  // OpenMP [2.8.1,simd Construct, Restrictions]
3546  // An ordered construct with the simd clause is the only OpenMP construct
3547  // that can appear in the simd region.
3548  NestingProhibited = ParentRegion == OMPD_critical ||
3549  isOpenMPTaskingDirective(ParentRegion) ||
3550  !(isOpenMPSimdDirective(ParentRegion) ||
3551  Stack->isParentOrderedRegion());
3552  Recommend = ShouldBeInOrderedRegion;
3553  } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
3554  // OpenMP [2.16, Nesting of Regions]
3555  // If specified, a teams construct must be contained within a target
3556  // construct.
3557  NestingProhibited = ParentRegion != OMPD_target;
3558  OrphanSeen = ParentRegion == OMPD_unknown;
3559  Recommend = ShouldBeInTargetRegion;
3560  }
3561  if (!NestingProhibited &&
3562  !isOpenMPTargetExecutionDirective(CurrentRegion) &&
3563  !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
3564  (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
3565  // OpenMP [2.16, Nesting of Regions]
3566  // distribute, parallel, parallel sections, parallel workshare, and the
3567  // parallel loop and parallel loop SIMD constructs are the only OpenMP
3568  // constructs that can be closely nested in the teams region.
3569  NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3570  !isOpenMPDistributeDirective(CurrentRegion);
3571  Recommend = ShouldBeInParallelRegion;
3572  }
3573  if (!NestingProhibited &&
3574  isOpenMPNestingDistributeDirective(CurrentRegion)) {
3575  // OpenMP 4.5 [2.17 Nesting of Regions]
3576  // The region associated with the distribute construct must be strictly
3577  // nested inside a teams region
3578  NestingProhibited =
3579  (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
3580  Recommend = ShouldBeInTeamsRegion;
3581  }
3582  if (!NestingProhibited &&
3583  (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3584  isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3585  // OpenMP 4.5 [2.17 Nesting of Regions]
3586  // If a target, target update, target data, target enter data, or
3587  // target exit data construct is encountered during execution of a
3588  // target region, the behavior is unspecified.
3589  NestingProhibited = Stack->hasDirective(
3590  [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
3591  SourceLocation) {
3593  OffendingRegion = K;
3594  return true;
3595  }
3596  return false;
3597  },
3598  false /* don't skip top directive */);
3599  CloseNesting = false;
3600  }
3601  if (NestingProhibited) {
3602  if (OrphanSeen) {
3603  SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
3604  << getOpenMPDirectiveName(CurrentRegion) << Recommend;
3605  } else {
3606  SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
3607  << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3608  << Recommend << getOpenMPDirectiveName(CurrentRegion);
3609  }
3610  return true;
3611  }
3612  }
3613  return false;
3614 }
3615 
3617  ArrayRef<OMPClause *> Clauses,
3618  ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3619  bool ErrorFound = false;
3620  unsigned NamedModifiersNumber = 0;
3622  OMPD_unknown + 1);
3623  SmallVector<SourceLocation, 4> NameModifierLoc;
3624  for (const OMPClause *C : Clauses) {
3625  if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3626  // At most one if clause without a directive-name-modifier can appear on
3627  // the directive.
3628  OpenMPDirectiveKind CurNM = IC->getNameModifier();
3629  if (FoundNameModifiers[CurNM]) {
3630  S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
3631  << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3632  << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3633  ErrorFound = true;
3634  } else if (CurNM != OMPD_unknown) {
3635  NameModifierLoc.push_back(IC->getNameModifierLoc());
3636  ++NamedModifiersNumber;
3637  }
3638  FoundNameModifiers[CurNM] = IC;
3639  if (CurNM == OMPD_unknown)
3640  continue;
3641  // Check if the specified name modifier is allowed for the current
3642  // directive.
3643  // At most one if clause with the particular directive-name-modifier can
3644  // appear on the directive.
3645  bool MatchFound = false;
3646  for (auto NM : AllowedNameModifiers) {
3647  if (CurNM == NM) {
3648  MatchFound = true;
3649  break;
3650  }
3651  }
3652  if (!MatchFound) {
3653  S.Diag(IC->getNameModifierLoc(),
3654  diag::err_omp_wrong_if_directive_name_modifier)
3656  ErrorFound = true;
3657  }
3658  }
3659  }
3660  // If any if clause on the directive includes a directive-name-modifier then
3661  // all if clauses on the directive must include a directive-name-modifier.
3662  if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3663  if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3664  S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
3665  diag::err_omp_no_more_if_clause);
3666  } else {
3667  std::string Values;
3668  std::string Sep(", ");
3669  unsigned AllowedCnt = 0;
3670  unsigned TotalAllowedNum =
3671  AllowedNameModifiers.size() - NamedModifiersNumber;
3672  for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3673  ++Cnt) {
3674  OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3675  if (!FoundNameModifiers[NM]) {
3676  Values += "'";
3677  Values += getOpenMPDirectiveName(NM);
3678  Values += "'";
3679  if (AllowedCnt + 2 == TotalAllowedNum)
3680  Values += " or ";
3681  else if (AllowedCnt + 1 != TotalAllowedNum)
3682  Values += Sep;
3683  ++AllowedCnt;
3684  }
3685  }
3686  S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
3687  diag::err_omp_unnamed_if_clause)
3688  << (TotalAllowedNum > 1) << Values;
3689  }
3690  for (SourceLocation Loc : NameModifierLoc) {
3691  S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3692  }
3693  ErrorFound = true;
3694  }
3695  return ErrorFound;
3696 }
3697 
3698 static std::pair<ValueDecl *, bool>
3700  SourceRange &ERange, bool AllowArraySection = false) {
3701  if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
3703  return std::make_pair(nullptr, true);
3704 
3705  // OpenMP [3.1, C/C++]
3706  // A list item is a variable name.
3707  // OpenMP [2.9.3.3, Restrictions, p.1]
3708  // A variable that is part of another variable (as an array or
3709  // structure element) cannot appear in a private clause.
3710  RefExpr = RefExpr->IgnoreParens();
3711  enum {
3712  NoArrayExpr = -1,
3713  ArraySubscript = 0,
3714  OMPArraySection = 1
3715  } IsArrayExpr = NoArrayExpr;
3716  if (AllowArraySection) {
3717  if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
3718  Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
3719  while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
3720  Base = TempASE->getBase()->IgnoreParenImpCasts();
3721  RefExpr = Base;
3722  IsArrayExpr = ArraySubscript;
3723  } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
3724  Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
3725  while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
3726  Base = TempOASE->getBase()->IgnoreParenImpCasts();
3727  while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
3728  Base = TempASE->getBase()->IgnoreParenImpCasts();
3729  RefExpr = Base;
3730  IsArrayExpr = OMPArraySection;
3731  }
3732  }
3733  ELoc = RefExpr->getExprLoc();
3734  ERange = RefExpr->getSourceRange();
3735  RefExpr = RefExpr->IgnoreParenImpCasts();
3736  auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
3737  auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
3738  if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
3739  (S.getCurrentThisType().isNull() || !ME ||
3740  !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
3741  !isa<FieldDecl>(ME->getMemberDecl()))) {
3742  if (IsArrayExpr != NoArrayExpr) {
3743  S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
3744  << ERange;
3745  } else {
3746  S.Diag(ELoc,
3747  AllowArraySection
3748  ? diag::err_omp_expected_var_name_member_expr_or_array_item
3749  : diag::err_omp_expected_var_name_member_expr)
3750  << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
3751  }
3752  return std::make_pair(nullptr, false);
3753  }
3754  return std::make_pair(
3755  getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
3756 }
3757 
3758 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
3759  ArrayRef<OMPClause *> Clauses) {
3760  assert(!S.CurContext->isDependentContext() &&
3761  "Expected non-dependent context.");
3762  auto AllocateRange =
3763  llvm::make_filter_range(Clauses, OMPAllocateClause::classof);
3764  llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>>
3765  DeclToCopy;
3766  auto PrivateRange = llvm::make_filter_range(Clauses, [](const OMPClause *C) {
3767  return isOpenMPPrivate(C->getClauseKind());
3768  });
3769  for (OMPClause *Cl : PrivateRange) {
3771  if (Cl->getClauseKind() == OMPC_private) {
3772  auto *PC = cast<OMPPrivateClause>(Cl);
3773  I = PC->private_copies().begin();
3774  It = PC->varlist_begin();
3775  Et = PC->varlist_end();
3776  } else if (Cl->getClauseKind() == OMPC_firstprivate) {
3777  auto *PC = cast<OMPFirstprivateClause>(Cl);
3778  I = PC->private_copies().begin();
3779  It = PC->varlist_begin();
3780  Et = PC->varlist_end();
3781  } else if (Cl->getClauseKind() == OMPC_lastprivate) {
3782  auto *PC = cast<OMPLastprivateClause>(Cl);
3783  I = PC->private_copies().begin();
3784  It = PC->varlist_begin();
3785  Et = PC->varlist_end();
3786  } else if (Cl->getClauseKind() == OMPC_linear) {
3787  auto *PC = cast<OMPLinearClause>(Cl);
3788  I = PC->privates().begin();
3789  It = PC->varlist_begin();
3790  Et = PC->varlist_end();
3791  } else if (Cl->getClauseKind() == OMPC_reduction) {
3792  auto *PC = cast<OMPReductionClause>(Cl);
3793  I = PC->privates().begin();
3794  It = PC->varlist_begin();
3795  Et = PC->varlist_end();
3796  } else if (Cl->getClauseKind() == OMPC_task_reduction) {
3797  auto *PC = cast<OMPTaskReductionClause>(Cl);
3798  I = PC->privates().begin();
3799  It = PC->varlist_begin();
3800  Et = PC->varlist_end();
3801  } else if (Cl->getClauseKind() == OMPC_in_reduction) {
3802  auto *PC = cast<OMPInReductionClause>(Cl);
3803  I = PC->privates().begin();
3804  It = PC->varlist_begin();
3805  Et = PC->varlist_end();
3806  } else {
3807  llvm_unreachable("Expected private clause.");
3808  }
3809  for (Expr *E : llvm::make_range(It, Et)) {
3810  if (!*I) {
3811  ++I;
3812  continue;
3813  }
3814  SourceLocation ELoc;
3815  SourceRange ERange;
3816  Expr *SimpleRefExpr = E;
3817  auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
3818  /*AllowArraySection=*/true);
3819  DeclToCopy.try_emplace(Res.first,
3820  cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()));
3821  ++I;
3822  }
3823  }
3824  for (OMPClause *C : AllocateRange) {
3825  auto *AC = cast<OMPAllocateClause>(C);
3826  OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
3827  getAllocatorKind(S, Stack, AC->getAllocator());
3828  // OpenMP, 2.11.4 allocate Clause, Restrictions.
3829  // For task, taskloop or target directives, allocation requests to memory
3830  // allocators with the trait access set to thread result in unspecified
3831  // behavior.
3832  if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc &&
3833  (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
3834  isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()))) {
3835  S.Diag(AC->getAllocator()->getExprLoc(),
3836  diag::warn_omp_allocate_thread_on_task_target_directive)
3837  << getOpenMPDirectiveName(Stack->getCurrentDirective());
3838  }
3839  for (Expr *E : AC->varlists()) {
3840  SourceLocation ELoc;
3841  SourceRange ERange;
3842  Expr *SimpleRefExpr = E;
3843  auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange);
3844  ValueDecl *VD = Res.first;
3845  DSAStackTy::DSAVarData Data = Stack->getTopDSA(VD, /*FromParent=*/false);
3846  if (!isOpenMPPrivate(Data.CKind)) {
3847  S.Diag(E->getExprLoc(),
3848  diag::err_omp_expected_private_copy_for_allocate);
3849  continue;
3850  }
3851  VarDecl *PrivateVD = DeclToCopy[VD];
3852  if (checkPreviousOMPAllocateAttribute(S, Stack, E, PrivateVD,
3853  AllocatorKind, AC->getAllocator()))
3854  continue;
3855  applyOMPAllocateAttribute(S, PrivateVD, AllocatorKind, AC->getAllocator(),
3856  E->getSourceRange());
3857  }
3858  }
3859 }
3860 
3863  OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3864  Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
3865  StmtResult Res = StmtError();
3866  // First check CancelRegion which is then used in checkNestingOfRegions.
3867  if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
3868  checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3869  StartLoc))
3870  return StmtError();
3871 
3872  llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
3873  VarsWithInheritedDSAType VarsWithInheritedDSA;
3874  bool ErrorFound = false;
3875  ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
3876  if (AStmt && !CurContext->isDependentContext()) {
3877  assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3878 
3879  // Check default data sharing attributes for referenced variables.
3880  DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3881  int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
3882  Stmt *S = AStmt;
3883  while (--ThisCaptureLevel >= 0)
3884  S = cast<CapturedStmt>(S)->getCapturedStmt();
3885  DSAChecker.Visit(S);
3886  if (DSAChecker.isErrorFound())
3887  return StmtError();
3888  // Generate list of implicitly defined firstprivate variables.
3889  VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
3890 
3891  SmallVector<Expr *, 4> ImplicitFirstprivates(
3892  DSAChecker.getImplicitFirstprivate().begin(),
3893  DSAChecker.getImplicitFirstprivate().end());
3894  SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
3895  DSAChecker.getImplicitMap().end());
3896  // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
3897  for (OMPClause *C : Clauses) {
3898  if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
3899  for (Expr *E : IRC->taskgroup_descriptors())
3900  if (E)
3901  ImplicitFirstprivates.emplace_back(E);
3902  }
3903  }
3904  if (!ImplicitFirstprivates.empty()) {
3905  if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3906  ImplicitFirstprivates, SourceLocation(), SourceLocation(),
3907  SourceLocation())) {
3908  ClausesWithImplicit.push_back(Implicit);
3909  ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3910  ImplicitFirstprivates.size();
3911  } else {
3912  ErrorFound = true;
3913  }
3914  }
3915  if (!ImplicitMaps.empty()) {
3916  CXXScopeSpec MapperIdScopeSpec;
3917  DeclarationNameInfo MapperId;
3918  if (OMPClause *Implicit = ActOnOpenMPMapClause(
3919  llvm::None, llvm::None, MapperIdScopeSpec, MapperId,
3920  OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true, SourceLocation(),
3921  SourceLocation(), ImplicitMaps, OMPVarListLocTy())) {
3922  ClausesWithImplicit.emplace_back(Implicit);
3923  ErrorFound |=
3924  cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
3925  } else {
3926  ErrorFound = true;
3927  }
3928  }
3929  }
3930 
3931  llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
3932  switch (Kind) {
3933  case OMPD_parallel:
3934  Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3935  EndLoc);
3936  AllowedNameModifiers.push_back(OMPD_parallel);
3937  break;
3938  case OMPD_simd:
3939  Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3940  VarsWithInheritedDSA);
3941  break;
3942  case OMPD_for:
3943  Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3944  VarsWithInheritedDSA);
3945  break;
3946  case OMPD_for_simd:
3947  Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3948  EndLoc, VarsWithInheritedDSA);
3949  break;
3950  case OMPD_sections:
3951  Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3952  EndLoc);
3953  break;
3954  case OMPD_section:
3955  assert(ClausesWithImplicit.empty() &&
3956  "No clauses are allowed for 'omp section' directive");
3957  Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3958  break;
3959  case OMPD_single:
3960  Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3961  EndLoc);
3962  break;
3963  case OMPD_master:
3964  assert(ClausesWithImplicit.empty() &&
3965  "No clauses are allowed for 'omp master' directive");
3966  Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3967  break;
3968  case OMPD_critical:
3969  Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3970  StartLoc, EndLoc);
3971  break;
3972  case OMPD_parallel_for:
3973  Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3974  EndLoc, VarsWithInheritedDSA);
3975  AllowedNameModifiers.push_back(OMPD_parallel);
3976  break;
3977  case OMPD_parallel_for_simd:
3978  Res = ActOnOpenMPParallelForSimdDirective(
3979  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3980  AllowedNameModifiers.push_back(OMPD_parallel);
3981  break;
3982  case OMPD_parallel_sections:
3983  Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3984  StartLoc, EndLoc);
3985  AllowedNameModifiers.push_back(OMPD_parallel);
3986  break;
3987  case OMPD_task:
3988  Res =
3989  ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3990  AllowedNameModifiers.push_back(OMPD_task);
3991  break;
3992  case OMPD_taskyield:
3993  assert(ClausesWithImplicit.empty() &&
3994  "No clauses are allowed for 'omp taskyield' directive");
3995  assert(AStmt == nullptr &&
3996  "No associated statement allowed for 'omp taskyield' directive");
3997  Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3998  break;
3999  case OMPD_barrier:
4000  assert(ClausesWithImplicit.empty() &&
4001  "No clauses are allowed for 'omp barrier' directive");
4002  assert(AStmt == nullptr &&
4003  "No associated statement allowed for 'omp barrier' directive");
4004  Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
4005  break;
4006  case OMPD_taskwait:
4007  assert(ClausesWithImplicit.empty() &&
4008  "No clauses are allowed for 'omp taskwait' directive");
4009  assert(AStmt == nullptr &&
4010  "No associated statement allowed for 'omp taskwait' directive");
4011  Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
4012  break;
4013  case OMPD_taskgroup:
4014  Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
4015  EndLoc);
4016  break;
4017  case OMPD_flush:
4018  assert(AStmt == nullptr &&
4019  "No associated statement allowed for 'omp flush' directive");
4020  Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
4021  break;
4022  case OMPD_ordered:
4023  Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
4024  EndLoc);
4025  break;
4026  case OMPD_atomic:
4027  Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
4028  EndLoc);
4029  break;
4030  case OMPD_teams:
4031  Res =
4032  ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
4033  break;
4034  case OMPD_target:
4035  Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
4036  EndLoc);
4037  AllowedNameModifiers.push_back(OMPD_target);
4038  break;
4039  case OMPD_target_parallel:
4040  Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
4041  StartLoc, EndLoc);
4042  AllowedNameModifiers.push_back(OMPD_target);
4043  AllowedNameModifiers.push_back(OMPD_parallel);
4044  break;
4045  case OMPD_target_parallel_for:
4046  Res = ActOnOpenMPTargetParallelForDirective(
4047  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4048  AllowedNameModifiers.push_back(OMPD_target);
4049  AllowedNameModifiers.push_back(OMPD_parallel);
4050  break;
4051  case OMPD_cancellation_point:
4052  assert(ClausesWithImplicit.empty() &&
4053  "No clauses are allowed for 'omp cancellation point' directive");
4054  assert(AStmt == nullptr && "No associated statement allowed for 'omp "
4055  "cancellation point' directive");
4056  Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
4057  break;
4058  case OMPD_cancel:
4059  assert(AStmt == nullptr &&
4060  "No associated statement allowed for 'omp cancel' directive");
4061  Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
4062  CancelRegion);
4063  AllowedNameModifiers.push_back(OMPD_cancel);
4064  break;
4065  case OMPD_target_data:
4066  Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
4067  EndLoc);
4068  AllowedNameModifiers.push_back(OMPD_target_data);
4069  break;
4070  case OMPD_target_enter_data:
4071  Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
4072  EndLoc, AStmt);
4073  AllowedNameModifiers.push_back(OMPD_target_enter_data);
4074  break;
4075  case OMPD_target_exit_data:
4076  Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
4077  EndLoc, AStmt);
4078  AllowedNameModifiers.push_back(OMPD_target_exit_data);
4079  break;
4080  case OMPD_taskloop:
4081  Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
4082  EndLoc, VarsWithInheritedDSA);
4083  AllowedNameModifiers.push_back(OMPD_taskloop);
4084  break;
4085  case OMPD_taskloop_simd:
4086  Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
4087  EndLoc, VarsWithInheritedDSA);
4088  AllowedNameModifiers.push_back(OMPD_taskloop);
4089  break;
4090  case OMPD_distribute:
4091  Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
4092  EndLoc, VarsWithInheritedDSA);
4093  break;
4094  case OMPD_target_update:
4095  Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
4096  EndLoc, AStmt);
4097  AllowedNameModifiers.push_back(OMPD_target_update);
4098  break;
4099  case OMPD_distribute_parallel_for:
4100  Res = ActOnOpenMPDistributeParallelForDirective(
4101  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4102  AllowedNameModifiers.push_back(OMPD_parallel);
4103  break;
4104  case OMPD_distribute_parallel_for_simd:
4105  Res = ActOnOpenMPDistributeParallelForSimdDirective(
4106  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4107  AllowedNameModifiers.push_back(OMPD_parallel);
4108  break;
4109  case OMPD_distribute_simd:
4110  Res = ActOnOpenMPDistributeSimdDirective(
4111  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4112  break;
4113  case OMPD_target_parallel_for_simd:
4114  Res = ActOnOpenMPTargetParallelForSimdDirective(
4115  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4116  AllowedNameModifiers.push_back(OMPD_target);
4117  AllowedNameModifiers.push_back(OMPD_parallel);
4118  break;
4119  case OMPD_target_simd:
4120  Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
4121  EndLoc, VarsWithInheritedDSA);
4122  AllowedNameModifiers.push_back(OMPD_target);
4123  break;
4124  case OMPD_teams_distribute:
4125  Res = ActOnOpenMPTeamsDistributeDirective(
4126  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4127  break;
4128  case OMPD_teams_distribute_simd:
4129  Res = ActOnOpenMPTeamsDistributeSimdDirective(
4130  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4131  break;
4132  case OMPD_teams_distribute_parallel_for_simd:
4133  Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
4134  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4135  AllowedNameModifiers.push_back(OMPD_parallel);
4136  break;
4137  case OMPD_teams_distribute_parallel_for:
4138  Res = ActOnOpenMPTeamsDistributeParallelForDirective(
4139  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4140  AllowedNameModifiers.push_back(OMPD_parallel);
4141  break;
4142  case OMPD_target_teams:
4143  Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
4144  EndLoc);
4145  AllowedNameModifiers.push_back(OMPD_target);
4146  break;
4147  case OMPD_target_teams_distribute:
4148  Res = ActOnOpenMPTargetTeamsDistributeDirective(
4149  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4150  AllowedNameModifiers.push_back(OMPD_target);
4151  break;
4152  case OMPD_target_teams_distribute_parallel_for:
4153  Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
4154  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4155  AllowedNameModifiers.push_back(OMPD_target);
4156  AllowedNameModifiers.push_back(OMPD_parallel);
4157  break;
4158  case OMPD_target_teams_distribute_parallel_for_simd:
4159  Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
4160  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4161  AllowedNameModifiers.push_back(OMPD_target);
4162  AllowedNameModifiers.push_back(OMPD_parallel);
4163  break;
4164  case OMPD_target_teams_distribute_simd:
4165  Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
4166  ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4167  AllowedNameModifiers.push_back(OMPD_target);
4168  break;
4169  case OMPD_declare_target:
4170  case OMPD_end_declare_target:
4171  case OMPD_threadprivate:
4172  case OMPD_allocate:
4173  case OMPD_declare_reduction:
4174  case OMPD_declare_mapper:
4175  case OMPD_declare_simd:
4176  case OMPD_requires:
4177  llvm_unreachable("OpenMP Directive is not allowed");
4178  case OMPD_unknown:
4179  llvm_unreachable("Unknown OpenMP directive");
4180  }
4181 
4182  ErrorFound = Res.isInvalid() || ErrorFound;
4183 
4184  for (const auto &P : VarsWithInheritedDSA) {
4185  Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
4186  << P.first << P.second->getSourceRange();
4187  }
4188  ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
4189 
4190  if (!AllowedNameModifiers.empty())
4191  ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
4192  ErrorFound;
4193 
4194  if (ErrorFound)
4195  return StmtError();
4196 
4199  ->getStructuredBlock()
4200  ->setIsOMPStructuredBlock(true);
4201  }
4202 
4203  if (!CurContext->isDependentContext() &&
4205  !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
4206  DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() ||
4207  DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() ||
4208  DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) {
4209  // Register target to DSA Stack.
4210  DSAStack->addTargetDirLocation(StartLoc);
4211  }
4212 
4213  return Res;
4214 }
4215 
4217  DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
4218  ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
4219  ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
4220  ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
4221  assert(Aligneds.size() == Alignments.size());
4222  assert(Linears.size() == LinModifiers.size());
4223  assert(Linears.size() == Steps.size());
4224  if (!DG || DG.get().isNull())
4225  return DeclGroupPtrTy();
4226 
4227  if (!DG.get().isSingleDecl()) {
4228  Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
4229  return DG;
4230  }
4231  Decl *ADecl = DG.get().getSingleDecl();
4232  if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
4233  ADecl = FTD->getTemplatedDecl();
4234 
4235  auto *FD = dyn_cast<FunctionDecl>(ADecl);
4236  if (!FD) {
4237  Diag(ADecl->getLocation(), diag::err_omp_function_expected);
4238  return DeclGroupPtrTy();
4239  }
4240 
4241  // OpenMP [2.8.2, declare simd construct, Description]
4242  // The parameter of the simdlen clause must be a constant positive integer
4243  // expression.
4244  ExprResult SL;
4245  if (Simdlen)
4246  SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
4247  // OpenMP [2.8.2, declare simd construct, Description]
4248  // The special this pointer can be used as if was one of the arguments to the
4249  // function in any of the linear, aligned, or uniform clauses.
4250  // The uniform clause declares one or more arguments to have an invariant
4251  // value for all concurrent invocations of the function in the execution of a
4252  // single SIMD loop.
4253  llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
4254  const Expr *UniformedLinearThis = nullptr;
4255  for (const Expr *E : Uniforms) {
4256  E = E->IgnoreParenImpCasts();
4257  if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
4258  if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
4259  if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
4260  FD->getParamDecl(PVD->getFunctionScopeIndex())
4261  ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
4262  UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E);
4263  continue;
4264  }
4265  if (isa<CXXThisExpr>(E)) {
4266  UniformedLinearThis = E;
4267  continue;
4268  }
4269  Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
4270  << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
4271  }
4272  // OpenMP [2.8.2, declare simd construct, Description]
4273  // The aligned clause declares that the object to which each list item points
4274  // is aligned to the number of bytes expressed in the optional parameter of
4275  // the aligned clause.
4276  // The special this pointer can be used as if was one of the arguments to the
4277  // function in any of the linear, aligned, or uniform clauses.
4278  // The type of list items appearing in the aligned clause must be array,
4279  // pointer, reference to array, or reference to pointer.
4280  llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
4281  const Expr *AlignedThis = nullptr;
4282  for (const Expr *E : Aligneds) {
4283  E = E->IgnoreParenImpCasts();
4284  if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
4285  if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
4286  const VarDecl *CanonPVD = PVD->getCanonicalDecl();
4287  if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
4288  FD->getParamDecl(PVD->getFunctionScopeIndex())
4289  ->getCanonicalDecl() == CanonPVD) {
4290  // OpenMP [2.8.1, simd construct, Restrictions]
4291  // A list-item cannot appear in more than one aligned clause.
4292  if (AlignedArgs.count(CanonPVD) > 0) {
4293  Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
4294  << 1 << E->getSourceRange();
4295  Diag(AlignedArgs[CanonPVD]->getExprLoc(),
4296  diag::note_omp_explicit_dsa)
4297  << getOpenMPClauseName(OMPC_aligned);
4298  continue;
4299  }
4300  AlignedArgs[CanonPVD] = E;
4301  QualType QTy = PVD->getType()
4302  .getNonReferenceType()
4303  .getUnqualifiedType()
4304  .getCanonicalType();
4305  const Type *Ty = QTy.getTypePtrOrNull();
4306  if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
4307  Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
4308  << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
4309  Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
4310  }
4311  continue;
4312  }
4313  }
4314  if (isa<CXXThisExpr>(E)) {
4315  if (AlignedThis) {
4316  Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
4317  << 2 << E->getSourceRange();
4318  Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
4319  << getOpenMPClauseName(OMPC_aligned);
4320  }
4321  AlignedThis = E;
4322  continue;
4323  }
4324  Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
4325  << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
4326  }
4327  // The optional parameter of the aligned clause, alignment, must be a constant
4328  // positive integer expression. If no optional parameter is specified,
4329  // implementation-defined default alignments for SIMD instructions on the
4330  // target platforms are assumed.
4331  SmallVector<const Expr *, 4> NewAligns;
4332  for (Expr *E : Alignments) {
4333  ExprResult Align;
4334  if (E)
4335  Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
4336  NewAligns.push_back(Align.get());
4337  }
4338  // OpenMP [2.8.2, declare simd construct, Description]
4339  // The linear clause declares one or more list items to be private to a SIMD
4340  // lane and to have a linear relationship with respect to the iteration space
4341  // of a loop.
4342  // The special this pointer can be used as if was one of the arguments to the
4343  // function in any of the linear, aligned, or uniform clauses.
4344  // When a linear-step expression is specified in a linear clause it must be
4345  // either a constant integer expression or an integer-typed parameter that is
4346  // specified in a uniform clause on the directive.
4347  llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
4348  const bool IsUniformedThis = UniformedLinearThis != nullptr;
4349  auto MI = LinModifiers.begin();
4350  for (const Expr *E : Linears) {
4351  auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
4352  ++MI;
4353  E = E->IgnoreParenImpCasts();
4354  if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
4355  if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
4356  const VarDecl *CanonPVD = PVD->getCanonicalDecl();
4357  if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
4358  FD->getParamDecl(PVD->getFunctionScopeIndex())
4359  ->getCanonicalDecl() == CanonPVD) {
4360  // OpenMP [2.15.3.7, linear Clause, Restrictions]
4361  // A list-item cannot appear in more than one linear clause.
4362  if (LinearArgs.count(CanonPVD) > 0) {
4363  Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
4364  << getOpenMPClauseName(OMPC_linear)
4365  << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
4366  Diag(LinearArgs[CanonPVD]->getExprLoc(),
4367  diag::note_omp_explicit_dsa)
4368  << getOpenMPClauseName(OMPC_linear);
4369  continue;
4370  }
4371  // Each argument can appear in at most one uniform or linear clause.
4372  if (UniformedArgs.count(CanonPVD) > 0) {
4373  Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
4374  << getOpenMPClauseName(OMPC_linear)
4376  Diag(UniformedArgs[CanonPVD]->getExprLoc(),
4377  diag::note_omp_explicit_dsa)
4379  continue;
4380  }
4381  LinearArgs[CanonPVD] = E;
4382  if (E->isValueDependent() || E->isTypeDependent() ||
4383  E->isInstantiationDependent() ||
4385  continue;
4386  (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
4387  PVD->getOriginalType());
4388  continue;
4389  }
4390  }
4391  if (isa<CXXThisExpr>(E)) {
4392  if (UniformedLinearThis) {
4393  Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
4394  << getOpenMPClauseName(OMPC_linear)
4395  << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
4396  << E->getSourceRange();
4397  Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
4398  << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
4399  : OMPC_linear);
4400  continue;
4401  }
4402  UniformedLinearThis = E;
4403  if (E->isValueDependent() || E->isTypeDependent() ||
4405  continue;
4406  (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
4407  E->getType());
4408  continue;
4409  }
4410  Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
4411  << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
4412  }
4413  Expr *Step = nullptr;
4414  Expr *NewStep = nullptr;
4415  SmallVector<Expr *, 4> NewSteps;
4416  for (Expr *E : Steps) {
4417  // Skip the same step expression, it was checked already.
4418  if (Step == E || !E) {
4419  NewSteps.push_back(E ? NewStep : nullptr);
4420  continue;
4421  }
4422  Step = E;
4423  if (const auto *DRE = dyn_cast<DeclRefExpr>(Step))
4424  if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
4425  const VarDecl *CanonPVD = PVD->getCanonicalDecl();
4426  if (UniformedArgs.count(CanonPVD) == 0) {
4427  Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
4428  << Step->getSourceRange();
4429  } else if (E->isValueDependent() || E->isTypeDependent() ||
4430  E->isInstantiationDependent() ||
4432  CanonPVD->getType()->hasIntegerRepresentation()) {
4433  NewSteps.push_back(Step);
4434  } else {
4435  Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
4436  << Step->getSourceRange();
4437  }
4438  continue;
4439  }
4440  NewStep = Step;
4441  if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
4442  !Step->isInstantiationDependent() &&
4444  NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
4445  .get();
4446  if (NewStep)
4447  NewStep = VerifyIntegerConstantExpression(NewStep).get();
4448  }
4449  NewSteps.push_back(NewStep);
4450  }
4451  auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
4452  Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
4453  Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
4454  const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
4455  const_cast<Expr **>(Linears.data()), Linears.size(),
4456  const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
4457  NewSteps.data(), NewSteps.size(), SR);
4458  ADecl->addAttr(NewAttr);
4459  return ConvertDeclToDeclGroup(ADecl);
4460 }
4461 
4463  Stmt *AStmt,
4464  SourceLocation StartLoc,
4465  SourceLocation EndLoc) {
4466  if (!AStmt)
4467  return StmtError();
4468 
4469  auto *CS = cast<CapturedStmt>(AStmt);
4470  // 1.2.2 OpenMP Language Terminology
4471  // Structured block - An executable statement with a single entry at the
4472  // top and a single exit at the bottom.
4473  // The point of exit cannot be a branch out of the structured block.
4474  // longjmp() and throw() must not violate the entry/exit criteria.
4475  CS->getCapturedDecl()->setNothrow();
4476 
4477  setFunctionHasBranchProtectedScope();
4478 
4479  return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4480  DSAStack->isCancelRegion());
4481 }
4482 
4483 namespace {
4484 /// Helper class for checking canonical form of the OpenMP loops and
4485 /// extracting iteration space of each loop in the loop nest, that will be used
4486 /// for IR generation.
4487 class OpenMPIterationSpaceChecker {
4488  /// Reference to Sema.
4489  Sema &SemaRef;
4490  /// A location for diagnostics (when there is no some better location).
4491  SourceLocation DefaultLoc;
4492  /// A location for diagnostics (when increment is not compatible).
4493  SourceLocation ConditionLoc;
4494  /// A source location for referring to loop init later.
4495  SourceRange InitSrcRange;
4496  /// A source location for referring to condition later.
4497  SourceRange ConditionSrcRange;
4498  /// A source location for referring to increment later.
4499  SourceRange IncrementSrcRange;
4500  /// Loop variable.
4501  ValueDecl *LCDecl = nullptr;
4502  /// Reference to loop variable.
4503  Expr *LCRef = nullptr;
4504  /// Lower bound (initializer for the var).
4505  Expr *LB = nullptr;
4506  /// Upper bound.
4507  Expr *UB = nullptr;
4508  /// Loop step (increment).
4509  Expr *Step = nullptr;
4510  /// This flag is true when condition is one of:
4511  /// Var < UB
4512  /// Var <= UB
4513  /// UB > Var
4514  /// UB >= Var
4515  /// This will have no value when the condition is !=
4516  llvm::Optional<bool> TestIsLessOp;
4517  /// This flag is true when condition is strict ( < or > ).
4518  bool TestIsStrictOp = false;
4519  /// This flag is true when step is subtracted on each iteration.
4520  bool SubtractStep = false;
4521 
4522 public:
4523  OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
4524  : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
4525  /// Check init-expr for canonical loop form and save loop counter
4526  /// variable - #Var and its initialization value - #LB.
4527  bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
4528  /// Check test-expr for canonical form, save upper-bound (#UB), flags
4529  /// for less/greater and for strict/non-strict comparison.
4530  bool checkAndSetCond(Expr *S);
4531  /// Check incr-expr for canonical loop form and return true if it
4532  /// does not conform, otherwise save loop step (#Step).
4533  bool checkAndSetInc(Expr *S);
4534  /// Return the loop counter variable.
4535  ValueDecl *getLoopDecl() const { return LCDecl; }
4536  /// Return the reference expression to loop counter variable.
4537  Expr *getLoopDeclRefExpr() const { return LCRef; }
4538  /// Source range of the loop init.
4539  SourceRange getInitSrcRange() const { return InitSrcRange; }
4540  /// Source range of the loop condition.
4541  SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
4542  /// Source range of the loop increment.
4543  SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
4544  /// True if the step should be subtracted.
4545  bool shouldSubtractStep() const { return SubtractStep; }
4546  /// True, if the compare operator is strict (<, > or !=).
4547  bool isStrictTestOp() const { return TestIsStrictOp; }
4548  /// Build the expression to calculate the number of iterations.
4549  Expr *buildNumIterations(
4550  Scope *S, const bool LimitedType,
4551  llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
4552  /// Build the precondition expression for the loops.
4553  Expr *
4554  buildPreCond(Scope *S, Expr *Cond,
4555  llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
4556  /// Build reference expression to the counter be used for codegen.
4557  DeclRefExpr *
4558  buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4559  DSAStackTy &DSA) const;
4560  /// Build reference expression to the private counter be used for
4561  /// codegen.
4562  Expr *buildPrivateCounterVar() const;
4563  /// Build initialization of the counter be used for codegen.
4564  Expr *buildCounterInit() const;
4565  /// Build step of the counter be used for codegen.
4566  Expr *buildCounterStep() const;
4567  /// Build loop data with counter value for depend clauses in ordered
4568  /// directives.
4569  Expr *
4570  buildOrderedLoopData(Scope *S, Expr *Counter,
4571  llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4572  SourceLocation Loc, Expr *Inc = nullptr,
4573  OverloadedOperatorKind OOK = OO_Amp);
4574  /// Return true if any expression is dependent.
4575  bool dependent() const;
4576 
4577 private:
4578  /// Check the right-hand side of an assignment in the increment
4579  /// expression.
4580  bool checkAndSetIncRHS(Expr *RHS);
4581  /// Helper to set loop counter variable and its initializer.
4582  bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
4583  /// Helper to set upper bound.
4584  bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp,
4585  SourceRange SR, SourceLocation SL);
4586  /// Helper to set loop increment.
4587  bool setStep(Expr *NewStep, bool Subtract);
4588 };
4589 
4590 bool OpenMPIterationSpaceChecker::dependent() const {
4591  if (!LCDecl) {
4592  assert(!LB && !UB && !Step);
4593  return false;
4594  }
4595  return LCDecl->getType()->isDependentType() ||
4596  (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
4597  (Step && Step->isValueDependent());
4598 }
4599 
4600 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
4601  Expr *NewLCRefExpr,
4602  Expr *NewLB) {
4603  // State consistency checking to ensure correct usage.
4604  assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
4605  UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
4606  if (!NewLCDecl || !NewLB)
4607  return true;
4608  LCDecl = getCanonicalDecl(NewLCDecl);
4609  LCRef = NewLCRefExpr;
4610  if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
4611  if (const CXXConstructorDecl *Ctor = CE->getConstructor())
4612  if ((Ctor->isCopyOrMoveConstructor() ||
4613  Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4614  CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
4615  NewLB = CE->getArg(0)->IgnoreParenImpCasts();
4616  LB = NewLB;
4617  return false;
4618 }
4619 
4620 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB,
4621  llvm::Optional<bool> LessOp,
4622  bool StrictOp, SourceRange SR,
4623  SourceLocation SL) {
4624  // State consistency checking to ensure correct usage.
4625  assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
4626  Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
4627  if (!NewUB)
4628  return true;
4629  UB = NewUB;
4630  if (LessOp)
4631  TestIsLessOp = LessOp;
4632  TestIsStrictOp = StrictOp;
4633  ConditionSrcRange = SR;
4634  ConditionLoc = SL;
4635  return false;
4636 }
4637 
4638 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
4639  // State consistency checking to ensure correct usage.
4640  assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
4641  if (!NewStep)
4642  return true;
4643  if (!NewStep->isValueDependent()) {
4644  // Check that the step is integer expression.
4645  SourceLocation StepLoc = NewStep->getBeginLoc();
4647  StepLoc, getExprAsWritten(NewStep));
4648  if (Val.isInvalid())
4649  return true;
4650  NewStep = Val.get();
4651 
4652  // OpenMP [2.6, Canonical Loop Form, Restrictions]
4653  // If test-expr is of form var relational-op b and relational-op is < or
4654  // <= then incr-expr must cause var to increase on each iteration of the
4655  // loop. If test-expr is of form var relational-op b and relational-op is
4656  // > or >= then incr-expr must cause var to decrease on each iteration of
4657  // the loop.
4658  // If test-expr is of form b relational-op var and relational-op is < or
4659  // <= then incr-expr must cause var to decrease on each iteration of the
4660  // loop. If test-expr is of form b relational-op var and relational-op is
4661  // > or >= then incr-expr must cause var to increase on each iteration of
4662  // the loop.
4663  llvm::APSInt Result;
4664  bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
4665  bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
4666  bool IsConstNeg =
4667  IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
4668  bool IsConstPos =
4669  IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
4670  bool IsConstZero = IsConstant && !Result.getBoolValue();
4671 
4672  // != with increment is treated as <; != with decrement is treated as >
4673  if (!TestIsLessOp.hasValue())
4674  TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract);
4675  if (UB && (IsConstZero ||
4676  (TestIsLessOp.getValue() ?
4677  (IsConstNeg || (IsUnsigned && Subtract)) :
4678  (IsConstPos || (IsUnsigned && !Subtract))))) {
4679  SemaRef.Diag(NewStep->getExprLoc(),
4680  diag::err_omp_loop_incr_not_compatible)
4681  << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange();
4682  SemaRef.Diag(ConditionLoc,
4683  diag::note_omp_loop_cond_requres_compatible_incr)
4684  << TestIsLessOp.getValue() << ConditionSrcRange;
4685  return true;
4686  }
4687  if (TestIsLessOp.getValue() == Subtract) {
4688  NewStep =
4689  SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
4690  .get();
4691  Subtract = !Subtract;
4692  }
4693  }
4694 
4695  Step = NewStep;
4696  SubtractStep = Subtract;
4697  return false;
4698 }
4699 
4700 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
4701  // Check init-expr for canonical loop form and save loop counter
4702  // variable - #Var and its initialization value - #LB.
4703  // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
4704  // var = lb
4705  // integer-type var = lb
4706  // random-access-iterator-type var = lb
4707  // pointer-type var = lb
4708  //
4709  if (!S) {
4710  if (EmitDiags) {
4711  SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
4712  }
4713  return true;
4714  }
4715  if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4716  if (!ExprTemp->cleanupsHaveSideEffects())
4717  S = ExprTemp->getSubExpr();
4718 
4719  InitSrcRange = S->getSourceRange();
4720  if (Expr *E = dyn_cast<Expr>(S))
4721  S = E->IgnoreParens();
4722  if (auto *BO = dyn_cast<BinaryOperator>(S)) {
4723  if (BO->getOpcode() == BO_Assign) {
4724  Expr *LHS = BO->getLHS()->IgnoreParens();
4725  if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4726  if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4727  if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4728  return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4729  return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
4730  }
4731  if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4732  if (ME->isArrow() &&
4733  isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4734  return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4735  }
4736  }
4737  } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
4738  if (DS->isSingleDecl()) {
4739  if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
4740  if (Var->hasInit() && !Var->getType()->isReferenceType()) {
4741  // Accept non-canonical init form here but emit ext. warning.
4742  if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
4743  SemaRef.Diag(S->getBeginLoc(),
4744  diag::ext_omp_loop_not_canonical_init)
4745  << S->getSourceRange();
4746  return setLCDeclAndLB(
4747  Var,
4748  buildDeclRefExpr(SemaRef, Var,
4749  Var->getType().getNonReferenceType(),
4750  DS->getBeginLoc()),
4751  Var->getInit());
4752  }
4753  }
4754  }
4755  } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4756  if (CE->getOperator() == OO_Equal) {
4757  Expr *LHS = CE->getArg(0);
4758  if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4759  if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4760  if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4761  return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4762  return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
4763  }
4764  if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4765  if (ME->isArrow() &&
4766  isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4767  return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4768  }
4769  }
4770  }
4771 
4772  if (dependent() || SemaRef.CurContext->isDependentContext())
4773  return false;
4774  if (EmitDiags) {
4775  SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init)
4776  << S->getSourceRange();
4777  }
4778  return true;
4779 }
4780 
4781 /// Ignore parenthesizes, implicit casts, copy constructor and return the
4782 /// variable (which may be the loop variable) if possible.
4783 static const ValueDecl *getInitLCDecl(const Expr *E) {
4784  if (!E)
4785  return nullptr;
4786  E = getExprAsWritten(E);
4787  if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
4788  if (const CXXConstructorDecl *Ctor = CE->getConstructor())
4789  if ((Ctor->isCopyOrMoveConstructor() ||
4790  Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4791  CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
4792  E = CE->getArg(0)->IgnoreParenImpCasts();
4793  if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4794  if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
4795  return getCanonicalDecl(VD);
4796  }
4797  if (const auto *ME = dyn_cast_or_null<MemberExpr>(E))
4798  if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4799  return getCanonicalDecl(ME->getMemberDecl());
4800  return nullptr;
4801 }
4802 
4803 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
4804  // Check test-expr for canonical form, save upper-bound UB, flags for
4805  // less/greater and for strict/non-strict comparison.
4806  // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4807  // var relational-op b
4808  // b relational-op var
4809  //
4810  if (!S) {
4811  SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
4812  return true;
4813  }
4814  S = getExprAsWritten(S);
4815  SourceLocation CondLoc = S->getBeginLoc();
4816  if (auto *BO = dyn_cast<BinaryOperator>(S)) {
4817  if (BO->isRelationalOp()) {
4818  if (getInitLCDecl(BO->getLHS()) == LCDecl)
4819  return setUB(BO->getRHS(),
4820  (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4821  (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4822  BO->getSourceRange(), BO->getOperatorLoc());
4823  if (getInitLCDecl(BO->getRHS()) == LCDecl)
4824  return setUB(BO->getLHS(),
4825  (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4826  (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4827  BO->getSourceRange(), BO->getOperatorLoc());
4828  } else if (BO->getOpcode() == BO_NE)
4829  return setUB(getInitLCDecl(BO->getLHS()) == LCDecl ?
4830  BO->getRHS() : BO->getLHS(),
4831  /*LessOp=*/llvm::None,
4832  /*StrictOp=*/true,
4833  BO->getSourceRange(), BO->getOperatorLoc());
4834  } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4835  if (CE->getNumArgs() == 2) {
4836  auto Op = CE->getOperator();
4837  switch (Op) {
4838  case OO_Greater:
4839  case OO_GreaterEqual:
4840  case OO_Less:
4841  case OO_LessEqual:
4842  if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4843  return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
4844  Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4845  CE->getOperatorLoc());
4846  if (getInitLCDecl(CE->getArg(1)) == LCDecl)
4847  return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
4848  Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4849  CE->getOperatorLoc());
4850  break;
4851  case OO_ExclaimEqual:
4852  return setUB(getInitLCDecl(CE->getArg(0)) == LCDecl ?
4853  CE->getArg(1) : CE->getArg(0),
4854  /*LessOp=*/llvm::None,
4855  /*StrictOp=*/true,
4856  CE->getSourceRange(),
4857  CE->getOperatorLoc());
4858  break;
4859  default:
4860  break;
4861  }
4862  }
4863  }
4864  if (dependent() || SemaRef.CurContext->isDependentContext())
4865  return false;
4866  SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
4867  << S->getSourceRange() << LCDecl;
4868  return true;
4869 }
4870 
4871 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
4872  // RHS of canonical loop form increment can be:
4873  // var + incr
4874  // incr + var
4875  // var - incr
4876  //
4877  RHS = RHS->IgnoreParenImpCasts();
4878  if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
4879  if (BO->isAdditiveOp()) {
4880  bool IsAdd = BO->getOpcode() == BO_Add;
4881  if (getInitLCDecl(BO->getLHS()) == LCDecl)
4882  return setStep(BO->getRHS(), !IsAdd);
4883  if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl)
4884  return setStep(BO->getLHS(), /*Subtract=*/false);
4885  }
4886  } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
4887  bool IsAdd = CE->getOperator() == OO_Plus;
4888  if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
4889  if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4890  return setStep(CE->getArg(1), !IsAdd);
4891  if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl)
4892  return setStep(CE->getArg(0), /*Subtract=*/false);
4893  }
4894  }
4895  if (dependent() || SemaRef.CurContext->isDependentContext())
4896  return false;
4897  SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
4898  << RHS->getSourceRange() << LCDecl;
4899  return true;
4900 }
4901 
4902 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
4903  // Check incr-expr for canonical loop form and return true if it
4904  // does not conform.
4905  // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4906  // ++var
4907  // var++
4908  // --var
4909  // var--
4910  // var += incr
4911  // var -= incr
4912  // var = var + incr
4913  // var = incr + var
4914  // var = var - incr
4915  //
4916  if (!S) {
4917  SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
4918  return true;
4919  }
4920  if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4921  if (!ExprTemp->cleanupsHaveSideEffects())
4922  S = ExprTemp->getSubExpr();
4923 
4924  IncrementSrcRange = S->getSourceRange();
4925  S = S->IgnoreParens();
4926  if (auto *UO = dyn_cast<UnaryOperator>(S)) {
4927  if (UO->isIncrementDecrementOp() &&
4928  getInitLCDecl(UO->getSubExpr()) == LCDecl)
4929  return setStep(SemaRef
4930  .ActOnIntegerConstant(UO->getBeginLoc(),
4931  (UO->isDecrementOp() ? -1 : 1))
4932  .get(),
4933  /*Subtract=*/false);
4934  } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
4935  switch (BO->getOpcode()) {
4936  case BO_AddAssign:
4937  case BO_SubAssign:
4938  if (getInitLCDecl(BO->getLHS()) == LCDecl)
4939  return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
4940  break;
4941  case BO_Assign:
4942  if (getInitLCDecl(BO->getLHS()) == LCDecl)
4943  return checkAndSetIncRHS(BO->getRHS());
4944  break;
4945  default:
4946  break;
4947  }
4948  } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4949  switch (CE->getOperator()) {
4950  case OO_PlusPlus:
4951  case OO_MinusMinus:
4952  if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4953  return setStep(SemaRef
4954  .ActOnIntegerConstant(
4955  CE->getBeginLoc(),
4956  ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
4957  .get(),
4958  /*Subtract=*/false);
4959  break;
4960  case OO_PlusEqual:
4961  case OO_MinusEqual:
4962  if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4963  return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
4964  break;
4965  case OO_Equal:
4966  if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4967  return checkAndSetIncRHS(CE->getArg(1));
4968  break;
4969  default:
4970  break;
4971  }
4972  }
4973  if (dependent() || SemaRef.CurContext->isDependentContext())
4974  return false;
4975  SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
4976  << S->getSourceRange() << LCDecl;
4977  return true;
4978 }
4979 
4980 static ExprResult
4981 tryBuildCapture(Sema &SemaRef, Expr *Capture,
4982  llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
4983  if (SemaRef.CurContext->isDependentContext())
4984  return ExprResult(Capture);
4985  if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4986  return SemaRef.PerformImplicitConversion(
4987  Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4988  /*AllowExplicit=*/true);
4989  auto I = Captures.find(Capture);
4990  if (I != Captures.end())
4991  return buildCapture(SemaRef, Capture, I->second);
4992  DeclRefExpr *Ref = nullptr;
4993  ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4994  Captures[Capture] = Ref;
4995  return Res;
4996 }
4997 
4998 /// Build the expression to calculate the number of iterations.
4999 Expr *OpenMPIterationSpaceChecker::buildNumIterations(
5000  Scope *S, const bool LimitedType,
5001  llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
5002  ExprResult Diff;
5003  QualType VarType = LCDecl->getType().getNonReferenceType();
5004  if (VarType->isIntegerType() || VarType->isPointerType() ||
5005  SemaRef.getLangOpts().CPlusPlus) {
5006  // Upper - Lower
5007  Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB;
5008  Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB;
5009  Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
5010  Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
5011  if (!Upper || !Lower)
5012  return nullptr;
5013 
5014  Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
5015 
5016  if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
5017  // BuildBinOp already emitted error, this one is to point user to upper
5018  // and lower bound, and to tell what is passed to 'operator-'.
5019  SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
5020  << Upper->getSourceRange() << Lower->getSourceRange();
5021  return nullptr;
5022  }
5023  }
5024 
5025  if (!Diff.isUsable())
5026  return nullptr;
5027 
5028  // Upper - Lower [- 1]
5029  if (TestIsStrictOp)
5030  Diff = SemaRef.BuildBinOp(
5031  S, DefaultLoc, BO_Sub, Diff.get(),
5032  SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5033  if (!Diff.isUsable())
5034  return nullptr;
5035 
5036  // Upper - Lower [- 1] + Step
5037  ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
5038  if (!NewStep.isUsable())
5039  return nullptr;
5040  Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
5041  if (!Diff.isUsable())
5042  return nullptr;
5043 
5044  // Parentheses (for dumping/debugging purposes only).
5045  Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
5046  if (!Diff.isUsable())
5047  return nullptr;
5048 
5049  // (Upper - Lower [- 1] + Step) / Step
5050  Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
5051  if (!Diff.isUsable())
5052  return nullptr;
5053 
5054  // OpenMP runtime requires 32-bit or 64-bit loop variables.
5055  QualType Type = Diff.get()->getType();
5056  ASTContext &C = SemaRef.Context;
5057  bool UseVarType = VarType->hasIntegerRepresentation() &&
5058  C.getTypeSize(Type) > C.getTypeSize(VarType);
5059  if (!Type->isIntegerType() || UseVarType) {
5060  unsigned NewSize =
5061  UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
5062  bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
5064  Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
5065  if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
5066  Diff = SemaRef.PerformImplicitConversion(
5067  Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
5068  if (!Diff.isUsable())
5069  return nullptr;
5070  }
5071  }
5072  if (LimitedType) {
5073  unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
5074  if (NewSize != C.getTypeSize(Type)) {
5075  if (NewSize < C.getTypeSize(Type)) {
5076  assert(NewSize == 64 && "incorrect loop var size");
5077  SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
5078  << InitSrcRange << ConditionSrcRange;
5079  }
5080  QualType NewType = C.getIntTypeForBitwidth(
5081  NewSize, Type->hasSignedIntegerRepresentation() ||
5082  C.getTypeSize(Type) < NewSize);
5083  if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
5084  Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
5085  Sema::AA_Converting, true);
5086  if (!Diff.isUsable())
5087  return nullptr;
5088  }
5089  }
5090  }
5091 
5092  return Diff.get();
5093 }
5094 
5095 Expr *OpenMPIterationSpaceChecker::buildPreCond(
5096  Scope *S, Expr *Cond,
5097  llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
5098  // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
5099  bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
5100  SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
5101 
5102  ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures);
5103  ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures);
5104  if (!NewLB.isUsable() || !NewUB.isUsable())
5105  return nullptr;
5106 
5107  ExprResult CondExpr =
5108  SemaRef.BuildBinOp(S, DefaultLoc,
5109  TestIsLessOp.getValue() ?
5110  (TestIsStrictOp ? BO_LT : BO_LE) :
5111  (TestIsStrictOp ? BO_GT : BO_GE),
5112  NewLB.get(), NewUB.get());
5113  if (CondExpr.isUsable()) {
5114  if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
5115  SemaRef.Context.BoolTy))
5116  CondExpr = SemaRef.PerformImplicitConversion(
5117  CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
5118  /*AllowExplicit=*/true);
5119  }
5120  SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
5121  // Otherwise use original loop condition and evaluate it in runtime.
5122  return CondExpr.isUsable() ? CondExpr.get() : Cond;
5123 }
5124 
5125 /// Build reference expression to the counter be used for codegen.
5126 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
5127  llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
5128  DSAStackTy &DSA) const {
5129  auto *VD = dyn_cast<VarDecl>(LCDecl);
5130  if (!VD) {
5131  VD = SemaRef.isOpenMPCapturedDecl(LCDecl);
5133  SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
5134  const DSAStackTy::DSAVarData Data =
5135  DSA.getTopDSA(LCDecl, /*FromParent=*/false);
5136  // If the loop control decl is explicitly marked as private, do not mark it
5137  // as captured again.
5138  if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
5139  Captures.insert(std::make_pair(LCRef, Ref));
5140  return Ref;
5141  }
5142  return cast<DeclRefExpr>(LCRef);
5143 }
5144 
5145 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
5146  if (LCDecl && !LCDecl->isInvalidDecl()) {
5147  QualType Type = LCDecl->getType().getNonReferenceType();
5148  VarDecl *PrivateVar = buildVarDecl(
5149  SemaRef, DefaultLoc, Type, LCDecl->getName(),
5150  LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
5151  isa<VarDecl>(LCDecl)
5152  ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
5153  : nullptr);
5154  if (PrivateVar->isInvalidDecl())
5155  return nullptr;
5156  return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
5157  }
5158  return nullptr;
5159 }
5160 
5161 /// Build initialization of the counter to be used for codegen.
5163 
5164 /// Build step of the counter be used for codegen.
5165 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
5166 
5167 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
5168  Scope *S, Expr *Counter,
5169  llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
5170  Expr *Inc, OverloadedOperatorKind OOK) {
5171  Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get();
5172  if (!Cnt)
5173  return nullptr;
5174  if (Inc) {
5175  assert((OOK == OO_Plus || OOK == OO_Minus) &&
5176  "Expected only + or - operations for depend clauses.");
5177  BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
5178  Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get();
5179  if (!Cnt)
5180  return nullptr;
5181  }
5182  ExprResult Diff;
5183  QualType VarType = LCDecl->getType().getNonReferenceType();
5184  if (VarType->isIntegerType() || VarType->isPointerType() ||
5185  SemaRef.getLangOpts().CPlusPlus) {
5186  // Upper - Lower
5187  Expr *Upper = TestIsLessOp.getValue()
5188  ? Cnt
5189  : tryBuildCapture(SemaRef, UB, Captures).get();
5190  Expr *Lower = TestIsLessOp.getValue()
5191  ? tryBuildCapture(SemaRef, LB, Captures).get()
5192  : Cnt;
5193  if (!Upper || !Lower)
5194  return nullptr;
5195 
5196  Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
5197 
5198  if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
5199  // BuildBinOp already emitted error, this one is to point user to upper
5200  // and lower bound, and to tell what is passed to 'operator-'.
5201  SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
5202  << Upper->getSourceRange() << Lower->getSourceRange();
5203  return nullptr;
5204  }
5205  }
5206 
5207  if (!Diff.isUsable())
5208  return nullptr;
5209 
5210  // Parentheses (for dumping/debugging purposes only).
5211  Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
5212  if (!Diff.isUsable())
5213  return nullptr;
5214 
5215  ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
5216  if (!NewStep.isUsable())
5217  return nullptr;
5218  // (Upper - Lower) / Step
5219  Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
5220  if (!Diff.isUsable())
5221  return nullptr;
5222 
5223  return Diff.get();
5224 }
5225 
5226 /// Iteration space of a single for loop.
5227 struct LoopIterationSpace final {
5228  /// True if the condition operator is the strict compare operator (<, > or
5229  /// !=).
5230  bool IsStrictCompare = false;
5231  /// Condition of the loop.
5232  Expr *PreCond = nullptr;
5233  /// This expression calculates the number of iterations in the loop.
5234  /// It is always possible to calculate it before starting the loop.
5235  Expr *NumIterations = nullptr;
5236  /// The loop counter variable.
5237  Expr *CounterVar = nullptr;
5238  /// Private loop counter variable.
5239  Expr *PrivateCounterVar = nullptr;
5240  /// This is initializer for the initial value of #CounterVar.
5241  Expr *CounterInit = nullptr;
5242  /// This is step for the #CounterVar used to generate its update:
5243  /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
5244  Expr *CounterStep = nullptr;
5245  /// Should step be subtracted?
5246  bool Subtract = false;
5247  /// Source range of the loop init.
5248  SourceRange InitSrcRange;
5249  /// Source range of the loop condition.
5250  SourceRange CondSrcRange;
5251  /// Source range of the loop increment.
5252  SourceRange IncSrcRange;
5253 };
5254 
5255 } // namespace
5256 
5258  assert(getLangOpts().OpenMP && "OpenMP is not active.");
5259  assert(Init && "Expected loop in canonical form.");
5260  unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
5261  if (AssociatedLoops > 0 &&
5262  isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
5263  DSAStack->loopStart();
5264  OpenMPIterationSpaceChecker ISC(*this, ForLoc);
5265  if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) {
5266  if (ValueDecl *D = ISC.getLoopDecl()) {
5267  auto *VD = dyn_cast<VarDecl>(D);
5268  if (!VD) {
5269  if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
5270  VD = Private;
5271  } else {
5272  DeclRefExpr *Ref = buildCapture(*this, D, ISC.getLoopDeclRefExpr(),
5273  /*WithInit=*/false);
5274  VD = cast<VarDecl>(Ref->getDecl());
5275  }
5276  }
5277  DSAStack->addLoopControlVariable(D, VD);
5278  const Decl *LD = DSAStack->getPossiblyLoopCunter();
5279  if (LD != D->getCanonicalDecl()) {
5280  DSAStack->resetPossibleLoopCounter();
5281  if (auto *Var = dyn_cast_or_null<VarDecl>(LD))
5282  MarkDeclarationsReferencedInExpr(
5283  buildDeclRefExpr(*this, const_cast<VarDecl *>(Var),
5284  Var->getType().getNonLValueExprType(Context),
5285  ForLoc, /*RefersToCapture=*/true));
5286  }
5287  }
5288  }
5289  DSAStack->setAssociatedLoops(AssociatedLoops - 1);
5290  }
5291 }
5292 
5293 /// Called on a for stmt to check and extract its iteration space
5294 /// for further processing (such as collapsing).
5296  OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
5297  unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
5298  unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
5299  Expr *OrderedLoopCountExpr,
5300  Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
5301  LoopIterationSpace &ResultIterSpace,
5302  llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
5303  // OpenMP [2.6, Canonical Loop Form]
5304  // for (init-expr; test-expr; incr-expr) structured-block
5305  auto *For = dyn_cast_or_null<ForStmt>(S);
5306  if (!For) {
5307  SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for)
5308  << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
5309  << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount
5310  << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
5311  if (TotalNestedLoopCount > 1) {
5312  if (CollapseLoopCountExpr && OrderedLoopCountExpr)
5313  SemaRef.Diag(DSA.getConstructLoc(),
5314  diag::note_omp_collapse_ordered_expr)
5315  << 2 << CollapseLoopCountExpr->getSourceRange()
5316  << OrderedLoopCountExpr->getSourceRange();
5317  else if (CollapseLoopCountExpr)
5318  SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
5319  diag::note_omp_collapse_ordered_expr)
5320  << 0 << CollapseLoopCountExpr->getSourceRange();
5321  else
5322  SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
5323  diag::note_omp_collapse_ordered_expr)
5324  << 1 << OrderedLoopCountExpr->getSourceRange();
5325  }
5326  return true;
5327  }
5328  assert(For->getBody());
5329 
5330  OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
5331 
5332  // Check init.
5333  Stmt *Init = For->getInit();
5334  if (ISC.checkAndSetInit(Init))
5335  return true;
5336 
5337  bool HasErrors = false;
5338 
5339  // Check loop variable's type.
5340  if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
5341  Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
5342 
5343  // OpenMP [2.6, Canonical Loop Form]
5344  // Var is one of the following:
5345  // A variable of signed or unsigned integer type.
5346  // For C++, a variable of a random access iterator type.
5347  // For C, a variable of a pointer type.
5348  QualType VarType = LCDecl->getType().getNonReferenceType();
5349  if (!VarType->isDependentType() && !VarType->isIntegerType() &&
5350  !VarType->isPointerType() &&
5351  !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
5352  SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type)
5353  << SemaRef.getLangOpts().CPlusPlus;
5354  HasErrors = true;
5355  }
5356 
5357  // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
5358  // a Construct
5359  // The loop iteration variable(s) in the associated for-loop(s) of a for or
5360  // parallel for construct is (are) private.
5361  // The loop iteration variable in the associated for-loop of a simd
5362  // construct with just one associated for-loop is linear with a
5363  // constant-linear-step that is the increment of the associated for-loop.
5364  // Exclude loop var from the list of variables with implicitly defined data
5365  // sharing attributes.
5366  VarsWithImplicitDSA.erase(LCDecl);
5367 
5368  // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
5369  // in a Construct, C/C++].
5370  // The loop iteration variable in the associated for-loop of a simd
5371  // construct with just one associated for-loop may be listed in a linear
5372  // clause with a constant-linear-step that is the increment of the
5373  // associated for-loop.
5374  // The loop iteration variable(s) in the associated for-loop(s) of a for or
5375  // parallel for construct may be listed in a private or lastprivate clause.
5376  DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
5377  // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
5378  // declared in the loop and it is predetermined as a private.
5379  OpenMPClauseKind PredeterminedCKind =
5380  isOpenMPSimdDirective(DKind)
5381  ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
5382  : OMPC_private;
5383  if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
5384  DVar.CKind != PredeterminedCKind) ||
5385  ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
5386  isOpenMPDistributeDirective(DKind)) &&
5387  !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
5388  DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
5389  (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
5390  SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa)
5391  << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
5392  << getOpenMPClauseName(PredeterminedCKind);
5393  if (DVar.RefExpr == nullptr)
5394  DVar.CKind = PredeterminedCKind;
5395  reportOriginalDsa(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
5396  HasErrors = true;
5397  } else if (LoopDeclRefExpr != nullptr) {
5398  // Make the loop iteration variable private (for worksharing constructs),
5399  // linear (for simd directives with the only one associated loop) or
5400  // lastprivate (for simd directives with several collapsed or ordered
5401  // loops).
5402  if (DVar.CKind == OMPC_unknown)
5403  DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
5404  }
5405 
5406  assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
5407 
5408  // Check test-expr.
5409  HasErrors |= ISC.checkAndSetCond(For->getCond());
5410 
5411  // Check incr-expr.
5412  HasErrors |= ISC.checkAndSetInc(For->getInc());
5413  }
5414 
5415  if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
5416  return HasErrors;
5417 
5418  // Build the loop's iteration space representation.
5419  ResultIterSpace.PreCond =
5420  ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures);
5421  ResultIterSpace.NumIterations = ISC.buildNumIterations(
5422  DSA.getCurScope(),
5423  (isOpenMPWorksharingDirective(DKind) ||
5425  Captures);
5426  ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA);
5427  ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar();
5428  ResultIterSpace.CounterInit = ISC.buildCounterInit();
5429  ResultIterSpace.CounterStep = ISC.buildCounterStep();
5430  ResultIterSpace.InitSrcRange = ISC.getInitSrcRange();
5431  ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange();
5432  ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange();
5433  ResultIterSpace.Subtract = ISC.shouldSubtractStep();
5434  ResultIterSpace.IsStrictCompare = ISC.isStrictTestOp();
5435 
5436  HasErrors |= (ResultIterSpace.PreCond == nullptr ||
5437  ResultIterSpace.NumIterations == nullptr ||
5438  ResultIterSpace.CounterVar == nullptr ||
5439  ResultIterSpace.PrivateCounterVar == nullptr ||
5440  ResultIterSpace.CounterInit == nullptr ||
5441  ResultIterSpace.CounterStep == nullptr);
5442  if (!HasErrors && DSA.isOrderedRegion()) {
5443  if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
5444  if (CurrentNestedLoopCount <
5445  DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
5446  DSA.getOrderedRegionParam().second->setLoopNumIterations(
5447  CurrentNestedLoopCount, ResultIterSpace.NumIterations);
5448  DSA.getOrderedRegionParam().second->setLoopCounter(
5449  CurrentNestedLoopCount, ResultIterSpace.CounterVar);
5450  }
5451  }
5452  for (auto &Pair : DSA.getDoacrossDependClauses()) {
5453  if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) {
5454  // Erroneous case - clause has some problems.
5455  continue;
5456  }
5457  if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink &&
5458  Pair.second.size() <= CurrentNestedLoopCount) {
5459  // Erroneous case - clause has some problems.
5460  Pair.first->setLoopData(CurrentNestedLoopCount, nullptr);
5461  continue;
5462  }
5463  Expr *CntValue;
5464  if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
5465  CntValue = ISC.buildOrderedLoopData(
5466  DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
5467  Pair.first->getDependencyLoc());
5468  else
5469  CntValue = ISC.buildOrderedLoopData(
5470  DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
5471  Pair.first->getDependencyLoc(),
5472  Pair.second[CurrentNestedLoopCount].first,
5473  Pair.second[CurrentNestedLoopCount].second);
5474  Pair.first->setLoopData(CurrentNestedLoopCount, CntValue);
5475  }
5476  }
5477 
5478  return HasErrors;
5479 }
5480 
5481 /// Build 'VarRef = Start.
5482 static ExprResult
5484  ExprResult Start,
5485  llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
5486  // Build 'VarRef = Start.
5487  ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
5488  if (!NewStart.isUsable())
5489  return ExprError();
5490  if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
5491  VarRef.get()->getType())) {
5492  NewStart = SemaRef.PerformImplicitConversion(
5493  NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
5494  /*AllowExplicit=*/true);
5495  if (!NewStart.isUsable())
5496  return ExprError();
5497  }
5498 
5499  ExprResult Init =
5500  SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
5501  return Init;
5502 }
5503 
5504 /// Build 'VarRef = Start + Iter * Step'.
5506  Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
5507  ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
5508  llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
5509  // Add parentheses (for debugging purposes only).
5510  Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
5511  if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
5512  !Step.isUsable())
5513  return ExprError();
5514 
5515  ExprResult NewStep = Step;
5516  if (Captures)
5517  NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
5518  if (NewStep.isInvalid())
5519  return ExprError();
5520  ExprResult Update =
5521  SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
5522  if (!Update.isUsable())
5523  return ExprError();
5524 
5525  // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
5526  // 'VarRef = Start (+|-) Iter * Step'.
5527  ExprResult NewStart = Start;
5528  if (Captures)
5529  NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
5530  if (NewStart.isInvalid())
5531  return ExprError();
5532 
5533  // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
5534  ExprResult SavedUpdate = Update;
5535  ExprResult UpdateVal;
5536  if (VarRef.get()->getType()->isOverloadableType() ||
5537  NewStart.get()->getType()->isOverloadableType() ||
5538  Update.get()->getType()->isOverloadableType()) {
5539  bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
5540  SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
5541  Update =
5542  SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
5543  if (Update.isUsable()) {
5544  UpdateVal =
5545  SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
5546  VarRef.get(), SavedUpdate.get());
5547  if (UpdateVal.isUsable()) {
5548  Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
5549  UpdateVal.get());
5550  }
5551  }
5552  SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
5553  }
5554 
5555  // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
5556  if (!Update.isUsable() || !UpdateVal.isUsable()) {
5557  Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
5558  NewStart.get(), SavedUpdate.get());
5559  if (!Update.isUsable())
5560  return ExprError();
5561 
5562  if (!SemaRef.Context.hasSameType(Update.get()->getType(),
5563  VarRef.get()->getType())) {
5564  Update = SemaRef.PerformImplicitConversion(
5565  Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
5566  if (!Update.isUsable())
5567  return ExprError();
5568  }
5569 
5570  Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
5571  }
5572  return Update;
5573 }
5574 
5575 /// Convert integer expression \a E to make it have at least \a Bits
5576 /// bits.
5577 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
5578  if (E == nullptr)
5579  return ExprError();
5580  ASTContext &C = SemaRef.Context;
5581  QualType OldType = E->getType();
5582  unsigned HasBits = C.getTypeSize(OldType);
5583  if (HasBits >= Bits)
5584  return ExprResult(E);
5585  // OK to convert to signed, because new type has more bits than old.
5586  QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
5587  return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
5588  true);
5589 }
5590 
5591 /// Check if the given expression \a E is a constant integer that fits
5592 /// into \a Bits bits.
5593 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
5594  if (E == nullptr)
5595  return false;
5596  llvm::APSInt Result;
5597  if (E->isIntegerConstantExpr(Result, SemaRef.Context))
5598  return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
5599  return false;
5600 }
5601 
5602 /// Build preinits statement for the given declarations.
5603 static Stmt *buildPreInits(ASTContext &Context,
5604  MutableArrayRef<Decl *> PreInits) {
5605  if (!PreInits.empty()) {
5606  return new (Context) DeclStmt(
5607  DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
5609  }
5610  return nullptr;
5611 }
5612 
5613 /// Build preinits statement for the given declarations.
5614 static Stmt *
5616  const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
5617  if (!Captures.empty()) {
5618  SmallVector<Decl *, 16> PreInits;
5619  for (const auto &Pair : Captures)
5620  PreInits.push_back(Pair.second->getDecl());
5621  return buildPreInits(Context, PreInits);
5622  }
5623  return nullptr;
5624 }
5625 
5626 /// Build postupdate expression for the given list of postupdates expressions.
5627 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
5628  Expr *PostUpdate = nullptr;
5629  if (!PostUpdates.empty()) {
5630  for (Expr *E : PostUpdates) {
5631  Expr *ConvE = S.BuildCStyleCastExpr(
5632  E->getExprLoc(),
5634  E->getExprLoc(), E)
5635  .get();
5636  PostUpdate = PostUpdate
5637  ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
5638  PostUpdate, ConvE)
5639  .get()
5640  : ConvE;
5641  }
5642  }
5643  return PostUpdate;
5644 }
5645 
5646 /// Called on a for stmt to check itself and nested loops (if any).
5647 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
5648 /// number of collapsed loops otherwise.
5649 static unsigned
5650 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
5651  Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
5652  DSAStackTy &DSA,
5653  Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
5655  unsigned NestedLoopCount = 1;
5656  if (CollapseLoopCountExpr) {
5657  // Found 'collapse' clause - calculate collapse number.
5659  if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
5660  NestedLoopCount = Result.Val.getInt().getLimitedValue();
5661  }
5662  unsigned OrderedLoopCount = 1;
5663  if (OrderedLoopCountExpr) {
5664  // Found 'ordered' clause - calculate collapse number.
5665  Expr::EvalResult EVResult;
5666  if (OrderedLoopCountExpr->EvaluateAsInt(EVResult, SemaRef.getASTContext())) {
5667  llvm::APSInt Result = EVResult.Val.getInt();
5668  if (Result.getLimitedValue() < NestedLoopCount) {
5669  SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
5670  diag::err_omp_wrong_ordered_loop_count)
5671  << OrderedLoopCountExpr->getSourceRange();
5672  SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
5673  diag::note_collapse_loop_count)
5674  << CollapseLoopCountExpr->getSourceRange();
5675  }
5676  OrderedLoopCount = Result.getLimitedValue();
5677  }
5678  }
5679  // This is helper routine for loop directives (e.g., 'for', 'simd',
5680  // 'for simd', etc.).
5681  llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
5683  std::max(OrderedLoopCount, NestedLoopCount));
5684  Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
5685  for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
5687  DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
5688  std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
5689  OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
5690  Captures))
5691  return 0;
5692  // Move on to the next nested for loop, or to the loop body.
5693  // OpenMP [2.8.1, simd construct, Restrictions]
5694  // All loops associated with the construct must be perfectly nested; that
5695  // is, there must be no intervening code nor any OpenMP directive between
5696  // any two loops.
5697  CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
5698  }
5699  for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) {
5701  DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
5702  std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
5703  OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
5704  Captures))
5705  return 0;
5706  if (Cnt > 0 && IterSpaces[Cnt].CounterVar) {
5707  // Handle initialization of captured loop iterator variables.
5708  auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar);
5709  if (isa<OMPCapturedExprDecl>(DRE->getDecl())) {
5710  Captures[DRE] = DRE;
5711  }
5712  }
5713  // Move on to the next nested for loop, or to the loop body.
5714  // OpenMP [2.8.1, simd construct, Restrictions]
5715  // All loops associated with the construct must be perfectly nested; that
5716  // is, there must be no intervening code nor any OpenMP directive between
5717  // any two loops.
5718  CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
5719  }
5720 
5721  Built.clear(/* size */ NestedLoopCount);
5722 
5723  if (SemaRef.CurContext->isDependentContext())
5724  return NestedLoopCount;
5725 
5726  // An example of what is generated for the following code:
5727  //
5728  // #pragma omp simd collapse(2) ordered(2)
5729  // for (i = 0; i < NI; ++i)
5730  // for (k = 0; k < NK; ++k)
5731  // for (j = J0; j < NJ; j+=2) {
5732  // <loop body>
5733  // }
5734  //
5735  // We generate the code below.
5736  // Note: the loop body may be outlined in CodeGen.
5737  // Note: some counters may be C++ classes, operator- is used to find number of
5738  // iterations and operator+= to calculate counter value.
5739  // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
5740  // or i64 is currently supported).
5741  //
5742  // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
5743  // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
5744  // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
5745  // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
5746  // // similar updates for vars in clauses (e.g. 'linear')
5747  // <loop body (using local i and j)>
5748  // }
5749  // i = NI; // assign final values of counters
5750  // j = NJ;
5751  //
5752 
5753  // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
5754  // the iteration counts of the collapsed for loops.
5755  // Precondition tests if there is at least one iteration (all conditions are
5756  // true).
5757  auto PreCond = ExprResult(IterSpaces[0].PreCond);