Coverage Report

Created: 2022-01-22 13:19

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Sema/SemaOpenMP.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
/// \file
9
/// This file implements semantic analysis for OpenMP directives and
10
/// clauses.
11
///
12
//===----------------------------------------------------------------------===//
13
14
#include "TreeTransform.h"
15
#include "clang/AST/ASTContext.h"
16
#include "clang/AST/ASTMutationListener.h"
17
#include "clang/AST/CXXInheritance.h"
18
#include "clang/AST/Decl.h"
19
#include "clang/AST/DeclCXX.h"
20
#include "clang/AST/DeclOpenMP.h"
21
#include "clang/AST/OpenMPClause.h"
22
#include "clang/AST/StmtCXX.h"
23
#include "clang/AST/StmtOpenMP.h"
24
#include "clang/AST/StmtVisitor.h"
25
#include "clang/AST/TypeOrdering.h"
26
#include "clang/Basic/DiagnosticSema.h"
27
#include "clang/Basic/OpenMPKinds.h"
28
#include "clang/Basic/PartialDiagnostic.h"
29
#include "clang/Basic/TargetInfo.h"
30
#include "clang/Sema/Initialization.h"
31
#include "clang/Sema/Lookup.h"
32
#include "clang/Sema/Scope.h"
33
#include "clang/Sema/ScopeInfo.h"
34
#include "clang/Sema/SemaInternal.h"
35
#include "llvm/ADT/IndexedMap.h"
36
#include "llvm/ADT/PointerEmbeddedInt.h"
37
#include "llvm/ADT/STLExtras.h"
38
#include "llvm/ADT/StringExtras.h"
39
#include "llvm/Frontend/OpenMP/OMPAssume.h"
40
#include "llvm/Frontend/OpenMP/OMPConstants.h"
41
#include <set>
42
43
using namespace clang;
44
using namespace llvm::omp;
45
46
//===----------------------------------------------------------------------===//
47
// Stack of data-sharing attributes for variables
48
//===----------------------------------------------------------------------===//
49
50
static const Expr *checkMapClauseExpressionBase(
51
    Sema &SemaRef, Expr *E,
52
    OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
53
    OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, bool NoDiagnose);
54
55
namespace {
56
/// Default data sharing attributes, which can be applied to directive.
57
enum DefaultDataSharingAttributes {
58
  DSA_unspecified = 0,       /// Data sharing attribute not specified.
59
  DSA_none = 1 << 0,         /// Default data sharing attribute 'none'.
60
  DSA_shared = 1 << 1,       /// Default data sharing attribute 'shared'.
61
  DSA_firstprivate = 1 << 2, /// Default data sharing attribute 'firstprivate'.
62
};
63
64
/// Stack for tracking declarations used in OpenMP directives and
65
/// clauses and their data-sharing attributes.
66
class DSAStackTy {
67
public:
68
  struct DSAVarData {
69
    OpenMPDirectiveKind DKind = OMPD_unknown;
70
    OpenMPClauseKind CKind = OMPC_unknown;
71
    unsigned Modifier = 0;
72
    const Expr *RefExpr = nullptr;
73
    DeclRefExpr *PrivateCopy = nullptr;
74
    SourceLocation ImplicitDSALoc;
75
    bool AppliedToPointee = false;
76
9.75M
    DSAVarData() = default;
77
    DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
78
               const Expr *RefExpr, DeclRefExpr *PrivateCopy,
79
               SourceLocation ImplicitDSALoc, unsigned Modifier,
80
               bool AppliedToPointee)
81
        : DKind(DKind), CKind(CKind), Modifier(Modifier), RefExpr(RefExpr),
82
          PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc),
83
1.06k
          AppliedToPointee(AppliedToPointee) {}
84
  };
85
  using OperatorOffsetTy =
86
      llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>;
87
  using DoacrossDependMapTy =
88
      llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>;
89
  /// Kind of the declaration used in the uses_allocators clauses.
90
  enum class UsesAllocatorsDeclKind {
91
    /// Predefined allocator
92
    PredefinedAllocator,
93
    /// User-defined allocator
94
    UserDefinedAllocator,
95
    /// The declaration that represent allocator trait
96
    AllocatorTrait,
97
  };
98
99
private:
100
  struct DSAInfo {
101
    OpenMPClauseKind Attributes = OMPC_unknown;
102
    unsigned Modifier = 0;
103
    /// Pointer to a reference expression and a flag which shows that the
104
    /// variable is marked as lastprivate(true) or not (false).
105
    llvm::PointerIntPair<const Expr *, 1, bool> RefExpr;
106
    DeclRefExpr *PrivateCopy = nullptr;
107
    /// true if the attribute is applied to the pointee, not the variable
108
    /// itself.
109
    bool AppliedToPointee = false;
110
  };
111
  using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>;
112
  using UsedRefMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>;
113
  using LCDeclInfo = std::pair<unsigned, VarDecl *>;
114
  using LoopControlVariablesMapTy =
115
      llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>;
116
  /// Struct that associates a component with the clause kind where they are
117
  /// found.
118
  struct MappedExprComponentTy {
119
    OMPClauseMappableExprCommon::MappableExprComponentLists Components;
120
    OpenMPClauseKind Kind = OMPC_unknown;
121
  };
122
  using MappedExprComponentsTy =
123
      llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>;
124
  using CriticalsWithHintsTy =
125
      llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>;
126
  struct ReductionData {
127
    using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>;
128
    SourceRange ReductionRange;
129
    llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp;
130
1.97k
    ReductionData() = default;
131
1.94k
    void set(BinaryOperatorKind BO, SourceRange RR) {
132
1.94k
      ReductionRange = RR;
133
1.94k
      ReductionOp = BO;
134
1.94k
    }
135
30
    void set(const Expr *RefExpr, SourceRange RR) {
136
30
      ReductionRange = RR;
137
30
      ReductionOp = RefExpr;
138
30
    }
139
  };
140
  using DeclReductionMapTy =
141
      llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>;
142
  struct DefaultmapInfo {
143
    OpenMPDefaultmapClauseModifier ImplicitBehavior =
144
        OMPC_DEFAULTMAP_MODIFIER_unknown;
145
    SourceLocation SLoc;
146
1.02M
    DefaultmapInfo() = default;
147
    DefaultmapInfo(OpenMPDefaultmapClauseModifier M, SourceLocation Loc)
148
0
        : ImplicitBehavior(M), SLoc(Loc) {}
149
  };
150
151
  struct SharingMapTy {
152
    DeclSAMapTy SharingMap;
153
    DeclReductionMapTy ReductionMap;
154
    UsedRefMapTy AlignedMap;
155
    UsedRefMapTy NontemporalMap;
156
    MappedExprComponentsTy MappedExprComponents;
157
    LoopControlVariablesMapTy LCVMap;
158
    DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
159
    SourceLocation DefaultAttrLoc;
160
    DefaultmapInfo DefaultmapMap[OMPC_DEFAULTMAP_unknown];
161
    OpenMPDirectiveKind Directive = OMPD_unknown;
162
    DeclarationNameInfo DirectiveName;
163
    Scope *CurScope = nullptr;
164
    DeclContext *Context = nullptr;
165
    SourceLocation ConstructLoc;
166
    /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
167
    /// get the data (loop counters etc.) about enclosing loop-based construct.
168
    /// This data is required during codegen.
169
    DoacrossDependMapTy DoacrossDepends;
170
    /// First argument (Expr *) contains optional argument of the
171
    /// 'ordered' clause, the second one is true if the regions has 'ordered'
172
    /// clause, false otherwise.
173
    llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion;
174
    unsigned AssociatedLoops = 1;
175
    bool HasMutipleLoops = false;
176
    const Decl *PossiblyLoopCounter = nullptr;
177
    bool NowaitRegion = false;
178
    bool CancelRegion = false;
179
    bool LoopStart = false;
180
    bool BodyComplete = false;
181
    SourceLocation PrevScanLocation;
182
    SourceLocation PrevOrderedLocation;
183
    SourceLocation InnerTeamsRegionLoc;
184
    /// Reference to the taskgroup task_reduction reference expression.
185
    Expr *TaskgroupReductionRef = nullptr;
186
    llvm::DenseSet<QualType> MappedClassesQualTypes;
187
    SmallVector<Expr *, 4> InnerUsedAllocators;
188
    llvm::DenseSet<CanonicalDeclPtr<Decl>> ImplicitTaskFirstprivates;
189
    /// List of globals marked as declare target link in this target region
190
    /// (isOpenMPTargetExecutionDirective(Directive) == true).
191
    llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls;
192
    /// List of decls used in inclusive/exclusive clauses of the scan directive.
193
    llvm::DenseSet<CanonicalDeclPtr<Decl>> UsedInScanDirective;
194
    llvm::DenseMap<CanonicalDeclPtr<const Decl>, UsesAllocatorsDeclKind>
195
        UsesAllocatorsDecls;
196
    Expr *DeclareMapperVar = nullptr;
197
    SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
198
                 Scope *CurScope, SourceLocation Loc)
199
        : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
200
342k
          ConstructLoc(Loc) {}
201
    SharingMapTy() = default;
202
  };
203
204
  using StackTy = SmallVector<SharingMapTy, 4>;
205
206
  /// Stack of used declaration and their data-sharing attributes.
207
  DeclSAMapTy Threadprivates;
208
  const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
209
  SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
210
  /// true, if check for DSA must be from parent directive, false, if
211
  /// from current directive.
212
  OpenMPClauseKind ClauseKindMode = OMPC_unknown;
213
  Sema &SemaRef;
214
  bool ForceCapturing = false;
215
  /// true if all the variables in the target executable directives must be
216
  /// captured by reference.
217
  bool ForceCaptureByReferenceInTargetExecutable = false;
218
  CriticalsWithHintsTy Criticals;
219
  unsigned IgnoredStackElements = 0;
220
221
  /// Iterators over the stack iterate in order from innermost to outermost
222
  /// directive.
223
  using const_iterator = StackTy::const_reverse_iterator;
224
1.15M
  const_iterator begin() const {
225
1.15M
    return Stack.empty() ? 
const_iterator()332
226
1.15M
                         : 
Stack.back().first.rbegin() + IgnoredStackElements1.14M
;
227
1.15M
  }
228
9.38M
  const_iterator end() const {
229
9.38M
    return Stack.empty() ? 
const_iterator()332
:
Stack.back().first.rend()9.38M
;
230
9.38M
  }
231
  using iterator = StackTy::reverse_iterator;
232
3.91M
  iterator begin() {
233
3.91M
    return Stack.empty() ? 
iterator()0
234
3.91M
                         : Stack.back().first.rbegin() + IgnoredStackElements;
235
3.91M
  }
236
3.92M
  iterator end() {
237
3.92M
    return Stack.empty() ? 
iterator()0
: Stack.back().first.rend();
238
3.92M
  }
239
240
  // Convenience operations to get at the elements of the stack.
241
242
100M
  bool isStackEmpty() const {
243
100M
    return Stack.empty() ||
244
100M
           
Stack.back().second != CurrentNonCapturingFunctionScope100M
||
245
100M
           
Stack.back().first.size() <= IgnoredStackElements100M
;
246
100M
  }
247
73.1M
  size_t getStackSize() const {
248
73.1M
    return isStackEmpty() ? 
0407k
249
73.1M
                          : 
Stack.back().first.size() - IgnoredStackElements72.7M
;
250
73.1M
  }
251
252
36.4M
  SharingMapTy *getTopOfStackOrNull() {
253
36.4M
    size_t Size = getStackSize();
254
36.4M
    if (Size == 0)
255
406k
      return nullptr;
256
36.0M
    return &Stack.back().first[Size - 1];
257
36.4M
  }
258
22.0M
  const SharingMapTy *getTopOfStackOrNull() const {
259
22.0M
    return const_cast<DSAStackTy &>(*this).getTopOfStackOrNull();
260
22.0M
  }
261
14.3M
  SharingMapTy &getTopOfStack() {
262
14.3M
    assert(!isStackEmpty() && "no current directive");
263
0
    return *getTopOfStackOrNull();
264
14.3M
  }
265
12.5M
  const SharingMapTy &getTopOfStack() const {
266
12.5M
    return const_cast<DSAStackTy &>(*this).getTopOfStack();
267
12.5M
  }
268
269
575k
  SharingMapTy *getSecondOnStackOrNull() {
270
575k
    size_t Size = getStackSize();
271
575k
    if (Size <= 1)
272
171k
      return nullptr;
273
404k
    return &Stack.back().first[Size - 2];
274
575k
  }
275
535k
  const SharingMapTy *getSecondOnStackOrNull() const {
276
535k
    return const_cast<DSAStackTy &>(*this).getSecondOnStackOrNull();
277
535k
  }
278
279
  /// Get the stack element at a certain level (previously returned by
280
  /// \c getNestingLevel).
281
  ///
282
  /// Note that nesting levels count from outermost to innermost, and this is
283
  /// the reverse of our iteration order where new inner levels are pushed at
284
  /// the front of the stack.
285
19.3M
  SharingMapTy &getStackElemAtLevel(unsigned Level) {
286
19.3M
    assert(Level < getStackSize() && "no such stack element");
287
0
    return Stack.back().first[Level];
288
19.3M
  }
289
19.1M
  const SharingMapTy &getStackElemAtLevel(unsigned Level) const {
290
19.1M
    return const_cast<DSAStackTy &>(*this).getStackElemAtLevel(Level);
291
19.1M
  }
292
293
  DSAVarData getDSA(const_iterator &Iter, ValueDecl *D) const;
294
295
  /// Checks if the variable is a local for OpenMP region.
296
  bool isOpenMPLocal(VarDecl *D, const_iterator Iter) const;
297
298
  /// Vector of previously declared requires directives
299
  SmallVector<const OMPRequiresDecl *, 2> RequiresDecls;
300
  /// omp_allocator_handle_t type.
301
  QualType OMPAllocatorHandleT;
302
  /// omp_depend_t type.
303
  QualType OMPDependT;
304
  /// omp_event_handle_t type.
305
  QualType OMPEventHandleT;
306
  /// omp_alloctrait_t type.
307
  QualType OMPAlloctraitT;
308
  /// Expression for the predefined allocators.
309
  Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = {
310
      nullptr};
311
  /// Vector of previously encountered target directives
312
  SmallVector<SourceLocation, 2> TargetLocations;
313
  SourceLocation AtomicLocation;
314
  /// Vector of declare variant construct traits.
315
  SmallVector<llvm::omp::TraitProperty, 8> ConstructTraits;
316
317
public:
318
85.5k
  explicit DSAStackTy(Sema &S) : SemaRef(S) {}
319
320
  /// Sets omp_allocator_handle_t type.
321
569
  void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; }
322
  /// Gets omp_allocator_handle_t type.
323
3.42k
  QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; }
324
  /// Sets omp_alloctrait_t type.
325
28
  void setOMPAlloctraitT(QualType Ty) { OMPAlloctraitT = Ty; }
326
  /// Gets omp_alloctrait_t type.
327
78
  QualType getOMPAlloctraitT() const { return OMPAlloctraitT; }
328
  /// Sets the given default allocator.
329
  void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
330
5.12k
                    Expr *Allocator) {
331
5.12k
    OMPPredefinedAllocators[AllocatorKind] = Allocator;
332
5.12k
  }
333
  /// Returns the specified default allocator.
334
8.73k
  Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const {
335
8.73k
    return OMPPredefinedAllocators[AllocatorKind];
336
8.73k
  }
337
  /// Sets omp_depend_t type.
338
26
  void setOMPDependT(QualType Ty) { OMPDependT = Ty; }
339
  /// Gets omp_depend_t type.
340
4.36k
  QualType getOMPDependT() const { return OMPDependT; }
341
342
  /// Sets omp_event_handle_t type.
343
15
  void setOMPEventHandleT(QualType Ty) { OMPEventHandleT = Ty; }
344
  /// Gets omp_event_handle_t type.
345
254
  QualType getOMPEventHandleT() const { return OMPEventHandleT; }
346
347
5.46M
  bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
348
530k
  OpenMPClauseKind getClauseParsingMode() const {
349
530k
    assert(isClauseParsingMode() && "Must be in clause parsing mode.");
350
0
    return ClauseKindMode;
351
530k
  }
352
447k
  void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
353
354
286k
  bool isBodyComplete() const {
355
286k
    const SharingMapTy *Top = getTopOfStackOrNull();
356
286k
    return Top && 
Top->BodyComplete222k
;
357
286k
  }
358
312k
  void setBodyComplete() { getTopOfStack().BodyComplete = true; }
359
360
363k
  bool isForceVarCapturing() const { return ForceCapturing; }
361
110k
  void setForceVarCapturing(bool V) { ForceCapturing = V; }
362
363
84.1k
  void setForceCaptureByReferenceInTargetExecutable(bool V) {
364
84.1k
    ForceCaptureByReferenceInTargetExecutable = V;
365
84.1k
  }
366
882k
  bool isForceCaptureByReferenceInTargetExecutable() const {
367
882k
    return ForceCaptureByReferenceInTargetExecutable;
368
882k
  }
369
370
  void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
371
342k
            Scope *CurScope, SourceLocation Loc) {
372
342k
    assert(!IgnoredStackElements &&
373
342k
           "cannot change stack while ignoring elements");
374
342k
    if (Stack.empty() ||
375
342k
        
Stack.back().second != CurrentNonCapturingFunctionScope309k
)
376
32.9k
      Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope);
377
342k
    Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc);
378
342k
    Stack.back().first.back().DefaultAttrLoc = Loc;
379
342k
  }
380
381
342k
  void pop() {
382
342k
    assert(!IgnoredStackElements &&
383
342k
           "cannot change stack while ignoring elements");
384
0
    assert(!Stack.back().first.empty() &&
385
342k
           "Data-sharing attributes stack is empty!");
386
0
    Stack.back().first.pop_back();
387
342k
  }
388
389
  /// RAII object to temporarily leave the scope of a directive when we want to
390
  /// logically operate in its parent.
391
  class ParentDirectiveScope {
392
    DSAStackTy &Self;
393
    bool Active;
394
395
  public:
396
    ParentDirectiveScope(DSAStackTy &Self, bool Activate)
397
1.71M
        : Self(Self), Active(false) {
398
1.71M
      if (Activate)
399
42.3k
        enable();
400
1.71M
    }
401
1.71M
    ~ParentDirectiveScope() { disable(); }
402
1.71M
    void disable() {
403
1.71M
      if (Active) {
404
42.3k
        --Self.IgnoredStackElements;
405
42.3k
        Active = false;
406
42.3k
      }
407
1.71M
    }
408
42.3k
    void enable() {
409
42.3k
      if (!Active) {
410
42.3k
        ++Self.IgnoredStackElements;
411
42.3k
        Active = true;
412
42.3k
      }
413
42.3k
    }
414
  };
415
416
  /// Marks that we're started loop parsing.
417
166k
  void loopInit() {
418
166k
    assert(isOpenMPLoopDirective(getCurrentDirective()) &&
419
166k
           "Expected loop-based directive.");
420
0
    getTopOfStack().LoopStart = true;
421
166k
  }
422
  /// Start capturing of the variables in the loop context.
423
208k
  void loopStart() {
424
208k
    assert(isOpenMPLoopDirective(getCurrentDirective()) &&
425
208k
           "Expected loop-based directive.");
426
0
    getTopOfStack().LoopStart = false;
427
208k
  }
428
  /// true, if variables are captured, false otherwise.
429
1.13M
  bool isLoopStarted() const {
430
1.13M
    assert(isOpenMPLoopDirective(getCurrentDirective()) &&
431
1.13M
           "Expected loop-based directive.");
432
0
    return !getTopOfStack().LoopStart;
433
1.13M
  }
434
  /// Marks (or clears) declaration as possibly loop counter.
435
177k
  void resetPossibleLoopCounter(const Decl *D = nullptr) {
436
177k
    getTopOfStack().PossiblyLoopCounter = D ? 
D->getCanonicalDecl()43.6k
:
D134k
;
437
177k
  }
438
  /// Gets the possible loop counter decl.
439
2.46M
  const Decl *getPossiblyLoopCunter() const {
440
2.46M
    return getTopOfStack().PossiblyLoopCounter;
441
2.46M
  }
442
  /// Start new OpenMP region stack in new non-capturing function.
443
89.4k
  void pushFunction() {
444
89.4k
    assert(!IgnoredStackElements &&
445
89.4k
           "cannot change stack while ignoring elements");
446
0
    const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
447
89.4k
    assert(!isa<CapturingScopeInfo>(CurFnScope));
448
0
    CurrentNonCapturingFunctionScope = CurFnScope;
449
89.4k
  }
450
  /// Pop region stack for non-capturing function.
451
657k
  void popFunction(const FunctionScopeInfo *OldFSI) {
452
657k
    assert(!IgnoredStackElements &&
453
657k
           "cannot change stack while ignoring elements");
454
657k
    if (!Stack.empty() && 
Stack.back().second == OldFSI604k
) {
455
32.8k
      assert(Stack.back().first.empty());
456
0
      Stack.pop_back();
457
32.8k
    }
458
0
    CurrentNonCapturingFunctionScope = nullptr;
459
1.22M
    for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) {
460
1.22M
      if (!isa<CapturingScopeInfo>(FSI)) {
461
575k
        CurrentNonCapturingFunctionScope = FSI;
462
575k
        break;
463
575k
      }
464
1.22M
    }
465
657k
  }
466
467
78
  void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) {
468
78
    Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint);
469
78
  }
470
  const std::pair<const OMPCriticalDirective *, llvm::APSInt>
471
1.62k
  getCriticalWithHint(const DeclarationNameInfo &Name) const {
472
1.62k
    auto I = Criticals.find(Name.getAsString());
473
1.62k
    if (I != Criticals.end())
474
146
      return I->second;
475
1.48k
    return std::make_pair(nullptr, llvm::APSInt());
476
1.62k
  }
477
  /// If 'aligned' declaration for given variable \a D was not seen yet,
478
  /// add it and return NULL; otherwise return previous occurrence's expression
479
  /// for diagnostics.
480
  const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE);
481
  /// If 'nontemporal' declaration for given variable \a D was not seen yet,
482
  /// add it and return NULL; otherwise return previous occurrence's expression
483
  /// for diagnostics.
484
  const Expr *addUniqueNontemporal(const ValueDecl *D, const Expr *NewDE);
485
486
  /// Register specified variable as loop control variable.
487
  void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture);
488
  /// Check if the specified variable is a loop control variable for
489
  /// current region.
490
  /// \return The index of the loop control variable in the list of associated
491
  /// for-loops (from outer to inner).
492
  const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const;
493
  /// Check if the specified variable is a loop control variable for
494
  /// parent region.
495
  /// \return The index of the loop control variable in the list of associated
496
  /// for-loops (from outer to inner).
497
  const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const;
498
  /// Check if the specified variable is a loop control variable for
499
  /// current region.
500
  /// \return The index of the loop control variable in the list of associated
501
  /// for-loops (from outer to inner).
502
  const LCDeclInfo isLoopControlVariable(const ValueDecl *D,
503
                                         unsigned Level) const;
504
  /// Get the loop control variable for the I-th loop (or nullptr) in
505
  /// parent directive.
506
  const ValueDecl *getParentLoopControlVariable(unsigned I) const;
507
508
  /// Marks the specified decl \p D as used in scan directive.
509
160
  void markDeclAsUsedInScanDirective(ValueDecl *D) {
510
160
    if (SharingMapTy *Stack = getSecondOnStackOrNull())
511
160
      Stack->UsedInScanDirective.insert(D);
512
160
  }
513
514
  /// Checks if the specified declaration was used in the inner scan directive.
515
166
  bool isUsedInScanDirective(ValueDecl *D) const {
516
166
    if (const SharingMapTy *Stack = getTopOfStackOrNull())
517
166
      return Stack->UsedInScanDirective.contains(D);
518
0
    return false;
519
166
  }
520
521
  /// Adds explicit data sharing attribute to the specified declaration.
522
  void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
523
              DeclRefExpr *PrivateCopy = nullptr, unsigned Modifier = 0,
524
              bool AppliedToPointee = false);
525
526
  /// Adds additional information for the reduction items with the reduction id
527
  /// represented as an operator.
528
  void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
529
                                 BinaryOperatorKind BOK);
530
  /// Adds additional information for the reduction items with the reduction id
531
  /// represented as reduction identifier.
532
  void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
533
                                 const Expr *ReductionRef);
534
  /// Returns the location and reduction operation from the innermost parent
535
  /// region for the given \p D.
536
  const DSAVarData
537
  getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
538
                                   BinaryOperatorKind &BOK,
539
                                   Expr *&TaskgroupDescriptor) const;
540
  /// Returns the location and reduction operation from the innermost parent
541
  /// region for the given \p D.
542
  const DSAVarData
543
  getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
544
                                   const Expr *&ReductionRef,
545
                                   Expr *&TaskgroupDescriptor) const;
546
  /// Return reduction reference expression for the current taskgroup or
547
  /// parallel/worksharing directives with task reductions.
548
78.4k
  Expr *getTaskgroupReductionRef() const {
549
78.4k
    assert((getTopOfStack().Directive == OMPD_taskgroup ||
550
78.4k
            ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
551
78.4k
              isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
552
78.4k
             !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
553
78.4k
           "taskgroup reference expression requested for non taskgroup or "
554
78.4k
           "parallel/worksharing directive.");
555
0
    return getTopOfStack().TaskgroupReductionRef;
556
78.4k
  }
557
  /// Checks if the given \p VD declaration is actually a taskgroup reduction
558
  /// descriptor variable at the \p Level of OpenMP regions.
559
607k
  bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const {
560
607k
    return getStackElemAtLevel(Level).TaskgroupReductionRef &&
561
607k
           cast<DeclRefExpr>(getStackElemAtLevel(Level).TaskgroupReductionRef)
562
13.6k
                   ->getDecl() == VD;
563
607k
  }
564
565
  /// Returns data sharing attributes from top of the stack for the
566
  /// specified declaration.
567
  const DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
568
  /// Returns data-sharing attributes for the specified declaration.
569
  const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const;
570
  /// Returns data-sharing attributes for the specified declaration.
571
  const DSAVarData getImplicitDSA(ValueDecl *D, unsigned Level) const;
572
  /// Checks if the specified variables has data-sharing attributes which
573
  /// match specified \a CPred predicate in any directive which matches \a DPred
574
  /// predicate.
575
  const DSAVarData
576
  hasDSA(ValueDecl *D,
577
         const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
578
         const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
579
         bool FromParent) const;
580
  /// Checks if the specified variables has data-sharing attributes which
581
  /// match specified \a CPred predicate in any innermost directive which
582
  /// matches \a DPred predicate.
583
  const DSAVarData
584
  hasInnermostDSA(ValueDecl *D,
585
                  const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
586
                  const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
587
                  bool FromParent) const;
588
  /// Checks if the specified variables has explicit data-sharing
589
  /// attributes which match specified \a CPred predicate at the specified
590
  /// OpenMP region.
591
  bool
592
  hasExplicitDSA(const ValueDecl *D,
593
                 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
594
                 unsigned Level, bool NotLastprivate = false) const;
595
596
  /// Returns true if the directive at level \Level matches in the
597
  /// specified \a DPred predicate.
598
  bool hasExplicitDirective(
599
      const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
600
      unsigned Level) const;
601
602
  /// Finds a directive which matches specified \a DPred predicate.
603
  bool hasDirective(
604
      const llvm::function_ref<bool(
605
          OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)>
606
          DPred,
607
      bool FromParent) const;
608
609
  /// Returns currently analyzed directive.
610
14.2M
  OpenMPDirectiveKind getCurrentDirective() const {
611
14.2M
    const SharingMapTy *Top = getTopOfStackOrNull();
612
14.2M
    return Top ? 
Top->Directive14.0M
:
OMPD_unknown230k
;
613
14.2M
  }
614
  /// Returns directive kind at specified level.
615
1.73M
  OpenMPDirectiveKind getDirective(unsigned Level) const {
616
1.73M
    assert(!isStackEmpty() && "No directive at specified level.");
617
0
    return getStackElemAtLevel(Level).Directive;
618
1.73M
  }
619
  /// Returns the capture region at the specified level.
620
  OpenMPDirectiveKind getCaptureRegion(unsigned Level,
621
10.7k
                                       unsigned OpenMPCaptureLevel) const {
622
10.7k
    SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
623
10.7k
    getOpenMPCaptureRegions(CaptureRegions, getDirective(Level));
624
10.7k
    return CaptureRegions[OpenMPCaptureLevel];
625
10.7k
  }
626
  /// Returns parent directive.
627
528k
  OpenMPDirectiveKind getParentDirective() const {
628
528k
    const SharingMapTy *Parent = getSecondOnStackOrNull();
629
528k
    return Parent ? 
Parent->Directive359k
:
OMPD_unknown169k
;
630
528k
  }
631
632
  /// Add requires decl to internal vector
633
155
  void addRequiresDecl(OMPRequiresDecl *RD) { RequiresDecls.push_back(RD); }
634
635
  /// Checks if the defined 'requires' directive has specified type of clause.
636
363k
  template <typename ClauseType> bool hasRequiresDeclWithClause() const {
637
363k
    return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
638
19.6k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
639
19.6k
        return isa<ClauseType>(C);
640
19.6k
      });
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPDynamicAllocatorsClause>() const::'lambda'(clang::OMPRequiresDecl const*)::operator()(clang::OMPRequiresDecl const*) const::'lambda'(clang::OMPClause const*)::operator()(clang::OMPClause const*) const
Line
Count
Source
638
4.95k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
639
4.95k
        return isa<ClauseType>(C);
640
4.95k
      });
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPUnifiedSharedMemoryClause>() const::'lambda'(clang::OMPRequiresDecl const*)::operator()(clang::OMPRequiresDecl const*) const::'lambda'(clang::OMPClause const*)::operator()(clang::OMPClause const*) const
Line
Count
Source
638
4.90k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
639
4.90k
        return isa<ClauseType>(C);
640
4.90k
      });
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPUnifiedAddressClause>() const::'lambda'(clang::OMPRequiresDecl const*)::operator()(clang::OMPRequiresDecl const*) const::'lambda'(clang::OMPClause const*)::operator()(clang::OMPClause const*) const
Line
Count
Source
638
4.88k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
639
4.88k
        return isa<ClauseType>(C);
640
4.88k
      });
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPReverseOffloadClause>() const::'lambda'(clang::OMPRequiresDecl const*)::operator()(clang::OMPRequiresDecl const*) const::'lambda'(clang::OMPClause const*)::operator()(clang::OMPClause const*) const
Line
Count
Source
638
4.88k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
639
4.88k
        return isa<ClauseType>(C);
640
4.88k
      });
641
19.6k
    });
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPDynamicAllocatorsClause>() const::'lambda'(clang::OMPRequiresDecl const*)::operator()(clang::OMPRequiresDecl const*) const
Line
Count
Source
637
4.95k
    return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
638
4.95k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
639
4.95k
        return isa<ClauseType>(C);
640
4.95k
      });
641
4.95k
    });
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPUnifiedSharedMemoryClause>() const::'lambda'(clang::OMPRequiresDecl const*)::operator()(clang::OMPRequiresDecl const*) const
Line
Count
Source
637
4.90k
    return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
638
4.90k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
639
4.90k
        return isa<ClauseType>(C);
640
4.90k
      });
641
4.90k
    });
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPUnifiedAddressClause>() const::'lambda'(clang::OMPRequiresDecl const*)::operator()(clang::OMPRequiresDecl const*) const
Line
Count
Source
637
4.88k
    return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
638
4.88k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
639
4.88k
        return isa<ClauseType>(C);
640
4.88k
      });
641
4.88k
    });
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPReverseOffloadClause>() const::'lambda'(clang::OMPRequiresDecl const*)::operator()(clang::OMPRequiresDecl const*) const
Line
Count
Source
637
4.88k
    return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
