Coverage Report

Created: 2022-07-16 07:03

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