Coverage Report

Created: 2022-05-14 11:35

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