638
4.88k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
639
4.88k
        return isa<ClauseType>(C);
640
4.88k
      });
641
4.88k
    });
642
363k
  }
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPDynamicAllocatorsClause>() const
Line
Count
Source
636
90.9k
  template <typename ClauseType> bool hasRequiresDeclWithClause() const {
637
90.9k
    return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
638
90.9k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
639
90.9k
        return isa<ClauseType>(C);
640
90.9k
      });
641
90.9k
    });
642
90.9k
  }
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPUnifiedSharedMemoryClause>() const
Line
Count
Source
636
92.7k
  template <typename ClauseType> bool hasRequiresDeclWithClause() const {
637
92.7k
    return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
638
92.7k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
639
92.7k
        return isa<ClauseType>(C);
640
92.7k
      });
641
92.7k
    });
642
92.7k
  }
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPUnifiedAddressClause>() const
Line
Count
Source
636
90.0k
  template <typename ClauseType> bool hasRequiresDeclWithClause() const {
637
90.0k
    return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
638
90.0k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
639
90.0k
        return isa<ClauseType>(C);
640
90.0k
      });
641
90.0k
    });
642
90.0k
  }
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPReverseOffloadClause>() const
Line
Count
Source
636
90.0k
  template <typename ClauseType> bool hasRequiresDeclWithClause() const {
637
90.0k
    return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
638
90.0k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
639
90.0k
        return isa<ClauseType>(C);
640
90.0k
      });
641
90.0k
    });
642
90.0k
  }
643
644
  /// Checks for a duplicate clause amongst previously declared requires
645
  /// directives
646
170
  bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const {
647
170
    bool IsDuplicate = false;
648
174
    for (OMPClause *CNew : ClauseList) {
649
174
      for (const OMPRequiresDecl *D : RequiresDecls) {
650
135
        for (const OMPClause *CPrev : D->clauselists()) {
651
135
          if (CNew->getClauseKind() == CPrev->getClauseKind()) {
652
19
            SemaRef.Diag(CNew->getBeginLoc(),
653
19
                         diag::err_omp_requires_clause_redeclaration)
654
19
                << getOpenMPClauseName(CNew->getClauseKind());
655
19
            SemaRef.Diag(CPrev->getBeginLoc(),
656
19
                         diag::note_omp_requires_previous_clause)
657
19
                << getOpenMPClauseName(CPrev->getClauseKind());
658
19
            IsDuplicate = true;
659
19
          }
660
135
        }
661
135
      }
662
174
    }
663
170
    return IsDuplicate;
664
170
  }
665
666
  /// Add location of previously encountered target to internal vector
667
85.1k
  void addTargetDirLocation(SourceLocation LocStart) {
668
85.1k
    TargetLocations.push_back(LocStart);
669
85.1k
  }
670
671
  /// Add location for the first encountered atomicc directive.
672
5.27k
  void addAtomicDirectiveLoc(SourceLocation Loc) {
673
5.27k
    if (AtomicLocation.isInvalid())
674
78
      AtomicLocation = Loc;
675
5.27k
  }
676
677
  /// Returns the location of the first encountered atomic directive in the
678
  /// module.
679
170
  SourceLocation getAtomicDirectiveLoc() const { return AtomicLocation; }
680
681
  // Return previously encountered target region locations.
682
170
  ArrayRef<SourceLocation> getEncounteredTargetLocs() const {
683
170
    return TargetLocations;
684
170
  }
685
686
  /// Set default data sharing attribute to none.
687
1.06k
  void setDefaultDSANone(SourceLocation Loc) {
688
1.06k
    getTopOfStack().DefaultAttr = DSA_none;
689
1.06k
    getTopOfStack().DefaultAttrLoc = Loc;
690
1.06k
  }
691
  /// Set default data sharing attribute to shared.
692
711
  void setDefaultDSAShared(SourceLocation Loc) {
693
711
    getTopOfStack().DefaultAttr = DSA_shared;
694
711
    getTopOfStack().DefaultAttrLoc = Loc;
695
711
  }
696
  /// Set default data sharing attribute to firstprivate.
697
94
  void setDefaultDSAFirstPrivate(SourceLocation Loc) {
698
94
    getTopOfStack().DefaultAttr = DSA_firstprivate;
699
94
    getTopOfStack().DefaultAttrLoc = Loc;
700
94
  }
701
  /// Set default data mapping attribute to Modifier:Kind
702
  void setDefaultDMAAttr(OpenMPDefaultmapClauseModifier M,
703
3.23k
                         OpenMPDefaultmapClauseKind Kind, SourceLocation Loc) {
704
3.23k
    DefaultmapInfo &DMI = getTopOfStack().DefaultmapMap[Kind];
705
3.23k
    DMI.ImplicitBehavior = M;
706
3.23k
    DMI.SLoc = Loc;
707
3.23k
  }
708
  /// Check whether the implicit-behavior has been set in defaultmap
709
1.96k
  bool checkDefaultmapCategory(OpenMPDefaultmapClauseKind VariableCategory) {
710
1.96k
    if (VariableCategory == OMPC_DEFAULTMAP_unknown)
711
600
      return getTopOfStack()
712
600
                     .DefaultmapMap[OMPC_DEFAULTMAP_aggregate]
713
600
                     .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown ||
714
600
             getTopOfStack()
715
600
                     .DefaultmapMap[OMPC_DEFAULTMAP_scalar]
716
600
                     .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown ||
717
600
             getTopOfStack()
718
600
                     .DefaultmapMap[OMPC_DEFAULTMAP_pointer]
719
600
                     .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown;
720
1.36k
    return getTopOfStack().DefaultmapMap[VariableCategory].ImplicitBehavior !=
721
1.36k
           OMPC_DEFAULTMAP_MODIFIER_unknown;
722
1.96k
  }
723
724
2.49k
  ArrayRef<llvm::omp::TraitProperty> getConstructTraits() {
725
2.49k
    return ConstructTraits;
726
2.49k
  }
727
  void handleConstructTrait(ArrayRef<llvm::omp::TraitProperty> Traits,
728
672k
                            bool ScopeEntry) {
729
672k
    if (ScopeEntry)
730
336k
      ConstructTraits.append(Traits.begin(), Traits.end());
731
336k
    else
732
536k
      
for (llvm::omp::TraitProperty Trait : llvm::reverse(Traits))336k
{
733
536k
        llvm::omp::TraitProperty Top = ConstructTraits.pop_back_val();
734
536k
        assert(Top == Trait && "Something left a trait on the stack!");
735
0
        (void)Trait;
736
536k
        (void)Top;
737
536k
      }
738
672k
  }
739
740
0
  DefaultDataSharingAttributes getDefaultDSA(unsigned Level) const {
741
0
    return getStackSize() <= Level ? DSA_unspecified
742
0
                                   : getStackElemAtLevel(Level).DefaultAttr;
743
0
  }
744
1.91M
  DefaultDataSharingAttributes getDefaultDSA() const {
745
1.91M
    return isStackEmpty() ? 
DSA_unspecified0
: getTopOfStack().DefaultAttr;
746
1.91M
  }
747
518
  SourceLocation getDefaultDSALocation() const {
748
518
    return isStackEmpty() ? 
SourceLocation()0
: getTopOfStack().DefaultAttrLoc;
749
518
  }
750
  OpenMPDefaultmapClauseModifier
751
183k
  getDefaultmapModifier(OpenMPDefaultmapClauseKind Kind) const {
752
183k
    return isStackEmpty()
753
183k
               ? 
OMPC_DEFAULTMAP_MODIFIER_unknown0
754
183k
               : getTopOfStack().DefaultmapMap[Kind].ImplicitBehavior;
755
183k
  }
756
  OpenMPDefaultmapClauseModifier
757
  getDefaultmapModifierAtLevel(unsigned Level,
758
760k
                               OpenMPDefaultmapClauseKind Kind) const {
759
760k
    return getStackElemAtLevel(Level).DefaultmapMap[Kind].ImplicitBehavior;
760
760k
  }
761
  bool isDefaultmapCapturedByRef(unsigned Level,
762
577k
                                 OpenMPDefaultmapClauseKind Kind) const {
763
577k
    OpenMPDefaultmapClauseModifier M =
764
577k
        getDefaultmapModifierAtLevel(Level, Kind);
765
577k
    if (Kind == OMPC_DEFAULTMAP_scalar || 
Kind == OMPC_DEFAULTMAP_pointer110k
) {
766
577k
      return (M == OMPC_DEFAULTMAP_MODIFIER_alloc) ||
767
577k
             
(M == OMPC_DEFAULTMAP_MODIFIER_to)577k
||
768
577k
             
(M == OMPC_DEFAULTMAP_MODIFIER_from)577k
||
769
577k
             
(M == OMPC_DEFAULTMAP_MODIFIER_tofrom)577k
;
770
577k
    }
771
0
    return true;
772
577k
  }
773
  static bool mustBeFirstprivateBase(OpenMPDefaultmapClauseModifier M,
774
248k
                                     OpenMPDefaultmapClauseKind Kind) {
775
248k
    switch (Kind) {
776
164k
    case OMPC_DEFAULTMAP_scalar:
777
192k
    case OMPC_DEFAULTMAP_pointer:
778
192k
      return (M == OMPC_DEFAULTMAP_MODIFIER_unknown) ||
779
192k
             
(M == OMPC_DEFAULTMAP_MODIFIER_firstprivate)1.73k
||
780
192k
             
(M == OMPC_DEFAULTMAP_MODIFIER_default)1.68k
;
781
55.1k
    case OMPC_DEFAULTMAP_aggregate:
782
55.1k
      return M == OMPC_DEFAULTMAP_MODIFIER_firstprivate;
783
0
    default:
784
0
      break;
785
248k
    }
786
0
    llvm_unreachable("Unexpected OpenMPDefaultmapClauseKind enum");
787
0
  }
788
  bool mustBeFirstprivateAtLevel(unsigned Level,
789
182k
                                 OpenMPDefaultmapClauseKind Kind) const {
790
182k
    OpenMPDefaultmapClauseModifier M =
791
182k
        getDefaultmapModifierAtLevel(Level, Kind);
792
182k
    return mustBeFirstprivateBase(M, Kind);
793
182k
  }
794
65.2k
  bool mustBeFirstprivate(OpenMPDefaultmapClauseKind Kind) const {
795
65.2k
    OpenMPDefaultmapClauseModifier M = getDefaultmapModifier(Kind);
796
65.2k
    return mustBeFirstprivateBase(M, Kind);
797
65.2k
  }
798
799
  /// Checks if the specified variable is a threadprivate.
800
2.67M
  bool isThreadPrivate(VarDecl *D) {
801
2.67M
    const DSAVarData DVar = getTopDSA(D, false);
802
2.67M
    return isOpenMPThreadPrivate(DVar.CKind);
803
2.67M
  }
804
805
  /// Marks current region as ordered (it has an 'ordered' clause).
806
  void setOrderedRegion(bool IsOrdered, const Expr *Param,
807
1.18k
                        OMPOrderedClause *Clause) {
808
1.18k
    if (IsOrdered)
809
1.18k
      getTopOfStack().OrderedRegion.emplace(Param, Clause);
810
0
    else
811
0
      getTopOfStack().OrderedRegion.reset();
812
1.18k
  }
813
  /// Returns true, if region is ordered (has associated 'ordered' clause),
814
  /// false - otherwise.
815
105k
  bool isOrderedRegion() const {
816
105k
    if (const SharingMapTy *Top = getTopOfStackOrNull())
817
105k
      return Top->OrderedRegion.hasValue();
818
0
    return false;
819
105k
  }
820
  /// Returns optional parameter for the ordered region.
821
2.07k
  std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const {
822
2.07k
    if (const SharingMapTy *Top = getTopOfStackOrNull())
823
2.07k
      if (Top->OrderedRegion.hasValue())
824
2.07k
        return Top->OrderedRegion.getValue();
825
0
    return std::make_pair(nullptr, nullptr);
826
2.07k
  }
827
  /// Returns true, if parent region is ordered (has associated
828
  /// 'ordered' clause), false - otherwise.
829
2.12k
  bool isParentOrderedRegion() const {
830
2.12k
    if (const SharingMapTy *Parent = getSecondOnStackOrNull())
831
2.11k
      return Parent->OrderedRegion.hasValue();
832
12
    return false;
833
2.12k
  }
834
  /// Returns optional parameter for the ordered region.
835
  std::pair<const Expr *, OMPOrderedClause *>
836
2.59k
  getParentOrderedRegionParam() const {
837
2.59k
    if (const SharingMapTy *Parent = getSecondOnStackOrNull())
838
2.02k
      if (Parent->OrderedRegion.hasValue())
839
2.01k
        return Parent->OrderedRegion.getValue();
840
584
    return std::make_pair(nullptr, nullptr);
841
2.59k
  }
842
  /// Marks current region as nowait (it has a 'nowait' clause).
843
1.52k
  void setNowaitRegion(bool IsNowait = true) {
844
1.52k
    getTopOfStack().NowaitRegion = IsNowait;
845
1.52k
  }
846
  /// Returns true, if parent region is nowait (has associated
847
  /// 'nowait' clause), false - otherwise.
848
803
  bool isParentNowaitRegion() const {
849
803
    if (const SharingMapTy *Parent = getSecondOnStackOrNull())
850
803
      return Parent->NowaitRegion;
851
0
    return false;
852
803
  }
853
  /// Marks parent region as cancel region.
854
1.47k
  void setParentCancelRegion(bool Cancel = true) {
855
1.47k
    if (SharingMapTy *Parent = getSecondOnStackOrNull())
856
1.47k
      Parent->CancelRegion |= Cancel;
857
1.47k
  }
858
  /// Return true if current region has inner cancel construct.
859
89.5k
  bool isCancelRegion() const {
860
89.5k
    const SharingMapTy *Top = getTopOfStackOrNull();
861
89.5k
    return Top ? Top->CancelRegion : 
false0
;
862
89.5k
  }
863
864
  /// Mark that parent region already has scan directive.
865
98
  void setParentHasScanDirective(SourceLocation Loc) {
866
98
    if (SharingMapTy *Parent = getSecondOnStackOrNull())
867
98
      Parent->PrevScanLocation = Loc;
868
98
  }
869
  /// Return true if current region has inner cancel construct.
870
102
  bool doesParentHasScanDirective() const {
871
102
    const SharingMapTy *Top = getSecondOnStackOrNull();
872
102
    return Top ? Top->PrevScanLocation.isValid() : 
false0
;
873
102
  }
874
  /// Return true if current region has inner cancel construct.
875
4
  SourceLocation getParentScanDirectiveLoc() const {
876
4
    const SharingMapTy *Top = getSecondOnStackOrNull();
877
4
    return Top ? Top->PrevScanLocation : 
SourceLocation()0
;
878
4
  }
879
  /// Mark that parent region already has ordered directive.
880
878
  void setParentHasOrderedDirective(SourceLocation Loc) {
881
878
    if (SharingMapTy *Parent = getSecondOnStackOrNull())
882
319
      Parent->PrevOrderedLocation = Loc;
883
878
  }
884
  /// Return true if current region has inner ordered construct.
885
890
  bool doesParentHasOrderedDirective() const {
886
890
    const SharingMapTy *Top = getSecondOnStackOrNull();
887
890
    return Top ? 
Top->PrevOrderedLocation.isValid()331
:
false559
;
888
890
  }
889
  /// Returns the location of the previously specified ordered directive.
890
12
  SourceLocation getParentOrderedDirectiveLoc() const {
891
12
    const SharingMapTy *Top = getSecondOnStackOrNull();
892
12
    return Top ? Top->PrevOrderedLocation : 
SourceLocation()0
;
893
12
  }
894
895
  /// Set collapse value for the region.
896
169k
  void setAssociatedLoops(unsigned Val) {
897
169k
    getTopOfStack().AssociatedLoops = Val;
898
169k
    if (Val > 1)
899
4.38k
      getTopOfStack().HasMutipleLoops = true;
900
169k
  }
901
  /// Return collapse value for region.
902
2.52M
  unsigned getAssociatedLoops() const {
903
2.52M
    const SharingMapTy *Top = getTopOfStackOrNull();
904
2.52M
    return Top ? 
Top->AssociatedLoops2.52M
:
0461
;
905
2.52M
  }
906
  /// Returns true if the construct is associated with multiple loops.
907
95.3k
  bool hasMutipleLoops() const {
908
95.3k
    const SharingMapTy *Top = getTopOfStackOrNull();
909
95.3k
    return Top ? Top->HasMutipleLoops : 
false0
;
910
95.3k
  }
911
912
  /// Marks current target region as one with closely nested teams
913
  /// region.
914
36.6k
  void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
915
36.6k
    if (SharingMapTy *Parent = getSecondOnStackOrNull())
916
36.4k
      Parent->InnerTeamsRegionLoc = TeamsRegionLoc;
917
36.6k
  }
918
  /// Returns true, if current region has closely nested teams region.
919
53.6k
  bool hasInnerTeamsRegion() const {
920
53.6k
    return getInnerTeamsRegionLoc().isValid();
921
53.6k
  }
922
  /// Returns location of the nested teams region (if any).
923
53.7k
  SourceLocation getInnerTeamsRegionLoc() const {
924
53.7k
    const SharingMapTy *Top = getTopOfStackOrNull();
925
53.7k
    return Top ? Top->InnerTeamsRegionLoc : 
SourceLocation()0
;
926
53.7k
  }
927
928
2.74M
  Scope *getCurScope() const {
929
2.74M
    const SharingMapTy *Top = getTopOfStackOrNull();
930
2.74M
    return Top ? 
Top->CurScope2.74M
:
nullptr8
;
931
2.74M
  }
932
336k
  void setContext(DeclContext *DC) { getTopOfStack().Context = DC; }
933
617k
  SourceLocation getConstructLoc() const {
934
617k
    const SharingMapTy *Top = getTopOfStackOrNull();
935
617k
    return Top ? Top->ConstructLoc : 
SourceLocation()0
;
936
617k
  }
937
938
  /// Do the check specified in \a Check to all component lists and return true
939
  /// if any issue is found.
940
  bool checkMappableExprComponentListsForDecl(
941
      const ValueDecl *VD, bool CurrentRegionOnly,
942
      const llvm::function_ref<
943
          bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
944
               OpenMPClauseKind)>
945
277k
          Check) const {
946
277k
    if (isStackEmpty())
947
0
      return false;
948
277k
    auto SI = begin();
949
277k
    auto SE = end();
950
951
277k
    if (SI == SE)
952
0
      return false;
953
954
277k
    if (CurrentRegionOnly)
955
242k
      SE = std::next(SI);
956
34.6k
    else
957
34.6k
      std::advance(SI, 1);
958
959
529k
    for (; SI != SE; 
++SI252k
) {
960
260k
      auto MI = SI->MappedExprComponents.find(VD);
961
260k
      if (MI != SI->MappedExprComponents.end())
962
12.7k
        for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
963
12.7k
             MI->second.Components)
964
12.9k
          if (Check(L, MI->second.Kind))
965
8.28k
            return true;
966
260k
    }
967
269k
    return false;
968
277k
  }
969
970
  /// Do the check specified in \a Check to all component lists at a given level
971
  /// and return true if any issue is found.
972
  bool checkMappableExprComponentListsForDeclAtLevel(
973
      const ValueDecl *VD, unsigned Level,
974
      const llvm::function_ref<
975
          bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
976
               OpenMPClauseKind)>
977
1.14M
          Check) const {
978
1.14M
    if (getStackSize() <= Level)
979
0
      return false;
980
981
1.14M
    const SharingMapTy &StackElem = getStackElemAtLevel(Level);
982
1.14M
    auto MI = StackElem.MappedExprComponents.find(VD);
983
1.14M
    if (MI != StackElem.MappedExprComponents.end())
984
36.7k
      for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
985
36.7k
           MI->second.Components)
986
36.8k
        if (Check(L, MI->second.Kind))
987
19.4k
          return true;
988
1.12M
    return false;
989
1.14M
  }
990
991
  /// Create a new mappable expression component list associated with a given
992
  /// declaration and initialize it with the provided list of components.
993
  void addMappableExpressionComponents(
994
      const ValueDecl *VD,
995
      OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
996
50.1k
      OpenMPClauseKind WhereFoundClauseKind) {
997
50.1k
    MappedExprComponentTy &MEC = getTopOfStack().MappedExprComponents[VD];
998
    // Create new entry and append the new components there.
999
50.1k
    MEC.Components.resize(MEC.Components.size() + 1);
1000
50.1k
    MEC.Components.back().append(Components.begin(), Components.end());
1001
50.1k
    MEC.Kind = WhereFoundClauseKind;
1002
50.1k
  }
1003
1004
937k
  unsigned getNestingLevel() const {
1005
937k
    assert(!isStackEmpty());
1006
0
    return getStackSize() - 1;
1007
937k
  }
1008
  void addDoacrossDependClause(OMPDependClause *C,
1009
499
                               const OperatorOffsetTy &OpsOffs) {
1010
499
    SharingMapTy *Parent = getSecondOnStackOrNull();
1011
499
    assert(Parent && isOpenMPWorksharingDirective(Parent->Directive));
1012
0
    Parent->DoacrossDepends.try_emplace(C, OpsOffs);
1013
499
  }
1014
  llvm::iterator_range<DoacrossDependMapTy::const_iterator>
1015
855
  getDoacrossDependClauses() const {
1016
855
    const SharingMapTy &StackElem = getTopOfStack();
1017
855
    if (isOpenMPWorksharingDirective(StackElem.Directive)) {
1018
855
      const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends;
1019
855
      return llvm::make_range(Ref.begin(), Ref.end());
1020
855
    }
1021
0
    return llvm::make_range(StackElem.DoacrossDepends.end(),
1022
0
                            StackElem.DoacrossDepends.end());
1023
855
  }
1024
1025
  // Store types of classes which have been explicitly mapped
1026
97
  void addMappedClassesQualTypes(QualType QT) {
1027
97
    SharingMapTy &StackElem = getTopOfStack();
1028
97
    StackElem.MappedClassesQualTypes.insert(QT);
1029
97
  }
1030
1031
  // Return set of mapped classes types
1032
1.44k
  bool isClassPreviouslyMapped(QualType QT) const {
1033
1.44k
    const SharingMapTy &StackElem = getTopOfStack();
1034
1.44k
    return StackElem.MappedClassesQualTypes.contains(QT);
1035
1.44k
  }
1036
1037
  /// Adds global declare target to the parent target region.
1038
8
  void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) {
1039
8
    assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
1040
8
               E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link &&
1041
8
           "Expected declare target link global.");
1042
16
    for (auto &Elem : *this) {
1043
16
      if (isOpenMPTargetExecutionDirective(Elem.Directive)) {
1044
8
        Elem.DeclareTargetLinkVarDecls.push_back(E);
1045
8
        return;
1046
8
      }
1047
16
    }
1048
8
  }
1049
1050
  /// Returns the list of globals with declare target link if current directive
1051
  /// is target.
1052
93.1k
  ArrayRef<DeclRefExpr *> getLinkGlobals() const {
1053
93.1k
    assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) &&
1054
93.1k
           "Expected target executable directive.");
1055
0
    return getTopOfStack().DeclareTargetLinkVarDecls;
1056
93.1k
  }
1057
1058
  /// Adds list of allocators expressions.
1059
985
  void addInnerAllocatorExpr(Expr *E) {
1060
985
    getTopOfStack().InnerUsedAllocators.push_back(E);
1061
985
  }
1062
  /// Return list of used allocators.
1063
313k
  ArrayRef<Expr *> getInnerAllocators() const {
1064
313k
    return getTopOfStack().InnerUsedAllocators;
1065
313k
  }
1066
  /// Marks the declaration as implicitly firstprivate nin the task-based
1067
  /// regions.
1068
115k
  void addImplicitTaskFirstprivate(unsigned Level, Decl *D) {
1069
115k
    getStackElemAtLevel(Level).ImplicitTaskFirstprivates.insert(D);
1070
115k
  }
1071
  /// Checks if the decl is implicitly firstprivate in the task-based region.
1072
273k
  bool isImplicitTaskFirstprivate(Decl *D) const {
1073
273k
    return getTopOfStack().ImplicitTaskFirstprivates.contains(D);
1074
273k
  }
1075
1076
  /// Marks decl as used in uses_allocators clause as the allocator.
1077
324
  void addUsesAllocatorsDecl(const Decl *D, UsesAllocatorsDeclKind Kind) {
1078
324
    getTopOfStack().UsesAllocatorsDecls.try_emplace(D, Kind);
1079
324
  }
1080
  /// Checks if specified decl is used in uses allocator clause as the
1081
  /// allocator.
1082
  Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(unsigned Level,
1083
1.99M
                                                        const Decl *D) const {
1084
1.99M
    const SharingMapTy &StackElem = getTopOfStack();
1085
1.99M
    auto I = StackElem.UsesAllocatorsDecls.find(D);
1086
1.99M
    if (I == StackElem.UsesAllocatorsDecls.end())
1087
1.99M
      return None;
1088
564
    return I->getSecond();
1089
1.99M
  }
1090
431k
  Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(const Decl *D) const {
1091
431k
    const SharingMapTy &StackElem = getTopOfStack();
1092
431k
    auto I = StackElem.UsesAllocatorsDecls.find(D);
1093
431k
    if (I == StackElem.UsesAllocatorsDecls.end())
1094
430k
      return None;
1095
260
    return I->getSecond();
1096
431k
  }
1097
1098
332
  void addDeclareMapperVarRef(Expr *Ref) {
1099
332
    SharingMapTy &StackElem = getTopOfStack();
1100
332
    StackElem.DeclareMapperVar = Ref;
1101
332
  }
1102
1.32M
  const Expr *getDeclareMapperVarRef() const {
1103
1.32M
    const SharingMapTy *Top = getTopOfStackOrNull();
1104
1.32M
    return Top ? 
Top->DeclareMapperVar1.21M
:
nullptr112k
;
1105
1.32M
  }
1106
};
1107
1108
8.24M
bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) {
1109
8.24M
  return isOpenMPParallelDirective(DKind) || 
isOpenMPTeamsDirective(DKind)7.20M
;
1110
8.24M
}
1111
1112
7.70M
bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) {
1113
7.70M
  return isImplicitTaskingRegion(DKind) || 
isOpenMPTaskingDirective(DKind)6.25M
||
1114
7.70M
         
DKind == OMPD_unknown4.92M
;
1115
7.70M
}
1116
1117
} // namespace
1118
1119
725k
static const Expr *getExprAsWritten(const Expr *E) {
1120
725k
  if (const auto *FE = dyn_cast<FullExpr>(E))
1121
211
    E = FE->getSubExpr();
1122
1123
725k
  if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
1124
0
    E = MTE->getSubExpr();
1125
1126
725k
  while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
1127
18
    E = Binder->getSubExpr();
1128
1129
725k
  if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
1130
220k
    E = ICE->getSubExprAsWritten();
1131
725k
  return E->IgnoreParens();
1132
725k
}
1133
1134
291k
static Expr *getExprAsWritten(Expr *E) {
1135
291k
  return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E)));
1136
291k
}
1137
1138
20.1M
static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
1139
20.1M
  if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
1140
140k
    if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
1141
31.7k
      D = ME->getMemberDecl();
1142
20.1M
  const auto *VD = dyn_cast<VarDecl>(D);
1143
20.1M
  const auto *FD = dyn_cast<FieldDecl>(D);
1144
20.1M
  if (VD != nullptr) {
1145
20.0M
    VD = VD->getCanonicalDecl();
1146
20.0M
    D = VD;
1147
20.0M
  } else {
1148
117k
    assert(FD);
1149
0
    FD = FD->getCanonicalDecl();
1150
117k
    D = FD;
1151
117k
  }
1152
0
  return D;
1153
20.1M
}
1154
1155
11.0M
static ValueDecl *getCanonicalDecl(ValueDecl *D) {
1156
11.0M
  return const_cast<ValueDecl *>(
1157
11.0M
      getCanonicalDecl(const_cast<const ValueDecl *>(D)));
1158
11.0M
}
1159
1160
DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter,
1161
4.11M
                                          ValueDecl *D) const {
1162
4.11M
  D = getCanonicalDecl(D);
1163
4.11M
  auto *VD = dyn_cast<VarDecl>(D);
1164
4.11M
  const auto *FD = dyn_cast<FieldDecl>(D);
1165
4.11M
  DSAVarData DVar;
1166
4.11M
  if (Iter == end()) {
1167
    // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1168
    // in a region but not in construct]
1169
    //  File-scope or namespace-scope variables referenced in called routines
1170
    //  in the region are shared unless they appear in a threadprivate
1171
    //  directive.
1172
1.04M
    if (VD && 
!VD->isFunctionOrMethodVarDecl()1.04M
&&
!isa<ParmVarDecl>(VD)694k
)
1173
509k
      DVar.CKind = OMPC_shared;
1174
1175
    // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
1176
    // in a region but not in construct]
1177
    //  Variables with static storage duration that are declared in called
1178
    //  routines in the region are shared.
1179
1.04M
    if (VD && 
VD->hasGlobalStorage()1.04M
)
1180
513k
      DVar.CKind = OMPC_shared;
1181
1182
    // Non-static data members are shared by default.
1183
1.04M
    if (FD)
1184
3.29k
      DVar.CKind = OMPC_shared;
1185
1186
1.04M
    return DVar;
1187
1.04M
  }
1188
1189
  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1190
  // in a Construct, C/C++, predetermined, p.1]
1191
  // Variables with automatic storage duration that are declared in a scope
1192
  // inside the construct are private.
1193
3.06M
  if (VD && 
isOpenMPLocal(VD, Iter)3.05M
&&
VD->isLocalVarDecl()2.77k
&&
1194
3.06M
      
(2.68k
VD->getStorageClass() == SC_Auto2.68k
||
VD->getStorageClass() == SC_None2.68k
)) {
1195
2.68k
    DVar.CKind = OMPC_private;
1196
2.68k
    return DVar;
1197
2.68k
  }
1198
1199
3.06M
  DVar.DKind = Iter->Directive;
1200
  // Explicitly specified attributes and local variables with predetermined
1201
  // attributes.
1202
3.06M
  if (Iter->SharingMap.count(D)) {
1203
31.1k
    const DSAInfo &Data = Iter->SharingMap.lookup(D);
1204
31.1k
    DVar.RefExpr = Data.RefExpr.getPointer();
1205
31.1k
    DVar.PrivateCopy = Data.PrivateCopy;
1206
31.1k
    DVar.CKind = Data.Attributes;
1207
31.1k
    DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1208
31.1k
    DVar.Modifier = Data.Modifier;
1209
31.1k
    DVar.AppliedToPointee = Data.AppliedToPointee;
1210
31.1k
    return DVar;
1211
31.1k
  }
1212
1213
  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1214
  // in a Construct, C/C++, implicitly determined, p.1]
1215
  //  In a parallel or task construct, the data-sharing attributes of these
1216
  //  variables are determined by the default clause, if present.
1217
3.03M
  switch (Iter->DefaultAttr) {
1218
1.68k
  case DSA_shared:
1219
1.68k
    DVar.CKind = OMPC_shared;
1220
1.68k
    DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1221
1.68k
    return DVar;
1222
1.05k
  case DSA_none:
1223
1.05k
    return DVar;
1224
342
  case DSA_firstprivate:
1225
342
    if (VD->getStorageDuration() == SD_Static &&
1226
342
        
VD->getDeclContext()->isFileContext()328
) {
1227
296
      DVar.CKind = OMPC_unknown;
1228
296
    } else {
1229
46
      DVar.CKind = OMPC_firstprivate;
1230
46
    }
1231
342
    DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1232
342
    return DVar;
1233
3.02M
  case DSA_unspecified:
1234
    // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1235
    // in a Construct, implicitly determined, p.2]
1236
    //  In a parallel construct, if no default clause is present, these
1237
    //  variables are shared.
1238
3.02M
    DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1239
3.02M
    if ((isOpenMPParallelDirective(DVar.DKind) &&
1240
3.02M
         
!isOpenMPTaskLoopDirective(DVar.DKind)261k
) ||
1241
3.02M
        
isOpenMPTeamsDirective(DVar.DKind)2.80M
) {
1242
390k
      DVar.CKind = OMPC_shared;
1243
390k
      return DVar;
1244
390k
    }
1245
1246
    // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1247
    // in a Construct, implicitly determined, p.4]
1248
    //  In a task construct, if no default clause is present, a variable that in
1249
    //  the enclosing context is determined to be shared by all implicit tasks
1250
    //  bound to the current team is shared.
1251
2.63M
    if (isOpenMPTaskingDirective(DVar.DKind)) {
1252
1.24M
      DSAVarData DVarTemp;
1253
1.24M
      const_iterator I = Iter, E = end();
1254
1.73M
      do {
1255
1.73M
        ++I;
1256
        // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
1257
        // Referenced in a Construct, implicitly determined, p.6]
1258
        //  In a task construct, if no default clause is present, a variable
1259
        //  whose data-sharing attribute is not determined by the rules above is
1260
        //  firstprivate.
1261
1.73M
        DVarTemp = getDSA(I, D);
1262
1.73M
        if (DVarTemp.CKind != OMPC_shared) {
1263
706k
          DVar.RefExpr = nullptr;
1264
706k
          DVar.CKind = OMPC_firstprivate;
1265
706k
          return DVar;
1266
706k
        }
1267
1.73M
      } while (
I != E1.02M
&&
!isImplicitTaskingRegion(I->Directive)539k
);
1268
542k
      DVar.CKind =
1269
542k
          (DVarTemp.CKind == OMPC_unknown) ? 
OMPC_firstprivate0
: OMPC_shared;
1270
542k
      return DVar;
1271
1.24M
    }
1272
3.03M
  }
1273
  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1274
  // in a Construct, implicitly determined, p.3]
1275
  //  For constructs other than task, if no default clause is present, these
1276
  //  variables inherit their data-sharing attributes from the enclosing
1277
  //  context.
1278
1.38M
  return getDSA(++Iter, D);
1279
3.03M
}
1280
1281
const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
1282
1.78k
                                         const Expr *NewDE) {
1283
1.78k
  assert(!isStackEmpty() && "Data sharing attributes stack is empty");
1284
0
  D = getCanonicalDecl(D);
1285
1.78k
  SharingMapTy &StackElem = getTopOfStack();
1286
1.78k
  auto It = StackElem.AlignedMap.find(D);
1287
1.78k
  if (It == StackElem.AlignedMap.end()) {
1288
1.74k
    assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
1289
0
    StackElem.AlignedMap[D] = NewDE;
1290
1.74k
    return nullptr;
1291
1.74k
  }
1292
40
  assert(It->second && "Unexpected nullptr expr in the aligned map");
1293
0
  return It->second;
1294
1.78k
}
1295
1296
const Expr *DSAStackTy::addUniqueNontemporal(const ValueDecl *D,
1297
626
                                             const Expr *NewDE) {
1298
626
  assert(!isStackEmpty() && "Data sharing attributes stack is empty");
1299
0
  D = getCanonicalDecl(D);
1300
626
  SharingMapTy &StackElem = getTopOfStack();
1301
626
  auto It = StackElem.NontemporalMap.find(D);
1302
626
  if (It == StackElem.NontemporalMap.end()) {
1303
598
    assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
1304
0
    StackElem.NontemporalMap[D] = NewDE;
1305
598
    return nullptr;
1306
598
  }
1307
28
  assert(It->second && "Unexpected nullptr expr in the aligned map");
1308
0
  return It->second;
1309
626
}
1310
1311
163k
void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
1312
163k
  assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1313
