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