Coverage Report

Created: 2020-02-15 09:57

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