0
  D = getCanonicalDecl(D);
1314
163k
  SharingMapTy &StackElem = getTopOfStack();
1315
163k
  StackElem.LCVMap.try_emplace(
1316
163k
      D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
1317
163k
}
1318
1319
const DSAStackTy::LCDeclInfo
1320
3.39M
DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
1321
3.39M
  assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1322
0
  D = getCanonicalDecl(D);
1323
3.39M
  const SharingMapTy &StackElem = getTopOfStack();
1324
3.39M
  auto It = StackElem.LCVMap.find(D);
1325
3.39M
  if (It != StackElem.LCVMap.end())
1326
4.85k
    return It->second;
1327
3.38M
  return {0, nullptr};
1328
3.39M
}
1329
1330
const DSAStackTy::LCDeclInfo
1331
132k
DSAStackTy::isLoopControlVariable(const ValueDecl *D, unsigned Level) const {
1332
132k
  assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1333
0
  D = getCanonicalDecl(D);
1334
854k
  for (unsigned I = Level + 1; I > 0; 
--I721k
) {
1335
724k
    const SharingMapTy &StackElem = getStackElemAtLevel(I - 1);
1336
724k
    auto It = StackElem.LCVMap.find(D);
1337
724k
    if (It != StackElem.LCVMap.end())
1338
3.02k
      return It->second;
1339
724k
  }
1340
129k
  return {0, nullptr};
1341
132k
}
1342
1343
const DSAStackTy::LCDeclInfo
1344
272
DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
1345
272
  const SharingMapTy *Parent = getSecondOnStackOrNull();
1346
272
  assert(Parent && "Data-sharing attributes stack is empty");
1347
0
  D = getCanonicalDecl(D);
1348
272
  auto It = Parent->LCVMap.find(D);
1349
272
  if (It != Parent->LCVMap.end())
1350
266
    return It->second;
1351
6
  return {0, nullptr};
1352
272
}
1353
1354
132
const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
1355
132
  const SharingMapTy *Parent = getSecondOnStackOrNull();
1356
132
  assert(Parent && "Data-sharing attributes stack is empty");
1357
132
  if (Parent->LCVMap.size() < I)
1358
12
    return nullptr;
1359
120
  for (const auto &Pair : Parent->LCVMap)
1360
180
    if (Pair.second.first == I)
1361
120
      return Pair.first;
1362
0
  return nullptr;
1363
120
}
1364
1365
void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
1366
                        DeclRefExpr *PrivateCopy, unsigned Modifier,
1367
223k
                        bool AppliedToPointee) {
1368
223k
  D = getCanonicalDecl(D);
1369
223k
  if (A == OMPC_threadprivate) {
1370
2.71k
    DSAInfo &Data = Threadprivates[D];
1371
2.71k
    Data.Attributes = A;
1372
2.71k
    Data.RefExpr.setPointer(E);
1373
2.71k
    Data.PrivateCopy = nullptr;
1374
2.71k
    Data.Modifier = Modifier;
1375
220k
  } else {
1376
220k
    DSAInfo &Data = getTopOfStack().SharingMap[D];
1377
220k
    assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
1378
220k
           (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
1379
220k
           (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
1380
220k
           (isLoopControlVariable(D).first && A == OMPC_private));
1381
0
    Data.Modifier = Modifier;
1382
220k
    if (A == OMPC_lastprivate && 
Data.Attributes == OMPC_firstprivate10.5k
) {
1383
216
      Data.RefExpr.setInt(/*IntVal=*/true);
1384
216
      return;
1385
216
    }
1386
220k
    const bool IsLastprivate =
1387
220k
        A == OMPC_lastprivate || 
Data.Attributes == OMPC_lastprivate210k
;
1388
220k
    Data.Attributes = A;
1389
220k
    Data.RefExpr.setPointerAndInt(E, IsLastprivate);
1390
220k
    Data.PrivateCopy = PrivateCopy;
1391
220k
    Data.AppliedToPointee = AppliedToPointee;
1392
220k
    if (PrivateCopy) {
1393
2.81k
      DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()];
1394
2.81k
      Data.Modifier = Modifier;
1395
2.81k
      Data.Attributes = A;
1396
2.81k
      Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
1397
2.81k
      Data.PrivateCopy = nullptr;
1398
2.81k
      Data.AppliedToPointee = AppliedToPointee;
1399
2.81k
    }
1400
220k
  }
1401
223k
}
1402
1403
/// Build a variable declaration for OpenMP loop iteration variable.
1404
static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
1405
                             StringRef Name, const AttrVec *Attrs = nullptr,
1406
885k
                             DeclRefExpr *OrigRef = nullptr) {
1407
885k
  DeclContext *DC = SemaRef.CurContext;
1408
885k
  IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
1409
885k
  TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
1410
885k
  auto *Decl =
1411
885k
      VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
1412
885k
  if (Attrs) {
1413
1.72k
    for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
1414
2.36k
         I != E; 
++I636
)
1415
636
      Decl->addAttr(*I);
1416
1.72k
  }
1417
885k
  Decl->setImplicit();
1418
885k
  if (OrigRef) {
1419
215k
    Decl->addAttr(
1420
215k
        OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
1421
215k
  }
1422
885k
  return Decl;
1423
885k
}
1424
1425
static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
1426
                                     SourceLocation Loc,
1427
1.71M
                                     bool RefersToCapture = false) {
1428
1.71M
  D->setReferenced();
1429
1.71M
  D->markUsed(S.Context);
1430
1.71M
  return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
1431
1.71M
                             SourceLocation(), D, RefersToCapture, Loc, Ty,
1432
1.71M
                             VK_LValue);
1433
1.71M
}
1434
1435
void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1436
1.94k
                                           BinaryOperatorKind BOK) {
1437
1.94k
  D = getCanonicalDecl(D);
1438
1.94k
  assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1439
0
  assert(
1440
1.94k
      getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1441
1.94k
      "Additional reduction info may be specified only for reduction items.");
1442
0
  ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1443
1.94k
  assert(ReductionData.ReductionRange.isInvalid() &&
1444
1.94k
         (getTopOfStack().Directive == OMPD_taskgroup ||
1445
1.94k
          ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
1446
1.94k
            isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
1447
1.94k
           !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
1448
1.94k
         "Additional reduction info may be specified only once for reduction "
1449
1.94k
         "items.");
1450
0
  ReductionData.set(BOK, SR);
1451
1.94k
  Expr *&TaskgroupReductionRef = getTopOfStack().TaskgroupReductionRef;
1452
1.94k
  if (!TaskgroupReductionRef) {
1453
1.77k
    VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1454
1.77k
                               SemaRef.Context.VoidPtrTy, ".task_red.");
1455
1.77k
    TaskgroupReductionRef =
1456
1.77k
        buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
1457
1.77k
  }
1458
1.94k
}
1459
1460
void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1461
30
                                           const Expr *ReductionRef) {
1462
30
  D = getCanonicalDecl(D);
1463
30
  assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1464
0
  assert(
1465
30
      getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1466
30
      "Additional reduction info may be specified only for reduction items.");
1467
0
  ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1468
30
  assert(ReductionData.ReductionRange.isInvalid() &&
1469
30
         (getTopOfStack().Directive == OMPD_taskgroup ||
1470
30
          ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
1471
30
            isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
1472
30
           !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
1473
30
         "Additional reduction info may be specified only once for reduction "
1474
30
         "items.");
1475
0
  ReductionData.set(ReductionRef, SR);
1476
30
  Expr *&TaskgroupReductionRef = getTopOfStack().TaskgroupReductionRef;
1477
30
  if (!TaskgroupReductionRef) {
1478
30
    VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1479
30
                               SemaRef.Context.VoidPtrTy, ".task_red.");
1480
30
    TaskgroupReductionRef =
1481
30
        buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
1482
30
  }
1483
30
}
1484
1485
const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1486
    const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
1487
1.25k
    Expr *&TaskgroupDescriptor) const {
1488
1.25k
  D = getCanonicalDecl(D);
1489
1.25k
  assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1490
1.40k
  for (const_iterator I = begin() + 1, E = end(); I != E; 
++I154
) {
1491
1.22k
    const DSAInfo &Data = I->SharingMap.lookup(D);
1492
1.22k
    if (Data.Attributes != OMPC_reduction ||
1493
1.22k
        
Data.Modifier != OMPC_REDUCTION_task1.07k
)
1494
154
      continue;
1495
1.06k
    const ReductionData &ReductionData = I->ReductionMap.lookup(D);
1496
1.06k
    if (!ReductionData.ReductionOp ||
1497
1.06k
        ReductionData.ReductionOp.is<const Expr *>())
1498
30
      return DSAVarData();
1499
1.03k
    SR = ReductionData.ReductionRange;
1500
1.03k
    BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
1501
1.03k
    assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1502
1.03k
                                       "expression for the descriptor is not "
1503
1.03k
                                       "set.");
1504
0
    TaskgroupDescriptor = I->TaskgroupReductionRef;
1505
1.03k
    return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(),
1506
1.03k
                      Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task,
1507
1.03k
                      /*AppliedToPointee=*/false);
1508
1.06k
  }
1509
188
  return DSAVarData();
1510
1.25k
}
1511
1512
const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1513
    const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
1514
1.25k
    Expr *&TaskgroupDescriptor) const {
1515
1.25k
  D = getCanonicalDecl(D);
1516
1.25k
  assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1517
1.40k
  for (const_iterator I = begin() + 1, E = end(); I != E; 
++I154
) {
1518
1.22k
    const DSAInfo &Data = I->SharingMap.lookup(D);
1519
1.22k
    if (Data.Attributes != OMPC_reduction ||
1520
1.22k
        
Data.Modifier != OMPC_REDUCTION_task1.07k
)
1521
154
      continue;
1522
1.06k
    const ReductionData &ReductionData = I->ReductionMap.lookup(D);
1523
1.06k
    if (!ReductionData.ReductionOp ||
1524
1.06k
        !ReductionData.ReductionOp.is<const Expr *>())
1525
1.03k
      return DSAVarData();
1526
30
    SR = ReductionData.ReductionRange;
1527
30
    ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
1528
30
    assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1529
30
                                       "expression for the descriptor is not "
1530
30
                                       "set.");
1531
0
    TaskgroupDescriptor = I->TaskgroupReductionRef;
1532
30
    return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(),
1533
30
                      Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task,
1534
30
                      /*AppliedToPointee=*/false);
1535
1.06k
  }
1536
188
  return DSAVarData();
1537
1.25k
}
1538
1539
3.06M
bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const {
1540
3.06M
  D = D->getCanonicalDecl();
1541
7.50M
  for (const_iterator E = end(); I != E; 
++I4.44M
) {
1542
6.39M
    if (isImplicitOrExplicitTaskingRegion(I->Directive) ||
1543
6.39M
        
isOpenMPTargetExecutionDirective(I->Directive)4.62M
) {
1544
1.95M
      if (I->CurScope) {
1545
1.74M
        Scope *TopScope = I->CurScope->getParent();
1546
1.74M
        Scope *CurScope = getCurScope();
1547
23.0M
        while (CurScope && CurScope != TopScope && 
!CurScope->isDeclScope(D)21.2M
)
1548
21.2M
          CurScope = CurScope->getParent();
1549
1.74M
        return CurScope != TopScope;
1550
1.74M
      }
1551
618k
      
for (DeclContext *DC = D->getDeclContext(); 206k
DC;
DC = DC->getParent()411k
)
1552
413k
        if (I->Context == DC)
1553
1.46k
          return true;
1554
205k
      return false;
1555
206k
    }
1556
6.39M
  }
1557
1.11M
  return false;
1558
3.06M
}
1559
1560
static bool isConstNotMutableType(Sema &SemaRef, QualType Type,
1561
                                  bool AcceptIfMutable = true,
1562
77.2k
                                  bool *IsClassType = nullptr) {
1563
77.2k
  ASTContext &Context = SemaRef.getASTContext();
1564
77.2k
  Type = Type.getNonReferenceType().getCanonicalType();
1565
77.2k
  bool IsConstant = Type.isConstant(Context);
1566
77.2k
  Type = Context.getBaseElementType(Type);
1567
77.2k
  const CXXRecordDecl *RD = AcceptIfMutable && 
SemaRef.getLangOpts().CPlusPlus26.1k
1568
77.2k
                                ? 
Type->getAsCXXRecordDecl()24.3k
1569
77.2k
                                : 
nullptr52.8k
;
1570
77.2k
  if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1571
2.19k
    if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
1572
2.19k
      RD = CTD->getTemplatedDecl();
1573
77.2k
  if (IsClassType)
1574
77.0k
    *IsClassType = RD;
1575
77.2k
  return IsConstant && 
!(11.0k
SemaRef.getLangOpts().CPlusPlus11.0k
&&
RD11.0k
&&
1576
11.0k
                         
RD->hasDefinition()1.03k
&&
RD->hasMutableFields()1.03k
);
1577
77.2k
}
1578
1579
static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D,
1580
                                      QualType Type, OpenMPClauseKind CKind,
1581
                                      SourceLocation ELoc,
1582
                                      bool AcceptIfMutable = true,
1583
77.0k
                                      bool ListItemNotVar = false) {
1584
77.0k
  ASTContext &Context = SemaRef.getASTContext();
1585
77.0k
  bool IsClassType;
1586
77.0k
  if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) {
1587
10.3k
    unsigned Diag = ListItemNotVar ? 
diag::err_omp_const_list_item0
1588
10.3k
                    : IsClassType  ? 
diag::err_omp_const_not_mutable_variable296
1589
10.3k
                                   : 
diag::err_omp_const_variable10.0k
;
1590
10.3k
    SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind);
1591
10.3k
    if (!ListItemNotVar && D) {
1592
10.3k
      const VarDecl *VD = dyn_cast<VarDecl>(D);
1593
10.3k
      bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
1594
10.3k
                               VarDecl::DeclarationOnly;
1595
10.3k
      SemaRef.Diag(D->getLocation(),
1596
10.3k
                   IsDecl ? 
diag::note_previous_decl2.84k
:
diag::note_defined_here7.47k
)
1597
10.3k
          << D;
1598
10.3k
    }
1599
10.3k
    return true;
1600
10.3k
  }
1601
66.7k
  return false;
1602
77.0k
}
1603
1604
const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1605
3.93M
                                                   bool FromParent) {
1606
3.93M
  D = getCanonicalDecl(D);
1607
3.93M
  DSAVarData DVar;
1608
1609
3.93M
  auto *VD = dyn_cast<VarDecl>(D);
1610
3.93M
  auto TI = Threadprivates.find(D);
1611
3.93M
  if (TI != Threadprivates.end()) {
1612
30.6k
    DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
1613
30.6k
    DVar.CKind = OMPC_threadprivate;
1614
30.6k
    DVar.Modifier = TI->getSecond().Modifier;
1615
30.6k
    return DVar;
1616
30.6k
  }
1617
3.89M
  if (VD && 
VD->hasAttr<OMPThreadPrivateDeclAttr>()3.87M
) {
1618
10
    DVar.RefExpr = buildDeclRefExpr(
1619
10
        SemaRef, VD, D->getType().getNonReferenceType(),
1620
10
        VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1621
10
    DVar.CKind = OMPC_threadprivate;
1622
10
    addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1623
10
    return DVar;
1624
10
  }
1625
  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1626
  // in a Construct, C/C++, predetermined, p.1]
1627
  //  Variables appearing in threadprivate directives are threadprivate.
1628
3.89M
  if ((VD && 
VD->getTLSKind() != VarDecl::TLS_None3.87M
&&
1629
3.89M
       
!(9
VD->hasAttr<OMPThreadPrivateDeclAttr>()9
&&
1630
9
         
SemaRef.getLangOpts().OpenMPUseTLS0
&&
1631
9
         
SemaRef.getASTContext().getTargetInfo().isTLSSupported()0
)) ||
1632
3.89M
      
(3.89M
VD3.89M
&&
VD->getStorageClass() == SC_Register3.87M
&&
1633
3.89M
       
VD->hasAttr<AsmLabelAttr>()224
&&
!VD->isLocalVarDecl()0
)) {
1634
9
    DVar.RefExpr = buildDeclRefExpr(
1635
9
        SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
1636
9
    DVar.CKind = OMPC_threadprivate;
1637
9
    addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1638
9
    return DVar;
1639
9
  }
1640
3.89M
  if (SemaRef.getLangOpts().OpenMPCUDAMode && 
VD10.3k
&&
1641
3.89M
      
VD->isLocalVarDeclOrParm()10.3k
&&
!isStackEmpty()10.2k
&&
1642
3.89M
      
!isLoopControlVariable(D).first10.2k
) {
1643
10.0k
    const_iterator IterTarget =
1644
10.3k
        std::find_if(begin(), end(), [](const SharingMapTy &Data) {
1645
10.3k
          return isOpenMPTargetExecutionDirective(Data.Directive);
1646
10.3k
        });
1647
10.0k
    if (IterTarget != end()) {
1648
10.0k
      const_iterator ParentIterTarget = IterTarget + 1;
1649
20.3k
      for (const_iterator Iter = begin(); Iter != ParentIterTarget; 
++Iter10.2k
) {
1650
10.2k
        if (isOpenMPLocal(VD, Iter)) {
1651
13
          DVar.RefExpr =
1652
13
              buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1653
13
                               D->getLocation());
1654
13
          DVar.CKind = OMPC_threadprivate;
1655
13
          return DVar;
1656
13
        }
1657
10.2k
      }
1658
10.0k
      if (!isClauseParsingMode() || 
IterTarget != begin()438
) {
1659
9.67k
        auto DSAIter = IterTarget->SharingMap.find(D);
1660
9.67k
        if (DSAIter != IterTarget->SharingMap.end() &&
1661
9.67k
            
isOpenMPPrivate(DSAIter->getSecond().Attributes)1.42k
) {
1662
1.42k
          DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1663
1.42k
          DVar.CKind = OMPC_threadprivate;
1664
1.42k
          return DVar;
1665
1.42k
        }
1666
8.25k
        const_iterator End = end();
1667
8.25k
        if (!SemaRef.isOpenMPCapturedByRef(D,
1668
8.25k
                                           std::distance(ParentIterTarget, End),
1669
8.25k
                                           /*OpenMPCaptureLevel=*/0)) {
1670
2.32k
          DVar.RefExpr =
1671
2.32k
              buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1672
2.32k
                               IterTarget->ConstructLoc);
1673
2.32k
          DVar.CKind = OMPC_threadprivate;
1674
2.32k
          return DVar;
1675
2.32k
        }
1676
8.25k
      }
1677
10.0k
    }
1678
10.0k
  }
1679
1680
3.89M
  if (isStackEmpty())
1681
    // Not in OpenMP execution region and top scope was already checked.
1682
1.28k
    return DVar;
1683
1684
  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1685
  // in a Construct, C/C++, predetermined, p.4]
1686
  //  Static data members are shared.
1687
  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1688
  // in a Construct, C/C++, predetermined, p.7]
1689
  //  Variables with static storage duration that are declared in a scope
1690
  //  inside the construct are shared.
1691
3.89M
  if (VD && 
VD->isStaticDataMember()3.86M
) {
1692
    // Check for explicitly specified attributes.
1693
20.9k
    const_iterator I = begin();
1694
20.9k
    const_iterator EndI = end();
1695
20.9k
    if (FromParent && 
I != EndI6.05k
)
1696
6.05k
      ++I;
1697
20.9k
    if (I != EndI) {
1698
20.9k
      auto It = I->SharingMap.find(D);
1699
20.9k
      if (It != I->SharingMap.end()) {
1700
2.32k
        const DSAInfo &Data = It->getSecond();
1701
2.32k
        DVar.RefExpr = Data.RefExpr.getPointer();
1702
2.32k
        DVar.PrivateCopy = Data.PrivateCopy;
1703
2.32k
        DVar.CKind = Data.Attributes;
1704
2.32k
        DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1705
2.32k
        DVar.DKind = I->Directive;
1706
2.32k
        DVar.Modifier = Data.Modifier;
1707
2.32k
        DVar.AppliedToPointee = Data.AppliedToPointee;
1708
2.32k
        return DVar;
1709
2.32k
      }
1710
20.9k
    }
1711
1712
18.6k
    DVar.CKind = OMPC_shared;
1713
18.6k
    return DVar;
1714
20.9k
  }
1715
1716
3.87M
  auto &&MatchesAlways = [](OpenMPDirectiveKind) 
{ return true; }16
;
1717
  // The predetermined shared attribute for const-qualified types having no
1718
  // mutable members was removed after OpenMP 3.1.
1719
3.87M
  if (SemaRef.LangOpts.OpenMP <= 31) {
1720
    // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1721
    // in a Construct, C/C++, predetermined, p.6]
1722
    //  Variables with const qualified type having no mutable member are
1723
    //  shared.
1724
64
    if (isConstNotMutableType(SemaRef, D->getType())) {
1725
      // Variables with const-qualified type having no mutable member may be
1726
      // listed in a firstprivate clause, even if they are static data members.
1727
16
      DSAVarData DVarTemp = hasInnermostDSA(
1728
16
          D,
1729
16
          [](OpenMPClauseKind C, bool) {
1730
16
            return C == OMPC_firstprivate || C == OMPC_shared;
1731
16
          },
1732
16
          MatchesAlways, FromParent);
1733
16
      if (DVarTemp.CKind != OMPC_unknown && 
DVarTemp.RefExpr0
)
1734
0
        return DVarTemp;
1735
1736
16
      DVar.CKind = OMPC_shared;
1737
16
      return DVar;
1738
16
    }
1739
64
  }
1740
1741
  // Explicitly specified attributes and local variables with predetermined
1742
  // attributes.
1743
3.87M
  const_iterator I = begin();
1744
3.87M
  const_iterator EndI = end();
1745
3.87M
  if (FromParent && 
I != EndI145k
)
1746
145k
    ++I;
1747
3.87M
  if (I == EndI)
1748
6
    return DVar;
1749
3.87M
  auto It = I->SharingMap.find(D);
1750
3.87M
  if (It != I->SharingMap.end()) {
1751
442k
    const DSAInfo &Data = It->getSecond();
1752
442k
    DVar.RefExpr = Data.RefExpr.getPointer();
1753
442k
    DVar.PrivateCopy = Data.PrivateCopy;
1754
442k
    DVar.CKind = Data.Attributes;
1755
442k
    DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1756
442k
    DVar.DKind = I->Directive;
1757
442k
    DVar.Modifier = Data.Modifier;
1758
442k
    DVar.AppliedToPointee = Data.AppliedToPointee;
1759
442k
  }
1760
1761
3.87M
  return DVar;
1762
3.87M
}
1763
1764
const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1765
88.7k
                                                        bool FromParent) const {
1766
88.7k
  if (isStackEmpty()) {
1767
0
    const_iterator I;
1768
0
    return getDSA(I, D);
1769
0
  }
1770
88.7k
  D = getCanonicalDecl(D);
1771
88.7k
  const_iterator StartI = begin();
1772
88.7k
  const_iterator EndI = end();
1773
88.7k
  if (FromParent && 
StartI != EndI4.95k
)
1774
4.95k
    ++StartI;
1775
88.7k
  return getDSA(StartI, D);
1776
88.7k
}
1777
1778
const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1779
188k
                                                        unsigned Level) const {
1780
188k
  if (getStackSize() <= Level)
1781
0
    return DSAVarData();
1782
188k
  D = getCanonicalDecl(D);
1783
188k
  const_iterator StartI = std::next(begin(), getStackSize() - 1 - Level);
1784
188k
  return getDSA(StartI, D);
1785
188k
}
1786
1787
const DSAStackTy::DSAVarData
1788
DSAStackTy::hasDSA(ValueDecl *D,
1789
                   const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
1790
                   const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1791
325k
                   bool FromParent) const {
1792
325k
  if (isStackEmpty())
1793
0
    return {};
1794
325k
  D = getCanonicalDecl(D);
1795
325k
  const_iterator I = begin();
1796
325k
  const_iterator EndI = end();
1797
325k
  if (FromParent && 
I != EndI105k
)
1798
105k
    ++I;
1799
1.01M
  for (; I != EndI; 
++I685k
) {
1800
693k
    if (!DPred(I->Directive) &&
1801
693k
        
!isImplicitOrExplicitTaskingRegion(I->Directive)0
)
1802
0
      continue;
1803
693k
    const_iterator NewI = I;
1804
693k
    DSAVarData DVar = getDSA(NewI, D);
1805
693k
    if (I == NewI && 
CPred(DVar.CKind, DVar.AppliedToPointee)176k
)
1806
7.98k
      return DVar;
1807
693k
  }
1808
317k
  return {};
1809
325k
}
1810
1811
const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
1812
    ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
1813
    const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1814
85.3k
    bool FromParent) const {
1815
85.3k
  if (isStackEmpty())
1816
0
    return {};
1817
85.3k
  D = getCanonicalDecl(D);
1818
85.3k
  const_iterator StartI = begin();
1819
85.3k
  const_iterator EndI = end();
1820
85.3k
  if (FromParent && 
StartI != EndI85.2k
)
1821
85.2k
    ++StartI;
1822
85.3k
  if (StartI == EndI || 
!DPred(StartI->Directive)49.5k
)
1823
65.3k
    return {};
1824
19.9k
  const_iterator NewI = StartI;
1825
19.9k
  DSAVarData DVar = getDSA(NewI, D);
1826
19.9k
  return (NewI == StartI && 
CPred(DVar.CKind, DVar.AppliedToPointee)19.6k
)
1827
19.9k
             ? 
DVar146
1828
19.9k
             : 
DSAVarData()19.7k
;
1829
85.3k
}
1830
1831
bool DSAStackTy::hasExplicitDSA(
1832
    const ValueDecl *D,
1833
    const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
1834
4.49M
    unsigned Level, bool NotLastprivate) const {
1835
4.49M
  if (getStackSize() <= Level)
1836
0
    return false;
1837
4.49M
  D = getCanonicalDecl(D);
1838
4.49M
  const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1839
4.49M
  auto I = StackElem.SharingMap.find(D);
1840
4.49M
  if (I != StackElem.SharingMap.end() && 
I->getSecond().RefExpr.getPointer()463k
&&
1841
4.49M
      
CPred(I->getSecond().Attributes, I->getSecond().AppliedToPointee)463k
&&
1842
4.49M
      
(157k
!NotLastprivate157k
||
!I->getSecond().RefExpr.getInt()13.2k
))
1843
156k
    return true;
1844
  // Check predetermined rules for the loop control variables.
1845
4.33M
  auto LI = StackElem.LCVMap.find(D);
1846
4.33M
  if (LI != StackElem.LCVMap.end())
1847
27.4k
    return CPred(OMPC_private, /*AppliedToPointee=*/false);
1848
4.30M
  return false;
1849
4.33M
}
1850
1851
bool DSAStackTy::hasExplicitDirective(
1852
    const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1853
9.71M
    unsigned Level) const {
1854
9.71M
  if (getStackSize() <= Level)
1855
0
    return false;
1856
9.71M
  const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1857
9.71M
  return DPred(StackElem.Directive);
1858
9.71M
}
1859
1860
bool DSAStackTy::hasDirective(
1861
    const llvm::function_ref<bool(OpenMPDirectiveKind,
1862
                                  const DeclarationNameInfo &, SourceLocation)>
1863
        DPred,
1864
182k
    bool FromParent) const {
1865
  // We look only in the enclosing region.
1866
182k
  size_t Skip = FromParent ? 
20
: 1;
1867
182k
  for (const_iterator I = begin() + std::min(Skip, getStackSize()), E = end();
1868
291k
       I != E; 
++I109k
) {
1869
226k
    if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1870
116k
      return true;
1871
226k
  }
1872
65.3k
  return false;
1873
182k
}
1874
1875
85.5k
void Sema::InitDataSharingAttributesStack() {
1876
85.5k
  VarDataSharingAttributesStack = new DSAStackTy(*this);
1877
85.5k
}
1878
1879
67.0M
#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1880
1881
89.4k
void Sema::pushOpenMPFunctionRegion() { DSAStack->pushFunction(); }
1882
1883
657k
void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1884
657k
  DSAStack->popFunction(OldFSI);
