Coverage Report

Created: 2021-08-24 07:12

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