1885
657k
}
1886
1887
250
static bool isOpenMPDeviceDelayedContext(Sema &S) {
1888
250
  assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice &&
1889
250
         "Expected OpenMP device compilation.");
1890
0
  return !S.isInOpenMPTargetExecutionDirective();
1891
250
}
1892
1893
namespace {
1894
/// Status of the function emission on the host/device.
1895
enum class FunctionEmissionStatus {
1896
  Emitted,
1897
  Discarded,
1898
  Unknown,
1899
};
1900
} // anonymous namespace
1901
1902
Sema::SemaDiagnosticBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc,
1903
                                                         unsigned DiagID,
1904
348
                                                         FunctionDecl *FD) {
1905
348
  assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice &&
1906
348
         "Expected OpenMP device compilation.");
1907
1908
0
  SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop;
1909
348
  if (FD) {
1910
327
    FunctionEmissionStatus FES = getEmissionStatus(FD);
1911
327
    switch (FES) {
1912
48
    case FunctionEmissionStatus::Emitted:
1913
48
      Kind = SemaDiagnosticBuilder::K_Immediate;
1914
48
      break;
1915
250
    case FunctionEmissionStatus::Unknown:
1916
      // TODO: We should always delay diagnostics here in case a target
1917
      //       region is in a function we do not emit. However, as the
1918
      //       current diagnostics are associated with the function containing
1919
      //       the target region and we do not emit that one, we would miss out
1920
      //       on diagnostics for the target region itself. We need to anchor
1921
      //       the diagnostics with the new generated function *or* ensure we
1922
      //       emit diagnostics associated with the surrounding function.
1923
250
      Kind = isOpenMPDeviceDelayedContext(*this)
1924
250
                 ? 
SemaDiagnosticBuilder::K_Deferred213
1925
250
                 : 
SemaDiagnosticBuilder::K_Immediate37
;
1926
250
      break;
1927
29
    case FunctionEmissionStatus::TemplateDiscarded:
1928
29
    case FunctionEmissionStatus::OMPDiscarded:
1929
29
      Kind = SemaDiagnosticBuilder::K_Nop;
1930
29
      break;
1931
0
    case FunctionEmissionStatus::CUDADiscarded:
1932
0
      llvm_unreachable("CUDADiscarded unexpected in OpenMP device compilation");
1933
0
      break;
1934
327
    }
1935
327
  }
1936
1937
348
  return SemaDiagnosticBuilder(Kind, Loc, DiagID, FD, *this);
1938
348
}
1939
1940
Sema::SemaDiagnosticBuilder Sema::diagIfOpenMPHostCode(SourceLocation Loc,
1941
                                                       unsigned DiagID,
1942
1
                                                       FunctionDecl *FD) {
1943
1
  assert(LangOpts.OpenMP && !LangOpts.OpenMPIsDevice &&
1944
1
         "Expected OpenMP host compilation.");
1945
1946
0
  SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop;
1947
1
  if (FD) {
1948
0
    FunctionEmissionStatus FES = getEmissionStatus(FD);
1949
0
    switch (FES) {
1950
0
    case FunctionEmissionStatus::Emitted:
1951
0
      Kind = SemaDiagnosticBuilder::K_Immediate;
1952
0
      break;
1953
0
    case FunctionEmissionStatus::Unknown:
1954
0
      Kind = SemaDiagnosticBuilder::K_Deferred;
1955
0
      break;
1956
0
    case FunctionEmissionStatus::TemplateDiscarded:
1957
0
    case FunctionEmissionStatus::OMPDiscarded:
1958
0
    case FunctionEmissionStatus::CUDADiscarded:
1959
0
      Kind = SemaDiagnosticBuilder::K_Nop;
1960
0
      break;
1961
0
    }
1962
0
  }
1963
1964
1
  return SemaDiagnosticBuilder(Kind, Loc, DiagID, FD, *this);
1965
1
}
1966
1967
static OpenMPDefaultmapClauseKind
1968
910k
getVariableCategoryFromDecl(const LangOptions &LO, const ValueDecl *VD) {
1969
910k
  if (LO.OpenMP <= 45) {
1970
302k
    if (VD->getType().getNonReferenceType()->isScalarType())
1971
268k
      return OMPC_DEFAULTMAP_scalar;
1972
33.5k
    return OMPC_DEFAULTMAP_aggregate;
1973
302k
  }
1974
608k
  if (VD->getType().getNonReferenceType()->isAnyPointerType())
1975
151k
    return OMPC_DEFAULTMAP_pointer;
1976
456k
  if (VD->getType().getNonReferenceType()->isScalarType())
1977
409k
    return OMPC_DEFAULTMAP_scalar;
1978
47.4k
  return OMPC_DEFAULTMAP_aggregate;
1979
456k
}
1980
1981
bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level,
1982
1.30M
                                 unsigned OpenMPCaptureLevel) const {
1983
1.30M
  assert(LangOpts.OpenMP && "OpenMP is not allowed");
1984
1985
0
  ASTContext &Ctx = getASTContext();
1986
1.30M
  bool IsByRef = true;
1987
1988
  // Find the directive that is associated with the provided scope.
1989
1.30M
  D = cast<ValueDecl>(D->getCanonicalDecl());
1990
1.30M
  QualType Ty = D->getType();
1991
1992
1.30M
  bool IsVariableUsedInMapClause = false;
1993
1.30M
  if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
1994
    // This table summarizes how a given variable should be passed to the device
1995
    // given its type and the clauses where it appears. This table is based on
1996
    // the description in OpenMP 4.5 [2.10.4, target Construct] and
1997
    // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1998
    //
1999
    // =========================================================================
2000
    // | type |  defaultmap   | pvt | first | is_device_ptr |    map   | res.  |
2001
    // |      |(tofrom:scalar)|     |  pvt  |               |          |       |
2002
    // =========================================================================
2003
    // | scl  |               |     |       |       -       |          | bycopy|
2004
    // | scl  |               |  -  |   x   |       -       |     -    | bycopy|
2005
    // | scl  |               |  x  |   -   |       -       |     -    | null  |
2006
    // | scl  |       x       |     |       |       -       |          | byref |
2007
    // | scl  |       x       |  -  |   x   |       -       |     -    | bycopy|
2008
    // | scl  |       x       |  x  |   -   |       -       |     -    | null  |
2009
    // | scl  |               |  -  |   -   |       -       |     x    | byref |
2010
    // | scl  |       x       |  -  |   -   |       -       |     x    | byref |
2011
    //
2012
    // | agg  |      n.a.     |     |       |       -       |          | byref |
2013
    // | agg  |      n.a.     |  -  |   x   |       -       |     -    | byref |
2014
    // | agg  |      n.a.     |  x  |   -   |       -       |     -    | null  |
2015
    // | agg  |      n.a.     |  -  |   -   |       -       |     x    | byref |
2016
    // | agg  |      n.a.     |  -  |   -   |       -       |    x[]   | byref |
2017
    //
2018
    // | ptr  |      n.a.     |     |       |       -       |          | bycopy|
2019
    // | ptr  |      n.a.     |  -  |   x   |       -       |     -    | bycopy|
2020
    // | ptr  |      n.a.     |  x  |   -   |       -       |     -    | null  |
2021
    // | ptr  |      n.a.     |  -  |   -   |       -       |     x    | byref |
2022
    // | ptr  |      n.a.     |  -  |   -   |       -       |    x[]   | bycopy|
2023
    // | ptr  |      n.a.     |  -  |   -   |       x       |          | bycopy|
2024
    // | ptr  |      n.a.     |  -  |   -   |       x       |     x    | bycopy|
2025
    // | ptr  |      n.a.     |  -  |   -   |       x       |    x[]   | bycopy|
2026
    // =========================================================================
2027
    // Legend:
2028
    //  scl - scalar
2029
    //  ptr - pointer
2030
    //  agg - aggregate
2031
    //  x - applies
2032
    //  - - invalid in this combination
2033
    //  [] - mapped with an array section
2034
    //  byref - should be mapped by reference
2035
    //  byval - should be mapped by value
2036
    //  null - initialize a local variable to null on the device
2037
    //
2038
    // Observations:
2039
    //  - All scalar declarations that show up in a map clause have to be passed
2040
    //    by reference, because they may have been mapped in the enclosing data
2041
    //    environment.
2042
    //  - If the scalar value does not fit the size of uintptr, it has to be
2043
    //    passed by reference, regardless the result in the table above.
2044
    //  - For pointers mapped by value that have either an implicit map or an
2045
    //    array section, the runtime library may pass the NULL value to the
2046
    //    device instead of the value passed to it by the compiler.
2047
2048
865k
    if (Ty->isReferenceType())
2049
27.8k
      Ty = Ty->castAs<ReferenceType>()->getPointeeType();
2050
2051
    // Locate map clauses and see if the variable being captured is referred to
2052
    // in any of those clauses. Here we only care about variables, not fields,
2053
    // because fields are part of aggregates.
2054
865k
    bool IsVariableAssociatedWithSection = false;
2055
2056
865k
    DSAStack->checkMappableExprComponentListsForDeclAtLevel(
2057
865k
        D, Level,
2058
865k
        [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection,
2059
865k
         D](OMPClauseMappableExprCommon::MappableExprComponentListRef
2060
865k
                MapExprComponents,
2061
865k
            OpenMPClauseKind WhereFoundClauseKind) {
2062
          // Only the map clause information influences how a variable is
2063
          // captured. E.g. is_device_ptr does not require changing the default
2064
          // behavior.
2065
27.0k
          if (WhereFoundClauseKind != OMPC_map)
2066
1.71k
            return false;
2067
2068
25.3k
          auto EI = MapExprComponents.rbegin();
2069
25.3k
          auto EE = MapExprComponents.rend();
2070
2071
25.3k
          assert(EI != EE && "Invalid map expression!");
2072
2073
25.3k
          if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
2074
25.3k
            IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
2075
2076
25.3k
          ++EI;
2077
25.3k
          if (EI == EE)
2078
15.6k
            return false;
2079
2080
9.63k
          if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
2081
9.63k
              
isa<OMPArraySectionExpr>(EI->getAssociatedExpression())8.19k
||
2082
9.63k
              
isa<MemberExpr>(EI->getAssociatedExpression())3.19k
||
2083
9.63k
              
isa<OMPArrayShapingExpr>(EI->getAssociatedExpression())96
) {
2084
9.63k
            IsVariableAssociatedWithSection = true;
2085
            // There is nothing more we need to know about this variable.
2086
9.63k
            return true;
2087
9.63k
          }
2088
2089
          // Keep looking for more map info.
2090
0
          return false;
2091
9.63k
        });
2092
2093
865k
    if (IsVariableUsedInMapClause) {
2094
      // If variable is identified in a map clause it is always captured by
2095
      // reference except if it is a pointer that is dereferenced somehow.
2096
25.1k
      IsByRef = !(Ty->isPointerType() && 
IsVariableAssociatedWithSection4.07k
);
2097
840k
    } else {
2098
      // By default, all the data that has a scalar type is mapped by copy
2099
      // (except for reduction variables).
2100
      // Defaultmap scalar is mutual exclusive to defaultmap pointer
2101
840k
      IsByRef = (DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
2102
840k
                 
!Ty->isAnyPointerType()8
) ||
2103
840k
                
!Ty->isScalarType()840k
||
2104
840k
                
DSAStack577k
->isDefaultmapCapturedByRef(
2105
577k
                    Level, getVariableCategoryFromDecl(LangOpts, D)) ||
2106
840k
                
DSAStack572k
->hasExplicitDSA(
2107
572k
                    D,
2108
572k
                    [](OpenMPClauseKind K, bool AppliedToPointee) {
2109
33.1k
                      return K == OMPC_reduction && 
!AppliedToPointee10.4k
;
2110
33.1k
                    },
2111
572k
                    Level);
2112
840k
    }
2113
865k
  }
2114
2115
1.30M
  if (IsByRef && 
Ty.getNonReferenceType()->isScalarType()744k
) {
2116
315k
    IsByRef =
2117
315k
        ((IsVariableUsedInMapClause &&
2118
315k
          
DSAStack10.7k
->getCaptureRegion(Level, OpenMPCaptureLevel) ==
2119
10.7k
              OMPD_target) ||
2120
315k
         
!(311k
DSAStack311k
->hasExplicitDSA(
2121
311k
               D,
2122
311k
               [](OpenMPClauseKind K, bool AppliedToPointee) -> bool {
2123
50.2k
                 return K == OMPC_firstprivate ||
2124
50.2k
                        
(43.9k
K == OMPC_reduction43.9k
&&
AppliedToPointee27.4k
);
2125
50.2k
               },
2126
311k
               Level, /*NotLastprivate=*/true) ||
2127
311k
           
DSAStack305k
->isUsesAllocatorsDecl(Level, D)305k
)) &&
2128
        // If the variable is artificial and must be captured by value - try to
2129
        // capture by value.
2130
315k
        
!(309k
isa<OMPCapturedExprDecl>(D)309k
&&
!D->hasAttr<OMPCaptureNoInitAttr>()3.49k
&&
2131
309k
          
!cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue()3.36k
) &&
2132
        // If the variable is implicitly firstprivate and scalar - capture by
2133
        // copy
2134
315k
        
!(306k
DSAStack306k
->getDefaultDSA() == DSA_firstprivate306k
&&
2135
306k
          
!180
DSAStack180
->hasExplicitDSA(
2136
180
              D, [](OpenMPClauseKind K, bool) 
{ return K != OMPC_unknown; }0
,
2137
180
              Level) &&
2138
306k
          
!180
DSAStack180
->isLoopControlVariable(D, Level).first);
2139
315k
  }
2140
2141
  // When passing data by copy, we need to make sure it fits the uintptr size
2142
  // and alignment, because the runtime library only deals with uintptr types.
2143
  // If it does not fit the uintptr size, we need to pass the data by reference
2144
  // instead.
2145
1.30M
  if (!IsByRef &&
2146
1.30M
      
(574k
Ctx.getTypeSizeInChars(Ty) >
2147
574k
           Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
2148
574k
       
Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())572k
)) {
2149
2.09k
    IsByRef = true;
2150
2.09k
  }
2151
2152
1.30M
  return IsByRef;
2153
1.30M
}
2154
2155
566k
unsigned Sema::getOpenMPNestingLevel() const {
2156
566k
  assert(getLangOpts().OpenMP);
2157
566k
  return DSAStack->getNestingLevel();
2158
566k
}
2159
2160
268k
bool Sema::isInOpenMPTargetExecutionDirective() const {
2161
268k
  return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
2162
268k
          
!94.5k
DSAStack94.5k
->isClauseParsingMode()) ||
2163
268k
         
DSAStack174k
->hasDirective(
2164
174k
             [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
2165
210k
                SourceLocation) -> bool {
2166
210k
               return isOpenMPTargetExecutionDirective(K);
2167
210k
             },
2168
174k
             false);
2169
268k
}
2170
2171
VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo,
2172
1.71M
                                    unsigned StopAt) {
2173
1.71M
  assert(LangOpts.OpenMP && "OpenMP is not allowed");
2174
0
  D = getCanonicalDecl(D);
2175
2176
1.71M
  auto *VD = dyn_cast<VarDecl>(D);
2177
  // Do not capture constexpr variables.
2178
1.71M
  if (VD && 
VD->isConstexpr()1.69M
)
2179
80
    return nullptr;
2180
2181
  // If we want to determine whether the variable should be captured from the
2182
  // perspective of the current capturing scope, and we've already left all the
2183
  // capturing scopes of the top directive on the stack, check from the
2184
  // perspective of its parent directive (if any) instead.
2185
1.71M
  DSAStackTy::ParentDirectiveScope InParentDirectiveRAII(
2186
1.71M
      *DSAStack, CheckScopeInfo && 
DSAStack286k
->isBodyComplete()286k
);
2187
2188
  // If we are attempting to capture a global variable in a directive with
2189
  // 'target' we return true so that this global is also mapped to the device.
2190
  //
2191
1.71M
  if (VD && 
!VD->hasLocalStorage()1.69M
&&
2192
1.71M
      
(363k
getCurCapturedRegion()363k
||
getCurBlock()159k
||
getCurLambda()158k
)) {
2193
209k
    if (isInOpenMPTargetExecutionDirective()) {
2194
164k
      DSAStackTy::DSAVarData DVarTop =
2195
164k
          DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
2196
164k
      if (DVarTop.CKind != OMPC_unknown && 
DVarTop.RefExpr70.0k
)
2197
63.2k
        return VD;
2198
      // If the declaration is enclosed in a 'declare target' directive,
2199
      // then it should not be captured.
2200
      //
2201
101k
      if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
2202
318
        return nullptr;
2203
100k
      CapturedRegionScopeInfo *CSI = nullptr;
2204
100k
      for (FunctionScopeInfo *FSI : llvm::drop_begin(
2205
100k
               llvm::reverse(FunctionScopes),
2206
100k
               
CheckScopeInfo100k
?
(FunctionScopes.size() - (StopAt + 1))53.6k
:
047.0k
)) {
2207
100k
        if (!isa<CapturingScopeInfo>(FSI))
2208
32
          return nullptr;
2209
100k
        if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI))
2210
100k
          if (RSI->CapRegionKind == CR_OpenMP) {
2211
100k
            CSI = RSI;
2212
100k
            break;
2213
100k
          }
2214
100k
      }
2215
100k
      assert(CSI && "Failed to find CapturedRegionScopeInfo");
2216
0
      SmallVector<OpenMPDirectiveKind, 4> Regions;
2217
100k
      getOpenMPCaptureRegions(Regions,
2218
100k
                              DSAStack->getDirective(CSI->OpenMPLevel));
2219
100k
      if (Regions[CSI->OpenMPCaptureLevel] != OMPD_task)
2220
84.3k
        return VD;
2221
100k
    }
2222
61.6k
    if (isInOpenMPDeclareTargetContext()) {
2223
      // Try to mark variable as declare target if it is used in capturing
2224
      // regions.
2225
121
      if (LangOpts.OpenMP <= 45 &&
2226
121
          
!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)30
)
2227
10
        checkDeclIsAllowedInOpenMPTarget(nullptr, VD);
2228
121
      return nullptr;
2229
121
    }
2230
61.6k
  }
2231
2232
1.57M
  if (CheckScopeInfo) {
2233
208k
    bool OpenMPFound = false;
2234
211k
    for (unsigned I = StopAt + 1; I > 0; 
--I3.37k
) {
2235
211k
      FunctionScopeInfo *FSI = FunctionScopes[I - 1];
2236
211k
      if (!isa<CapturingScopeInfo>(FSI))
2237
155k
        return nullptr;
2238
55.3k
      if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI))
2239
52.0k
        if (RSI->CapRegionKind == CR_OpenMP) {
2240
51.9k
          OpenMPFound = true;
2241
51.9k
          break;
2242
51.9k
        }
2243
55.3k
    }
2244
52.5k
    if (!OpenMPFound)
2245
531
      return nullptr;
2246
52.5k
  }
2247
2248
1.41M
  if (DSAStack->getCurrentDirective() != OMPD_unknown &&
2249
1.41M
      
(1.39M
!1.39M
DSAStack1.39M
->isClauseParsingMode() ||
2250
1.39M
       
DSAStack297k
->getParentDirective() != OMPD_unknown297k
)) {
2251
1.38M
    auto &&Info = DSAStack->isLoopControlVariable(D);
2252
1.38M
    if (Info.first ||
2253
1.38M
        
(1.37M
VD1.37M
&&
VD->hasLocalStorage()1.36M
&&
2254
1.37M
         
isImplicitOrExplicitTaskingRegion(1.30M
DSAStack1.30M
->getCurrentDirective())) ||
2255
1.38M
        
(361k
VD361k
&&
DSAStack350k
->isForceVarCapturing()350k
))
2256
1.01M
      return VD ? 
VD1.01M
:
Info.second772
;
2257
361k
    DSAStackTy::DSAVarData DVarTop =
2258
361k
        DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
2259
361k
    if (DVarTop.CKind != OMPC_unknown && 
isOpenMPPrivate(DVarTop.CKind)46.9k
&&
2260
361k
        
(41.5k
!VD41.5k
||
VD->hasLocalStorage()36.1k
||
!DVarTop.AppliedToPointee19.3k
))
2261
41.4k
      return VD ? 
VD36.0k
:
cast<VarDecl>(DVarTop.PrivateCopy->getDecl())5.44k
;
2262
    // Threadprivate variables must not be captured.
2263
319k
    if (isOpenMPThreadPrivate(DVarTop.CKind))
2264
2.43k
      return nullptr;
2265
    // The variable is not private or it is the variable in the directive with
2266
    // default(none) clause and not used in any clause.
2267
317k
    DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA(
2268
317k
        D,
2269
317k
        [](OpenMPClauseKind C, bool AppliedToPointee) {
2270
172k
          return isOpenMPPrivate(C) && 
!AppliedToPointee5.10k
;
2271
172k
        },
2272
687k
        [](OpenMPDirectiveKind) { return true; },
2273
317k
        DSAStack->isClauseParsingMode());
2274
    // Global shared must not be captured.
2275
317k
    if (VD && 
!VD->hasLocalStorage()311k
&&
DVarPrivate.CKind == OMPC_unknown36.2k
&&
2276
317k
        
(35.9k
(35.9k
DSAStack35.9k
->getDefaultDSA() != DSA_none35.9k
&&
2277
35.9k
          
DSAStack35.5k
->getDefaultDSA() != DSA_firstprivate35.5k
) ||
2278
35.9k
         
DVarTop.CKind == OMPC_shared550
))
2279
35.5k
      return nullptr;
2280
281k
    if (DVarPrivate.CKind != OMPC_unknown ||
2281
281k
        
(276k
VD276k
&&
(272k
DSAStack272k
->getDefaultDSA() == DSA_none272k
||
2282
272k
                
DSAStack271k
->getDefaultDSA() == DSA_firstprivate271k
)))
2283
5.37k
      return VD ? 
VD3.95k
:
cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl())1.42k
;
2284
281k
  }
2285
311k
  return nullptr;
2286
1.41M
}
2287
2288
void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
2289
25.1k
                                        unsigned Level) const {
2290
25.1k
  FunctionScopesIndex -= getOpenMPCaptureLevels(DSAStack->getDirective(Level));
2291
25.1k
}
2292
2293
168k
void Sema::startOpenMPLoop() {
2294
168k
  assert(LangOpts.OpenMP && "OpenMP must be enabled.");
2295
168k
  if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
2296
166k
    DSAStack->loopInit();
2297
168k
}
2298
2299
127
void Sema::startOpenMPCXXRangeFor() {
2300
127
  assert(LangOpts.OpenMP && "OpenMP must be enabled.");
2301
127
  if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2302
127
    DSAStack->resetPossibleLoopCounter();
2303
127
    DSAStack->loopStart();
2304
127
  }
2305
127
}
2306
2307
OpenMPClauseKind Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level,
2308
2.88M
                                           unsigned CapLevel) const {
2309
2.88M
  assert(LangOpts.OpenMP && "OpenMP is not allowed");
2310
2.88M
  if (DSAStack->hasExplicitDirective(isOpenMPTaskingDirective, Level)) {
2311
254k
    bool IsTriviallyCopyable =
2312
254k
        D->getType().getNonReferenceType().isTriviallyCopyableType(Context) &&
2313
254k
        !D->getType()
2314
208k
             .getNonReferenceType()
2315
208k
             .getCanonicalType()
2316
208k
             ->getAsCXXRecordDecl();
2317
254k
    OpenMPDirectiveKind DKind = DSAStack->getDirective(Level);
2318
254k
    SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2319
254k
    getOpenMPCaptureRegions(CaptureRegions, DKind);
2320
254k
    if (isOpenMPTaskingDirective(CaptureRegions[CapLevel]) &&
2321
254k
        
(220k
IsTriviallyCopyable220k
||
2322
220k
         
!isOpenMPTaskLoopDirective(CaptureRegions[CapLevel])39.0k
)) {
2323
190k
      if (DSAStack->hasExplicitDSA(
2324
190k
              D,
2325
190k
              [](OpenMPClauseKind K, bool) 
{ return K == OMPC_firstprivate; }28.1k
,
2326
190k
              Level, /*NotLastprivate=*/true))
2327
6.42k
        return OMPC_firstprivate;
2328
184k
      DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level);
2329
184k
      if (DVar.CKind != OMPC_shared &&
2330
184k
          
!132k
DSAStack132k
->isLoopControlVariable(D, Level).first &&
!DVar.RefExpr129k
) {
2331
115k
        DSAStack->addImplicitTaskFirstprivate(Level, D);
2332
115k
        return OMPC_firstprivate;
2333
115k
      }
2334
184k
    }
2335
254k
  }
2336
2.76M
  if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2337
2.34M
    if (DSAStack->getAssociatedLoops() > 0 && 
!1.13M
DSAStack1.13M
->isLoopStarted()) {
2338
43.6k
      DSAStack->resetPossibleLoopCounter(D);
2339
43.6k
      DSAStack->loopStart();
2340
43.6k
      return OMPC_private;
2341
43.6k
    }
2342
2.30M
    if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() ||
2343
2.30M
         
DSAStack1.88M
->isLoopControlVariable(D).first1.88M
) &&
2344
2.30M
        
!420k
DSAStack420k
->hasExplicitDSA(
2345
420k
            D, [](OpenMPClauseKind K, bool) 
{ return K != OMPC_private; }59.3k
,
2346
420k
            Level) &&
2347
2.30M
        
!isOpenMPSimdDirective(409k
DSAStack409k
->getCurrentDirective()))
2348
98.1k
      return OMPC_private;
2349
2.30M
  }
2350
2.62M
  if (const auto *VD = dyn_cast<VarDecl>(D)) {
2351
2.62M
    if (DSAStack->isThreadPrivate(const_cast<VarDecl *>(VD)) &&
2352
2.62M
        
DSAStack13.3k
->isForceVarCapturing()13.3k
&&
2353
2.62M
        
!350
DSAStack350
->hasExplicitDSA(
2354
350
            D, [](OpenMPClauseKind K, bool) 
{ return K == OMPC_copyin; }344
,
2355
350
            Level))
2356
6
      return OMPC_private;
2357
2.62M
  }
2358
  // User-defined allocators are private since they must be defined in the
2359
  // context of target region.
2360
2.62M
  if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level) &&
2361
2.62M
      
DSAStack1.69M
->isUsesAllocatorsDecl(Level, D).getValueOr(
2362
1.69M
          DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) ==
2363
1.69M
          DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator)
2364
10
    return OMPC_private;
2365
2.62M
  return (DSAStack->hasExplicitDSA(
2366
2.62M
              D, [](OpenMPClauseKind K, bool) 
{ return K == OMPC_private; }228k
,
2367
2.62M
              Level) ||
2368
2.62M
          
(2.58M
DSAStack2.58M
->isClauseParsingMode()2.58M
&&
2369
2.58M
           
DSAStack530k
->getClauseParsingMode() == OMPC_private530k
) ||
2370
          // Consider taskgroup reduction descriptor variable a private
2371
          // to avoid possible capture in the region.
2372
2.62M
          
(2.57M
DSAStack2.57M
->hasExplicitDirective(
2373
2.57M
               [](OpenMPDirectiveKind K) {
2374
2.57M
                 return K == OMPD_taskgroup ||
2375
2.57M
                        
(2.56M
(2.56M
isOpenMPParallelDirective(K)2.56M
||
2376
2.56M
                          
isOpenMPWorksharingDirective(K)1.47M
) &&
2377
2.56M
                         
!isOpenMPSimdDirective(K)1.15M
);
2378
2.57M
               },
2379
2.57M
               Level) &&
2380
2.57M
           
DSAStack607k
->isTaskgroupReductionRef(D, Level)607k
))
2381
2.62M
             ? 
OMPC_private49.3k
2382
2.62M
             : 
OMPC_unknown2.57M
;
2383
2.62M
}
2384
2385
void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
2386
371k
                                unsigned Level) {
2387
371k
  assert(LangOpts.OpenMP && "OpenMP is not allowed");
2388
0
  D = getCanonicalDecl(D);
2389
371k
  OpenMPClauseKind OMPC = OMPC_unknown;
2390
460k
  for (unsigned I = 
DSAStack371k
->getNestingLevel() + 1; I > Level;
--I89.7k
) {
2391
371k
    const unsigned NewLevel = I - 1;
2392
371k
    if (DSAStack->hasExplicitDSA(
2393
371k
            D,
2394
371k
            [&OMPC](const OpenMPClauseKind K, bool AppliedToPointee) {
2395
91.1k
              if (isOpenMPPrivate(K) && 
!AppliedToPointee89.8k
) {
2396
88.7k
                OMPC = K;
2397
88.7k
                return true;
2398
88.7k
              }
2399
2.39k
              return false;
2400
91.1k
            },
2401
371k
            NewLevel))
2402
88.7k
      break;
2403
282k
    if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
2404
282k
            D, NewLevel,
2405
282k
            [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
2406
282k
               OpenMPClauseKind) 
{ return true; }9.84k
)) {
2407
9.84k
      OMPC = OMPC_map;
2408
9.84k
      break;
2409
9.84k
    }
2410
272k
    if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
2411
272k
                                       NewLevel)) {
2412
182k
      OMPC = OMPC_map;
2413
182k
      if (DSAStack->mustBeFirstprivateAtLevel(
2414
182k
              NewLevel, getVariableCategoryFromDecl(LangOpts, D)))
2415
141k
        OMPC = OMPC_firstprivate;
2416
182k
      break;
2417
182k
    }
2418
272k
  }
2419
371k
  if (OMPC != OMPC_unknown)
2420
281k
    FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, unsigned(OMPC)));
2421
371k
}
2422
2423
bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D, unsigned Level,
2424
1.33M
                                      unsigned CaptureLevel) const {
2425
1.33M
  assert(LangOpts.OpenMP && "OpenMP is not allowed");
2426
  // Return true if the current level is no longer enclosed in a target region.
2427
2428
0
  SmallVector<OpenMPDirectiveKind, 4> Regions;
2429
1.33M
  getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
2430
1.33M
  const auto *VD = dyn_cast<VarDecl>(D);
2431
1.33M
  return VD && !VD->hasLocalStorage() &&
2432
1.33M
         
DSAStack47.7k
->hasExplicitDirective(isOpenMPTargetExecutionDirective,
2433
47.7k
                                        Level) &&
2434
1.33M
         
Regions[CaptureLevel] != OMPD_task25.1k
;
2435
1.33M
}
2436
2437
bool Sema::isOpenMPGlobalCapturedDecl(ValueDecl *D, unsigned Level,
2438
58.1k
                                      unsigned CaptureLevel) const {
2439
58.1k
  assert(LangOpts.OpenMP && "OpenMP is not allowed");
2440
  // Return true if the current level is no longer enclosed in a target region.
2441
2442
58.1k
  if (const auto *VD = dyn_cast<VarDecl>(D)) {
2443
58.1k
    if (!VD->hasLocalStorage()) {
2444
58.1k
      if (isInOpenMPTargetExecutionDirective())
2445
44.4k
        return true;
2446
13.7k
      DSAStackTy::DSAVarData TopDVar =
2447
13.7k
          DSAStack->getTopDSA(D, /*FromParent=*/false);
2448
13.7k
      unsigned NumLevels =
2449
13.7k
          getOpenMPCaptureLevels(DSAStack->getDirective(Level));
2450
13.7k
      if (Level == 0)
2451
10.6k
        return (NumLevels == CaptureLevel + 1) && 
TopDVar.CKind != OMPC_shared9.00k
;
2452
4.09k
      
do 3.05k
{
2453
4.09k
        --Level;
2454
4.09k
        DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level);
2455
4.09k
        if (DVar.CKind != OMPC_shared)
2456
316
          return true;
2457
4.09k
      } while (
Level > 03.77k
);
2458
3.05k
    }
2459
58.1k
  }
2460
2.73k
  return true;
2461
58.1k
}
2462
2463
80.9k
void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
2464
2465
void Sema::ActOnOpenMPBeginDeclareVariant(SourceLocation Loc,
2466
246
                                          OMPTraitInfo &TI) {
2467
246
  OMPDeclareVariantScopes.push_back(OMPDeclareVariantScope(TI));
2468
246
}
2469
2470
242
void Sema::ActOnOpenMPEndDeclareVariant() {
2471
242
  assert(isInOpenMPDeclareVariantScope() &&
2472
242
         "Not in OpenMP declare variant scope!");
2473
2474
0
  OMPDeclareVariantScopes.pop_back();
2475
242
}
2476
2477
void Sema::finalizeOpenMPDelayedAnalysis(const FunctionDecl *Caller,
2478
                                         const FunctionDecl *Callee,
2479
14.5k
                                         SourceLocation Loc) {
2480
14.5k
  assert(LangOpts.OpenMP && "Expected OpenMP compilation mode.");
2481
0
  Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
2482
14.5k
      OMPDeclareTargetDeclAttr::getDeviceType(Caller->getMostRecentDecl());
2483
  // Ignore host functions during device analyzis.
2484
14.5k
  if (LangOpts.OpenMPIsDevice &&
2485
14.5k
      
(863
!DevTy863
||
*DevTy == OMPDeclareTargetDeclAttr::DT_Host656
))
2486
207
    return;
2487
  // Ignore nohost functions during host analyzis.
2488
14.3k
  if (!LangOpts.OpenMPIsDevice && 
DevTy13.6k
&&
2489
14.3k
      
*DevTy == OMPDeclareTargetDeclAttr::DT_NoHost243
)
2490
0
    return;
2491
14.3k
  const FunctionDecl *FD = Callee->getMostRecentDecl();
2492
14.3k
  DevTy = OMPDeclareTargetDeclAttr::getDeviceType(FD);
2493
14.3k
  if (LangOpts.OpenMPIsDevice && 
DevTy656
&&
2494
14.3k
      
*DevTy == OMPDeclareTargetDeclAttr::DT_Host315
) {
2495
    // Diagnose host function called during device codegen.
2496
4
    StringRef HostDevTy =
2497
4
        getOpenMPSimpleClauseTypeName(OMPC_device_type, OMPC_DEVICE_TYPE_host);
2498
4
    Diag(Loc, diag::err_omp_wrong_device_function_call) << HostDevTy << 0;
2499
4
    Diag(*OMPDeclareTargetDeclAttr::getLocation(FD),
2500
4
         diag::note_omp_marked_device_type_here)
2501
4
        << HostDevTy;
2502
4
    return;
2503
4
  }
2504
14.3k
  if (!LangOpts.OpenMPIsDevice && 
DevTy13.6k
&&
2505
14.3k
      
*DevTy == OMPDeclareTargetDeclAttr::DT_NoHost284
) {
2506
    // Diagnose nohost function called during host codegen.
2507
25
    StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName(
2508
25
        OMPC_device_type, OMPC_DEVICE_TYPE_nohost);
2509
25
    Diag(Loc, diag::err_omp_wrong_device_function_call) << NoHostDevTy << 1;
2510
25
    Diag(*OMPDeclareTargetDeclAttr::getLocation(FD),
2511
25
         diag::note_omp_marked_device_type_here)
2512
25
        << NoHostDevTy;
2513
25
  }
2514
14.3k
}
2515
2516
void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
2517
                               const DeclarationNameInfo &DirName,
2518
342k
                               Scope *CurScope, SourceLocation Loc) {
2519
342k
  DSAStack->push(DKind, DirName, CurScope, Loc);
2520
342k
  PushExpressionEvaluationContext(
2521
342k
      ExpressionEvaluationContext::PotentiallyEvaluated);
2522
342k
}
2523
2524
223k
void Sema::StartOpenMPClause(OpenMPClauseKind K) {
2525
223k
  DSAStack->setClauseParsingMode(K);
2526
223k
}
2527
2528
223k
void Sema::EndOpenMPClause() {
2529
223k
  DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
2530
223k
  CleanupVarDeclMarking();
2531
223k
}
2532
2533
static std::pair<ValueDecl *, bool>
2534
getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
2535
               SourceRange &ERange, bool AllowArraySection = false);
2536
2537
/// Check consistency of the reduction clauses.
2538
static void checkReductionClauses(Sema &S, DSAStackTy *Stack,
2539
291k
                                  ArrayRef<OMPClause *> Clauses) {
2540
291k
  bool InscanFound = false;
2541
291k
  SourceLocation InscanLoc;
2542
  // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions.
2543
  // A reduction clause without the inscan reduction-modifier may not appear on
2544
  // a construct on which a reduction clause with the inscan reduction-modifier
2545
  // appears.
2546
291k
  for (OMPClause *C : Clauses) {
2547
199k
    if (C->getClauseKind() != OMPC_reduction)
2548
176k
      continue;
2549
22.8k
    auto *RC = cast<OMPReductionClause>(C);
2550
22.8k
    if (RC->getModifier() == OMPC_REDUCTION_inscan) {
2551
118
      InscanFound = true;
2552
118
      InscanLoc = RC->getModifierLoc();
2553
118
      continue;
2554
118
    }
2555
22.6k
    if (RC->getModifier() == OMPC_REDUCTION_task) {
2556
      // OpenMP 5.0, 2.19.5.4 reduction Clause.
2557
      // A reduction clause with the task reduction-modifier may only appear on
2558
      // a parallel construct, a worksharing construct or a combined or
2559
      // composite construct for which any of the aforementioned constructs is a
2560
      // constituent construct and simd or loop are not constituent constructs.
2561
351
      OpenMPDirectiveKind CurDir = Stack->getCurrentDirective();
2562
351
      if (!(isOpenMPParallelDirective(CurDir) ||
2563
351
            
isOpenMPWorksharingDirective(CurDir)142
) ||
2564
351
          
isOpenMPSimdDirective(CurDir)251
)
2565
142
        S.Diag(RC->getModifierLoc(),
2566
142
               diag::err_omp_reduction_task_not_parallel_or_worksharing);
2567
351
      continue;
2568
351
    }
2569
22.6k
  }
2570
291k
  if (InscanFound) {
2571
130
    for (OMPClause *C : Clauses) {
2572
130
      if (C->getClauseKind() != OMPC_reduction)
2573
0
        continue;
2574
130
      auto *RC = cast<OMPReductionClause>(C);
2575
130
      if (RC->getModifier() != OMPC_REDUCTION_inscan) {
2576
12
        S.Diag(RC->getModifier() == OMPC_REDUCTION_unknown
2577
12
                   ? 
RC->getBeginLoc()6
2578
12
                   : 
RC->getModifierLoc()6
,
2579
12
               diag::err_omp_inscan_reduction_expected);
2580
12
        S.Diag(InscanLoc, diag::note_omp_previous_inscan_reduction);
2581
12
        continue;
2582
12
      }
2583
178
      
for (Expr *Ref : RC->varlists())118
{
2584
178
        assert(Ref && "NULL expr in OpenMP nontemporal clause.");
2585
0
        SourceLocation ELoc;
2586
178
        SourceRange ERange;
2587
178
        Expr *SimpleRefExpr = Ref;
2588
178
        auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
2589
178
                                  /*AllowArraySection=*/true);
2590
178
        ValueDecl *D = Res.first;
2591
178
        if (!D)
2592
12
          continue;
2593
166
        if (!Stack->isUsedInScanDirective(getCanonicalDecl(D))) {
2594
30
          S.Diag(Ref->getExprLoc(),
2595
30
                 diag::err_omp_reduction_not_inclusive_exclusive)
2596
30
              << Ref->getSourceRange();
2597
30
        }
2598
166
      }
2599
118
    }
2600
118
  }
2601
291k
}
2602
2603
static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
2604
                                 ArrayRef<OMPClause *> Clauses);
2605
static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2606
                                 bool WithInit);
2607
2608
static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
2609
                              const ValueDecl *D,
2610
                              const DSAStackTy::DSAVarData &DVar,
2611
                              bool IsLoopIterVar = false);
2612
2613
342k
void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
2614
  // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
2615
  //  A variable of class type (or array thereof) that appears in a lastprivate
2616
  //  clause requires an accessible, unambiguous default constructor for the
2617
  //  class type, unless the list item is also specified in a firstprivate
2618
  //  clause.
2619
342k
  if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
2620
291k
    for (OMPClause *C : D->clauses()) {
2621
199k
      if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
2622
5.84k
        SmallVector<Expr *, 8> PrivateCopies;
2623
10.3k
        for (Expr *DE : Clause->varlists()) {
2624
10.3k
          if (DE->isValueDependent() || 
DE->isTypeDependent()8.19k
) {
2625
2.13k
            PrivateCopies.push_back(nullptr);
2626
2.13k
            continue;
2627
2.13k
          }
2628
8.19k
          auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
2629
8.19k
          auto *VD = cast<VarDecl>(DRE->getDecl());
2630
8.19k
          QualType Type = VD->getType().getNonReferenceType();
2631
8.19k
          const DSAStackTy::DSAVarData DVar =
2632
8.19k
              DSAStack->getTopDSA(VD, /*FromParent=*/false);
2633
8.19k
          if (DVar.CKind == OMPC_lastprivate) {
2634
            // Generate helper private variable and initialize it with the
2635
            // default value. The address of the original variable is replaced
2636
            // by the address of the new private variable in CodeGen. This new
2637
            // variable is not added to IdResolver, so the code in the OpenMP
2638
            // region uses original variable for proper diagnostics.
2639
7.48k
            VarDecl *VDPrivate = buildVarDecl(
2640
7.48k
                *this, DE->getExprLoc(), Type.getUnqualifiedType(),
2641
7.48k
                VD->getName(), VD->hasAttrs() ? 
&VD->getAttrs()92
:
nullptr7.39k
, DRE);
2642
7.48k
            ActOnUninitializedDecl(VDPrivate);
2643
7.48k
            if (VDPrivate->isInvalidDecl()) {
2644
0
              PrivateCopies.push_back(nullptr);
2645
0
              continue;
2646
0
            }
2647
7.48k
            PrivateCopies.push_back(buildDeclRefExpr(
2648
7.48k
                *this, VDPrivate, DE->getType(), DE->getExprLoc()));
2649
7.48k
          } else {
2650
            // The variable is also a firstprivate, so initialization sequence
2651
            // for private copy is generated already.
2652
707
            PrivateCopies.push_back(nullptr);
2653
707
          }
2654
8.19k
        }
2655
5.84k
        Clause->setPrivateCopies(PrivateCopies);
2656
5.84k
        continue;
2657
5.84k
      }
2658
      // Finalize nontemporal clause by handling private copies, if any.
2659
193k
      if (auto *Clause = dyn_cast<OMPNontemporalClause>(C)) {
2660
438
        SmallVector<Expr *, 8> PrivateRefs;
2661
650
        for (Expr *RefExpr : Clause->varlists()) {
2662
650
          assert(RefExpr && "NULL expr in OpenMP nontemporal clause.");
2663
0
          SourceLocation ELoc;
2664
650
          SourceRange ERange;
2665
650
          Expr *SimpleRefExpr = RefExpr;
2666
650
          auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
2667
650
          if (Res.second)
2668
            // It will be analyzed later.
2669
52
            PrivateRefs.push_back(RefExpr);
2670
650
          ValueDecl *D = Res.first;
2671
650
          if (!D)
2672
52
            continue;
2673
2674
598
          const DSAStackTy::DSAVarData DVar =
2675
598
              DSAStack->getTopDSA(D, /*FromParent=*/false);
2676
598
          PrivateRefs.push_back(DVar.PrivateCopy ? 
DVar.PrivateCopy20
2677
598
                                                 : 
SimpleRefExpr578
);
2678
598
        }
2679
438
        Clause->setPrivateRefs(PrivateRefs);
2680
438
        continue;
2681
438
      }
2682
192k
      if (auto *Clause = dyn_cast<OMPUsesAllocatorsClause>(C)) {
2683
474
        for (unsigned I = 0, E = Clause->getNumberOfAllocators(); I < E; 
++I256
) {
2684
256
          OMPUsesAllocatorsClause::Data D = Clause->getAllocatorData(I);
2685
256
          auto *DRE = dyn_cast<DeclRefExpr>(D.Allocator->IgnoreParenImpCasts());
2686
256
          if (!DRE)
2687
0
            continue;
2688
256
          ValueDecl *VD = DRE->getDecl();
2689
256
          if (!VD || !isa<VarDecl>(VD))
2690
56
            continue;
2691
200
          DSAStackTy::DSAVarData DVar =
2692
200
              DSAStack->getTopDSA(VD, /*FromParent=*/false);
2693
          // OpenMP [2.12.5, target Construct]
2694
          // Memory allocators that appear in a uses_allocators clause cannot
2695
          // appear in other data-sharing attribute clauses or data-mapping
2696
          // attribute clauses in the same construct.
2697
200
          Expr *MapExpr = nullptr;
2698
200
          if (DVar.RefExpr ||
2699
200
              
DSAStack198
->checkMappableExprComponentListsForDecl(
2700
198
                  VD, /*CurrentRegionOnly=*/true,
2701
198
                  [VD, &MapExpr](
2702
198
                      OMPClauseMappableExprCommon::MappableExprComponentListRef
2703
198
                          MapExprComponents,
2704
198
                      OpenMPClauseKind C) {
2705
2
                    auto MI = MapExprComponents.rbegin();
2706
2
                    auto ME = MapExprComponents.rend();
2707
2
                    if (MI != ME &&
2708
2
                        MI->getAssociatedDeclaration()->getCanonicalDecl() ==
2709
2
                            VD->getCanonicalDecl()) {
2710
2
                      MapExpr = MI->getAssociatedExpression();
2711
2
                      return true;
2712
2
                    }
2713
0
                    return false;
2714
4
                  })) {
2715
4
            Diag(D.Allocator->getExprLoc(),
2716
4
                 diag::err_omp_allocator_used_in_clauses)
2717
4
                << D.Allocator->getSourceRange();
2718
4
            if (DVar.RefExpr)
2719
2
              reportOriginalDsa(*this, DSAStack, VD, DVar);
2720
2
            else
2721
2
              Diag(MapExpr->getExprLoc(), diag::note_used_here)
2722
2
                  << MapExpr->getSourceRange();
2723
4
          }
2724
200
        }
2725
218
        continue;
2726
218
      }
2727
192k
    }
2728
    // Check allocate clauses.
2729
291k
    if (!CurContext->isDependentContext())
2730
214k
      checkAllocateClauses(*this, DSAStack, D->clauses());
2731
291k
    checkReductionClauses(*this, DSAStack, D->clauses());
2732
291k
  }
2733
2734
342k
  DSAStack->pop();
2735
342k
  DiscardCleanupsInEvaluationContext();
2736
342k
  PopExpressionEvaluationContext();
2737
342k
}
2738
2739
static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
2740
                                     Expr *NumIterations, Sema &SemaRef,
2741
                                     Scope *S, DSAStackTy *Stack);
2742
2743
namespace {
2744
2745
class VarDeclFilterCCC final : public CorrectionCandidateCallback {
2746
private:
2747
  Sema &SemaRef;
2748
2749
public:
2750
40
  explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
2751
16
  bool ValidateCandidate(const TypoCorrection &Candidate) override {
2752
16
    NamedDecl *ND = Candidate.getCorrectionDecl();
2753
16
    if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
2754
16
      return VD->hasGlobalStorage() &&
2755
16
             SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
2756
16
                                   SemaRef.getCurScope());
2757
16
    }
2758
0
    return false;
2759
16
  }
2760
2761
40
  std::unique_ptr<CorrectionCandidateCallback> clone() override {
2762
40
    return std::make_unique<VarDeclFilterCCC>(*this);
2763
40
  }
2764
};
2765
2766
class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
2767
private:
2768
  Sema &SemaRef;
2769
2770
public:
2771
18
  explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
2772
18
  bool ValidateCandidate(const TypoCorrection &Candidate) override {
2773
18
    NamedDecl *ND = Candidate.getCorrectionDecl();
2774
18
    if (ND && 
(0
(0
isa<VarDecl>(ND)0
&&
ND->getKind() == Decl::Var0
) ||
2775
0
               isa<FunctionDecl>(ND))) {
2776
0
      return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
2777
0
                                   SemaRef.getCurScope());
2778
0
    }
2779
18
    return false;
2780
18
  }
2781
2782
18
  std::unique_ptr<CorrectionCandidateCallback> clone() override {
2783
18
    return std::make_unique<VarOrFuncDeclFilterCCC>(*this);
2784
18
  }
2785
};
2786
2787
} // namespace
2788
2789
ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
2790
                                         CXXScopeSpec &ScopeSpec,
2791
                                         const DeclarationNameInfo &Id,
2792
3.47k
                                         OpenMPDirectiveKind Kind) {
2793
3.47k
  LookupResult Lookup(*this, Id, LookupOrdinaryName);
2794
3.47k
  LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
2795
2796
3.47k
  if (Lookup.isAmbiguous())
2797
8
    return ExprError();
2798
2799
3.46k
  VarDecl *VD;
2800
3.46k
  if (!Lookup.isSingleResult()) {
2801
40
    VarDeclFilterCCC CCC(*this);
2802
40
    if (TypoCorrection Corrected =
2803
40
            CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC,
2804
40
                        CTK_ErrorRecovery)) {
2805
8
      diagnoseTypo(Corrected,
2806
8
                   PDiag(Lookup.empty()
2807
8
                             ? diag::err_undeclared_var_use_suggest
2808
8
                             : 
diag::err_omp_expected_var_arg_suggest0
)
2809
8
                       << Id.getName());
2810
8
      VD = Corrected.getCorrectionDeclAs<VarDecl>();
2811
32
    } else {
2812
32
      Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
2813
32
                                       : 
diag::err_omp_expected_var_arg0
)
2814
32
          << Id.getName();
2815
32
      return ExprError();
2816
32
    }
2817
3.42k
  } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
2818
16
    Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
2819
16
    Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
2820
16
    return ExprError();
2821
16
  }
2822
3.41k
  Lookup.suppressDiagnostics();
2823
2824
  // OpenMP [2.9.2, Syntax, C/C++]
2825
  //   Variables must be file-scope, namespace-scope, or static block-scope.
2826
3.41k
  if (Kind == OMPD_threadprivate && 
!VD->hasGlobalStorage()2.66k
) {
2827
8
    Diag(Id.getLoc(), diag::err_omp_global_var_arg)
2828
8
        << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal();
2829
8
    bool IsDecl =
2830
8
        VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2831
8
    Diag(VD->getLocation(),
2832
8
         IsDecl ? 
diag::note_previous_decl0
: diag::note_defined_here)
2833
8
        << VD;
2834
8
    return ExprError();
2835
8
  }
2836
2837
3.40k
  VarDecl *CanonicalVD = VD->getCanonicalDecl();
2838
3.40k
  NamedDecl *ND = CanonicalVD;
2839
  // OpenMP [2.9.2, Restrictions, C/C++, p.2]
2840
  //   A threadprivate directive for file-scope variables must appear outside
2841
  //   any definition or declaration.
2842
3.40k
  if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
2843
3.40k
      
!getCurLexicalContext()->isTranslationUnit()1.74k
) {
2844
16
    Diag(Id.getLoc(), diag::err_omp_var_scope)
2845
16
        << getOpenMPDirectiveName(Kind) << VD;
2846
16
    bool IsDecl =
2847
16
        VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2848
16
    Diag(VD->getLocation(),
2849
16
         IsDecl ? 
diag::note_previous_decl0
: diag::note_defined_here)
2850
16
        << VD;
2851
16
    return ExprError();
2852
16
  }
2853
  // OpenMP [2.9.2, Restrictions, C/C++, p.3]
2854
  //   A threadprivate directive for static class member variables must appear
2855
  //   in the class definition, in the same scope in which the member
2856
  //   variables are declared.
2857
3.39k
  if (CanonicalVD->isStaticDataMember() &&
2858
3.39k
      
!CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())204
) {
2859
8
    Diag(Id.getLoc(), diag::err_omp_var_scope)
2860
8
        << getOpenMPDirectiveName(Kind) << VD;
2861
8
    bool IsDecl =
2862
8
        VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2863
8
    Diag(VD->getLocation(),
2864
8
         IsDecl ? diag::note_previous_decl : 
diag::note_defined_here0
)
2865
8
        << VD;
2866
8
    return ExprError();
2867
8
  }
2868
  // OpenMP [2.9.2, Restrictions, C/C++, p.4]
2869
  //   A threadprivate directive for namespace-scope variables must appear
2870
  //   outside any definition or declaration other than the namespace
2871
  //   definition itself.
2872
3.38k
  if (CanonicalVD->getDeclContext()->isNamespace() &&
2873
3.38k
      
(829
!getCurLexicalContext()->isFileContext()829
||
2874
829
       !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
2875
0
    Diag(Id.getLoc(), diag::err_omp_var_scope)
2876
0
        << getOpenMPDirectiveName(Kind) << VD;
2877
0
    bool IsDecl =
2878
0
        VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2879
0
    Diag(VD->getLocation(),
2880
0
         IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2881
0
        << VD;
2882
0
    return ExprError();
2883
0
  }
2884
  // OpenMP [2.9.2, Restrictions, C/C++, p.6]
2885
  //   A threadprivate directive for static block-scope variables must appear
2886
  //   in the scope of the variable and not in a nested scope.
2887
3.38k
  if (CanonicalVD->isLocalVarDecl() && 
CurScope591
&&
2888
3.38k
      
!isDeclInScope(ND, getCurLexicalContext(), CurScope)591
) {
2889
8
    Diag(Id.getLoc(), diag::err_omp_var_scope)
2890
8
        << getOpenMPDirectiveName(Kind) << VD;
2891
8
    bool IsDecl =
2892
8
        VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2893
8
    Diag(VD->getLocation(),
2894
8
         IsDecl ? 
diag::note_previous_decl0
: diag::note_defined_here)
2895
8
        << VD;
2896
8
    return ExprError();
2897
8
  }
2898
2899
  // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
2900
  //   A threadprivate directive must lexically precede all references to any
2901
  //   of the variables in its list.
2902
3.37k
  if (Kind == OMPD_threadprivate && 
VD->isUsed()2.64k
&&
2903
3.37k
      
!85
DSAStack85
->isThreadPrivate(VD)) {
2904
4
    Diag(Id.getLoc(), diag::err_omp_var_used)
2905
4
        << getOpenMPDirectiveName(Kind) << VD;
2906
4
    return ExprError();
2907
4
  }
2908
2909
3.37k
  QualType ExprType = VD->getType().getNonReferenceType();
2910
3.37k
  return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
2911
3.37k
                             SourceLocation(), VD,
2912
3.37k
                             /*RefersToEnclosingVariableOrCapture=*/false,
2913
3.37k
                             Id.getLoc(), ExprType, VK_LValue);
2914
3.37k
}
2915
2916
Sema::DeclGroupPtrTy
2917
Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
2918
2.51k
                                        ArrayRef<Expr *> VarList) {
2919
2.51k
  if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
2920
2.44k
    CurContext->addDecl(D);
2921
2.44k
    return DeclGroupPtrTy::make(DeclGroupRef(D));
2922
2.44k
  }
2923
68
  return nullptr;
2924
2.51k
}
2925
2926
namespace {
2927
class LocalVarRefChecker final
2928
    : public ConstStmtVisitor<LocalVarRefChecker, bool> {
2929
  Sema &SemaRef;
2930
2931
public:
2932
23
  bool VisitDeclRefExpr(const DeclRefExpr *E) {
2933
23
    if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
2934
16
      if (VD->hasLocalStorage()) {
2935
4
        SemaRef.Diag(E->getBeginLoc(),
2936
4
                     diag::err_omp_local_var_in_threadprivate_init)
2937
4
            << E->getSourceRange();
2938
4
        SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
2939
4
            << VD << VD->getSourceRange();
2940
4
        return true;
2941
4
      }
2942
16
    }
2943
19
    return false;
2944
23
  }
2945
2.16k
  bool VisitStmt(const Stmt *S) {
2946
2.16k
    for (const Stmt *Child : S->children()) {
2947
980
      if (Child && Visit(Child))
2948
8
        return true;
2949
980
    }
2950
2.15k
    return false;
2951
2.16k
  }
2952
1.20k
  explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
2953
};
2954
} // namespace
2955
2956
OMPThreadPrivateDecl *
2957
2.75k
Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
2958
2.75k
  SmallVector<Expr *, 8> Vars;
2959
2.89k
  for (Expr *RefExpr : VarList) {
2960
2.89k
    auto *DE = cast<DeclRefExpr>(RefExpr);
2961
2.89k
    auto *VD = cast<VarDecl>(DE->getDecl());
2962
2.89k
    SourceLocation ILoc = DE->getExprLoc();
2963
2964
    // Mark variable as used.
2965
2.89k
    VD->setReferenced();
2966
2.89k
    VD->markUsed(Context);
2967
2968
2.89k
    QualType QType = VD->getType();
2969
2.89k
    if (QType->isDependentType() || 
QType->isInstantiationDependentType()2.71k
) {
2970
      // It will be analyzed later.
2971
178
      Vars.push_back(DE);
2972
178
      continue;
2973
178
    }
2974
2975
    // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2976
    //   A threadprivate variable must not have an incomplete type.
2977
2.71k
    if (RequireCompleteType(ILoc, VD->getType(),
2978
2.71k
                            diag::err_omp_threadprivate_incomplete_type)) {
2979
4
      continue;
2980
4
    }
2981
2982
    // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2983
    //   A threadprivate variable must not have a reference type.
2984
2.70k
    if (VD->getType()->isReferenceType()) {
2985
4
      Diag(ILoc, diag::err_omp_ref_type_arg)
2986
4
          << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
2987
4
      bool IsDecl =
2988
4
          VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2989
4
      Diag(VD->getLocation(),
2990
4
           IsDecl ? 
diag::note_previous_decl0
: diag::note_defined_here)
2991
4
          << VD;
2992
4
      continue;
2993
4
    }
2994
2995
    // Check if this is a TLS variable. If TLS is not being supported, produce
2996
    // the corresponding diagnostic.
2997
2.70k
    if ((VD->getTLSKind() != VarDecl::TLS_None &&
2998
2.70k
         
!(40
VD->hasAttr<OMPThreadPrivateDeclAttr>()40
&&
2999
40
           
getLangOpts().OpenMPUseTLS36
&&
3000
40
           
getASTContext().getTargetInfo().isTLSSupported()36
)) ||
3001
2.70k
        
(2.70k
VD->getStorageClass() == SC_Register2.70k
&&
VD->hasAttr<AsmLabelAttr>()4
&&
3002
2.70k
         
!VD->isLocalVarDecl()4
)) {
3003
8
      Diag(ILoc, diag::err_omp_var_thread_local)
3004
8
          << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 
04
:
14
);
3005
8
      bool IsDecl =
3006
8
          VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3007
8
      Diag(VD->getLocation(),
3008
8
           IsDecl ? 
diag::note_previous_decl0
: diag::note_defined_here)
3009
8
          << VD;
3010
8
      continue;
3011
8
    }
3012
3013
    // Check if initial value of threadprivate variable reference variable with
3014
    // local storage (it is not supported by runtime).
3015
2.69k
    if (const Expr *Init = VD->getAnyInitializer()) {
3016
1.20k
      LocalVarRefChecker Checker(*this);
3017
1.20k
      if (Checker.Visit(Init))
3018
4
        continue;
3019
1.20k
    }
3020
3021
2.69k
    Vars.push_back(RefExpr);
3022
2.69k
    DSAStack->addDSA(VD, DE, OMPC_threadprivate);
3023
2.69k
    VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
3024
2.69k
        Context, SourceRange(Loc, Loc)));
3025
2.69k
    if (ASTMutationListener *ML = Context.getASTMutationListener())
3026
212
      ML->DeclarationMarkedOpenMPThreadPrivate(VD);
3027
2.69k
  }
3028
2.75k
  OMPThreadPrivateDecl *D = nullptr;
3029
2.75k
  if (!Vars.empty()) {
3030
2.68k
    D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
3031
2.68k
                                     Vars);
3032
2.68k
    D->setAccess(AS_public);
3033
2.68k
  }
3034
2.75k
  return D;
3035
2.75k
}
3036
3037
static OMPAllocateDeclAttr::AllocatorTypeTy
3038
2.06k
getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) {
3039
2.06k
  if (!Allocator)
3040
892
    return OMPAllocateDeclAttr::OMPNullMemAlloc;
3041
1.17k
  if (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
3042
1.17k
      Allocator->isInstantiationDependent() ||
3043
1.17k
      Allocator->containsUnexpandedParameterPack())
3044
0
    return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
3045
1.17k
  auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
3046
1.17k
  const Expr *AE = Allocator->IgnoreParenImpCasts();
3047
8.75k
  for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 
++I7.58k
) {
3048
8.73k
    auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
3049
8.73k
    const Expr *DefAllocator = Stack->getAllocator(AllocatorKind);
3050
8.73k
    llvm::FoldingSetNodeID AEId, DAEId;
3051
8.73k
    AE->Profile(AEId, S.getASTContext(), /*Canonical=*/true);
3052
8.73k
    DefAllocator->Profile(DAEId, S.getASTContext(), /*Canonical=*/true);
3053
8.73k
    if (AEId == DAEId) {
3054
1.15k
      AllocatorKindRes = AllocatorKind;
3055
1.15k
      break;
3056
1.15k
    }
3057
8.73k
  }
3058
1.17k
  return AllocatorKindRes;
3059
1.17k
}
3060
3061
static bool checkPreviousOMPAllocateAttribute(
3062
    Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD,
3063
1.92k
    OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) {
3064
1.92k
  if (!VD->hasAttr<OMPAllocateDeclAttr>())
3065
1.75k
    return false;
3066
170
  const auto *A = VD->getAttr<OMPAllocateDeclAttr>();
3067
170
  Expr *PrevAllocator = A->getAllocator();
3068
170
  OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind =
3069
170
      getAllocatorKind(S, Stack, PrevAllocator);
3070
170
  bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind;
3071
170
  if (AllocatorsMatch &&
3072
170
      
AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc164
&&
3073
170
      
Allocator0
&&
PrevAllocator0
) {
3074
0
    const Expr *AE = Allocator->IgnoreParenImpCasts();
3075
0
    const Expr *PAE = PrevAllocator->IgnoreParenImpCasts();
3076
0
    llvm::FoldingSetNodeID AEId, PAEId;
3077
0
    AE->Profile(AEId, S.Context, /*Canonical=*/true);
3078
0
    PAE->Profile(PAEId, S.Context, /*Canonical=*/true);
3079
0
    AllocatorsMatch = AEId == PAEId;
3080
0
  }
3081
170
  if (!AllocatorsMatch) {
3082
6
    SmallString<256> AllocatorBuffer;
3083
6
    llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer);
3084
6
    if (Allocator)
3085
4
      Allocator->printPretty(AllocatorStream, nullptr, S.getPrintingPolicy());
3086
6
    SmallString<256> PrevAllocatorBuffer;
3087
6
    llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer);
3088
6
    if (PrevAllocator)
3089
4
      PrevAllocator->printPretty(PrevAllocatorStream, nullptr,
3090
4
                                 S.getPrintingPolicy());
3091
3092
6
    SourceLocation AllocatorLoc =
3093
6
        Allocator ? 
Allocator->getExprLoc()4
:
RefExpr->getExprLoc()2
;
3094
6
    SourceRange AllocatorRange =
3095
6
        Allocator ? 
Allocator->getSourceRange()4
:
RefExpr->getSourceRange()2
;
3096
6
    SourceLocation PrevAllocatorLoc =
3097
6
        PrevAllocator ? 
PrevAllocator->getExprLoc()4
:
A->getLocation()2
;
3098
6
    SourceRange PrevAllocatorRange =
3099
6
        PrevAllocator ? 
PrevAllocator->getSourceRange()4
:
A->getRange()2
;
3100
6
    S.Diag(AllocatorLoc, diag::warn_omp_used_different_allocator)
3101
6
        << (Allocator ? 
14
:
02
) << AllocatorStream.str()
3102
6
        << (PrevAllocator ? 
14
:
02
) << PrevAllocatorStream.str()
3103
6
        << AllocatorRange;
3104
6
    S.Diag(PrevAllocatorLoc, diag::note_omp_previous_allocator)
3105
6
        << PrevAllocatorRange;
3106
6
    return true;
3107
6
  }
3108
164
  return false;
3109
170
}
3110
3111
static void
3112
applyOMPAllocateAttribute(Sema &S, VarDecl *VD,
3113
                          OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
3114
1.91k
                          Expr *Allocator, Expr *Alignment, SourceRange SR) {
3115
1.91k
  if (VD->hasAttr<OMPAllocateDeclAttr>())
3116
164
    return;
3117
1.74k
  if (Alignment &&
3118
1.74k
      
(71
Alignment->isTypeDependent()71
||
Alignment->isValueDependent()71
||
3119
71
       
Alignment->isInstantiationDependent()64
||
3120
71
       
Alignment->containsUnexpandedParameterPack()64
))
3121
    // Apply later when we have a usable value.
3122
7
    return;
3123
1.73k
  if (Allocator &&
3124
1.73k
      
(1.08k
Allocator->isTypeDependent()1.08k
||
Allocator->isValueDependent()1.08k
||
3125
1.08k
       Allocator->isInstantiationDependent() ||
3126
1.08k
       Allocator->containsUnexpandedParameterPack()))
3127
0
    return;
3128
1.73k
  auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind,
3129
1.73k
                                                Allocator, Alignment, SR);
3130
1.73k
  VD->addAttr(A);
3131
1.73k
  if (ASTMutationListener *ML = S.Context.getASTMutationListener())
3132
250
    ML->DeclarationMarkedOpenMPAllocate(VD, A);
3133
1.73k
}
3134
3135
Sema::DeclGroupPtrTy
3136
Sema::ActOnOpenMPAllocateDirective(SourceLocation Loc, ArrayRef<Expr *> VarList,
3137
                                   ArrayRef<OMPClause *> Clauses,
3138
775
                                   DeclContext *Owner) {
3139
775
  assert(Clauses.size() <= 2 && "Expected at most two clauses.");
3140
0
  Expr *Alignment = nullptr;
3141
775
  Expr *Allocator = nullptr;
3142
775
  if (Clauses.empty()) {
3143
    // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions.
3144
    // allocate directives that appear in a target region must specify an
3145
    // allocator clause unless a requires directive with the dynamic_allocators
3146
    // clause is present in the same compilation unit.
3147
319
    if (LangOpts.OpenMPIsDevice &&
3148
319
        
!4
DSAStack4
->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
3149
2
      targetDiag(Loc, diag::err_expected_allocator_clause);
3150
456
  } else {
3151
456
    for (const OMPClause *C : Clauses)
3152
498
      if (const auto *AC = dyn_cast<OMPAllocatorClause>(C))
3153
438
        Allocator = AC->getAllocator();
3154
60
      else if (const auto *AC = dyn_cast<OMPAlignClause>(C))
3155
60
        Alignment = AC->getAlignment();
3156
0
      else
3157
0
        llvm_unreachable("Unexpected clause on allocate directive");
3158
456
  }
3159
775
  OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
3160
775
      getAllocatorKind(*this, DSAStack, Allocator);
3161
775
  SmallVector<Expr *, 8> Vars;
3162
812
  for (Expr *RefExpr : VarList) {
3163
812
    auto *DE = cast<DeclRefExpr>(RefExpr);
3164
812
    auto *VD = cast<VarDecl>(DE->getDecl());
3165
3166
    // Check if this is a TLS variable or global register.
3167
812
    if (VD->getTLSKind() != VarDecl::TLS_None ||
3168
812
        
VD->hasAttr<OMPThreadPrivateDeclAttr>()808
||
3169
812
        
(808
VD->getStorageClass() == SC_Register808
&&
VD->hasAttr<AsmLabelAttr>()4
&&
3170
808
         
!VD->isLocalVarDecl()4
))
3171
8
      continue;
3172
3173
    // If the used several times in the allocate directive, the same allocator
3174
    // must be used.
3175
804
    if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD,
3176
804
                                          AllocatorKind, Allocator))
3177
6
      continue;
3178
3179
    // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++
3180
    // If a list item has a static storage type, the allocator expression in the
3181
    // allocator clause must be a constant expression that evaluates to one of
3182
    // the predefined memory allocator values.
3183
798
    if (Allocator && 
VD->hasGlobalStorage()477
) {
3184
251
      if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) {
3185
4
        Diag(Allocator->getExprLoc(),
3186
4
             diag::err_omp_expected_predefined_allocator)
3187
4
            << Allocator->getSourceRange();
3188
4
        bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3189
4
                      VarDecl::DeclarationOnly;
3190
4
        Diag(VD->getLocation(),
3191
4
             IsDecl ? 
diag::note_previous_decl0
: diag::note_defined_here)
3192
4
            << VD;
3193
4
        continue;
3194
4
      }
3195
251
    }
3196
3197
794
    Vars.push_back(RefExpr);
3198
794
    applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator, Alignment,
3199
794
                              DE->getSourceRange());
3200
794
  }
3201
775
  if (Vars.empty())
3202
58
    return nullptr;
3203
717
  if (!Owner)
3204
635
    Owner = getCurLexicalContext();
3205
717
  auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses);
3206
717
  D->setAccess(AS_public);
3207
717
  Owner->addDecl(D);
3208
717
  return DeclGroupPtrTy::make(DeclGroupRef(D));
3209
775
}
3210
3211
Sema::DeclGroupPtrTy
3212
Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc,
3213
171
                                   ArrayRef<OMPClause *> ClauseList) {
3214
171
  OMPRequiresDecl *D = nullptr;
3215
171
  if (!CurContext->isFileContext()) {
3216
1
    Diag(Loc, diag::err_omp_invalid_scope) << "requires";
3217
170
  } else {
3218
170
    D = CheckOMPRequiresDecl(Loc, ClauseList);
3219
170
    if (D) {
3220
155
      CurContext->addDecl(D);
3221
155
      DSAStack->addRequiresDecl(D);
3222
155
    }
3223
170
  }
3224
171
  return DeclGroupPtrTy::make(DeclGroupRef(D));
3225
171
}
3226
3227
void Sema::ActOnOpenMPAssumesDirective(SourceLocation Loc,
3228
                                       OpenMPDirectiveKind DKind,
3229
                                       ArrayRef<std::string> Assumptions,
3230
205
                                       bool SkippedClauses) {
3231
205
  if (!SkippedClauses && 
Assumptions.empty()115
)
3232
8
    Diag(Loc, diag::err_omp_no_clause_for_directive)
3233
8
        << llvm::omp::getAllAssumeClauseOptions()
3234
8
        << llvm::omp::getOpenMPDirectiveName(DKind);
3235
3236
205
  auto *AA = AssumptionAttr::Create(Context, llvm::join(Assumptions, ","), Loc);
3237
205
  if (DKind == llvm::omp::Directive::OMPD_begin_assumes) {
3238
122
    OMPAssumeScoped.push_back(AA);
3239
122
    return;
3240
122
  }
3241
3242
  // Global assumes without assumption clauses are ignored.
3243
83
  if (Assumptions.empty())
3244
32
    return;
3245
3246
51
  assert(DKind == llvm::omp::Directive::OMPD_assumes &&
3247
51
         "Unexpected omp assumption directive!");
3248
0
  OMPAssumeGlobal.push_back(AA);
3249
3250
  // The OMPAssumeGlobal scope above will take care of new declarations but
3251
  // we also want to apply the assumption to existing ones, e.g., to
3252
  // declarations in included headers. To this end, we traverse all existing
3253
  // declaration contexts and annotate function declarations here.
3254
51
  SmallVector<DeclContext *, 8> DeclContexts;
3255
51
  auto *Ctx = CurContext;
3256
55
  while (Ctx->getLexicalParent())
3257
4
    Ctx = Ctx->getLexicalParent();
3258
51
  DeclContexts.push_back(Ctx);
3259
5.75k
  while (!DeclContexts.empty()) {
3260
5.70k
    DeclContext *DC = DeclContexts.pop_back_val();
3261
9.31k
    for (auto *SubDC : DC->decls()) {
3262
9.31k
      if (SubDC->isInvalidDecl())
3263
0
        continue;
3264
9.31k
      if (auto *CTD = dyn_cast<ClassTemplateDecl>(SubDC)) {
3265
4
        DeclContexts.push_back(CTD->getTemplatedDecl());
3266
4
        for (auto *S : CTD->specializations())
3267
4
          DeclContexts.push_back(S);
3268
4
        continue;
3269
4
      }
3270
9.31k
      if (auto *DC = dyn_cast<DeclContext>(SubDC))
3271
5.64k
        DeclContexts.push_back(DC);
3272
9.31k
      if (auto *F = dyn_cast<FunctionDecl>(SubDC)) {
3273
5.31k
        F->addAttr(AA);
3274
5.31k
        continue;
3275
5.31k
      }
3276
9.31k
    }
3277
5.70k
  }
3278
51
}
3279
3280
118
void Sema::ActOnOpenMPEndAssumesDirective() {
3281
118
  assert(isInOpenMPAssumeScope() && "Not in OpenMP assumes scope!");
3282
0
  OMPAssumeScoped.pop_back();
3283
118
}
3284
3285
OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc,
3286
170
                                            ArrayRef<OMPClause *> ClauseList) {
3287
  /// For target specific clauses, the requires directive cannot be
3288
  /// specified after the handling of any of the target regions in the
3289
  /// current compilation unit.
3290
170
  ArrayRef<SourceLocation> TargetLocations =
3291
170
      DSAStack->getEncounteredTargetLocs();
3292
170
  SourceLocation AtomicLoc = DSAStack->getAtomicDirectiveLoc();
3293
170
  if (!TargetLocations.empty() || 
!AtomicLoc.isInvalid()165
) {
3294
8
    for (const OMPClause *CNew : ClauseList) {
3295
      // Check if any of the requires clauses affect target regions.
3296
8
      if (isa<OMPUnifiedSharedMemoryClause>(CNew) ||
3297
8
          
isa<OMPUnifiedAddressClause>(CNew)7
||
3298
8
          
isa<OMPReverseOffloadClause>(CNew)6
||
3299
8
          
isa<OMPDynamicAllocatorsClause>(CNew)5
) {
3300
4
        Diag(Loc, diag::err_omp_directive_before_requires)
3301
4
            << "target" << getOpenMPClauseName(CNew->getClauseKind());
3302
4
        for (SourceLocation TargetLoc : TargetLocations) {
3303
4
          Diag(TargetLoc, diag::note_omp_requires_encountered_directive)
3304
4
              << "target";
3305
4
        }
3306
4
      } else if (!AtomicLoc.isInvalid() &&
3307
4
                 
isa<OMPAtomicDefaultMemOrderClause>(CNew)3
) {
3308
3
        Diag(Loc, diag::err_omp_directive_before_requires)
3309
3
            << "atomic" << getOpenMPClauseName(CNew->getClauseKind());
3310
3
        Diag(AtomicLoc, diag::note_omp_requires_encountered_directive)
3311
3
            << "atomic";
3312
3
      }
3313
8
    }
3314
8
  }
3315
3316
170
  if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
3317
155
    return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc,
3318
155
                                   ClauseList);
3319
15
  return nullptr;
3320
170
}
3321
3322
static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
3323
                              const ValueDecl *D,
3324
                              const DSAStackTy::DSAVarData &DVar,
3325
5.38k
                              bool IsLoopIterVar) {
3326
5.38k
  if (DVar.RefExpr) {
3327
4.22k
    SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
3328
4.22k
        << getOpenMPClauseName(DVar.CKind);
3329
4.22k
    return;
3330
4.22k
  }
3331
1.16k
  enum {
3332
1.16k
    PDSA_StaticMemberShared,
3333
1.16k
    PDSA_StaticLocalVarShared,
3334
1.16k
    PDSA_LoopIterVarPrivate,
3335
1.16k
    PDSA_LoopIterVarLinear,
3336
1.16k
    PDSA_LoopIterVarLastprivate,
3337
1.16k
    PDSA_ConstVarShared,
3338
1.16k
    PDSA_GlobalVarShared,
3339
1.16k
    PDSA_TaskVarFirstprivate,
3340
1.16k
    PDSA_LocalVarPrivate,
3341
1.16k
    PDSA_Implicit
3342
1.16k
  } Reason = PDSA_Implicit;
3343
1.16k
  bool ReportHint = false;
3344
1.16k
  auto ReportLoc = D->getLocation();
3345
1.16k
  auto *VD = dyn_cast<VarDecl>(D);
3346
1.16k
  if (IsLoopIterVar) {
3347
0
    if (DVar.CKind == OMPC_private)
3348
0
      Reason = PDSA_LoopIterVarPrivate;
3349
0
    else if (DVar.CKind == OMPC_lastprivate)
3350
0
      Reason = PDSA_LoopIterVarLastprivate;
3351
0
    else
3352
0
      Reason = PDSA_LoopIterVarLinear;
3353
1.16k
  } else if (isOpenMPTaskingDirective(DVar.DKind) &&
3354
1.16k
             
DVar.CKind == OMPC_firstprivate12
) {
3355
12
    Reason = PDSA_TaskVarFirstprivate;
3356
12
    ReportLoc = DVar.ImplicitDSALoc;
3357
1.14k
  } else if (VD && VD->isStaticLocal())
3358
0
    Reason = PDSA_StaticLocalVarShared;
3359
1.14k
  else if (VD && VD->isStaticDataMember())
3360
1.01k
    Reason = PDSA_StaticMemberShared;
3361
134
  else if (VD && VD->isFileVarDecl())
3362
0
    Reason = PDSA_GlobalVarShared;
3363
134
  else if (D->getType().isConstant(SemaRef.getASTContext()))
3364
0
    Reason = PDSA_ConstVarShared;
3365
134
  else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
3366
54
    ReportHint = true;
3367
54
    Reason = PDSA_LocalVarPrivate;
3368
54
  }
3369
1.16k
  if (Reason != PDSA_Implicit) {
3370
1.08k
    SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
3371
1.08k
        << Reason << ReportHint
3372
1.08k
        << getOpenMPDirectiveName(Stack->getCurrentDirective());
3373
1.08k
  } else 
if (80
DVar.ImplicitDSALoc.isValid()80
) {
3374
12
    SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
3375
12
        << getOpenMPClauseName(DVar.CKind);
3376
12
  }
3377
1.16k
}
3378
3379
static OpenMPMapClauseKind
3380
getMapClauseKindFromModifier(OpenMPDefaultmapClauseModifier M,
3381
16.9k
                             bool IsAggregateOrDeclareTarget) {
3382
16.9k
  OpenMPMapClauseKind Kind = OMPC_MAP_unknown;
3383
16.9k
  switch (M) {
3384
40
  case OMPC_DEFAULTMAP_MODIFIER_alloc:
3385
40
    Kind = OMPC_MAP_alloc;
3386
40
    break;
3387
32
  case OMPC_DEFAULTMAP_MODIFIER_to:
3388
32
    Kind = OMPC_MAP_to;
3389
32
    break;
3390
32
  case OMPC_DEFAULTMAP_MODIFIER_from:
3391
32
    Kind = OMPC_MAP_from;
3392
32
    break;
3393
344
  case OMPC_DEFAULTMAP_MODIFIER_tofrom:
3394
344
    Kind = OMPC_MAP_tofrom;
3395
344
    break;
3396
32
  case OMPC_DEFAULTMAP_MODIFIER_present:
3397
    // OpenMP 5.1 [2.21.7.3] defaultmap clause, Description]
3398
    // If implicit-behavior is present, each variable referenced in the
3399
    // construct in the category specified by variable-category is treated as if
3400
    // it had been listed in a map clause with the map-type of alloc and
3401
    // map-type-modifier of present.
3402
32
    Kind = OMPC_MAP_alloc;
3403
32
    break;
3404
0
  case OMPC_DEFAULTMAP_MODIFIER_firstprivate:
3405
0
  case OMPC_DEFAULTMAP_MODIFIER_last:
3406
0
    llvm_unreachable("Unexpected defaultmap implicit behavior");
3407
24
  case OMPC_DEFAULTMAP_MODIFIER_none:
3408
32
  case OMPC_DEFAULTMAP_MODIFIER_default:
3409
16.4k
  case OMPC_DEFAULTMAP_MODIFIER_unknown:
3410
    // IsAggregateOrDeclareTarget could be true if:
3411
    // 1. the implicit behavior for aggregate is tofrom
3412
    // 2. it's a declare target link
3413
16.4k
    if (IsAggregateOrDeclareTarget) {
3414
16.4k
      Kind = OMPC_MAP_tofrom;
3415
16.4k
      break;
3416
16.4k
    }
3417
16.9k
    
llvm_unreachable0
("Unexpected defaultmap implicit behavior");
3418
16.9k
  }
3419
16.9k
  assert(Kind != OMPC_MAP_unknown && "Expect map kind to be known");
3420
0
  return Kind;
3421
16.9k
}
3422
3423
namespace {
3424
class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
3425
  DSAStackTy *Stack;
3426
  Sema &SemaRef;
3427
  bool ErrorFound = false;
3428
  bool TryCaptureCXXThisMembers = false;
3429
  CapturedStmt *CS = nullptr;
3430
  const static unsigned DefaultmapKindNum = OMPC_DEFAULTMAP_pointer + 1;
3431
  llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
3432
  llvm::SmallVector<Expr *, 4> ImplicitMap[DefaultmapKindNum][OMPC_MAP_delete];
3433
  llvm::SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers>
3434
      ImplicitMapModifier[DefaultmapKindNum];
3435
  Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
3436
  llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
3437
3438
66.1k
  void VisitSubCaptures(OMPExecutableDirective *S) {
3439
    // Check implicitly captured variables.
3440
66.1k
    if (!S->hasAssociatedStmt() || 
!S->getAssociatedStmt()64.3k
)
3441
1.80k
      return;
3442
64.3k
    if (S->getDirectiveKind() == OMPD_atomic ||
3443
64.3k
        
S->getDirectiveKind() == OMPD_critical64.0k
||
3444
64.3k
        
S->getDirectiveKind() == OMPD_section63.7k
||
3445
64.3k
        
S->getDirectiveKind() == OMPD_master63.1k
||
3446
64.3k
        
S->getDirectiveKind() == OMPD_masked62.9k
||
3447
64.3k
        
isOpenMPLoopTransformationDirective(S->getDirectiveKind())62.9k
) {
3448
1.44k
      Visit(S->getAssociatedStmt());
3449
1.44k
      return;
3450
1.44k
    }
3451
62.9k
    visitSubCaptures(S->getInnermostCapturedStmt());
3452
    // Try to capture inner this->member references to generate correct mappings
3453
    // and diagnostics.
3454
62.9k
    if (TryCaptureCXXThisMembers ||
3455
62.9k
        
(62.8k
isOpenMPTargetExecutionDirective(Stack->getCurrentDirective())62.8k
&&
3456
62.8k
         llvm::any_of(S->getInnermostCapturedStmt()->captures(),
3457
27.6k
                      [](const CapturedStmt::Capture &C) {
3458
26.9k
                        return C.capturesThis();
3459
26.9k
                      }))) {
3460
626
      bool SavedTryCaptureCXXThisMembers = TryCaptureCXXThisMembers;
3461
626
      TryCaptureCXXThisMembers = true;
3462
626
      Visit(S->getInnermostCapturedStmt()->getCapturedStmt());
3463
626
      TryCaptureCXXThisMembers = SavedTryCaptureCXXThisMembers;
3464
626
    }
3465
    // In tasks firstprivates are not captured anymore, need to analyze them
3466
    // explicitly.
3467
62.9k
    if (isOpenMPTaskingDirective(S->getDirectiveKind()) &&
3468
62.9k
        
!isOpenMPTaskLoopDirective(S->getDirectiveKind())7.07k
) {
3469
1.31k
      for (OMPClause *C : S->clauses())
3470
1.19k
        if (auto *FC = dyn_cast<OMPFirstprivateClause>(C)) {
3471
664
          for (Expr *Ref : FC->varlists())
3472
1.07k
            Visit(Ref);
3473
664
        }
3474
1.31k
    }
3475
62.9k
  }
3476
3477
public:
3478
705k
  void VisitDeclRefExpr(DeclRefExpr *E) {
3479
705k
    if (TryCaptureCXXThisMembers || 
E->isTypeDependent()704k
||
3480
705k
        
E->isValueDependent()704k
||
E->containsUnexpandedParameterPack()704k
||
3481
705k
        
E->isInstantiationDependent()704k
)
3482
1.84k
      return;
3483
704k
    if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
3484
      // Check the datasharing rules for the expressions in the clauses.
3485
703k
      if (!CS || 
(703k
isa<OMPCapturedExprDecl>(VD)703k
&&
!CS->capturesVariable(VD)8.52k
&&
3486
703k
                  
!Stack->getTopDSA(VD, /*FromParent=*/false).RefExpr2.94k
)) {
3487
1.82k
        if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
3488
1.72k
          if (!CED->hasAttr<OMPCaptureNoInitAttr>()) {
3489
1.66k
            Visit(CED->getInit());
3490
1.66k
            return;
3491
1.66k
          }
3492
702k
      } else if (VD->isImplicit() || 
isa<OMPCapturedExprDecl>(VD)693k
)
3493
        // Do not analyze internal variables and do not enclose them into
3494
        // implicit clauses.
3495
8.17k
        return;
3496
694k
      VD = VD->getCanonicalDecl();
3497
      // Skip internally declared variables.
3498
694k
      if (VD->hasLocalStorage() && 
CS659k
&&
!CS->capturesVariable(VD)659k
&&
3499
694k
          
!Stack->isImplicitTaskFirstprivate(VD)270k
)
3500
263k
        return;
3501
      // Skip allocators in uses_allocators clauses.
3502
431k
      if (Stack->isUsesAllocatorsDecl(VD).hasValue())
3503
138
        return;
3504
3505
430k
      DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
3506
      // Check if the variable has explicit DSA set and stop analysis if it so.
3507
430k
      if (DVar.RefExpr || 
!ImplicitDeclarations.insert(VD).second313k
)
3508
279k
        return;
3509
3510
      // Skip internally declared static variables.
3511
151k
      llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
3512
151k
          OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
3513
151k
      if (VD->hasGlobalStorage() && 
CS9.90k
&&
!CS->capturesVariable(VD)9.90k
&&
3514
151k
          
(2.73k
Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>()2.73k
||
3515
2.73k
           
!Res2.71k
||
*Res != OMPDeclareTargetDeclAttr::MT_Link102
) &&
3516
151k
          
!Stack->isImplicitTaskFirstprivate(VD)2.67k
)
3517
2.65k
        return;
3518
3519
148k
      SourceLocation ELoc = E->getExprLoc();
3520
148k
      OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
3521
      // The default(none) clause requires that each variable that is referenced
3522
      // in the construct, and does not have a predetermined data-sharing
3523
      // attribute, must have its data-sharing attribute explicitly determined
3524
      // by being listed in a data-sharing attribute clause.
3525
148k
      if (DVar.CKind == OMPC_unknown &&
3526
148k
          
(148k
Stack->getDefaultDSA() == DSA_none148k
||
3527
148k
           
Stack->getDefaultDSA() == DSA_firstprivate147k
) &&
3528
148k
          
isImplicitOrExplicitTaskingRegion(DKind)466
&&
3529
148k
          
VarsWithInheritedDSA.count(VD) == 0466
) {
3530
466
        bool InheritedDSA = Stack->getDefaultDSA() == DSA_none;
3531
466
        if (!InheritedDSA && 
Stack->getDefaultDSA() == DSA_firstprivate66
) {
3532
66
          DSAStackTy::DSAVarData DVar =
3533
66
              Stack->getImplicitDSA(VD, /*FromParent=*/false);
3534
66
          InheritedDSA = DVar.CKind == OMPC_unknown;
3535
66
        }
3536
466
        if (InheritedDSA)
3537
448
          VarsWithInheritedDSA[VD] = E;
3538
466
        return;
3539
466
      }
3540
3541
      // OpenMP 5.0 [2.19.7.2, defaultmap clause, Description]
3542
      // If implicit-behavior is none, each variable referenced in the
3543
      // construct that does not have a predetermined data-sharing attribute
3544
      // and does not appear in a to or link clause on a declare target
3545
      // directive must be listed in a data-mapping attribute clause, a
3546
      // data-haring attribute clause (including a data-sharing attribute
3547
      // clause on a combined construct where target. is one of the
3548
      // constituent constructs), or an is_device_ptr clause.
3549
148k
      OpenMPDefaultmapClauseKind ClauseKind =
3550
148k
          getVariableCategoryFromDecl(SemaRef.getLangOpts(), VD);
3551
148k
      if (SemaRef.getLangOpts().OpenMP >= 50) {
3552
96.0k
        bool IsModifierNone = Stack->getDefaultmapModifier(ClauseKind) ==
3553
96.0k
                              OMPC_DEFAULTMAP_MODIFIER_none;
3554
96.0k
        if (DVar.CKind == OMPC_unknown && 
IsModifierNone95.7k
&&
3555
96.0k
            
VarsWithInheritedDSA.count(VD) == 0268
&&
!Res268
) {
3556
          // Only check for data-mapping attribute and is_device_ptr here
3557
          // since we have already make sure that the declaration does not
3558
          // have a data-sharing attribute above
3559
244
          if (!Stack->checkMappableExprComponentListsForDecl(
3560
244
                  VD, /*CurrentRegionOnly=*/true,
3561
244
                  [VD](OMPClauseMappableExprCommon::MappableExprComponentListRef
3562
244
                           MapExprComponents,
3563
244
                       OpenMPClauseKind) {
3564
166
                    auto MI = MapExprComponents.rbegin();
3565
166
                    auto ME = MapExprComponents.rend();
3566
166
                    return MI != ME && MI->getAssociatedDeclaration() == VD;
3567
166
                  })) {
3568
78
            VarsWithInheritedDSA[VD] = E;
3569
78
            return;
3570
78
          }
3571
244
        }
3572
96.0k
      }
3573
148k
      if (SemaRef.getLangOpts().OpenMP > 50) {
3574
4.98k
        bool IsModifierPresent = Stack->getDefaultmapModifier(ClauseKind) ==
3575
4.98k
                                 OMPC_DEFAULTMAP_MODIFIER_present;
3576
4.98k
        if (IsModifierPresent) {
3577
32
          if (llvm::find(ImplicitMapModifier[ClauseKind],
3578
32
                         OMPC_MAP_MODIFIER_present) ==
3579
32
              std::end(ImplicitMapModifier[ClauseKind])) {
3580
32
            ImplicitMapModifier[ClauseKind].push_back(
3581
32
                OMPC_MAP_MODIFIER_present);
3582
32
          }
3583
32
        }
3584
4.98k
      }
3585
3586
148k
      if (isOpenMPTargetExecutionDirective(DKind) &&
3587
148k
          
!Stack->isLoopControlVariable(VD).first67.9k
) {
3588
67.9k
        if (!Stack->checkMappableExprComponentListsForDecl(
3589
67.9k
                VD, /*CurrentRegionOnly=*/true,
3590
67.9k
                [this](OMPClauseMappableExprCommon::MappableExprComponentListRef
3591
67.9k
                           StackComponents,
3592
67.9k
                       OpenMPClauseKind) {
3593
2.87k
                  if (SemaRef.LangOpts.OpenMP >= 50)
3594
2.15k
                    return !StackComponents.empty();
3595
                  // Variable is used if it has been marked as an array, array
3596
                  // section, array shaping or the variable iself.
3597
716
                  return StackComponents.size() == 1 ||
3598
716
                         std::all_of(
3599
328
                             std::next(StackComponents.rbegin()),
3600
328
                             StackComponents.rend(),
3601
328
                             [](const OMPClauseMappableExprCommon::
3602
440
                                    MappableComponent &MC) {
3603
440
                               return MC.getAssociatedDeclaration() ==
3604
440
                                          nullptr &&
3605
440
                                      
(308
isa<OMPArraySectionExpr>(
3606
308
                                           MC.getAssociatedExpression()) ||
3607
308
                                       isa<OMPArrayShapingExpr>(
3608
92
                                           MC.getAssociatedExpression()) ||
3609
308
                                       isa<ArraySubscriptExpr>(
3610
92
                                           MC.getAssociatedExpression()));
3611
440
                             });
3612
65.2k
                })) {
3613
65.2k
          bool IsFirstprivate = false;
3614
          // By default lambdas are captured as firstprivates.
3615
65.2k
          if (const auto *RD =
3616
65.2k
                  VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
3617
5.51k
            IsFirstprivate = RD->isLambda();
3618
65.2k
          IsFirstprivate =
3619
65.2k
              IsFirstprivate || 
(65.2k
Stack->mustBeFirstprivate(ClauseKind)65.2k
&&
!Res49.7k
);
3620
65.2k
          if (IsFirstprivate) {
3621
49.7k
            ImplicitFirstprivate.emplace_back(E);
3622
49.7k
          } else {
3623
15.5k
            OpenMPDefaultmapClauseModifier M =
3624
15.5k
                Stack->getDefaultmapModifier(ClauseKind);
3625
15.5k
            OpenMPMapClauseKind Kind = getMapClauseKindFromModifier(
3626
15.5k
                M, ClauseKind == OMPC_DEFAULTMAP_aggregate || 
Res440
);
3627
15.5k
            ImplicitMap[ClauseKind][Kind].emplace_back(E);
3628
15.5k
          }
3629
65.2k
          return;
3630
65.2k
        }
3631
67.9k
      }
3632
3633
      // OpenMP [2.9.3.6, Restrictions, p.2]
3634
      //  A list item that appears in a reduction clause of the innermost
3635
      //  enclosing worksharing or parallel construct may not be accessed in an
3636
      //  explicit task.
3637
82.8k
      DVar = Stack->hasInnermostDSA(
3638
82.8k
          VD,
3639
82.8k
          [](OpenMPClauseKind C, bool AppliedToPointee) {
3640
18.5k
            return C == OMPC_reduction && 
!AppliedToPointee86
;
3641
18.5k
          },
3642
82.8k
          [](OpenMPDirectiveKind K) {
3643
48.0k
            return isOpenMPParallelDirective(K) ||
3644
48.0k
                   
isOpenMPWorksharingDirective(K)37.5k
||
isOpenMPTeamsDirective(K)37.1k
;
3645
48.0k
          },
3646
82.8k
          /*FromParent=*/true);
3647
82.8k
      if (isOpenMPTaskingDirective(DKind) && 
DVar.CKind == OMPC_reduction10.6k
) {
3648
84
        ErrorFound = true;
3649
84
        SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
3650
84
        reportOriginalDsa(SemaRef, Stack, VD, DVar);
3651
84
        return;
3652
84
      }
3653
3654
      // Define implicit data-sharing attributes for task.
3655
82.7k
      DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
3656
82.7k
      if (((isOpenMPTaskingDirective(DKind) && 
DVar.CKind != OMPC_shared10.6k
) ||
3657
82.7k
           
(78.6k
Stack->getDefaultDSA() == DSA_firstprivate78.6k
&&
3658
78.6k
            
DVar.CKind == OMPC_firstprivate0
&&
!DVar.RefExpr0
)) &&
3659
82.7k
          
!Stack->isLoopControlVariable(VD).first4.10k
) {
3660
4.10k
        ImplicitFirstprivate.push_back(E);
3661
4.10k
        return;
3662
4.10k
      }
3663
3664
      // Store implicitly used globals with declare target link for parent
3665
      // target.
3666
78.6k
      if (!isOpenMPTargetExecutionDirective(DKind) && 
Res75.9k
&&
3667
78.6k
          
*Res == OMPDeclareTargetDeclAttr::MT_Link8
) {
3668
8
        Stack->addToParentTargetRegionLinkGlobals(E);
3669
8
        return;
3670
8
      }
3671
78.6k
    }
3672
704k
  }
3673
7.79k
  void VisitMemberExpr(MemberExpr *E) {
3674
7.79k
    if (E->isTypeDependent() || E->isValueDependent() ||
3675
7.79k
        E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
3676
0
      return;
3677
7.79k
    auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
3678
7.79k
    OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
3679
7.79k
    if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParenCasts())) {
3680
3.29k
      if (!FD)
3681
8
        return;
3682
3.28k
      DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
3683
      // Check if the variable has explicit DSA set and stop analysis if it
3684
      // so.
3685
3.28k
      if (DVar.RefExpr || 
!ImplicitDeclarations.insert(FD).second2.95k
)
3686
1.02k
        return;
3687
3688
2.26k
      if (isOpenMPTargetExecutionDirective(DKind) &&
3689
2.26k
          
!Stack->isLoopControlVariable(FD).first1.60k
&&
3690
2.26k
          !Stack->checkMappableExprComponentListsForDecl(
3691
1.60k
              FD, /*CurrentRegionOnly=*/true,
3692
1.60k
              [](OMPClauseMappableExprCommon::MappableExprComponentListRef
3693
1.60k
                     StackComponents,
3694
1.60k
                 OpenMPClauseKind) {
3695
136
                return isa<CXXThisExpr>(
3696
136
                    cast<MemberExpr>(
3697
136
                        StackComponents.back().getAssociatedExpression())
3698
136
                        ->getBase()
3699
136
                        ->IgnoreParens());
3700
1.46k
              })) {
3701
        // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
3702
        //  A bit-field cannot appear in a map clause.
3703
        //
3704
1.46k
        if (FD->isBitField())
3705
20
          return;
3706
3707
        // Check to see if the member expression is referencing a class that
3708
        // has already been explicitly mapped
3709
1.44k
        if (Stack->isClassPreviouslyMapped(TE->getType()))
3710
30
          return;
3711
3712
1.41k
        OpenMPDefaultmapClauseModifier Modifier =
3713
1.41k
            Stack->getDefaultmapModifier(OMPC_DEFAULTMAP_aggregate);
3714
1.41k
        OpenMPDefaultmapClauseKind ClauseKind =
3715
1.41k
            getVariableCategoryFromDecl(SemaRef.getLangOpts(), FD);
3716
1.41k
        OpenMPMapClauseKind Kind = getMapClauseKindFromModifier(
3717
1.41k
            Modifier, /*IsAggregateOrDeclareTarget*/ true);
3718
1.41k
        ImplicitMap[ClauseKind][Kind].emplace_back(E);
3719
1.41k
        return;
3720
1.44k
      }
3721
3722
798
      SourceLocation ELoc = E->getExprLoc();
3723
      // OpenMP [2.9.3.6, Restrictions, p.2]
3724
      //  A list item that appears in a reduction clause of the innermost
3725
      //  enclosing worksharing or parallel construct may not be accessed in
3726
      //  an  explicit task.
3727
798
      DVar = Stack->hasInnermostDSA(
3728
798
          FD,
3729
798
          [](OpenMPClauseKind C, bool AppliedToPointee) {
3730
18
            return C == OMPC_reduction && 
!AppliedToPointee0
;
3731
18
          },
3732
798
          [](OpenMPDirectiveKind K) {
3733
392
            return isOpenMPParallelDirective(K) ||
3734
392
                   
isOpenMPWorksharingDirective(K)374
||
isOpenMPTeamsDirective(K)374
;
3735
392
          },
3736
798
          /*FromParent=*/true);
3737
798
      if (isOpenMPTaskingDirective(DKind) && 
DVar.CKind == OMPC_reduction82
) {
3738
0
        ErrorFound = true;
3739
0
        SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
3740
0
        reportOriginalDsa(SemaRef, Stack, FD, DVar);
3741
0
        return;
3742
0
      }
3743
3744
      // Define implicit data-sharing attributes for task.
3745
798
      DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
3746
798
      if (isOpenMPTaskingDirective(DKind) && 
DVar.CKind != OMPC_shared82
&&
3747
798
          
!Stack->isLoopControlVariable(FD).first0
) {
3748
        // Check if there is a captured expression for the current field in the
3749
        // region. Do not mark it as firstprivate unless there is no captured
3750
        // expression.
3751
        // TODO: try to make it firstprivate.
3752
0
        if (DVar.CKind != OMPC_unknown)
3753
0
          ImplicitFirstprivate.push_back(E);
3754
0
      }
3755
798
      return;
3756
798
    }
3757
4.49k
    if (isOpenMPTargetExecutionDirective(DKind)) {
3758
2.68k
      OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
3759
2.68k
      if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
3760
2.68k
                                        Stack->getCurrentDirective(),
3761
2.68k
                                        /*NoDiagnose=*/true))
3762
40
        return;
3763
2.64k
      const auto *VD = cast<ValueDecl>(
3764
2.64k
          CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
3765
2.64k
      if (!Stack->checkMappableExprComponentListsForDecl(
3766
2.64k
              VD, /*CurrentRegionOnly=*/true,
3767
2.64k
              [&CurComponents](
3768
2.64k
                  OMPClauseMappableExprCommon::MappableExprComponentListRef
3769
2.64k
                      StackComponents,
3770
2.64k
                  OpenMPClauseKind) {
3771
864
                auto CCI = CurComponents.rbegin();
3772
864
                auto CCE = CurComponents.rend();
3773
1.56k
                for (const auto &SC : llvm::reverse(StackComponents)) {
3774
                  // Do both expressions have the same kind?
3775
1.56k
                  if (CCI->getAssociatedExpression()->getStmtClass() !=
3776
1.56k
                      SC.getAssociatedExpression()->getStmtClass())
3777
80
                    if (!((isa<OMPArraySectionExpr>(
3778
80
                               SC.getAssociatedExpression()) ||
3779
80
                           isa<OMPArrayShapingExpr>(
3780
0
                               SC.getAssociatedExpression())) &&
3781
80
                          isa<ArraySubscriptExpr>(
3782
80
                              CCI->getAssociatedExpression())))
3783
0
                      return false;
3784
3785
1.56k
                  const Decl *CCD = CCI->getAssociatedDeclaration();
3786
1.56k
                  const Decl *SCD = SC.getAssociatedDeclaration();
3787
1.56k
                  CCD = CCD ? 
CCD->getCanonicalDecl()1.48k
:
nullptr80
;
3788
1.56k
                  SCD = SCD ? 
SCD->getCanonicalDecl()1.48k
:
nullptr80
;
3789
1.56k
                  if (SCD != CCD)
3790
420
                    return false;
3791
1.14k
                  std::advance(CCI, 1);
3792
1.14k
                  if (CCI == CCE)
3793
148
                    break;
3794
1.14k
                }
3795
444
                return true;
3796
2.20k
              })) {
3797
2.20k
        Visit(E->getBase());
3798
2.20k
      }
3799
2.64k
    } else 
if (1.81k
!TryCaptureCXXThisMembers1.81k
) {
3800
1.81k
      Visit(E->getBase());
3801
1.81k
    }
3802
4.49k
  }
3803
66.1k
  void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
3804
66.1k
    for (OMPClause *C : S->clauses()) {
3805
      // Skip analysis of arguments of private clauses for task|target
3806
      // directives.
3807
32.5k
      if (isa_and_nonnull<OMPPrivateClause>(C))
3808
2.80k
        continue;
3809
      // Skip analysis of arguments of implicitly defined firstprivate clause
3810
      // for task|target directives.
3811
      // Skip analysis of arguments of implicitly defined map clause for target
3812
      // directives.
3813
29.7k
      if (C && !((isa<OMPFirstprivateClause>(C) || 
isa<OMPMapClause>(C)24.4k
) &&
3814
29.7k
                 
C->isImplicit()9.21k
&&
3815
29.7k
                 
!isOpenMPTaskingDirective(Stack->getCurrentDirective())3.20k
)) {
3816
30.8k
        for (Stmt *CC : C->children()) {
3817
30.8k
          if (CC)
3818
29.8k
            Visit(CC);
3819
30.8k
        }
3820
27.0k
      }
3821
29.7k
    }
3822
    // Check implicitly captured variables.
3823
66.1k
    VisitSubCaptures(S);
3824
66.1k
  }
3825
3826
58
  void VisitOMPLoopTransformationDirective(OMPLoopTransformationDirective *S) {
3827
    // Loop transformation directives do not introduce data sharing
3828
58
    VisitStmt(S);
3829
58
  }
3830
3831
79.7k
  void VisitCallExpr(CallExpr *S) {
3832
79.7k
    for (Stmt *C : S->arguments()) {
3833
21.0k
      if (C) {
3834
        // Check implicitly captured variables in the task-based directives to
3835
        // check if they must be firstprivatized.
3836
21.0k
        Visit(C);
3837
21.0k
      }
3838
21.0k
    }
3839
79.7k
    if (Expr *Callee = S->getCallee())
3840
79.7k
      if (auto *CE = dyn_cast<MemberExpr>(Callee->IgnoreParenImpCasts()))
3841
308
        Visit(CE->getBase());
3842
79.7k
  }
3843
1.40M
  void VisitStmt(Stmt *S) {
3844
1.86M
    for (Stmt *C : S->children()) {
3845
1.86M
      if (C) {
3846
        // Check implicitly captured variables in the task-based directives to
3847
        // check if they must be firstprivatized.
3848
1.74M
        Visit(C);
3849
1.74M
      }
3850
1.86M
    }
3851
1.40M
  }
3852
3853
246k
  void visitSubCaptures(CapturedStmt *S) {
3854
249k
    for (const CapturedStmt::Capture &Cap : S->captures()) {
3855
249k
      if (!Cap.capturesVariable() && 
!Cap.capturesVariableByCopy()81.4k
)
3856
8.60k
        continue;
3857
241k
      VarDecl *VD = Cap.getCapturedVar();
3858
      // Do not try to map the variable if it or its sub-component was mapped
3859
      // already.
3860
241k
      if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
3861
241k
          Stack->checkMappableExprComponentListsForDecl(
3862
116k
              VD, /*CurrentRegionOnly=*/true,
3863
116k
              [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
3864
116k
                 OpenMPClauseKind) 
{ return true; }3.84k
))
3865
3.84k
        continue;
3866
237k
      DeclRefExpr *DRE = buildDeclRefExpr(
3867
237k
          SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context),
3868
237k
          Cap.getLocation(), /*RefersToCapture=*/true);
3869
237k
      Visit(DRE);
3870
237k
    }
3871
246k
  }
3872
215k
  bool isErrorFound() const { return ErrorFound; }
3873
430k
  ArrayRef<Expr *> getImplicitFirstprivate() const {
3874
430k
    return ImplicitFirstprivate;
3875
430k
  }
3876
  ArrayRef<Expr *> getImplicitMap(OpenMPDefaultmapClauseKind DK,
3877
2.58M
                                  OpenMPMapClauseKind MK) const {
3878
2.58M
    return ImplicitMap[DK][MK];
3879
2.58M
  }
3880
  ArrayRef<OpenMPMapModifierKind>
3881
645k
  getImplicitMapModifier(OpenMPDefaultmapClauseKind Kind) const {
3882
645k
    return ImplicitMapModifier[Kind];
3883
645k
  }
3884
216k
  const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
3885
216k
    return VarsWithInheritedDSA;
3886
216k
  }
3887
3888
  DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
3889
216k
      : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {
3890
    // Process declare target link variables for the target directives.
3891
216k
    if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) {
3892
93.1k
      for (DeclRefExpr *E : Stack->getLinkGlobals())
3893
8
        Visit(E);
3894
93.1k
    }
3895
216k
  }
3896
};
3897
} // namespace
3898
3899
static void handleDeclareVariantConstructTrait(DSAStackTy *Stack,
3900
                                               OpenMPDirectiveKind DKind,
3901
672k
                                               bool ScopeEntry) {
3902
672k
  SmallVector<llvm::omp::TraitProperty, 8> Traits;
3903
672k
  if (isOpenMPTargetExecutionDirective(DKind))
3904
271k
    Traits.emplace_back(llvm::omp::TraitProperty::construct_target_target);
3905
672k
  if (isOpenMPTeamsDirective(DKind))
3906
172k
    Traits.emplace_back(llvm::omp::TraitProperty::construct_teams_teams);
3907
672k
  if (isOpenMPParallelDirective(DKind))
3908
245k
    Traits.emplace_back(llvm::omp::TraitProperty::construct_parallel_parallel);
3909
672k
  if (isOpenMPWorksharingDirective(DKind))
3910
193k
    Traits.emplace_back(llvm::omp::TraitProperty::construct_for_for);
3911
672k
  if (isOpenMPSimdDirective(DKind))
3912
190k
    Traits.emplace_back(llvm::omp::TraitProperty::construct_simd_simd);
3913
672k
  Stack->handleConstructTrait(Traits, ScopeEntry);
3914
672k
}
3915
3916
336k
void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
3917
336k
  switch (DKind) {
3918
29.3k
  case OMPD_parallel:
3919
34.5k
  case OMPD_parallel_for:
3920
41.0k
  case OMPD_parallel_for_simd:
3921
44.9k
  case OMPD_parallel_sections:
3922
48.0k
  case OMPD_parallel_master:
3923
69.2k
  case OMPD_teams:
3924
74.1k
  case OMPD_teams_distribute:
3925
79.7k
  case OMPD_teams_distribute_simd: {
3926
79.7k
    QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3927
79.7k
    QualType KmpInt32PtrTy =
3928
79.7k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3929
79.7k
    Sema::CapturedParamNameType Params[] = {
3930
79.7k
        std::make_pair(".global_tid.", KmpInt32PtrTy),
3931
79.7k
        std::make_pair(".bound_tid.", KmpInt32PtrTy),
3932
79.7k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
3933
79.7k
    };
3934
79.7k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3935
79.7k
                             Params);
3936
79.7k
    break;
3937
74.1k
  }
3938
8.02k
  case OMPD_target_teams:
3939
16.0k
  case OMPD_target_parallel:
3940
24.4k
  case OMPD_target_parallel_for:
3941
32.9k
  case OMPD_target_parallel_for_simd:
3942
40.4k
  case OMPD_target_teams_distribute:
3943
49.8k
  case OMPD_target_teams_distribute_simd: {
3944
49.8k
    QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3945
49.8k
    QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3946
49.8k
    QualType KmpInt32PtrTy =
3947
49.8k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3948
49.8k
    QualType Args[] = {VoidPtrTy};
3949
49.8k
    FunctionProtoType::ExtProtoInfo EPI;
3950
49.8k
    EPI.Variadic = true;
3951
49.8k
    QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3952
49.8k
    Sema::CapturedParamNameType Params[] = {
3953
49.8k
        std::make_pair(".global_tid.", KmpInt32Ty),
3954
49.8k
        std::make_pair(".part_id.", KmpInt32PtrTy),
3955
49.8k
        std::make_pair(".privates.", VoidPtrTy),
3956
49.8k
        std::make_pair(
3957
49.8k
            ".copy_fn.",
3958
49.8k
            Context.getPointerType(CopyFnType).withConst().withRestrict()),
3959
49.8k
        std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3960
49.8k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
3961
49.8k
    };
3962
49.8k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3963
49.8k
                             Params, /*OpenMPCaptureLevel=*/0);
3964
    // Mark this captured region as inlined, because we don't use outlined
3965
    // function directly.
3966
49.8k
    getCurCapturedRegion()->TheCapturedDecl->addAttr(
3967
49.8k
        AlwaysInlineAttr::CreateImplicit(
3968
49.8k
            Context, {}, AttributeCommonInfo::AS_Keyword,
3969
49.8k
            AlwaysInlineAttr::Keyword_forceinline));
3970
49.8k
    Sema::CapturedParamNameType ParamsTarget[] = {
3971
49.8k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
3972
49.8k
    };
3973
    // Start a captured region for 'target' with no implicit parameters.
3974
49.8k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3975
49.8k
                             ParamsTarget, /*OpenMPCaptureLevel=*/1);
3976
49.8k
    Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
3977
49.8k
        std::make_pair(".global_tid.", KmpInt32PtrTy),
3978
49.8k
        std::make_pair(".bound_tid.", KmpInt32PtrTy),
3979
49.8k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
3980
49.8k
    };
3981
    // Start a captured region for 'teams' or 'parallel'.  Both regions have
3982
    // the same implicit parameters.
3983
49.8k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3984
49.8k
                             ParamsTeamsOrParallel, /*OpenMPCaptureLevel=*/2);
3985
49.8k
    break;
3986
40.4k
  }
3987
59.3k
  case OMPD_target:
3988
68.0k
  case OMPD_target_simd: {
3989
68.0k
    QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3990
68.0k
    QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3991
68.0k
    QualType KmpInt32PtrTy =
3992
68.0k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3993
68.0k
    QualType Args[] = {VoidPtrTy};
3994
68.0k
    FunctionProtoType::ExtProtoInfo EPI;
3995
68.0k
    EPI.Variadic = true;
3996
68.0k
    QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3997
68.0k
    Sema::CapturedParamNameType Params[] = {
3998
68.0k
        std::make_pair(".global_tid.", KmpInt32Ty),
3999
68.0k
        std::make_pair(".part_id.", KmpInt32PtrTy),
4000
68.0k
        std::make_pair(".privates.", VoidPtrTy),
4001
68.0k
        std::make_pair(
4002
68.0k
            ".copy_fn.",
4003
68.0k
            Context.getPointerType(CopyFnType).withConst().withRestrict()),
4004
68.0k
        std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
4005
68.0k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
4006
68.0k
    };
4007
68.0k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4008
68.0k
                             Params, /*OpenMPCaptureLevel=*/0);
4009
    // Mark this captured region as inlined, because we don't use outlined
4010
    // function directly.
4011
68.0k
    getCurCapturedRegion()->TheCapturedDecl->addAttr(
4012
68.0k
        AlwaysInlineAttr::CreateImplicit(
4013
68.0k
            Context, {}, AttributeCommonInfo::AS_Keyword,
4014
68.0k
            AlwaysInlineAttr::Keyword_forceinline));
4015
68.0k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4016
68.0k
                             std::make_pair(StringRef(), QualType()),
4017
68.0k
                             /*OpenMPCaptureLevel=*/1);
4018
68.0k
    break;
4019
59.3k
  }
4020
5.38k
  case OMPD_atomic:
4021
7.20k
  case OMPD_critical:
4022
8.50k
  case OMPD_section:
4023
9.89k
  case OMPD_master:
4024
10.0k
  case OMPD_masked:
4025
10.1k
  case OMPD_tile:
4026
10.2k
  case OMPD_unroll:
4027
10.2k
    break;
4028
53
  case OMPD_loop:
4029
    // TODO: 'loop' may require additional parameters depending on the binding.
4030
    // Treat similar to OMPD_simd/OMPD_for for now.
4031
6.68k
  case OMPD_simd:
4032
14.5k
  case OMPD_for:
4033
20.9k
  case OMPD_for_simd:
4034
25.3k
  case OMPD_sections:
4035
27.5k
  case OMPD_single:
4036
30.3k
  case OMPD_taskgroup:
4037
32.5k
  case OMPD_distribute:
4038
38.0k
  case OMPD_distribute_simd:
4039
39.4k
  case OMPD_ordered:
4040
44.9k
  case OMPD_target_data:
4041
45.1k
  case OMPD_dispatch: {
4042
45.1k
    Sema::CapturedParamNameType Params[] = {
4043
45.1k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
4044
45.1k
    };
4045
45.1k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4046
45.1k
                             Params);
4047
45.1k
    break;
4048
44.9k
  }
4049
4.28k
  case OMPD_task: {
4050
4.28k
    QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
4051
4.28k
    QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4052
4.28k
    QualType KmpInt32PtrTy =
4053
4.28k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4054
4.28k
    QualType Args[] = {VoidPtrTy};
4055
4.28k
    FunctionProtoType::ExtProtoInfo EPI;
4056
4.28k
    EPI.Variadic = true;
4057
4.28k
    QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
4058
4.28k
    Sema::CapturedParamNameType Params[] = {
4059
4.28k
        std::make_pair(".global_tid.", KmpInt32Ty),
4060
4.28k
        std::make_pair(".part_id.", KmpInt32PtrTy),
4061
4.28k
        std::make_pair(".privates.", VoidPtrTy),
4062
4.28k
        std::make_pair(
4063
4.28k
            ".copy_fn.",
4064
4.28k
            Context.getPointerType(CopyFnType).withConst().withRestrict()),
4065
4.28k
        std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
4066
4.28k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
4067
4.28k
    };
4068
4.28k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4069
4.28k
                             Params);
4070
    // Mark this captured region as inlined, because we don't use outlined
4071
    // function directly.
4072
4.28k
    getCurCapturedRegion()->TheCapturedDecl->addAttr(
4073
4.28k
        AlwaysInlineAttr::CreateImplicit(
4074
4.28k
            Context, {}, AttributeCommonInfo::AS_Keyword,
4075
4.28k
            AlwaysInlineAttr::Keyword_forceinline));
4076
4.28k
    break;
4077
44.9k
  }
4078
5.71k
  case OMPD_taskloop:
4079
10.9k
  case OMPD_taskloop_simd:
4080
15.0k
  case OMPD_master_taskloop:
4081
20.1k
  case OMPD_master_taskloop_simd: {
4082
20.1k
    QualType KmpInt32Ty =
4083
20.1k
        Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
4084
20.1k
            .withConst();
4085
20.1k
    QualType KmpUInt64Ty =
4086
20.1k
        Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
4087
20.1k
            .withConst();
4088
20.1k
    QualType KmpInt64Ty =
4089
20.1k
        Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
4090
20.1k
            .withConst();
4091
20.1k
    QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4092
20.1k
    QualType KmpInt32PtrTy =
4093
20.1k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4094
20.1k
    QualType Args[] = {VoidPtrTy};
4095
20.1k
    FunctionProtoType::ExtProtoInfo EPI;
4096
20.1k
    EPI.Variadic = true;
4097
20.1k
    QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
4098
20.1k
    Sema::CapturedParamNameType Params[] = {
4099
20.1k
        std::make_pair(".global_tid.", KmpInt32Ty),
4100
20.1k
        std::make_pair(".part_id.", KmpInt32PtrTy),
4101
20.1k
        std::make_pair(".privates.", VoidPtrTy),
4102
20.1k
        std::make_pair(
4103
20.1k
            ".copy_fn.",
4104
20.1k
            Context.getPointerType(CopyFnType).withConst().withRestrict()),
4105
20.1k
        std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
4106
20.1k
        std::make_pair(".lb.", KmpUInt64Ty),
4107
20.1k
        std::make_pair(".ub.", KmpUInt64Ty),
4108
20.1k
        std::make_pair(".st.", KmpInt64Ty),
4109
20.1k
        std::make_pair(".liter.", KmpInt32Ty),
4110
20.1k
        std::make_pair(".reductions.", VoidPtrTy),
4111
20.1k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
4112
20.1k
    };
4113
20.1k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4114
20.1k
                             Params);
4115
    // Mark this captured region as inlined, because we don't use outlined
4116
    // function directly.
4117
20.1k
    getCurCapturedRegion()->TheCapturedDecl->addAttr(
4118
20.1k
        AlwaysInlineAttr::CreateImplicit(
4119
20.1k
            Context, {}, AttributeCommonInfo::AS_Keyword,
4120
20.1k
            AlwaysInlineAttr::Keyword_forceinline));
4121
20.1k
    break;
4122
15.0k
  }
4123
3.35k
  case OMPD_parallel_master_taskloop:
4124
7.75k
  case OMPD_parallel_master_taskloop_simd: {
4125
7.75k
    QualType KmpInt32Ty =
4126
7.75k
        Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
4127
7.75k
            .withConst();
4128
7.75k
    QualType KmpUInt64Ty =
4129
7.75k
        Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
4130
7.75k
            .withConst();
4131
7.75k
    QualType KmpInt64Ty =
4132
7.75k
        Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
4133
7.75k
            .withConst();
4134
7.75k
    QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4135
7.75k
    QualType KmpInt32PtrTy =
4136
7.75k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4137
7.75k
    Sema::CapturedParamNameType ParamsParallel[] = {
4138
7.75k
        std::make_pair(".global_tid.", KmpInt32PtrTy),
4139
7.75k
        std::make_pair(".bound_tid.", KmpInt32PtrTy),
4140
7.75k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
4141
7.75k
    };
4142
    // Start a captured region for 'parallel'.
4143
7.75k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4144
7.75k
                             ParamsParallel, /*OpenMPCaptureLevel=*/0);
4145
7.75k
    QualType Args[] = {VoidPtrTy};
4146
7.75k
    FunctionProtoType::ExtProtoInfo EPI;
4147
7.75k
    EPI.Variadic = true;
4148
7.75k
    QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
4149
7.75k
    Sema::CapturedParamNameType Params[] = {
4150
7.75k
        std::make_pair(".global_tid.", KmpInt32Ty),
4151
7.75k
        std::make_pair(".part_id.", KmpInt32PtrTy),
4152
7.75k
        std::make_pair(".privates.", VoidPtrTy),
4153
7.75k
        std::make_pair(
4154
7.75k
            ".copy_fn.",
4155
7.75k
            Context.getPointerType(CopyFnType).withConst().withRestrict()),
4156
7.75k
        std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
4157
7.75k
        std::make_pair(".lb.", KmpUInt64Ty),
4158
7.75k
        std::make_pair(".ub.", KmpUInt64Ty),
4159
7.75k
        std::make_pair(".st.", KmpInt64Ty),
4160
7.75k
        std::make_pair(".liter.", KmpInt32Ty),
4161
7.75k
        std::make_pair(".reductions.", VoidPtrTy),
4162
7.75k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
4163
7.75k
    };
4164
7.75k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4165
7.75k
                             Params, /*OpenMPCaptureLevel=*/1);
4166
    // Mark this captured region as inlined, because we don't use outlined
4167
    // function directly.
4168
7.75k
    getCurCapturedRegion()->TheCapturedDecl->addAttr(
4169
7.75k
        AlwaysInlineAttr::CreateImplicit(
4170
7.75k
            Context, {}, AttributeCommonInfo::AS_Keyword,
4171
7.75k
            AlwaysInlineAttr::Keyword_forceinline));
4172
7.75k
    break;
4173
3.35k
  }
4174
6.91k
  case OMPD_distribute_parallel_for_simd:
4175
12.3k
  case OMPD_distribute_parallel_for: {
4176
12.3k
    QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
4177
12.3k
    QualType KmpInt32PtrTy =
4178
12.3k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4179
12.3k
    Sema::CapturedParamNameType Params[] = {
4180
12.3k
        std::make_pair(".global_tid.", KmpInt32PtrTy),
4181
12.3k
        std::make_pair(".bound_tid.", KmpInt32PtrTy),
4182
12.3k
        std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
4183
12.3k
        std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
4184
12.3k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
4185
12.3k
    };
4186
12.3k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4187
12.3k
                             Params);
4188
12.3k
    break;
4189
6.91k
  }
4190
7.89k
  case OMPD_target_teams_distribute_parallel_for:
4191
17.7k
  case OMPD_target_teams_distribute_parallel_for_simd: {
4192
17.7k
    QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
4193
17.7k
    QualType KmpInt32PtrTy =
4194
17.7k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4195
17.7k
    QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4196
4197
17.7k
    QualType Args[] = {VoidPtrTy};
4198
17.7k
    FunctionProtoType::ExtProtoInfo EPI;
4199
17.7k
    EPI.Variadic = true;
4200
17.7k
    QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
4201
17.7k
    Sema::CapturedParamNameType Params[] = {
4202
17.7k
        std::make_pair(".global_tid.", KmpInt32Ty),
4203
17.7k
        std::make_pair(".part_id.", KmpInt32PtrTy),
4204
17.7k
        std::make_pair(".privates.", VoidPtrTy),
4205
17.7k
        std::make_pair(
4206
17.7k
            ".copy_fn.",
4207
17.7k
            Context.getPointerType(CopyFnType).withConst().withRestrict()),
4208
17.7k
        std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
4209
17.7k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
4210
17.7k
    };
4211
17.7k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4212
17.7k
                             Params, /*OpenMPCaptureLevel=*/0);
4213
    // Mark this captured region as inlined, because we don't use outlined
4214
    // function directly.
4215
17.7k
    getCurCapturedRegion()->TheCapturedDecl->addAttr(
4216
17.7k
        AlwaysInlineAttr::CreateImplicit(
4217
17.7k
            Context, {}, AttributeCommonInfo::AS_Keyword,
4218
17.7k
            AlwaysInlineAttr::Keyword_forceinline));
4219
17.7k
    Sema::CapturedParamNameType ParamsTarget[] = {
4220
17.7k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
4221
17.7k
    };
4222
    // Start a captured region for 'target' with no implicit parameters.
4223
17.7k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4224
17.7k
                             ParamsTarget, /*OpenMPCaptureLevel=*/1);
4225
4226
17.7k
    Sema::CapturedParamNameType ParamsTeams[] = {
4227
17.7k
        std::make_pair(".global_tid.", KmpInt32PtrTy),
4228
17.7k
        std::make_pair(".bound_tid.", KmpInt32PtrTy),
4229
17.7k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
4230
17.7k
    };
4231
    // Start a captured region for 'target' with no implicit parameters.
4232
17.7k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4233
17.7k
                             ParamsTeams, /*OpenMPCaptureLevel=*/2);
4234
4235
17.7k
    Sema::CapturedParamNameType ParamsParallel[] = {
4236
17.7k
        std::make_pair(".global_tid.", KmpInt32PtrTy),
4237
17.7k
        std::make_pair(".bound_tid.", KmpInt32PtrTy),
4238
17.7k
        std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
4239
17.7k
        std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
4240
17.7k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
4241
17.7k
    };
4242
    // Start a captured region for 'teams' or 'parallel'.  Both regions have
4243
    // the same implicit parameters.
4244
17.7k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4245
17.7k
                             ParamsParallel, /*OpenMPCaptureLevel=*/3);
4246
17.7k
    break;
4247
7.89k
  }
4248
4249
5.47k
  case OMPD_teams_distribute_parallel_for:
4250
11.9k
  case OMPD_teams_distribute_parallel_for_simd: {
4251
11.9k
    QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
4252
11.9k
    QualType KmpInt32PtrTy =
4253
11.9k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4254
4255
11.9k
    Sema::CapturedParamNameType ParamsTeams[] = {
4256
11.9k
        std::make_pair(".global_tid.", KmpInt32PtrTy),
4257
11.9k
        std::make_pair(".bound_tid.", KmpInt32PtrTy),
4258
11.9k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
4259
11.9k
    };
4260
    // Start a captured region for 'target' with no implicit parameters.
4261
11.9k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4262
11.9k
                             ParamsTeams, /*OpenMPCaptureLevel=*/0);
4263
4264
11.9k
    Sema::CapturedParamNameType ParamsParallel[] = {
4265
11.9k
        std::make_pair(".global_tid.", KmpInt32PtrTy),
4266
11.9k
        std::make_pair(".bound_tid.", KmpInt32PtrTy),
4267
11.9k
        std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
4268
11.9k
        std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
4269
11.9k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
4270
11.9k
    };
4271
    // Start a captured region for 'teams' or 'parallel'.  Both regions have
4272
    // the same implicit parameters.
4273
11.9k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4274
11.9k
                             ParamsParallel, /*OpenMPCaptureLevel=*/1);
4275
11.9k
    break;
4276
5.47k
  }
4277
4.50k
  case OMPD_target_update:
4278
6.60k
  case OMPD_target_enter_data:
4279
8.67k
  case OMPD_target_exit_data: {
4280
8.67k
    QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
4281
8.67k
    QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4282
8.67k
    QualType KmpInt32PtrTy =
4283
8.67k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4284
8.67k
    QualType Args[] = {VoidPtrTy};
4285
8.67k
    FunctionProtoType::ExtProtoInfo EPI;
4286
8.67k
    EPI.Variadic = true;
4287
8.67k
    QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
4288
8.67k
    Sema::CapturedParamNameType Params[] = {
4289
8.67k
        std::make_pair(".global_tid.", KmpInt32Ty),
4290
8.67k
        std::make_pair(".part_id.", KmpInt32PtrTy),
4291
8.67k
        std::make_pair(".privates.", VoidPtrTy),
4292
8.67k
        std::make_pair(
4293
8.67k
            ".copy_fn.",
4294
8.67k
            Context.getPointerType(CopyFnType).withConst().withRestrict()),
4295
8.67k
        std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
4296
8.67k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
4297
8.67k
    };
4298
8.67k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4299
8.67k
                             Params);
4300
    // Mark this captured region as inlined, because we don't use outlined
4301
    // function directly.
4302
8.67k
    getCurCapturedRegion()->TheCapturedDecl->addAttr(
4303
8.67k
        AlwaysInlineAttr::CreateImplicit(
4304
8.67k
            Context, {}, AttributeCommonInfo::AS_Keyword,
4305
8.67k
            AlwaysInlineAttr::Keyword_forceinline));
4306
8.67k
    break;
4307
6.60k
  }
4308
0
  case OMPD_threadprivate:
4309
0
  case OMPD_allocate:
4310
0
  case OMPD_taskyield:
4311
0
  case OMPD_barrier:
4312
0
  case OMPD_taskwait:
4313
0
  case OMPD_cancellation_point:
4314
0
  case OMPD_cancel:
4315
0
  case OMPD_flush:
4316
0
  case OMPD_depobj:
4317
0
  case OMPD_scan:
4318
0
  case OMPD_declare_reduction:
4319
0
  case OMPD_declare_mapper:
4320
0
  case OMPD_declare_simd:
4321
0
  case OMPD_declare_target:
4322
0
  case OMPD_end_declare_target:
4323
0
  case OMPD_requires:
4324
0
  case OMPD_declare_variant:
4325
0
  case OMPD_begin_declare_variant:
4326
0
  case OMPD_end_declare_variant:
4327
0
  case OMPD_metadirective:
4328
0
    llvm_unreachable("OpenMP Directive is not allowed");
4329
0
  case OMPD_unknown:
4330
0
  default:
4331
0
    llvm_unreachable("Unknown OpenMP directive");
4332
336k
  }
4333
336k
  DSAStack->setContext(CurContext);
4334
336k
  handleDeclareVariantConstructTrait(DSAStack, DKind, /* ScopeEntry */ true);
4335
336k
}
4336
4337
316
int Sema::getNumberOfConstructScopes(unsigned Level) const {
4338
316
  return getOpenMPCaptureLevels(DSAStack->getDirective(Level));
4339
316
}
4340
4341
441k
int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
4342
441k
  SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
4343
441k
  getOpenMPCaptureRegions(CaptureRegions, DKind);
4344
441k
  return CaptureRegions.size();
4345
441k
}
4346
4347
static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
4348
                                             Expr *CaptureExpr, bool WithInit,
4349
66.2k
                                             bool AsExpression) {
4350
66.2k
  assert(CaptureExpr);
4351
0
  ASTContext &C = S.getASTContext();
4352
66.2k
  Expr *Init = AsExpression ? 
CaptureExpr63.4k
:
CaptureExpr->IgnoreImpCasts()2.81k
;
4353
66.2k
  QualType Ty = Init->getType();
4354
66.2k
  if (CaptureExpr->getObjectKind() == OK_Ordinary && 
CaptureExpr->isGLValue()66.1k
) {
4355
4.11k
    if (S.getLangOpts().CPlusPlus) {
4356
4.11k
      Ty = C.getLValueReferenceType(Ty);
4357
4.11k
    } else {
4358
0
      Ty = C.getPointerType(Ty);
4359
0
      ExprResult Res =
4360
0
          S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
4361
0
      if (!Res.isUsable())
4362
0
        return nullptr;
4363
0
      Init = Res.get();
4364
0
    }
4365
4.11k
    WithInit = true;
4366
4.11k
  }
4367
66.2k
  auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
4368
66.2k
                                          CaptureExpr->getBeginLoc());
4369
66.2k
  if (!WithInit)
4370
144
    CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
4371
66.2k
  S.CurContext->addHiddenDecl(CED);
4372
66.2k
  Sema::TentativeAnalysisScope Trap(S);
4373
66.2k
  S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
4374
66.2k
  return CED;
4375
66.2k
}
4376
4377
static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
4378
3.03k
                                 bool WithInit) {
4379
3.03k
  OMPCapturedExprDecl *CD;
4380
3.03k
  if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
4381
218
    CD = cast<OMPCapturedExprDecl>(VD);
4382
2.81k
  else
4383
2.81k
    CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
4384
2.81k
                          /*AsExpression=*/false);
4385
3.03k
  return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
4386
3.03k
                          CaptureExpr->getExprLoc());
4387
3.03k
}
4388
4389
127k
static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
4390
127k
  CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
4391
127k
  if (!Ref) {
4392
63.4k
    OMPCapturedExprDecl *CD = buildCaptureDecl(
4393
63.4k
        S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
4394
63.4k
        /*WithInit=*/true, /*AsExpression=*/true);
4395
63.4k
    Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
4396
63.4k
                           CaptureExpr->getExprLoc());
4397
63.4k
  }
4398
127k
  ExprResult Res = Ref;
4399
127k
  if (!S.getLangOpts().CPlusPlus &&
4400
127k
      
CaptureExpr->getObjectKind() == OK_Ordinary1.43k
&&
CaptureExpr->isGLValue()1.43k
&&
4401
127k
      
Ref->getType()->isPointerType()0
) {
4402
0
    Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
4403
0
    if (!Res.isUsable())
4404
0
      return ExprError();
4405
0
  }
4406
127k
  return S.DefaultLvalueConversion(Res.get());
4407
127k
}
4408
4409
namespace {
4410
// OpenMP directives parsed in this section are represented as a
4411
// CapturedStatement with an associated statement.  If a syntax error
4412
// is detected during the parsing of the associated statement, the
4413
// compiler must abort processing and close the CapturedStatement.
4414
//
4415
// Combined directives such as 'target parallel' have more than one
4416
// nested CapturedStatements.  This RAII ensures that we unwind out
4417
// of all the nested CapturedStatements when an error is found.
4418
class CaptureRegionUnwinderRAII {
4419
private:
4420
  Sema &S;
4421
  bool &ErrorFound;
4422
  OpenMPDirectiveKind DKind = OMPD_unknown;
4423
4424
public:
4425
  CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
4426
                            OpenMPDirectiveKind DKind)
4427
325k
      : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
4428
325k
  ~CaptureRegionUnwinderRAII() {
4429
325k
    if (ErrorFound) {
4430
12.9k
      int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
4431
31.4k
      while (--ThisCaptureLevel >= 0)
4432
18.5k
        S.ActOnCapturedRegionError();
4433
12.9k
    }
4434
325k
  }
4435
};
4436
} // namespace
4437
4438
1.18M
void Sema::tryCaptureOpenMPLambdas(ValueDecl *V) {
4439
  // Capture variables captured by reference in lambdas for target-based
4440
  // directives.
4441
1.18M
  if (!CurContext->isDependentContext() &&
4442
1.18M
      (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) ||
4443
1.18M
       isOpenMPTargetDataManagementDirective(
4444
688k
           DSAStack->getCurrentDirective()))) {
4445
529k
    QualType Type = V->getType();
4446
529k
    if (const auto *RD = Type.getCanonicalType()
4447
529k
                             .getNonReferenceType()
4448
529k
                             ->getAsCXXRecordDecl()) {
4449
42.0k
      bool SavedForceCaptureByReferenceInTargetExecutable =
4450
42.0k
          DSAStack->isForceCaptureByReferenceInTargetExecutable();
4451
42.0k
      DSAStack->setForceCaptureByReferenceInTargetExecutable(
4452
42.0k
          /*V=*/true);
4453
42.0k
      if (RD->isLambda()) {
4454
120
        llvm::DenseMap<const VarDecl *, FieldDecl *> Captures;
4455
120
        FieldDecl *ThisCapture;
4456
120
        RD->getCaptureFields(Captures, ThisCapture);
4457
184
        for (const LambdaCapture &LC : RD->captures()) {
4458
184
          if (LC.getCaptureKind() == LCK_ByRef) {
4459
12
            VarDecl *VD = LC.getCapturedVar();
4460
12
            DeclContext *VDC = VD->getDeclContext();
4461
12
            if (!VDC->Encloses(CurContext))
4462
0
              continue;
4463
12
            MarkVariableReferenced(LC.getLocation(), VD);
4464
172
          } else if (LC.getCaptureKind() == LCK_This) {
4465
0
            QualType ThisTy = getCurrentThisType();
4466
0
            if (!ThisTy.isNull() &&
4467
0
                Context.typesAreCompatible(ThisTy, ThisCapture->getType()))
4468
0
              CheckCXXThisCapture(LC.getLocation());
4469
0
          }
4470
184
        }
4471
120
      }
4472
42.0k
      DSAStack->setForceCaptureByReferenceInTargetExecutable(
4473
42.0k
          SavedForceCaptureByReferenceInTargetExecutable);
4474
42.0k
    }
4475
529k
  }
4476
1.18M
}
4477
4478
static bool checkOrderedOrderSpecified(Sema &S,
4479
313k
                                       const ArrayRef<OMPClause *> Clauses) {
4480
313k
  const OMPOrderedClause *Ordered = nullptr;
4481
313k
  const OMPOrderClause *Order = nullptr;
4482
4483
313k
  for (const OMPClause *Clause : Clauses) {
4484
149k
    if (Clause->getClauseKind() == OMPC_ordered)
4485
1.10k
      Ordered = cast<OMPOrderedClause>(Clause);
4486
148k
    else if (Clause->getClauseKind() == OMPC_order) {
4487
191
      Order = cast<OMPOrderClause>(Clause);
4488
191
      if (Order->getKind() != OMPC_ORDER_concurrent)
4489
0
        Order = nullptr;
4490
191
    }
4491
149k
    if (Ordered && 
Order1.44k
)
4492
2
      break;
4493
149k
  }
4494
4495
313k
  if (Ordered && 
Order1.10k
) {
4496
2
    S.Diag(Order->getKindKwLoc(),
4497
2
           diag::err_omp_simple_clause_incompatible_with_ordered)
4498
2
        << getOpenMPClauseName(OMPC_order)
4499
2
        << getOpenMPSimpleClauseTypeName(OMPC_order, OMPC_ORDER_concurrent)
4500
2
        << SourceRange(Order->getBeginLoc(), Order->getEndLoc());
4501
2
    S.Diag(Ordered->getBeginLoc(), diag::note_omp_ordered_param)
4502
2
        << 0 << SourceRange(Ordered->getBeginLoc(), Ordered->getEndLoc());
4503
2
    return true;
4504
2
  }
4505
313k
  return false;
4506
313k
}
4507
4508
StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
4509
336k
                                      ArrayRef<OMPClause *> Clauses) {
4510
336k
  handleDeclareVariantConstructTrait(DSAStack, DSAStack->getCurrentDirective(),
4511
336k
                                     /* ScopeEntry */ false);
4512
336k
  if (DSAStack->getCurrentDirective() == OMPD_atomic ||
4513
336k
      
DSAStack330k
->getCurrentDirective() == OMPD_critical330k
||
4514
336k
      
DSAStack328k
->getCurrentDirective() == OMPD_section328k
||
4515
336k
      
DSAStack327k
->getCurrentDirective() == OMPD_master327k
||
4516
336k
      
DSAStack326k
->getCurrentDirective() == OMPD_masked326k
)
4517
10.0k
    return S;
4518
4519
325k
  bool ErrorFound = false;
4520
325k
  CaptureRegionUnwinderRAII CaptureRegionUnwinder(
4521
325k
      *this, ErrorFound, DSAStack->getCurrentDirective());
4522
325k
  if (!S.isUsable()) {
4523
12.8k
    ErrorFound = true;
4524
12.8k
    return StmtError();
4525
12.8k
  }
4526
4527
313k
  SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
4528
313k
  getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
4529
313k
  OMPOrderedClause *OC = nullptr;
4530
313k
  OMPScheduleClause *SC = nullptr;
4531
313k
  SmallVector<const OMPLinearClause *, 4> LCs;
4532
313k
  SmallVector<const OMPClauseWithPreInit *, 4> PICs;
4533
  // This is required for proper codegen.
4534
313k
  for (OMPClause *Clause : Clauses) {
4535
149k
    if (!LangOpts.OpenMPSimd &&
4536
149k
        
isOpenMPTaskingDirective(79.4k
DSAStack79.4k
->getCurrentDirective()) &&
4537
149k
        
Clause->getClauseKind() == OMPC_in_reduction8.37k
) {
4538
      // Capture taskgroup task_reduction descriptors inside the tasking regions
4539
      // with the corresponding in_reduction items.
4540
780
      auto *IRC = cast<OMPInReductionClause>(Clause);
4541
780
      for (Expr *E : IRC->taskgroup_descriptors())
4542
913
        if (E)
4543
476
          MarkDeclarationsReferencedInExpr(E);
4544
780
    }
4545
149k
    if (isOpenMPPrivate(Clause->getClauseKind()) ||
4546
149k
        
Clause->getClauseKind() == OMPC_copyprivate95.0k
||
4547
149k
        
(94.8k
getLangOpts().OpenMPUseTLS94.8k
&&
4548
94.8k
         
getASTContext().getTargetInfo().isTLSSupported()51.3k
&&
4549
94.8k
         
Clause->getClauseKind() == OMPC_copyin49.9k
)) {
4550
55.1k
      DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
4551
      // Mark all variables in private list clauses as used in inner region.
4552
74.5k
      for (Stmt *VarRef : Clause->children()) {
4553
74.5k
        if (auto *E = cast_or_null<Expr>(VarRef)) {
4554
74.5k
          MarkDeclarationsReferencedInExpr(E);
4555
74.5k
        }
4556
74.5k
      }
4557
55.1k
      DSAStack->setForceVarCapturing(/*V=*/false);
4558
94.6k
    } else if (isOpenMPLoopTransformationDirective(
4559
94.6k
                   DSAStack->getCurrentDirective())) {
4560
160
      assert(CaptureRegions.empty() &&
4561
160
             "No captured regions in loop transformation directives.");
4562
94.5k
    } else if (CaptureRegions.size() > 1 ||
4563
94.5k
               
CaptureRegions.back() != OMPD_unknown42.0k
) {
4564
81.9k
      if (auto *C = OMPClauseWithPreInit::get(Clause))
4565
23.7k
        PICs.push_back(C);
4566
81.9k
      if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
4567
0
        if (Expr *E = C->getPostUpdateExpr())
4568
0
          MarkDeclarationsReferencedInExpr(E);
4569
0
      }
4570
81.9k
    }
4571
149k
    if (Clause->getClauseKind() == OMPC_schedule)
4572
3.63k
      SC = cast<OMPScheduleClause>(Clause);
4573
146k
    else if (Clause->getClauseKind() == OMPC_ordered)
4574
1.10k
      OC = cast<OMPOrderedClause>(Clause);
4575
145k
    else if (Clause->getClauseKind() == OMPC_linear)
4576
2.61k
      LCs.push_back(cast<OMPLinearClause>(Clause));
4577
149k
  }
4578
  // Capture allocator expressions if used.
4579
313k
  for (Expr *E : DSAStack->getInnerAllocators())
4580
985
    MarkDeclarationsReferencedInExpr(E);
4581
  // OpenMP, 2.7.1 Loop Construct, Restrictions
4582
  // The nonmonotonic modifier cannot be specified if an ordered clause is
4583
  // specified.
4584
313k
  if (SC &&
4585
313k
      
(3.63k
SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic3.63k
||
4586
3.63k
       SC->getSecondScheduleModifier() ==
4587
3.51k
           OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
4588
313k
      
OC127
) {
4589
8
    Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
4590
8
             ? SC->getFirstScheduleModifierLoc()
4591
8
             : 
SC->getSecondScheduleModifierLoc()0
,
4592
8
         diag::err_omp_simple_clause_incompatible_with_ordered)
4593
8
        << getOpenMPClauseName(OMPC_schedule)
4594
8
        << getOpenMPSimpleClauseTypeName(OMPC_schedule,
4595
8
                                         OMPC_SCHEDULE_MODIFIER_nonmonotonic)
4596
8
        << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
4597
8
    ErrorFound = true;
4598
8
  }
4599
  // OpenMP 5.0, 2.9.2 Worksharing-Loop Construct, Restrictions.
4600
  // If an order(concurrent) clause is present, an ordered clause may not appear
4601
  // on the same directive.
4602
313k
  if (checkOrderedOrderSpecified(*this, Clauses))
4603
2
    ErrorFound = true;
4604
313k
  if (!LCs.empty() && 
OC2.55k
&&
OC->getNumForLoops()32
) {
4605
4
    for (const OMPLinearClause *C : LCs) {
4606
4
      Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
4607
4
          << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
4608
4
    }
4609
4
    ErrorFound = true;
4610
4
  }
4611
313k
  if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
4612
313k
      
isOpenMPSimdDirective(95.3k
DSAStack95.3k
->getCurrentDirective()) &&
OC44.6k
&&
4613
313k
      
OC->getNumForLoops()228
) {
4614
76
    Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
4615
76
        << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
4616
76
    ErrorFound = true;
4617
76
  }
4618
313k
  if (ErrorFound) {
4619
90
    return StmtError();
4620
90
  }
4621
313k
  StmtResult SR = S;
4622
313k
  unsigned CompletedRegions = 0;
4623
548k
  for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
4624
    // Mark all variables in private list clauses as used in inner region.
4625
    // Required for proper codegen of combined directives.
4626
    // TODO: add processing for other clauses.
4627
548k
    if (ThisCaptureRegion != OMPD_unknown) {
4628
503k
      for (const clang::OMPClauseWithPreInit *C : PICs) {
4629
52.1k
        OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
4630
        // Find the particular capture region for the clause if the
4631
        // directive is a combined one with multiple capture regions.
4632
        // If the directive is not a combined one, the capture region
4633
        // associated with the clause is OMPD_unknown and is generated
4634
        // only once.
4635
52.1k
        if (CaptureRegion == ThisCaptureRegion ||
4636
52.1k
            
CaptureRegion == OMPD_unknown44.1k
) {
4637
39.4k
          if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
4638
6.88k
            for (Decl *D : DS->decls())
4639
6.88k
              MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
4640
6.88k
          }
4641
39.4k
        }
4642
52.1k
      }
4643
503k
    }
4644
548k
    if (ThisCaptureRegion == OMPD_target) {
4645
      // Capture allocator traits in the target region. They are used implicitly
4646
      // and, thus, are not captured by default.
4647
130k
      for (OMPClause *C : Clauses) {
4648
61.0k
        if (const auto *UAC = dyn_cast<OMPUsesAllocatorsClause>(C)) {
4649
474
          for (unsigned I = 0, End = UAC->getNumberOfAllocators(); I < End;
4650
256
               ++I) {
4651
256
            OMPUsesAllocatorsClause::Data D = UAC->getAllocatorData(I);
4652
256
            if (Expr *E = D.AllocatorTraits)
4653
36
              MarkDeclarationsReferencedInExpr(E);
4654
256
          }
4655
218
          continue;
4656
218
        }
4657
61.0k
      }
4658
130k
    }
4659
548k
    if (ThisCaptureRegion == OMPD_parallel) {
4660
      // Capture temp arrays for inscan reductions and locals in aligned
4661
      // clauses.
4662
117k
      for (OMPClause *C : Clauses) {
4663
61.3k
        if (auto *RC = dyn_cast<OMPReductionClause>(C)) {
4664
12.2k
          if (RC->getModifier() != OMPC_REDUCTION_inscan)
4665
12.1k
            continue;
4666
46
          for (Expr *E : RC->copy_array_temps())
4667
86
            MarkDeclarationsReferencedInExpr(E);
4668
46
        }
4669
49.1k
        if (auto *AC = dyn_cast<OMPAlignedClause>(C)) {
4670
600
          for (Expr *E : AC->varlists())
4671
648
            MarkDeclarationsReferencedInExpr(E);
4672
600
        }
4673
49.1k
      }
4674
117k
    }
4675
548k
    if (++CompletedRegions == CaptureRegions.size())
4676
312k
      DSAStack->setBodyComplete();
4677
548k
    SR = ActOnCapturedRegionEnd(SR.get());
4678
548k
  }
4679
313k
  return SR;
4680
313k
}
4681
4682
static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
4683
                              OpenMPDirectiveKind CancelRegion,
4684
321k
                              SourceLocation StartLoc) {
4685
  // CancelRegion is only needed for cancel and cancellation_point.
4686
321k
  if (CurrentRegion != OMPD_cancel && 
CurrentRegion != OMPD_cancellation_point320k
)
4687
320k
    return false;
4688
4689
995
  if (CancelRegion == OMPD_parallel || 
CancelRegion == OMPD_for637
||
4690
995
      
CancelRegion == OMPD_sections404
||
CancelRegion == OMPD_taskgroup252
)
4691
975
    return false;
4692
4693
20
  SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4694
20
      << getOpenMPDirectiveName(CancelRegion);
4695
20
  return true;
4696
995
}
4697
4698
static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
4699
                                  OpenMPDirectiveKind CurrentRegion,
4700
                                  const DeclarationNameInfo &CurrentName,
4701
                                  OpenMPDirectiveKind CancelRegion,
4702
                                  OpenMPBindClauseKind BindKind,
4703
321k
                                  SourceLocation StartLoc) {
4704
321k
  if (Stack->getCurScope()) {
4705
229k
    OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
4706
229k
    OpenMPDirectiveKind OffendingRegion = ParentRegion;
4707
229k
    bool NestingProhibited = false;
4708
229k
    bool CloseNesting = true;
4709
229k
    bool OrphanSeen = false;
4710
229k
    enum {
4711
229k
      NoRecommend,
4712
229k
      ShouldBeInParallelRegion,
4713
229k
      ShouldBeInOrderedRegion,
4714
229k
      ShouldBeInTargetRegion,
4715
229k
      ShouldBeInTeamsRegion,
4716
229k
      ShouldBeInLoopSimdRegion,
4717
229k
    } Recommend = NoRecommend;
4718
229k
    if (isOpenMPSimdDirective(ParentRegion) &&
4719
229k
        
(5.02k
(5.02k
SemaRef.LangOpts.OpenMP <= 455.02k
&&
CurrentRegion != OMPD_ordered3.19k
) ||
4720
5.02k
         
(1.94k
SemaRef.LangOpts.OpenMP >= 501.94k
&&
CurrentRegion != OMPD_ordered1.82k
&&
4721
1.94k
          
CurrentRegion != OMPD_simd1.64k
&&
CurrentRegion != OMPD_atomic1.60k
&&
4722
4.51k
          
CurrentRegion != OMPD_scan1.56k
))) {
4723
      // OpenMP [2.16, Nesting of Regions]
4724
      // OpenMP constructs may not be nested inside a simd region.
4725
      // OpenMP [2.8.1,simd Construct, Restrictions]
4726
      // An ordered construct with the simd clause is the only OpenMP
4727
      // construct that can appear in the simd region.
4728
      // Allowing a SIMD construct nested in another SIMD construct is an
4729
      // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
4730
      // message.
4731
      // OpenMP 5.0 [2.9.3.1, simd Construct, Restrictions]
4732
      // The only OpenMP constructs that can be encountered during execution of
4733
      // a simd region are the atomic construct, the loop construct, the simd
4734
      // construct and the ordered construct with the simd clause.
4735
4.51k
      SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
4736
4.51k
                                 ? 
diag::err_omp_prohibited_region_simd4.43k
4737
4.51k
                                 : 
diag::warn_omp_nesting_simd76
)
4738
4.51k
          << (SemaRef.LangOpts.OpenMP >= 50 ? 
11.43k
:
03.08k
);
4739
4.51k
      return CurrentRegion != OMPD_simd;
4740
4.51k
    }
4741
225k
    if (ParentRegion == OMPD_atomic) {
4742
      // OpenMP [2.16, Nesting of Regions]
4743
      // OpenMP constructs may not be nested inside an atomic region.
4744
516
      SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
4745
516
      return true;
4746
516
    }
4747
224k
    if (CurrentRegion == OMPD_section) {
4748
      // OpenMP [2.7.2, sections Construct, Restrictions]
4749
      // Orphaned section directives are prohibited. That is, the section
4750
      // directives must appear within the sections construct and must not be
4751
      // encountered elsewhere in the sections region.
4752
923
      if (ParentRegion != OMPD_sections &&
4753
923
          
ParentRegion != OMPD_parallel_sections300
) {
4754
222
        SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
4755
222
            << (ParentRegion != OMPD_unknown)
4756
222
            << getOpenMPDirectiveName(ParentRegion);
4757
222
        return true;
4758
222
      }
4759
701
      return false;
4760
923
    }
4761
    // Allow some constructs (except teams and cancellation constructs) to be
4762
    // orphaned (they could be used in functions, called from OpenMP regions
4763
    // with the required preconditions).
4764
223k
    if (ParentRegion == OMPD_unknown &&
4765
223k
        
!isOpenMPNestingTeamsDirective(CurrentRegion)149k
&&
4766
223k
        
CurrentRegion != OMPD_cancellation_point148k
&&
4767
223k
        
CurrentRegion != OMPD_cancel148k
&&
CurrentRegion != OMPD_scan148k
)
4768
148k
      return false;
4769
74.9k
    if (CurrentRegion == OMPD_cancellation_point ||
4770
74.9k
        
CurrentRegion == OMPD_cancel74.7k
) {
4771
      // OpenMP [2.16, Nesting of Regions]
4772
      // A cancellation point construct for which construct-type-clause is
4773
      // taskgroup must be nested inside a task construct. A cancellation
4774
      // point construct for which construct-type-clause is not taskgroup must
4775
      // be closely nested inside an OpenMP construct that matches the type
4776
      // specified in construct-type-clause.
4777
      // A cancel construct for which construct-type-clause is taskgroup must be
4778
      // nested inside a task construct. A cancel construct for which
4779
      // construct-type-clause is not taskgroup must be closely nested inside an
4780
      // OpenMP construct that matches the type specified in
4781
      // construct-type-clause.
4782
847
      NestingProhibited =
4783
847
          !((CancelRegion == OMPD_parallel &&
4784
847
             
(302
ParentRegion == OMPD_parallel302
||
4785
302
              
ParentRegion == OMPD_target_parallel116
)) ||
4786
847
            
(609
CancelRegion == OMPD_for609
&&
4787
609
             
(221
ParentRegion == OMPD_for221
||
ParentRegion == OMPD_parallel_for185
||
4788
221
              
ParentRegion == OMPD_target_parallel_for165
||
4789
221
              
ParentRegion == OMPD_distribute_parallel_for85
||
4790
221
              
ParentRegion == OMPD_teams_distribute_parallel_for65
||
4791
221
              
ParentRegion == OMPD_target_teams_distribute_parallel_for53
)) ||
4792
847
            
(416
CancelRegion == OMPD_taskgroup416
&&
4793
416
             
(172
ParentRegion == OMPD_task172
||
4794
172
              
(136
SemaRef.getLangOpts().OpenMP >= 50136
&&
4795
136
               
(112
ParentRegion == OMPD_taskloop112
||
4796
112
                
ParentRegion == OMPD_master_taskloop84
||
4797
112
                
ParentRegion == OMPD_parallel_master_taskloop56
)))) ||
4798
847
            
(296
CancelRegion == OMPD_sections296
&&
4799
296
             
(152
ParentRegion == OMPD_section152
||
ParentRegion == OMPD_sections116
||
4800
152
              
ParentRegion == OMPD_parallel_sections68
)));
4801
847
      OrphanSeen = ParentRegion == OMPD_unknown;
4802
74.1k
    } else if (CurrentRegion == OMPD_master || 
CurrentRegion == OMPD_masked73.7k
) {
4803
      // OpenMP 5.1 [2.22, Nesting of Regions]
4804
      // A masked region may not be closely nested inside a worksharing, loop,
4805
      // atomic, task, or taskloop region.
4806
431
      NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
4807
431
                          
isOpenMPGenericLoopDirective(ParentRegion)287
||
4808
431
                          
isOpenMPTaskingDirective(ParentRegion)286
;
4809
73.7k
    } else if (CurrentRegion == OMPD_critical && 
CurrentName.getName()448
) {
4810
      // OpenMP [2.16, Nesting of Regions]
4811
      // A critical region may not be nested (closely or otherwise) inside a
4812
      // critical region with the same name. Note that this restriction is not
4813
      // sufficient to prevent deadlock.
4814
76
      SourceLocation PreviousCriticalLoc;
4815
76
      bool DeadLock = Stack->hasDirective(
4816
76
          [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
4817
76
                                              const DeclarationNameInfo &DNI,
4818
176
                                              SourceLocation Loc) {
4819
176
            if (K == OMPD_critical && 
DNI.getName() == CurrentName.getName()130
) {
4820
22
              PreviousCriticalLoc = Loc;
4821
22
              return true;
4822
22
            }
4823
154
            return false;
4824
176
          },
4825
76
          false /* skip top directive */);
4826
76
      if (DeadLock) {
4827
22
        SemaRef.Diag(StartLoc,
4828
22
                     diag::err_omp_prohibited_region_critical_same_name)
4829
22
            << CurrentName.getName();
4830
22
        if (PreviousCriticalLoc.isValid())
4831
22
          SemaRef.Diag(PreviousCriticalLoc,
4832
22
                       diag::note_omp_previous_critical_region);
4833
22
        return true;
4834
22
      }
4835
73.6k
    } else if (CurrentRegion == OMPD_barrier) {
4836
      // OpenMP 5.1 [2.22, Nesting of Regions]
4837
      // A barrier region may not be closely nested inside a worksharing, loop,
4838
      // task, taskloop, critical, ordered, atomic, or masked region.
4839
299
      NestingProhibited =
4840
299
          isOpenMPWorksharingDirective(ParentRegion) ||
4841
299
          
isOpenMPGenericLoopDirective(ParentRegion)179
||
4842
299
          
isOpenMPTaskingDirective(ParentRegion)178
||
4843
299
          
ParentRegion == OMPD_master154
||
ParentRegion == OMPD_masked142
||
4844
299
          
ParentRegion == OMPD_parallel_master142
||
4845
299
          
ParentRegion == OMPD_critical136
||
ParentRegion == OMPD_ordered124
;
4846
73.3k
    } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
4847
73.3k
               
!isOpenMPParallelDirective(CurrentRegion)26.6k
&&
4848
73.3k
               
!isOpenMPTeamsDirective(CurrentRegion)7.72k
) {
4849
      // OpenMP 5.1 [2.22, Nesting of Regions]
4850
      // A loop region that binds to a parallel region or a worksharing region
4851
      // may not be closely nested inside a worksharing, loop, task, taskloop,
4852
      // critical, ordered, atomic, or masked region.
4853
7.72k
      NestingProhibited =
4854
7.72k
          isOpenMPWorksharingDirective(ParentRegion) ||
4855
7.72k
          
isOpenMPGenericLoopDirective(ParentRegion)7.24k
||
4856
7.72k
          
isOpenMPTaskingDirective(ParentRegion)7.24k
||
4857
7.72k
          
ParentRegion == OMPD_master7.13k
||
ParentRegion == OMPD_masked7.08k
||
4858
7.72k
          
ParentRegion == OMPD_parallel_master7.08