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 "clang/Sema/SemaOpenMP.h"
15#include "clang/AST/ASTConsumer.h"
16
17#include "TreeTransform.h"
18#include "clang/AST/ASTContext.h"
19#include "clang/AST/ASTMutationListener.h"
20#include "clang/AST/CXXInheritance.h"
21#include "clang/AST/Decl.h"
22#include "clang/AST/DeclCXX.h"
23#include "clang/AST/DeclOpenMP.h"
24#include "clang/AST/DynamicRecursiveASTVisitor.h"
25#include "clang/AST/OpenMPClause.h"
26#include "clang/AST/StmtCXX.h"
27#include "clang/AST/StmtOpenMP.h"
28#include "clang/AST/StmtVisitor.h"
29#include "clang/Basic/DiagnosticSema.h"
30#include "clang/Basic/OpenMPKinds.h"
31#include "clang/Basic/PartialDiagnostic.h"
32#include "clang/Basic/TargetInfo.h"
33#include "clang/Sema/EnterExpressionEvaluationContext.h"
34#include "clang/Sema/Initialization.h"
35#include "clang/Sema/Lookup.h"
36#include "clang/Sema/ParsedAttr.h"
37#include "clang/Sema/Scope.h"
38#include "clang/Sema/ScopeInfo.h"
39#include "clang/Sema/Sema.h"
40#include "llvm/ADT/IndexedMap.h"
41#include "llvm/ADT/PointerEmbeddedInt.h"
42#include "llvm/ADT/STLExtras.h"
43#include "llvm/ADT/Sequence.h"
44#include "llvm/ADT/SetVector.h"
45#include "llvm/ADT/SmallSet.h"
46#include "llvm/ADT/StringExtras.h"
47#include "llvm/Frontend/OpenMP/OMPAssume.h"
48#include "llvm/Frontend/OpenMP/OMPConstants.h"
49#include "llvm/IR/Assumptions.h"
50#include <optional>
51
52using namespace clang;
53using namespace llvm::omp;
54
55//===----------------------------------------------------------------------===//
56// Stack of data-sharing attributes for variables
57//===----------------------------------------------------------------------===//
58
59static const Expr *checkMapClauseExpressionBase(
60 Sema &SemaRef, Expr *E,
61 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
62 OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, bool NoDiagnose);
63
64static std::string getOpenMPClauseNameForDiag(OpenMPClauseKind C);
65
66namespace {
67/// Default data sharing attributes, which can be applied to directive.
68enum DefaultDataSharingAttributes {
69 DSA_unspecified = 0, /// Data sharing attribute not specified.
70 DSA_none = 1 << 0, /// Default data sharing attribute 'none'.
71 DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'.
72 DSA_private = 1 << 2, /// Default data sharing attribute 'private'.
73 DSA_firstprivate = 1 << 3, /// Default data sharing attribute 'firstprivate'.
74};
75
76/// Variable Category attributes to restrict the modifier of the
77/// default clause (DefaultDataSharingAttributes)
78/// Not mentioning any Variable category attribute indicates
79/// the modifier (DefaultDataSharingAttributes) is for all variables.
80enum DefaultDataSharingVCAttributes {
81 DSA_VC_all = 0, /// for all variables.
82 DSA_VC_aggregate, /// for aggregate variables.
83 DSA_VC_pointer, /// for pointer variables.
84 DSA_VC_scalar, /// for scalar variables.
85};
86
87/// Stack for tracking declarations used in OpenMP directives and
88/// clauses and their data-sharing attributes.
89class DSAStackTy {
90public:
91 struct DSAVarData {
92 OpenMPDirectiveKind DKind = OMPD_unknown;
93 OpenMPClauseKind CKind = OMPC_unknown;
94 unsigned Modifier = 0;
95 const Expr *RefExpr = nullptr;
96 DeclRefExpr *PrivateCopy = nullptr;
97 SourceLocation ImplicitDSALoc;
98 bool AppliedToPointee = false;
99 DSAVarData() = default;
100 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
101 const Expr *RefExpr, DeclRefExpr *PrivateCopy,
102 SourceLocation ImplicitDSALoc, unsigned Modifier,
103 bool AppliedToPointee)
104 : DKind(DKind), CKind(CKind), Modifier(Modifier), RefExpr(RefExpr),
105 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc),
106 AppliedToPointee(AppliedToPointee) {}
107 };
108 using OperatorOffsetTy =
109 llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>;
110 using DoacrossClauseMapTy = llvm::DenseMap<OMPClause *, OperatorOffsetTy>;
111 /// Kind of the declaration used in the uses_allocators clauses.
112 enum class UsesAllocatorsDeclKind {
113 /// Predefined allocator
114 PredefinedAllocator,
115 /// User-defined allocator
116 UserDefinedAllocator,
117 /// The declaration that represent allocator trait
118 AllocatorTrait,
119 };
120
121private:
122 struct DSAInfo {
123 OpenMPClauseKind Attributes = OMPC_unknown;
124 unsigned Modifier = 0;
125 /// Pointer to a reference expression and a flag which shows that the
126 /// variable is marked as lastprivate(true) or not (false).
127 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr;
128 DeclRefExpr *PrivateCopy = nullptr;
129 /// true if the attribute is applied to the pointee, not the variable
130 /// itself.
131 bool AppliedToPointee = false;
132 };
133 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>;
134 using UsedRefMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>;
135 using LCDeclInfo = std::pair<unsigned, VarDecl *>;
136 using LoopControlVariablesMapTy =
137 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>;
138 /// Struct that associates a component with the clause kind where they are
139 /// found.
140 struct MappedExprComponentTy {
141 OMPClauseMappableExprCommon::MappableExprComponentLists Components;
142 OpenMPClauseKind Kind = OMPC_unknown;
143 };
144 using MappedExprComponentsTy =
145 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>;
146 using CriticalsWithHintsTy =
147 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>;
148 struct ReductionData {
149 using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>;
150 SourceRange ReductionRange;
151 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp;
152 ReductionData() = default;
153 void set(BinaryOperatorKind BO, SourceRange RR) {
154 ReductionRange = RR;
155 ReductionOp = BO;
156 }
157 void set(const Expr *RefExpr, SourceRange RR) {
158 ReductionRange = RR;
159 ReductionOp = RefExpr;
160 }
161 };
162 using DeclReductionMapTy =
163 llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>;
164 struct DefaultmapInfo {
165 OpenMPDefaultmapClauseModifier ImplicitBehavior =
166 OMPC_DEFAULTMAP_MODIFIER_unknown;
167 SourceLocation SLoc;
168 DefaultmapInfo() = default;
169 DefaultmapInfo(OpenMPDefaultmapClauseModifier M, SourceLocation Loc)
170 : ImplicitBehavior(M), SLoc(Loc) {}
171 };
172
173 struct SharingMapTy {
174 DeclSAMapTy SharingMap;
175 DeclReductionMapTy ReductionMap;
176 UsedRefMapTy AlignedMap;
177 UsedRefMapTy NontemporalMap;
178 MappedExprComponentsTy MappedExprComponents;
179 LoopControlVariablesMapTy LCVMap;
180 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
181 SourceLocation DefaultAttrLoc;
182 DefaultDataSharingVCAttributes DefaultVCAttr = DSA_VC_all;
183 SourceLocation DefaultAttrVCLoc;
184 DefaultmapInfo DefaultmapMap[OMPC_DEFAULTMAP_unknown + 1];
185 OpenMPDirectiveKind Directive = OMPD_unknown;
186 DeclarationNameInfo DirectiveName;
187 Scope *CurScope = nullptr;
188 DeclContext *Context = nullptr;
189 SourceLocation ConstructLoc;
190 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
191 /// get the data (loop counters etc.) about enclosing loop-based construct.
192 /// This data is required during codegen.
193 DoacrossClauseMapTy DoacrossDepends;
194 /// First argument (Expr *) contains optional argument of the
195 /// 'ordered' clause, the second one is true if the regions has 'ordered'
196 /// clause, false otherwise.
197 std::optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion;
198 bool RegionHasOrderConcurrent = false;
199 unsigned AssociatedLoops = 1;
200 bool HasMutipleLoops = false;
201 const Decl *PossiblyLoopCounter = nullptr;
202 bool NowaitRegion = false;
203 bool UntiedRegion = false;
204 bool CancelRegion = false;
205 bool LoopStart = false;
206 bool BodyComplete = false;
207 SourceLocation PrevScanLocation;
208 SourceLocation PrevOrderedLocation;
209 SourceLocation InnerTeamsRegionLoc;
210 /// Reference to the taskgroup task_reduction reference expression.
211 Expr *TaskgroupReductionRef = nullptr;
212 llvm::DenseSet<QualType> MappedClassesQualTypes;
213 SmallVector<Expr *, 4> InnerUsedAllocators;
214 llvm::DenseSet<CanonicalDeclPtr<Decl>> ImplicitTaskFirstprivates;
215 /// List of globals marked as declare target link in this target region
216 /// (isOpenMPTargetExecutionDirective(Directive) == true).
217 llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls;
218 /// List of decls used in inclusive/exclusive clauses of the scan directive.
219 llvm::DenseSet<CanonicalDeclPtr<Decl>> UsedInScanDirective;
220 llvm::DenseMap<CanonicalDeclPtr<const Decl>, UsesAllocatorsDeclKind>
221 UsesAllocatorsDecls;
222 /// Data is required on creating capture fields for implicit
223 /// default first|private clause.
224 struct ImplicitDefaultFDInfoTy {
225 /// Field decl.
226 const FieldDecl *FD = nullptr;
227 /// Nesting stack level
228 size_t StackLevel = 0;
229 /// Capture variable decl.
230 VarDecl *VD = nullptr;
231 ImplicitDefaultFDInfoTy(const FieldDecl *FD, size_t StackLevel,
232 VarDecl *VD)
233 : FD(FD), StackLevel(StackLevel), VD(VD) {}
234 };
235 /// List of captured fields
236 llvm::SmallVector<ImplicitDefaultFDInfoTy, 8>
237 ImplicitDefaultFirstprivateFDs;
238 Expr *DeclareMapperVar = nullptr;
239 SmallVector<VarDecl *, 16> IteratorVarDecls;
240 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
241 Scope *CurScope, SourceLocation Loc)
242 : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
243 ConstructLoc(Loc) {}
244 SharingMapTy() = default;
245 };
246
247 using StackTy = SmallVector<SharingMapTy, 4>;
248
249 /// Stack of used declaration and their data-sharing attributes.
250 DeclSAMapTy Threadprivates;
251 DeclSAMapTy Groupprivates;
252 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
253 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
254 /// true, if check for DSA must be from parent directive, false, if
255 /// from current directive.
256 OpenMPClauseKind ClauseKindMode = OMPC_unknown;
257 Sema &SemaRef;
258 bool ForceCapturing = false;
259 /// true if all the variables in the target executable directives must be
260 /// captured by reference.
261 bool ForceCaptureByReferenceInTargetExecutable = false;
262 CriticalsWithHintsTy Criticals;
263 unsigned IgnoredStackElements = 0;
264
265 /// Iterators over the stack iterate in order from innermost to outermost
266 /// directive.
267 using const_iterator = StackTy::const_reverse_iterator;
268 const_iterator begin() const {
269 return Stack.empty() ? const_iterator()
270 : Stack.back().first.rbegin() + IgnoredStackElements;
271 }
272 const_iterator end() const {
273 return Stack.empty() ? const_iterator() : Stack.back().first.rend();
274 }
275 using iterator = StackTy::reverse_iterator;
276 iterator begin() {
277 return Stack.empty() ? iterator()
278 : Stack.back().first.rbegin() + IgnoredStackElements;
279 }
280 iterator end() {
281 return Stack.empty() ? iterator() : Stack.back().first.rend();
282 }
283
284 // Convenience operations to get at the elements of the stack.
285
286 bool isStackEmpty() const {
287 return Stack.empty() ||
288 Stack.back().second != CurrentNonCapturingFunctionScope ||
289 Stack.back().first.size() <= IgnoredStackElements;
290 }
291 size_t getStackSize() const {
292 return isStackEmpty() ? 0
293 : Stack.back().first.size() - IgnoredStackElements;
294 }
295
296 SharingMapTy *getTopOfStackOrNull() {
297 size_t Size = getStackSize();
298 if (Size == 0)
299 return nullptr;
300 return &Stack.back().first[Size - 1];
301 }
302 const SharingMapTy *getTopOfStackOrNull() const {
303 return const_cast<DSAStackTy &>(*this).getTopOfStackOrNull();
304 }
305 SharingMapTy &getTopOfStack() {
306 assert(!isStackEmpty() && "no current directive");
307 return *getTopOfStackOrNull();
308 }
309 const SharingMapTy &getTopOfStack() const {
310 return const_cast<DSAStackTy &>(*this).getTopOfStack();
311 }
312
313 SharingMapTy *getSecondOnStackOrNull() {
314 size_t Size = getStackSize();
315 if (Size <= 1)
316 return nullptr;
317 return &Stack.back().first[Size - 2];
318 }
319 const SharingMapTy *getSecondOnStackOrNull() const {
320 return const_cast<DSAStackTy &>(*this).getSecondOnStackOrNull();
321 }
322
323 /// Get the stack element at a certain level (previously returned by
324 /// \c getNestingLevel).
325 ///
326 /// Note that nesting levels count from outermost to innermost, and this is
327 /// the reverse of our iteration order where new inner levels are pushed at
328 /// the front of the stack.
329 SharingMapTy &getStackElemAtLevel(unsigned Level) {
330 assert(Level < getStackSize() && "no such stack element");
331 return Stack.back().first[Level];
332 }
333 const SharingMapTy &getStackElemAtLevel(unsigned Level) const {
334 return const_cast<DSAStackTy &>(*this).getStackElemAtLevel(Level);
335 }
336
337 DSAVarData getDSA(const_iterator &Iter, ValueDecl *D) const;
338
339 /// Checks if the variable is a local for OpenMP region.
340 bool isOpenMPLocal(VarDecl *D, const_iterator Iter) const;
341
342 /// Vector of previously declared requires directives
343 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls;
344 /// omp_allocator_handle_t type.
345 QualType OMPAllocatorHandleT;
346 /// omp_depend_t type.
347 QualType OMPDependT;
348 /// omp_event_handle_t type.
349 QualType OMPEventHandleT;
350 /// omp_alloctrait_t type.
351 QualType OMPAlloctraitT;
352 /// Expression for the predefined allocators.
353 Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = {
354 nullptr};
355 /// Vector of previously encountered target directives
356 SmallVector<SourceLocation, 2> TargetLocations;
357 SourceLocation AtomicLocation;
358 /// Vector of declare variant construct traits.
359 SmallVector<llvm::omp::TraitProperty, 8> ConstructTraits;
360
361public:
362 explicit DSAStackTy(Sema &S) : SemaRef(S) {}
363
364 /// Sets omp_allocator_handle_t type.
365 void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; }
366 /// Gets omp_allocator_handle_t type.
367 QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; }
368 /// Sets omp_alloctrait_t type.
369 void setOMPAlloctraitT(QualType Ty) { OMPAlloctraitT = Ty; }
370 /// Gets omp_alloctrait_t type.
371 QualType getOMPAlloctraitT() const { return OMPAlloctraitT; }
372 /// Sets the given default allocator.
373 void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
374 Expr *Allocator) {
375 OMPPredefinedAllocators[AllocatorKind] = Allocator;
376 }
377 /// Returns the specified default allocator.
378 Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const {
379 return OMPPredefinedAllocators[AllocatorKind];
380 }
381 /// Sets omp_depend_t type.
382 void setOMPDependT(QualType Ty) { OMPDependT = Ty; }
383 /// Gets omp_depend_t type.
384 QualType getOMPDependT() const { return OMPDependT; }
385
386 /// Sets omp_event_handle_t type.
387 void setOMPEventHandleT(QualType Ty) { OMPEventHandleT = Ty; }
388 /// Gets omp_event_handle_t type.
389 QualType getOMPEventHandleT() const { return OMPEventHandleT; }
390
391 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
392 OpenMPClauseKind getClauseParsingMode() const {
393 assert(isClauseParsingMode() && "Must be in clause parsing mode.");
394 return ClauseKindMode;
395 }
396 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
397
398 bool isBodyComplete() const {
399 const SharingMapTy *Top = getTopOfStackOrNull();
400 return Top && Top->BodyComplete;
401 }
402 void setBodyComplete() { getTopOfStack().BodyComplete = true; }
403
404 bool isForceVarCapturing() const { return ForceCapturing; }
405 void setForceVarCapturing(bool V) { ForceCapturing = V; }
406
407 void setForceCaptureByReferenceInTargetExecutable(bool V) {
408 ForceCaptureByReferenceInTargetExecutable = V;
409 }
410 bool isForceCaptureByReferenceInTargetExecutable() const {
411 return ForceCaptureByReferenceInTargetExecutable;
412 }
413
414 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
415 Scope *CurScope, SourceLocation Loc) {
416 assert(!IgnoredStackElements &&
417 "cannot change stack while ignoring elements");
418 if (Stack.empty() ||
419 Stack.back().second != CurrentNonCapturingFunctionScope)
420 Stack.emplace_back(Args: StackTy(), Args&: CurrentNonCapturingFunctionScope);
421 Stack.back().first.emplace_back(Args&: DKind, Args: DirName, Args&: CurScope, Args&: Loc);
422 Stack.back().first.back().DefaultAttrLoc = Loc;
423 }
424
425 void pop() {
426 assert(!IgnoredStackElements &&
427 "cannot change stack while ignoring elements");
428 assert(!Stack.back().first.empty() &&
429 "Data-sharing attributes stack is empty!");
430 Stack.back().first.pop_back();
431 }
432
433 /// RAII object to temporarily leave the scope of a directive when we want to
434 /// logically operate in its parent.
435 class ParentDirectiveScope {
436 DSAStackTy &Self;
437 bool Active;
438
439 public:
440 ParentDirectiveScope(DSAStackTy &Self, bool Activate)
441 : Self(Self), Active(false) {
442 if (Activate)
443 enable();
444 }
445 ~ParentDirectiveScope() { disable(); }
446 void disable() {
447 if (Active) {
448 --Self.IgnoredStackElements;
449 Active = false;
450 }
451 }
452 void enable() {
453 if (!Active) {
454 ++Self.IgnoredStackElements;
455 Active = true;
456 }
457 }
458 };
459
460 /// Marks that we're started loop parsing.
461 void loopInit() {
462 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
463 "Expected loop-based directive.");
464 getTopOfStack().LoopStart = true;
465 }
466 /// Start capturing of the variables in the loop context.
467 void loopStart() {
468 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
469 "Expected loop-based directive.");
470 getTopOfStack().LoopStart = false;
471 }
472 /// true, if variables are captured, false otherwise.
473 bool isLoopStarted() const {
474 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
475 "Expected loop-based directive.");
476 return !getTopOfStack().LoopStart;
477 }
478 /// Marks (or clears) declaration as possibly loop counter.
479 void resetPossibleLoopCounter(const Decl *D = nullptr) {
480 getTopOfStack().PossiblyLoopCounter = D ? D->getCanonicalDecl() : D;
481 }
482 /// Gets the possible loop counter decl.
483 const Decl *getPossiblyLoopCounter() const {
484 return getTopOfStack().PossiblyLoopCounter;
485 }
486 /// Start new OpenMP region stack in new non-capturing function.
487 void pushFunction() {
488 assert(!IgnoredStackElements &&
489 "cannot change stack while ignoring elements");
490 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
491 assert(!isa<CapturingScopeInfo>(CurFnScope));
492 CurrentNonCapturingFunctionScope = CurFnScope;
493 }
494 /// Pop region stack for non-capturing function.
495 void popFunction(const FunctionScopeInfo *OldFSI) {
496 assert(!IgnoredStackElements &&
497 "cannot change stack while ignoring elements");
498 if (!Stack.empty() && Stack.back().second == OldFSI) {
499 assert(Stack.back().first.empty());
500 Stack.pop_back();
501 }
502 CurrentNonCapturingFunctionScope = nullptr;
503 for (const FunctionScopeInfo *FSI : llvm::reverse(C&: SemaRef.FunctionScopes)) {
504 if (!isa<CapturingScopeInfo>(Val: FSI)) {
505 CurrentNonCapturingFunctionScope = FSI;
506 break;
507 }
508 }
509 }
510
511 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) {
512 Criticals.try_emplace(Key: D->getDirectiveName().getAsString(), Args&: D, Args&: Hint);
513 }
514 std::pair<const OMPCriticalDirective *, llvm::APSInt>
515 getCriticalWithHint(const DeclarationNameInfo &Name) const {
516 auto I = Criticals.find(Key: Name.getAsString());
517 if (I != Criticals.end())
518 return I->second;
519 return std::make_pair(x: nullptr, y: llvm::APSInt());
520 }
521 /// If 'aligned' declaration for given variable \a D was not seen yet,
522 /// add it and return NULL; otherwise return previous occurrence's expression
523 /// for diagnostics.
524 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE);
525 /// If 'nontemporal' declaration for given variable \a D was not seen yet,
526 /// add it and return NULL; otherwise return previous occurrence's expression
527 /// for diagnostics.
528 const Expr *addUniqueNontemporal(const ValueDecl *D, const Expr *NewDE);
529
530 /// Register specified variable as loop control variable.
531 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture);
532 /// Check if the specified variable is a loop control variable for
533 /// current region.
534 /// \return The index of the loop control variable in the list of associated
535 /// for-loops (from outer to inner).
536 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const;
537 /// Check if the specified variable is a loop control variable for
538 /// parent region.
539 /// \return The index of the loop control variable in the list of associated
540 /// for-loops (from outer to inner).
541 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const;
542 /// Check if the specified variable is a loop control variable for
543 /// current region.
544 /// \return The index of the loop control variable in the list of associated
545 /// for-loops (from outer to inner).
546 const LCDeclInfo isLoopControlVariable(const ValueDecl *D,
547 unsigned Level) const;
548 /// Get the loop control variable for the I-th loop (or nullptr) in
549 /// parent directive.
550 const ValueDecl *getParentLoopControlVariable(unsigned I) const;
551
552 /// Marks the specified decl \p D as used in scan directive.
553 void markDeclAsUsedInScanDirective(ValueDecl *D) {
554 if (SharingMapTy *Stack = getSecondOnStackOrNull())
555 Stack->UsedInScanDirective.insert(V: D);
556 }
557
558 /// Checks if the specified declaration was used in the inner scan directive.
559 bool isUsedInScanDirective(ValueDecl *D) const {
560 if (const SharingMapTy *Stack = getTopOfStackOrNull())
561 return Stack->UsedInScanDirective.contains(V: D);
562 return false;
563 }
564
565 /// Adds explicit data sharing attribute to the specified declaration.
566 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
567 DeclRefExpr *PrivateCopy = nullptr, unsigned Modifier = 0,
568 bool AppliedToPointee = false);
569
570 /// Adds additional information for the reduction items with the reduction id
571 /// represented as an operator.
572 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
573 BinaryOperatorKind BOK);
574 /// Adds additional information for the reduction items with the reduction id
575 /// represented as reduction identifier.
576 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
577 const Expr *ReductionRef);
578 /// Returns the location and reduction operation from the innermost parent
579 /// region for the given \p D.
580 const DSAVarData
581 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
582 BinaryOperatorKind &BOK,
583 Expr *&TaskgroupDescriptor) const;
584 /// Returns the location and reduction operation from the innermost parent
585 /// region for the given \p D.
586 const DSAVarData
587 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
588 const Expr *&ReductionRef,
589 Expr *&TaskgroupDescriptor) const;
590 /// Return reduction reference expression for the current taskgroup or
591 /// parallel/worksharing directives with task reductions.
592 Expr *getTaskgroupReductionRef() const {
593 assert((getTopOfStack().Directive == OMPD_taskgroup ||
594 ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
595 isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
596 !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
597 "taskgroup reference expression requested for non taskgroup or "
598 "parallel/worksharing directive.");
599 return getTopOfStack().TaskgroupReductionRef;
600 }
601 /// Checks if the given \p VD declaration is actually a taskgroup reduction
602 /// descriptor variable at the \p Level of OpenMP regions.
603 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const {
604 return getStackElemAtLevel(Level).TaskgroupReductionRef &&
605 cast<DeclRefExpr>(Val: getStackElemAtLevel(Level).TaskgroupReductionRef)
606 ->getDecl() == VD;
607 }
608
609 /// Returns data sharing attributes from top of the stack for the
610 /// specified declaration.
611 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
612 /// Returns data-sharing attributes for the specified declaration.
613 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const;
614 /// Returns data-sharing attributes for the specified declaration.
615 const DSAVarData getImplicitDSA(ValueDecl *D, unsigned Level) const;
616 /// Checks if the specified variables has data-sharing attributes which
617 /// match specified \a CPred predicate in any directive which matches \a DPred
618 /// predicate.
619 const DSAVarData
620 hasDSA(ValueDecl *D,
621 const llvm::function_ref<bool(OpenMPClauseKind, bool,
622 DefaultDataSharingAttributes)>
623 CPred,
624 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
625 bool FromParent) const;
626 /// Checks if the specified variables has data-sharing attributes which
627 /// match specified \a CPred predicate in any innermost directive which
628 /// matches \a DPred predicate.
629 const DSAVarData
630 hasInnermostDSA(ValueDecl *D,
631 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
632 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
633 bool FromParent) const;
634 /// Checks if the specified variables has explicit data-sharing
635 /// attributes which match specified \a CPred predicate at the specified
636 /// OpenMP region.
637 bool
638 hasExplicitDSA(const ValueDecl *D,
639 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
640 unsigned Level, bool NotLastprivate = false) const;
641
642 /// Returns true if the directive at level \Level matches in the
643 /// specified \a DPred predicate.
644 bool hasExplicitDirective(
645 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
646 unsigned Level) const;
647
648 /// Finds a directive which matches specified \a DPred predicate.
649 bool hasDirective(
650 const llvm::function_ref<bool(
651 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)>
652 DPred,
653 bool FromParent) const;
654
655 /// Returns currently analyzed directive.
656 OpenMPDirectiveKind getCurrentDirective() const {
657 const SharingMapTy *Top = getTopOfStackOrNull();
658 return Top ? Top->Directive : OMPD_unknown;
659 }
660 /// Returns directive kind at specified level.
661 OpenMPDirectiveKind getDirective(unsigned Level) const {
662 assert(!isStackEmpty() && "No directive at specified level.");
663 return getStackElemAtLevel(Level).Directive;
664 }
665 /// Returns the capture region at the specified level.
666 OpenMPDirectiveKind getCaptureRegion(unsigned Level,
667 unsigned OpenMPCaptureLevel) const {
668 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
669 getOpenMPCaptureRegions(CaptureRegions, DKind: getDirective(Level));
670 return CaptureRegions[OpenMPCaptureLevel];
671 }
672 /// Returns parent directive.
673 OpenMPDirectiveKind getParentDirective() const {
674 const SharingMapTy *Parent = getSecondOnStackOrNull();
675 return Parent ? Parent->Directive : OMPD_unknown;
676 }
677
678 /// Add requires decl to internal vector
679 void addRequiresDecl(OMPRequiresDecl *RD) { RequiresDecls.push_back(Elt: RD); }
680
681 /// Checks if the defined 'requires' directive has specified type of clause.
682 template <typename ClauseType> bool hasRequiresDeclWithClause() const {
683 return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
684 return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
685 return isa<ClauseType>(C);
686 });
687 });
688 }
689
690 /// Checks for a duplicate clause amongst previously declared requires
691 /// directives
692 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const {
693 bool IsDuplicate = false;
694 for (OMPClause *CNew : ClauseList) {
695 for (const OMPRequiresDecl *D : RequiresDecls) {
696 for (const OMPClause *CPrev : D->clauselists()) {
697 if (CNew->getClauseKind() == CPrev->getClauseKind()) {
698 SemaRef.Diag(Loc: CNew->getBeginLoc(),
699 DiagID: diag::err_omp_requires_clause_redeclaration)
700 << getOpenMPClauseNameForDiag(C: CNew->getClauseKind());
701 SemaRef.Diag(Loc: CPrev->getBeginLoc(),
702 DiagID: diag::note_omp_requires_previous_clause)
703 << getOpenMPClauseNameForDiag(C: CPrev->getClauseKind());
704 IsDuplicate = true;
705 }
706 }
707 }
708 }
709 return IsDuplicate;
710 }
711
712 /// Add location of previously encountered target to internal vector
713 void addTargetDirLocation(SourceLocation LocStart) {
714 TargetLocations.push_back(Elt: LocStart);
715 }
716
717 /// Add location for the first encountered atomic directive.
718 void addAtomicDirectiveLoc(SourceLocation Loc) {
719 if (AtomicLocation.isInvalid())
720 AtomicLocation = Loc;
721 }
722
723 /// Returns the location of the first encountered atomic directive in the
724 /// module.
725 SourceLocation getAtomicDirectiveLoc() const { return AtomicLocation; }
726
727 // Return previously encountered target region locations.
728 ArrayRef<SourceLocation> getEncounteredTargetLocs() const {
729 return TargetLocations;
730 }
731
732 /// Set default data sharing attribute to none.
733 void setDefaultDSANone(SourceLocation Loc) {
734 getTopOfStack().DefaultAttr = DSA_none;
735 getTopOfStack().DefaultAttrLoc = Loc;
736 }
737 /// Set default data sharing attribute to shared.
738 void setDefaultDSAShared(SourceLocation Loc) {
739 getTopOfStack().DefaultAttr = DSA_shared;
740 getTopOfStack().DefaultAttrLoc = Loc;
741 }
742 /// Set default data sharing attribute to private.
743 void setDefaultDSAPrivate(SourceLocation Loc) {
744 getTopOfStack().DefaultAttr = DSA_private;
745 getTopOfStack().DefaultAttrLoc = Loc;
746 }
747 /// Set default data sharing attribute to firstprivate.
748 void setDefaultDSAFirstPrivate(SourceLocation Loc) {
749 getTopOfStack().DefaultAttr = DSA_firstprivate;
750 getTopOfStack().DefaultAttrLoc = Loc;
751 }
752 /// Set default data sharing variable category attribute to aggregate.
753 void setDefaultDSAVCAggregate(SourceLocation VCLoc) {
754 getTopOfStack().DefaultVCAttr = DSA_VC_aggregate;
755 getTopOfStack().DefaultAttrVCLoc = VCLoc;
756 }
757 /// Set default data sharing variable category attribute to all.
758 void setDefaultDSAVCAll(SourceLocation VCLoc) {
759 getTopOfStack().DefaultVCAttr = DSA_VC_all;
760 getTopOfStack().DefaultAttrVCLoc = VCLoc;
761 }
762 /// Set default data sharing variable category attribute to pointer.
763 void setDefaultDSAVCPointer(SourceLocation VCLoc) {
764 getTopOfStack().DefaultVCAttr = DSA_VC_pointer;
765 getTopOfStack().DefaultAttrVCLoc = VCLoc;
766 }
767 /// Set default data sharing variable category attribute to scalar.
768 void setDefaultDSAVCScalar(SourceLocation VCLoc) {
769 getTopOfStack().DefaultVCAttr = DSA_VC_scalar;
770 getTopOfStack().DefaultAttrVCLoc = VCLoc;
771 }
772 /// Set default data mapping attribute to Modifier:Kind
773 void setDefaultDMAAttr(OpenMPDefaultmapClauseModifier M,
774 OpenMPDefaultmapClauseKind Kind, SourceLocation Loc) {
775 DefaultmapInfo &DMI = getTopOfStack().DefaultmapMap[Kind];
776 DMI.ImplicitBehavior = M;
777 DMI.SLoc = Loc;
778 }
779 /// Check whether the implicit-behavior has been set in defaultmap
780 bool checkDefaultmapCategory(OpenMPDefaultmapClauseKind VariableCategory) {
781 if (VariableCategory == OMPC_DEFAULTMAP_unknown)
782 return getTopOfStack()
783 .DefaultmapMap[OMPC_DEFAULTMAP_aggregate]
784 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown ||
785 getTopOfStack()
786 .DefaultmapMap[OMPC_DEFAULTMAP_scalar]
787 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown ||
788 getTopOfStack()
789 .DefaultmapMap[OMPC_DEFAULTMAP_pointer]
790 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown;
791 return getTopOfStack().DefaultmapMap[VariableCategory].ImplicitBehavior !=
792 OMPC_DEFAULTMAP_MODIFIER_unknown;
793 }
794
795 ArrayRef<llvm::omp::TraitProperty> getConstructTraits() {
796 return ConstructTraits;
797 }
798 void handleConstructTrait(ArrayRef<llvm::omp::TraitProperty> Traits,
799 bool ScopeEntry) {
800 if (ScopeEntry)
801 ConstructTraits.append(in_start: Traits.begin(), in_end: Traits.end());
802 else
803 for (llvm::omp::TraitProperty Trait : llvm::reverse(C&: Traits)) {
804 llvm::omp::TraitProperty Top = ConstructTraits.pop_back_val();
805 assert(Top == Trait && "Something left a trait on the stack!");
806 (void)Trait;
807 (void)Top;
808 }
809 }
810
811 DefaultDataSharingAttributes getDefaultDSA(unsigned Level) const {
812 return getStackSize() <= Level ? DSA_unspecified
813 : getStackElemAtLevel(Level).DefaultAttr;
814 }
815 DefaultDataSharingAttributes getDefaultDSA() const {
816 return isStackEmpty() ? DSA_unspecified : getTopOfStack().DefaultAttr;
817 }
818 SourceLocation getDefaultDSALocation() const {
819 return isStackEmpty() ? SourceLocation() : getTopOfStack().DefaultAttrLoc;
820 }
821 OpenMPDefaultmapClauseModifier
822 getDefaultmapModifier(OpenMPDefaultmapClauseKind Kind) const {
823 return isStackEmpty()
824 ? OMPC_DEFAULTMAP_MODIFIER_unknown
825 : getTopOfStack().DefaultmapMap[Kind].ImplicitBehavior;
826 }
827 OpenMPDefaultmapClauseModifier
828 getDefaultmapModifierAtLevel(unsigned Level,
829 OpenMPDefaultmapClauseKind Kind) const {
830 return getStackElemAtLevel(Level).DefaultmapMap[Kind].ImplicitBehavior;
831 }
832 bool isDefaultmapCapturedByRef(unsigned Level,
833 OpenMPDefaultmapClauseKind Kind) const {
834 OpenMPDefaultmapClauseModifier M =
835 getDefaultmapModifierAtLevel(Level, Kind);
836 if (Kind == OMPC_DEFAULTMAP_scalar || Kind == OMPC_DEFAULTMAP_pointer) {
837 return (M == OMPC_DEFAULTMAP_MODIFIER_alloc) ||
838 (M == OMPC_DEFAULTMAP_MODIFIER_to) ||
839 (M == OMPC_DEFAULTMAP_MODIFIER_from) ||
840 (M == OMPC_DEFAULTMAP_MODIFIER_tofrom) ||
841 (M == OMPC_DEFAULTMAP_MODIFIER_present) ||
842 (M == OMPC_DEFAULTMAP_MODIFIER_storage);
843 }
844 return true;
845 }
846 static bool mustBeFirstprivateBase(OpenMPDefaultmapClauseModifier M,
847 OpenMPDefaultmapClauseKind Kind) {
848 switch (Kind) {
849 case OMPC_DEFAULTMAP_scalar:
850 case OMPC_DEFAULTMAP_pointer:
851 return (M == OMPC_DEFAULTMAP_MODIFIER_unknown) ||
852 (M == OMPC_DEFAULTMAP_MODIFIER_firstprivate) ||
853 (M == OMPC_DEFAULTMAP_MODIFIER_default);
854 case OMPC_DEFAULTMAP_aggregate:
855 return M == OMPC_DEFAULTMAP_MODIFIER_firstprivate;
856 default:
857 break;
858 }
859 llvm_unreachable("Unexpected OpenMPDefaultmapClauseKind enum");
860 }
861 bool mustBeFirstprivateAtLevel(unsigned Level,
862 OpenMPDefaultmapClauseKind Kind) const {
863 OpenMPDefaultmapClauseModifier M =
864 getDefaultmapModifierAtLevel(Level, Kind);
865 return mustBeFirstprivateBase(M, Kind);
866 }
867 bool mustBeFirstprivate(OpenMPDefaultmapClauseKind Kind) const {
868 OpenMPDefaultmapClauseModifier M = getDefaultmapModifier(Kind);
869 return mustBeFirstprivateBase(M, Kind);
870 }
871
872 /// Checks if the specified variable is a threadprivate.
873 bool isThreadPrivate(VarDecl *D) {
874 const DSAVarData DVar = getTopDSA(D, FromParent: false);
875 return isOpenMPThreadPrivate(Kind: DVar.CKind);
876 }
877
878 /// Marks current region as ordered (it has an 'ordered' clause).
879 void setOrderedRegion(bool IsOrdered, const Expr *Param,
880 OMPOrderedClause *Clause) {
881 if (IsOrdered)
882 getTopOfStack().OrderedRegion.emplace(args&: Param, args&: Clause);
883 else
884 getTopOfStack().OrderedRegion.reset();
885 }
886 /// Returns true, if region is ordered (has associated 'ordered' clause),
887 /// false - otherwise.
888 bool isOrderedRegion() const {
889 if (const SharingMapTy *Top = getTopOfStackOrNull())
890 return Top->OrderedRegion.has_value();
891 return false;
892 }
893 /// Returns optional parameter for the ordered region.
894 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const {
895 if (const SharingMapTy *Top = getTopOfStackOrNull())
896 if (Top->OrderedRegion)
897 return *Top->OrderedRegion;
898 return std::make_pair(x: nullptr, y: nullptr);
899 }
900 /// Returns true, if parent region is ordered (has associated
901 /// 'ordered' clause), false - otherwise.
902 bool isParentOrderedRegion() const {
903 if (const SharingMapTy *Parent = getSecondOnStackOrNull())
904 return Parent->OrderedRegion.has_value();
905 return false;
906 }
907 /// Returns optional parameter for the ordered region.
908 std::pair<const Expr *, OMPOrderedClause *>
909 getParentOrderedRegionParam() const {
910 if (const SharingMapTy *Parent = getSecondOnStackOrNull())
911 if (Parent->OrderedRegion)
912 return *Parent->OrderedRegion;
913 return std::make_pair(x: nullptr, y: nullptr);
914 }
915 /// Marks current region as having an 'order' clause.
916 void setRegionHasOrderConcurrent(bool HasOrderConcurrent) {
917 getTopOfStack().RegionHasOrderConcurrent = HasOrderConcurrent;
918 }
919 /// Returns true, if parent region is order (has associated
920 /// 'order' clause), false - otherwise.
921 bool isParentOrderConcurrent() const {
922 if (const SharingMapTy *Parent = getSecondOnStackOrNull())
923 return Parent->RegionHasOrderConcurrent;
924 return false;
925 }
926 /// Marks current region as nowait (it has a 'nowait' clause).
927 void setNowaitRegion(bool IsNowait = true) {
928 getTopOfStack().NowaitRegion = IsNowait;
929 }
930 /// Returns true, if parent region is nowait (has associated
931 /// 'nowait' clause), false - otherwise.
932 bool isParentNowaitRegion() const {
933 if (const SharingMapTy *Parent = getSecondOnStackOrNull())
934 return Parent->NowaitRegion;
935 return false;
936 }
937 /// Marks current region as untied (it has a 'untied' clause).
938 void setUntiedRegion(bool IsUntied = true) {
939 getTopOfStack().UntiedRegion = IsUntied;
940 }
941 /// Return true if current region is untied.
942 bool isUntiedRegion() const {
943 const SharingMapTy *Top = getTopOfStackOrNull();
944 return Top ? Top->UntiedRegion : false;
945 }
946 /// Marks parent region as cancel region.
947 void setParentCancelRegion(bool Cancel = true) {
948 if (SharingMapTy *Parent = getSecondOnStackOrNull())
949 Parent->CancelRegion |= Cancel;
950 }
951 /// Return true if current region has inner cancel construct.
952 bool isCancelRegion() const {
953 const SharingMapTy *Top = getTopOfStackOrNull();
954 return Top ? Top->CancelRegion : false;
955 }
956
957 /// Mark that parent region already has scan directive.
958 void setParentHasScanDirective(SourceLocation Loc) {
959 if (SharingMapTy *Parent = getSecondOnStackOrNull())
960 Parent->PrevScanLocation = Loc;
961 }
962 /// Return true if current region has inner cancel construct.
963 bool doesParentHasScanDirective() const {
964 const SharingMapTy *Top = getSecondOnStackOrNull();
965 return Top ? Top->PrevScanLocation.isValid() : false;
966 }
967 /// Return true if current region has inner cancel construct.
968 SourceLocation getParentScanDirectiveLoc() const {
969 const SharingMapTy *Top = getSecondOnStackOrNull();
970 return Top ? Top->PrevScanLocation : SourceLocation();
971 }
972 /// Mark that parent region already has ordered directive.
973 void setParentHasOrderedDirective(SourceLocation Loc) {
974 if (SharingMapTy *Parent = getSecondOnStackOrNull())
975 Parent->PrevOrderedLocation = Loc;
976 }
977 /// Return true if current region has inner ordered construct.
978 bool doesParentHasOrderedDirective() const {
979 const SharingMapTy *Top = getSecondOnStackOrNull();
980 return Top ? Top->PrevOrderedLocation.isValid() : false;
981 }
982 /// Returns the location of the previously specified ordered directive.
983 SourceLocation getParentOrderedDirectiveLoc() const {
984 const SharingMapTy *Top = getSecondOnStackOrNull();
985 return Top ? Top->PrevOrderedLocation : SourceLocation();
986 }
987
988 /// Set collapse value for the region.
989 void setAssociatedLoops(unsigned Val) {
990 getTopOfStack().AssociatedLoops = Val;
991 if (Val > 1)
992 getTopOfStack().HasMutipleLoops = true;
993 }
994 /// Return collapse value for region.
995 unsigned getAssociatedLoops() const {
996 const SharingMapTy *Top = getTopOfStackOrNull();
997 return Top ? Top->AssociatedLoops : 0;
998 }
999 /// Returns true if the construct is associated with multiple loops.
1000 bool hasMutipleLoops() const {
1001 const SharingMapTy *Top = getTopOfStackOrNull();
1002 return Top ? Top->HasMutipleLoops : false;
1003 }
1004
1005 /// Marks current target region as one with closely nested teams
1006 /// region.
1007 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
1008 if (SharingMapTy *Parent = getSecondOnStackOrNull())
1009 Parent->InnerTeamsRegionLoc = TeamsRegionLoc;
1010 }
1011 /// Returns true, if current region has closely nested teams region.
1012 bool hasInnerTeamsRegion() const {
1013 return getInnerTeamsRegionLoc().isValid();
1014 }
1015 /// Returns location of the nested teams region (if any).
1016 SourceLocation getInnerTeamsRegionLoc() const {
1017 const SharingMapTy *Top = getTopOfStackOrNull();
1018 return Top ? Top->InnerTeamsRegionLoc : SourceLocation();
1019 }
1020
1021 Scope *getCurScope() const {
1022 const SharingMapTy *Top = getTopOfStackOrNull();
1023 return Top ? Top->CurScope : nullptr;
1024 }
1025 void setContext(DeclContext *DC) { getTopOfStack().Context = DC; }
1026 SourceLocation getConstructLoc() const {
1027 const SharingMapTy *Top = getTopOfStackOrNull();
1028 return Top ? Top->ConstructLoc : SourceLocation();
1029 }
1030
1031 /// Do the check specified in \a Check to all component lists and return true
1032 /// if any issue is found.
1033 bool checkMappableExprComponentListsForDecl(
1034 const ValueDecl *VD, bool CurrentRegionOnly,
1035 const llvm::function_ref<
1036 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
1037 OpenMPClauseKind)>
1038 Check) const {
1039 if (isStackEmpty())
1040 return false;
1041 auto SI = begin();
1042 auto SE = end();
1043
1044 if (SI == SE)
1045 return false;
1046
1047 if (CurrentRegionOnly)
1048 SE = std::next(x: SI);
1049 else
1050 std::advance(i&: SI, n: 1);
1051
1052 for (; SI != SE; ++SI) {
1053 auto MI = SI->MappedExprComponents.find(Val: VD);
1054 if (MI != SI->MappedExprComponents.end())
1055 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
1056 MI->second.Components)
1057 if (Check(L, MI->second.Kind))
1058 return true;
1059 }
1060 return false;
1061 }
1062
1063 /// Do the check specified in \a Check to all component lists at a given level
1064 /// and return true if any issue is found.
1065 bool checkMappableExprComponentListsForDeclAtLevel(
1066 const ValueDecl *VD, unsigned Level,
1067 const llvm::function_ref<
1068 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
1069 OpenMPClauseKind)>
1070 Check) const {
1071 if (getStackSize() <= Level)
1072 return false;
1073
1074 const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1075 auto MI = StackElem.MappedExprComponents.find(Val: VD);
1076 if (MI != StackElem.MappedExprComponents.end())
1077 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
1078 MI->second.Components)
1079 if (Check(L, MI->second.Kind))
1080 return true;
1081 return false;
1082 }
1083
1084 /// Create a new mappable expression component list associated with a given
1085 /// declaration and initialize it with the provided list of components.
1086 void addMappableExpressionComponents(
1087 const ValueDecl *VD,
1088 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
1089 OpenMPClauseKind WhereFoundClauseKind) {
1090 MappedExprComponentTy &MEC = getTopOfStack().MappedExprComponents[VD];
1091 // Create new entry and append the new components there.
1092 MEC.Components.resize(N: MEC.Components.size() + 1);
1093 MEC.Components.back().append(in_start: Components.begin(), in_end: Components.end());
1094 MEC.Kind = WhereFoundClauseKind;
1095 }
1096
1097 unsigned getNestingLevel() const {
1098 assert(!isStackEmpty());
1099 return getStackSize() - 1;
1100 }
1101 void addDoacrossDependClause(OMPClause *C, const OperatorOffsetTy &OpsOffs) {
1102 SharingMapTy *Parent = getSecondOnStackOrNull();
1103 assert(Parent && isOpenMPWorksharingDirective(Parent->Directive));
1104 Parent->DoacrossDepends.try_emplace(Key: C, Args: OpsOffs);
1105 }
1106 llvm::iterator_range<DoacrossClauseMapTy::const_iterator>
1107 getDoacrossDependClauses() const {
1108 const SharingMapTy &StackElem = getTopOfStack();
1109 if (isOpenMPWorksharingDirective(DKind: StackElem.Directive)) {
1110 const DoacrossClauseMapTy &Ref = StackElem.DoacrossDepends;
1111 return llvm::make_range(x: Ref.begin(), y: Ref.end());
1112 }
1113 return llvm::make_range(x: StackElem.DoacrossDepends.end(),
1114 y: StackElem.DoacrossDepends.end());
1115 }
1116
1117 // Store types of classes which have been explicitly mapped
1118 void addMappedClassesQualTypes(QualType QT) {
1119 SharingMapTy &StackElem = getTopOfStack();
1120 StackElem.MappedClassesQualTypes.insert(V: QT);
1121 }
1122
1123 // Return set of mapped classes types
1124 bool isClassPreviouslyMapped(QualType QT) const {
1125 const SharingMapTy &StackElem = getTopOfStack();
1126 return StackElem.MappedClassesQualTypes.contains(V: QT);
1127 }
1128
1129 /// Adds global declare target to the parent target region.
1130 void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) {
1131 assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
1132 E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link &&
1133 "Expected declare target link global.");
1134 for (auto &Elem : *this) {
1135 if (isOpenMPTargetExecutionDirective(DKind: Elem.Directive)) {
1136 Elem.DeclareTargetLinkVarDecls.push_back(Elt: E);
1137 return;
1138 }
1139 }
1140 }
1141
1142 /// Returns the list of globals with declare target link if current directive
1143 /// is target.
1144 ArrayRef<DeclRefExpr *> getLinkGlobals() const {
1145 assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) &&
1146 "Expected target executable directive.");
1147 return getTopOfStack().DeclareTargetLinkVarDecls;
1148 }
1149
1150 /// Adds list of allocators expressions.
1151 void addInnerAllocatorExpr(Expr *E) {
1152 getTopOfStack().InnerUsedAllocators.push_back(Elt: E);
1153 }
1154 /// Return list of used allocators.
1155 ArrayRef<Expr *> getInnerAllocators() const {
1156 return getTopOfStack().InnerUsedAllocators;
1157 }
1158 /// Marks the declaration as implicitly firstprivate nin the task-based
1159 /// regions.
1160 void addImplicitTaskFirstprivate(unsigned Level, Decl *D) {
1161 getStackElemAtLevel(Level).ImplicitTaskFirstprivates.insert(V: D);
1162 }
1163 /// Checks if the decl is implicitly firstprivate in the task-based region.
1164 bool isImplicitTaskFirstprivate(Decl *D) const {
1165 return getTopOfStack().ImplicitTaskFirstprivates.contains(V: D);
1166 }
1167
1168 /// Marks decl as used in uses_allocators clause as the allocator.
1169 void addUsesAllocatorsDecl(const Decl *D, UsesAllocatorsDeclKind Kind) {
1170 getTopOfStack().UsesAllocatorsDecls.try_emplace(Key: D, Args&: Kind);
1171 }
1172 /// Checks if specified decl is used in uses allocator clause as the
1173 /// allocator.
1174 std::optional<UsesAllocatorsDeclKind>
1175 isUsesAllocatorsDecl(unsigned Level, const Decl *D) const {
1176 const SharingMapTy &StackElem = getTopOfStack();
1177 auto I = StackElem.UsesAllocatorsDecls.find(Val: D);
1178 if (I == StackElem.UsesAllocatorsDecls.end())
1179 return std::nullopt;
1180 return I->getSecond();
1181 }
1182 std::optional<UsesAllocatorsDeclKind>
1183 isUsesAllocatorsDecl(const Decl *D) const {
1184 const SharingMapTy &StackElem = getTopOfStack();
1185 auto I = StackElem.UsesAllocatorsDecls.find(Val: D);
1186 if (I == StackElem.UsesAllocatorsDecls.end())
1187 return std::nullopt;
1188 return I->getSecond();
1189 }
1190
1191 void addDeclareMapperVarRef(Expr *Ref) {
1192 SharingMapTy &StackElem = getTopOfStack();
1193 StackElem.DeclareMapperVar = Ref;
1194 }
1195 const Expr *getDeclareMapperVarRef() const {
1196 const SharingMapTy *Top = getTopOfStackOrNull();
1197 return Top ? Top->DeclareMapperVar : nullptr;
1198 }
1199
1200 /// Add a new iterator variable.
1201 void addIteratorVarDecl(VarDecl *VD) {
1202 SharingMapTy &StackElem = getTopOfStack();
1203 StackElem.IteratorVarDecls.push_back(Elt: VD->getCanonicalDecl());
1204 }
1205 /// Check if variable declaration is an iterator VarDecl.
1206 bool isIteratorVarDecl(const VarDecl *VD) const {
1207 const SharingMapTy *Top = getTopOfStackOrNull();
1208 if (!Top)
1209 return false;
1210
1211 return llvm::is_contained(Range: Top->IteratorVarDecls, Element: VD->getCanonicalDecl());
1212 }
1213 /// get captured field from ImplicitDefaultFirstprivateFDs
1214 VarDecl *getImplicitFDCapExprDecl(const FieldDecl *FD) const {
1215 const_iterator I = begin();
1216 const_iterator EndI = end();
1217 size_t StackLevel = getStackSize();
1218 for (; I != EndI; ++I) {
1219 if (I->DefaultAttr == DSA_firstprivate || I->DefaultAttr == DSA_private)
1220 break;
1221 StackLevel--;
1222 }
1223 assert((StackLevel > 0 && I != EndI) || (StackLevel == 0 && I == EndI));
1224 if (I == EndI)
1225 return nullptr;
1226 for (const auto &IFD : I->ImplicitDefaultFirstprivateFDs)
1227 if (IFD.FD == FD && IFD.StackLevel == StackLevel)
1228 return IFD.VD;
1229 return nullptr;
1230 }
1231 /// Check if capture decl is field captured in ImplicitDefaultFirstprivateFDs
1232 bool isImplicitDefaultFirstprivateFD(VarDecl *VD) const {
1233 const_iterator I = begin();
1234 const_iterator EndI = end();
1235 for (; I != EndI; ++I)
1236 if (I->DefaultAttr == DSA_firstprivate || I->DefaultAttr == DSA_private)
1237 break;
1238 if (I == EndI)
1239 return false;
1240 for (const auto &IFD : I->ImplicitDefaultFirstprivateFDs)
1241 if (IFD.VD == VD)
1242 return true;
1243 return false;
1244 }
1245 /// Store capture FD info in ImplicitDefaultFirstprivateFDs
1246 void addImplicitDefaultFirstprivateFD(const FieldDecl *FD, VarDecl *VD) {
1247 iterator I = begin();
1248 const_iterator EndI = end();
1249 size_t StackLevel = getStackSize();
1250 for (; I != EndI; ++I) {
1251 if (I->DefaultAttr == DSA_private || I->DefaultAttr == DSA_firstprivate) {
1252 I->ImplicitDefaultFirstprivateFDs.emplace_back(Args&: FD, Args&: StackLevel, Args&: VD);
1253 break;
1254 }
1255 StackLevel--;
1256 }
1257 assert((StackLevel > 0 && I != EndI) || (StackLevel == 0 && I == EndI));
1258 }
1259};
1260
1261bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) {
1262 return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind);
1263}
1264
1265bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) {
1266 return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(Kind: DKind) ||
1267 DKind == OMPD_unknown;
1268}
1269
1270} // namespace
1271
1272static const Expr *getExprAsWritten(const Expr *E) {
1273 if (const auto *FE = dyn_cast<FullExpr>(Val: E))
1274 E = FE->getSubExpr();
1275
1276 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(Val: E))
1277 E = MTE->getSubExpr();
1278
1279 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(Val: E))
1280 E = Binder->getSubExpr();
1281
1282 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(Val: E))
1283 E = ICE->getSubExprAsWritten();
1284 return E->IgnoreParens();
1285}
1286
1287static Expr *getExprAsWritten(Expr *E) {
1288 return const_cast<Expr *>(getExprAsWritten(E: const_cast<const Expr *>(E)));
1289}
1290
1291static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
1292 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(Val: D))
1293 if (const auto *ME = dyn_cast<MemberExpr>(Val: getExprAsWritten(E: CED->getInit())))
1294 D = ME->getMemberDecl();
1295
1296 D = cast<ValueDecl>(Val: D->getCanonicalDecl());
1297 return D;
1298}
1299
1300static ValueDecl *getCanonicalDecl(ValueDecl *D) {
1301 return const_cast<ValueDecl *>(
1302 getCanonicalDecl(D: const_cast<const ValueDecl *>(D)));
1303}
1304
1305static std::string getOpenMPClauseNameForDiag(OpenMPClauseKind C) {
1306 if (C == OMPC_threadprivate)
1307 return getOpenMPClauseName(C).str() + " or thread local";
1308 return getOpenMPClauseName(C).str();
1309}
1310
1311DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter,
1312 ValueDecl *D) const {
1313 D = getCanonicalDecl(D);
1314 auto *VD = dyn_cast<VarDecl>(Val: D);
1315 const auto *FD = dyn_cast<FieldDecl>(Val: D);
1316 DSAVarData DVar;
1317 if (Iter == end()) {
1318 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1319 // in a region but not in construct]
1320 // File-scope or namespace-scope variables referenced in called routines
1321 // in the region are shared unless they appear in a threadprivate
1322 // directive.
1323 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(Val: VD))
1324 DVar.CKind = OMPC_shared;
1325
1326 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
1327 // in a region but not in construct]
1328 // Variables with static storage duration that are declared in called
1329 // routines in the region are shared.
1330 if (VD && VD->hasGlobalStorage())
1331 DVar.CKind = OMPC_shared;
1332
1333 // Non-static data members are shared by default.
1334 if (FD)
1335 DVar.CKind = OMPC_shared;
1336
1337 return DVar;
1338 }
1339
1340 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1341 // in a Construct, C/C++, predetermined, p.1]
1342 // Variables with automatic storage duration that are declared in a scope
1343 // inside the construct are private.
1344 if (VD && isOpenMPLocal(D: VD, Iter) && VD->isLocalVarDecl() &&
1345 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
1346 DVar.CKind = OMPC_private;
1347 return DVar;
1348 }
1349
1350 DVar.DKind = Iter->Directive;
1351 // Explicitly specified attributes and local variables with predetermined
1352 // attributes.
1353 if (Iter->SharingMap.count(Val: D)) {
1354 const DSAInfo &Data = Iter->SharingMap.lookup(Val: D);
1355 DVar.RefExpr = Data.RefExpr.getPointer();
1356 DVar.PrivateCopy = Data.PrivateCopy;
1357 DVar.CKind = Data.Attributes;
1358 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1359 DVar.Modifier = Data.Modifier;
1360 DVar.AppliedToPointee = Data.AppliedToPointee;
1361 return DVar;
1362 }
1363
1364 DefaultDataSharingAttributes IterDA = Iter->DefaultAttr;
1365 switch (Iter->DefaultVCAttr) {
1366 case DSA_VC_aggregate:
1367 if (!D->getType()->isAggregateType())
1368 IterDA = DSA_none;
1369 break;
1370 case DSA_VC_pointer:
1371 if (!D->getType()->isPointerType())
1372 IterDA = DSA_none;
1373 break;
1374 case DSA_VC_scalar:
1375 if (!D->getType()->isScalarType())
1376 IterDA = DSA_none;
1377 break;
1378 case DSA_VC_all:
1379 break;
1380 }
1381
1382 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1383 // in a Construct, C/C++, implicitly determined, p.1]
1384 // In a parallel or task construct, the data-sharing attributes of these
1385 // variables are determined by the default clause, if present.
1386 switch (IterDA) {
1387 case DSA_shared:
1388 DVar.CKind = OMPC_shared;
1389 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1390 return DVar;
1391 case DSA_none:
1392 return DVar;
1393 case DSA_firstprivate:
1394 if (VD && VD->getStorageDuration() == SD_Static &&
1395 VD->getDeclContext()->isFileContext()) {
1396 DVar.CKind = OMPC_unknown;
1397 } else {
1398 DVar.CKind = OMPC_firstprivate;
1399 }
1400 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1401 return DVar;
1402 case DSA_private:
1403 // each variable with static storage duration that is declared
1404 // in a namespace or global scope and referenced in the construct,
1405 // and that does not have a predetermined data-sharing attribute
1406 if (VD && VD->getStorageDuration() == SD_Static &&
1407 VD->getDeclContext()->isFileContext()) {
1408 DVar.CKind = OMPC_unknown;
1409 } else {
1410 DVar.CKind = OMPC_private;
1411 }
1412 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1413 return DVar;
1414 case DSA_unspecified:
1415 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1416 // in a Construct, implicitly determined, p.2]
1417 // In a parallel construct, if no default clause is present, these
1418 // variables are shared.
1419 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1420 if ((isOpenMPParallelDirective(DKind: DVar.DKind) &&
1421 !isOpenMPTaskLoopDirective(DKind: DVar.DKind)) ||
1422 isOpenMPTeamsDirective(DKind: DVar.DKind)) {
1423 DVar.CKind = OMPC_shared;
1424 return DVar;
1425 }
1426
1427 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1428 // in a Construct, implicitly determined, p.4]
1429 // In a task construct, if no default clause is present, a variable that in
1430 // the enclosing context is determined to be shared by all implicit tasks
1431 // bound to the current team is shared.
1432 if (isOpenMPTaskingDirective(Kind: DVar.DKind)) {
1433 DSAVarData DVarTemp;
1434 const_iterator I = Iter, E = end();
1435 do {
1436 ++I;
1437 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
1438 // Referenced in a Construct, implicitly determined, p.6]
1439 // In a task construct, if no default clause is present, a variable
1440 // whose data-sharing attribute is not determined by the rules above is
1441 // firstprivate.
1442 DVarTemp = getDSA(Iter&: I, D);
1443 if (DVarTemp.CKind != OMPC_shared) {
1444 DVar.RefExpr = nullptr;
1445 DVar.CKind = OMPC_firstprivate;
1446 return DVar;
1447 }
1448 } while (I != E && !isImplicitTaskingRegion(DKind: I->Directive));
1449 DVar.CKind =
1450 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
1451 return DVar;
1452 }
1453 }
1454 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1455 // in a Construct, implicitly determined, p.3]
1456 // For constructs other than task, if no default clause is present, these
1457 // variables inherit their data-sharing attributes from the enclosing
1458 // context.
1459 return getDSA(Iter&: ++Iter, D);
1460}
1461
1462const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
1463 const Expr *NewDE) {
1464 assert(!isStackEmpty() && "Data sharing attributes stack is empty");
1465 D = getCanonicalDecl(D);
1466 SharingMapTy &StackElem = getTopOfStack();
1467 auto [It, Inserted] = StackElem.AlignedMap.try_emplace(Key: D, Args&: NewDE);
1468 if (Inserted) {
1469 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
1470 return nullptr;
1471 }
1472 assert(It->second && "Unexpected nullptr expr in the aligned map");
1473 return It->second;
1474}
1475
1476const Expr *DSAStackTy::addUniqueNontemporal(const ValueDecl *D,
1477 const Expr *NewDE) {
1478 assert(!isStackEmpty() && "Data sharing attributes stack is empty");
1479 D = getCanonicalDecl(D);
1480 SharingMapTy &StackElem = getTopOfStack();
1481 auto [It, Inserted] = StackElem.NontemporalMap.try_emplace(Key: D, Args&: NewDE);
1482 if (Inserted) {
1483 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
1484 return nullptr;
1485 }
1486 assert(It->second && "Unexpected nullptr expr in the aligned map");
1487 return It->second;
1488}
1489
1490void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
1491 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1492 D = getCanonicalDecl(D);
1493 SharingMapTy &StackElem = getTopOfStack();
1494 StackElem.LCVMap.try_emplace(
1495 Key: D, Args: LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
1496}
1497
1498const DSAStackTy::LCDeclInfo
1499DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
1500 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1501 D = getCanonicalDecl(D);
1502 const SharingMapTy &StackElem = getTopOfStack();
1503 auto It = StackElem.LCVMap.find(Val: D);
1504 if (It != StackElem.LCVMap.end())
1505 return It->second;
1506 return {0, nullptr};
1507}
1508
1509const DSAStackTy::LCDeclInfo
1510DSAStackTy::isLoopControlVariable(const ValueDecl *D, unsigned Level) const {
1511 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1512 D = getCanonicalDecl(D);
1513 for (unsigned I = Level + 1; I > 0; --I) {
1514 const SharingMapTy &StackElem = getStackElemAtLevel(Level: I - 1);
1515 auto It = StackElem.LCVMap.find(Val: D);
1516 if (It != StackElem.LCVMap.end())
1517 return It->second;
1518 }
1519 return {0, nullptr};
1520}
1521
1522const DSAStackTy::LCDeclInfo
1523DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
1524 const SharingMapTy *Parent = getSecondOnStackOrNull();
1525 assert(Parent && "Data-sharing attributes stack is empty");
1526 D = getCanonicalDecl(D);
1527 auto It = Parent->LCVMap.find(Val: D);
1528 if (It != Parent->LCVMap.end())
1529 return It->second;
1530 return {0, nullptr};
1531}
1532
1533const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
1534 const SharingMapTy *Parent = getSecondOnStackOrNull();
1535 assert(Parent && "Data-sharing attributes stack is empty");
1536 if (Parent->LCVMap.size() < I)
1537 return nullptr;
1538 for (const auto &Pair : Parent->LCVMap)
1539 if (Pair.second.first == I)
1540 return Pair.first;
1541 return nullptr;
1542}
1543
1544void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
1545 DeclRefExpr *PrivateCopy, unsigned Modifier,
1546 bool AppliedToPointee) {
1547 D = getCanonicalDecl(D);
1548 if (A == OMPC_threadprivate) {
1549 DSAInfo &Data = Threadprivates[D];
1550 Data.Attributes = A;
1551 Data.RefExpr.setPointer(E);
1552 Data.PrivateCopy = nullptr;
1553 Data.Modifier = Modifier;
1554 } else if (A == OMPC_groupprivate) {
1555 DSAInfo &Data = Groupprivates[D];
1556 Data.Attributes = A;
1557 Data.RefExpr.setPointer(E);
1558 Data.PrivateCopy = nullptr;
1559 Data.Modifier = Modifier;
1560 } else {
1561 DSAInfo &Data = getTopOfStack().SharingMap[D];
1562 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
1563 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
1564 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
1565 (isLoopControlVariable(D).first && A == OMPC_private));
1566 Data.Modifier = Modifier;
1567 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
1568 Data.RefExpr.setInt(/*IntVal=*/true);
1569 return;
1570 }
1571 const bool IsLastprivate =
1572 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
1573 Data.Attributes = A;
1574 Data.RefExpr.setPointerAndInt(PtrVal: E, IntVal: IsLastprivate);
1575 Data.PrivateCopy = PrivateCopy;
1576 Data.AppliedToPointee = AppliedToPointee;
1577 if (PrivateCopy) {
1578 DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()];
1579 Data.Modifier = Modifier;
1580 Data.Attributes = A;
1581 Data.RefExpr.setPointerAndInt(PtrVal: PrivateCopy, IntVal: IsLastprivate);
1582 Data.PrivateCopy = nullptr;
1583 Data.AppliedToPointee = AppliedToPointee;
1584 }
1585 }
1586}
1587
1588/// Build a variable declaration for OpenMP loop iteration variable.
1589static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
1590 StringRef Name, const AttrVec *Attrs = nullptr,
1591 DeclRefExpr *OrigRef = nullptr) {
1592 DeclContext *DC = SemaRef.CurContext;
1593 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
1594 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(T: Type, Loc);
1595 auto *Decl =
1596 VarDecl::Create(C&: SemaRef.Context, DC, StartLoc: Loc, IdLoc: Loc, Id: II, T: Type, TInfo, S: SC_None);
1597 if (Attrs) {
1598 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
1599 I != E; ++I)
1600 Decl->addAttr(A: *I);
1601 }
1602 Decl->setImplicit();
1603 if (OrigRef) {
1604 Decl->addAttr(
1605 A: OMPReferencedVarAttr::CreateImplicit(Ctx&: SemaRef.Context, Ref: OrigRef));
1606 }
1607 return Decl;
1608}
1609
1610static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
1611 SourceLocation Loc,
1612 bool RefersToCapture = false) {
1613 D->setReferenced();
1614 D->markUsed(C&: S.Context);
1615 return DeclRefExpr::Create(Context: S.getASTContext(), QualifierLoc: NestedNameSpecifierLoc(),
1616 TemplateKWLoc: SourceLocation(), D, RefersToEnclosingVariableOrCapture: RefersToCapture, NameLoc: Loc, T: Ty,
1617 VK: VK_LValue);
1618}
1619
1620void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1621 BinaryOperatorKind BOK) {
1622 D = getCanonicalDecl(D);
1623 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1624 assert(
1625 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1626 "Additional reduction info may be specified only for reduction items.");
1627 ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1628 assert(ReductionData.ReductionRange.isInvalid() &&
1629 (getTopOfStack().Directive == OMPD_taskgroup ||
1630 ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
1631 isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
1632 !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
1633 "Additional reduction info may be specified only once for reduction "
1634 "items.");
1635 ReductionData.set(BO: BOK, RR: SR);
1636 Expr *&TaskgroupReductionRef = getTopOfStack().TaskgroupReductionRef;
1637 if (!TaskgroupReductionRef) {
1638 VarDecl *VD = buildVarDecl(SemaRef, Loc: SR.getBegin(),
1639 Type: SemaRef.Context.VoidPtrTy, Name: ".task_red.");
1640 TaskgroupReductionRef =
1641 buildDeclRefExpr(S&: SemaRef, D: VD, Ty: SemaRef.Context.VoidPtrTy, Loc: SR.getBegin());
1642 }
1643}
1644
1645void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1646 const Expr *ReductionRef) {
1647 D = getCanonicalDecl(D);
1648 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1649 assert(
1650 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1651 "Additional reduction info may be specified only for reduction items.");
1652 ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1653 assert(ReductionData.ReductionRange.isInvalid() &&
1654 (getTopOfStack().Directive == OMPD_taskgroup ||
1655 ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
1656 isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
1657 !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
1658 "Additional reduction info may be specified only once for reduction "
1659 "items.");
1660 ReductionData.set(RefExpr: ReductionRef, RR: SR);
1661 Expr *&TaskgroupReductionRef = getTopOfStack().TaskgroupReductionRef;
1662 if (!TaskgroupReductionRef) {
1663 VarDecl *VD = buildVarDecl(SemaRef, Loc: SR.getBegin(),
1664 Type: SemaRef.Context.VoidPtrTy, Name: ".task_red.");
1665 TaskgroupReductionRef =
1666 buildDeclRefExpr(S&: SemaRef, D: VD, Ty: SemaRef.Context.VoidPtrTy, Loc: SR.getBegin());
1667 }
1668}
1669
1670const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1671 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
1672 Expr *&TaskgroupDescriptor) const {
1673 D = getCanonicalDecl(D);
1674 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1675 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) {
1676 const DSAInfo &Data = I->SharingMap.lookup(Val: D);
1677 if (Data.Attributes != OMPC_reduction ||
1678 Data.Modifier != OMPC_REDUCTION_task)
1679 continue;
1680 const ReductionData &ReductionData = I->ReductionMap.lookup(Val: D);
1681 if (!ReductionData.ReductionOp ||
1682 isa<const Expr *>(Val: ReductionData.ReductionOp))
1683 return DSAVarData();
1684 SR = ReductionData.ReductionRange;
1685 BOK = cast<ReductionData::BOKPtrType>(Val: ReductionData.ReductionOp);
1686 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1687 "expression for the descriptor is not "
1688 "set.");
1689 TaskgroupDescriptor = I->TaskgroupReductionRef;
1690 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(),
1691 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task,
1692 /*AppliedToPointee=*/false);
1693 }
1694 return DSAVarData();
1695}
1696
1697const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1698 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
1699 Expr *&TaskgroupDescriptor) const {
1700 D = getCanonicalDecl(D);
1701 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1702 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) {
1703 const DSAInfo &Data = I->SharingMap.lookup(Val: D);
1704 if (Data.Attributes != OMPC_reduction ||
1705 Data.Modifier != OMPC_REDUCTION_task)
1706 continue;
1707 const ReductionData &ReductionData = I->ReductionMap.lookup(Val: D);
1708 if (!ReductionData.ReductionOp ||
1709 !isa<const Expr *>(Val: ReductionData.ReductionOp))
1710 return DSAVarData();
1711 SR = ReductionData.ReductionRange;
1712 ReductionRef = cast<const Expr *>(Val: ReductionData.ReductionOp);
1713 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1714 "expression for the descriptor is not "
1715 "set.");
1716 TaskgroupDescriptor = I->TaskgroupReductionRef;
1717 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(),
1718 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task,
1719 /*AppliedToPointee=*/false);
1720 }
1721 return DSAVarData();
1722}
1723
1724bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const {
1725 D = D->getCanonicalDecl();
1726 for (const_iterator E = end(); I != E; ++I) {
1727 if (isImplicitOrExplicitTaskingRegion(DKind: I->Directive) ||
1728 isOpenMPTargetExecutionDirective(DKind: I->Directive)) {
1729 if (I->CurScope) {
1730 Scope *TopScope = I->CurScope->getParent();
1731 Scope *CurScope = getCurScope();
1732 while (CurScope && CurScope != TopScope && !CurScope->isDeclScope(D))
1733 CurScope = CurScope->getParent();
1734 return CurScope != TopScope;
1735 }
1736 for (DeclContext *DC = D->getDeclContext(); DC; DC = DC->getParent())
1737 if (I->Context == DC)
1738 return true;
1739 return false;
1740 }
1741 }
1742 return false;
1743}
1744
1745static bool isConstNotMutableType(Sema &SemaRef, QualType Type,
1746 bool AcceptIfMutable = true,
1747 bool *IsClassType = nullptr) {
1748 ASTContext &Context = SemaRef.getASTContext();
1749 Type = Type.getNonReferenceType().getCanonicalType();
1750 bool IsConstant = Type.isConstant(Ctx: Context);
1751 Type = Context.getBaseElementType(QT: Type);
1752 const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus
1753 ? Type->getAsCXXRecordDecl()
1754 : nullptr;
1755 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(Val: RD))
1756 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
1757 RD = CTD->getTemplatedDecl();
1758 if (IsClassType)
1759 *IsClassType = RD;
1760 return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD &&
1761 RD->hasDefinition() && RD->hasMutableFields());
1762}
1763
1764static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D,
1765 QualType Type, OpenMPClauseKind CKind,
1766 SourceLocation ELoc,
1767 bool AcceptIfMutable = true,
1768 bool ListItemNotVar = false) {
1769 ASTContext &Context = SemaRef.getASTContext();
1770 bool IsClassType;
1771 if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, IsClassType: &IsClassType)) {
1772 unsigned Diag = ListItemNotVar ? diag::err_omp_const_list_item
1773 : IsClassType ? diag::err_omp_const_not_mutable_variable
1774 : diag::err_omp_const_variable;
1775 SemaRef.Diag(Loc: ELoc, DiagID: Diag) << getOpenMPClauseNameForDiag(C: CKind);
1776 if (!ListItemNotVar && D) {
1777 const VarDecl *VD = dyn_cast<VarDecl>(Val: D);
1778 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
1779 VarDecl::DeclarationOnly;
1780 SemaRef.Diag(Loc: D->getLocation(),
1781 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1782 << D;
1783 }
1784 return true;
1785 }
1786 return false;
1787}
1788
1789const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1790 bool FromParent) {
1791 D = getCanonicalDecl(D);
1792 DSAVarData DVar;
1793
1794 auto *VD = dyn_cast<VarDecl>(Val: D);
1795 auto TI = Threadprivates.find(Val: D);
1796 if (TI != Threadprivates.end()) {
1797 DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
1798 DVar.CKind = OMPC_threadprivate;
1799 DVar.Modifier = TI->getSecond().Modifier;
1800 return DVar;
1801 }
1802 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
1803 DVar.RefExpr = buildDeclRefExpr(
1804 S&: SemaRef, D: VD, Ty: D->getType().getNonReferenceType(),
1805 Loc: VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1806 DVar.CKind = OMPC_threadprivate;
1807 addDSA(D, E: DVar.RefExpr, A: OMPC_threadprivate);
1808 return DVar;
1809 }
1810 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1811 // in a Construct, C/C++, predetermined, p.1]
1812 // Variables appearing in threadprivate directives are threadprivate.
1813 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
1814 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1815 SemaRef.getLangOpts().OpenMPUseTLS &&
1816 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
1817 (VD && VD->getStorageClass() == SC_Register &&
1818 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
1819 DVar.RefExpr = buildDeclRefExpr(
1820 S&: SemaRef, D: VD, Ty: D->getType().getNonReferenceType(), Loc: D->getLocation());
1821 DVar.CKind = OMPC_threadprivate;
1822 addDSA(D, E: DVar.RefExpr, A: OMPC_threadprivate);
1823 return DVar;
1824 }
1825 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
1826 VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
1827 !isLoopControlVariable(D).first) {
1828 const_iterator IterTarget =
1829 std::find_if(first: begin(), last: end(), pred: [](const SharingMapTy &Data) {
1830 return isOpenMPTargetExecutionDirective(DKind: Data.Directive);
1831 });
1832 if (IterTarget != end()) {
1833 const_iterator ParentIterTarget = IterTarget + 1;
1834 for (const_iterator Iter = begin(); Iter != ParentIterTarget; ++Iter) {
1835 if (isOpenMPLocal(D: VD, I: Iter)) {
1836 DVar.RefExpr =
1837 buildDeclRefExpr(S&: SemaRef, D: VD, Ty: D->getType().getNonReferenceType(),
1838 Loc: D->getLocation());
1839 DVar.CKind = OMPC_threadprivate;
1840 return DVar;
1841 }
1842 }
1843 if (!isClauseParsingMode() || IterTarget != begin()) {
1844 auto DSAIter = IterTarget->SharingMap.find(Val: D);
1845 if (DSAIter != IterTarget->SharingMap.end() &&
1846 isOpenMPPrivate(Kind: DSAIter->getSecond().Attributes)) {
1847 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1848 DVar.CKind = OMPC_threadprivate;
1849 return DVar;
1850 }
1851 const_iterator End = end();
1852 if (!SemaRef.OpenMP().isOpenMPCapturedByRef(
1853 D, Level: std::distance(first: ParentIterTarget, last: End),
1854 /*OpenMPCaptureLevel=*/0)) {
1855 DVar.RefExpr =
1856 buildDeclRefExpr(S&: SemaRef, D: VD, Ty: D->getType().getNonReferenceType(),
1857 Loc: IterTarget->ConstructLoc);
1858 DVar.CKind = OMPC_threadprivate;
1859 return DVar;
1860 }
1861 }
1862 }
1863 }
1864
1865 if (isStackEmpty())
1866 // Not in OpenMP execution region and top scope was already checked.
1867 return DVar;
1868
1869 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1870 // in a Construct, C/C++, predetermined, p.4]
1871 // Static data members are shared.
1872 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1873 // in a Construct, C/C++, predetermined, p.7]
1874 // Variables with static storage duration that are declared in a scope
1875 // inside the construct are shared.
1876 if (VD && VD->isStaticDataMember()) {
1877 // Check for explicitly specified attributes.
1878 const_iterator I = begin();
1879 const_iterator EndI = end();
1880 if (FromParent && I != EndI)
1881 ++I;
1882 if (I != EndI) {
1883 auto It = I->SharingMap.find(Val: D);
1884 if (It != I->SharingMap.end()) {
1885 const DSAInfo &Data = It->getSecond();
1886 DVar.RefExpr = Data.RefExpr.getPointer();
1887 DVar.PrivateCopy = Data.PrivateCopy;
1888 DVar.CKind = Data.Attributes;
1889 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1890 DVar.DKind = I->Directive;
1891 DVar.Modifier = Data.Modifier;
1892 DVar.AppliedToPointee = Data.AppliedToPointee;
1893 return DVar;
1894 }
1895 }
1896
1897 DVar.CKind = OMPC_shared;
1898 return DVar;
1899 }
1900
1901 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
1902 // The predetermined shared attribute for const-qualified types having no
1903 // mutable members was removed after OpenMP 3.1.
1904 if (SemaRef.LangOpts.OpenMP <= 31) {
1905 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1906 // in a Construct, C/C++, predetermined, p.6]
1907 // Variables with const qualified type having no mutable member are
1908 // shared.
1909 if (isConstNotMutableType(SemaRef, Type: D->getType())) {
1910 // Variables with const-qualified type having no mutable member may be
1911 // listed in a firstprivate clause, even if they are static data members.
1912 DSAVarData DVarTemp = hasInnermostDSA(
1913 D,
1914 CPred: [](OpenMPClauseKind C, bool) {
1915 return C == OMPC_firstprivate || C == OMPC_shared;
1916 },
1917 DPred: MatchesAlways, FromParent);
1918 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
1919 return DVarTemp;
1920
1921 DVar.CKind = OMPC_shared;
1922 return DVar;
1923 }
1924 }
1925
1926 // Explicitly specified attributes and local variables with predetermined
1927 // attributes.
1928 const_iterator I = begin();
1929 const_iterator EndI = end();
1930 if (FromParent && I != EndI)
1931 ++I;
1932 if (I == EndI)
1933 return DVar;
1934 auto It = I->SharingMap.find(Val: D);
1935 if (It != I->SharingMap.end()) {
1936 const DSAInfo &Data = It->getSecond();
1937 DVar.RefExpr = Data.RefExpr.getPointer();
1938 DVar.PrivateCopy = Data.PrivateCopy;
1939 DVar.CKind = Data.Attributes;
1940 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1941 DVar.DKind = I->Directive;
1942 DVar.Modifier = Data.Modifier;
1943 DVar.AppliedToPointee = Data.AppliedToPointee;
1944 }
1945
1946 return DVar;
1947}
1948
1949const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1950 bool FromParent) const {
1951 if (isStackEmpty()) {
1952 const_iterator I;
1953 return getDSA(Iter&: I, D);
1954 }
1955 D = getCanonicalDecl(D);
1956 const_iterator StartI = begin();
1957 const_iterator EndI = end();
1958 if (FromParent && StartI != EndI)
1959 ++StartI;
1960 return getDSA(Iter&: StartI, D);
1961}
1962
1963const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1964 unsigned Level) const {
1965 if (getStackSize() <= Level)
1966 return DSAVarData();
1967 D = getCanonicalDecl(D);
1968 const_iterator StartI = std::next(x: begin(), n: getStackSize() - 1 - Level);
1969 return getDSA(Iter&: StartI, D);
1970}
1971
1972const DSAStackTy::DSAVarData
1973DSAStackTy::hasDSA(ValueDecl *D,
1974 const llvm::function_ref<bool(OpenMPClauseKind, bool,
1975 DefaultDataSharingAttributes)>
1976 CPred,
1977 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1978 bool FromParent) const {
1979 if (isStackEmpty())
1980 return {};
1981 D = getCanonicalDecl(D);
1982 const_iterator I = begin();
1983 const_iterator EndI = end();
1984 if (FromParent && I != EndI)
1985 ++I;
1986 for (; I != EndI; ++I) {
1987 if (!DPred(I->Directive) &&
1988 !isImplicitOrExplicitTaskingRegion(DKind: I->Directive))
1989 continue;
1990 const_iterator NewI = I;
1991 DSAVarData DVar = getDSA(Iter&: NewI, D);
1992 if (I == NewI && CPred(DVar.CKind, DVar.AppliedToPointee, I->DefaultAttr))
1993 return DVar;
1994 }
1995 return {};
1996}
1997
1998const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
1999 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
2000 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
2001 bool FromParent) const {
2002 if (isStackEmpty())
2003 return {};
2004 D = getCanonicalDecl(D);
2005 const_iterator StartI = begin();
2006 const_iterator EndI = end();
2007 if (FromParent && StartI != EndI)
2008 ++StartI;
2009 if (StartI == EndI || !DPred(StartI->Directive))
2010 return {};
2011 const_iterator NewI = StartI;
2012 DSAVarData DVar = getDSA(Iter&: NewI, D);
2013 return (NewI == StartI && CPred(DVar.CKind, DVar.AppliedToPointee))
2014 ? DVar
2015 : DSAVarData();
2016}
2017
2018bool DSAStackTy::hasExplicitDSA(
2019 const ValueDecl *D,
2020 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
2021 unsigned Level, bool NotLastprivate) const {
2022 if (getStackSize() <= Level)
2023 return false;
2024 D = getCanonicalDecl(D);
2025 const SharingMapTy &StackElem = getStackElemAtLevel(Level);
2026 auto I = StackElem.SharingMap.find(Val: D);
2027 if (I != StackElem.SharingMap.end() && I->getSecond().RefExpr.getPointer() &&
2028 CPred(I->getSecond().Attributes, I->getSecond().AppliedToPointee) &&
2029 (!NotLastprivate || !I->getSecond().RefExpr.getInt()))
2030 return true;
2031 // Check predetermined rules for the loop control variables.
2032 auto LI = StackElem.LCVMap.find(Val: D);
2033 if (LI != StackElem.LCVMap.end())
2034 return CPred(OMPC_private, /*AppliedToPointee=*/false);
2035 return false;
2036}
2037
2038bool DSAStackTy::hasExplicitDirective(
2039 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
2040 unsigned Level) const {
2041 if (getStackSize() <= Level)
2042 return false;
2043 const SharingMapTy &StackElem = getStackElemAtLevel(Level);
2044 return DPred(StackElem.Directive);
2045}
2046
2047bool DSAStackTy::hasDirective(
2048 const llvm::function_ref<bool(OpenMPDirectiveKind,
2049 const DeclarationNameInfo &, SourceLocation)>
2050 DPred,
2051 bool FromParent) const {
2052 // We look only in the enclosing region.
2053 size_t Skip = FromParent ? 2 : 1;
2054 for (const_iterator I = begin() + std::min(a: Skip, b: getStackSize()), E = end();
2055 I != E; ++I) {
2056 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
2057 return true;
2058 }
2059 return false;
2060}
2061
2062void SemaOpenMP::InitDataSharingAttributesStack() {
2063 VarDataSharingAttributesStack = new DSAStackTy(SemaRef);
2064}
2065
2066#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
2067
2068void SemaOpenMP::pushOpenMPFunctionRegion() { DSAStack->pushFunction(); }
2069
2070void SemaOpenMP::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
2071 DSAStack->popFunction(OldFSI);
2072}
2073
2074static bool isOpenMPDeviceDelayedContext(Sema &S) {
2075 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsTargetDevice &&
2076 "Expected OpenMP device compilation.");
2077 return !S.OpenMP().isInOpenMPTargetExecutionDirective();
2078}
2079
2080namespace {
2081/// Status of the function emission on the host/device.
2082enum class FunctionEmissionStatus {
2083 Emitted,
2084 Discarded,
2085 Unknown,
2086};
2087} // anonymous namespace
2088
2089SemaBase::SemaDiagnosticBuilder
2090SemaOpenMP::diagIfOpenMPDeviceCode(SourceLocation Loc, unsigned DiagID,
2091 const FunctionDecl *FD) {
2092 assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsTargetDevice &&
2093 "Expected OpenMP device compilation.");
2094
2095 SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop;
2096 if (FD) {
2097 Sema::FunctionEmissionStatus FES = SemaRef.getEmissionStatus(Decl: FD);
2098 switch (FES) {
2099 case Sema::FunctionEmissionStatus::Emitted:
2100 Kind = SemaDiagnosticBuilder::K_Immediate;
2101 break;
2102 case Sema::FunctionEmissionStatus::Unknown:
2103 // TODO: We should always delay diagnostics here in case a target
2104 // region is in a function we do not emit. However, as the
2105 // current diagnostics are associated with the function containing
2106 // the target region and we do not emit that one, we would miss out
2107 // on diagnostics for the target region itself. We need to anchor
2108 // the diagnostics with the new generated function *or* ensure we
2109 // emit diagnostics associated with the surrounding function.
2110 Kind = isOpenMPDeviceDelayedContext(S&: SemaRef)
2111 ? SemaDiagnosticBuilder::K_Deferred
2112 : SemaDiagnosticBuilder::K_Immediate;
2113 break;
2114 case Sema::FunctionEmissionStatus::TemplateDiscarded:
2115 case Sema::FunctionEmissionStatus::OMPDiscarded:
2116 Kind = SemaDiagnosticBuilder::K_Nop;
2117 break;
2118 case Sema::FunctionEmissionStatus::CUDADiscarded:
2119 llvm_unreachable("CUDADiscarded unexpected in OpenMP device compilation");
2120 break;
2121 }
2122 }
2123
2124 return SemaDiagnosticBuilder(Kind, Loc, DiagID, FD, SemaRef);
2125}
2126
2127SemaBase::SemaDiagnosticBuilder
2128SemaOpenMP::diagIfOpenMPHostCode(SourceLocation Loc, unsigned DiagID,
2129 const FunctionDecl *FD) {
2130 assert(getLangOpts().OpenMP && !getLangOpts().OpenMPIsTargetDevice &&
2131 "Expected OpenMP host compilation.");
2132
2133 SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop;
2134 if (FD) {
2135 Sema::FunctionEmissionStatus FES = SemaRef.getEmissionStatus(Decl: FD);
2136 switch (FES) {
2137 case Sema::FunctionEmissionStatus::Emitted:
2138 Kind = SemaDiagnosticBuilder::K_Immediate;
2139 break;
2140 case Sema::FunctionEmissionStatus::Unknown:
2141 Kind = SemaDiagnosticBuilder::K_Deferred;
2142 break;
2143 case Sema::FunctionEmissionStatus::TemplateDiscarded:
2144 case Sema::FunctionEmissionStatus::OMPDiscarded:
2145 case Sema::FunctionEmissionStatus::CUDADiscarded:
2146 Kind = SemaDiagnosticBuilder::K_Nop;
2147 break;
2148 }
2149 }
2150
2151 return SemaDiagnosticBuilder(Kind, Loc, DiagID, FD, SemaRef);
2152}
2153
2154static OpenMPDefaultmapClauseKind
2155getVariableCategoryFromDecl(const LangOptions &LO, const ValueDecl *VD) {
2156 if (LO.OpenMP <= 45) {
2157 if (VD->getType().getNonReferenceType()->isScalarType())
2158 return OMPC_DEFAULTMAP_scalar;
2159 return OMPC_DEFAULTMAP_aggregate;
2160 }
2161 if (VD->getType().getNonReferenceType()->isAnyPointerType())
2162 return OMPC_DEFAULTMAP_pointer;
2163 if (VD->getType().getNonReferenceType()->isScalarType())
2164 return OMPC_DEFAULTMAP_scalar;
2165 return OMPC_DEFAULTMAP_aggregate;
2166}
2167
2168bool SemaOpenMP::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level,
2169 unsigned OpenMPCaptureLevel) const {
2170 assert(getLangOpts().OpenMP && "OpenMP is not allowed");
2171
2172 ASTContext &Ctx = getASTContext();
2173 bool IsByRef = true;
2174
2175 // Find the directive that is associated with the provided scope.
2176 D = cast<ValueDecl>(Val: D->getCanonicalDecl());
2177 QualType Ty = D->getType();
2178
2179 bool IsVariableUsedInMapClause = false;
2180 if (DSAStack->hasExplicitDirective(DPred: isOpenMPTargetExecutionDirective, Level)) {
2181 // This table summarizes how a given variable should be passed to the device
2182 // given its type and the clauses where it appears. This table is based on
2183 // the description in OpenMP 4.5 [2.10.4, target Construct] and
2184 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
2185 //
2186 // =========================================================================
2187 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
2188 // | |(tofrom:scalar)| | pvt | |has_dv_adr| |
2189 // =========================================================================
2190 // | scl | | | | - | | bycopy|
2191 // | scl | | - | x | - | - | bycopy|
2192 // | scl | | x | - | - | - | null |
2193 // | scl | x | | | - | | byref |
2194 // | scl | x | - | x | - | - | bycopy|
2195 // | scl | x | x | - | - | - | null |
2196 // | scl | | - | - | - | x | byref |
2197 // | scl | x | - | - | - | x | byref |
2198 //
2199 // | agg | n.a. | | | - | | byref |
2200 // | agg | n.a. | - | x | - | - | byref |
2201 // | agg | n.a. | x | - | - | - | null |
2202 // | agg | n.a. | - | - | - | x | byref |
2203 // | agg | n.a. | - | - | - | x[] | byref |
2204 //
2205 // | ptr | n.a. | | | - | | bycopy|
2206 // | ptr | n.a. | - | x | - | - | bycopy|
2207 // | ptr | n.a. | x | - | - | - | null |
2208 // | ptr | n.a. | - | - | - | x | byref |
2209 // | ptr | n.a. | - | - | - | x, x[] | bycopy|
2210 // | ptr | n.a. | - | - | - | x[] | bycopy|
2211 // | ptr | n.a. | - | - | x | | bycopy|
2212 // | ptr | n.a. | - | - | x | x | bycopy|
2213 // | ptr | n.a. | - | - | x | x[] | bycopy|
2214 // =========================================================================
2215 // Legend:
2216 // scl - scalar
2217 // ptr - pointer
2218 // agg - aggregate
2219 // x - applies
2220 // - - invalid in this combination
2221 // [] - mapped with an array section
2222 // byref - should be mapped by reference
2223 // byval - should be mapped by value
2224 // null - initialize a local variable to null on the device
2225 //
2226 // Observations:
2227 // - All scalar declarations that show up in a map clause have to be passed
2228 // by reference, because they may have been mapped in the enclosing data
2229 // environment.
2230 // - If the scalar value does not fit the size of uintptr, it has to be
2231 // passed by reference, regardless the result in the table above.
2232 // - For pointers mapped by value that have either an implicit map or an
2233 // array section, the runtime library may pass the NULL value to the
2234 // device instead of the value passed to it by the compiler.
2235 // - If both a pointer and a dereference of it are mapped, then the pointer
2236 // should be passed by reference.
2237
2238 if (Ty->isReferenceType())
2239 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
2240
2241 // Locate map clauses and see if the variable being captured is mapped by
2242 // itself, or referred to, in any of those clauses. Here we only care about
2243 // variables, not fields, because fields are part of aggregates.
2244 bool IsVariableAssociatedWithSection = false;
2245 bool IsVariableItselfMapped = false;
2246
2247 DSAStack->checkMappableExprComponentListsForDeclAtLevel(
2248 VD: D, Level,
2249 Check: [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection,
2250 &IsVariableItselfMapped,
2251 D](OMPClauseMappableExprCommon::MappableExprComponentListRef
2252 MapExprComponents,
2253 OpenMPClauseKind WhereFoundClauseKind) {
2254 // Both map and has_device_addr clauses information influences how a
2255 // variable is captured. E.g. is_device_ptr does not require changing
2256 // the default behavior.
2257 if (WhereFoundClauseKind != OMPC_map &&
2258 WhereFoundClauseKind != OMPC_has_device_addr)
2259 return false;
2260
2261 auto EI = MapExprComponents.rbegin();
2262 auto EE = MapExprComponents.rend();
2263
2264 assert(EI != EE && "Invalid map expression!");
2265
2266 if (isa<DeclRefExpr>(Val: EI->getAssociatedExpression()) &&
2267 EI->getAssociatedDeclaration() == D) {
2268 IsVariableUsedInMapClause = true;
2269
2270 // If the component list has only one element, it's for mapping the
2271 // variable itself, like map(p). This takes precedence in
2272 // determining how it's captured, so we don't need to look further
2273 // for any other maps that use the variable (like map(p[0]) etc.)
2274 if (MapExprComponents.size() == 1) {
2275 IsVariableItselfMapped = true;
2276 return true;
2277 }
2278 }
2279
2280 ++EI;
2281 if (EI == EE)
2282 return false;
2283 auto Last = std::prev(x: EE);
2284 const auto *UO =
2285 dyn_cast<UnaryOperator>(Val: Last->getAssociatedExpression());
2286 if ((UO && UO->getOpcode() == UO_Deref) ||
2287 isa<ArraySubscriptExpr>(Val: Last->getAssociatedExpression()) ||
2288 isa<ArraySectionExpr>(Val: Last->getAssociatedExpression()) ||
2289 isa<MemberExpr>(Val: EI->getAssociatedExpression()) ||
2290 isa<OMPArrayShapingExpr>(Val: Last->getAssociatedExpression())) {
2291 IsVariableAssociatedWithSection = true;
2292 // We've found a case like map(p[0]) or map(p->a) or map(*p),
2293 // so we are done with this particular map, but we need to keep
2294 // looking in case we find a map(p).
2295 return false;
2296 }
2297
2298 // Keep looking for more map info.
2299 return false;
2300 });
2301
2302 if (IsVariableUsedInMapClause) {
2303 // If variable is identified in a map clause it is always captured by
2304 // reference except if it is a pointer that is dereferenced somehow, but
2305 // not itself mapped.
2306 //
2307 // OpenMP 6.0, 7.1.1: Data sharing attribute rules, variables referenced
2308 // in a construct::
2309 // If a list item in a has_device_addr clause or in a map clause on the
2310 // target construct has a base pointer, and the base pointer is a scalar
2311 // variable *that is not a list item in a map clause on the construct*,
2312 // the base pointer is firstprivate.
2313 //
2314 // OpenMP 4.5, 2.15.1.1: Data-sharing Attribute Rules for Variables
2315 // Referenced in a Construct:
2316 // If an array section is a list item in a map clause on the target
2317 // construct and the array section is derived from a variable for which
2318 // the type is pointer then that variable is firstprivate.
2319 IsByRef = IsVariableItselfMapped ||
2320 !(Ty->isPointerType() && IsVariableAssociatedWithSection);
2321 } else {
2322 // By default, all the data that has a scalar type is mapped by copy
2323 // (except for reduction variables).
2324 // Defaultmap scalar is mutual exclusive to defaultmap pointer
2325 IsByRef = (DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
2326 !Ty->isAnyPointerType()) ||
2327 !Ty->isScalarType() ||
2328 DSAStack->isDefaultmapCapturedByRef(
2329 Level, Kind: getVariableCategoryFromDecl(LO: getLangOpts(), VD: D)) ||
2330 DSAStack->hasExplicitDSA(
2331 D,
2332 CPred: [](OpenMPClauseKind K, bool AppliedToPointee) {
2333 return K == OMPC_reduction && !AppliedToPointee;
2334 },
2335 Level);
2336 }
2337 }
2338
2339 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
2340 IsByRef =
2341 ((IsVariableUsedInMapClause &&
2342 DSAStack->getCaptureRegion(Level, OpenMPCaptureLevel) ==
2343 OMPD_target) ||
2344 !(DSAStack->hasExplicitDSA(
2345 D,
2346 CPred: [](OpenMPClauseKind K, bool AppliedToPointee) -> bool {
2347 return K == OMPC_firstprivate ||
2348 (K == OMPC_reduction && AppliedToPointee);
2349 },
2350 Level, /*NotLastprivate=*/true) ||
2351 DSAStack->isUsesAllocatorsDecl(Level, D))) &&
2352 // If the variable is artificial and must be captured by value - try to
2353 // capture by value.
2354 !(isa<OMPCapturedExprDecl>(Val: D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
2355 !cast<OMPCapturedExprDecl>(Val: D)->getInit()->isGLValue()) &&
2356 // If the variable is implicitly firstprivate and scalar - capture by
2357 // copy
2358 !((DSAStack->getDefaultDSA() == DSA_firstprivate ||
2359 DSAStack->getDefaultDSA() == DSA_private) &&
2360 !DSAStack->hasExplicitDSA(
2361 D, CPred: [](OpenMPClauseKind K, bool) { return K != OMPC_unknown; },
2362 Level) &&
2363 !DSAStack->isLoopControlVariable(D, Level).first);
2364 }
2365
2366 // When passing data by copy, we need to make sure it fits the uintptr size
2367 // and alignment, because the runtime library only deals with uintptr types.
2368 // If it does not fit the uintptr size, we need to pass the data by reference
2369 // instead.
2370 if (!IsByRef && (Ctx.getTypeSizeInChars(T: Ty) >
2371 Ctx.getTypeSizeInChars(T: Ctx.getUIntPtrType()) ||
2372 Ctx.getAlignOfGlobalVarInChars(T: Ty, VD: dyn_cast<VarDecl>(Val: D)) >
2373 Ctx.getTypeAlignInChars(T: Ctx.getUIntPtrType()))) {
2374 IsByRef = true;
2375 }
2376
2377 return IsByRef;
2378}
2379
2380unsigned SemaOpenMP::getOpenMPNestingLevel() const {
2381 assert(getLangOpts().OpenMP);
2382 return DSAStack->getNestingLevel();
2383}
2384
2385bool SemaOpenMP::isInOpenMPTaskUntiedContext() const {
2386 return isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
2387 DSAStack->isUntiedRegion();
2388}
2389
2390bool SemaOpenMP::isInOpenMPTargetExecutionDirective() const {
2391 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
2392 !DSAStack->isClauseParsingMode()) ||
2393 DSAStack->hasDirective(
2394 DPred: [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
2395 SourceLocation) -> bool {
2396 return isOpenMPTargetExecutionDirective(DKind: K);
2397 },
2398 FromParent: false);
2399}
2400
2401bool SemaOpenMP::isOpenMPRebuildMemberExpr(ValueDecl *D) {
2402 // Only rebuild for Field.
2403 if (!isa<FieldDecl>(Val: D))
2404 return false;
2405 DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA(
2406 D,
2407 CPred: [](OpenMPClauseKind C, bool AppliedToPointee,
2408 DefaultDataSharingAttributes DefaultAttr) {
2409 return isOpenMPPrivate(Kind: C) && !AppliedToPointee &&
2410 (DefaultAttr == DSA_firstprivate || DefaultAttr == DSA_private);
2411 },
2412 DPred: [](OpenMPDirectiveKind) { return true; },
2413 DSAStack->isClauseParsingMode());
2414 if (DVarPrivate.CKind != OMPC_unknown)
2415 return true;
2416 return false;
2417}
2418
2419static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
2420 Expr *CaptureExpr, bool WithInit,
2421 DeclContext *CurContext,
2422 bool AsExpression);
2423
2424VarDecl *SemaOpenMP::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo,
2425 unsigned StopAt) {
2426 assert(getLangOpts().OpenMP && "OpenMP is not allowed");
2427 D = getCanonicalDecl(D);
2428
2429 auto *VD = dyn_cast<VarDecl>(Val: D);
2430 // Do not capture constexpr variables.
2431 if (VD && VD->isConstexpr())
2432 return nullptr;
2433
2434 // If we want to determine whether the variable should be captured from the
2435 // perspective of the current capturing scope, and we've already left all the
2436 // capturing scopes of the top directive on the stack, check from the
2437 // perspective of its parent directive (if any) instead.
2438 DSAStackTy::ParentDirectiveScope InParentDirectiveRAII(
2439 *DSAStack, CheckScopeInfo && DSAStack->isBodyComplete());
2440
2441 // If we are attempting to capture a global variable in a directive with
2442 // 'target' we return true so that this global is also mapped to the device.
2443 //
2444 if (VD && !VD->hasLocalStorage() &&
2445 (SemaRef.getCurCapturedRegion() || SemaRef.getCurBlock() ||
2446 SemaRef.getCurLambda())) {
2447 if (isInOpenMPTargetExecutionDirective()) {
2448 DSAStackTy::DSAVarData DVarTop =
2449 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
2450 if (DVarTop.CKind != OMPC_unknown && DVarTop.RefExpr)
2451 return VD;
2452 // If the declaration is enclosed in a 'declare target' directive,
2453 // then it should not be captured.
2454 //
2455 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
2456 return nullptr;
2457 CapturedRegionScopeInfo *CSI = nullptr;
2458 for (FunctionScopeInfo *FSI : llvm::drop_begin(
2459 RangeOrContainer: llvm::reverse(C&: SemaRef.FunctionScopes),
2460 N: CheckScopeInfo ? (SemaRef.FunctionScopes.size() - (StopAt + 1))
2461 : 0)) {
2462 if (!isa<CapturingScopeInfo>(Val: FSI))
2463 return nullptr;
2464 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(Val: FSI))
2465 if (RSI->CapRegionKind == CR_OpenMP) {
2466 CSI = RSI;
2467 break;
2468 }
2469 }
2470 assert(CSI && "Failed to find CapturedRegionScopeInfo");
2471 SmallVector<OpenMPDirectiveKind, 4> Regions;
2472 getOpenMPCaptureRegions(CaptureRegions&: Regions,
2473 DSAStack->getDirective(Level: CSI->OpenMPLevel));
2474 if (Regions[CSI->OpenMPCaptureLevel] != OMPD_task)
2475 return VD;
2476 }
2477 if (isInOpenMPDeclareTargetContext()) {
2478 // Try to mark variable as declare target if it is used in capturing
2479 // regions.
2480 if (getLangOpts().OpenMP <= 45 &&
2481 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
2482 checkDeclIsAllowedInOpenMPTarget(E: nullptr, D: VD);
2483 return nullptr;
2484 }
2485 }
2486
2487 if (CheckScopeInfo) {
2488 bool OpenMPFound = false;
2489 for (unsigned I = StopAt + 1; I > 0; --I) {
2490 FunctionScopeInfo *FSI = SemaRef.FunctionScopes[I - 1];
2491 if (!isa<CapturingScopeInfo>(Val: FSI))
2492 return nullptr;
2493 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(Val: FSI))
2494 if (RSI->CapRegionKind == CR_OpenMP) {
2495 OpenMPFound = true;
2496 break;
2497 }
2498 }
2499 if (!OpenMPFound)
2500 return nullptr;
2501 }
2502
2503 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
2504 (!DSAStack->isClauseParsingMode() ||
2505 DSAStack->getParentDirective() != OMPD_unknown)) {
2506 auto &&Info = DSAStack->isLoopControlVariable(D);
2507 if (Info.first ||
2508 (VD && VD->hasLocalStorage() &&
2509 isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) ||
2510 (VD && DSAStack->isForceVarCapturing()))
2511 return VD ? VD : Info.second;
2512 DSAStackTy::DSAVarData DVarTop =
2513 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
2514 if (DVarTop.CKind != OMPC_unknown && isOpenMPPrivate(Kind: DVarTop.CKind) &&
2515 (!VD || VD->hasLocalStorage() ||
2516 !(DVarTop.AppliedToPointee && DVarTop.CKind != OMPC_reduction)))
2517 return VD ? VD : cast<VarDecl>(Val: DVarTop.PrivateCopy->getDecl());
2518 // Threadprivate variables must not be captured.
2519 if (isOpenMPThreadPrivate(Kind: DVarTop.CKind))
2520 return nullptr;
2521 // The variable is not private or it is the variable in the directive with
2522 // default(none) clause and not used in any clause.
2523 DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA(
2524 D,
2525 CPred: [](OpenMPClauseKind C, bool AppliedToPointee, bool) {
2526 return isOpenMPPrivate(Kind: C) && !AppliedToPointee;
2527 },
2528 DPred: [](OpenMPDirectiveKind) { return true; },
2529 DSAStack->isClauseParsingMode());
2530 // Global shared must not be captured.
2531 if (VD && !VD->hasLocalStorage() && DVarPrivate.CKind == OMPC_unknown &&
2532 ((DSAStack->getDefaultDSA() != DSA_none &&
2533 DSAStack->getDefaultDSA() != DSA_private &&
2534 DSAStack->getDefaultDSA() != DSA_firstprivate) ||
2535 DVarTop.CKind == OMPC_shared))
2536 return nullptr;
2537 auto *FD = dyn_cast<FieldDecl>(Val: D);
2538 if (DVarPrivate.CKind != OMPC_unknown && !VD && FD &&
2539 !DVarPrivate.PrivateCopy) {
2540 DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA(
2541 D,
2542 CPred: [](OpenMPClauseKind C, bool AppliedToPointee,
2543 DefaultDataSharingAttributes DefaultAttr) {
2544 return isOpenMPPrivate(Kind: C) && !AppliedToPointee &&
2545 (DefaultAttr == DSA_firstprivate ||
2546 DefaultAttr == DSA_private);
2547 },
2548 DPred: [](OpenMPDirectiveKind) { return true; },
2549 DSAStack->isClauseParsingMode());
2550 if (DVarPrivate.CKind == OMPC_unknown)
2551 return nullptr;
2552
2553 VarDecl *VD = DSAStack->getImplicitFDCapExprDecl(FD);
2554 if (VD)
2555 return VD;
2556 if (SemaRef.getCurrentThisType().isNull())
2557 return nullptr;
2558 Expr *ThisExpr = SemaRef.BuildCXXThisExpr(Loc: SourceLocation(),
2559 Type: SemaRef.getCurrentThisType(),
2560 /*IsImplicit=*/true);
2561 const CXXScopeSpec CS = CXXScopeSpec();
2562 Expr *ME = SemaRef.BuildMemberExpr(
2563 Base: ThisExpr, /*IsArrow=*/true, OpLoc: SourceLocation(),
2564 NNS: NestedNameSpecifierLoc(), TemplateKWLoc: SourceLocation(), Member: FD,
2565 FoundDecl: DeclAccessPair::make(D: FD, AS: FD->getAccess()),
2566 /*HadMultipleCandidates=*/false, MemberNameInfo: DeclarationNameInfo(), Ty: FD->getType(),
2567 VK: VK_LValue, OK: OK_Ordinary);
2568 OMPCapturedExprDecl *CD = buildCaptureDecl(
2569 S&: SemaRef, Id: FD->getIdentifier(), CaptureExpr: ME, WithInit: DVarPrivate.CKind != OMPC_private,
2570 CurContext: SemaRef.CurContext->getParent(), /*AsExpression=*/false);
2571 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
2572 S&: SemaRef, D: CD, Ty: CD->getType().getNonReferenceType(), Loc: SourceLocation());
2573 VD = cast<VarDecl>(Val: VDPrivateRefExpr->getDecl());
2574 DSAStack->addImplicitDefaultFirstprivateFD(FD, VD);
2575 return VD;
2576 }
2577 if (DVarPrivate.CKind != OMPC_unknown ||
2578 (VD && (DSAStack->getDefaultDSA() == DSA_none ||
2579 DSAStack->getDefaultDSA() == DSA_private ||
2580 DSAStack->getDefaultDSA() == DSA_firstprivate)))
2581 return VD ? VD : cast<VarDecl>(Val: DVarPrivate.PrivateCopy->getDecl());
2582 }
2583 return nullptr;
2584}
2585
2586void SemaOpenMP::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
2587 unsigned Level) const {
2588 FunctionScopesIndex -= getOpenMPCaptureLevels(DSAStack->getDirective(Level));
2589}
2590
2591void SemaOpenMP::startOpenMPLoop() {
2592 assert(getLangOpts().OpenMP && "OpenMP must be enabled.");
2593 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
2594 DSAStack->loopInit();
2595}
2596
2597void SemaOpenMP::startOpenMPCXXRangeFor() {
2598 assert(getLangOpts().OpenMP && "OpenMP must be enabled.");
2599 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2600 DSAStack->resetPossibleLoopCounter();
2601 DSAStack->loopStart();
2602 }
2603}
2604
2605OpenMPClauseKind SemaOpenMP::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level,
2606 unsigned CapLevel) const {
2607 assert(getLangOpts().OpenMP && "OpenMP is not allowed");
2608 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
2609 (!DSAStack->isClauseParsingMode() ||
2610 DSAStack->getParentDirective() != OMPD_unknown)) {
2611 DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA(
2612 D,
2613 CPred: [](OpenMPClauseKind C, bool AppliedToPointee,
2614 DefaultDataSharingAttributes DefaultAttr) {
2615 return isOpenMPPrivate(Kind: C) && !AppliedToPointee &&
2616 DefaultAttr == DSA_private;
2617 },
2618 DPred: [](OpenMPDirectiveKind) { return true; },
2619 DSAStack->isClauseParsingMode());
2620 if (DVarPrivate.CKind == OMPC_private && isa<OMPCapturedExprDecl>(Val: D) &&
2621 DSAStack->isImplicitDefaultFirstprivateFD(VD: cast<VarDecl>(Val: D)) &&
2622 !DSAStack->isLoopControlVariable(D).first)
2623 return OMPC_private;
2624 }
2625 if (DSAStack->hasExplicitDirective(DPred: isOpenMPTaskingDirective, Level)) {
2626 bool IsTriviallyCopyable =
2627 D->getType().getNonReferenceType().isTriviallyCopyableType(
2628 Context: getASTContext()) &&
2629 !D->getType()
2630 .getNonReferenceType()
2631 .getCanonicalType()
2632 ->getAsCXXRecordDecl();
2633 OpenMPDirectiveKind DKind = DSAStack->getDirective(Level);
2634 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2635 getOpenMPCaptureRegions(CaptureRegions, DKind);
2636 if (isOpenMPTaskingDirective(Kind: CaptureRegions[CapLevel]) &&
2637 (IsTriviallyCopyable ||
2638 !isOpenMPTaskLoopDirective(DKind: CaptureRegions[CapLevel]))) {
2639 if (DSAStack->hasExplicitDSA(
2640 D,
2641 CPred: [](OpenMPClauseKind K, bool) { return K == OMPC_firstprivate; },
2642 Level, /*NotLastprivate=*/true))
2643 return OMPC_firstprivate;
2644 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level);
2645 if (DVar.CKind != OMPC_shared &&
2646 !DSAStack->isLoopControlVariable(D, Level).first && !DVar.RefExpr) {
2647 DSAStack->addImplicitTaskFirstprivate(Level, D);
2648 return OMPC_firstprivate;
2649 }
2650 }
2651 }
2652 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()) &&
2653 !isOpenMPLoopTransformationDirective(DSAStack->getCurrentDirective())) {
2654 if (DSAStack->getAssociatedLoops() > 0 && !DSAStack->isLoopStarted()) {
2655 DSAStack->resetPossibleLoopCounter(D);
2656 DSAStack->loopStart();
2657 return OMPC_private;
2658 }
2659 if ((DSAStack->getPossiblyLoopCounter() == D->getCanonicalDecl() ||
2660 DSAStack->isLoopControlVariable(D).first) &&
2661 !DSAStack->hasExplicitDSA(
2662 D, CPred: [](OpenMPClauseKind K, bool) { return K != OMPC_private; },
2663 Level) &&
2664 !isOpenMPSimdDirective(DSAStack->getCurrentDirective()))
2665 return OMPC_private;
2666 }
2667 if (const auto *VD = dyn_cast<VarDecl>(Val: D)) {
2668 if (DSAStack->isThreadPrivate(D: const_cast<VarDecl *>(VD)) &&
2669 DSAStack->isForceVarCapturing() &&
2670 !DSAStack->hasExplicitDSA(
2671 D, CPred: [](OpenMPClauseKind K, bool) { return K == OMPC_copyin; },
2672 Level))
2673 return OMPC_private;
2674 }
2675 // User-defined allocators are private since they must be defined in the
2676 // context of target region.
2677 if (DSAStack->hasExplicitDirective(DPred: isOpenMPTargetExecutionDirective, Level) &&
2678 DSAStack->isUsesAllocatorsDecl(Level, D).value_or(
2679 u: DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) ==
2680 DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator)
2681 return OMPC_private;
2682 return (DSAStack->hasExplicitDSA(
2683 D, CPred: [](OpenMPClauseKind K, bool) { return K == OMPC_private; },
2684 Level) ||
2685 (DSAStack->isClauseParsingMode() &&
2686 DSAStack->getClauseParsingMode() == OMPC_private) ||
2687 // Consider taskgroup reduction descriptor variable a private
2688 // to avoid possible capture in the region.
2689 (DSAStack->hasExplicitDirective(
2690 DPred: [](OpenMPDirectiveKind K) {
2691 return K == OMPD_taskgroup ||
2692 ((isOpenMPParallelDirective(DKind: K) ||
2693 isOpenMPWorksharingDirective(DKind: K)) &&
2694 !isOpenMPSimdDirective(DKind: K));
2695 },
2696 Level) &&
2697 DSAStack->isTaskgroupReductionRef(VD: D, Level)))
2698 ? OMPC_private
2699 : OMPC_unknown;
2700}
2701
2702void SemaOpenMP::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
2703 unsigned Level) {
2704 assert(getLangOpts().OpenMP && "OpenMP is not allowed");
2705 D = getCanonicalDecl(D);
2706 OpenMPClauseKind OMPC = OMPC_unknown;
2707 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
2708 const unsigned NewLevel = I - 1;
2709 if (DSAStack->hasExplicitDSA(
2710 D,
2711 CPred: [&OMPC](const OpenMPClauseKind K, bool AppliedToPointee) {
2712 if (isOpenMPPrivate(Kind: K) && !AppliedToPointee) {
2713 OMPC = K;
2714 return true;
2715 }
2716 return false;
2717 },
2718 Level: NewLevel))
2719 break;
2720 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
2721 VD: D, Level: NewLevel,
2722 Check: [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
2723 OpenMPClauseKind) { return true; })) {
2724 OMPC = OMPC_map;
2725 break;
2726 }
2727 if (DSAStack->hasExplicitDirective(DPred: isOpenMPTargetExecutionDirective,
2728 Level: NewLevel)) {
2729 OMPC = OMPC_map;
2730 if (DSAStack->mustBeFirstprivateAtLevel(
2731 Level: NewLevel, Kind: getVariableCategoryFromDecl(LO: getLangOpts(), VD: D)))
2732 OMPC = OMPC_firstprivate;
2733 break;
2734 }
2735 }
2736 if (OMPC != OMPC_unknown)
2737 FD->addAttr(
2738 A: OMPCaptureKindAttr::CreateImplicit(Ctx&: getASTContext(), CaptureKindVal: unsigned(OMPC)));
2739}
2740
2741bool SemaOpenMP::isOpenMPTargetCapturedDecl(const ValueDecl *D, unsigned Level,
2742 unsigned CaptureLevel) const {
2743 assert(getLangOpts().OpenMP && "OpenMP is not allowed");
2744 // Return true if the current level is no longer enclosed in a target region.
2745
2746 SmallVector<OpenMPDirectiveKind, 4> Regions;
2747 getOpenMPCaptureRegions(CaptureRegions&: Regions, DSAStack->getDirective(Level));
2748 const auto *VD = dyn_cast<VarDecl>(Val: D);
2749 return VD && !VD->hasLocalStorage() &&
2750 DSAStack->hasExplicitDirective(DPred: isOpenMPTargetExecutionDirective,
2751 Level) &&
2752 Regions[CaptureLevel] != OMPD_task;
2753}
2754
2755bool SemaOpenMP::isOpenMPGlobalCapturedDecl(ValueDecl *D, unsigned Level,
2756 unsigned CaptureLevel) const {
2757 assert(getLangOpts().OpenMP && "OpenMP is not allowed");
2758 // Return true if the current level is no longer enclosed in a target region.
2759
2760 if (const auto *VD = dyn_cast<VarDecl>(Val: D)) {
2761 if (!VD->hasLocalStorage()) {
2762 if (isInOpenMPTargetExecutionDirective())
2763 return true;
2764 DSAStackTy::DSAVarData TopDVar =
2765 DSAStack->getTopDSA(D, /*FromParent=*/false);
2766 unsigned NumLevels =
2767 getOpenMPCaptureLevels(DSAStack->getDirective(Level));
2768 if (Level == 0)
2769 // non-file scope static variable with default(firstprivate)
2770 // should be global captured.
2771 return (NumLevels == CaptureLevel + 1 &&
2772 (TopDVar.CKind != OMPC_shared ||
2773 DSAStack->getDefaultDSA() == DSA_firstprivate));
2774 do {
2775 --Level;
2776 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level);
2777 if (DVar.CKind != OMPC_shared)
2778 return true;
2779 } while (Level > 0);
2780 }
2781 }
2782 return true;
2783}
2784
2785void SemaOpenMP::DestroyDataSharingAttributesStack() { delete DSAStack; }
2786
2787void SemaOpenMP::ActOnOpenMPBeginDeclareVariant(SourceLocation Loc,
2788 OMPTraitInfo &TI) {
2789 OMPDeclareVariantScopes.push_back(Elt: OMPDeclareVariantScope(TI));
2790}
2791
2792void SemaOpenMP::ActOnOpenMPEndDeclareVariant() {
2793 assert(isInOpenMPDeclareVariantScope() &&
2794 "Not in OpenMP declare variant scope!");
2795
2796 OMPDeclareVariantScopes.pop_back();
2797}
2798
2799void SemaOpenMP::finalizeOpenMPDelayedAnalysis(const FunctionDecl *Caller,
2800 const FunctionDecl *Callee,
2801 SourceLocation Loc) {
2802 assert(getLangOpts().OpenMP && "Expected OpenMP compilation mode.");
2803 std::optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
2804 OMPDeclareTargetDeclAttr::getDeviceType(VD: Caller->getMostRecentDecl());
2805 // Ignore host functions during device analysis.
2806 if (getLangOpts().OpenMPIsTargetDevice &&
2807 (!DevTy || *DevTy == OMPDeclareTargetDeclAttr::DT_Host))
2808 return;
2809 // Ignore nohost functions during host analysis.
2810 if (!getLangOpts().OpenMPIsTargetDevice && DevTy &&
2811 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost)
2812 return;
2813 const FunctionDecl *FD = Callee->getMostRecentDecl();
2814 DevTy = OMPDeclareTargetDeclAttr::getDeviceType(VD: FD);
2815 if (getLangOpts().OpenMPIsTargetDevice && DevTy &&
2816 *DevTy == OMPDeclareTargetDeclAttr::DT_Host) {
2817 // Diagnose host function called during device codegen.
2818 StringRef HostDevTy =
2819 getOpenMPSimpleClauseTypeName(Kind: OMPC_device_type, Type: OMPC_DEVICE_TYPE_host);
2820 Diag(Loc, DiagID: diag::err_omp_wrong_device_function_call) << HostDevTy << 0;
2821 Diag(Loc: *OMPDeclareTargetDeclAttr::getLocation(VD: FD),
2822 DiagID: diag::note_omp_marked_device_type_here)
2823 << HostDevTy;
2824 return;
2825 }
2826 if (!getLangOpts().OpenMPIsTargetDevice &&
2827 !getLangOpts().OpenMPOffloadMandatory && DevTy &&
2828 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) {
2829 // In OpenMP 5.2 or later, if the function has a host variant then allow
2830 // that to be called instead
2831 auto &&HasHostAttr = [](const FunctionDecl *Callee) {
2832 for (OMPDeclareVariantAttr *A :
2833 Callee->specific_attrs<OMPDeclareVariantAttr>()) {
2834 auto *DeclRefVariant = cast<DeclRefExpr>(Val: A->getVariantFuncRef());
2835 auto *VariantFD = cast<FunctionDecl>(Val: DeclRefVariant->getDecl());
2836 std::optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
2837 OMPDeclareTargetDeclAttr::getDeviceType(
2838 VD: VariantFD->getMostRecentDecl());
2839 if (!DevTy || *DevTy == OMPDeclareTargetDeclAttr::DT_Host)
2840 return true;
2841 }
2842 return false;
2843 };
2844 if (getLangOpts().OpenMP >= 52 &&
2845 Callee->hasAttr<OMPDeclareVariantAttr>() && HasHostAttr(Callee))
2846 return;
2847 // Diagnose nohost function called during host codegen.
2848 StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName(
2849 Kind: OMPC_device_type, Type: OMPC_DEVICE_TYPE_nohost);
2850 Diag(Loc, DiagID: diag::err_omp_wrong_device_function_call) << NoHostDevTy << 1;
2851 Diag(Loc: *OMPDeclareTargetDeclAttr::getLocation(VD: FD),
2852 DiagID: diag::note_omp_marked_device_type_here)
2853 << NoHostDevTy;
2854 }
2855}
2856
2857void SemaOpenMP::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
2858 const DeclarationNameInfo &DirName,
2859 Scope *CurScope, SourceLocation Loc) {
2860 DSAStack->push(DKind, DirName, CurScope, Loc);
2861 SemaRef.PushExpressionEvaluationContext(
2862 NewContext: Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
2863}
2864
2865void SemaOpenMP::StartOpenMPClause(OpenMPClauseKind K) {
2866 DSAStack->setClauseParsingMode(K);
2867}
2868
2869void SemaOpenMP::EndOpenMPClause() {
2870 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
2871 SemaRef.CleanupVarDeclMarking();
2872}
2873
2874static std::pair<ValueDecl *, bool>
2875getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
2876 SourceRange &ERange, bool AllowArraySection = false,
2877 bool AllowAssumedSizeArray = false, StringRef DiagType = "");
2878
2879/// Check consistency of the reduction clauses.
2880static void checkReductionClauses(Sema &S, DSAStackTy *Stack,
2881 ArrayRef<OMPClause *> Clauses) {
2882 bool InscanFound = false;
2883 SourceLocation InscanLoc;
2884 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions.
2885 // A reduction clause without the inscan reduction-modifier may not appear on
2886 // a construct on which a reduction clause with the inscan reduction-modifier
2887 // appears.
2888 for (OMPClause *C : Clauses) {
2889 if (C->getClauseKind() != OMPC_reduction)
2890 continue;
2891 auto *RC = cast<OMPReductionClause>(Val: C);
2892 if (RC->getModifier() == OMPC_REDUCTION_inscan) {
2893 InscanFound = true;
2894 InscanLoc = RC->getModifierLoc();
2895 continue;
2896 }
2897 if (RC->getModifier() == OMPC_REDUCTION_task) {
2898 // OpenMP 5.0, 2.19.5.4 reduction Clause.
2899 // A reduction clause with the task reduction-modifier may only appear on
2900 // a parallel construct, a worksharing construct or a combined or
2901 // composite construct for which any of the aforementioned constructs is a
2902 // constituent construct and simd or loop are not constituent constructs.
2903 OpenMPDirectiveKind CurDir = Stack->getCurrentDirective();
2904 if (!(isOpenMPParallelDirective(DKind: CurDir) ||
2905 isOpenMPWorksharingDirective(DKind: CurDir)) ||
2906 isOpenMPSimdDirective(DKind: CurDir))
2907 S.Diag(Loc: RC->getModifierLoc(),
2908 DiagID: diag::err_omp_reduction_task_not_parallel_or_worksharing);
2909 continue;
2910 }
2911 }
2912 if (InscanFound) {
2913 for (OMPClause *C : Clauses) {
2914 if (C->getClauseKind() != OMPC_reduction)
2915 continue;
2916 auto *RC = cast<OMPReductionClause>(Val: C);
2917 if (RC->getModifier() != OMPC_REDUCTION_inscan) {
2918 S.Diag(Loc: RC->getModifier() == OMPC_REDUCTION_unknown
2919 ? RC->getBeginLoc()
2920 : RC->getModifierLoc(),
2921 DiagID: diag::err_omp_inscan_reduction_expected);
2922 S.Diag(Loc: InscanLoc, DiagID: diag::note_omp_previous_inscan_reduction);
2923 continue;
2924 }
2925 for (Expr *Ref : RC->varlist()) {
2926 assert(Ref && "NULL expr in OpenMP reduction clause.");
2927 SourceLocation ELoc;
2928 SourceRange ERange;
2929 Expr *SimpleRefExpr = Ref;
2930 auto Res = getPrivateItem(S, RefExpr&: SimpleRefExpr, ELoc, ERange,
2931 /*AllowArraySection=*/true);
2932 ValueDecl *D = Res.first;
2933 if (!D)
2934 continue;
2935 if (!Stack->isUsedInScanDirective(D: getCanonicalDecl(D))) {
2936 S.Diag(Loc: Ref->getExprLoc(),
2937 DiagID: diag::err_omp_reduction_not_inclusive_exclusive)
2938 << Ref->getSourceRange();
2939 }
2940 }
2941 }
2942 }
2943}
2944
2945static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
2946 ArrayRef<OMPClause *> Clauses);
2947static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2948 bool WithInit);
2949
2950static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
2951 const ValueDecl *D,
2952 const DSAStackTy::DSAVarData &DVar,
2953 bool IsLoopIterVar = false);
2954
2955void SemaOpenMP::EndOpenMPDSABlock(Stmt *CurDirective) {
2956 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
2957 // A variable of class type (or array thereof) that appears in a lastprivate
2958 // clause requires an accessible, unambiguous default constructor for the
2959 // class type, unless the list item is also specified in a firstprivate
2960 // clause.
2961
2962 auto FinalizeLastprivate = [&](OMPLastprivateClause *Clause) {
2963 SmallVector<Expr *, 8> PrivateCopies;
2964 for (Expr *DE : Clause->varlist()) {
2965 if (DE->isValueDependent() || DE->isTypeDependent()) {
2966 PrivateCopies.push_back(Elt: nullptr);
2967 continue;
2968 }
2969 auto *DRE = cast<DeclRefExpr>(Val: DE->IgnoreParens());
2970 auto *VD = cast<VarDecl>(Val: DRE->getDecl());
2971 QualType Type = VD->getType().getNonReferenceType();
2972 const DSAStackTy::DSAVarData DVar =
2973 DSAStack->getTopDSA(D: VD, /*FromParent=*/false);
2974 if (DVar.CKind != OMPC_lastprivate) {
2975 // The variable is also a firstprivate, so initialization sequence
2976 // for private copy is generated already.
2977 PrivateCopies.push_back(Elt: nullptr);
2978 continue;
2979 }
2980 // Generate helper private variable and initialize it with the
2981 // default value. The address of the original variable is replaced
2982 // by the address of the new private variable in CodeGen. This new
2983 // variable is not added to IdResolver, so the code in the OpenMP
2984 // region uses original variable for proper diagnostics.
2985 VarDecl *VDPrivate = buildVarDecl(
2986 SemaRef, Loc: DE->getExprLoc(), Type: Type.getUnqualifiedType(), Name: VD->getName(),
2987 Attrs: VD->hasAttrs() ? &VD->getAttrs() : nullptr, OrigRef: DRE);
2988 SemaRef.ActOnUninitializedDecl(dcl: VDPrivate);
2989 if (VDPrivate->isInvalidDecl()) {
2990 PrivateCopies.push_back(Elt: nullptr);
2991 continue;
2992 }
2993 PrivateCopies.push_back(Elt: buildDeclRefExpr(
2994 S&: SemaRef, D: VDPrivate, Ty: DE->getType(), Loc: DE->getExprLoc()));
2995 }
2996 Clause->setPrivateCopies(PrivateCopies);
2997 };
2998
2999 auto FinalizeNontemporal = [&](OMPNontemporalClause *Clause) {
3000 // Finalize nontemporal clause by handling private copies, if any.
3001 SmallVector<Expr *, 8> PrivateRefs;
3002 for (Expr *RefExpr : Clause->varlist()) {
3003 assert(RefExpr && "NULL expr in OpenMP nontemporal clause.");
3004 SourceLocation ELoc;
3005 SourceRange ERange;
3006 Expr *SimpleRefExpr = RefExpr;
3007 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
3008 if (Res.second)
3009 // It will be analyzed later.
3010 PrivateRefs.push_back(Elt: RefExpr);
3011 ValueDecl *D = Res.first;
3012 if (!D)
3013 continue;
3014
3015 const DSAStackTy::DSAVarData DVar =
3016 DSAStack->getTopDSA(D, /*FromParent=*/false);
3017 PrivateRefs.push_back(Elt: DVar.PrivateCopy ? DVar.PrivateCopy
3018 : SimpleRefExpr);
3019 }
3020 Clause->setPrivateRefs(PrivateRefs);
3021 };
3022
3023 auto FinalizeAllocators = [&](OMPUsesAllocatorsClause *Clause) {
3024 for (unsigned I = 0, E = Clause->getNumberOfAllocators(); I < E; ++I) {
3025 OMPUsesAllocatorsClause::Data D = Clause->getAllocatorData(I);
3026 auto *DRE = dyn_cast<DeclRefExpr>(Val: D.Allocator->IgnoreParenImpCasts());
3027 if (!DRE)
3028 continue;
3029 ValueDecl *VD = DRE->getDecl();
3030 if (!VD || !isa<VarDecl>(Val: VD))
3031 continue;
3032 DSAStackTy::DSAVarData DVar =
3033 DSAStack->getTopDSA(D: VD, /*FromParent=*/false);
3034 // OpenMP [2.12.5, target Construct]
3035 // Memory allocators that appear in a uses_allocators clause cannot
3036 // appear in other data-sharing attribute clauses or data-mapping
3037 // attribute clauses in the same construct.
3038 Expr *MapExpr = nullptr;
3039 if (DVar.RefExpr ||
3040 DSAStack->checkMappableExprComponentListsForDecl(
3041 VD, /*CurrentRegionOnly=*/true,
3042 Check: [VD, &MapExpr](
3043 OMPClauseMappableExprCommon::MappableExprComponentListRef
3044 MapExprComponents,
3045 OpenMPClauseKind C) {
3046 auto MI = MapExprComponents.rbegin();
3047 auto ME = MapExprComponents.rend();
3048 if (MI != ME &&
3049 MI->getAssociatedDeclaration()->getCanonicalDecl() ==
3050 VD->getCanonicalDecl()) {
3051 MapExpr = MI->getAssociatedExpression();
3052 return true;
3053 }
3054 return false;
3055 })) {
3056 Diag(Loc: D.Allocator->getExprLoc(), DiagID: diag::err_omp_allocator_used_in_clauses)
3057 << D.Allocator->getSourceRange();
3058 if (DVar.RefExpr)
3059 reportOriginalDsa(SemaRef, DSAStack, D: VD, DVar);
3060 else
3061 Diag(Loc: MapExpr->getExprLoc(), DiagID: diag::note_used_here)
3062 << MapExpr->getSourceRange();
3063 }
3064 }
3065 };
3066
3067 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(Val: CurDirective)) {
3068 for (OMPClause *C : D->clauses()) {
3069 if (auto *Clause = dyn_cast<OMPLastprivateClause>(Val: C)) {
3070 FinalizeLastprivate(Clause);
3071 } else if (auto *Clause = dyn_cast<OMPNontemporalClause>(Val: C)) {
3072 FinalizeNontemporal(Clause);
3073 } else if (auto *Clause = dyn_cast<OMPUsesAllocatorsClause>(Val: C)) {
3074 FinalizeAllocators(Clause);
3075 }
3076 }
3077 // Check allocate clauses.
3078 if (!SemaRef.CurContext->isDependentContext())
3079 checkAllocateClauses(S&: SemaRef, DSAStack, Clauses: D->clauses());
3080 checkReductionClauses(S&: SemaRef, DSAStack, Clauses: D->clauses());
3081 }
3082
3083 DSAStack->pop();
3084 SemaRef.DiscardCleanupsInEvaluationContext();
3085 SemaRef.PopExpressionEvaluationContext();
3086}
3087
3088static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
3089 Expr *NumIterations, Sema &SemaRef,
3090 Scope *S, DSAStackTy *Stack);
3091
3092static bool finishLinearClauses(Sema &SemaRef, ArrayRef<OMPClause *> Clauses,
3093 OMPLoopBasedDirective::HelperExprs &B,
3094 DSAStackTy *Stack) {
3095 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
3096 "loop exprs were not built");
3097
3098 if (SemaRef.CurContext->isDependentContext())
3099 return false;
3100
3101 // Finalize the clauses that need pre-built expressions for CodeGen.
3102 for (OMPClause *C : Clauses) {
3103 auto *LC = dyn_cast<OMPLinearClause>(Val: C);
3104 if (!LC)
3105 continue;
3106 if (FinishOpenMPLinearClause(Clause&: *LC, IV: cast<DeclRefExpr>(Val: B.IterationVarRef),
3107 NumIterations: B.NumIterations, SemaRef,
3108 S: SemaRef.getCurScope(), Stack))
3109 return true;
3110 }
3111
3112 return false;
3113}
3114
3115namespace {
3116
3117class VarDeclFilterCCC final : public CorrectionCandidateCallback {
3118private:
3119 Sema &SemaRef;
3120
3121public:
3122 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
3123 bool ValidateCandidate(const TypoCorrection &Candidate) override {
3124 NamedDecl *ND = Candidate.getCorrectionDecl();
3125 if (const auto *VD = dyn_cast_or_null<VarDecl>(Val: ND)) {
3126 return VD->hasGlobalStorage() &&
3127 SemaRef.isDeclInScope(D: ND, Ctx: SemaRef.getCurLexicalContext(),
3128 S: SemaRef.getCurScope());
3129 }
3130 return false;
3131 }
3132
3133 std::unique_ptr<CorrectionCandidateCallback> clone() override {
3134 return std::make_unique<VarDeclFilterCCC>(args&: *this);
3135 }
3136};
3137
3138class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
3139private:
3140 Sema &SemaRef;
3141
3142public:
3143 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
3144 bool ValidateCandidate(const TypoCorrection &Candidate) override {
3145 NamedDecl *ND = Candidate.getCorrectionDecl();
3146 if (ND && ((isa<VarDecl>(Val: ND) && ND->getKind() == Decl::Var) ||
3147 isa<FunctionDecl>(Val: ND))) {
3148 return SemaRef.isDeclInScope(D: ND, Ctx: SemaRef.getCurLexicalContext(),
3149 S: SemaRef.getCurScope());
3150 }
3151 return false;
3152 }
3153
3154 std::unique_ptr<CorrectionCandidateCallback> clone() override {
3155 return std::make_unique<VarOrFuncDeclFilterCCC>(args&: *this);
3156 }
3157};
3158
3159} // namespace
3160
3161ExprResult SemaOpenMP::ActOnOpenMPIdExpression(Scope *CurScope,
3162 CXXScopeSpec &ScopeSpec,
3163 const DeclarationNameInfo &Id,
3164 OpenMPDirectiveKind Kind) {
3165 ASTContext &Context = getASTContext();
3166 unsigned OMPVersion = getLangOpts().OpenMP;
3167 LookupResult Lookup(SemaRef, Id, Sema::LookupOrdinaryName);
3168 SemaRef.LookupParsedName(R&: Lookup, S: CurScope, SS: &ScopeSpec,
3169 /*ObjectType=*/QualType(),
3170 /*AllowBuiltinCreation=*/true);
3171
3172 if (Lookup.isAmbiguous())
3173 return ExprError();
3174
3175 VarDecl *VD;
3176 if (!Lookup.isSingleResult()) {
3177 VarDeclFilterCCC CCC(SemaRef);
3178 if (TypoCorrection Corrected =
3179 SemaRef.CorrectTypo(Typo: Id, LookupKind: Sema::LookupOrdinaryName, S: CurScope, SS: nullptr,
3180 CCC, Mode: CorrectTypoKind::ErrorRecovery)) {
3181 SemaRef.diagnoseTypo(
3182 Correction: Corrected,
3183 TypoDiag: SemaRef.PDiag(DiagID: Lookup.empty() ? diag::err_undeclared_var_use_suggest
3184 : diag::err_omp_expected_var_arg_suggest)
3185 << Id.getName());
3186 VD = Corrected.getCorrectionDeclAs<VarDecl>();
3187 } else {
3188 Diag(Loc: Id.getLoc(), DiagID: Lookup.empty() ? diag::err_undeclared_var_use
3189 : diag::err_omp_expected_var_arg)
3190 << Id.getName();
3191 return ExprError();
3192 }
3193 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
3194 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_expected_var_arg) << Id.getName();
3195 Diag(Loc: Lookup.getFoundDecl()->getLocation(), DiagID: diag::note_declared_at);
3196 return ExprError();
3197 }
3198 Lookup.suppressDiagnostics();
3199
3200 // OpenMP [2.9.2, Syntax, C/C++]
3201 // Variables must be file-scope, namespace-scope, or static block-scope.
3202 if ((Kind == OMPD_threadprivate || Kind == OMPD_groupprivate) &&
3203 !VD->hasGlobalStorage()) {
3204 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_global_var_arg)
3205 << getOpenMPDirectiveName(D: Kind, Ver: OMPVersion) << !VD->isStaticLocal();
3206 bool IsDecl =
3207 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3208 Diag(Loc: VD->getLocation(),
3209 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3210 << VD;
3211 return ExprError();
3212 }
3213
3214 VarDecl *CanonicalVD = VD->getCanonicalDecl();
3215 NamedDecl *ND = CanonicalVD;
3216 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
3217 // A threadprivate or groupprivate directive for file-scope variables must
3218 // appear outside any definition or declaration.
3219 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
3220 !SemaRef.getCurLexicalContext()->isTranslationUnit()) {
3221 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_var_scope)
3222 << getOpenMPDirectiveName(D: Kind, Ver: OMPVersion) << VD;
3223 bool IsDecl =
3224 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3225 Diag(Loc: VD->getLocation(),
3226 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3227 << VD;
3228 return ExprError();
3229 }
3230 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
3231 // A threadprivate or groupprivate directive for static class member
3232 // variables must appear in the class definition, in the same scope in which
3233 // the member variables are declared.
3234 if (CanonicalVD->isStaticDataMember() &&
3235 !CanonicalVD->getDeclContext()->Equals(DC: SemaRef.getCurLexicalContext())) {
3236 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_var_scope)
3237 << getOpenMPDirectiveName(D: Kind, Ver: OMPVersion) << VD;
3238 bool IsDecl =
3239 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3240 Diag(Loc: VD->getLocation(),
3241 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3242 << VD;
3243 return ExprError();
3244 }
3245 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
3246 // A threadprivate or groupprivate directive for namespace-scope variables
3247 // must appear outside any definition or declaration other than the
3248 // namespace definition itself.
3249 if (CanonicalVD->getDeclContext()->isNamespace() &&
3250 (!SemaRef.getCurLexicalContext()->isFileContext() ||
3251 !SemaRef.getCurLexicalContext()->Encloses(
3252 DC: CanonicalVD->getDeclContext()))) {
3253 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_var_scope)
3254 << getOpenMPDirectiveName(D: Kind, Ver: OMPVersion) << VD;
3255 bool IsDecl =
3256 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3257 Diag(Loc: VD->getLocation(),
3258 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3259 << VD;
3260 return ExprError();
3261 }
3262 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
3263 // A threadprivate or groupprivate directive for static block-scope
3264 // variables must appear in the scope of the variable and not in a nested
3265 // scope.
3266 if (CanonicalVD->isLocalVarDecl() && CurScope &&
3267 !SemaRef.isDeclInScope(D: ND, Ctx: SemaRef.getCurLexicalContext(), S: CurScope)) {
3268 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_var_scope)
3269 << getOpenMPDirectiveName(D: Kind, Ver: OMPVersion) << VD;
3270 bool IsDecl =
3271 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3272 Diag(Loc: VD->getLocation(),
3273 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3274 << VD;
3275 return ExprError();
3276 }
3277
3278 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
3279 // A threadprivate or groupprivate directive must lexically precede all
3280 // references to any of the variables in its list.
3281 if ((Kind == OMPD_threadprivate && VD->isUsed() &&
3282 !DSAStack->isThreadPrivate(D: VD)) ||
3283 (Kind == OMPD_groupprivate && VD->isUsed())) {
3284 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_var_used)
3285 << getOpenMPDirectiveName(D: Kind, Ver: OMPVersion) << VD;
3286 return ExprError();
3287 }
3288
3289 QualType ExprType = VD->getType().getNonReferenceType();
3290 return DeclRefExpr::Create(Context, QualifierLoc: NestedNameSpecifierLoc(),
3291 TemplateKWLoc: SourceLocation(), D: VD,
3292 /*RefersToEnclosingVariableOrCapture=*/false,
3293 NameLoc: Id.getLoc(), T: ExprType, VK: VK_LValue);
3294}
3295
3296SemaOpenMP::DeclGroupPtrTy
3297SemaOpenMP::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
3298 ArrayRef<Expr *> VarList) {
3299 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
3300 SemaRef.CurContext->addDecl(D);
3301 return DeclGroupPtrTy::make(P: DeclGroupRef(D));
3302 }
3303 return nullptr;
3304}
3305
3306SemaOpenMP::DeclGroupPtrTy
3307SemaOpenMP::ActOnOpenMPGroupPrivateDirective(SourceLocation Loc,
3308 ArrayRef<Expr *> VarList) {
3309 if (!getLangOpts().OpenMP || getLangOpts().OpenMP < 60) {
3310 Diag(Loc, DiagID: diag::err_omp_unexpected_directive)
3311 << getOpenMPDirectiveName(D: OMPD_groupprivate, Ver: getLangOpts().OpenMP);
3312 return nullptr;
3313 }
3314 if (OMPGroupPrivateDecl *D = CheckOMPGroupPrivateDecl(Loc, VarList)) {
3315 SemaRef.CurContext->addDecl(D);
3316 return DeclGroupPtrTy::make(P: DeclGroupRef(D));
3317 }
3318 return nullptr;
3319}
3320
3321namespace {
3322class LocalVarRefChecker final
3323 : public ConstStmtVisitor<LocalVarRefChecker, bool> {
3324 Sema &SemaRef;
3325
3326public:
3327 bool VisitDeclRefExpr(const DeclRefExpr *E) {
3328 if (const auto *VD = dyn_cast<VarDecl>(Val: E->getDecl())) {
3329 if (VD->hasLocalStorage()) {
3330 SemaRef.Diag(Loc: E->getBeginLoc(),
3331 DiagID: diag::err_omp_local_var_in_threadprivate_init)
3332 << E->getSourceRange();
3333 SemaRef.Diag(Loc: VD->getLocation(), DiagID: diag::note_defined_here)
3334 << VD << VD->getSourceRange();
3335 return true;
3336 }
3337 }
3338 return false;
3339 }
3340 bool VisitStmt(const Stmt *S) {
3341 for (const Stmt *Child : S->children()) {
3342 if (Child && Visit(S: Child))
3343 return true;
3344 }
3345 return false;
3346 }
3347 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
3348};
3349} // namespace
3350
3351OMPThreadPrivateDecl *
3352SemaOpenMP::CheckOMPThreadPrivateDecl(SourceLocation Loc,
3353 ArrayRef<Expr *> VarList) {
3354 ASTContext &Context = getASTContext();
3355 SmallVector<Expr *, 8> Vars;
3356 for (Expr *RefExpr : VarList) {
3357 auto *DE = cast<DeclRefExpr>(Val: RefExpr);
3358 auto *VD = cast<VarDecl>(Val: DE->getDecl());
3359 SourceLocation ILoc = DE->getExprLoc();
3360
3361 // Mark variable as used.
3362 VD->setReferenced();
3363 VD->markUsed(C&: Context);
3364
3365 QualType QType = VD->getType();
3366 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
3367 // It will be analyzed later.
3368 Vars.push_back(Elt: DE);
3369 continue;
3370 }
3371
3372 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
3373 // A threadprivate variable must not have an incomplete type.
3374 if (SemaRef.RequireCompleteType(
3375 Loc: ILoc, T: VD->getType(), DiagID: diag::err_omp_threadprivate_incomplete_type)) {
3376 continue;
3377 }
3378
3379 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
3380 // A threadprivate variable must not have a reference type.
3381 if (VD->getType()->isReferenceType()) {
3382 unsigned OMPVersion = getLangOpts().OpenMP;
3383 Diag(Loc: ILoc, DiagID: diag::err_omp_ref_type_arg)
3384 << getOpenMPDirectiveName(D: OMPD_threadprivate, Ver: OMPVersion)
3385 << VD->getType();
3386 bool IsDecl =
3387 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3388 Diag(Loc: VD->getLocation(),
3389 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3390 << VD;
3391 continue;
3392 }
3393
3394 // Check if this is a TLS variable. If TLS is not being supported, produce
3395 // the corresponding diagnostic.
3396 if ((VD->getTLSKind() != VarDecl::TLS_None &&
3397 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
3398 getLangOpts().OpenMPUseTLS &&
3399 getASTContext().getTargetInfo().isTLSSupported())) ||
3400 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
3401 !VD->isLocalVarDecl())) {
3402 Diag(Loc: ILoc, DiagID: diag::err_omp_var_thread_local)
3403 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
3404 bool IsDecl =
3405 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3406 Diag(Loc: VD->getLocation(),
3407 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3408 << VD;
3409 continue;
3410 }
3411
3412 // Check if initial value of threadprivate variable reference variable with
3413 // local storage (it is not supported by runtime).
3414 if (const Expr *Init = VD->getAnyInitializer()) {
3415 LocalVarRefChecker Checker(SemaRef);
3416 if (Checker.Visit(S: Init))
3417 continue;
3418 }
3419
3420 Vars.push_back(Elt: RefExpr);
3421 DSAStack->addDSA(D: VD, E: DE, A: OMPC_threadprivate);
3422 VD->addAttr(A: OMPThreadPrivateDeclAttr::CreateImplicit(
3423 Ctx&: Context, Range: SourceRange(Loc, Loc)));
3424 if (ASTMutationListener *ML = Context.getASTMutationListener())
3425 ML->DeclarationMarkedOpenMPThreadPrivate(D: VD);
3426 }
3427 OMPThreadPrivateDecl *D = nullptr;
3428 if (!Vars.empty()) {
3429 D = OMPThreadPrivateDecl::Create(C&: Context, DC: SemaRef.getCurLexicalContext(),
3430 L: Loc, VL: Vars);
3431 D->setAccess(AS_public);
3432 }
3433 return D;
3434}
3435
3436OMPGroupPrivateDecl *
3437SemaOpenMP::CheckOMPGroupPrivateDecl(SourceLocation Loc,
3438 ArrayRef<Expr *> VarList) {
3439 ASTContext &Context = getASTContext();
3440 SmallVector<Expr *, 8> Vars;
3441 for (Expr *RefExpr : VarList) {
3442 auto *DE = cast<DeclRefExpr>(Val: RefExpr);
3443 auto *VD = cast<VarDecl>(Val: DE->getDecl());
3444 SourceLocation ILoc = DE->getExprLoc();
3445
3446 // Mark variable as used.
3447 VD->setReferenced();
3448 VD->markUsed(C&: Context);
3449
3450 QualType QType = VD->getType();
3451 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
3452 // It will be analyzed later.
3453 Vars.push_back(Elt: DE);
3454 continue;
3455 }
3456
3457 // OpenMP groupprivate restrictions:
3458 // A groupprivate variable must not have an incomplete type.
3459 if (SemaRef.RequireCompleteType(
3460 Loc: ILoc, T: VD->getType(), DiagID: diag::err_omp_groupprivate_incomplete_type)) {
3461 continue;
3462 }
3463
3464 // A groupprivate variable must not have a reference type.
3465 if (VD->getType()->isReferenceType()) {
3466 Diag(Loc: ILoc, DiagID: diag::err_omp_ref_type_arg)
3467 << getOpenMPDirectiveName(D: OMPD_groupprivate) << VD->getType();
3468 bool IsDecl =
3469 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3470 Diag(Loc: VD->getLocation(),
3471 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3472 << VD;
3473 continue;
3474 }
3475
3476 // A variable that is declared with an initializer must not appear in a
3477 // groupprivate directive.
3478 if (VD->getAnyInitializer()) {
3479 Diag(Loc: ILoc, DiagID: diag::err_omp_groupprivate_with_initializer)
3480 << VD->getDeclName();
3481 bool IsDecl =
3482 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3483 Diag(Loc: VD->getLocation(),
3484 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3485 << VD;
3486 continue;
3487 }
3488
3489 Vars.push_back(Elt: RefExpr);
3490 DSAStack->addDSA(D: VD, E: DE, A: OMPC_groupprivate);
3491 VD->addAttr(A: OMPGroupPrivateDeclAttr::CreateImplicit(Ctx&: Context,
3492 Range: SourceRange(Loc, Loc)));
3493 if (ASTMutationListener *ML = Context.getASTMutationListener())
3494 ML->DeclarationMarkedOpenMPGroupPrivate(D: VD);
3495 }
3496 OMPGroupPrivateDecl *D = nullptr;
3497 if (!Vars.empty()) {
3498 D = OMPGroupPrivateDecl::Create(C&: Context, DC: SemaRef.getCurLexicalContext(),
3499 L: Loc, VL: Vars);
3500 D->setAccess(AS_public);
3501 }
3502 return D;
3503}
3504
3505static OMPAllocateDeclAttr::AllocatorTypeTy
3506getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) {
3507 if (!Allocator)
3508 return OMPAllocateDeclAttr::OMPNullMemAlloc;
3509 if (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
3510 Allocator->isInstantiationDependent() ||
3511 Allocator->containsUnexpandedParameterPack())
3512 return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
3513 auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
3514 llvm::FoldingSetNodeID AEId;
3515 const Expr *AE = Allocator->IgnoreParenImpCasts();
3516 AE->IgnoreImpCasts()->Profile(ID&: AEId, Context: S.getASTContext(), /*Canonical=*/true);
3517 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
3518 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
3519 const Expr *DefAllocator = Stack->getAllocator(AllocatorKind);
3520 llvm::FoldingSetNodeID DAEId;
3521 DefAllocator->IgnoreImpCasts()->Profile(ID&: DAEId, Context: S.getASTContext(),
3522 /*Canonical=*/true);
3523 if (AEId == DAEId) {
3524 AllocatorKindRes = AllocatorKind;
3525 break;
3526 }
3527 }
3528 return AllocatorKindRes;
3529}
3530
3531static bool checkPreviousOMPAllocateAttribute(
3532 Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD,
3533 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) {
3534 if (!VD->hasAttr<OMPAllocateDeclAttr>())
3535 return false;
3536 const auto *A = VD->getAttr<OMPAllocateDeclAttr>();
3537 Expr *PrevAllocator = A->getAllocator();
3538 OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind =
3539 getAllocatorKind(S, Stack, Allocator: PrevAllocator);
3540 bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind;
3541 if (AllocatorsMatch &&
3542 AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc &&
3543 Allocator && PrevAllocator) {
3544 const Expr *AE = Allocator->IgnoreParenImpCasts();
3545 const Expr *PAE = PrevAllocator->IgnoreParenImpCasts();
3546 llvm::FoldingSetNodeID AEId, PAEId;
3547 AE->Profile(ID&: AEId, Context: S.Context, /*Canonical=*/true);
3548 PAE->Profile(ID&: PAEId, Context: S.Context, /*Canonical=*/true);
3549 AllocatorsMatch = AEId == PAEId;
3550 }
3551 if (!AllocatorsMatch) {
3552 SmallString<256> AllocatorBuffer;
3553 llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer);
3554 if (Allocator)
3555 Allocator->printPretty(OS&: AllocatorStream, Helper: nullptr, Policy: S.getPrintingPolicy());
3556 SmallString<256> PrevAllocatorBuffer;
3557 llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer);
3558 if (PrevAllocator)
3559 PrevAllocator->printPretty(OS&: PrevAllocatorStream, Helper: nullptr,
3560 Policy: S.getPrintingPolicy());
3561
3562 SourceLocation AllocatorLoc =
3563 Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc();
3564 SourceRange AllocatorRange =
3565 Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange();
3566 SourceLocation PrevAllocatorLoc =
3567 PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation();
3568 SourceRange PrevAllocatorRange =
3569 PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange();
3570 S.Diag(Loc: AllocatorLoc, DiagID: diag::warn_omp_used_different_allocator)
3571 << (Allocator ? 1 : 0) << AllocatorStream.str()
3572 << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str()
3573 << AllocatorRange;
3574 S.Diag(Loc: PrevAllocatorLoc, DiagID: diag::note_omp_previous_allocator)
3575 << PrevAllocatorRange;
3576 return true;
3577 }
3578 return false;
3579}
3580
3581static void
3582applyOMPAllocateAttribute(Sema &S, VarDecl *VD,
3583 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
3584 Expr *Allocator, Expr *Alignment, SourceRange SR) {
3585 if (VD->hasAttr<OMPAllocateDeclAttr>())
3586 return;
3587 if (Alignment &&
3588 (Alignment->isTypeDependent() || Alignment->isValueDependent() ||
3589 Alignment->isInstantiationDependent() ||
3590 Alignment->containsUnexpandedParameterPack()))
3591 // Apply later when we have a usable value.
3592 return;
3593 if (Allocator &&
3594 (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
3595 Allocator->isInstantiationDependent() ||
3596 Allocator->containsUnexpandedParameterPack()))
3597 return;
3598 auto *A = OMPAllocateDeclAttr::CreateImplicit(Ctx&: S.Context, AllocatorType: AllocatorKind,
3599 Allocator, Alignment, Range: SR);
3600 VD->addAttr(A);
3601 if (ASTMutationListener *ML = S.Context.getASTMutationListener())
3602 ML->DeclarationMarkedOpenMPAllocate(D: VD, A);
3603}
3604
3605SemaOpenMP::DeclGroupPtrTy SemaOpenMP::ActOnOpenMPAllocateDirective(
3606 SourceLocation Loc, ArrayRef<Expr *> VarList, ArrayRef<OMPClause *> Clauses,
3607 DeclContext *Owner) {
3608 assert(Clauses.size() <= 2 && "Expected at most two clauses.");
3609 Expr *Alignment = nullptr;
3610 Expr *Allocator = nullptr;
3611 if (Clauses.empty()) {
3612 // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions.
3613 // allocate directives that appear in a target region must specify an
3614 // allocator clause unless a requires directive with the dynamic_allocators
3615 // clause is present in the same compilation unit.
3616 if (getLangOpts().OpenMPIsTargetDevice &&
3617 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
3618 SemaRef.targetDiag(Loc, DiagID: diag::err_expected_allocator_clause);
3619 } else {
3620 for (const OMPClause *C : Clauses)
3621 if (const auto *AC = dyn_cast<OMPAllocatorClause>(Val: C))
3622 Allocator = AC->getAllocator();
3623 else if (const auto *AC = dyn_cast<OMPAlignClause>(Val: C))
3624 Alignment = AC->getAlignment();
3625 else
3626 llvm_unreachable("Unexpected clause on allocate directive");
3627 }
3628 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
3629 getAllocatorKind(S&: SemaRef, DSAStack, Allocator);
3630 SmallVector<Expr *, 8> Vars;
3631 for (Expr *RefExpr : VarList) {
3632 auto *DE = cast<DeclRefExpr>(Val: RefExpr);
3633 auto *VD = cast<VarDecl>(Val: DE->getDecl());
3634
3635 // Check if this is a TLS variable or global register.
3636 if (VD->getTLSKind() != VarDecl::TLS_None ||
3637 VD->hasAttr<OMPThreadPrivateDeclAttr>() ||
3638 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
3639 !VD->isLocalVarDecl()))
3640 continue;
3641
3642 // If the used several times in the allocate directive, the same allocator
3643 // must be used.
3644 if (checkPreviousOMPAllocateAttribute(S&: SemaRef, DSAStack, RefExpr, VD,
3645 AllocatorKind, Allocator))
3646 continue;
3647
3648 // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++
3649 // If a list item has a static storage type, the allocator expression in the
3650 // allocator clause must be a constant expression that evaluates to one of
3651 // the predefined memory allocator values.
3652 if (Allocator && VD->hasGlobalStorage()) {
3653 if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) {
3654 Diag(Loc: Allocator->getExprLoc(),
3655 DiagID: diag::err_omp_expected_predefined_allocator)
3656 << Allocator->getSourceRange();
3657 bool IsDecl = VD->isThisDeclarationADefinition(getASTContext()) ==
3658 VarDecl::DeclarationOnly;
3659 Diag(Loc: VD->getLocation(),
3660 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3661 << VD;
3662 continue;
3663 }
3664 }
3665
3666 Vars.push_back(Elt: RefExpr);
3667 applyOMPAllocateAttribute(S&: SemaRef, VD, AllocatorKind, Allocator, Alignment,
3668 SR: DE->getSourceRange());
3669 }
3670 if (Vars.empty())
3671 return nullptr;
3672 if (!Owner)
3673 Owner = SemaRef.getCurLexicalContext();
3674 auto *D = OMPAllocateDecl::Create(C&: getASTContext(), DC: Owner, L: Loc, VL: Vars, CL: Clauses);
3675 D->setAccess(AS_public);
3676 Owner->addDecl(D);
3677 return DeclGroupPtrTy::make(P: DeclGroupRef(D));
3678}
3679
3680SemaOpenMP::DeclGroupPtrTy
3681SemaOpenMP::ActOnOpenMPRequiresDirective(SourceLocation Loc,
3682 ArrayRef<OMPClause *> ClauseList) {
3683 OMPRequiresDecl *D = nullptr;
3684 if (!SemaRef.CurContext->isFileContext()) {
3685 Diag(Loc, DiagID: diag::err_omp_invalid_scope) << "requires";
3686 } else {
3687 D = CheckOMPRequiresDecl(Loc, Clauses: ClauseList);
3688 if (D) {
3689 SemaRef.CurContext->addDecl(D);
3690 DSAStack->addRequiresDecl(RD: D);
3691 }
3692 }
3693 return DeclGroupPtrTy::make(P: DeclGroupRef(D));
3694}
3695
3696void SemaOpenMP::ActOnOpenMPAssumesDirective(SourceLocation Loc,
3697 OpenMPDirectiveKind DKind,
3698 ArrayRef<std::string> Assumptions,
3699 bool SkippedClauses) {
3700 if (!SkippedClauses && Assumptions.empty()) {
3701 unsigned OMPVersion = getLangOpts().OpenMP;
3702 Diag(Loc, DiagID: diag::err_omp_no_clause_for_directive)
3703 << llvm::omp::getAllAssumeClauseOptions()
3704 << llvm::omp::getOpenMPDirectiveName(D: DKind, Ver: OMPVersion);
3705 }
3706
3707 auto *AA =
3708 OMPAssumeAttr::Create(Ctx&: getASTContext(), Assumption: llvm::join(R&: Assumptions, Separator: ","), Range: Loc);
3709 if (DKind == llvm::omp::Directive::OMPD_begin_assumes) {
3710 OMPAssumeScoped.push_back(Elt: AA);
3711 return;
3712 }
3713
3714 // Global assumes without assumption clauses are ignored.
3715 if (Assumptions.empty())
3716 return;
3717
3718 assert(DKind == llvm::omp::Directive::OMPD_assumes &&
3719 "Unexpected omp assumption directive!");
3720 OMPAssumeGlobal.push_back(Elt: AA);
3721
3722 // The OMPAssumeGlobal scope above will take care of new declarations but
3723 // we also want to apply the assumption to existing ones, e.g., to
3724 // declarations in included headers. To this end, we traverse all existing
3725 // declaration contexts and annotate function declarations here.
3726 SmallVector<DeclContext *, 8> DeclContexts;
3727 auto *Ctx = SemaRef.CurContext;
3728 while (Ctx->getLexicalParent())
3729 Ctx = Ctx->getLexicalParent();
3730 DeclContexts.push_back(Elt: Ctx);
3731 while (!DeclContexts.empty()) {
3732 DeclContext *DC = DeclContexts.pop_back_val();
3733 for (auto *SubDC : DC->decls()) {
3734 if (SubDC->isInvalidDecl())
3735 continue;
3736 if (auto *CTD = dyn_cast<ClassTemplateDecl>(Val: SubDC)) {
3737 DeclContexts.push_back(Elt: CTD->getTemplatedDecl());
3738 llvm::append_range(C&: DeclContexts, R: CTD->specializations());
3739 continue;
3740 }
3741 if (auto *DC = dyn_cast<DeclContext>(Val: SubDC))
3742 DeclContexts.push_back(Elt: DC);
3743 if (auto *F = dyn_cast<FunctionDecl>(Val: SubDC)) {
3744 F->addAttr(A: AA);
3745 continue;
3746 }
3747 }
3748 }
3749}
3750
3751void SemaOpenMP::ActOnOpenMPEndAssumesDirective() {
3752 assert(isInOpenMPAssumeScope() && "Not in OpenMP assumes scope!");
3753 OMPAssumeScoped.pop_back();
3754}
3755
3756StmtResult SemaOpenMP::ActOnOpenMPAssumeDirective(ArrayRef<OMPClause *> Clauses,
3757 Stmt *AStmt,
3758 SourceLocation StartLoc,
3759 SourceLocation EndLoc) {
3760 if (!AStmt)
3761 return StmtError();
3762
3763 return OMPAssumeDirective::Create(Ctx: getASTContext(), StartLoc, EndLoc, Clauses,
3764 AStmt);
3765}
3766
3767OMPRequiresDecl *
3768SemaOpenMP::CheckOMPRequiresDecl(SourceLocation Loc,
3769 ArrayRef<OMPClause *> ClauseList) {
3770 /// For target specific clauses, the requires directive cannot be
3771 /// specified after the handling of any of the target regions in the
3772 /// current compilation unit.
3773 ArrayRef<SourceLocation> TargetLocations =
3774 DSAStack->getEncounteredTargetLocs();
3775 SourceLocation AtomicLoc = DSAStack->getAtomicDirectiveLoc();
3776 if (!TargetLocations.empty() || !AtomicLoc.isInvalid()) {
3777 for (const OMPClause *CNew : ClauseList) {
3778 // Check if any of the requires clauses affect target regions.
3779 if (isa<OMPUnifiedSharedMemoryClause>(Val: CNew) ||
3780 isa<OMPUnifiedAddressClause>(Val: CNew) ||
3781 isa<OMPReverseOffloadClause>(Val: CNew) ||
3782 isa<OMPDynamicAllocatorsClause>(Val: CNew)) {
3783 Diag(Loc, DiagID: diag::err_omp_directive_before_requires)
3784 << "target" << getOpenMPClauseNameForDiag(C: CNew->getClauseKind());
3785 for (SourceLocation TargetLoc : TargetLocations) {
3786 Diag(Loc: TargetLoc, DiagID: diag::note_omp_requires_encountered_directive)
3787 << "target";
3788 }
3789 } else if (!AtomicLoc.isInvalid() &&
3790 isa<OMPAtomicDefaultMemOrderClause>(Val: CNew)) {
3791 Diag(Loc, DiagID: diag::err_omp_directive_before_requires)
3792 << "atomic" << getOpenMPClauseNameForDiag(C: CNew->getClauseKind());
3793 Diag(Loc: AtomicLoc, DiagID: diag::note_omp_requires_encountered_directive)
3794 << "atomic";
3795 }
3796 }
3797 }
3798
3799 if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
3800 return OMPRequiresDecl::Create(
3801 C&: getASTContext(), DC: SemaRef.getCurLexicalContext(), L: Loc, CL: ClauseList);
3802 return nullptr;
3803}
3804
3805static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
3806 const ValueDecl *D,
3807 const DSAStackTy::DSAVarData &DVar,
3808 bool IsLoopIterVar) {
3809 if (DVar.RefExpr) {
3810 SemaRef.Diag(Loc: DVar.RefExpr->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
3811 << getOpenMPClauseNameForDiag(C: DVar.CKind);
3812 return;
3813 }
3814 enum {
3815 PDSA_StaticMemberShared,
3816 PDSA_StaticLocalVarShared,
3817 PDSA_LoopIterVarPrivate,
3818 PDSA_LoopIterVarLinear,
3819 PDSA_LoopIterVarLastprivate,
3820 PDSA_ConstVarShared,
3821 PDSA_GlobalVarShared,
3822 PDSA_TaskVarFirstprivate,
3823 PDSA_LocalVarPrivate,
3824 PDSA_Implicit
3825 } Reason = PDSA_Implicit;
3826 bool ReportHint = false;
3827 auto ReportLoc = D->getLocation();
3828 auto *VD = dyn_cast<VarDecl>(Val: D);
3829 if (IsLoopIterVar) {
3830 if (DVar.CKind == OMPC_private)
3831 Reason = PDSA_LoopIterVarPrivate;
3832 else if (DVar.CKind == OMPC_lastprivate)
3833 Reason = PDSA_LoopIterVarLastprivate;
3834 else
3835 Reason = PDSA_LoopIterVarLinear;
3836 } else if (isOpenMPTaskingDirective(Kind: DVar.DKind) &&
3837 DVar.CKind == OMPC_firstprivate) {
3838 Reason = PDSA_TaskVarFirstprivate;
3839 ReportLoc = DVar.ImplicitDSALoc;
3840 } else if (VD && VD->isStaticLocal())
3841 Reason = PDSA_StaticLocalVarShared;
3842 else if (VD && VD->isStaticDataMember())
3843 Reason = PDSA_StaticMemberShared;
3844 else if (VD && VD->isFileVarDecl())
3845 Reason = PDSA_GlobalVarShared;
3846 else if (D->getType().isConstant(Ctx: SemaRef.getASTContext()))
3847 Reason = PDSA_ConstVarShared;
3848 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
3849 ReportHint = true;
3850 Reason = PDSA_LocalVarPrivate;
3851 }
3852 if (Reason != PDSA_Implicit) {
3853 unsigned OMPVersion = SemaRef.getLangOpts().OpenMP;
3854 SemaRef.Diag(Loc: ReportLoc, DiagID: diag::note_omp_predetermined_dsa)
3855 << Reason << ReportHint
3856 << getOpenMPDirectiveName(D: Stack->getCurrentDirective(), Ver: OMPVersion);
3857 } else if (DVar.ImplicitDSALoc.isValid()) {
3858 SemaRef.Diag(Loc: DVar.ImplicitDSALoc, DiagID: diag::note_omp_implicit_dsa)
3859 << getOpenMPClauseNameForDiag(C: DVar.CKind);
3860 }
3861}
3862
3863static OpenMPMapClauseKind
3864getMapClauseKindFromModifier(OpenMPDefaultmapClauseModifier M,
3865 bool IsAggregateOrDeclareTarget) {
3866 OpenMPMapClauseKind Kind = OMPC_MAP_unknown;
3867 switch (M) {
3868 case OMPC_DEFAULTMAP_MODIFIER_alloc:
3869 case OMPC_DEFAULTMAP_MODIFIER_storage:
3870 Kind = OMPC_MAP_alloc;
3871 break;
3872 case OMPC_DEFAULTMAP_MODIFIER_to:
3873 Kind = OMPC_MAP_to;
3874 break;
3875 case OMPC_DEFAULTMAP_MODIFIER_from:
3876 Kind = OMPC_MAP_from;
3877 break;
3878 case OMPC_DEFAULTMAP_MODIFIER_tofrom:
3879 Kind = OMPC_MAP_tofrom;
3880 break;
3881 case OMPC_DEFAULTMAP_MODIFIER_present:
3882 // OpenMP 5.1 [2.21.7.3] defaultmap clause, Description]
3883 // If implicit-behavior is present, each variable referenced in the
3884 // construct in the category specified by variable-category is treated as if
3885 // it had been listed in a map clause with the map-type of alloc and
3886 // map-type-modifier of present.
3887 Kind = OMPC_MAP_alloc;
3888 break;
3889 case OMPC_DEFAULTMAP_MODIFIER_firstprivate:
3890 case OMPC_DEFAULTMAP_MODIFIER_private:
3891 case OMPC_DEFAULTMAP_MODIFIER_last:
3892 llvm_unreachable("Unexpected defaultmap implicit behavior");
3893 case OMPC_DEFAULTMAP_MODIFIER_none:
3894 case OMPC_DEFAULTMAP_MODIFIER_default:
3895 case OMPC_DEFAULTMAP_MODIFIER_unknown:
3896 // IsAggregateOrDeclareTarget could be true if:
3897 // 1. the implicit behavior for aggregate is tofrom
3898 // 2. it's a declare target link
3899 if (IsAggregateOrDeclareTarget) {
3900 Kind = OMPC_MAP_tofrom;
3901 break;
3902 }
3903 llvm_unreachable("Unexpected defaultmap implicit behavior");
3904 }
3905 assert(Kind != OMPC_MAP_unknown && "Expect map kind to be known");
3906 return Kind;
3907}
3908
3909namespace {
3910struct VariableImplicitInfo {
3911 static const unsigned MapKindNum = OMPC_MAP_unknown;
3912 static const unsigned DefaultmapKindNum = OMPC_DEFAULTMAP_unknown + 1;
3913
3914 llvm::SetVector<Expr *> Privates;
3915 llvm::SetVector<Expr *> Firstprivates;
3916 llvm::SetVector<Expr *> Mappings[DefaultmapKindNum][MapKindNum];
3917 llvm::SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers>
3918 MapModifiers[DefaultmapKindNum];
3919};
3920
3921class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
3922 DSAStackTy *Stack;
3923 Sema &SemaRef;
3924 OpenMPDirectiveKind DKind = OMPD_unknown;
3925 bool ErrorFound = false;
3926 bool TryCaptureCXXThisMembers = false;
3927 CapturedStmt *CS = nullptr;
3928
3929 VariableImplicitInfo ImpInfo;
3930 SemaOpenMP::VarsWithInheritedDSAType VarsWithInheritedDSA;
3931 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
3932
3933 void VisitSubCaptures(OMPExecutableDirective *S) {
3934 // Check implicitly captured variables.
3935 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt())
3936 return;
3937 if (S->getDirectiveKind() == OMPD_atomic ||
3938 S->getDirectiveKind() == OMPD_critical ||
3939 S->getDirectiveKind() == OMPD_section ||
3940 S->getDirectiveKind() == OMPD_master ||
3941 S->getDirectiveKind() == OMPD_masked ||
3942 S->getDirectiveKind() == OMPD_scope ||
3943 S->getDirectiveKind() == OMPD_assume ||
3944 isOpenMPLoopTransformationDirective(DKind: S->getDirectiveKind())) {
3945 Visit(S: S->getAssociatedStmt());
3946 return;
3947 }
3948 visitSubCaptures(S: S->getInnermostCapturedStmt());
3949 // Try to capture inner this->member references to generate correct mappings
3950 // and diagnostics.
3951 if (TryCaptureCXXThisMembers ||
3952 (isOpenMPTargetExecutionDirective(DKind) &&
3953 llvm::any_of(Range: S->getInnermostCapturedStmt()->captures(),
3954 P: [](const CapturedStmt::Capture &C) {
3955 return C.capturesThis();
3956 }))) {
3957 bool SavedTryCaptureCXXThisMembers = TryCaptureCXXThisMembers;
3958 TryCaptureCXXThisMembers = true;
3959 Visit(S: S->getInnermostCapturedStmt()->getCapturedStmt());
3960 TryCaptureCXXThisMembers = SavedTryCaptureCXXThisMembers;
3961 }
3962 // In tasks firstprivates are not captured anymore, need to analyze them
3963 // explicitly.
3964 if (isOpenMPTaskingDirective(Kind: S->getDirectiveKind()) &&
3965 !isOpenMPTaskLoopDirective(DKind: S->getDirectiveKind())) {
3966 for (OMPClause *C : S->clauses())
3967 if (auto *FC = dyn_cast<OMPFirstprivateClause>(Val: C)) {
3968 for (Expr *Ref : FC->varlist())
3969 Visit(S: Ref);
3970 }
3971 }
3972 }
3973
3974public:
3975 void VisitDeclRefExpr(DeclRefExpr *E) {
3976 if (TryCaptureCXXThisMembers || E->isTypeDependent() ||
3977 E->isValueDependent() || E->containsUnexpandedParameterPack() ||
3978 E->isInstantiationDependent() ||
3979 E->isNonOdrUse() == clang::NOUR_Unevaluated)
3980 return;
3981 if (auto *VD = dyn_cast<VarDecl>(Val: E->getDecl())) {
3982 // Check the datasharing rules for the expressions in the clauses.
3983 if (!CS || (isa<OMPCapturedExprDecl>(Val: VD) && !CS->capturesVariable(Var: VD) &&
3984 !Stack->getTopDSA(D: VD, /*FromParent=*/false).RefExpr &&
3985 !Stack->isImplicitDefaultFirstprivateFD(VD))) {
3986 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(Val: VD))
3987 if (!CED->hasAttr<OMPCaptureNoInitAttr>()) {
3988 Visit(S: CED->getInit());
3989 return;
3990 }
3991 } else if (VD->isImplicit() || isa<OMPCapturedExprDecl>(Val: VD))
3992 // Do not analyze internal variables and do not enclose them into
3993 // implicit clauses.
3994 if (!Stack->isImplicitDefaultFirstprivateFD(VD))
3995 return;
3996 VD = VD->getCanonicalDecl();
3997 // Skip internally declared variables.
3998 if (VD->hasLocalStorage() && CS && !CS->capturesVariable(Var: VD) &&
3999 !Stack->isImplicitDefaultFirstprivateFD(VD) &&
4000 !Stack->isImplicitTaskFirstprivate(D: VD))
4001 return;
4002 // Skip allocators in uses_allocators clauses.
4003 if (Stack->isUsesAllocatorsDecl(D: VD))
4004 return;
4005
4006 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D: VD, /*FromParent=*/false);
4007 // Check if the variable has explicit DSA set and stop analysis if it so.
4008 if (DVar.RefExpr || !ImplicitDeclarations.insert(V: VD).second)
4009 return;
4010
4011 // Skip internally declared static variables.
4012 std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
4013 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
4014 if (VD->hasGlobalStorage() && CS && !CS->capturesVariable(Var: VD) &&
4015 (Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
4016 !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link) &&
4017 !Stack->isImplicitDefaultFirstprivateFD(VD) &&
4018 !Stack->isImplicitTaskFirstprivate(D: VD))
4019 return;
4020
4021 SourceLocation ELoc = E->getExprLoc();
4022 // The default(none) clause requires that each variable that is referenced
4023 // in the construct, and does not have a predetermined data-sharing
4024 // attribute, must have its data-sharing attribute explicitly determined
4025 // by being listed in a data-sharing attribute clause.
4026 if (DVar.CKind == OMPC_unknown &&
4027 (Stack->getDefaultDSA() == DSA_none ||
4028 Stack->getDefaultDSA() == DSA_private ||
4029 Stack->getDefaultDSA() == DSA_firstprivate) &&
4030 isImplicitOrExplicitTaskingRegion(DKind) &&
4031 VarsWithInheritedDSA.count(Val: VD) == 0) {
4032 bool InheritedDSA = Stack->getDefaultDSA() == DSA_none;
4033 if (!InheritedDSA && (Stack->getDefaultDSA() == DSA_firstprivate ||
4034 Stack->getDefaultDSA() == DSA_private)) {
4035 DSAStackTy::DSAVarData DVar =
4036 Stack->getImplicitDSA(D: VD, /*FromParent=*/false);
4037 InheritedDSA = DVar.CKind == OMPC_unknown;
4038 }
4039 if (InheritedDSA)
4040 VarsWithInheritedDSA[VD] = E;
4041 if (Stack->getDefaultDSA() == DSA_none)
4042 return;
4043 }
4044
4045 // OpenMP 5.0 [2.19.7.2, defaultmap clause, Description]
4046 // If implicit-behavior is none, each variable referenced in the
4047 // construct that does not have a predetermined data-sharing attribute
4048 // and does not appear in a to or link clause on a declare target
4049 // directive must be listed in a data-mapping attribute clause, a
4050 // data-sharing attribute clause (including a data-sharing attribute
4051 // clause on a combined construct where target. is one of the
4052 // constituent constructs), or an is_device_ptr clause.
4053 OpenMPDefaultmapClauseKind ClauseKind =
4054 getVariableCategoryFromDecl(LO: SemaRef.getLangOpts(), VD);
4055 if (SemaRef.getLangOpts().OpenMP >= 50) {
4056 bool IsModifierNone = Stack->getDefaultmapModifier(Kind: ClauseKind) ==
4057 OMPC_DEFAULTMAP_MODIFIER_none;
4058 if (DVar.CKind == OMPC_unknown && IsModifierNone &&
4059 VarsWithInheritedDSA.count(Val: VD) == 0 && !Res) {
4060 // Only check for data-mapping attribute and is_device_ptr here
4061 // since we have already make sure that the declaration does not
4062 // have a data-sharing attribute above
4063 if (!Stack->checkMappableExprComponentListsForDecl(
4064 VD, /*CurrentRegionOnly=*/true,
4065 Check: [VD](OMPClauseMappableExprCommon::MappableExprComponentListRef
4066 MapExprComponents,
4067 OpenMPClauseKind) {
4068 auto MI = MapExprComponents.rbegin();
4069 auto ME = MapExprComponents.rend();
4070 return MI != ME && MI->getAssociatedDeclaration() == VD;
4071 })) {
4072 VarsWithInheritedDSA[VD] = E;
4073 return;
4074 }
4075 }
4076 }
4077 if (SemaRef.getLangOpts().OpenMP > 50) {
4078 bool IsModifierPresent = Stack->getDefaultmapModifier(Kind: ClauseKind) ==
4079 OMPC_DEFAULTMAP_MODIFIER_present;
4080 if (IsModifierPresent) {
4081 if (!llvm::is_contained(Range&: ImpInfo.MapModifiers[ClauseKind],
4082 Element: OMPC_MAP_MODIFIER_present)) {
4083 ImpInfo.MapModifiers[ClauseKind].push_back(
4084 Elt: OMPC_MAP_MODIFIER_present);
4085 }
4086 }
4087 }
4088
4089 if (isOpenMPTargetExecutionDirective(DKind) &&
4090 !Stack->isLoopControlVariable(D: VD).first) {
4091 if (!Stack->checkMappableExprComponentListsForDecl(
4092 VD, /*CurrentRegionOnly=*/true,
4093 Check: [this](OMPClauseMappableExprCommon::MappableExprComponentListRef
4094 StackComponents,
4095 OpenMPClauseKind) {
4096 if (SemaRef.LangOpts.OpenMP >= 50)
4097 return !StackComponents.empty();
4098 // Variable is used if it has been marked as an array, array
4099 // section, array shaping or the variable itself.
4100 return StackComponents.size() == 1 ||
4101 llvm::all_of(
4102 Range: llvm::drop_begin(RangeOrContainer: llvm::reverse(C&: StackComponents)),
4103 P: [](const OMPClauseMappableExprCommon::
4104 MappableComponent &MC) {
4105 return MC.getAssociatedDeclaration() ==
4106 nullptr &&
4107 (isa<ArraySectionExpr>(
4108 Val: MC.getAssociatedExpression()) ||
4109 isa<OMPArrayShapingExpr>(
4110 Val: MC.getAssociatedExpression()) ||
4111 isa<ArraySubscriptExpr>(
4112 Val: MC.getAssociatedExpression()));
4113 });
4114 })) {
4115 bool IsFirstprivate = false;
4116 // By default lambdas are captured as firstprivates.
4117 if (const auto *RD =
4118 VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
4119 IsFirstprivate = RD->isLambda();
4120 IsFirstprivate =
4121 IsFirstprivate || (Stack->mustBeFirstprivate(Kind: ClauseKind) && !Res);
4122 if (IsFirstprivate) {
4123 ImpInfo.Firstprivates.insert(X: E);
4124 } else {
4125 OpenMPDefaultmapClauseModifier M =
4126 Stack->getDefaultmapModifier(Kind: ClauseKind);
4127 if (M == OMPC_DEFAULTMAP_MODIFIER_private) {
4128 ImpInfo.Privates.insert(X: E);
4129 } else {
4130 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier(
4131 M, IsAggregateOrDeclareTarget: ClauseKind == OMPC_DEFAULTMAP_aggregate || Res);
4132 ImpInfo.Mappings[ClauseKind][Kind].insert(X: E);
4133 }
4134 }
4135 return;
4136 }
4137 }
4138
4139 // OpenMP [2.9.3.6, Restrictions, p.2]
4140 // A list item that appears in a reduction clause of the innermost
4141 // enclosing worksharing or parallel construct may not be accessed in an
4142 // explicit task.
4143 DVar = Stack->hasInnermostDSA(
4144 D: VD,
4145 CPred: [](OpenMPClauseKind C, bool AppliedToPointee) {
4146 return C == OMPC_reduction && !AppliedToPointee;
4147 },
4148 DPred: [](OpenMPDirectiveKind K) {
4149 return isOpenMPParallelDirective(DKind: K) ||
4150 isOpenMPWorksharingDirective(DKind: K) || isOpenMPTeamsDirective(DKind: K);
4151 },
4152 /*FromParent=*/true);
4153 if (isOpenMPTaskingDirective(Kind: DKind) && DVar.CKind == OMPC_reduction) {
4154 ErrorFound = true;
4155 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_reduction_in_task);
4156 reportOriginalDsa(SemaRef, Stack, D: VD, DVar);
4157 return;
4158 }
4159
4160 // Define implicit data-sharing attributes for task.
4161 DVar = Stack->getImplicitDSA(D: VD, /*FromParent=*/false);
4162 if (((isOpenMPTaskingDirective(Kind: DKind) && DVar.CKind != OMPC_shared) ||
4163 (((Stack->getDefaultDSA() == DSA_firstprivate &&
4164 DVar.CKind == OMPC_firstprivate) ||
4165 (Stack->getDefaultDSA() == DSA_private &&
4166 DVar.CKind == OMPC_private)) &&
4167 !DVar.RefExpr)) &&
4168 !Stack->isLoopControlVariable(D: VD).first) {
4169 if (Stack->getDefaultDSA() == DSA_private)
4170 ImpInfo.Privates.insert(X: E);
4171 else
4172 ImpInfo.Firstprivates.insert(X: E);
4173 return;
4174 }
4175
4176 // Store implicitly used globals with declare target link for parent
4177 // target.
4178 if (!isOpenMPTargetExecutionDirective(DKind) && Res &&
4179 *Res == OMPDeclareTargetDeclAttr::MT_Link) {
4180 Stack->addToParentTargetRegionLinkGlobals(E);
4181 return;
4182 }
4183 }
4184 }
4185 void VisitMemberExpr(MemberExpr *E) {
4186 if (E->isTypeDependent() || E->isValueDependent() ||
4187 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
4188 return;
4189 auto *FD = dyn_cast<FieldDecl>(Val: E->getMemberDecl());
4190 if (auto *TE = dyn_cast<CXXThisExpr>(Val: E->getBase()->IgnoreParenCasts())) {
4191 if (!FD)
4192 return;
4193 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D: FD, /*FromParent=*/false);
4194 // Check if the variable has explicit DSA set and stop analysis if it
4195 // so.
4196 if (DVar.RefExpr || !ImplicitDeclarations.insert(V: FD).second)
4197 return;
4198
4199 if (isOpenMPTargetExecutionDirective(DKind) &&
4200 !Stack->isLoopControlVariable(D: FD).first &&
4201 !Stack->checkMappableExprComponentListsForDecl(
4202 VD: FD, /*CurrentRegionOnly=*/true,
4203 Check: [](OMPClauseMappableExprCommon::MappableExprComponentListRef
4204 StackComponents,
4205 OpenMPClauseKind) {
4206 return isa<CXXThisExpr>(
4207 Val: cast<MemberExpr>(
4208 Val: StackComponents.back().getAssociatedExpression())
4209 ->getBase()
4210 ->IgnoreParens());
4211 })) {
4212 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
4213 // A bit-field cannot appear in a map clause.
4214 //
4215 if (FD->isBitField())
4216 return;
4217
4218 // Check to see if the member expression is referencing a class that
4219 // has already been explicitly mapped
4220 if (Stack->isClassPreviouslyMapped(QT: TE->getType()))
4221 return;
4222
4223 OpenMPDefaultmapClauseModifier Modifier =
4224 Stack->getDefaultmapModifier(Kind: OMPC_DEFAULTMAP_aggregate);
4225 OpenMPDefaultmapClauseKind ClauseKind =
4226 getVariableCategoryFromDecl(LO: SemaRef.getLangOpts(), VD: FD);
4227 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier(
4228 M: Modifier, /*IsAggregateOrDeclareTarget=*/true);
4229 ImpInfo.Mappings[ClauseKind][Kind].insert(X: E);
4230 return;
4231 }
4232
4233 SourceLocation ELoc = E->getExprLoc();
4234 // OpenMP [2.9.3.6, Restrictions, p.2]
4235 // A list item that appears in a reduction clause of the innermost
4236 // enclosing worksharing or parallel construct may not be accessed in
4237 // an explicit task.
4238 DVar = Stack->hasInnermostDSA(
4239 D: FD,
4240 CPred: [](OpenMPClauseKind C, bool AppliedToPointee) {
4241 return C == OMPC_reduction && !AppliedToPointee;
4242 },
4243 DPred: [](OpenMPDirectiveKind K) {
4244 return isOpenMPParallelDirective(DKind: K) ||
4245 isOpenMPWorksharingDirective(DKind: K) || isOpenMPTeamsDirective(DKind: K);
4246 },
4247 /*FromParent=*/true);
4248 if (isOpenMPTaskingDirective(Kind: DKind) && DVar.CKind == OMPC_reduction) {
4249 ErrorFound = true;
4250 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_reduction_in_task);
4251 reportOriginalDsa(SemaRef, Stack, D: FD, DVar);
4252 return;
4253 }
4254
4255 // Define implicit data-sharing attributes for task.
4256 DVar = Stack->getImplicitDSA(D: FD, /*FromParent=*/false);
4257 if (isOpenMPTaskingDirective(Kind: DKind) && DVar.CKind != OMPC_shared &&
4258 !Stack->isLoopControlVariable(D: FD).first) {
4259 // Check if there is a captured expression for the current field in the
4260 // region. Do not mark it as firstprivate unless there is no captured
4261 // expression.
4262 // TODO: try to make it firstprivate.
4263 if (DVar.CKind != OMPC_unknown)
4264 ImpInfo.Firstprivates.insert(X: E);
4265 }
4266 return;
4267 }
4268 if (isOpenMPTargetExecutionDirective(DKind)) {
4269 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
4270 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, CKind: OMPC_map,
4271 DKind, /*NoDiagnose=*/true))
4272 return;
4273 const auto *VD = cast<ValueDecl>(
4274 Val: CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
4275 if (!Stack->checkMappableExprComponentListsForDecl(
4276 VD, /*CurrentRegionOnly=*/true,
4277 Check: [&CurComponents](
4278 OMPClauseMappableExprCommon::MappableExprComponentListRef
4279 StackComponents,
4280 OpenMPClauseKind) {
4281 auto CCI = CurComponents.rbegin();
4282 auto CCE = CurComponents.rend();
4283 for (const auto &SC : llvm::reverse(C&: StackComponents)) {
4284 // Do both expressions have the same kind?
4285 if (CCI->getAssociatedExpression()->getStmtClass() !=
4286 SC.getAssociatedExpression()->getStmtClass())
4287 if (!((isa<ArraySectionExpr>(
4288 Val: SC.getAssociatedExpression()) ||
4289 isa<OMPArrayShapingExpr>(
4290 Val: SC.getAssociatedExpression())) &&
4291 isa<ArraySubscriptExpr>(
4292 Val: CCI->getAssociatedExpression())))
4293 return false;
4294
4295 const Decl *CCD = CCI->getAssociatedDeclaration();
4296 const Decl *SCD = SC.getAssociatedDeclaration();
4297 CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
4298 SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
4299 if (SCD != CCD)
4300 return false;
4301 std::advance(i&: CCI, n: 1);
4302 if (CCI == CCE)
4303 break;
4304 }
4305 return true;
4306 })) {
4307 Visit(S: E->getBase());
4308 }
4309 } else if (!TryCaptureCXXThisMembers) {
4310 Visit(S: E->getBase());
4311 }
4312 }
4313 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
4314 for (OMPClause *C : S->clauses()) {
4315 // Skip analysis of arguments of private clauses for task|target
4316 // directives.
4317 if (isa_and_nonnull<OMPPrivateClause>(Val: C))
4318 continue;
4319 // Skip analysis of arguments of implicitly defined firstprivate clause
4320 // for task|target directives.
4321 // Skip analysis of arguments of implicitly defined map clause for target
4322 // directives.
4323 if (C && !((isa<OMPFirstprivateClause>(Val: C) || isa<OMPMapClause>(Val: C)) &&
4324 C->isImplicit() && !isOpenMPTaskingDirective(Kind: DKind))) {
4325 for (Stmt *CC : C->children()) {
4326 if (CC)
4327 Visit(S: CC);
4328 }
4329 }
4330 }
4331 // Check implicitly captured variables.
4332 VisitSubCaptures(S);
4333 }
4334
4335 void VisitOMPCanonicalLoopNestTransformationDirective(
4336 OMPCanonicalLoopNestTransformationDirective *S) {
4337 // Loop transformation directives do not introduce data sharing
4338 VisitStmt(S);
4339 }
4340
4341 void VisitCallExpr(CallExpr *S) {
4342 for (Stmt *C : S->arguments()) {
4343 if (C) {
4344 // Check implicitly captured variables in the task-based directives to
4345 // check if they must be firstprivatized.
4346 Visit(S: C);
4347 }
4348 }
4349 if (Expr *Callee = S->getCallee()) {
4350 auto *CI = Callee->IgnoreParenImpCasts();
4351 if (auto *CE = dyn_cast<MemberExpr>(Val: CI))
4352 Visit(S: CE->getBase());
4353 else if (auto *CE = dyn_cast<DeclRefExpr>(Val: CI))
4354 Visit(S: CE);
4355 }
4356 }
4357 void VisitStmt(Stmt *S) {
4358 for (Stmt *C : S->children()) {
4359 if (C) {
4360 // Check implicitly captured variables in the task-based directives to
4361 // check if they must be firstprivatized.
4362 Visit(S: C);
4363 }
4364 }
4365 }
4366
4367 void visitSubCaptures(CapturedStmt *S) {
4368 for (const CapturedStmt::Capture &Cap : S->captures()) {
4369 if (!Cap.capturesVariable() && !Cap.capturesVariableByCopy())
4370 continue;
4371 VarDecl *VD = Cap.getCapturedVar();
4372 // Do not try to map the variable if it or its sub-component was mapped
4373 // already.
4374 if (isOpenMPTargetExecutionDirective(DKind) &&
4375 Stack->checkMappableExprComponentListsForDecl(
4376 VD, /*CurrentRegionOnly=*/true,
4377 Check: [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
4378 OpenMPClauseKind) { return true; }))
4379 continue;
4380 DeclRefExpr *DRE = buildDeclRefExpr(
4381 S&: SemaRef, D: VD, Ty: VD->getType().getNonLValueExprType(Context: SemaRef.Context),
4382 Loc: Cap.getLocation(), /*RefersToCapture=*/true);
4383 Visit(S: DRE);
4384 }
4385 }
4386 bool isErrorFound() const { return ErrorFound; }
4387 const VariableImplicitInfo &getImplicitInfo() const { return ImpInfo; }
4388 const SemaOpenMP::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
4389 return VarsWithInheritedDSA;
4390 }
4391
4392 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
4393 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {
4394 DKind = S->getCurrentDirective();
4395 // Process declare target link variables for the target directives.
4396 if (isOpenMPTargetExecutionDirective(DKind)) {
4397 for (DeclRefExpr *E : Stack->getLinkGlobals())
4398 Visit(S: E);
4399 }
4400 }
4401};
4402} // namespace
4403
4404static void handleDeclareVariantConstructTrait(DSAStackTy *Stack,
4405 OpenMPDirectiveKind DKind,
4406 bool ScopeEntry) {
4407 SmallVector<llvm::omp::TraitProperty, 8> Traits;
4408 if (isOpenMPTargetExecutionDirective(DKind))
4409 Traits.emplace_back(Args: llvm::omp::TraitProperty::construct_target_target);
4410 if (isOpenMPTeamsDirective(DKind))
4411 Traits.emplace_back(Args: llvm::omp::TraitProperty::construct_teams_teams);
4412 if (isOpenMPParallelDirective(DKind))
4413 Traits.emplace_back(Args: llvm::omp::TraitProperty::construct_parallel_parallel);
4414 if (isOpenMPWorksharingDirective(DKind))
4415 Traits.emplace_back(Args: llvm::omp::TraitProperty::construct_for_for);
4416 if (isOpenMPSimdDirective(DKind))
4417 Traits.emplace_back(Args: llvm::omp::TraitProperty::construct_simd_simd);
4418 Stack->handleConstructTrait(Traits, ScopeEntry);
4419}
4420
4421static SmallVector<SemaOpenMP::CapturedParamNameType>
4422getParallelRegionParams(Sema &SemaRef, bool LoopBoundSharing) {
4423 ASTContext &Context = SemaRef.getASTContext();
4424 QualType KmpInt32Ty =
4425 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1).withConst();
4426 QualType KmpInt32PtrTy =
4427 Context.getPointerType(T: KmpInt32Ty).withConst().withRestrict();
4428 SmallVector<SemaOpenMP::CapturedParamNameType> Params{
4429 std::make_pair(x: ".global_tid.", y&: KmpInt32PtrTy),
4430 std::make_pair(x: ".bound_tid.", y&: KmpInt32PtrTy),
4431 };
4432 if (LoopBoundSharing) {
4433 QualType KmpSizeTy = Context.getSizeType().withConst();
4434 Params.push_back(Elt: std::make_pair(x: ".previous.lb.", y&: KmpSizeTy));
4435 Params.push_back(Elt: std::make_pair(x: ".previous.ub.", y&: KmpSizeTy));
4436 }
4437
4438 // __context with shared vars
4439 Params.push_back(Elt: std::make_pair(x: StringRef(), y: QualType()));
4440 return Params;
4441}
4442
4443static SmallVector<SemaOpenMP::CapturedParamNameType>
4444getTeamsRegionParams(Sema &SemaRef) {
4445 return getParallelRegionParams(SemaRef, /*LoopBoundSharing=*/false);
4446}
4447
4448static SmallVector<SemaOpenMP::CapturedParamNameType>
4449getTaskRegionParams(Sema &SemaRef) {
4450 ASTContext &Context = SemaRef.getASTContext();
4451 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(DestWidth: 32, Signed: 1).withConst();
4452 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4453 QualType KmpInt32PtrTy =
4454 Context.getPointerType(T: KmpInt32Ty).withConst().withRestrict();
4455 QualType Args[] = {VoidPtrTy};
4456 FunctionProtoType::ExtProtoInfo EPI;
4457 EPI.Variadic = true;
4458 QualType CopyFnType = Context.getFunctionType(ResultTy: Context.VoidTy, Args, EPI);
4459 SmallVector<SemaOpenMP::CapturedParamNameType> Params{
4460 std::make_pair(x: ".global_tid.", y&: KmpInt32Ty),
4461 std::make_pair(x: ".part_id.", y&: KmpInt32PtrTy),
4462 std::make_pair(x: ".privates.", y&: VoidPtrTy),
4463 std::make_pair(
4464 x: ".copy_fn.",
4465 y: Context.getPointerType(T: CopyFnType).withConst().withRestrict()),
4466 std::make_pair(x: ".task_t.", y: Context.VoidPtrTy.withConst()),
4467 std::make_pair(x: StringRef(), y: QualType()) // __context with shared vars
4468 };
4469 return Params;
4470}
4471
4472static SmallVector<SemaOpenMP::CapturedParamNameType>
4473getTargetRegionParams(Sema &SemaRef) {
4474 ASTContext &Context = SemaRef.getASTContext();
4475 SmallVector<SemaOpenMP::CapturedParamNameType> Params;
4476 // __context with shared vars
4477 Params.push_back(Elt: std::make_pair(x: StringRef(), y: QualType()));
4478 // Implicit dyn_ptr argument, appended as the last parameter. Present on both
4479 // host and device so argument counts match without runtime manipulation.
4480 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4481 Params.push_back(Elt: std::make_pair(x: StringRef("dyn_ptr"), y&: VoidPtrTy));
4482 return Params;
4483}
4484
4485static SmallVector<SemaOpenMP::CapturedParamNameType>
4486getUnknownRegionParams(Sema &SemaRef) {
4487 SmallVector<SemaOpenMP::CapturedParamNameType> Params{
4488 std::make_pair(x: StringRef(), y: QualType()) // __context with shared vars
4489 };
4490 return Params;
4491}
4492
4493static SmallVector<SemaOpenMP::CapturedParamNameType>
4494getTaskloopRegionParams(Sema &SemaRef) {
4495 ASTContext &Context = SemaRef.getASTContext();
4496 QualType KmpInt32Ty =
4497 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1).withConst();
4498 QualType KmpUInt64Ty =
4499 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0).withConst();
4500 QualType KmpInt64Ty =
4501 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1).withConst();
4502 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4503 QualType KmpInt32PtrTy =
4504 Context.getPointerType(T: KmpInt32Ty).withConst().withRestrict();
4505 QualType Args[] = {VoidPtrTy};
4506 FunctionProtoType::ExtProtoInfo EPI;
4507 EPI.Variadic = true;
4508 QualType CopyFnType = Context.getFunctionType(ResultTy: Context.VoidTy, Args, EPI);
4509 SmallVector<SemaOpenMP::CapturedParamNameType> Params{
4510 std::make_pair(x: ".global_tid.", y&: KmpInt32Ty),
4511 std::make_pair(x: ".part_id.", y&: KmpInt32PtrTy),
4512 std::make_pair(x: ".privates.", y&: VoidPtrTy),
4513 std::make_pair(
4514 x: ".copy_fn.",
4515 y: Context.getPointerType(T: CopyFnType).withConst().withRestrict()),
4516 std::make_pair(x: ".task_t.", y: Context.VoidPtrTy.withConst()),
4517 std::make_pair(x: ".lb.", y&: KmpUInt64Ty),
4518 std::make_pair(x: ".ub.", y&: KmpUInt64Ty),
4519 std::make_pair(x: ".st.", y&: KmpInt64Ty),
4520 std::make_pair(x: ".liter.", y&: KmpInt32Ty),
4521 std::make_pair(x: ".reductions.", y&: VoidPtrTy),
4522 std::make_pair(x: StringRef(), y: QualType()) // __context with shared vars
4523 };
4524 return Params;
4525}
4526
4527static void processCapturedRegions(Sema &SemaRef, OpenMPDirectiveKind DKind,
4528 Scope *CurScope, SourceLocation Loc) {
4529 SmallVector<OpenMPDirectiveKind> Regions;
4530 getOpenMPCaptureRegions(CaptureRegions&: Regions, DKind);
4531
4532 bool LoopBoundSharing = isOpenMPLoopBoundSharingDirective(Kind: DKind);
4533
4534 auto MarkAsInlined = [&](CapturedRegionScopeInfo *CSI) {
4535 CSI->TheCapturedDecl->addAttr(A: AlwaysInlineAttr::CreateImplicit(
4536 Ctx&: SemaRef.getASTContext(), Range: {}, S: AlwaysInlineAttr::Keyword_forceinline));
4537 };
4538
4539 for (auto [Level, RKind] : llvm::enumerate(First&: Regions)) {
4540 switch (RKind) {
4541 // All region kinds that can be returned from `getOpenMPCaptureRegions`
4542 // are listed here.
4543 case OMPD_parallel:
4544 SemaRef.ActOnCapturedRegionStart(
4545 Loc, CurScope, Kind: CR_OpenMP,
4546 Params: getParallelRegionParams(SemaRef, LoopBoundSharing), OpenMPCaptureLevel: Level);
4547 break;
4548 case OMPD_teams:
4549 SemaRef.ActOnCapturedRegionStart(Loc, CurScope, Kind: CR_OpenMP,
4550 Params: getTeamsRegionParams(SemaRef), OpenMPCaptureLevel: Level);
4551 break;
4552 case OMPD_task:
4553 SemaRef.ActOnCapturedRegionStart(Loc, CurScope, Kind: CR_OpenMP,
4554 Params: getTaskRegionParams(SemaRef), OpenMPCaptureLevel: Level);
4555 // Mark this captured region as inlined, because we don't use outlined
4556 // function directly.
4557 MarkAsInlined(SemaRef.getCurCapturedRegion());
4558 break;
4559 case OMPD_taskloop:
4560 SemaRef.ActOnCapturedRegionStart(Loc, CurScope, Kind: CR_OpenMP,
4561 Params: getTaskloopRegionParams(SemaRef), OpenMPCaptureLevel: Level);
4562 // Mark this captured region as inlined, because we don't use outlined
4563 // function directly.
4564 MarkAsInlined(SemaRef.getCurCapturedRegion());
4565 break;
4566 case OMPD_target:
4567 SemaRef.ActOnCapturedRegionStart(Loc, CurScope, Kind: CR_OpenMP,
4568 Params: getTargetRegionParams(SemaRef), OpenMPCaptureLevel: Level);
4569 break;
4570 case OMPD_unknown:
4571 SemaRef.ActOnCapturedRegionStart(Loc, CurScope, Kind: CR_OpenMP,
4572 Params: getUnknownRegionParams(SemaRef));
4573 break;
4574 case OMPD_metadirective:
4575 case OMPD_nothing:
4576 default:
4577 llvm_unreachable("Unexpected capture region");
4578 }
4579 }
4580}
4581
4582void SemaOpenMP::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind,
4583 Scope *CurScope) {
4584 switch (DKind) {
4585 case OMPD_atomic:
4586 case OMPD_critical:
4587 case OMPD_masked:
4588 case OMPD_master:
4589 case OMPD_section:
4590 case OMPD_tile:
4591 case OMPD_stripe:
4592 case OMPD_unroll:
4593 case OMPD_reverse:
4594 case OMPD_interchange:
4595 case OMPD_fuse:
4596 case OMPD_assume:
4597 break;
4598 default:
4599 processCapturedRegions(SemaRef, DKind, CurScope,
4600 DSAStack->getConstructLoc());
4601 break;
4602 }
4603
4604 DSAStack->setContext(SemaRef.CurContext);
4605 handleDeclareVariantConstructTrait(DSAStack, DKind, /*ScopeEntry=*/true);
4606}
4607
4608int SemaOpenMP::getNumberOfConstructScopes(unsigned Level) const {
4609 return getOpenMPCaptureLevels(DSAStack->getDirective(Level));
4610}
4611
4612int SemaOpenMP::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
4613 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
4614 getOpenMPCaptureRegions(CaptureRegions, DKind);
4615 return CaptureRegions.size();
4616}
4617
4618static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
4619 Expr *CaptureExpr, bool WithInit,
4620 DeclContext *CurContext,
4621 bool AsExpression) {
4622 assert(CaptureExpr);
4623 ASTContext &C = S.getASTContext();
4624 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
4625 QualType Ty = Init->getType();
4626 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
4627 if (S.getLangOpts().CPlusPlus) {
4628 Ty = C.getLValueReferenceType(T: Ty);
4629 } else {
4630 Ty = C.getPointerType(T: Ty);
4631 ExprResult Res =
4632 S.CreateBuiltinUnaryOp(OpLoc: CaptureExpr->getExprLoc(), Opc: UO_AddrOf, InputExpr: Init);
4633 if (!Res.isUsable())
4634 return nullptr;
4635 Init = Res.get();
4636 }
4637 WithInit = true;
4638 }
4639 auto *CED = OMPCapturedExprDecl::Create(C, DC: CurContext, Id, T: Ty,
4640 StartLoc: CaptureExpr->getBeginLoc());
4641 if (!WithInit)
4642 CED->addAttr(A: OMPCaptureNoInitAttr::CreateImplicit(Ctx&: C));
4643 CurContext->addHiddenDecl(D: CED);
4644 Sema::TentativeAnalysisScope Trap(S);
4645 S.AddInitializerToDecl(dcl: CED, init: Init, /*DirectInit=*/false);
4646 return CED;
4647}
4648
4649static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
4650 bool WithInit) {
4651 OMPCapturedExprDecl *CD;
4652 if (VarDecl *VD = S.OpenMP().isOpenMPCapturedDecl(D))
4653 CD = cast<OMPCapturedExprDecl>(Val: VD);
4654 else
4655 CD = buildCaptureDecl(S, Id: D->getIdentifier(), CaptureExpr, WithInit,
4656 CurContext: S.CurContext,
4657 /*AsExpression=*/false);
4658 return buildDeclRefExpr(S, D: CD, Ty: CD->getType().getNonReferenceType(),
4659 Loc: CaptureExpr->getExprLoc());
4660}
4661
4662static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref,
4663 StringRef Name) {
4664 CaptureExpr = S.DefaultLvalueConversion(E: CaptureExpr).get();
4665 if (!Ref) {
4666 OMPCapturedExprDecl *CD = buildCaptureDecl(
4667 S, Id: &S.getASTContext().Idents.get(Name), CaptureExpr,
4668 /*WithInit=*/true, CurContext: S.CurContext, /*AsExpression=*/true);
4669 Ref = buildDeclRefExpr(S, D: CD, Ty: CD->getType().getNonReferenceType(),
4670 Loc: CaptureExpr->getExprLoc());
4671 }
4672 ExprResult Res = Ref;
4673 if (!S.getLangOpts().CPlusPlus &&
4674 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
4675 Ref->getType()->isPointerType()) {
4676 Res = S.CreateBuiltinUnaryOp(OpLoc: CaptureExpr->getExprLoc(), Opc: UO_Deref, InputExpr: Ref);
4677 if (!Res.isUsable())
4678 return ExprError();
4679 }
4680 return S.DefaultLvalueConversion(E: Res.get());
4681}
4682
4683namespace {
4684// OpenMP directives parsed in this section are represented as a
4685// CapturedStatement with an associated statement. If a syntax error
4686// is detected during the parsing of the associated statement, the
4687// compiler must abort processing and close the CapturedStatement.
4688//
4689// Combined directives such as 'target parallel' have more than one
4690// nested CapturedStatements. This RAII ensures that we unwind out
4691// of all the nested CapturedStatements when an error is found.
4692class CaptureRegionUnwinderRAII {
4693private:
4694 Sema &S;
4695 bool &ErrorFound;
4696 OpenMPDirectiveKind DKind = OMPD_unknown;
4697
4698public:
4699 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
4700 OpenMPDirectiveKind DKind)
4701 : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
4702 ~CaptureRegionUnwinderRAII() {
4703 if (ErrorFound) {
4704 int ThisCaptureLevel = S.OpenMP().getOpenMPCaptureLevels(DKind);
4705 while (--ThisCaptureLevel >= 0)
4706 S.ActOnCapturedRegionError();
4707 }
4708 }
4709};
4710} // namespace
4711
4712void SemaOpenMP::tryCaptureOpenMPLambdas(ValueDecl *V) {
4713 // Capture variables captured by reference in lambdas for target-based
4714 // directives.
4715 if (!SemaRef.CurContext->isDependentContext() &&
4716 (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) ||
4717 isOpenMPTargetDataManagementDirective(
4718 DSAStack->getCurrentDirective()))) {
4719 QualType Type = V->getType();
4720 if (const auto *RD = Type.getCanonicalType()
4721 .getNonReferenceType()
4722 ->getAsCXXRecordDecl()) {
4723 bool SavedForceCaptureByReferenceInTargetExecutable =
4724 DSAStack->isForceCaptureByReferenceInTargetExecutable();
4725 DSAStack->setForceCaptureByReferenceInTargetExecutable(
4726 /*V=*/true);
4727 if (RD->isLambda()) {
4728 llvm::DenseMap<const ValueDecl *, FieldDecl *> Captures;
4729 FieldDecl *ThisCapture;
4730 RD->getCaptureFields(Captures, ThisCapture);
4731 for (const LambdaCapture &LC : RD->captures()) {
4732 if (LC.getCaptureKind() == LCK_ByRef) {
4733 VarDecl *VD = cast<VarDecl>(Val: LC.getCapturedVar());
4734 DeclContext *VDC = VD->getDeclContext();
4735 if (!VDC->Encloses(DC: SemaRef.CurContext))
4736 continue;
4737 SemaRef.MarkVariableReferenced(Loc: LC.getLocation(), Var: VD);
4738 } else if (LC.getCaptureKind() == LCK_This) {
4739 QualType ThisTy = SemaRef.getCurrentThisType();
4740 if (!ThisTy.isNull() && getASTContext().typesAreCompatible(
4741 T1: ThisTy, T2: ThisCapture->getType()))
4742 SemaRef.CheckCXXThisCapture(Loc: LC.getLocation());
4743 }
4744 }
4745 }
4746 DSAStack->setForceCaptureByReferenceInTargetExecutable(
4747 SavedForceCaptureByReferenceInTargetExecutable);
4748 }
4749 }
4750}
4751
4752static bool checkOrderedOrderSpecified(Sema &S,
4753 const ArrayRef<OMPClause *> Clauses) {
4754 const OMPOrderedClause *Ordered = nullptr;
4755 const OMPOrderClause *Order = nullptr;
4756
4757 for (const OMPClause *Clause : Clauses) {
4758 if (Clause->getClauseKind() == OMPC_ordered)
4759 Ordered = cast<OMPOrderedClause>(Val: Clause);
4760 else if (Clause->getClauseKind() == OMPC_order) {
4761 Order = cast<OMPOrderClause>(Val: Clause);
4762 if (Order->getKind() != OMPC_ORDER_concurrent)
4763 Order = nullptr;
4764 }
4765 if (Ordered && Order)
4766 break;
4767 }
4768
4769 if (Ordered && Order) {
4770 S.Diag(Loc: Order->getKindKwLoc(),
4771 DiagID: diag::err_omp_simple_clause_incompatible_with_ordered)
4772 << getOpenMPClauseNameForDiag(C: OMPC_order)
4773 << getOpenMPSimpleClauseTypeName(Kind: OMPC_order, Type: OMPC_ORDER_concurrent)
4774 << SourceRange(Order->getBeginLoc(), Order->getEndLoc());
4775 S.Diag(Loc: Ordered->getBeginLoc(), DiagID: diag::note_omp_ordered_param)
4776 << 0 << SourceRange(Ordered->getBeginLoc(), Ordered->getEndLoc());
4777 return true;
4778 }
4779 return false;
4780}
4781
4782StmtResult SemaOpenMP::ActOnOpenMPRegionEnd(StmtResult S,
4783 ArrayRef<OMPClause *> Clauses) {
4784 handleDeclareVariantConstructTrait(DSAStack, DSAStack->getCurrentDirective(),
4785 /*ScopeEntry=*/false);
4786 if (!isOpenMPCapturingDirective(DSAStack->getCurrentDirective()))
4787 return S;
4788
4789 bool ErrorFound = false;
4790 CaptureRegionUnwinderRAII CaptureRegionUnwinder(
4791 SemaRef, ErrorFound, DSAStack->getCurrentDirective());
4792 if (!S.isUsable()) {
4793 ErrorFound = true;
4794 return StmtError();
4795 }
4796
4797 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
4798 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
4799 OMPOrderedClause *OC = nullptr;
4800 OMPScheduleClause *SC = nullptr;
4801 SmallVector<const OMPLinearClause *, 4> LCs;
4802 SmallVector<const OMPClauseWithPreInit *, 4> PICs;
4803 // This is required for proper codegen.
4804 for (OMPClause *Clause : Clauses) {
4805 if (!getLangOpts().OpenMPSimd &&
4806 (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) ||
4807 DSAStack->getCurrentDirective() == OMPD_target) &&
4808 Clause->getClauseKind() == OMPC_in_reduction) {
4809 // Capture taskgroup task_reduction descriptors inside the tasking regions
4810 // with the corresponding in_reduction items.
4811 auto *IRC = cast<OMPInReductionClause>(Val: Clause);
4812 for (Expr *E : IRC->taskgroup_descriptors())
4813 if (E)
4814 SemaRef.MarkDeclarationsReferencedInExpr(E);
4815 }
4816 if (isOpenMPPrivate(Kind: Clause->getClauseKind()) ||
4817 Clause->getClauseKind() == OMPC_copyprivate ||
4818 (getLangOpts().OpenMPUseTLS &&
4819 getASTContext().getTargetInfo().isTLSSupported() &&
4820 Clause->getClauseKind() == OMPC_copyin)) {
4821 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
4822 // Mark all variables in private list clauses as used in inner region.
4823 for (Stmt *VarRef : Clause->children()) {
4824 if (auto *E = cast_or_null<Expr>(Val: VarRef)) {
4825 SemaRef.MarkDeclarationsReferencedInExpr(E);
4826 }
4827 }
4828 DSAStack->setForceVarCapturing(/*V=*/false);
4829 } else if (CaptureRegions.size() > 1 ||
4830 CaptureRegions.back() != OMPD_unknown) {
4831 if (auto *C = OMPClauseWithPreInit::get(C: Clause))
4832 PICs.push_back(Elt: C);
4833 if (auto *C = OMPClauseWithPostUpdate::get(C: Clause)) {
4834 if (Expr *E = C->getPostUpdateExpr())
4835 SemaRef.MarkDeclarationsReferencedInExpr(E);
4836 }
4837 }
4838 if (Clause->getClauseKind() == OMPC_schedule)
4839 SC = cast<OMPScheduleClause>(Val: Clause);
4840 else if (Clause->getClauseKind() == OMPC_ordered)
4841 OC = cast<OMPOrderedClause>(Val: Clause);
4842 else if (Clause->getClauseKind() == OMPC_linear)
4843 LCs.push_back(Elt: cast<OMPLinearClause>(Val: Clause));
4844 }
4845 // Capture allocator expressions if used.
4846 for (Expr *E : DSAStack->getInnerAllocators())
4847 SemaRef.MarkDeclarationsReferencedInExpr(E);
4848 // OpenMP, 2.7.1 Loop Construct, Restrictions
4849 // The nonmonotonic modifier cannot be specified if an ordered clause is
4850 // specified.
4851 if (SC &&
4852 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
4853 SC->getSecondScheduleModifier() ==
4854 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
4855 OC) {
4856 Diag(Loc: SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
4857 ? SC->getFirstScheduleModifierLoc()
4858 : SC->getSecondScheduleModifierLoc(),
4859 DiagID: diag::err_omp_simple_clause_incompatible_with_ordered)
4860 << getOpenMPClauseNameForDiag(C: OMPC_schedule)
4861 << getOpenMPSimpleClauseTypeName(Kind: OMPC_schedule,
4862 Type: OMPC_SCHEDULE_MODIFIER_nonmonotonic)
4863 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
4864 ErrorFound = true;
4865 }
4866 // OpenMP 5.0, 2.9.2 Worksharing-Loop Construct, Restrictions.
4867 // If an order(concurrent) clause is present, an ordered clause may not appear
4868 // on the same directive.
4869 if (checkOrderedOrderSpecified(S&: SemaRef, Clauses))
4870 ErrorFound = true;
4871 if (!LCs.empty() && OC && OC->getNumForLoops()) {
4872 for (const OMPLinearClause *C : LCs) {
4873 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_linear_ordered)
4874 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
4875 }
4876 ErrorFound = true;
4877 }
4878 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
4879 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
4880 OC->getNumForLoops()) {
4881 unsigned OMPVersion = getLangOpts().OpenMP;
4882 Diag(Loc: OC->getBeginLoc(), DiagID: diag::err_omp_ordered_simd)
4883 << getOpenMPDirectiveName(DSAStack->getCurrentDirective(), Ver: OMPVersion);
4884 ErrorFound = true;
4885 }
4886 if (ErrorFound) {
4887 return StmtError();
4888 }
4889 StmtResult SR = S;
4890 unsigned CompletedRegions = 0;
4891 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(C&: CaptureRegions)) {
4892 // Mark all variables in private list clauses as used in inner region.
4893 // Required for proper codegen of combined directives.
4894 // TODO: add processing for other clauses.
4895 if (ThisCaptureRegion != OMPD_unknown) {
4896 for (const clang::OMPClauseWithPreInit *C : PICs) {
4897 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
4898 // Find the particular capture region for the clause if the
4899 // directive is a combined one with multiple capture regions.
4900 // If the directive is not a combined one, the capture region
4901 // associated with the clause is OMPD_unknown and is generated
4902 // only once.
4903 if (CaptureRegion == ThisCaptureRegion ||
4904 CaptureRegion == OMPD_unknown) {
4905 if (auto *DS = cast_or_null<DeclStmt>(Val: C->getPreInitStmt())) {
4906 for (Decl *D : DS->decls())
4907 SemaRef.MarkVariableReferenced(Loc: D->getLocation(),
4908 Var: cast<VarDecl>(Val: D));
4909 }
4910 }
4911 }
4912 }
4913 if (ThisCaptureRegion == OMPD_target) {
4914 // Capture allocator traits in the target region. They are used implicitly
4915 // and, thus, are not captured by default.
4916 for (OMPClause *C : Clauses) {
4917 if (const auto *UAC = dyn_cast<OMPUsesAllocatorsClause>(Val: C)) {
4918 for (unsigned I = 0, End = UAC->getNumberOfAllocators(); I < End;
4919 ++I) {
4920 OMPUsesAllocatorsClause::Data D = UAC->getAllocatorData(I);
4921 if (Expr *E = D.AllocatorTraits)
4922 SemaRef.MarkDeclarationsReferencedInExpr(E);
4923 }
4924 continue;
4925 }
4926 }
4927 }
4928 if (ThisCaptureRegion == OMPD_parallel) {
4929 // Capture temp arrays for inscan reductions and locals in aligned
4930 // clauses.
4931 for (OMPClause *C : Clauses) {
4932 if (auto *RC = dyn_cast<OMPReductionClause>(Val: C)) {
4933 if (RC->getModifier() != OMPC_REDUCTION_inscan)
4934 continue;
4935 for (Expr *E : RC->copy_array_temps())
4936 if (E)
4937 SemaRef.MarkDeclarationsReferencedInExpr(E);
4938 }
4939 if (auto *AC = dyn_cast<OMPAlignedClause>(Val: C)) {
4940 for (Expr *E : AC->varlist())
4941 SemaRef.MarkDeclarationsReferencedInExpr(E);
4942 }
4943 }
4944 }
4945 if (++CompletedRegions == CaptureRegions.size())
4946 DSAStack->setBodyComplete();
4947 SR = SemaRef.ActOnCapturedRegionEnd(S: SR.get());
4948 }
4949 return SR;
4950}
4951
4952static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
4953 OpenMPDirectiveKind CancelRegion,
4954 SourceLocation StartLoc) {
4955 // CancelRegion is only needed for cancel and cancellation_point.
4956 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
4957 return false;
4958
4959 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
4960 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
4961 return false;
4962
4963 unsigned OMPVersion = SemaRef.getLangOpts().OpenMP;
4964 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_wrong_cancel_region)
4965 << getOpenMPDirectiveName(D: CancelRegion, Ver: OMPVersion);
4966 return true;
4967}
4968
4969static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
4970 OpenMPDirectiveKind CurrentRegion,
4971 const DeclarationNameInfo &CurrentName,
4972 OpenMPDirectiveKind CancelRegion,
4973 OpenMPBindClauseKind BindKind,
4974 SourceLocation StartLoc) {
4975 if (!Stack->getCurScope())
4976 return false;
4977
4978 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
4979 OpenMPDirectiveKind OffendingRegion = ParentRegion;
4980 bool NestingProhibited = false;
4981 bool CloseNesting = true;
4982 bool OrphanSeen = false;
4983 enum {
4984 NoRecommend,
4985 ShouldBeInParallelRegion,
4986 ShouldBeInOrderedRegion,
4987 ShouldBeInTargetRegion,
4988 ShouldBeInTeamsRegion,
4989 ShouldBeInLoopSimdRegion,
4990 } Recommend = NoRecommend;
4991
4992 SmallVector<OpenMPDirectiveKind, 4> LeafOrComposite;
4993 ArrayRef<OpenMPDirectiveKind> ParentLOC =
4994 getLeafOrCompositeConstructs(D: ParentRegion, Output&: LeafOrComposite);
4995 OpenMPDirectiveKind EnclosingConstruct = ParentLOC.back();
4996 unsigned OMPVersion = SemaRef.getLangOpts().OpenMP;
4997
4998 if (OMPVersion >= 50 && Stack->isParentOrderConcurrent() &&
4999 !isOpenMPOrderConcurrentNestableDirective(DKind: CurrentRegion,
5000 LangOpts: SemaRef.LangOpts)) {
5001 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_prohibited_region_order)
5002 << getOpenMPDirectiveName(D: CurrentRegion, Ver: OMPVersion);
5003 return true;
5004 }
5005 if (isOpenMPSimdDirective(DKind: ParentRegion) &&
5006 ((OMPVersion <= 45 && CurrentRegion != OMPD_ordered) ||
5007 (OMPVersion >= 50 && CurrentRegion != OMPD_ordered &&
5008 CurrentRegion != OMPD_simd && CurrentRegion != OMPD_atomic &&
5009 CurrentRegion != OMPD_scan))) {
5010 // OpenMP [2.16, Nesting of Regions]
5011 // OpenMP constructs may not be nested inside a simd region.
5012 // OpenMP [2.8.1,simd Construct, Restrictions]
5013 // An ordered construct with the simd clause is the only OpenMP
5014 // construct that can appear in the simd region.
5015 // Allowing a SIMD construct nested in another SIMD construct is an
5016 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
5017 // message.
5018 // OpenMP 5.0 [2.9.3.1, simd Construct, Restrictions]
5019 // The only OpenMP constructs that can be encountered during execution of
5020 // a simd region are the atomic construct, the loop construct, the simd
5021 // construct and the ordered construct with the simd clause.
5022 SemaRef.Diag(Loc: StartLoc, DiagID: (CurrentRegion != OMPD_simd)
5023 ? diag::err_omp_prohibited_region_simd
5024 : diag::warn_omp_nesting_simd)
5025 << (OMPVersion >= 50 ? 1 : 0);
5026 return CurrentRegion != OMPD_simd;
5027 }
5028 if (EnclosingConstruct == OMPD_atomic) {
5029 // OpenMP [2.16, Nesting of Regions]
5030 // OpenMP constructs may not be nested inside an atomic region.
5031 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_prohibited_region_atomic);
5032 return true;
5033 }
5034 if (CurrentRegion == OMPD_section) {
5035 // OpenMP [2.7.2, sections Construct, Restrictions]
5036 // Orphaned section directives are prohibited. That is, the section
5037 // directives must appear within the sections construct and must not be
5038 // encountered elsewhere in the sections region.
5039 if (EnclosingConstruct != OMPD_sections) {
5040 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_orphaned_section_directive)
5041 << (ParentRegion != OMPD_unknown)
5042 << getOpenMPDirectiveName(D: ParentRegion, Ver: OMPVersion);
5043 return true;
5044 }
5045 return false;
5046 }
5047 // Allow some constructs (except teams and cancellation constructs) to be
5048 // orphaned (they could be used in functions, called from OpenMP regions
5049 // with the required preconditions).
5050 if (ParentRegion == OMPD_unknown &&
5051 !isOpenMPNestingTeamsDirective(DKind: CurrentRegion) &&
5052 CurrentRegion != OMPD_cancellation_point &&
5053 CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_scan)
5054 return false;
5055 // Checks needed for mapping "loop" construct. Please check mapLoopConstruct
5056 // for a detailed explanation
5057 if (OMPVersion >= 50 && CurrentRegion == OMPD_loop &&
5058 (BindKind == OMPC_BIND_parallel || BindKind == OMPC_BIND_teams) &&
5059 (isOpenMPWorksharingDirective(DKind: ParentRegion) ||
5060 EnclosingConstruct == OMPD_loop)) {
5061 int ErrorMsgNumber = (BindKind == OMPC_BIND_parallel) ? 1 : 4;
5062 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_prohibited_region)
5063 << true << getOpenMPDirectiveName(D: ParentRegion, Ver: OMPVersion)
5064 << ErrorMsgNumber << getOpenMPDirectiveName(D: CurrentRegion, Ver: OMPVersion);
5065 return true;
5066 }
5067 if (CurrentRegion == OMPD_cancellation_point ||
5068 CurrentRegion == OMPD_cancel) {
5069 // OpenMP [2.16, Nesting of Regions]
5070 // A cancellation point construct for which construct-type-clause is
5071 // taskgroup must be nested inside a task construct. A cancellation
5072 // point construct for which construct-type-clause is not taskgroup must
5073 // be closely nested inside an OpenMP construct that matches the type
5074 // specified in construct-type-clause.
5075 // A cancel construct for which construct-type-clause is taskgroup must be
5076 // nested inside a task construct. A cancel construct for which
5077 // construct-type-clause is not taskgroup must be closely nested inside an
5078 // OpenMP construct that matches the type specified in
5079 // construct-type-clause.
5080 ArrayRef<OpenMPDirectiveKind> Leafs = getLeafConstructsOrSelf(D: ParentRegion);
5081 if (CancelRegion == OMPD_taskgroup) {
5082 NestingProhibited =
5083 EnclosingConstruct != OMPD_task &&
5084 (OMPVersion < 50 || EnclosingConstruct != OMPD_taskloop);
5085 } else if (CancelRegion == OMPD_sections) {
5086 NestingProhibited = EnclosingConstruct != OMPD_section &&
5087 EnclosingConstruct != OMPD_sections;
5088 } else {
5089 NestingProhibited = CancelRegion != Leafs.back();
5090 }
5091 OrphanSeen = ParentRegion == OMPD_unknown;
5092 } else if (CurrentRegion == OMPD_master || CurrentRegion == OMPD_masked) {
5093 // OpenMP 5.1 [2.22, Nesting of Regions]
5094 // A masked region may not be closely nested inside a worksharing, loop,
5095 // atomic, task, or taskloop region.
5096 NestingProhibited = isOpenMPWorksharingDirective(DKind: ParentRegion) ||
5097 isOpenMPGenericLoopDirective(DKind: ParentRegion) ||
5098 isOpenMPTaskingDirective(Kind: ParentRegion);
5099 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
5100 // OpenMP [2.16, Nesting of Regions]
5101 // A critical region may not be nested (closely or otherwise) inside a
5102 // critical region with the same name. Note that this restriction is not
5103 // sufficient to prevent deadlock.
5104 SourceLocation PreviousCriticalLoc;
5105 bool DeadLock = Stack->hasDirective(
5106 DPred: [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
5107 const DeclarationNameInfo &DNI,
5108 SourceLocation Loc) {
5109 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
5110 PreviousCriticalLoc = Loc;
5111 return true;
5112 }
5113 return false;
5114 },
5115 FromParent: false /* skip top directive */);
5116 if (DeadLock) {
5117 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_prohibited_region_critical_same_name)
5118 << CurrentName.getName();
5119 if (PreviousCriticalLoc.isValid())
5120 SemaRef.Diag(Loc: PreviousCriticalLoc,
5121 DiagID: diag::note_omp_previous_critical_region);
5122 return true;
5123 }
5124 } else if (CurrentRegion == OMPD_barrier || CurrentRegion == OMPD_scope) {
5125 // OpenMP 5.1 [2.22, Nesting of Regions]
5126 // A scope region may not be closely nested inside a worksharing, loop,
5127 // task, taskloop, critical, ordered, atomic, or masked region.
5128 // OpenMP 5.1 [2.22, Nesting of Regions]
5129 // A barrier region may not be closely nested inside a worksharing, loop,
5130 // task, taskloop, critical, ordered, atomic, or masked region.
5131 NestingProhibited = isOpenMPWorksharingDirective(DKind: ParentRegion) ||
5132 isOpenMPGenericLoopDirective(DKind: ParentRegion) ||
5133 isOpenMPTaskingDirective(Kind: ParentRegion) ||
5134 llvm::is_contained(Set: {OMPD_masked, OMPD_master,
5135 OMPD_critical, OMPD_ordered},
5136 Element: EnclosingConstruct);
5137 } else if (isOpenMPWorksharingDirective(DKind: CurrentRegion) &&
5138 !isOpenMPParallelDirective(DKind: CurrentRegion) &&
5139 !isOpenMPTeamsDirective(DKind: CurrentRegion)) {
5140 // OpenMP 5.1 [2.22, Nesting of Regions]
5141 // A loop region that binds to a parallel region or a worksharing region
5142 // may not be closely nested inside a worksharing, loop, task, taskloop,
5143 // critical, ordered, atomic, or masked region.
5144 NestingProhibited = isOpenMPWorksharingDirective(DKind: ParentRegion) ||
5145 isOpenMPGenericLoopDirective(DKind: ParentRegion) ||
5146 isOpenMPTaskingDirective(Kind: ParentRegion) ||
5147 llvm::is_contained(Set: {OMPD_masked, OMPD_master,
5148 OMPD_critical, OMPD_ordered},
5149 Element: EnclosingConstruct);
5150 Recommend = ShouldBeInParallelRegion;
5151 } else if (CurrentRegion == OMPD_ordered) {
5152 // OpenMP [2.16, Nesting of Regions]
5153 // An ordered region may not be closely nested inside a critical,
5154 // atomic, or explicit task region.
5155 // An ordered region must be closely nested inside a loop region (or
5156 // parallel loop region) with an ordered clause.
5157 // OpenMP [2.8.1,simd Construct, Restrictions]
5158 // An ordered construct with the simd clause is the only OpenMP construct
5159 // that can appear in the simd region.
5160 NestingProhibited = EnclosingConstruct == OMPD_critical ||
5161 isOpenMPTaskingDirective(Kind: ParentRegion) ||
5162 !(isOpenMPSimdDirective(DKind: ParentRegion) ||
5163 Stack->isParentOrderedRegion());
5164 Recommend = ShouldBeInOrderedRegion;
5165 } else if (isOpenMPNestingTeamsDirective(DKind: CurrentRegion)) {
5166 // OpenMP [2.16, Nesting of Regions]
5167 // If specified, a teams construct must be contained within a target
5168 // construct.
5169 NestingProhibited =
5170 (OMPVersion <= 45 && EnclosingConstruct != OMPD_target) ||
5171 (OMPVersion >= 50 && EnclosingConstruct != OMPD_unknown &&
5172 EnclosingConstruct != OMPD_target);
5173 OrphanSeen = ParentRegion == OMPD_unknown;
5174 Recommend = ShouldBeInTargetRegion;
5175 } else if (CurrentRegion == OMPD_scan) {
5176 if (OMPVersion >= 50) {
5177 // OpenMP spec 5.0 and 5.1 require scan to be directly enclosed by for,
5178 // simd, or for simd. This has to take into account combined directives.
5179 // In 5.2 this seems to be implied by the fact that the specified
5180 // separated constructs are do, for, and simd.
5181 NestingProhibited = !llvm::is_contained(
5182 Set: {OMPD_for, OMPD_simd, OMPD_for_simd}, Element: EnclosingConstruct);
5183 } else {
5184 NestingProhibited = true;
5185 }
5186 OrphanSeen = ParentRegion == OMPD_unknown;
5187 Recommend = ShouldBeInLoopSimdRegion;
5188 }
5189 if (!NestingProhibited && !isOpenMPTargetExecutionDirective(DKind: CurrentRegion) &&
5190 !isOpenMPTargetDataManagementDirective(DKind: CurrentRegion) &&
5191 EnclosingConstruct == OMPD_teams) {
5192 // OpenMP [5.1, 2.22, Nesting of Regions]
5193 // distribute, distribute simd, distribute parallel worksharing-loop,
5194 // distribute parallel worksharing-loop SIMD, loop, parallel regions,
5195 // including any parallel regions arising from combined constructs,
5196 // omp_get_num_teams() regions, and omp_get_team_num() regions are the
5197 // only OpenMP regions that may be strictly nested inside the teams
5198 // region.
5199 //
5200 // As an extension, we permit atomic within teams as well.
5201 NestingProhibited = !isOpenMPParallelDirective(DKind: CurrentRegion) &&
5202 !isOpenMPDistributeDirective(DKind: CurrentRegion) &&
5203 CurrentRegion != OMPD_loop &&
5204 !(SemaRef.getLangOpts().OpenMPExtensions &&
5205 CurrentRegion == OMPD_atomic);
5206 Recommend = ShouldBeInParallelRegion;
5207 }
5208 if (!NestingProhibited && CurrentRegion == OMPD_loop) {
5209 // OpenMP [5.1, 2.11.7, loop Construct, Restrictions]
5210 // If the bind clause is present on the loop construct and binding is
5211 // teams then the corresponding loop region must be strictly nested inside
5212 // a teams region.
5213 NestingProhibited =
5214 BindKind == OMPC_BIND_teams && EnclosingConstruct != OMPD_teams;
5215 Recommend = ShouldBeInTeamsRegion;
5216 }
5217 if (!NestingProhibited && isOpenMPNestingDistributeDirective(DKind: CurrentRegion)) {
5218 // OpenMP 4.5 [2.17 Nesting of Regions]
5219 // The region associated with the distribute construct must be strictly
5220 // nested inside a teams region
5221 NestingProhibited = EnclosingConstruct != OMPD_teams;
5222 Recommend = ShouldBeInTeamsRegion;
5223 }
5224 if (!NestingProhibited &&
5225 (isOpenMPTargetExecutionDirective(DKind: CurrentRegion) ||
5226 isOpenMPTargetDataManagementDirective(DKind: CurrentRegion))) {
5227 // OpenMP 4.5 [2.17 Nesting of Regions]
5228 // If a target, target update, target data, target enter data, or
5229 // target exit data construct is encountered during execution of a
5230 // target region, the behavior is unspecified.
5231 NestingProhibited = Stack->hasDirective(
5232 DPred: [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
5233 SourceLocation) {
5234 if (isOpenMPTargetExecutionDirective(DKind: K)) {
5235 OffendingRegion = K;
5236 return true;
5237 }
5238 return false;
5239 },
5240 FromParent: false /* don't skip top directive */);
5241 CloseNesting = false;
5242 }
5243 if (NestingProhibited) {
5244 if (OrphanSeen) {
5245 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_orphaned_device_directive)
5246 << getOpenMPDirectiveName(D: CurrentRegion, Ver: OMPVersion) << Recommend;
5247 } else {
5248 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_prohibited_region)
5249 << CloseNesting << getOpenMPDirectiveName(D: OffendingRegion, Ver: OMPVersion)
5250 << Recommend << getOpenMPDirectiveName(D: CurrentRegion, Ver: OMPVersion);
5251 }
5252 return true;
5253 }
5254 return false;
5255}
5256
5257struct Kind2Unsigned {
5258 using argument_type = OpenMPDirectiveKind;
5259 unsigned operator()(argument_type DK) { return unsigned(DK); }
5260};
5261static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
5262 ArrayRef<OMPClause *> Clauses,
5263 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
5264 bool ErrorFound = false;
5265 unsigned NamedModifiersNumber = 0;
5266 llvm::IndexedMap<const OMPIfClause *, Kind2Unsigned> FoundNameModifiers;
5267 FoundNameModifiers.resize(S: llvm::omp::Directive_enumSize + 1);
5268 SmallVector<SourceLocation, 4> NameModifierLoc;
5269 unsigned OMPVersion = S.getLangOpts().OpenMP;
5270 for (const OMPClause *C : Clauses) {
5271 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(Val: C)) {
5272 // At most one if clause without a directive-name-modifier can appear on
5273 // the directive.
5274 OpenMPDirectiveKind CurNM = IC->getNameModifier();
5275 auto &FNM = FoundNameModifiers[CurNM];
5276 if (FNM) {
5277 S.Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_more_one_clause)
5278 << getOpenMPDirectiveName(D: Kind, Ver: OMPVersion)
5279 << getOpenMPClauseNameForDiag(C: OMPC_if) << (CurNM != OMPD_unknown)
5280 << getOpenMPDirectiveName(D: CurNM, Ver: OMPVersion);
5281 ErrorFound = true;
5282 } else if (CurNM != OMPD_unknown) {
5283 NameModifierLoc.push_back(Elt: IC->getNameModifierLoc());
5284 ++NamedModifiersNumber;
5285 }
5286 FNM = IC;
5287 if (CurNM == OMPD_unknown)
5288 continue;
5289 // Check if the specified name modifier is allowed for the current
5290 // directive.
5291 // At most one if clause with the particular directive-name-modifier can
5292 // appear on the directive.
5293 if (!llvm::is_contained(Range&: AllowedNameModifiers, Element: CurNM)) {
5294 S.Diag(Loc: IC->getNameModifierLoc(),
5295 DiagID: diag::err_omp_wrong_if_directive_name_modifier)
5296 << getOpenMPDirectiveName(D: CurNM, Ver: OMPVersion)
5297 << getOpenMPDirectiveName(D: Kind, Ver: OMPVersion);
5298 ErrorFound = true;
5299 }
5300 }
5301 }
5302 // If any if clause on the directive includes a directive-name-modifier then
5303 // all if clauses on the directive must include a directive-name-modifier.
5304 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
5305 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
5306 S.Diag(Loc: FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
5307 DiagID: diag::err_omp_no_more_if_clause);
5308 } else {
5309 std::string Values;
5310 std::string Sep(", ");
5311 unsigned AllowedCnt = 0;
5312 unsigned TotalAllowedNum =
5313 AllowedNameModifiers.size() - NamedModifiersNumber;
5314 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
5315 ++Cnt) {
5316 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
5317 if (!FoundNameModifiers[NM]) {
5318 Values += "'";
5319 Values += getOpenMPDirectiveName(D: NM, Ver: OMPVersion);
5320 Values += "'";
5321 if (AllowedCnt + 2 == TotalAllowedNum)
5322 Values += " or ";
5323 else if (AllowedCnt + 1 != TotalAllowedNum)
5324 Values += Sep;
5325 ++AllowedCnt;
5326 }
5327 }
5328 S.Diag(Loc: FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
5329 DiagID: diag::err_omp_unnamed_if_clause)
5330 << (TotalAllowedNum > 1) << Values;
5331 }
5332 for (SourceLocation Loc : NameModifierLoc) {
5333 S.Diag(Loc, DiagID: diag::note_omp_previous_named_if_clause);
5334 }
5335 ErrorFound = true;
5336 }
5337 return ErrorFound;
5338}
5339
5340static std::pair<ValueDecl *, bool>
5341getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
5342 SourceRange &ERange, bool AllowArraySection,
5343 bool AllowAssumedSizeArray, StringRef DiagType) {
5344 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
5345 RefExpr->containsUnexpandedParameterPack())
5346 return std::make_pair(x: nullptr, y: true);
5347
5348 // OpenMP [3.1, C/C++]
5349 // A list item is a variable name.
5350 // OpenMP [2.9.3.3, Restrictions, p.1]
5351 // A variable that is part of another variable (as an array or
5352 // structure element) cannot appear in a private clause.
5353 //
5354 // OpenMP [6.0]
5355 // 5.2.5 Array Sections, p. 166, L28-29
5356 // When the length is absent and the size of the dimension is not known,
5357 // the array section is an assumed-size array.
5358 // 2 Glossary, p. 23, L4-6
5359 // assumed-size array
5360 // For C/C++, an array section for which the length is absent and the
5361 // size of the dimensions is not known.
5362 // 5.2.5 Array Sections, p. 168, L11
5363 // An assumed-size array can appear only in clauses for which it is
5364 // explicitly allowed.
5365 // 7.4 List Item Privatization, Restrictions, p. 222, L15
5366 // Assumed-size arrays must not be privatized.
5367 RefExpr = RefExpr->IgnoreParens();
5368 enum {
5369 NoArrayExpr = -1,
5370 ArraySubscript = 0,
5371 OMPArraySection = 1
5372 } IsArrayExpr = NoArrayExpr;
5373 if (AllowArraySection) {
5374 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(Val: RefExpr)) {
5375 Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
5376 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Val: Base))
5377 Base = TempASE->getBase()->IgnoreParenImpCasts();
5378 RefExpr = Base;
5379 IsArrayExpr = ArraySubscript;
5380 } else if (auto *OASE = dyn_cast_or_null<ArraySectionExpr>(Val: RefExpr)) {
5381 Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
5382 if (S.getLangOpts().OpenMP >= 60 && !AllowAssumedSizeArray &&
5383 OASE->getColonLocFirst().isValid() && !OASE->getLength()) {
5384 QualType BaseType = ArraySectionExpr::getBaseOriginalType(Base);
5385 if (BaseType.isNull() || (!BaseType->isConstantArrayType() &&
5386 !BaseType->isVariableArrayType())) {
5387 S.Diag(Loc: OASE->getColonLocFirst(),
5388 DiagID: diag::err_omp_section_length_undefined)
5389 << (!BaseType.isNull() && BaseType->isArrayType());
5390 return std::make_pair(x: nullptr, y: false);
5391 }
5392 }
5393 while (auto *TempOASE = dyn_cast<ArraySectionExpr>(Val: Base))
5394 Base = TempOASE->getBase()->IgnoreParenImpCasts();
5395 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Val: Base))
5396 Base = TempASE->getBase()->IgnoreParenImpCasts();
5397 RefExpr = Base;
5398 IsArrayExpr = OMPArraySection;
5399 }
5400 }
5401 ELoc = RefExpr->getExprLoc();
5402 ERange = RefExpr->getSourceRange();
5403 RefExpr = RefExpr->IgnoreParenImpCasts();
5404 auto *DE = dyn_cast_or_null<DeclRefExpr>(Val: RefExpr);
5405 auto *ME = dyn_cast_or_null<MemberExpr>(Val: RefExpr);
5406 if ((!DE || !isa<VarDecl>(Val: DE->getDecl())) &&
5407 (S.getCurrentThisType().isNull() || !ME ||
5408 !isa<CXXThisExpr>(Val: ME->getBase()->IgnoreParenImpCasts()) ||
5409 !isa<FieldDecl>(Val: ME->getMemberDecl()))) {
5410 if (IsArrayExpr != NoArrayExpr) {
5411 S.Diag(Loc: ELoc, DiagID: diag::err_omp_expected_base_var_name)
5412 << IsArrayExpr << ERange;
5413 } else if (!DiagType.empty()) {
5414 unsigned DiagSelect = S.getLangOpts().CPlusPlus
5415 ? (S.getCurrentThisType().isNull() ? 1 : 2)
5416 : 0;
5417 S.Diag(Loc: ELoc, DiagID: diag::err_omp_expected_var_name_member_expr_with_type)
5418 << DiagSelect << DiagType << ERange;
5419 } else {
5420 S.Diag(Loc: ELoc,
5421 DiagID: AllowArraySection
5422 ? diag::err_omp_expected_var_name_member_expr_or_array_item
5423 : diag::err_omp_expected_var_name_member_expr)
5424 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
5425 }
5426 return std::make_pair(x: nullptr, y: false);
5427 }
5428 return std::make_pair(
5429 x: getCanonicalDecl(D: DE ? DE->getDecl() : ME->getMemberDecl()), y: false);
5430}
5431
5432namespace {
5433/// Checks if the allocator is used in uses_allocators clause to be allowed in
5434/// target regions.
5435class AllocatorChecker final : public ConstStmtVisitor<AllocatorChecker, bool> {
5436 DSAStackTy *S = nullptr;
5437
5438public:
5439 bool VisitDeclRefExpr(const DeclRefExpr *E) {
5440 return S->isUsesAllocatorsDecl(D: E->getDecl())
5441 .value_or(u: DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) ==
5442 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait;
5443 }
5444 bool VisitStmt(const Stmt *S) {
5445 for (const Stmt *Child : S->children()) {
5446 if (Child && Visit(S: Child))
5447 return true;
5448 }
5449 return false;
5450 }
5451 explicit AllocatorChecker(DSAStackTy *S) : S(S) {}
5452};
5453} // namespace
5454
5455static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
5456 ArrayRef<OMPClause *> Clauses) {
5457 assert(!S.CurContext->isDependentContext() &&
5458 "Expected non-dependent context.");
5459 auto AllocateRange =
5460 llvm::make_filter_range(Range&: Clauses, Pred: OMPAllocateClause::classof);
5461 llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>> DeclToCopy;
5462 auto PrivateRange = llvm::make_filter_range(Range&: Clauses, Pred: [](const OMPClause *C) {
5463 return isOpenMPPrivate(Kind: C->getClauseKind());
5464 });
5465 for (OMPClause *Cl : PrivateRange) {
5466 MutableArrayRef<Expr *>::iterator I, It, Et;
5467 if (Cl->getClauseKind() == OMPC_private) {
5468 auto *PC = cast<OMPPrivateClause>(Val: Cl);
5469 I = PC->private_copies().begin();
5470 It = PC->varlist_begin();
5471 Et = PC->varlist_end();
5472 } else if (Cl->getClauseKind() == OMPC_firstprivate) {
5473 auto *PC = cast<OMPFirstprivateClause>(Val: Cl);
5474 I = PC->private_copies().begin();
5475 It = PC->varlist_begin();
5476 Et = PC->varlist_end();
5477 } else if (Cl->getClauseKind() == OMPC_lastprivate) {
5478 auto *PC = cast<OMPLastprivateClause>(Val: Cl);
5479 I = PC->private_copies().begin();
5480 It = PC->varlist_begin();
5481 Et = PC->varlist_end();
5482 } else if (Cl->getClauseKind() == OMPC_linear) {
5483 auto *PC = cast<OMPLinearClause>(Val: Cl);
5484 I = PC->privates().begin();
5485 It = PC->varlist_begin();
5486 Et = PC->varlist_end();
5487 } else if (Cl->getClauseKind() == OMPC_reduction) {
5488 auto *PC = cast<OMPReductionClause>(Val: Cl);
5489 I = PC->privates().begin();
5490 It = PC->varlist_begin();
5491 Et = PC->varlist_end();
5492 } else if (Cl->getClauseKind() == OMPC_task_reduction) {
5493 auto *PC = cast<OMPTaskReductionClause>(Val: Cl);
5494 I = PC->privates().begin();
5495 It = PC->varlist_begin();
5496 Et = PC->varlist_end();
5497 } else if (Cl->getClauseKind() == OMPC_in_reduction) {
5498 auto *PC = cast<OMPInReductionClause>(Val: Cl);
5499 I = PC->privates().begin();
5500 It = PC->varlist_begin();
5501 Et = PC->varlist_end();
5502 } else {
5503 llvm_unreachable("Expected private clause.");
5504 }
5505 for (Expr *E : llvm::make_range(x: It, y: Et)) {
5506 if (!*I) {
5507 ++I;
5508 continue;
5509 }
5510 SourceLocation ELoc;
5511 SourceRange ERange;
5512 Expr *SimpleRefExpr = E;
5513 auto Res = getPrivateItem(S, RefExpr&: SimpleRefExpr, ELoc, ERange,
5514 /*AllowArraySection=*/true);
5515 DeclToCopy.try_emplace(Key: Res.first,
5516 Args: cast<VarDecl>(Val: cast<DeclRefExpr>(Val: *I)->getDecl()));
5517 ++I;
5518 }
5519 }
5520 for (OMPClause *C : AllocateRange) {
5521 auto *AC = cast<OMPAllocateClause>(Val: C);
5522 if (S.getLangOpts().OpenMP >= 50 &&
5523 !Stack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>() &&
5524 isOpenMPTargetExecutionDirective(DKind: Stack->getCurrentDirective()) &&
5525 AC->getAllocator()) {
5526 Expr *Allocator = AC->getAllocator();
5527 // OpenMP, 2.12.5 target Construct
5528 // Memory allocators that do not appear in a uses_allocators clause cannot
5529 // appear as an allocator in an allocate clause or be used in the target
5530 // region unless a requires directive with the dynamic_allocators clause
5531 // is present in the same compilation unit.
5532 AllocatorChecker Checker(Stack);
5533 if (Checker.Visit(S: Allocator))
5534 S.Diag(Loc: Allocator->getExprLoc(),
5535 DiagID: diag::err_omp_allocator_not_in_uses_allocators)
5536 << Allocator->getSourceRange();
5537 }
5538 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
5539 getAllocatorKind(S, Stack, Allocator: AC->getAllocator());
5540 // OpenMP, 2.11.4 allocate Clause, Restrictions.
5541 // For task, taskloop or target directives, allocation requests to memory
5542 // allocators with the trait access set to thread result in unspecified
5543 // behavior.
5544 if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc &&
5545 (isOpenMPTaskingDirective(Kind: Stack->getCurrentDirective()) ||
5546 isOpenMPTargetExecutionDirective(DKind: Stack->getCurrentDirective()))) {
5547 unsigned OMPVersion = S.getLangOpts().OpenMP;
5548 S.Diag(Loc: AC->getAllocator()->getExprLoc(),
5549 DiagID: diag::warn_omp_allocate_thread_on_task_target_directive)
5550 << getOpenMPDirectiveName(D: Stack->getCurrentDirective(), Ver: OMPVersion);
5551 }
5552 for (Expr *E : AC->varlist()) {
5553 SourceLocation ELoc;
5554 SourceRange ERange;
5555 Expr *SimpleRefExpr = E;
5556 auto Res = getPrivateItem(S, RefExpr&: SimpleRefExpr, ELoc, ERange);
5557 ValueDecl *VD = Res.first;
5558 if (!VD)
5559 continue;
5560 DSAStackTy::DSAVarData Data = Stack->getTopDSA(D: VD, /*FromParent=*/false);
5561 if (!isOpenMPPrivate(Kind: Data.CKind)) {
5562 S.Diag(Loc: E->getExprLoc(),
5563 DiagID: diag::err_omp_expected_private_copy_for_allocate);
5564 continue;
5565 }
5566 VarDecl *PrivateVD = DeclToCopy[VD];
5567 if (checkPreviousOMPAllocateAttribute(S, Stack, RefExpr: E, VD: PrivateVD,
5568 AllocatorKind, Allocator: AC->getAllocator()))
5569 continue;
5570 applyOMPAllocateAttribute(S, VD: PrivateVD, AllocatorKind, Allocator: AC->getAllocator(),
5571 Alignment: AC->getAlignment(), SR: E->getSourceRange());
5572 }
5573 }
5574}
5575
5576namespace {
5577/// Rewrite statements and expressions for Sema \p Actions CurContext.
5578///
5579/// Used to wrap already parsed statements/expressions into a new CapturedStmt
5580/// context. DeclRefExpr used inside the new context are changed to refer to the
5581/// captured variable instead.
5582class CaptureVars : public TreeTransform<CaptureVars> {
5583 using BaseTransform = TreeTransform<CaptureVars>;
5584
5585public:
5586 CaptureVars(Sema &Actions) : BaseTransform(Actions) {}
5587
5588 bool AlwaysRebuild() { return true; }
5589};
5590} // namespace
5591
5592static VarDecl *precomputeExpr(Sema &Actions,
5593 SmallVectorImpl<Stmt *> &BodyStmts, Expr *E,
5594 StringRef Name) {
5595 Expr *NewE = AssertSuccess(R: CaptureVars(Actions).TransformExpr(E));
5596 VarDecl *NewVar = buildVarDecl(SemaRef&: Actions, Loc: {}, Type: NewE->getType(), Name, Attrs: nullptr,
5597 OrigRef: dyn_cast<DeclRefExpr>(Val: E->IgnoreImplicit()));
5598 auto *NewDeclStmt = cast<DeclStmt>(Val: AssertSuccess(
5599 R: Actions.ActOnDeclStmt(Decl: Actions.ConvertDeclToDeclGroup(Ptr: NewVar), StartLoc: {}, EndLoc: {})));
5600 Actions.AddInitializerToDecl(dcl: NewDeclStmt->getSingleDecl(), init: NewE, DirectInit: false);
5601 BodyStmts.push_back(Elt: NewDeclStmt);
5602 return NewVar;
5603}
5604
5605/// Create a closure that computes the number of iterations of a loop.
5606///
5607/// \param Actions The Sema object.
5608/// \param LogicalTy Type for the logical iteration number.
5609/// \param Rel Comparison operator of the loop condition.
5610/// \param StartExpr Value of the loop counter at the first iteration.
5611/// \param StopExpr Expression the loop counter is compared against in the loop
5612/// condition. \param StepExpr Amount of increment after each iteration.
5613///
5614/// \return Closure (CapturedStmt) of the distance calculation.
5615static CapturedStmt *buildDistanceFunc(Sema &Actions, QualType LogicalTy,
5616 BinaryOperator::Opcode Rel,
5617 Expr *StartExpr, Expr *StopExpr,
5618 Expr *StepExpr) {
5619 ASTContext &Ctx = Actions.getASTContext();
5620 TypeSourceInfo *LogicalTSI = Ctx.getTrivialTypeSourceInfo(T: LogicalTy);
5621
5622 // Captured regions currently don't support return values, we use an
5623 // out-parameter instead. All inputs are implicit captures.
5624 // TODO: Instead of capturing each DeclRefExpr occurring in
5625 // StartExpr/StopExpr/Step, these could also be passed as a value capture.
5626 QualType ResultTy = Ctx.getLValueReferenceType(T: LogicalTy);
5627 Sema::CapturedParamNameType Params[] = {{"Distance", ResultTy},
5628 {StringRef(), QualType()}};
5629 Actions.ActOnCapturedRegionStart(Loc: {}, CurScope: nullptr, Kind: CR_Default, Params);
5630
5631 Stmt *Body;
5632 {
5633 Sema::CompoundScopeRAII CompoundScope(Actions);
5634 CapturedDecl *CS = cast<CapturedDecl>(Val: Actions.CurContext);
5635
5636 // Get the LValue expression for the result.
5637 ImplicitParamDecl *DistParam = CS->getParam(i: 0);
5638 DeclRefExpr *DistRef = Actions.BuildDeclRefExpr(
5639 D: DistParam, Ty: LogicalTy, VK: VK_LValue, NameInfo: {}, SS: nullptr, FoundD: nullptr, TemplateKWLoc: {}, TemplateArgs: nullptr);
5640
5641 SmallVector<Stmt *, 4> BodyStmts;
5642
5643 // Capture all referenced variable references.
5644 // TODO: Instead of computing NewStart/NewStop/NewStep inside the
5645 // CapturedStmt, we could compute them before and capture the result, to be
5646 // used jointly with the LoopVar function.
5647 VarDecl *NewStart = precomputeExpr(Actions, BodyStmts, E: StartExpr, Name: ".start");
5648 VarDecl *NewStop = precomputeExpr(Actions, BodyStmts, E: StopExpr, Name: ".stop");
5649 VarDecl *NewStep = precomputeExpr(Actions, BodyStmts, E: StepExpr, Name: ".step");
5650 auto BuildVarRef = [&](VarDecl *VD) {
5651 return buildDeclRefExpr(S&: Actions, D: VD, Ty: VD->getType(), Loc: {});
5652 };
5653
5654 IntegerLiteral *Zero = IntegerLiteral::Create(
5655 C: Ctx, V: llvm::APInt(Ctx.getIntWidth(T: LogicalTy), 0), type: LogicalTy, l: {});
5656 IntegerLiteral *One = IntegerLiteral::Create(
5657 C: Ctx, V: llvm::APInt(Ctx.getIntWidth(T: LogicalTy), 1), type: LogicalTy, l: {});
5658 Expr *Dist;
5659 if (Rel == BO_NE) {
5660 // When using a != comparison, the increment can be +1 or -1. This can be
5661 // dynamic at runtime, so we need to check for the direction.
5662 Expr *IsNegStep = AssertSuccess(
5663 R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_LT, LHSExpr: BuildVarRef(NewStep), RHSExpr: Zero));
5664
5665 // Positive increment.
5666 Expr *ForwardRange = AssertSuccess(R: Actions.BuildBinOp(
5667 S: nullptr, OpLoc: {}, Opc: BO_Sub, LHSExpr: BuildVarRef(NewStop), RHSExpr: BuildVarRef(NewStart)));
5668 ForwardRange = AssertSuccess(
5669 R: Actions.BuildCStyleCastExpr(LParenLoc: {}, Ty: LogicalTSI, RParenLoc: {}, Op: ForwardRange));
5670 Expr *ForwardDist = AssertSuccess(R: Actions.BuildBinOp(
5671 S: nullptr, OpLoc: {}, Opc: BO_Div, LHSExpr: ForwardRange, RHSExpr: BuildVarRef(NewStep)));
5672
5673 // Negative increment.
5674 Expr *BackwardRange = AssertSuccess(R: Actions.BuildBinOp(
5675 S: nullptr, OpLoc: {}, Opc: BO_Sub, LHSExpr: BuildVarRef(NewStart), RHSExpr: BuildVarRef(NewStop)));
5676 BackwardRange = AssertSuccess(
5677 R: Actions.BuildCStyleCastExpr(LParenLoc: {}, Ty: LogicalTSI, RParenLoc: {}, Op: BackwardRange));
5678 Expr *NegIncAmount = AssertSuccess(
5679 R: Actions.BuildUnaryOp(S: nullptr, OpLoc: {}, Opc: UO_Minus, Input: BuildVarRef(NewStep)));
5680 Expr *BackwardDist = AssertSuccess(
5681 R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Div, LHSExpr: BackwardRange, RHSExpr: NegIncAmount));
5682
5683 // Use the appropriate case.
5684 Dist = AssertSuccess(R: Actions.ActOnConditionalOp(
5685 QuestionLoc: {}, ColonLoc: {}, CondExpr: IsNegStep, LHSExpr: BackwardDist, RHSExpr: ForwardDist));
5686 } else {
5687 assert((Rel == BO_LT || Rel == BO_LE || Rel == BO_GE || Rel == BO_GT) &&
5688 "Expected one of these relational operators");
5689
5690 // We can derive the direction from any other comparison operator. It is
5691 // non well-formed OpenMP if Step increments/decrements in the other
5692 // directions. Whether at least the first iteration passes the loop
5693 // condition.
5694 Expr *HasAnyIteration = AssertSuccess(R: Actions.BuildBinOp(
5695 S: nullptr, OpLoc: {}, Opc: Rel, LHSExpr: BuildVarRef(NewStart), RHSExpr: BuildVarRef(NewStop)));
5696
5697 // Compute the range between first and last counter value.
5698 Expr *Range;
5699 if (Rel == BO_GE || Rel == BO_GT)
5700 Range = AssertSuccess(R: Actions.BuildBinOp(
5701 S: nullptr, OpLoc: {}, Opc: BO_Sub, LHSExpr: BuildVarRef(NewStart), RHSExpr: BuildVarRef(NewStop)));
5702 else
5703 Range = AssertSuccess(R: Actions.BuildBinOp(
5704 S: nullptr, OpLoc: {}, Opc: BO_Sub, LHSExpr: BuildVarRef(NewStop), RHSExpr: BuildVarRef(NewStart)));
5705
5706 // Ensure unsigned range space.
5707 Range =
5708 AssertSuccess(R: Actions.BuildCStyleCastExpr(LParenLoc: {}, Ty: LogicalTSI, RParenLoc: {}, Op: Range));
5709
5710 if (Rel == BO_LE || Rel == BO_GE) {
5711 // Add one to the range if the relational operator is inclusive.
5712 Range =
5713 AssertSuccess(R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Add, LHSExpr: Range, RHSExpr: One));
5714 }
5715
5716 // Divide by the absolute step amount. If the range is not a multiple of
5717 // the step size, rounding-up the effective upper bound ensures that the
5718 // last iteration is included.
5719 // Note that the rounding-up may cause an overflow in a temporary that
5720 // could be avoided, but would have occurred in a C-style for-loop as
5721 // well.
5722 Expr *Divisor = BuildVarRef(NewStep);
5723 if (Rel == BO_GE || Rel == BO_GT)
5724 Divisor =
5725 AssertSuccess(R: Actions.BuildUnaryOp(S: nullptr, OpLoc: {}, Opc: UO_Minus, Input: Divisor));
5726 Expr *DivisorMinusOne =
5727 AssertSuccess(R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Sub, LHSExpr: Divisor, RHSExpr: One));
5728 Expr *RangeRoundUp = AssertSuccess(
5729 R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Add, LHSExpr: Range, RHSExpr: DivisorMinusOne));
5730 Dist = AssertSuccess(
5731 R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Div, LHSExpr: RangeRoundUp, RHSExpr: Divisor));
5732
5733 // If there is not at least one iteration, the range contains garbage. Fix
5734 // to zero in this case.
5735 Dist = AssertSuccess(
5736 R: Actions.ActOnConditionalOp(QuestionLoc: {}, ColonLoc: {}, CondExpr: HasAnyIteration, LHSExpr: Dist, RHSExpr: Zero));
5737 }
5738
5739 // Assign the result to the out-parameter.
5740 Stmt *ResultAssign = AssertSuccess(R: Actions.BuildBinOp(
5741 S: Actions.getCurScope(), OpLoc: {}, Opc: BO_Assign, LHSExpr: DistRef, RHSExpr: Dist));
5742 BodyStmts.push_back(Elt: ResultAssign);
5743
5744 Body = AssertSuccess(R: Actions.ActOnCompoundStmt(L: {}, R: {}, Elts: BodyStmts, isStmtExpr: false));
5745 }
5746
5747 return cast<CapturedStmt>(
5748 Val: AssertSuccess(R: Actions.ActOnCapturedRegionEnd(S: Body)));
5749}
5750
5751/// Create a closure that computes the loop variable from the logical iteration
5752/// number.
5753///
5754/// \param Actions The Sema object.
5755/// \param LoopVarTy Type for the loop variable used for result value.
5756/// \param LogicalTy Type for the logical iteration number.
5757/// \param StartExpr Value of the loop counter at the first iteration.
5758/// \param Step Amount of increment after each iteration.
5759/// \param Deref Whether the loop variable is a dereference of the loop
5760/// counter variable.
5761///
5762/// \return Closure (CapturedStmt) of the loop value calculation.
5763static CapturedStmt *buildLoopVarFunc(Sema &Actions, QualType LoopVarTy,
5764 QualType LogicalTy,
5765 DeclRefExpr *StartExpr, Expr *Step,
5766 bool Deref) {
5767 ASTContext &Ctx = Actions.getASTContext();
5768
5769 // Pass the result as an out-parameter. Passing as return value would require
5770 // the OpenMPIRBuilder to know additional C/C++ semantics, such as how to
5771 // invoke a copy constructor.
5772 QualType TargetParamTy = Ctx.getLValueReferenceType(T: LoopVarTy);
5773 SemaOpenMP::CapturedParamNameType Params[] = {{"LoopVar", TargetParamTy},
5774 {"Logical", LogicalTy},
5775 {StringRef(), QualType()}};
5776 Actions.ActOnCapturedRegionStart(Loc: {}, CurScope: nullptr, Kind: CR_Default, Params);
5777
5778 // Capture the initial iterator which represents the LoopVar value at the
5779 // zero's logical iteration. Since the original ForStmt/CXXForRangeStmt update
5780 // it in every iteration, capture it by value before it is modified.
5781 VarDecl *StartVar = cast<VarDecl>(Val: StartExpr->getDecl());
5782 bool Invalid = Actions.tryCaptureVariable(Var: StartVar, Loc: {},
5783 Kind: TryCaptureKind::ExplicitByVal, EllipsisLoc: {});
5784 (void)Invalid;
5785 assert(!Invalid && "Expecting capture-by-value to work.");
5786
5787 Expr *Body;
5788 {
5789 Sema::CompoundScopeRAII CompoundScope(Actions);
5790 auto *CS = cast<CapturedDecl>(Val: Actions.CurContext);
5791
5792 ImplicitParamDecl *TargetParam = CS->getParam(i: 0);
5793 DeclRefExpr *TargetRef = Actions.BuildDeclRefExpr(
5794 D: TargetParam, Ty: LoopVarTy, VK: VK_LValue, NameInfo: {}, SS: nullptr, FoundD: nullptr, TemplateKWLoc: {}, TemplateArgs: nullptr);
5795 ImplicitParamDecl *IndvarParam = CS->getParam(i: 1);
5796 DeclRefExpr *LogicalRef = Actions.BuildDeclRefExpr(
5797 D: IndvarParam, Ty: LogicalTy, VK: VK_LValue, NameInfo: {}, SS: nullptr, FoundD: nullptr, TemplateKWLoc: {}, TemplateArgs: nullptr);
5798
5799 // Capture the Start expression.
5800 CaptureVars Recap(Actions);
5801 Expr *NewStart = AssertSuccess(R: Recap.TransformExpr(E: StartExpr));
5802 Expr *NewStep = AssertSuccess(R: Recap.TransformExpr(E: Step));
5803
5804 Expr *Skip = AssertSuccess(
5805 R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Mul, LHSExpr: NewStep, RHSExpr: LogicalRef));
5806 // TODO: Explicitly cast to the iterator's difference_type instead of
5807 // relying on implicit conversion.
5808 Expr *Advanced =
5809 AssertSuccess(R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Add, LHSExpr: NewStart, RHSExpr: Skip));
5810
5811 if (Deref) {
5812 // For range-based for-loops convert the loop counter value to a concrete
5813 // loop variable value by dereferencing the iterator.
5814 Advanced =
5815 AssertSuccess(R: Actions.BuildUnaryOp(S: nullptr, OpLoc: {}, Opc: UO_Deref, Input: Advanced));
5816 }
5817
5818 // Assign the result to the output parameter.
5819 Body = AssertSuccess(R: Actions.BuildBinOp(S: Actions.getCurScope(), OpLoc: {},
5820 Opc: BO_Assign, LHSExpr: TargetRef, RHSExpr: Advanced));
5821 }
5822 return cast<CapturedStmt>(
5823 Val: AssertSuccess(R: Actions.ActOnCapturedRegionEnd(S: Body)));
5824}
5825
5826StmtResult SemaOpenMP::ActOnOpenMPCanonicalLoop(Stmt *AStmt) {
5827 ASTContext &Ctx = getASTContext();
5828
5829 // Extract the common elements of ForStmt and CXXForRangeStmt:
5830 // Loop variable, repeat condition, increment
5831 Expr *Cond, *Inc;
5832 VarDecl *LIVDecl, *LUVDecl;
5833 if (auto *For = dyn_cast<ForStmt>(Val: AStmt)) {
5834 Stmt *Init = For->getInit();
5835 if (auto *LCVarDeclStmt = dyn_cast<DeclStmt>(Val: Init)) {
5836 // For statement declares loop variable.
5837 LIVDecl = cast<VarDecl>(Val: LCVarDeclStmt->getSingleDecl());
5838 } else if (auto *LCAssign = dyn_cast<BinaryOperator>(Val: Init)) {
5839 // For statement reuses variable.
5840 assert(LCAssign->getOpcode() == BO_Assign &&
5841 "init part must be a loop variable assignment");
5842 auto *CounterRef = cast<DeclRefExpr>(Val: LCAssign->getLHS());
5843 LIVDecl = cast<VarDecl>(Val: CounterRef->getDecl());
5844 } else
5845 llvm_unreachable("Cannot determine loop variable");
5846 LUVDecl = LIVDecl;
5847
5848 Cond = For->getCond();
5849 Inc = For->getInc();
5850 } else if (auto *RangeFor = dyn_cast<CXXForRangeStmt>(Val: AStmt)) {
5851 DeclStmt *BeginStmt = RangeFor->getBeginStmt();
5852 LIVDecl = cast<VarDecl>(Val: BeginStmt->getSingleDecl());
5853 LUVDecl = RangeFor->getLoopVariable();
5854
5855 Cond = RangeFor->getCond();
5856 Inc = RangeFor->getInc();
5857 } else
5858 llvm_unreachable("unhandled kind of loop");
5859
5860 QualType CounterTy = LIVDecl->getType();
5861 QualType LVTy = LUVDecl->getType();
5862
5863 // Analyze the loop condition.
5864 Expr *LHS, *RHS;
5865 BinaryOperator::Opcode CondRel;
5866 Cond = Cond->IgnoreImplicit();
5867 if (auto *CondBinExpr = dyn_cast<BinaryOperator>(Val: Cond)) {
5868 LHS = CondBinExpr->getLHS();
5869 RHS = CondBinExpr->getRHS();
5870 CondRel = CondBinExpr->getOpcode();
5871 } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Val: Cond)) {
5872 assert(CondCXXOp->getNumArgs() == 2 && "Comparison should have 2 operands");
5873 LHS = CondCXXOp->getArg(Arg: 0);
5874 RHS = CondCXXOp->getArg(Arg: 1);
5875 switch (CondCXXOp->getOperator()) {
5876 case OO_ExclaimEqual:
5877 CondRel = BO_NE;
5878 break;
5879 case OO_Less:
5880 CondRel = BO_LT;
5881 break;
5882 case OO_LessEqual:
5883 CondRel = BO_LE;
5884 break;
5885 case OO_Greater:
5886 CondRel = BO_GT;
5887 break;
5888 case OO_GreaterEqual:
5889 CondRel = BO_GE;
5890 break;
5891 default:
5892 llvm_unreachable("unexpected iterator operator");
5893 }
5894 } else
5895 llvm_unreachable("unexpected loop condition");
5896
5897 // Normalize such that the loop counter is on the LHS.
5898 if (!isa<DeclRefExpr>(Val: LHS->IgnoreImplicit()) ||
5899 cast<DeclRefExpr>(Val: LHS->IgnoreImplicit())->getDecl() != LIVDecl) {
5900 std::swap(a&: LHS, b&: RHS);
5901 CondRel = BinaryOperator::reverseComparisonOp(Opc: CondRel);
5902 }
5903 auto *CounterRef = cast<DeclRefExpr>(Val: LHS->IgnoreImplicit());
5904
5905 // Decide the bit width for the logical iteration counter. By default use the
5906 // unsigned ptrdiff_t integer size (for iterators and pointers).
5907 // TODO: For iterators, use iterator::difference_type,
5908 // std::iterator_traits<>::difference_type or decltype(it - end).
5909 QualType LogicalTy = Ctx.getUnsignedPointerDiffType();
5910 if (CounterTy->isIntegerType()) {
5911 unsigned BitWidth = Ctx.getIntWidth(T: CounterTy);
5912 LogicalTy = Ctx.getIntTypeForBitwidth(DestWidth: BitWidth, Signed: false);
5913 }
5914
5915 // Analyze the loop increment.
5916 Expr *Step;
5917 if (auto *IncUn = dyn_cast<UnaryOperator>(Val: Inc)) {
5918 int Direction;
5919 switch (IncUn->getOpcode()) {
5920 case UO_PreInc:
5921 case UO_PostInc:
5922 Direction = 1;
5923 break;
5924 case UO_PreDec:
5925 case UO_PostDec:
5926 Direction = -1;
5927 break;
5928 default:
5929 llvm_unreachable("unhandled unary increment operator");
5930 }
5931 Step = IntegerLiteral::Create(
5932 C: Ctx,
5933 V: llvm::APInt(Ctx.getIntWidth(T: LogicalTy), Direction, /*isSigned=*/true),
5934 type: LogicalTy, l: {});
5935 } else if (auto *IncBin = dyn_cast<BinaryOperator>(Val: Inc)) {
5936 if (IncBin->getOpcode() == BO_AddAssign) {
5937 Step = IncBin->getRHS();
5938 } else if (IncBin->getOpcode() == BO_SubAssign) {
5939 Step = AssertSuccess(
5940 R: SemaRef.BuildUnaryOp(S: nullptr, OpLoc: {}, Opc: UO_Minus, Input: IncBin->getRHS()));
5941 } else
5942 llvm_unreachable("unhandled binary increment operator");
5943 } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Val: Inc)) {
5944 switch (CondCXXOp->getOperator()) {
5945 case OO_PlusPlus:
5946 Step = IntegerLiteral::Create(
5947 C: Ctx, V: llvm::APInt(Ctx.getIntWidth(T: LogicalTy), 1), type: LogicalTy, l: {});
5948 break;
5949 case OO_MinusMinus:
5950 Step = IntegerLiteral::Create(
5951 C: Ctx, V: llvm::APInt(Ctx.getIntWidth(T: LogicalTy), -1), type: LogicalTy, l: {});
5952 break;
5953 case OO_PlusEqual:
5954 Step = CondCXXOp->getArg(Arg: 1);
5955 break;
5956 case OO_MinusEqual:
5957 Step = AssertSuccess(
5958 R: SemaRef.BuildUnaryOp(S: nullptr, OpLoc: {}, Opc: UO_Minus, Input: CondCXXOp->getArg(Arg: 1)));
5959 break;
5960 default:
5961 llvm_unreachable("unhandled overloaded increment operator");
5962 }
5963 } else
5964 llvm_unreachable("unknown increment expression");
5965
5966 CapturedStmt *DistanceFunc =
5967 buildDistanceFunc(Actions&: SemaRef, LogicalTy, Rel: CondRel, StartExpr: LHS, StopExpr: RHS, StepExpr: Step);
5968 CapturedStmt *LoopVarFunc = buildLoopVarFunc(
5969 Actions&: SemaRef, LoopVarTy: LVTy, LogicalTy, StartExpr: CounterRef, Step, Deref: isa<CXXForRangeStmt>(Val: AStmt));
5970 DeclRefExpr *LVRef =
5971 SemaRef.BuildDeclRefExpr(D: LUVDecl, Ty: LUVDecl->getType(), VK: VK_LValue, NameInfo: {},
5972 SS: nullptr, FoundD: nullptr, TemplateKWLoc: {}, TemplateArgs: nullptr);
5973 return OMPCanonicalLoop::create(Ctx: getASTContext(), LoopStmt: AStmt, DistanceFunc,
5974 LoopVarFunc, LoopVarRef: LVRef);
5975}
5976
5977StmtResult SemaOpenMP::ActOnOpenMPLoopnest(Stmt *AStmt) {
5978 // Handle a literal loop.
5979 if (isa<ForStmt>(Val: AStmt) || isa<CXXForRangeStmt>(Val: AStmt))
5980 return ActOnOpenMPCanonicalLoop(AStmt);
5981
5982 // If not a literal loop, it must be the result of a loop transformation.
5983 OMPExecutableDirective *LoopTransform = cast<OMPExecutableDirective>(Val: AStmt);
5984 assert(
5985 isOpenMPLoopTransformationDirective(LoopTransform->getDirectiveKind()) &&
5986 "Loop transformation directive expected");
5987 return LoopTransform;
5988}
5989
5990static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S,
5991 CXXScopeSpec &MapperIdScopeSpec,
5992 const DeclarationNameInfo &MapperId,
5993 QualType Type,
5994 Expr *UnresolvedMapper);
5995
5996/// Perform DFS through the structure/class data members trying to find
5997/// member(s) with user-defined 'default' mapper and generate implicit map
5998/// clauses for such members with the found 'default' mapper.
5999static void
6000processImplicitMapsWithDefaultMappers(Sema &S, DSAStackTy *Stack,
6001 SmallVectorImpl<OMPClause *> &Clauses) {
6002 // Check for the default mapper for data members.
6003 if (S.getLangOpts().OpenMP < 50)
6004 return;
6005 for (int Cnt = 0, EndCnt = Clauses.size(); Cnt < EndCnt; ++Cnt) {
6006 auto *C = dyn_cast<OMPMapClause>(Val: Clauses[Cnt]);
6007 if (!C)
6008 continue;
6009 SmallVector<Expr *, 4> SubExprs;
6010 auto *MI = C->mapperlist_begin();
6011 for (auto I = C->varlist_begin(), End = C->varlist_end(); I != End;
6012 ++I, ++MI) {
6013 // Expression is mapped using mapper - skip it.
6014 if (*MI)
6015 continue;
6016 Expr *E = *I;
6017 // Expression is dependent - skip it, build the mapper when it gets
6018 // instantiated.
6019 if (E->isTypeDependent() || E->isValueDependent() ||
6020 E->containsUnexpandedParameterPack())
6021 continue;
6022 // Array section - need to check for the mapping of the array section
6023 // element.
6024 QualType CanonType = E->getType().getCanonicalType();
6025 if (CanonType->isSpecificBuiltinType(K: BuiltinType::ArraySection)) {
6026 const auto *OASE = cast<ArraySectionExpr>(Val: E->IgnoreParenImpCasts());
6027 QualType BaseType =
6028 ArraySectionExpr::getBaseOriginalType(Base: OASE->getBase());
6029 QualType ElemType;
6030 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
6031 ElemType = ATy->getElementType();
6032 else
6033 ElemType = BaseType->getPointeeType();
6034 CanonType = ElemType;
6035 }
6036
6037 // DFS over data members in structures/classes.
6038 SmallVector<std::pair<QualType, FieldDecl *>, 4> Types(
6039 1, {CanonType, nullptr});
6040 llvm::DenseMap<const Type *, Expr *> Visited;
6041 SmallVector<std::pair<FieldDecl *, unsigned>, 4> ParentChain(
6042 1, {nullptr, 1});
6043 while (!Types.empty()) {
6044 QualType BaseType;
6045 FieldDecl *CurFD;
6046 std::tie(args&: BaseType, args&: CurFD) = Types.pop_back_val();
6047 while (ParentChain.back().second == 0)
6048 ParentChain.pop_back();
6049 --ParentChain.back().second;
6050 if (BaseType.isNull())
6051 continue;
6052 // Only structs/classes are allowed to have mappers.
6053 const RecordDecl *RD = BaseType.getCanonicalType()->getAsRecordDecl();
6054 if (!RD)
6055 continue;
6056 auto It = Visited.find(Val: BaseType.getTypePtr());
6057 if (It == Visited.end()) {
6058 // Try to find the associated user-defined mapper.
6059 CXXScopeSpec MapperIdScopeSpec;
6060 DeclarationNameInfo DefaultMapperId;
6061 DefaultMapperId.setName(S.Context.DeclarationNames.getIdentifier(
6062 ID: &S.Context.Idents.get(Name: "default")));
6063 DefaultMapperId.setLoc(E->getExprLoc());
6064 ExprResult ER = buildUserDefinedMapperRef(
6065 SemaRef&: S, S: Stack->getCurScope(), MapperIdScopeSpec, MapperId: DefaultMapperId,
6066 Type: BaseType, /*UnresolvedMapper=*/nullptr);
6067 if (ER.isInvalid())
6068 continue;
6069 It = Visited.try_emplace(Key: BaseType.getTypePtr(), Args: ER.get()).first;
6070 }
6071 // Found default mapper.
6072 if (It->second) {
6073 auto *OE = new (S.Context) OpaqueValueExpr(E->getExprLoc(), CanonType,
6074 VK_LValue, OK_Ordinary, E);
6075 OE->setIsUnique(/*V=*/true);
6076 Expr *BaseExpr = OE;
6077 for (const auto &P : ParentChain) {
6078 if (P.first) {
6079 BaseExpr = S.BuildMemberExpr(
6080 Base: BaseExpr, /*IsArrow=*/false, OpLoc: E->getExprLoc(),
6081 NNS: NestedNameSpecifierLoc(), TemplateKWLoc: SourceLocation(), Member: P.first,
6082 FoundDecl: DeclAccessPair::make(D: P.first, AS: P.first->getAccess()),
6083 /*HadMultipleCandidates=*/false, MemberNameInfo: DeclarationNameInfo(),
6084 Ty: P.first->getType(), VK: VK_LValue, OK: OK_Ordinary);
6085 BaseExpr = S.DefaultLvalueConversion(E: BaseExpr).get();
6086 }
6087 }
6088 if (CurFD)
6089 BaseExpr = S.BuildMemberExpr(
6090 Base: BaseExpr, /*IsArrow=*/false, OpLoc: E->getExprLoc(),
6091 NNS: NestedNameSpecifierLoc(), TemplateKWLoc: SourceLocation(), Member: CurFD,
6092 FoundDecl: DeclAccessPair::make(D: CurFD, AS: CurFD->getAccess()),
6093 /*HadMultipleCandidates=*/false, MemberNameInfo: DeclarationNameInfo(),
6094 Ty: CurFD->getType(), VK: VK_LValue, OK: OK_Ordinary);
6095 SubExprs.push_back(Elt: BaseExpr);
6096 continue;
6097 }
6098 // Check for the "default" mapper for data members.
6099 bool FirstIter = true;
6100 for (FieldDecl *FD : RD->fields()) {
6101 if (!FD)
6102 continue;
6103 QualType FieldTy = FD->getType();
6104 if (FieldTy.isNull() ||
6105 !(FieldTy->isStructureOrClassType() || FieldTy->isUnionType()))
6106 continue;
6107 if (FirstIter) {
6108 FirstIter = false;
6109 ParentChain.emplace_back(Args&: CurFD, Args: 1);
6110 } else {
6111 ++ParentChain.back().second;
6112 }
6113 Types.emplace_back(Args&: FieldTy, Args&: FD);
6114 }
6115 }
6116 }
6117 if (SubExprs.empty())
6118 continue;
6119 CXXScopeSpec MapperIdScopeSpec;
6120 DeclarationNameInfo MapperId;
6121 if (OMPClause *NewClause = S.OpenMP().ActOnOpenMPMapClause(
6122 IteratorModifier: nullptr, MapTypeModifiers: C->getMapTypeModifiers(), MapTypeModifiersLoc: C->getMapTypeModifiersLoc(),
6123 MapperIdScopeSpec, MapperId, MapType: C->getMapType(),
6124 /*IsMapTypeImplicit=*/true, MapLoc: SourceLocation(), ColonLoc: SourceLocation(),
6125 VarList: SubExprs, Locs: OMPVarListLocTy()))
6126 Clauses.push_back(Elt: NewClause);
6127 }
6128}
6129
6130namespace {
6131/// A 'teams loop' with a nested 'loop bind(parallel)' or generic function
6132/// call in the associated loop-nest cannot be a 'parallel for'.
6133class TeamsLoopChecker final : public ConstStmtVisitor<TeamsLoopChecker> {
6134 Sema &SemaRef;
6135
6136public:
6137 bool teamsLoopCanBeParallelFor() const { return TeamsLoopCanBeParallelFor; }
6138
6139 // Is there a nested OpenMP loop bind(parallel)
6140 void VisitOMPExecutableDirective(const OMPExecutableDirective *D) {
6141 if (D->getDirectiveKind() == llvm::omp::Directive::OMPD_loop) {
6142 if (const auto *C = D->getSingleClause<OMPBindClause>())
6143 if (C->getBindKind() == OMPC_BIND_parallel) {
6144 TeamsLoopCanBeParallelFor = false;
6145 // No need to continue visiting any more
6146 return;
6147 }
6148 }
6149 for (const Stmt *Child : D->children())
6150 if (Child)
6151 Visit(S: Child);
6152 }
6153
6154 void VisitCallExpr(const CallExpr *C) {
6155 // Function calls inhibit parallel loop translation of 'target teams loop'
6156 // unless the assume-no-nested-parallelism flag has been specified.
6157 // OpenMP API runtime library calls do not inhibit parallel loop
6158 // translation, regardless of the assume-no-nested-parallelism.
6159 bool IsOpenMPAPI = false;
6160 auto *FD = dyn_cast_or_null<FunctionDecl>(Val: C->getCalleeDecl());
6161 if (FD) {
6162 std::string Name = FD->getNameInfo().getAsString();
6163 IsOpenMPAPI = Name.find(s: "omp_") == 0;
6164 }
6165 TeamsLoopCanBeParallelFor =
6166 IsOpenMPAPI || SemaRef.getLangOpts().OpenMPNoNestedParallelism;
6167 if (!TeamsLoopCanBeParallelFor)
6168 return;
6169
6170 for (const Stmt *Child : C->children())
6171 if (Child)
6172 Visit(S: Child);
6173 }
6174
6175 void VisitCapturedStmt(const CapturedStmt *S) {
6176 if (!S)
6177 return;
6178 Visit(S: S->getCapturedDecl()->getBody());
6179 }
6180
6181 void VisitStmt(const Stmt *S) {
6182 if (!S)
6183 return;
6184 for (const Stmt *Child : S->children())
6185 if (Child)
6186 Visit(S: Child);
6187 }
6188 explicit TeamsLoopChecker(Sema &SemaRef)
6189 : SemaRef(SemaRef), TeamsLoopCanBeParallelFor(true) {}
6190
6191private:
6192 bool TeamsLoopCanBeParallelFor;
6193};
6194} // namespace
6195
6196static bool teamsLoopCanBeParallelFor(Stmt *AStmt, Sema &SemaRef) {
6197 TeamsLoopChecker Checker(SemaRef);
6198 Checker.Visit(S: AStmt);
6199 return Checker.teamsLoopCanBeParallelFor();
6200}
6201
6202StmtResult SemaOpenMP::ActOnOpenMPExecutableDirective(
6203 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
6204 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
6205 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
6206 assert(isOpenMPExecutableDirective(Kind) && "Unexpected directive category");
6207
6208 StmtResult Res = StmtError();
6209 OpenMPBindClauseKind BindKind = OMPC_BIND_unknown;
6210 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
6211
6212 if (const OMPBindClause *BC =
6213 OMPExecutableDirective::getSingleClause<OMPBindClause>(Clauses))
6214 BindKind = BC->getBindKind();
6215
6216 if (Kind == OMPD_loop && BindKind == OMPC_BIND_unknown) {
6217 const OpenMPDirectiveKind ParentDirective = DSAStack->getParentDirective();
6218
6219 // Setting the enclosing teams or parallel construct for the loop
6220 // directive without bind clause.
6221 // [5.0:129:25-28] If the bind clause is not present on the construct and
6222 // the loop construct is closely nested inside a teams or parallel
6223 // construct, the binding region is the corresponding teams or parallel
6224 // region. If none of those conditions hold, the binding region is not
6225 // defined.
6226 BindKind = OMPC_BIND_thread; // Default bind(thread) if binding is unknown
6227 ArrayRef<OpenMPDirectiveKind> ParentLeafs =
6228 getLeafConstructsOrSelf(D: ParentDirective);
6229
6230 if (ParentDirective == OMPD_unknown) {
6231 Diag(DSAStack->getDefaultDSALocation(),
6232 DiagID: diag::err_omp_bind_required_on_loop);
6233 } else if (ParentLeafs.back() == OMPD_parallel) {
6234 BindKind = OMPC_BIND_parallel;
6235 } else if (ParentLeafs.back() == OMPD_teams) {
6236 BindKind = OMPC_BIND_teams;
6237 }
6238
6239 assert(BindKind != OMPC_BIND_unknown && "Expecting BindKind");
6240
6241 OMPClause *C =
6242 ActOnOpenMPBindClause(Kind: BindKind, KindLoc: SourceLocation(), StartLoc: SourceLocation(),
6243 LParenLoc: SourceLocation(), EndLoc: SourceLocation());
6244 ClausesWithImplicit.push_back(Elt: C);
6245 }
6246
6247 // Diagnose "loop bind(teams)" with "reduction".
6248 if (Kind == OMPD_loop && BindKind == OMPC_BIND_teams) {
6249 for (OMPClause *C : Clauses) {
6250 if (C->getClauseKind() == OMPC_reduction)
6251 Diag(DSAStack->getDefaultDSALocation(),
6252 DiagID: diag::err_omp_loop_reduction_clause);
6253 }
6254 }
6255
6256 // First check CancelRegion which is then used in checkNestingOfRegions.
6257 if (checkCancelRegion(SemaRef, CurrentRegion: Kind, CancelRegion, StartLoc) ||
6258 checkNestingOfRegions(SemaRef, DSAStack, CurrentRegion: Kind, CurrentName: DirName, CancelRegion,
6259 BindKind, StartLoc)) {
6260 return StmtError();
6261 }
6262
6263 // Report affected OpenMP target offloading behavior when in HIP lang-mode.
6264 if (getLangOpts().HIP && (isOpenMPTargetExecutionDirective(DKind: Kind) ||
6265 isOpenMPTargetDataManagementDirective(DKind: Kind)))
6266 Diag(Loc: StartLoc, DiagID: diag::warn_hip_omp_target_directives);
6267
6268 VarsWithInheritedDSAType VarsWithInheritedDSA;
6269 bool ErrorFound = false;
6270 ClausesWithImplicit.append(in_start: Clauses.begin(), in_end: Clauses.end());
6271
6272 if (AStmt && !SemaRef.CurContext->isDependentContext() &&
6273 isOpenMPCapturingDirective(DKind: Kind)) {
6274 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6275
6276 // Check default data sharing attributes for referenced variables.
6277 DSAAttrChecker DSAChecker(DSAStack, SemaRef, cast<CapturedStmt>(Val: AStmt));
6278 int ThisCaptureLevel = getOpenMPCaptureLevels(DKind: Kind);
6279 Stmt *S = AStmt;
6280 while (--ThisCaptureLevel >= 0)
6281 S = cast<CapturedStmt>(Val: S)->getCapturedStmt();
6282 DSAChecker.Visit(S);
6283 if (!isOpenMPTargetDataManagementDirective(DKind: Kind) &&
6284 !isOpenMPTaskingDirective(Kind)) {
6285 // Visit subcaptures to generate implicit clauses for captured vars.
6286 auto *CS = cast<CapturedStmt>(Val: AStmt);
6287 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
6288 getOpenMPCaptureRegions(CaptureRegions, DKind: Kind);
6289 // Ignore outer tasking regions for target directives.
6290 if (CaptureRegions.size() > 1 && CaptureRegions.front() == OMPD_task)
6291 CS = cast<CapturedStmt>(Val: CS->getCapturedStmt());
6292 DSAChecker.visitSubCaptures(S: CS);
6293 }
6294 if (DSAChecker.isErrorFound())
6295 return StmtError();
6296 // Generate list of implicitly defined firstprivate variables.
6297 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
6298 VariableImplicitInfo ImpInfo = DSAChecker.getImplicitInfo();
6299
6300 SmallVector<SourceLocation, NumberOfOMPMapClauseModifiers>
6301 ImplicitMapModifiersLoc[VariableImplicitInfo::DefaultmapKindNum];
6302 // Get the original location of present modifier from Defaultmap clause.
6303 SourceLocation PresentModifierLocs[VariableImplicitInfo::DefaultmapKindNum];
6304 for (OMPClause *C : Clauses) {
6305 if (auto *DMC = dyn_cast<OMPDefaultmapClause>(Val: C))
6306 if (DMC->getDefaultmapModifier() == OMPC_DEFAULTMAP_MODIFIER_present)
6307 PresentModifierLocs[DMC->getDefaultmapKind()] =
6308 DMC->getDefaultmapModifierLoc();
6309 }
6310
6311 for (OpenMPDefaultmapClauseKind K :
6312 llvm::enum_seq_inclusive<OpenMPDefaultmapClauseKind>(
6313 Begin: OpenMPDefaultmapClauseKind(), End: OMPC_DEFAULTMAP_unknown)) {
6314 std::fill_n(first: std::back_inserter(x&: ImplicitMapModifiersLoc[K]),
6315 n: ImpInfo.MapModifiers[K].size(), value: PresentModifierLocs[K]);
6316 }
6317 // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
6318 for (OMPClause *C : Clauses) {
6319 if (auto *IRC = dyn_cast<OMPInReductionClause>(Val: C)) {
6320 for (Expr *E : IRC->taskgroup_descriptors())
6321 if (E)
6322 ImpInfo.Firstprivates.insert(X: E);
6323 }
6324 // OpenMP 5.0, 2.10.1 task Construct
6325 // [detach clause]... The event-handle will be considered as if it was
6326 // specified on a firstprivate clause.
6327 if (auto *DC = dyn_cast<OMPDetachClause>(Val: C))
6328 ImpInfo.Firstprivates.insert(X: DC->getEventHandler());
6329 }
6330 if (!ImpInfo.Firstprivates.empty()) {
6331 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
6332 VarList: ImpInfo.Firstprivates.getArrayRef(), StartLoc: SourceLocation(),
6333 LParenLoc: SourceLocation(), EndLoc: SourceLocation())) {
6334 ClausesWithImplicit.push_back(Elt: Implicit);
6335 ErrorFound = cast<OMPFirstprivateClause>(Val: Implicit)->varlist_size() !=
6336 ImpInfo.Firstprivates.size();
6337 } else {
6338 ErrorFound = true;
6339 }
6340 }
6341 if (!ImpInfo.Privates.empty()) {
6342 if (OMPClause *Implicit = ActOnOpenMPPrivateClause(
6343 VarList: ImpInfo.Privates.getArrayRef(), StartLoc: SourceLocation(),
6344 LParenLoc: SourceLocation(), EndLoc: SourceLocation())) {
6345 ClausesWithImplicit.push_back(Elt: Implicit);
6346 ErrorFound = cast<OMPPrivateClause>(Val: Implicit)->varlist_size() !=
6347 ImpInfo.Privates.size();
6348 } else {
6349 ErrorFound = true;
6350 }
6351 }
6352 // OpenMP 5.0 [2.19.7]
6353 // If a list item appears in a reduction, lastprivate or linear
6354 // clause on a combined target construct then it is treated as
6355 // if it also appears in a map clause with a map-type of tofrom
6356 if (getLangOpts().OpenMP >= 50 && Kind != OMPD_target &&
6357 isOpenMPTargetExecutionDirective(DKind: Kind)) {
6358 SmallVector<Expr *, 4> ImplicitExprs;
6359 for (OMPClause *C : Clauses) {
6360 if (auto *RC = dyn_cast<OMPReductionClause>(Val: C))
6361 for (Expr *E : RC->varlist())
6362 if (!isa<DeclRefExpr>(Val: E->IgnoreParenImpCasts()))
6363 ImplicitExprs.emplace_back(Args&: E);
6364 }
6365 if (!ImplicitExprs.empty()) {
6366 ArrayRef<Expr *> Exprs = ImplicitExprs;
6367 CXXScopeSpec MapperIdScopeSpec;
6368 DeclarationNameInfo MapperId;
6369 if (OMPClause *Implicit = ActOnOpenMPMapClause(
6370 IteratorModifier: nullptr, MapTypeModifiers: OMPC_MAP_MODIFIER_unknown, MapTypeModifiersLoc: SourceLocation(),
6371 MapperIdScopeSpec, MapperId, MapType: OMPC_MAP_tofrom,
6372 /*IsMapTypeImplicit=*/true, MapLoc: SourceLocation(), ColonLoc: SourceLocation(),
6373 VarList: Exprs, Locs: OMPVarListLocTy(), /*NoDiagnose=*/true))
6374 ClausesWithImplicit.emplace_back(Args&: Implicit);
6375 }
6376 }
6377 for (unsigned I = 0; I < VariableImplicitInfo::DefaultmapKindNum; ++I) {
6378 int ClauseKindCnt = -1;
6379 for (unsigned J = 0; J < VariableImplicitInfo::MapKindNum; ++J) {
6380 ArrayRef<Expr *> ImplicitMap = ImpInfo.Mappings[I][J].getArrayRef();
6381 ++ClauseKindCnt;
6382 if (ImplicitMap.empty())
6383 continue;
6384 CXXScopeSpec MapperIdScopeSpec;
6385 DeclarationNameInfo MapperId;
6386 auto K = static_cast<OpenMPMapClauseKind>(ClauseKindCnt);
6387 if (OMPClause *Implicit = ActOnOpenMPMapClause(
6388 IteratorModifier: nullptr, MapTypeModifiers: ImpInfo.MapModifiers[I], MapTypeModifiersLoc: ImplicitMapModifiersLoc[I],
6389 MapperIdScopeSpec, MapperId, MapType: K, /*IsMapTypeImplicit=*/true,
6390 MapLoc: SourceLocation(), ColonLoc: SourceLocation(), VarList: ImplicitMap,
6391 Locs: OMPVarListLocTy())) {
6392 ClausesWithImplicit.emplace_back(Args&: Implicit);
6393 ErrorFound |= cast<OMPMapClause>(Val: Implicit)->varlist_size() !=
6394 ImplicitMap.size();
6395 } else {
6396 ErrorFound = true;
6397 }
6398 }
6399 }
6400 // Build expressions for implicit maps of data members with 'default'
6401 // mappers.
6402 if (getLangOpts().OpenMP >= 50)
6403 processImplicitMapsWithDefaultMappers(S&: SemaRef, DSAStack,
6404 Clauses&: ClausesWithImplicit);
6405 }
6406
6407 switch (Kind) {
6408 case OMPD_parallel:
6409 Res = ActOnOpenMPParallelDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6410 EndLoc);
6411 break;
6412 case OMPD_simd:
6413 Res = ActOnOpenMPSimdDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc,
6414 VarsWithImplicitDSA&: VarsWithInheritedDSA);
6415 break;
6416 case OMPD_tile:
6417 Res =
6418 ActOnOpenMPTileDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc);
6419 break;
6420 case OMPD_stripe:
6421 Res = ActOnOpenMPStripeDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6422 EndLoc);
6423 break;
6424 case OMPD_unroll:
6425 Res = ActOnOpenMPUnrollDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6426 EndLoc);
6427 break;
6428 case OMPD_reverse:
6429 assert(ClausesWithImplicit.empty() &&
6430 "reverse directive does not support any clauses");
6431 Res = ActOnOpenMPReverseDirective(AStmt, StartLoc, EndLoc);
6432 break;
6433 case OMPD_interchange:
6434 Res = ActOnOpenMPInterchangeDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6435 EndLoc);
6436 break;
6437 case OMPD_fuse:
6438 Res =
6439 ActOnOpenMPFuseDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc);
6440 break;
6441 case OMPD_for:
6442 Res = ActOnOpenMPForDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc,
6443 VarsWithImplicitDSA&: VarsWithInheritedDSA);
6444 break;
6445 case OMPD_for_simd:
6446 Res = ActOnOpenMPForSimdDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6447 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6448 break;
6449 case OMPD_sections:
6450 Res = ActOnOpenMPSectionsDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6451 EndLoc);
6452 break;
6453 case OMPD_section:
6454 assert(ClausesWithImplicit.empty() &&
6455 "No clauses are allowed for 'omp section' directive");
6456 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
6457 break;
6458 case OMPD_single:
6459 Res = ActOnOpenMPSingleDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6460 EndLoc);
6461 break;
6462 case OMPD_master:
6463 assert(ClausesWithImplicit.empty() &&
6464 "No clauses are allowed for 'omp master' directive");
6465 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
6466 break;
6467 case OMPD_masked:
6468 Res = ActOnOpenMPMaskedDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6469 EndLoc);
6470 break;
6471 case OMPD_critical:
6472 Res = ActOnOpenMPCriticalDirective(DirName, Clauses: ClausesWithImplicit, AStmt,
6473 StartLoc, EndLoc);
6474 break;
6475 case OMPD_parallel_for:
6476 Res = ActOnOpenMPParallelForDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6477 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6478 break;
6479 case OMPD_parallel_for_simd:
6480 Res = ActOnOpenMPParallelForSimdDirective(
6481 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6482 break;
6483 case OMPD_scope:
6484 Res =
6485 ActOnOpenMPScopeDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc);
6486 break;
6487 case OMPD_parallel_master:
6488 Res = ActOnOpenMPParallelMasterDirective(Clauses: ClausesWithImplicit, AStmt,
6489 StartLoc, EndLoc);
6490 break;
6491 case OMPD_parallel_masked:
6492 Res = ActOnOpenMPParallelMaskedDirective(Clauses: ClausesWithImplicit, AStmt,
6493 StartLoc, EndLoc);
6494 break;
6495 case OMPD_parallel_sections:
6496 Res = ActOnOpenMPParallelSectionsDirective(Clauses: ClausesWithImplicit, AStmt,
6497 StartLoc, EndLoc);
6498 break;
6499 case OMPD_task:
6500 Res =
6501 ActOnOpenMPTaskDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc);
6502 break;
6503 case OMPD_taskyield:
6504 assert(ClausesWithImplicit.empty() &&
6505 "No clauses are allowed for 'omp taskyield' directive");
6506 assert(AStmt == nullptr &&
6507 "No associated statement allowed for 'omp taskyield' directive");
6508 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
6509 break;
6510 case OMPD_error:
6511 assert(AStmt == nullptr &&
6512 "No associated statement allowed for 'omp error' directive");
6513 Res = ActOnOpenMPErrorDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc);
6514 break;
6515 case OMPD_barrier:
6516 assert(ClausesWithImplicit.empty() &&
6517 "No clauses are allowed for 'omp barrier' directive");
6518 assert(AStmt == nullptr &&
6519 "No associated statement allowed for 'omp barrier' directive");
6520 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
6521 break;
6522 case OMPD_taskwait:
6523 assert(AStmt == nullptr &&
6524 "No associated statement allowed for 'omp taskwait' directive");
6525 Res = ActOnOpenMPTaskwaitDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc);
6526 break;
6527 case OMPD_taskgroup:
6528 Res = ActOnOpenMPTaskgroupDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6529 EndLoc);
6530 break;
6531 case OMPD_flush:
6532 assert(AStmt == nullptr &&
6533 "No associated statement allowed for 'omp flush' directive");
6534 Res = ActOnOpenMPFlushDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc);
6535 break;
6536 case OMPD_depobj:
6537 assert(AStmt == nullptr &&
6538 "No associated statement allowed for 'omp depobj' directive");
6539 Res = ActOnOpenMPDepobjDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc);
6540 break;
6541 case OMPD_scan:
6542 assert(AStmt == nullptr &&
6543 "No associated statement allowed for 'omp scan' directive");
6544 Res = ActOnOpenMPScanDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc);
6545 break;
6546 case OMPD_ordered:
6547 Res = ActOnOpenMPOrderedDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6548 EndLoc);
6549 break;
6550 case OMPD_atomic:
6551 Res = ActOnOpenMPAtomicDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6552 EndLoc);
6553 break;
6554 case OMPD_teams:
6555 Res =
6556 ActOnOpenMPTeamsDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc);
6557 break;
6558 case OMPD_target:
6559 Res = ActOnOpenMPTargetDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6560 EndLoc);
6561 break;
6562 case OMPD_target_parallel:
6563 Res = ActOnOpenMPTargetParallelDirective(Clauses: ClausesWithImplicit, AStmt,
6564 StartLoc, EndLoc);
6565 break;
6566 case OMPD_target_parallel_for:
6567 Res = ActOnOpenMPTargetParallelForDirective(
6568 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6569 break;
6570 case OMPD_cancellation_point:
6571 assert(ClausesWithImplicit.empty() &&
6572 "No clauses are allowed for 'omp cancellation point' directive");
6573 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
6574 "cancellation point' directive");
6575 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
6576 break;
6577 case OMPD_cancel:
6578 assert(AStmt == nullptr &&
6579 "No associated statement allowed for 'omp cancel' directive");
6580 Res = ActOnOpenMPCancelDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc,
6581 CancelRegion);
6582 break;
6583 case OMPD_target_data:
6584 Res = ActOnOpenMPTargetDataDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6585 EndLoc);
6586 break;
6587 case OMPD_target_enter_data:
6588 Res = ActOnOpenMPTargetEnterDataDirective(Clauses: ClausesWithImplicit, StartLoc,
6589 EndLoc, AStmt);
6590 break;
6591 case OMPD_target_exit_data:
6592 Res = ActOnOpenMPTargetExitDataDirective(Clauses: ClausesWithImplicit, StartLoc,
6593 EndLoc, AStmt);
6594 break;
6595 case OMPD_taskloop:
6596 Res = ActOnOpenMPTaskLoopDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6597 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6598 break;
6599 case OMPD_taskloop_simd:
6600 Res = ActOnOpenMPTaskLoopSimdDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6601 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6602 break;
6603 case OMPD_master_taskloop:
6604 Res = ActOnOpenMPMasterTaskLoopDirective(
6605 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6606 break;
6607 case OMPD_masked_taskloop:
6608 Res = ActOnOpenMPMaskedTaskLoopDirective(
6609 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6610 break;
6611 case OMPD_master_taskloop_simd:
6612 Res = ActOnOpenMPMasterTaskLoopSimdDirective(
6613 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6614 break;
6615 case OMPD_masked_taskloop_simd:
6616 Res = ActOnOpenMPMaskedTaskLoopSimdDirective(
6617 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6618 break;
6619 case OMPD_parallel_master_taskloop:
6620 Res = ActOnOpenMPParallelMasterTaskLoopDirective(
6621 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6622 break;
6623 case OMPD_parallel_masked_taskloop:
6624 Res = ActOnOpenMPParallelMaskedTaskLoopDirective(
6625 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6626 break;
6627 case OMPD_parallel_master_taskloop_simd:
6628 Res = ActOnOpenMPParallelMasterTaskLoopSimdDirective(
6629 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6630 break;
6631 case OMPD_parallel_masked_taskloop_simd:
6632 Res = ActOnOpenMPParallelMaskedTaskLoopSimdDirective(
6633 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6634 break;
6635 case OMPD_distribute:
6636 Res = ActOnOpenMPDistributeDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6637 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6638 break;
6639 case OMPD_target_update:
6640 Res = ActOnOpenMPTargetUpdateDirective(Clauses: ClausesWithImplicit, StartLoc,
6641 EndLoc, AStmt);
6642 break;
6643 case OMPD_distribute_parallel_for:
6644 Res = ActOnOpenMPDistributeParallelForDirective(
6645 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6646 break;
6647 case OMPD_distribute_parallel_for_simd:
6648 Res = ActOnOpenMPDistributeParallelForSimdDirective(
6649 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6650 break;
6651 case OMPD_distribute_simd:
6652 Res = ActOnOpenMPDistributeSimdDirective(
6653 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6654 break;
6655 case OMPD_target_parallel_for_simd:
6656 Res = ActOnOpenMPTargetParallelForSimdDirective(
6657 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6658 break;
6659 case OMPD_target_simd:
6660 Res = ActOnOpenMPTargetSimdDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6661 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6662 break;
6663 case OMPD_teams_distribute:
6664 Res = ActOnOpenMPTeamsDistributeDirective(
6665 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6666 break;
6667 case OMPD_teams_distribute_simd:
6668 Res = ActOnOpenMPTeamsDistributeSimdDirective(
6669 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6670 break;
6671 case OMPD_teams_distribute_parallel_for_simd:
6672 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
6673 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6674 break;
6675 case OMPD_teams_distribute_parallel_for:
6676 Res = ActOnOpenMPTeamsDistributeParallelForDirective(
6677 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6678 break;
6679 case OMPD_target_teams:
6680 Res = ActOnOpenMPTargetTeamsDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6681 EndLoc);
6682 break;
6683 case OMPD_target_teams_distribute:
6684 Res = ActOnOpenMPTargetTeamsDistributeDirective(
6685 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6686 break;
6687 case OMPD_target_teams_distribute_parallel_for:
6688 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
6689 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6690 break;
6691 case OMPD_target_teams_distribute_parallel_for_simd:
6692 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
6693 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6694 break;
6695 case OMPD_target_teams_distribute_simd:
6696 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
6697 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6698 break;
6699 case OMPD_interop:
6700 assert(AStmt == nullptr &&
6701 "No associated statement allowed for 'omp interop' directive");
6702 Res = ActOnOpenMPInteropDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc);
6703 break;
6704 case OMPD_dispatch:
6705 Res = ActOnOpenMPDispatchDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6706 EndLoc);
6707 break;
6708 case OMPD_loop:
6709 Res = ActOnOpenMPGenericLoopDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6710 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6711 break;
6712 case OMPD_teams_loop:
6713 Res = ActOnOpenMPTeamsGenericLoopDirective(
6714 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6715 break;
6716 case OMPD_target_teams_loop:
6717 Res = ActOnOpenMPTargetTeamsGenericLoopDirective(
6718 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6719 break;
6720 case OMPD_parallel_loop:
6721 Res = ActOnOpenMPParallelGenericLoopDirective(
6722 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6723 break;
6724 case OMPD_target_parallel_loop:
6725 Res = ActOnOpenMPTargetParallelGenericLoopDirective(
6726 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6727 break;
6728 case OMPD_declare_target:
6729 case OMPD_end_declare_target:
6730 case OMPD_threadprivate:
6731 case OMPD_allocate:
6732 case OMPD_declare_reduction:
6733 case OMPD_declare_mapper:
6734 case OMPD_declare_simd:
6735 case OMPD_requires:
6736 case OMPD_declare_variant:
6737 case OMPD_begin_declare_variant:
6738 case OMPD_end_declare_variant:
6739 llvm_unreachable("OpenMP Directive is not allowed");
6740 case OMPD_unknown:
6741 default:
6742 llvm_unreachable("Unknown OpenMP directive");
6743 }
6744
6745 ErrorFound = Res.isInvalid() || ErrorFound;
6746
6747 // Check variables in the clauses if default(none) or
6748 // default(firstprivate) was specified.
6749 if (DSAStack->getDefaultDSA() == DSA_none ||
6750 DSAStack->getDefaultDSA() == DSA_private ||
6751 DSAStack->getDefaultDSA() == DSA_firstprivate) {
6752 DSAAttrChecker DSAChecker(DSAStack, SemaRef, nullptr);
6753 for (OMPClause *C : Clauses) {
6754 switch (C->getClauseKind()) {
6755 case OMPC_num_threads:
6756 case OMPC_dist_schedule:
6757 // Do not analyze if no parent teams directive.
6758 if (isOpenMPTeamsDirective(DKind: Kind))
6759 break;
6760 continue;
6761 case OMPC_if:
6762 if (isOpenMPTeamsDirective(DKind: Kind) &&
6763 cast<OMPIfClause>(Val: C)->getNameModifier() != OMPD_target)
6764 break;
6765 if (isOpenMPParallelDirective(DKind: Kind) &&
6766 isOpenMPTaskLoopDirective(DKind: Kind) &&
6767 cast<OMPIfClause>(Val: C)->getNameModifier() != OMPD_parallel)
6768 break;
6769 continue;
6770 case OMPC_schedule:
6771 case OMPC_detach:
6772 break;
6773 case OMPC_grainsize:
6774 case OMPC_num_tasks:
6775 case OMPC_final:
6776 case OMPC_priority:
6777 case OMPC_novariants:
6778 case OMPC_nocontext:
6779 // Do not analyze if no parent parallel directive.
6780 if (isOpenMPParallelDirective(DKind: Kind))
6781 break;
6782 continue;
6783 case OMPC_ordered:
6784 case OMPC_device:
6785 case OMPC_num_teams:
6786 case OMPC_thread_limit:
6787 case OMPC_hint:
6788 case OMPC_collapse:
6789 case OMPC_safelen:
6790 case OMPC_simdlen:
6791 case OMPC_sizes:
6792 case OMPC_default:
6793 case OMPC_proc_bind:
6794 case OMPC_private:
6795 case OMPC_firstprivate:
6796 case OMPC_lastprivate:
6797 case OMPC_shared:
6798 case OMPC_reduction:
6799 case OMPC_task_reduction:
6800 case OMPC_in_reduction:
6801 case OMPC_linear:
6802 case OMPC_aligned:
6803 case OMPC_copyin:
6804 case OMPC_copyprivate:
6805 case OMPC_nowait:
6806 case OMPC_untied:
6807 case OMPC_mergeable:
6808 case OMPC_allocate:
6809 case OMPC_read:
6810 case OMPC_write:
6811 case OMPC_update:
6812 case OMPC_capture:
6813 case OMPC_compare:
6814 case OMPC_seq_cst:
6815 case OMPC_acq_rel:
6816 case OMPC_acquire:
6817 case OMPC_release:
6818 case OMPC_relaxed:
6819 case OMPC_depend:
6820 case OMPC_threads:
6821 case OMPC_simd:
6822 case OMPC_map:
6823 case OMPC_nogroup:
6824 case OMPC_defaultmap:
6825 case OMPC_to:
6826 case OMPC_from:
6827 case OMPC_use_device_ptr:
6828 case OMPC_use_device_addr:
6829 case OMPC_is_device_ptr:
6830 case OMPC_has_device_addr:
6831 case OMPC_nontemporal:
6832 case OMPC_order:
6833 case OMPC_destroy:
6834 case OMPC_inclusive:
6835 case OMPC_exclusive:
6836 case OMPC_uses_allocators:
6837 case OMPC_affinity:
6838 case OMPC_bind:
6839 case OMPC_filter:
6840 case OMPC_severity:
6841 case OMPC_message:
6842 continue;
6843 case OMPC_allocator:
6844 case OMPC_flush:
6845 case OMPC_depobj:
6846 case OMPC_threadprivate:
6847 case OMPC_groupprivate:
6848 case OMPC_uniform:
6849 case OMPC_unknown:
6850 case OMPC_unified_address:
6851 case OMPC_unified_shared_memory:
6852 case OMPC_reverse_offload:
6853 case OMPC_dynamic_allocators:
6854 case OMPC_atomic_default_mem_order:
6855 case OMPC_self_maps:
6856 case OMPC_device_type:
6857 case OMPC_match:
6858 case OMPC_when:
6859 case OMPC_at:
6860 default:
6861 llvm_unreachable("Unexpected clause");
6862 }
6863 for (Stmt *CC : C->children()) {
6864 if (CC)
6865 DSAChecker.Visit(S: CC);
6866 }
6867 }
6868 for (const auto &P : DSAChecker.getVarsWithInheritedDSA())
6869 VarsWithInheritedDSA[P.getFirst()] = P.getSecond();
6870 }
6871 for (const auto &P : VarsWithInheritedDSA) {
6872 if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(Val: P.getFirst()))
6873 continue;
6874 ErrorFound = true;
6875 if (DSAStack->getDefaultDSA() == DSA_none ||
6876 DSAStack->getDefaultDSA() == DSA_private ||
6877 DSAStack->getDefaultDSA() == DSA_firstprivate) {
6878 Diag(Loc: P.second->getExprLoc(), DiagID: diag::err_omp_no_dsa_for_variable)
6879 << P.first << P.second->getSourceRange();
6880 Diag(DSAStack->getDefaultDSALocation(), DiagID: diag::note_omp_default_dsa_none);
6881 } else if (getLangOpts().OpenMP >= 50) {
6882 Diag(Loc: P.second->getExprLoc(),
6883 DiagID: diag::err_omp_defaultmap_no_attr_for_variable)
6884 << P.first << P.second->getSourceRange();
6885 Diag(DSAStack->getDefaultDSALocation(),
6886 DiagID: diag::note_omp_defaultmap_attr_none);
6887 }
6888 }
6889
6890 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
6891 for (OpenMPDirectiveKind D : getLeafConstructsOrSelf(D: Kind)) {
6892 if (isAllowedClauseForDirective(D, C: OMPC_if, Version: getLangOpts().OpenMP))
6893 AllowedNameModifiers.push_back(Elt: D);
6894 }
6895 if (!AllowedNameModifiers.empty())
6896 ErrorFound = checkIfClauses(S&: SemaRef, Kind, Clauses, AllowedNameModifiers) ||
6897 ErrorFound;
6898
6899 if (ErrorFound)
6900 return StmtError();
6901
6902 if (!SemaRef.CurContext->isDependentContext() &&
6903 isOpenMPTargetExecutionDirective(DKind: Kind) &&
6904 !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
6905 DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() ||
6906 DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() ||
6907 DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) {
6908 // Register target to DSA Stack.
6909 DSAStack->addTargetDirLocation(LocStart: StartLoc);
6910 }
6911
6912 return Res;
6913}
6914
6915SemaOpenMP::DeclGroupPtrTy SemaOpenMP::ActOnOpenMPDeclareSimdDirective(
6916 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
6917 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
6918 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
6919 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
6920 assert(Aligneds.size() == Alignments.size());
6921 assert(Linears.size() == LinModifiers.size());
6922 assert(Linears.size() == Steps.size());
6923 if (!DG || DG.get().isNull())
6924 return DeclGroupPtrTy();
6925
6926 const int SimdId = 0;
6927 if (!DG.get().isSingleDecl()) {
6928 Diag(Loc: SR.getBegin(), DiagID: diag::err_omp_single_decl_in_declare_simd_variant)
6929 << SimdId;
6930 return DG;
6931 }
6932 Decl *ADecl = DG.get().getSingleDecl();
6933 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(Val: ADecl))
6934 ADecl = FTD->getTemplatedDecl();
6935
6936 auto *FD = dyn_cast<FunctionDecl>(Val: ADecl);
6937 if (!FD) {
6938 Diag(Loc: ADecl->getLocation(), DiagID: diag::err_omp_function_expected) << SimdId;
6939 return DeclGroupPtrTy();
6940 }
6941
6942 // OpenMP [2.8.2, declare simd construct, Description]
6943 // The parameter of the simdlen clause must be a constant positive integer
6944 // expression.
6945 ExprResult SL;
6946 if (Simdlen)
6947 SL = VerifyPositiveIntegerConstantInClause(Op: Simdlen, CKind: OMPC_simdlen);
6948 // OpenMP [2.8.2, declare simd construct, Description]
6949 // The special this pointer can be used as if was one of the arguments to the
6950 // function in any of the linear, aligned, or uniform clauses.
6951 // The uniform clause declares one or more arguments to have an invariant
6952 // value for all concurrent invocations of the function in the execution of a
6953 // single SIMD loop.
6954 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
6955 const Expr *UniformedLinearThis = nullptr;
6956 for (const Expr *E : Uniforms) {
6957 E = E->IgnoreParenImpCasts();
6958 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: E))
6959 if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: DRE->getDecl()))
6960 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
6961 FD->getParamDecl(i: PVD->getFunctionScopeIndex())
6962 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
6963 UniformedArgs.try_emplace(Key: PVD->getCanonicalDecl(), Args&: E);
6964 continue;
6965 }
6966 if (isa<CXXThisExpr>(Val: E)) {
6967 UniformedLinearThis = E;
6968 continue;
6969 }
6970 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_param_or_this_in_clause)
6971 << FD->getDeclName() << (isa<CXXMethodDecl>(Val: ADecl) ? 1 : 0);
6972 }
6973 // OpenMP [2.8.2, declare simd construct, Description]
6974 // The aligned clause declares that the object to which each list item points
6975 // is aligned to the number of bytes expressed in the optional parameter of
6976 // the aligned clause.
6977 // The special this pointer can be used as if was one of the arguments to the
6978 // function in any of the linear, aligned, or uniform clauses.
6979 // The type of list items appearing in the aligned clause must be array,
6980 // pointer, reference to array, or reference to pointer.
6981 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
6982 const Expr *AlignedThis = nullptr;
6983 for (const Expr *E : Aligneds) {
6984 E = E->IgnoreParenImpCasts();
6985 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: E))
6986 if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: DRE->getDecl())) {
6987 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
6988 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
6989 FD->getParamDecl(i: PVD->getFunctionScopeIndex())
6990 ->getCanonicalDecl() == CanonPVD) {
6991 // OpenMP [2.8.1, simd construct, Restrictions]
6992 // A list-item cannot appear in more than one aligned clause.
6993 auto [It, Inserted] = AlignedArgs.try_emplace(Key: CanonPVD, Args&: E);
6994 if (!Inserted) {
6995 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_used_in_clause_twice)
6996 << 1 << getOpenMPClauseNameForDiag(C: OMPC_aligned)
6997 << E->getSourceRange();
6998 Diag(Loc: It->second->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
6999 << getOpenMPClauseNameForDiag(C: OMPC_aligned);
7000 continue;
7001 }
7002 QualType QTy = PVD->getType()
7003 .getNonReferenceType()
7004 .getUnqualifiedType()
7005 .getCanonicalType();
7006 const Type *Ty = QTy.getTypePtrOrNull();
7007 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
7008 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_aligned_expected_array_or_ptr)
7009 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
7010 Diag(Loc: PVD->getLocation(), DiagID: diag::note_previous_decl) << PVD;
7011 }
7012 continue;
7013 }
7014 }
7015 if (isa<CXXThisExpr>(Val: E)) {
7016 if (AlignedThis) {
7017 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_used_in_clause_twice)
7018 << 2 << getOpenMPClauseNameForDiag(C: OMPC_aligned)
7019 << E->getSourceRange();
7020 Diag(Loc: AlignedThis->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
7021 << getOpenMPClauseNameForDiag(C: OMPC_aligned);
7022 }
7023 AlignedThis = E;
7024 continue;
7025 }
7026 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_param_or_this_in_clause)
7027 << FD->getDeclName() << (isa<CXXMethodDecl>(Val: ADecl) ? 1 : 0);
7028 }
7029 // The optional parameter of the aligned clause, alignment, must be a constant
7030 // positive integer expression. If no optional parameter is specified,
7031 // implementation-defined default alignments for SIMD instructions on the
7032 // target platforms are assumed.
7033 SmallVector<const Expr *, 4> NewAligns;
7034 for (Expr *E : Alignments) {
7035 ExprResult Align;
7036 if (E)
7037 Align = VerifyPositiveIntegerConstantInClause(Op: E, CKind: OMPC_aligned);
7038 NewAligns.push_back(Elt: Align.get());
7039 }
7040 // OpenMP [2.8.2, declare simd construct, Description]
7041 // The linear clause declares one or more list items to be private to a SIMD
7042 // lane and to have a linear relationship with respect to the iteration space
7043 // of a loop.
7044 // The special this pointer can be used as if was one of the arguments to the
7045 // function in any of the linear, aligned, or uniform clauses.
7046 // When a linear-step expression is specified in a linear clause it must be
7047 // either a constant integer expression or an integer-typed parameter that is
7048 // specified in a uniform clause on the directive.
7049 llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
7050 const bool IsUniformedThis = UniformedLinearThis != nullptr;
7051 auto MI = LinModifiers.begin();
7052 for (const Expr *E : Linears) {
7053 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
7054 ++MI;
7055 E = E->IgnoreParenImpCasts();
7056 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: E))
7057 if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: DRE->getDecl())) {
7058 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
7059 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
7060 FD->getParamDecl(i: PVD->getFunctionScopeIndex())
7061 ->getCanonicalDecl() == CanonPVD) {
7062 // OpenMP [2.15.3.7, linear Clause, Restrictions]
7063 // A list-item cannot appear in more than one linear clause.
7064 if (auto It = LinearArgs.find(Val: CanonPVD); It != LinearArgs.end()) {
7065 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_wrong_dsa)
7066 << getOpenMPClauseNameForDiag(C: OMPC_linear)
7067 << getOpenMPClauseNameForDiag(C: OMPC_linear)
7068 << E->getSourceRange();
7069 Diag(Loc: It->second->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
7070 << getOpenMPClauseNameForDiag(C: OMPC_linear);
7071 continue;
7072 }
7073 // Each argument can appear in at most one uniform or linear clause.
7074 if (auto It = UniformedArgs.find(Val: CanonPVD);
7075 It != UniformedArgs.end()) {
7076 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_wrong_dsa)
7077 << getOpenMPClauseNameForDiag(C: OMPC_linear)
7078 << getOpenMPClauseNameForDiag(C: OMPC_uniform)
7079 << E->getSourceRange();
7080 Diag(Loc: It->second->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
7081 << getOpenMPClauseNameForDiag(C: OMPC_uniform);
7082 continue;
7083 }
7084 LinearArgs[CanonPVD] = E;
7085 if (E->isValueDependent() || E->isTypeDependent() ||
7086 E->isInstantiationDependent() ||
7087 E->containsUnexpandedParameterPack())
7088 continue;
7089 (void)CheckOpenMPLinearDecl(D: CanonPVD, ELoc: E->getExprLoc(), LinKind,
7090 Type: PVD->getOriginalType(),
7091 /*IsDeclareSimd=*/true);
7092 continue;
7093 }
7094 }
7095 if (isa<CXXThisExpr>(Val: E)) {
7096 if (UniformedLinearThis) {
7097 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_wrong_dsa)
7098 << getOpenMPClauseNameForDiag(C: OMPC_linear)
7099 << getOpenMPClauseNameForDiag(C: IsUniformedThis ? OMPC_uniform
7100 : OMPC_linear)
7101 << E->getSourceRange();
7102 Diag(Loc: UniformedLinearThis->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
7103 << getOpenMPClauseNameForDiag(C: IsUniformedThis ? OMPC_uniform
7104 : OMPC_linear);
7105 continue;
7106 }
7107 UniformedLinearThis = E;
7108 if (E->isValueDependent() || E->isTypeDependent() ||
7109 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
7110 continue;
7111 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, ELoc: E->getExprLoc(), LinKind,
7112 Type: E->getType(), /*IsDeclareSimd=*/true);
7113 continue;
7114 }
7115 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_param_or_this_in_clause)
7116 << FD->getDeclName() << (isa<CXXMethodDecl>(Val: ADecl) ? 1 : 0);
7117 }
7118 Expr *Step = nullptr;
7119 Expr *NewStep = nullptr;
7120 SmallVector<Expr *, 4> NewSteps;
7121 for (Expr *E : Steps) {
7122 // Skip the same step expression, it was checked already.
7123 if (Step == E || !E) {
7124 NewSteps.push_back(Elt: E ? NewStep : nullptr);
7125 continue;
7126 }
7127 Step = E;
7128 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: Step))
7129 if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: DRE->getDecl())) {
7130 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
7131 if (UniformedArgs.count(Val: CanonPVD) == 0) {
7132 Diag(Loc: Step->getExprLoc(), DiagID: diag::err_omp_expected_uniform_param)
7133 << Step->getSourceRange();
7134 } else if (E->isValueDependent() || E->isTypeDependent() ||
7135 E->isInstantiationDependent() ||
7136 E->containsUnexpandedParameterPack() ||
7137 CanonPVD->getType()->hasIntegerRepresentation()) {
7138 NewSteps.push_back(Elt: Step);
7139 } else {
7140 Diag(Loc: Step->getExprLoc(), DiagID: diag::err_omp_expected_int_param)
7141 << Step->getSourceRange();
7142 }
7143 continue;
7144 }
7145 NewStep = Step;
7146 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
7147 !Step->isInstantiationDependent() &&
7148 !Step->containsUnexpandedParameterPack()) {
7149 NewStep = PerformOpenMPImplicitIntegerConversion(OpLoc: Step->getExprLoc(), Op: Step)
7150 .get();
7151 if (NewStep)
7152 NewStep = SemaRef
7153 .VerifyIntegerConstantExpression(
7154 E: NewStep, /*FIXME*/ CanFold: AllowFoldKind::Allow)
7155 .get();
7156 }
7157 NewSteps.push_back(Elt: NewStep);
7158 }
7159 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
7160 Ctx&: getASTContext(), BranchState: BS, Simdlen: SL.get(), Uniforms: const_cast<Expr **>(Uniforms.data()),
7161 UniformsSize: Uniforms.size(), Aligneds: const_cast<Expr **>(Aligneds.data()), AlignedsSize: Aligneds.size(),
7162 Alignments: const_cast<Expr **>(NewAligns.data()), AlignmentsSize: NewAligns.size(),
7163 Linears: const_cast<Expr **>(Linears.data()), LinearsSize: Linears.size(),
7164 Modifiers: const_cast<unsigned *>(LinModifiers.data()), ModifiersSize: LinModifiers.size(),
7165 Steps: NewSteps.data(), StepsSize: NewSteps.size(), Range: SR);
7166 ADecl->addAttr(A: NewAttr);
7167 return DG;
7168}
7169
7170StmtResult SemaOpenMP::ActOnOpenMPInformationalDirective(
7171 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
7172 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7173 SourceLocation EndLoc) {
7174 assert(isOpenMPInformationalDirective(Kind) &&
7175 "Unexpected directive category");
7176
7177 StmtResult Res = StmtError();
7178
7179 switch (Kind) {
7180 case OMPD_assume:
7181 Res = ActOnOpenMPAssumeDirective(Clauses, AStmt, StartLoc, EndLoc);
7182 break;
7183 default:
7184 llvm_unreachable("Unknown OpenMP directive");
7185 }
7186
7187 return Res;
7188}
7189
7190static void setPrototype(Sema &S, FunctionDecl *FD, FunctionDecl *FDWithProto,
7191 QualType NewType) {
7192 assert(NewType->isFunctionProtoType() &&
7193 "Expected function type with prototype.");
7194 assert(FD->getType()->isFunctionNoProtoType() &&
7195 "Expected function with type with no prototype.");
7196 assert(FDWithProto->getType()->isFunctionProtoType() &&
7197 "Expected function with prototype.");
7198 // Synthesize parameters with the same types.
7199 FD->setType(NewType);
7200 SmallVector<ParmVarDecl *, 16> Params;
7201 for (const ParmVarDecl *P : FDWithProto->parameters()) {
7202 auto *Param = ParmVarDecl::Create(C&: S.getASTContext(), DC: FD, StartLoc: SourceLocation(),
7203 IdLoc: SourceLocation(), Id: nullptr, T: P->getType(),
7204 /*TInfo=*/nullptr, S: SC_None, DefArg: nullptr);
7205 Param->setScopeInfo(scopeDepth: 0, parameterIndex: Params.size());
7206 Param->setImplicit();
7207 Params.push_back(Elt: Param);
7208 }
7209
7210 FD->setParams(Params);
7211}
7212
7213void SemaOpenMP::ActOnFinishedFunctionDefinitionInOpenMPAssumeScope(Decl *D) {
7214 if (D->isInvalidDecl())
7215 return;
7216 FunctionDecl *FD = nullptr;
7217 if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(Val: D))
7218 FD = UTemplDecl->getTemplatedDecl();
7219 else
7220 FD = cast<FunctionDecl>(Val: D);
7221 assert(FD && "Expected a function declaration!");
7222
7223 // If we are instantiating templates we do *not* apply scoped assumptions but
7224 // only global ones. We apply scoped assumption to the template definition
7225 // though.
7226 if (!SemaRef.inTemplateInstantiation()) {
7227 for (OMPAssumeAttr *AA : OMPAssumeScoped)
7228 FD->addAttr(A: AA);
7229 }
7230 for (OMPAssumeAttr *AA : OMPAssumeGlobal)
7231 FD->addAttr(A: AA);
7232}
7233
7234SemaOpenMP::OMPDeclareVariantScope::OMPDeclareVariantScope(OMPTraitInfo &TI)
7235 : TI(&TI), NameSuffix(TI.getMangledName()) {}
7236
7237void SemaOpenMP::ActOnStartOfFunctionDefinitionInOpenMPDeclareVariantScope(
7238 Scope *S, Declarator &D, MultiTemplateParamsArg TemplateParamLists,
7239 SmallVectorImpl<FunctionDecl *> &Bases) {
7240 if (!D.getIdentifier())
7241 return;
7242
7243 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back();
7244
7245 // Template specialization is an extension, check if we do it.
7246 bool IsTemplated = !TemplateParamLists.empty();
7247 if (IsTemplated &&
7248 !DVScope.TI->isExtensionActive(
7249 TP: llvm::omp::TraitProperty::implementation_extension_allow_templates))
7250 return;
7251
7252 const IdentifierInfo *BaseII = D.getIdentifier();
7253 LookupResult Lookup(SemaRef, DeclarationName(BaseII), D.getIdentifierLoc(),
7254 Sema::LookupOrdinaryName);
7255 SemaRef.LookupParsedName(R&: Lookup, S, SS: &D.getCXXScopeSpec(),
7256 /*ObjectType=*/QualType());
7257
7258 TypeSourceInfo *TInfo = SemaRef.GetTypeForDeclarator(D);
7259 QualType FType = TInfo->getType();
7260
7261 bool IsConstexpr =
7262 D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Constexpr;
7263 bool IsConsteval =
7264 D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Consteval;
7265
7266 for (auto *Candidate : Lookup) {
7267 auto *CandidateDecl = Candidate->getUnderlyingDecl();
7268 FunctionDecl *UDecl = nullptr;
7269 if (IsTemplated && isa<FunctionTemplateDecl>(Val: CandidateDecl)) {
7270 auto *FTD = cast<FunctionTemplateDecl>(Val: CandidateDecl);
7271 // FIXME: Should this compare the template parameter lists on all levels?
7272 if (SemaRef.Context.isSameTemplateParameterList(
7273 X: FTD->getTemplateParameters(), Y: TemplateParamLists.back()))
7274 UDecl = FTD->getTemplatedDecl();
7275 } else if (!IsTemplated)
7276 UDecl = dyn_cast<FunctionDecl>(Val: CandidateDecl);
7277 if (!UDecl)
7278 continue;
7279
7280 // Don't specialize constexpr/consteval functions with
7281 // non-constexpr/consteval functions.
7282 if (UDecl->isConstexpr() && !IsConstexpr)
7283 continue;
7284 if (UDecl->isConsteval() && !IsConsteval)
7285 continue;
7286
7287 QualType UDeclTy = UDecl->getType();
7288 if (!UDeclTy->isDependentType()) {
7289 QualType NewType = getASTContext().mergeFunctionTypes(
7290 FType, UDeclTy, /*OfBlockPointer=*/false,
7291 /*Unqualified=*/false, /*AllowCXX=*/true);
7292 if (NewType.isNull())
7293 continue;
7294 }
7295
7296 // Found a base!
7297 Bases.push_back(Elt: UDecl);
7298 }
7299
7300 bool UseImplicitBase = !DVScope.TI->isExtensionActive(
7301 TP: llvm::omp::TraitProperty::implementation_extension_disable_implicit_base);
7302 // If no base was found we create a declaration that we use as base.
7303 if (Bases.empty() && UseImplicitBase) {
7304 D.setFunctionDefinitionKind(FunctionDefinitionKind::Declaration);
7305 Decl *BaseD = SemaRef.HandleDeclarator(S, D, TemplateParameterLists: TemplateParamLists);
7306 BaseD->setImplicit(true);
7307 if (auto *BaseTemplD = dyn_cast<FunctionTemplateDecl>(Val: BaseD))
7308 Bases.push_back(Elt: BaseTemplD->getTemplatedDecl());
7309 else
7310 Bases.push_back(Elt: cast<FunctionDecl>(Val: BaseD));
7311 }
7312
7313 std::string MangledName;
7314 MangledName += D.getIdentifier()->getName();
7315 MangledName += getOpenMPVariantManglingSeparatorStr();
7316 MangledName += DVScope.NameSuffix;
7317 IdentifierInfo &VariantII = getASTContext().Idents.get(Name: MangledName);
7318
7319 VariantII.setMangledOpenMPVariantName(true);
7320 D.SetIdentifier(Id: &VariantII, IdLoc: D.getBeginLoc());
7321}
7322
7323void SemaOpenMP::ActOnFinishedFunctionDefinitionInOpenMPDeclareVariantScope(
7324 Decl *D, SmallVectorImpl<FunctionDecl *> &Bases) {
7325 // Do not mark function as is used to prevent its emission if this is the
7326 // only place where it is used.
7327 EnterExpressionEvaluationContext Unevaluated(
7328 SemaRef, Sema::ExpressionEvaluationContext::Unevaluated);
7329
7330 FunctionDecl *FD = nullptr;
7331 if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(Val: D))
7332 FD = UTemplDecl->getTemplatedDecl();
7333 else
7334 FD = cast<FunctionDecl>(Val: D);
7335 auto *VariantFuncRef = DeclRefExpr::Create(
7336 Context: getASTContext(), QualifierLoc: NestedNameSpecifierLoc(), TemplateKWLoc: SourceLocation(), D: FD,
7337 /*RefersToEnclosingVariableOrCapture=*/false,
7338 /*NameLoc=*/FD->getLocation(), T: FD->getType(), VK: ExprValueKind::VK_PRValue);
7339
7340 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back();
7341 auto *OMPDeclareVariantA = OMPDeclareVariantAttr::CreateImplicit(
7342 Ctx&: getASTContext(), VariantFuncRef, TraitInfos: DVScope.TI,
7343 /*NothingArgs=*/AdjustArgsNothing: nullptr, /*NothingArgsSize=*/AdjustArgsNothingSize: 0,
7344 /*NeedDevicePtrArgs=*/AdjustArgsNeedDevicePtr: nullptr, /*NeedDevicePtrArgsSize=*/AdjustArgsNeedDevicePtrSize: 0,
7345 /*NeedDeviceAddrArgs=*/AdjustArgsNeedDeviceAddr: nullptr, /*NeedDeviceAddrArgsSize=*/AdjustArgsNeedDeviceAddrSize: 0,
7346 /*AppendArgs=*/nullptr, /*AppendArgsSize=*/0);
7347 for (FunctionDecl *BaseFD : Bases)
7348 BaseFD->addAttr(A: OMPDeclareVariantA);
7349}
7350
7351ExprResult SemaOpenMP::ActOnOpenMPCall(ExprResult Call, Scope *Scope,
7352 SourceLocation LParenLoc,
7353 MultiExprArg ArgExprs,
7354 SourceLocation RParenLoc,
7355 Expr *ExecConfig) {
7356 // The common case is a regular call we do not want to specialize at all. Try
7357 // to make that case fast by bailing early.
7358 CallExpr *CE = dyn_cast<CallExpr>(Val: Call.get());
7359 if (!CE)
7360 return Call;
7361
7362 FunctionDecl *CalleeFnDecl = CE->getDirectCallee();
7363
7364 // Mark indirect calls inside target regions, to allow for insertion of
7365 // __llvm_omp_indirect_call_lookup calls during codegen.
7366 if (!CalleeFnDecl) {
7367 if (isInOpenMPTargetExecutionDirective()) {
7368 Expr *E = CE->getCallee()->IgnoreParenImpCasts();
7369 DeclRefExpr *DRE = nullptr;
7370 while (E) {
7371 if ((DRE = dyn_cast<DeclRefExpr>(Val: E)))
7372 break;
7373 if (auto *ME = dyn_cast<MemberExpr>(Val: E))
7374 E = ME->getBase()->IgnoreParenImpCasts();
7375 else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: E))
7376 E = ASE->getBase()->IgnoreParenImpCasts();
7377 else
7378 break;
7379 }
7380 VarDecl *VD = DRE ? dyn_cast<VarDecl>(Val: DRE->getDecl()) : nullptr;
7381 if (VD && !VD->hasAttr<OMPTargetIndirectCallAttr>()) {
7382 VD->addAttr(A: OMPTargetIndirectCallAttr::CreateImplicit(Ctx&: getASTContext()));
7383 if (ASTMutationListener *ML = getASTContext().getASTMutationListener())
7384 ML->DeclarationMarkedOpenMPIndirectCall(D: VD);
7385 }
7386 }
7387
7388 return Call;
7389 }
7390
7391 if (getLangOpts().OpenMP >= 50 && getLangOpts().OpenMP <= 60 &&
7392 CalleeFnDecl->getIdentifier() &&
7393 CalleeFnDecl->getName().starts_with_insensitive(Prefix: "omp_")) {
7394 // checking for any calls inside an Order region
7395 if (Scope && Scope->isOpenMPOrderClauseScope())
7396 Diag(Loc: LParenLoc, DiagID: diag::err_omp_unexpected_call_to_omp_runtime_api);
7397 }
7398
7399 if (!CalleeFnDecl->hasAttr<OMPDeclareVariantAttr>())
7400 return Call;
7401
7402 ASTContext &Context = getASTContext();
7403 std::function<void(StringRef)> DiagUnknownTrait = [this,
7404 CE](StringRef ISATrait) {
7405 // TODO Track the selector locations in a way that is accessible here to
7406 // improve the diagnostic location.
7407 Diag(Loc: CE->getBeginLoc(), DiagID: diag::warn_unknown_declare_variant_isa_trait)
7408 << ISATrait;
7409 };
7410 TargetOMPContext OMPCtx(Context, std::move(DiagUnknownTrait),
7411 SemaRef.getCurFunctionDecl(),
7412 DSAStack->getConstructTraits(), getOpenMPDeviceNum());
7413
7414 QualType CalleeFnType = CalleeFnDecl->getType();
7415
7416 SmallVector<Expr *, 4> Exprs;
7417 SmallVector<VariantMatchInfo, 4> VMIs;
7418 while (CalleeFnDecl) {
7419 for (OMPDeclareVariantAttr *A :
7420 CalleeFnDecl->specific_attrs<OMPDeclareVariantAttr>()) {
7421 Expr *VariantRef = A->getVariantFuncRef();
7422
7423 VariantMatchInfo VMI;
7424 OMPTraitInfo &TI = A->getTraitInfo();
7425 TI.getAsVariantMatchInfo(ASTCtx&: Context, VMI);
7426 if (!isVariantApplicableInContext(VMI, Ctx: OMPCtx,
7427 /*DeviceSetOnly=*/DeviceOrImplementationSetOnly: false))
7428 continue;
7429
7430 VMIs.push_back(Elt: VMI);
7431 Exprs.push_back(Elt: VariantRef);
7432 }
7433
7434 CalleeFnDecl = CalleeFnDecl->getPreviousDecl();
7435 }
7436
7437 ExprResult NewCall;
7438 do {
7439 int BestIdx = getBestVariantMatchForContext(VMIs, Ctx: OMPCtx);
7440 if (BestIdx < 0)
7441 return Call;
7442 Expr *BestExpr = cast<DeclRefExpr>(Val: Exprs[BestIdx]);
7443 Decl *BestDecl = cast<DeclRefExpr>(Val: BestExpr)->getDecl();
7444
7445 {
7446 // Try to build a (member) call expression for the current best applicable
7447 // variant expression. We allow this to fail in which case we continue
7448 // with the next best variant expression. The fail case is part of the
7449 // implementation defined behavior in the OpenMP standard when it talks
7450 // about what differences in the function prototypes: "Any differences
7451 // that the specific OpenMP context requires in the prototype of the
7452 // variant from the base function prototype are implementation defined."
7453 // This wording is there to allow the specialized variant to have a
7454 // different type than the base function. This is intended and OK but if
7455 // we cannot create a call the difference is not in the "implementation
7456 // defined range" we allow.
7457 Sema::TentativeAnalysisScope Trap(SemaRef);
7458
7459 if (auto *SpecializedMethod = dyn_cast<CXXMethodDecl>(Val: BestDecl)) {
7460 auto *MemberCall = dyn_cast<CXXMemberCallExpr>(Val: CE);
7461 BestExpr = MemberExpr::CreateImplicit(
7462 C: Context, Base: MemberCall->getImplicitObjectArgument(),
7463 /*IsArrow=*/false, MemberDecl: SpecializedMethod, T: Context.BoundMemberTy,
7464 VK: MemberCall->getValueKind(), OK: MemberCall->getObjectKind());
7465 }
7466 NewCall = SemaRef.BuildCallExpr(S: Scope, Fn: BestExpr, LParenLoc, ArgExprs,
7467 RParenLoc, ExecConfig);
7468 if (NewCall.isUsable()) {
7469 if (CallExpr *NCE = dyn_cast<CallExpr>(Val: NewCall.get())) {
7470 FunctionDecl *NewCalleeFnDecl = NCE->getDirectCallee();
7471 QualType NewType = getASTContext().mergeFunctionTypes(
7472 CalleeFnType, NewCalleeFnDecl->getType(),
7473 /*OfBlockPointer=*/false,
7474 /*Unqualified=*/false, /*AllowCXX=*/true);
7475 if (!NewType.isNull())
7476 break;
7477 // Don't use the call if the function type was not compatible.
7478 NewCall = nullptr;
7479 }
7480 }
7481 }
7482
7483 VMIs.erase(CI: VMIs.begin() + BestIdx);
7484 Exprs.erase(CI: Exprs.begin() + BestIdx);
7485 } while (!VMIs.empty());
7486
7487 if (!NewCall.isUsable())
7488 return Call;
7489 return PseudoObjectExpr::Create(Context: getASTContext(), syntactic: CE, semantic: {NewCall.get()}, resultIndex: 0);
7490}
7491
7492std::optional<std::pair<FunctionDecl *, Expr *>>
7493SemaOpenMP::checkOpenMPDeclareVariantFunction(SemaOpenMP::DeclGroupPtrTy DG,
7494 Expr *VariantRef,
7495 OMPTraitInfo &TI,
7496 unsigned NumAppendArgs,
7497 SourceRange SR) {
7498 ASTContext &Context = getASTContext();
7499 if (!DG || DG.get().isNull())
7500 return std::nullopt;
7501
7502 const int VariantId = 1;
7503 // Must be applied only to single decl.
7504 if (!DG.get().isSingleDecl()) {
7505 Diag(Loc: SR.getBegin(), DiagID: diag::err_omp_single_decl_in_declare_simd_variant)
7506 << VariantId << SR;
7507 return std::nullopt;
7508 }
7509 Decl *ADecl = DG.get().getSingleDecl();
7510 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(Val: ADecl))
7511 ADecl = FTD->getTemplatedDecl();
7512
7513 // Decl must be a function.
7514 auto *FD = dyn_cast<FunctionDecl>(Val: ADecl);
7515 if (!FD) {
7516 Diag(Loc: ADecl->getLocation(), DiagID: diag::err_omp_function_expected)
7517 << VariantId << SR;
7518 return std::nullopt;
7519 }
7520
7521 auto &&HasMultiVersionAttributes = [](const FunctionDecl *FD) {
7522 // The 'target' attribute needs to be separately checked because it does
7523 // not always signify a multiversion function declaration.
7524 return FD->isMultiVersion() || FD->hasAttr<TargetAttr>();
7525 };
7526 // OpenMP is not compatible with multiversion function attributes.
7527 if (HasMultiVersionAttributes(FD)) {
7528 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_incompat_attributes)
7529 << SR;
7530 return std::nullopt;
7531 }
7532
7533 // Allow #pragma omp declare variant only if the function is not used.
7534 if (FD->isUsed(CheckUsedAttr: false))
7535 Diag(Loc: SR.getBegin(), DiagID: diag::warn_omp_declare_variant_after_used)
7536 << FD->getLocation();
7537
7538 // Check if the function was emitted already.
7539 const FunctionDecl *Definition;
7540 if (!FD->isThisDeclarationADefinition() && FD->isDefined(Definition) &&
7541 (getLangOpts().EmitAllDecls || Context.DeclMustBeEmitted(D: Definition)))
7542 Diag(Loc: SR.getBegin(), DiagID: diag::warn_omp_declare_variant_after_emitted)
7543 << FD->getLocation();
7544
7545 // The VariantRef must point to function.
7546 if (!VariantRef) {
7547 Diag(Loc: SR.getBegin(), DiagID: diag::err_omp_function_expected) << VariantId;
7548 return std::nullopt;
7549 }
7550
7551 auto ShouldDelayChecks = [](Expr *&E, bool) {
7552 return E && (E->isTypeDependent() || E->isValueDependent() ||
7553 E->containsUnexpandedParameterPack() ||
7554 E->isInstantiationDependent());
7555 };
7556 // Do not check templates, wait until instantiation.
7557 if (FD->isDependentContext() || ShouldDelayChecks(VariantRef, false) ||
7558 TI.anyScoreOrCondition(Cond: ShouldDelayChecks))
7559 return std::make_pair(x&: FD, y&: VariantRef);
7560
7561 // Deal with non-constant score and user condition expressions.
7562 auto HandleNonConstantScoresAndConditions = [this](Expr *&E,
7563 bool IsScore) -> bool {
7564 if (!E || E->isIntegerConstantExpr(Ctx: getASTContext()))
7565 return false;
7566
7567 if (IsScore) {
7568 // We warn on non-constant scores and pretend they were not present.
7569 Diag(Loc: E->getExprLoc(), DiagID: diag::warn_omp_declare_variant_score_not_constant)
7570 << E;
7571 E = nullptr;
7572 } else {
7573 // We could replace a non-constant user condition with "false" but we
7574 // will soon need to handle these anyway for the dynamic version of
7575 // OpenMP context selectors.
7576 Diag(Loc: E->getExprLoc(),
7577 DiagID: diag::err_omp_declare_variant_user_condition_not_constant)
7578 << E;
7579 }
7580 return true;
7581 };
7582 if (TI.anyScoreOrCondition(Cond: HandleNonConstantScoresAndConditions))
7583 return std::nullopt;
7584
7585 QualType AdjustedFnType = FD->getType();
7586 if (NumAppendArgs) {
7587 const auto *PTy = AdjustedFnType->getAsAdjusted<FunctionProtoType>();
7588 if (!PTy) {
7589 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_prototype_required)
7590 << SR;
7591 return std::nullopt;
7592 }
7593 // Adjust the function type to account for an extra omp_interop_t for each
7594 // specified in the append_args clause.
7595 const TypeDecl *TD = nullptr;
7596 LookupResult Result(SemaRef, &Context.Idents.get(Name: "omp_interop_t"),
7597 SR.getBegin(), Sema::LookupOrdinaryName);
7598 if (SemaRef.LookupName(R&: Result, S: SemaRef.getCurScope())) {
7599 NamedDecl *ND = Result.getFoundDecl();
7600 TD = dyn_cast_or_null<TypeDecl>(Val: ND);
7601 }
7602 if (!TD) {
7603 Diag(Loc: SR.getBegin(), DiagID: diag::err_omp_interop_type_not_found) << SR;
7604 return std::nullopt;
7605 }
7606 QualType InteropType =
7607 Context.getTypeDeclType(Keyword: ElaboratedTypeKeyword::None,
7608 /*Qualifier=*/std::nullopt, Decl: TD);
7609 if (PTy->isVariadic()) {
7610 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_append_args_with_varargs) << SR;
7611 return std::nullopt;
7612 }
7613 llvm::SmallVector<QualType, 8> Params;
7614 Params.append(in_start: PTy->param_type_begin(), in_end: PTy->param_type_end());
7615 Params.insert(I: Params.end(), NumToInsert: NumAppendArgs, Elt: InteropType);
7616 AdjustedFnType = Context.getFunctionType(ResultTy: PTy->getReturnType(), Args: Params,
7617 EPI: PTy->getExtProtoInfo());
7618 }
7619
7620 // Convert VariantRef expression to the type of the original function to
7621 // resolve possible conflicts.
7622 ExprResult VariantRefCast = VariantRef;
7623 if (getLangOpts().CPlusPlus) {
7624 QualType FnPtrType;
7625 auto *Method = dyn_cast<CXXMethodDecl>(Val: FD);
7626 if (Method && !Method->isStatic()) {
7627 FnPtrType = Context.getMemberPointerType(
7628 T: AdjustedFnType, /*Qualifier=*/std::nullopt, Cls: Method->getParent());
7629 ExprResult ER;
7630 {
7631 // Build addr_of unary op to correctly handle type checks for member
7632 // functions.
7633 Sema::TentativeAnalysisScope Trap(SemaRef);
7634 ER = SemaRef.CreateBuiltinUnaryOp(OpLoc: VariantRef->getBeginLoc(), Opc: UO_AddrOf,
7635 InputExpr: VariantRef);
7636 }
7637 if (!ER.isUsable()) {
7638 Diag(Loc: VariantRef->getExprLoc(), DiagID: diag::err_omp_function_expected)
7639 << VariantId << VariantRef->getSourceRange();
7640 return std::nullopt;
7641 }
7642 VariantRef = ER.get();
7643 } else {
7644 FnPtrType = Context.getPointerType(T: AdjustedFnType);
7645 }
7646 QualType VarianPtrType = Context.getPointerType(T: VariantRef->getType());
7647 if (VarianPtrType.getUnqualifiedType() != FnPtrType.getUnqualifiedType()) {
7648 ImplicitConversionSequence ICS = SemaRef.TryImplicitConversion(
7649 From: VariantRef, ToType: FnPtrType.getUnqualifiedType(),
7650 /*SuppressUserConversions=*/false, AllowExplicit: Sema::AllowedExplicit::None,
7651 /*InOverloadResolution=*/false,
7652 /*CStyle=*/false,
7653 /*AllowObjCWritebackConversion=*/false);
7654 if (ICS.isFailure()) {
7655 Diag(Loc: VariantRef->getExprLoc(),
7656 DiagID: diag::err_omp_declare_variant_incompat_types)
7657 << VariantRef->getType()
7658 << ((Method && !Method->isStatic()) ? FnPtrType : FD->getType())
7659 << (NumAppendArgs ? 1 : 0) << VariantRef->getSourceRange();
7660 return std::nullopt;
7661 }
7662 VariantRefCast = SemaRef.PerformImplicitConversion(
7663 From: VariantRef, ToType: FnPtrType.getUnqualifiedType(),
7664 Action: AssignmentAction::Converting);
7665 if (!VariantRefCast.isUsable())
7666 return std::nullopt;
7667 }
7668 // Drop previously built artificial addr_of unary op for member functions.
7669 if (Method && !Method->isStatic()) {
7670 Expr *PossibleAddrOfVariantRef = VariantRefCast.get();
7671 if (auto *UO = dyn_cast<UnaryOperator>(
7672 Val: PossibleAddrOfVariantRef->IgnoreImplicit()))
7673 VariantRefCast = UO->getSubExpr();
7674 }
7675 }
7676
7677 ExprResult ER = SemaRef.CheckPlaceholderExpr(E: VariantRefCast.get());
7678 if (!ER.isUsable() ||
7679 !ER.get()->IgnoreParenImpCasts()->getType()->isFunctionType()) {
7680 Diag(Loc: VariantRef->getExprLoc(), DiagID: diag::err_omp_function_expected)
7681 << VariantId << VariantRef->getSourceRange();
7682 return std::nullopt;
7683 }
7684
7685 // The VariantRef must point to function.
7686 auto *DRE = dyn_cast<DeclRefExpr>(Val: ER.get()->IgnoreParenImpCasts());
7687 if (!DRE) {
7688 Diag(Loc: VariantRef->getExprLoc(), DiagID: diag::err_omp_function_expected)
7689 << VariantId << VariantRef->getSourceRange();
7690 return std::nullopt;
7691 }
7692 auto *NewFD = dyn_cast_or_null<FunctionDecl>(Val: DRE->getDecl());
7693 if (!NewFD) {
7694 Diag(Loc: VariantRef->getExprLoc(), DiagID: diag::err_omp_function_expected)
7695 << VariantId << VariantRef->getSourceRange();
7696 return std::nullopt;
7697 }
7698
7699 if (FD->getCanonicalDecl() == NewFD->getCanonicalDecl()) {
7700 Diag(Loc: VariantRef->getExprLoc(),
7701 DiagID: diag::err_omp_declare_variant_same_base_function)
7702 << VariantRef->getSourceRange();
7703 return std::nullopt;
7704 }
7705
7706 // Check if function types are compatible in C.
7707 if (!getLangOpts().CPlusPlus) {
7708 QualType NewType =
7709 Context.mergeFunctionTypes(AdjustedFnType, NewFD->getType());
7710 if (NewType.isNull()) {
7711 Diag(Loc: VariantRef->getExprLoc(),
7712 DiagID: diag::err_omp_declare_variant_incompat_types)
7713 << NewFD->getType() << FD->getType() << (NumAppendArgs ? 1 : 0)
7714 << VariantRef->getSourceRange();
7715 return std::nullopt;
7716 }
7717 if (NewType->isFunctionProtoType()) {
7718 if (FD->getType()->isFunctionNoProtoType())
7719 setPrototype(S&: SemaRef, FD, FDWithProto: NewFD, NewType);
7720 else if (NewFD->getType()->isFunctionNoProtoType())
7721 setPrototype(S&: SemaRef, FD: NewFD, FDWithProto: FD, NewType);
7722 }
7723 }
7724
7725 // Check if variant function is not marked with declare variant directive.
7726 if (NewFD->hasAttrs() && NewFD->hasAttr<OMPDeclareVariantAttr>()) {
7727 Diag(Loc: VariantRef->getExprLoc(),
7728 DiagID: diag::warn_omp_declare_variant_marked_as_declare_variant)
7729 << VariantRef->getSourceRange();
7730 SourceRange SR =
7731 NewFD->specific_attr_begin<OMPDeclareVariantAttr>()->getRange();
7732 Diag(Loc: SR.getBegin(), DiagID: diag::note_omp_marked_declare_variant_here) << SR;
7733 return std::nullopt;
7734 }
7735
7736 enum DoesntSupport {
7737 VirtFuncs = 1,
7738 Constructors = 3,
7739 Destructors = 4,
7740 DeletedFuncs = 5,
7741 DefaultedFuncs = 6,
7742 ConstexprFuncs = 7,
7743 ConstevalFuncs = 8,
7744 };
7745 if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(Val: FD)) {
7746 if (CXXFD->isVirtual()) {
7747 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_doesnt_support)
7748 << VirtFuncs;
7749 return std::nullopt;
7750 }
7751
7752 if (isa<CXXConstructorDecl>(Val: FD)) {
7753 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_doesnt_support)
7754 << Constructors;
7755 return std::nullopt;
7756 }
7757
7758 if (isa<CXXDestructorDecl>(Val: FD)) {
7759 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_doesnt_support)
7760 << Destructors;
7761 return std::nullopt;
7762 }
7763 }
7764
7765 if (FD->isDeleted()) {
7766 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_doesnt_support)
7767 << DeletedFuncs;
7768 return std::nullopt;
7769 }
7770
7771 if (FD->isDefaulted()) {
7772 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_doesnt_support)
7773 << DefaultedFuncs;
7774 return std::nullopt;
7775 }
7776
7777 if (FD->isConstexpr()) {
7778 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_doesnt_support)
7779 << (NewFD->isConsteval() ? ConstevalFuncs : ConstexprFuncs);
7780 return std::nullopt;
7781 }
7782
7783 // Check general compatibility.
7784 if (SemaRef.areMultiversionVariantFunctionsCompatible(
7785 OldFD: FD, NewFD, NoProtoDiagID: PartialDiagnostic::NullDiagnostic(),
7786 NoteCausedDiagIDAt: PartialDiagnosticAt(SourceLocation(),
7787 PartialDiagnostic::NullDiagnostic()),
7788 NoSupportDiagIDAt: PartialDiagnosticAt(
7789 VariantRef->getExprLoc(),
7790 SemaRef.PDiag(DiagID: diag::err_omp_declare_variant_doesnt_support)),
7791 DiffDiagIDAt: PartialDiagnosticAt(VariantRef->getExprLoc(),
7792 SemaRef.PDiag(DiagID: diag::err_omp_declare_variant_diff)
7793 << FD->getLocation()),
7794 /*TemplatesSupported=*/true, /*ConstexprSupported=*/false,
7795 /*CLinkageMayDiffer=*/true))
7796 return std::nullopt;
7797 return std::make_pair(x&: FD, y: cast<Expr>(Val: DRE));
7798}
7799
7800void SemaOpenMP::ActOnOpenMPDeclareVariantDirective(
7801 FunctionDecl *FD, Expr *VariantRef, OMPTraitInfo &TI,
7802 ArrayRef<Expr *> AdjustArgsNothing,
7803 ArrayRef<Expr *> AdjustArgsNeedDevicePtr,
7804 ArrayRef<Expr *> AdjustArgsNeedDeviceAddr,
7805 ArrayRef<OMPInteropInfo> AppendArgs, SourceLocation AdjustArgsLoc,
7806 SourceLocation AppendArgsLoc, SourceRange SR) {
7807
7808 // OpenMP 5.1 [2.3.5, declare variant directive, Restrictions]
7809 // An adjust_args clause or append_args clause can only be specified if the
7810 // dispatch selector of the construct selector set appears in the match
7811 // clause.
7812
7813 SmallVector<Expr *, 8> AllAdjustArgs;
7814 llvm::append_range(C&: AllAdjustArgs, R&: AdjustArgsNothing);
7815 llvm::append_range(C&: AllAdjustArgs, R&: AdjustArgsNeedDevicePtr);
7816 llvm::append_range(C&: AllAdjustArgs, R&: AdjustArgsNeedDeviceAddr);
7817
7818 if (!AllAdjustArgs.empty() || !AppendArgs.empty()) {
7819 VariantMatchInfo VMI;
7820 TI.getAsVariantMatchInfo(ASTCtx&: getASTContext(), VMI);
7821 if (!llvm::is_contained(
7822 Range&: VMI.ConstructTraits,
7823 Element: llvm::omp::TraitProperty::construct_dispatch_dispatch)) {
7824 if (!AllAdjustArgs.empty())
7825 Diag(Loc: AdjustArgsLoc, DiagID: diag::err_omp_clause_requires_dispatch_construct)
7826 << getOpenMPClauseNameForDiag(C: OMPC_adjust_args);
7827 if (!AppendArgs.empty())
7828 Diag(Loc: AppendArgsLoc, DiagID: diag::err_omp_clause_requires_dispatch_construct)
7829 << getOpenMPClauseNameForDiag(C: OMPC_append_args);
7830 return;
7831 }
7832 }
7833
7834 // OpenMP 5.1 [2.3.5, declare variant directive, Restrictions]
7835 // Each argument can only appear in a single adjust_args clause for each
7836 // declare variant directive.
7837 llvm::SmallPtrSet<const VarDecl *, 4> AdjustVars;
7838
7839 for (Expr *E : AllAdjustArgs) {
7840 E = E->IgnoreParenImpCasts();
7841 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: E)) {
7842 if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: DRE->getDecl())) {
7843 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
7844 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
7845 FD->getParamDecl(i: PVD->getFunctionScopeIndex())
7846 ->getCanonicalDecl() == CanonPVD) {
7847 // It's a parameter of the function, check duplicates.
7848 if (!AdjustVars.insert(Ptr: CanonPVD).second) {
7849 Diag(Loc: DRE->getLocation(), DiagID: diag::err_omp_adjust_arg_multiple_clauses)
7850 << PVD;
7851 return;
7852 }
7853 continue;
7854 }
7855 }
7856 }
7857 // Anything that is not a function parameter is an error.
7858 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_param_or_this_in_clause) << FD << 0;
7859 return;
7860 }
7861
7862 // OpenMP 6.0 [9.6.2 (page 332, line 31-33, adjust_args clause, Restrictions]
7863 // If the `need_device_addr` adjust-op modifier is present, each list item
7864 // that appears in the clause must refer to an argument in the declaration of
7865 // the function variant that has a reference type
7866 if (getLangOpts().OpenMP >= 60) {
7867 for (Expr *E : AdjustArgsNeedDeviceAddr) {
7868 E = E->IgnoreParenImpCasts();
7869 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: E)) {
7870 if (const auto *VD = dyn_cast<VarDecl>(Val: DRE->getDecl())) {
7871 if (!VD->getType()->isReferenceType())
7872 Diag(Loc: E->getExprLoc(),
7873 DiagID: diag::err_omp_non_by_ref_need_device_addr_modifier_argument);
7874 }
7875 }
7876 }
7877 }
7878
7879 auto *NewAttr = OMPDeclareVariantAttr::CreateImplicit(
7880 Ctx&: getASTContext(), VariantFuncRef: VariantRef, TraitInfos: &TI,
7881 AdjustArgsNothing: const_cast<Expr **>(AdjustArgsNothing.data()), AdjustArgsNothingSize: AdjustArgsNothing.size(),
7882 AdjustArgsNeedDevicePtr: const_cast<Expr **>(AdjustArgsNeedDevicePtr.data()),
7883 AdjustArgsNeedDevicePtrSize: AdjustArgsNeedDevicePtr.size(),
7884 AdjustArgsNeedDeviceAddr: const_cast<Expr **>(AdjustArgsNeedDeviceAddr.data()),
7885 AdjustArgsNeedDeviceAddrSize: AdjustArgsNeedDeviceAddr.size(),
7886 AppendArgs: const_cast<OMPInteropInfo *>(AppendArgs.data()), AppendArgsSize: AppendArgs.size(), Range: SR);
7887 FD->addAttr(A: NewAttr);
7888}
7889
7890static CapturedStmt *
7891setBranchProtectedScope(Sema &SemaRef, OpenMPDirectiveKind DKind, Stmt *AStmt) {
7892 auto *CS = dyn_cast<CapturedStmt>(Val: AStmt);
7893 assert(CS && "Captured statement expected");
7894 // 1.2.2 OpenMP Language Terminology
7895 // Structured block - An executable statement with a single entry at the
7896 // top and a single exit at the bottom.
7897 // The point of exit cannot be a branch out of the structured block.
7898 // longjmp() and throw() must not violate the entry/exit criteria.
7899 CS->getCapturedDecl()->setNothrow();
7900
7901 for (int ThisCaptureLevel = SemaRef.OpenMP().getOpenMPCaptureLevels(DKind);
7902 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7903 CS = cast<CapturedStmt>(Val: CS->getCapturedStmt());
7904 // 1.2.2 OpenMP Language Terminology
7905 // Structured block - An executable statement with a single entry at the
7906 // top and a single exit at the bottom.
7907 // The point of exit cannot be a branch out of the structured block.
7908 // longjmp() and throw() must not violate the entry/exit criteria.
7909 CS->getCapturedDecl()->setNothrow();
7910 }
7911 SemaRef.setFunctionHasBranchProtectedScope();
7912 return CS;
7913}
7914
7915StmtResult
7916SemaOpenMP::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
7917 Stmt *AStmt, SourceLocation StartLoc,
7918 SourceLocation EndLoc) {
7919 if (!AStmt)
7920 return StmtError();
7921
7922 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel, AStmt);
7923
7924 return OMPParallelDirective::Create(
7925 C: getASTContext(), StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
7926 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
7927}
7928
7929namespace {
7930/// Iteration space of a single for loop.
7931struct LoopIterationSpace final {
7932 /// True if the condition operator is the strict compare operator (<, > or
7933 /// !=).
7934 bool IsStrictCompare = false;
7935 /// Condition of the loop.
7936 Expr *PreCond = nullptr;
7937 /// This expression calculates the number of iterations in the loop.
7938 /// It is always possible to calculate it before starting the loop.
7939 Expr *NumIterations = nullptr;
7940 /// The loop counter variable.
7941 Expr *CounterVar = nullptr;
7942 /// Private loop counter variable.
7943 Expr *PrivateCounterVar = nullptr;
7944 /// This is initializer for the initial value of #CounterVar.
7945 Expr *CounterInit = nullptr;
7946 /// This is step for the #CounterVar used to generate its update:
7947 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
7948 Expr *CounterStep = nullptr;
7949 /// Should step be subtracted?
7950 bool Subtract = false;
7951 /// Source range of the loop init.
7952 SourceRange InitSrcRange;
7953 /// Source range of the loop condition.
7954 SourceRange CondSrcRange;
7955 /// Source range of the loop increment.
7956 SourceRange IncSrcRange;
7957 /// Minimum value that can have the loop control variable. Used to support
7958 /// non-rectangular loops. Applied only for LCV with the non-iterator types,
7959 /// since only such variables can be used in non-loop invariant expressions.
7960 Expr *MinValue = nullptr;
7961 /// Maximum value that can have the loop control variable. Used to support
7962 /// non-rectangular loops. Applied only for LCV with the non-iterator type,
7963 /// since only such variables can be used in non-loop invariant expressions.
7964 Expr *MaxValue = nullptr;
7965 /// true, if the lower bound depends on the outer loop control var.
7966 bool IsNonRectangularLB = false;
7967 /// true, if the upper bound depends on the outer loop control var.
7968 bool IsNonRectangularUB = false;
7969 /// Index of the loop this loop depends on and forms non-rectangular loop
7970 /// nest.
7971 unsigned LoopDependentIdx = 0;
7972 /// Final condition for the non-rectangular loop nest support. It is used to
7973 /// check that the number of iterations for this particular counter must be
7974 /// finished.
7975 Expr *FinalCondition = nullptr;
7976};
7977
7978/// Scan an AST subtree, checking that no decls in the CollapsedLoopVarDecls
7979/// set are referenced. Used for verifying loop nest structure before
7980/// performing a loop collapse operation.
7981class ForSubExprChecker : public DynamicRecursiveASTVisitor {
7982 const llvm::SmallPtrSetImpl<const Decl *> &CollapsedLoopVarDecls;
7983 VarDecl *ForbiddenVar = nullptr;
7984 SourceRange ErrLoc;
7985
7986public:
7987 explicit ForSubExprChecker(
7988 const llvm::SmallPtrSetImpl<const Decl *> &CollapsedLoopVarDecls)
7989 : CollapsedLoopVarDecls(CollapsedLoopVarDecls) {
7990 // We want to visit implicit code, i.e. synthetic initialisation statements
7991 // created during range-for lowering.
7992 ShouldVisitImplicitCode = true;
7993 }
7994
7995 bool VisitDeclRefExpr(DeclRefExpr *E) override {
7996 ValueDecl *VD = E->getDecl();
7997 if (!isa<VarDecl, BindingDecl>(Val: VD))
7998 return true;
7999 VarDecl *V = VD->getPotentiallyDecomposedVarDecl();
8000 if (V->getType()->isReferenceType()) {
8001 VarDecl *VD = V->getDefinition();
8002 if (VD->hasInit()) {
8003 Expr *I = VD->getInit();
8004 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Val: I);
8005 if (!DRE)
8006 return true;
8007 V = DRE->getDecl()->getPotentiallyDecomposedVarDecl();
8008 }
8009 }
8010 Decl *Canon = V->getCanonicalDecl();
8011 if (CollapsedLoopVarDecls.contains(Ptr: Canon)) {
8012 ForbiddenVar = V;
8013 ErrLoc = E->getSourceRange();
8014 return false;
8015 }
8016
8017 return true;
8018 }
8019
8020 VarDecl *getForbiddenVar() const { return ForbiddenVar; }
8021 SourceRange getErrRange() const { return ErrLoc; }
8022};
8023
8024/// Helper class for checking canonical form of the OpenMP loops and
8025/// extracting iteration space of each loop in the loop nest, that will be used
8026/// for IR generation.
8027class OpenMPIterationSpaceChecker {
8028 /// Reference to Sema.
8029 Sema &SemaRef;
8030 /// Does the loop associated directive support non-rectangular loops?
8031 bool SupportsNonRectangular;
8032 /// Data-sharing stack.
8033 DSAStackTy &Stack;
8034 /// A location for diagnostics (when there is no some better location).
8035 SourceLocation DefaultLoc;
8036 /// A location for diagnostics (when increment is not compatible).
8037 SourceLocation ConditionLoc;
8038 /// The set of variables declared within the (to be collapsed) loop nest.
8039 const llvm::SmallPtrSetImpl<const Decl *> &CollapsedLoopVarDecls;
8040 /// A source location for referring to loop init later.
8041 SourceRange InitSrcRange;
8042 /// A source location for referring to condition later.
8043 SourceRange ConditionSrcRange;
8044 /// A source location for referring to increment later.
8045 SourceRange IncrementSrcRange;
8046 /// Loop variable.
8047 ValueDecl *LCDecl = nullptr;
8048 /// Reference to loop variable.
8049 Expr *LCRef = nullptr;
8050 /// Lower bound (initializer for the var).
8051 Expr *LB = nullptr;
8052 /// Upper bound.
8053 Expr *UB = nullptr;
8054 /// Loop step (increment).
8055 Expr *Step = nullptr;
8056 /// This flag is true when condition is one of:
8057 /// Var < UB
8058 /// Var <= UB
8059 /// UB > Var
8060 /// UB >= Var
8061 /// This will have no value when the condition is !=
8062 std::optional<bool> TestIsLessOp;
8063 /// This flag is true when condition is strict ( < or > ).
8064 bool TestIsStrictOp = false;
8065 /// This flag is true when step is subtracted on each iteration.
8066 bool SubtractStep = false;
8067 /// The outer loop counter this loop depends on (if any).
8068 const ValueDecl *DepDecl = nullptr;
8069 /// Contains number of loop (starts from 1) on which loop counter init
8070 /// expression of this loop depends on.
8071 std::optional<unsigned> InitDependOnLC;
8072 /// Contains number of loop (starts from 1) on which loop counter condition
8073 /// expression of this loop depends on.
8074 std::optional<unsigned> CondDependOnLC;
8075 /// Checks if the provide statement depends on the loop counter.
8076 std::optional<unsigned> doesDependOnLoopCounter(const Stmt *S,
8077 bool IsInitializer);
8078 /// Original condition required for checking of the exit condition for
8079 /// non-rectangular loop.
8080 Expr *Condition = nullptr;
8081
8082public:
8083 OpenMPIterationSpaceChecker(
8084 Sema &SemaRef, bool SupportsNonRectangular, DSAStackTy &Stack,
8085 SourceLocation DefaultLoc,
8086 const llvm::SmallPtrSetImpl<const Decl *> &CollapsedLoopDecls)
8087 : SemaRef(SemaRef), SupportsNonRectangular(SupportsNonRectangular),
8088 Stack(Stack), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
8089 CollapsedLoopVarDecls(CollapsedLoopDecls) {}
8090 /// Check init-expr for canonical loop form and save loop counter
8091 /// variable - #Var and its initialization value - #LB.
8092 bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
8093 /// Check test-expr for canonical form, save upper-bound (#UB), flags
8094 /// for less/greater and for strict/non-strict comparison.
8095 bool checkAndSetCond(Expr *S);
8096 /// Check incr-expr for canonical loop form and return true if it
8097 /// does not conform, otherwise save loop step (#Step).
8098 bool checkAndSetInc(Expr *S);
8099 /// Return the loop counter variable.
8100 ValueDecl *getLoopDecl() const { return LCDecl; }
8101 /// Return the reference expression to loop counter variable.
8102 Expr *getLoopDeclRefExpr() const { return LCRef; }
8103 /// Source range of the loop init.
8104 SourceRange getInitSrcRange() const { return InitSrcRange; }
8105 /// Source range of the loop condition.
8106 SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
8107 /// Source range of the loop increment.
8108 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
8109 /// True if the step should be subtracted.
8110 bool shouldSubtractStep() const { return SubtractStep; }
8111 /// True, if the compare operator is strict (<, > or !=).
8112 bool isStrictTestOp() const { return TestIsStrictOp; }
8113 /// Build the expression to calculate the number of iterations.
8114 Expr *buildNumIterations(
8115 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType,
8116 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
8117 /// Build the precondition expression for the loops.
8118 Expr *
8119 buildPreCond(Scope *S, Expr *Cond,
8120 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
8121 /// Build reference expression to the counter be used for codegen.
8122 DeclRefExpr *
8123 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
8124 DSAStackTy &DSA) const;
8125 /// Build reference expression to the private counter be used for
8126 /// codegen.
8127 Expr *buildPrivateCounterVar() const;
8128 /// Build initialization of the counter be used for codegen.
8129 Expr *buildCounterInit() const;
8130 /// Build step of the counter be used for codegen.
8131 Expr *buildCounterStep() const;
8132 /// Build loop data with counter value for depend clauses in ordered
8133 /// directives.
8134 Expr *
8135 buildOrderedLoopData(Scope *S, Expr *Counter,
8136 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
8137 SourceLocation Loc, Expr *Inc = nullptr,
8138 OverloadedOperatorKind OOK = OO_Amp);
8139 /// Builds the minimum value for the loop counter.
8140 std::pair<Expr *, Expr *> buildMinMaxValues(
8141 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
8142 /// Builds final condition for the non-rectangular loops.
8143 Expr *buildFinalCondition(Scope *S) const;
8144 /// Return true if any expression is dependent.
8145 bool dependent() const;
8146 /// Returns true if the initializer forms non-rectangular loop.
8147 bool doesInitDependOnLC() const { return InitDependOnLC.has_value(); }
8148 /// Returns true if the condition forms non-rectangular loop.
8149 bool doesCondDependOnLC() const { return CondDependOnLC.has_value(); }
8150 /// Returns index of the loop we depend on (starting from 1), or 0 otherwise.
8151 unsigned getLoopDependentIdx() const {
8152 return InitDependOnLC.value_or(u: CondDependOnLC.value_or(u: 0));
8153 }
8154
8155private:
8156 /// Check the right-hand side of an assignment in the increment
8157 /// expression.
8158 bool checkAndSetIncRHS(Expr *RHS);
8159 /// Helper to set loop counter variable and its initializer.
8160 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB,
8161 bool EmitDiags);
8162 /// Helper to set upper bound.
8163 bool setUB(Expr *NewUB, std::optional<bool> LessOp, bool StrictOp,
8164 SourceRange SR, SourceLocation SL);
8165 /// Helper to set loop increment.
8166 bool setStep(Expr *NewStep, bool Subtract);
8167};
8168
8169bool OpenMPIterationSpaceChecker::dependent() const {
8170 if (!LCDecl) {
8171 assert(!LB && !UB && !Step);
8172 return false;
8173 }
8174 return LCDecl->getType()->isDependentType() ||
8175 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
8176 (Step && Step->isValueDependent());
8177}
8178
8179bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
8180 Expr *NewLCRefExpr,
8181 Expr *NewLB, bool EmitDiags) {
8182 // State consistency checking to ensure correct usage.
8183 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
8184 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
8185 if (!NewLCDecl || !NewLB || NewLB->containsErrors())
8186 return true;
8187 LCDecl = getCanonicalDecl(D: NewLCDecl);
8188 LCRef = NewLCRefExpr;
8189 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(Val: NewLB))
8190 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
8191 if ((Ctor->isCopyOrMoveConstructor() ||
8192 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
8193 CE->getNumArgs() > 0 && CE->getArg(Arg: 0) != nullptr)
8194 NewLB = CE->getArg(Arg: 0)->IgnoreParenImpCasts();
8195 LB = NewLB;
8196 if (EmitDiags)
8197 InitDependOnLC = doesDependOnLoopCounter(S: LB, /*IsInitializer=*/true);
8198 return false;
8199}
8200
8201bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, std::optional<bool> LessOp,
8202 bool StrictOp, SourceRange SR,
8203 SourceLocation SL) {
8204 // State consistency checking to ensure correct usage.
8205 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
8206 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
8207 if (!NewUB || NewUB->containsErrors())
8208 return true;
8209 UB = NewUB;
8210 if (LessOp)
8211 TestIsLessOp = LessOp;
8212 TestIsStrictOp = StrictOp;
8213 ConditionSrcRange = SR;
8214 ConditionLoc = SL;
8215 CondDependOnLC = doesDependOnLoopCounter(S: UB, /*IsInitializer=*/false);
8216 return false;
8217}
8218
8219bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
8220 // State consistency checking to ensure correct usage.
8221 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
8222 if (!NewStep || NewStep->containsErrors())
8223 return true;
8224 if (!NewStep->isValueDependent()) {
8225 // Check that the step is integer expression.
8226 SourceLocation StepLoc = NewStep->getBeginLoc();
8227 ExprResult Val = SemaRef.OpenMP().PerformOpenMPImplicitIntegerConversion(
8228 OpLoc: StepLoc, Op: getExprAsWritten(E: NewStep));
8229 if (Val.isInvalid())
8230 return true;
8231 NewStep = Val.get();
8232
8233 // OpenMP [2.6, Canonical Loop Form, Restrictions]
8234 // If test-expr is of form var relational-op b and relational-op is < or
8235 // <= then incr-expr must cause var to increase on each iteration of the
8236 // loop. If test-expr is of form var relational-op b and relational-op is
8237 // > or >= then incr-expr must cause var to decrease on each iteration of
8238 // the loop.
8239 // If test-expr is of form b relational-op var and relational-op is < or
8240 // <= then incr-expr must cause var to decrease on each iteration of the
8241 // loop. If test-expr is of form b relational-op var and relational-op is
8242 // > or >= then incr-expr must cause var to increase on each iteration of
8243 // the loop.
8244 std::optional<llvm::APSInt> Result =
8245 NewStep->getIntegerConstantExpr(Ctx: SemaRef.Context);
8246 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
8247 bool IsConstNeg =
8248 Result && Result->isSigned() && (Subtract != Result->isNegative());
8249 bool IsConstPos =
8250 Result && Result->isSigned() && (Subtract == Result->isNegative());
8251 bool IsConstZero = Result && !Result->getBoolValue();
8252
8253 // != with increment is treated as <; != with decrement is treated as >
8254 if (!TestIsLessOp)
8255 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract);
8256 if (UB && (IsConstZero ||
8257 (*TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
8258 : (IsConstPos || (IsUnsigned && !Subtract))))) {
8259 SemaRef.Diag(Loc: NewStep->getExprLoc(),
8260 DiagID: diag::err_omp_loop_incr_not_compatible)
8261 << LCDecl << *TestIsLessOp << NewStep->getSourceRange();
8262 SemaRef.Diag(Loc: ConditionLoc,
8263 DiagID: diag::note_omp_loop_cond_requires_compatible_incr)
8264 << *TestIsLessOp << ConditionSrcRange;
8265 return true;
8266 }
8267 if (*TestIsLessOp == Subtract) {
8268 NewStep =
8269 SemaRef.CreateBuiltinUnaryOp(OpLoc: NewStep->getExprLoc(), Opc: UO_Minus, InputExpr: NewStep)
8270 .get();
8271 Subtract = !Subtract;
8272 }
8273 }
8274
8275 Step = NewStep;
8276 SubtractStep = Subtract;
8277 return false;
8278}
8279
8280namespace {
8281/// Checker for the non-rectangular loops. Checks if the initializer or
8282/// condition expression references loop counter variable.
8283class LoopCounterRefChecker final
8284 : public ConstStmtVisitor<LoopCounterRefChecker, bool> {
8285 Sema &SemaRef;
8286 DSAStackTy &Stack;
8287 const ValueDecl *CurLCDecl = nullptr;
8288 const ValueDecl *DepDecl = nullptr;
8289 const ValueDecl *PrevDepDecl = nullptr;
8290 bool IsInitializer = true;
8291 bool SupportsNonRectangular;
8292 unsigned BaseLoopId = 0;
8293 bool checkDecl(const Expr *E, const ValueDecl *VD) {
8294 if (getCanonicalDecl(D: VD) == getCanonicalDecl(D: CurLCDecl)) {
8295 SemaRef.Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_stmt_depends_on_loop_counter)
8296 << (IsInitializer ? 0 : 1);
8297 return false;
8298 }
8299 const auto &&Data = Stack.isLoopControlVariable(D: VD);
8300 // OpenMP, 2.9.1 Canonical Loop Form, Restrictions.
8301 // The type of the loop iterator on which we depend may not have a random
8302 // access iterator type.
8303 if (Data.first && VD->getType()->isRecordType()) {
8304 SmallString<128> Name;
8305 llvm::raw_svector_ostream OS(Name);
8306 VD->getNameForDiagnostic(OS, Policy: SemaRef.getPrintingPolicy(),
8307 /*Qualified=*/true);
8308 SemaRef.Diag(Loc: E->getExprLoc(),
8309 DiagID: diag::err_omp_wrong_dependency_iterator_type)
8310 << OS.str();
8311 SemaRef.Diag(Loc: VD->getLocation(), DiagID: diag::note_previous_decl) << VD;
8312 return false;
8313 }
8314 if (Data.first && !SupportsNonRectangular) {
8315 SemaRef.Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_invariant_dependency);
8316 return false;
8317 }
8318 if (Data.first &&
8319 (DepDecl || (PrevDepDecl &&
8320 getCanonicalDecl(D: VD) != getCanonicalDecl(D: PrevDepDecl)))) {
8321 if (!DepDecl && PrevDepDecl)
8322 DepDecl = PrevDepDecl;
8323 SmallString<128> Name;
8324 llvm::raw_svector_ostream OS(Name);
8325 DepDecl->getNameForDiagnostic(OS, Policy: SemaRef.getPrintingPolicy(),
8326 /*Qualified=*/true);
8327 SemaRef.Diag(Loc: E->getExprLoc(),
8328 DiagID: diag::err_omp_invariant_or_linear_dependency)
8329 << OS.str();
8330 return false;
8331 }
8332 if (Data.first) {
8333 DepDecl = VD;
8334 BaseLoopId = Data.first;
8335 }
8336 return Data.first;
8337 }
8338
8339public:
8340 bool VisitDeclRefExpr(const DeclRefExpr *E) {
8341 const ValueDecl *VD = E->getDecl();
8342 if (isa<VarDecl>(Val: VD))
8343 return checkDecl(E, VD);
8344 return false;
8345 }
8346 bool VisitMemberExpr(const MemberExpr *E) {
8347 if (isa<CXXThisExpr>(Val: E->getBase()->IgnoreParens())) {
8348 const ValueDecl *VD = E->getMemberDecl();
8349 if (isa<VarDecl>(Val: VD) || isa<FieldDecl>(Val: VD))
8350 return checkDecl(E, VD);
8351 }
8352 return false;
8353 }
8354 bool VisitStmt(const Stmt *S) {
8355 bool Res = false;
8356 for (const Stmt *Child : S->children())
8357 Res = (Child && Visit(S: Child)) || Res;
8358 return Res;
8359 }
8360 explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack,
8361 const ValueDecl *CurLCDecl, bool IsInitializer,
8362 const ValueDecl *PrevDepDecl = nullptr,
8363 bool SupportsNonRectangular = true)
8364 : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl),
8365 PrevDepDecl(PrevDepDecl), IsInitializer(IsInitializer),
8366 SupportsNonRectangular(SupportsNonRectangular) {}
8367 unsigned getBaseLoopId() const {
8368 assert(CurLCDecl && "Expected loop dependency.");
8369 return BaseLoopId;
8370 }
8371 const ValueDecl *getDepDecl() const {
8372 assert(CurLCDecl && "Expected loop dependency.");
8373 return DepDecl;
8374 }
8375};
8376} // namespace
8377
8378std::optional<unsigned>
8379OpenMPIterationSpaceChecker::doesDependOnLoopCounter(const Stmt *S,
8380 bool IsInitializer) {
8381 // Check for the non-rectangular loops.
8382 LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer,
8383 DepDecl, SupportsNonRectangular);
8384 if (LoopStmtChecker.Visit(S)) {
8385 DepDecl = LoopStmtChecker.getDepDecl();
8386 return LoopStmtChecker.getBaseLoopId();
8387 }
8388 return std::nullopt;
8389}
8390
8391bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
8392 // Check init-expr for canonical loop form and save loop counter
8393 // variable - #Var and its initialization value - #LB.
8394 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
8395 // var = lb
8396 // integer-type var = lb
8397 // random-access-iterator-type var = lb
8398 // pointer-type var = lb
8399 //
8400 if (!S) {
8401 if (EmitDiags) {
8402 SemaRef.Diag(Loc: DefaultLoc, DiagID: diag::err_omp_loop_not_canonical_init);
8403 }
8404 return true;
8405 }
8406 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(Val: S))
8407 if (!ExprTemp->cleanupsHaveSideEffects())
8408 S = ExprTemp->getSubExpr();
8409
8410 if (!CollapsedLoopVarDecls.empty()) {
8411 ForSubExprChecker FSEC{CollapsedLoopVarDecls};
8412 if (!FSEC.TraverseStmt(S)) {
8413 SourceRange Range = FSEC.getErrRange();
8414 SemaRef.Diag(Loc: Range.getBegin(), DiagID: diag::err_omp_loop_bad_collapse_var)
8415 << Range.getEnd() << 0 << FSEC.getForbiddenVar();
8416 return true;
8417 }
8418 }
8419
8420 InitSrcRange = S->getSourceRange();
8421 if (Expr *E = dyn_cast<Expr>(Val: S))
8422 S = E->IgnoreParens();
8423 if (auto *BO = dyn_cast<BinaryOperator>(Val: S)) {
8424 if (BO->getOpcode() == BO_Assign) {
8425 Expr *LHS = BO->getLHS()->IgnoreParens();
8426 if (auto *DRE = dyn_cast<DeclRefExpr>(Val: LHS)) {
8427 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(Val: DRE->getDecl()))
8428 if (auto *ME = dyn_cast<MemberExpr>(Val: getExprAsWritten(E: CED->getInit())))
8429 return setLCDeclAndLB(NewLCDecl: ME->getMemberDecl(), NewLCRefExpr: ME, NewLB: BO->getRHS(),
8430 EmitDiags);
8431 return setLCDeclAndLB(NewLCDecl: DRE->getDecl(), NewLCRefExpr: DRE, NewLB: BO->getRHS(), EmitDiags);
8432 }
8433 if (auto *ME = dyn_cast<MemberExpr>(Val: LHS)) {
8434 if (ME->isArrow() &&
8435 isa<CXXThisExpr>(Val: ME->getBase()->IgnoreParenImpCasts()))
8436 return setLCDeclAndLB(NewLCDecl: ME->getMemberDecl(), NewLCRefExpr: ME, NewLB: BO->getRHS(),
8437 EmitDiags);
8438 }
8439 }
8440 } else if (auto *DS = dyn_cast<DeclStmt>(Val: S)) {
8441 if (DS->isSingleDecl()) {
8442 if (auto *Var = dyn_cast_or_null<VarDecl>(Val: DS->getSingleDecl())) {
8443 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
8444 // Accept non-canonical init form here but emit ext. warning.
8445 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
8446 SemaRef.Diag(Loc: S->getBeginLoc(),
8447 DiagID: diag::ext_omp_loop_not_canonical_init)
8448 << S->getSourceRange();
8449 return setLCDeclAndLB(
8450 NewLCDecl: Var,
8451 NewLCRefExpr: buildDeclRefExpr(S&: SemaRef, D: Var,
8452 Ty: Var->getType().getNonReferenceType(),
8453 Loc: DS->getBeginLoc()),
8454 NewLB: Var->getInit(), EmitDiags);
8455 }
8456 }
8457 }
8458 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(Val: S)) {
8459 if (CE->getOperator() == OO_Equal) {
8460 Expr *LHS = CE->getArg(Arg: 0);
8461 if (auto *DRE = dyn_cast<DeclRefExpr>(Val: LHS)) {
8462 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(Val: DRE->getDecl()))
8463 if (auto *ME = dyn_cast<MemberExpr>(Val: getExprAsWritten(E: CED->getInit())))
8464 return setLCDeclAndLB(NewLCDecl: ME->getMemberDecl(), NewLCRefExpr: ME, NewLB: BO->getRHS(),
8465 EmitDiags);
8466 return setLCDeclAndLB(NewLCDecl: DRE->getDecl(), NewLCRefExpr: DRE, NewLB: CE->getArg(Arg: 1), EmitDiags);
8467 }
8468 if (auto *ME = dyn_cast<MemberExpr>(Val: LHS)) {
8469 if (ME->isArrow() &&
8470 isa<CXXThisExpr>(Val: ME->getBase()->IgnoreParenImpCasts()))
8471 return setLCDeclAndLB(NewLCDecl: ME->getMemberDecl(), NewLCRefExpr: ME, NewLB: BO->getRHS(),
8472 EmitDiags);
8473 }
8474 }
8475 }
8476
8477 if (dependent() || SemaRef.CurContext->isDependentContext())
8478 return false;
8479 if (EmitDiags) {
8480 SemaRef.Diag(Loc: S->getBeginLoc(), DiagID: diag::err_omp_loop_not_canonical_init)
8481 << S->getSourceRange();
8482 }
8483 return true;
8484}
8485
8486/// Ignore parenthesizes, implicit casts, copy constructor and return the
8487/// variable (which may be the loop variable) if possible.
8488static const ValueDecl *getInitLCDecl(const Expr *E) {
8489 if (!E)
8490 return nullptr;
8491 E = getExprAsWritten(E);
8492 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(Val: E))
8493 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
8494 if ((Ctor->isCopyOrMoveConstructor() ||
8495 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
8496 CE->getNumArgs() > 0 && CE->getArg(Arg: 0) != nullptr)
8497 E = CE->getArg(Arg: 0)->IgnoreParenImpCasts();
8498 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(Val: E)) {
8499 if (const auto *VD = dyn_cast<VarDecl>(Val: DRE->getDecl()))
8500 return getCanonicalDecl(D: VD);
8501 }
8502 if (const auto *ME = dyn_cast_or_null<MemberExpr>(Val: E))
8503 if (ME->isArrow() && isa<CXXThisExpr>(Val: ME->getBase()->IgnoreParenImpCasts()))
8504 return getCanonicalDecl(D: ME->getMemberDecl());
8505 return nullptr;
8506}
8507
8508bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
8509 // Check test-expr for canonical form, save upper-bound UB, flags for
8510 // less/greater and for strict/non-strict comparison.
8511 // OpenMP [2.9] Canonical loop form. Test-expr may be one of the following:
8512 // var relational-op b
8513 // b relational-op var
8514 //
8515 bool IneqCondIsCanonical = SemaRef.getLangOpts().OpenMP >= 50;
8516 if (!S) {
8517 SemaRef.Diag(Loc: DefaultLoc, DiagID: diag::err_omp_loop_not_canonical_cond)
8518 << (IneqCondIsCanonical ? 1 : 0) << LCDecl;
8519 return true;
8520 }
8521 Condition = S;
8522 S = getExprAsWritten(E: S);
8523
8524 if (!CollapsedLoopVarDecls.empty()) {
8525 ForSubExprChecker FSEC{CollapsedLoopVarDecls};
8526 if (!FSEC.TraverseStmt(S)) {
8527 SourceRange Range = FSEC.getErrRange();
8528 SemaRef.Diag(Loc: Range.getBegin(), DiagID: diag::err_omp_loop_bad_collapse_var)
8529 << Range.getEnd() << 1 << FSEC.getForbiddenVar();
8530 return true;
8531 }
8532 }
8533
8534 SourceLocation CondLoc = S->getBeginLoc();
8535 auto &&CheckAndSetCond =
8536 [this, IneqCondIsCanonical](BinaryOperatorKind Opcode, const Expr *LHS,
8537 const Expr *RHS, SourceRange SR,
8538 SourceLocation OpLoc) -> std::optional<bool> {
8539 if (BinaryOperator::isRelationalOp(Opc: Opcode)) {
8540 if (getInitLCDecl(E: LHS) == LCDecl)
8541 return setUB(NewUB: const_cast<Expr *>(RHS),
8542 LessOp: (Opcode == BO_LT || Opcode == BO_LE),
8543 StrictOp: (Opcode == BO_LT || Opcode == BO_GT), SR, SL: OpLoc);
8544 if (getInitLCDecl(E: RHS) == LCDecl)
8545 return setUB(NewUB: const_cast<Expr *>(LHS),
8546 LessOp: (Opcode == BO_GT || Opcode == BO_GE),
8547 StrictOp: (Opcode == BO_LT || Opcode == BO_GT), SR, SL: OpLoc);
8548 } else if (IneqCondIsCanonical && Opcode == BO_NE) {
8549 return setUB(NewUB: const_cast<Expr *>(getInitLCDecl(E: LHS) == LCDecl ? RHS : LHS),
8550 /*LessOp=*/std::nullopt,
8551 /*StrictOp=*/true, SR, SL: OpLoc);
8552 }
8553 return std::nullopt;
8554 };
8555 std::optional<bool> Res;
8556 if (auto *RBO = dyn_cast<CXXRewrittenBinaryOperator>(Val: S)) {
8557 CXXRewrittenBinaryOperator::DecomposedForm DF = RBO->getDecomposedForm();
8558 Res = CheckAndSetCond(DF.Opcode, DF.LHS, DF.RHS, RBO->getSourceRange(),
8559 RBO->getOperatorLoc());
8560 } else if (auto *BO = dyn_cast<BinaryOperator>(Val: S)) {
8561 Res = CheckAndSetCond(BO->getOpcode(), BO->getLHS(), BO->getRHS(),
8562 BO->getSourceRange(), BO->getOperatorLoc());
8563 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(Val: S)) {
8564 if (CE->getNumArgs() == 2) {
8565 Res = CheckAndSetCond(
8566 BinaryOperator::getOverloadedOpcode(OO: CE->getOperator()), CE->getArg(Arg: 0),
8567 CE->getArg(Arg: 1), CE->getSourceRange(), CE->getOperatorLoc());
8568 }
8569 }
8570 if (Res)
8571 return *Res;
8572 if (dependent() || SemaRef.CurContext->isDependentContext())
8573 return false;
8574 SemaRef.Diag(Loc: CondLoc, DiagID: diag::err_omp_loop_not_canonical_cond)
8575 << (IneqCondIsCanonical ? 1 : 0) << S->getSourceRange() << LCDecl;
8576 return true;
8577}
8578
8579bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
8580 // RHS of canonical loop form increment can be:
8581 // var + incr
8582 // incr + var
8583 // var - incr
8584 //
8585 RHS = RHS->IgnoreParenImpCasts();
8586 if (auto *BO = dyn_cast<BinaryOperator>(Val: RHS)) {
8587 if (BO->isAdditiveOp()) {
8588 bool IsAdd = BO->getOpcode() == BO_Add;
8589 if (getInitLCDecl(E: BO->getLHS()) == LCDecl)
8590 return setStep(NewStep: BO->getRHS(), Subtract: !IsAdd);
8591 if (IsAdd && getInitLCDecl(E: BO->getRHS()) == LCDecl)
8592 return setStep(NewStep: BO->getLHS(), /*Subtract=*/false);
8593 }
8594 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(Val: RHS)) {
8595 bool IsAdd = CE->getOperator() == OO_Plus;
8596 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
8597 if (getInitLCDecl(E: CE->getArg(Arg: 0)) == LCDecl)
8598 return setStep(NewStep: CE->getArg(Arg: 1), Subtract: !IsAdd);
8599 if (IsAdd && getInitLCDecl(E: CE->getArg(Arg: 1)) == LCDecl)
8600 return setStep(NewStep: CE->getArg(Arg: 0), /*Subtract=*/false);
8601 }
8602 }
8603 if (dependent() || SemaRef.CurContext->isDependentContext())
8604 return false;
8605 SemaRef.Diag(Loc: RHS->getBeginLoc(), DiagID: diag::err_omp_loop_not_canonical_incr)
8606 << RHS->getSourceRange() << LCDecl;
8607 return true;
8608}
8609
8610bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
8611 // Check incr-expr for canonical loop form and return true if it
8612 // does not conform.
8613 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
8614 // ++var
8615 // var++
8616 // --var
8617 // var--
8618 // var += incr
8619 // var -= incr
8620 // var = var + incr
8621 // var = incr + var
8622 // var = var - incr
8623 //
8624 if (!S) {
8625 SemaRef.Diag(Loc: DefaultLoc, DiagID: diag::err_omp_loop_not_canonical_incr) << LCDecl;
8626 return true;
8627 }
8628 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(Val: S))
8629 if (!ExprTemp->cleanupsHaveSideEffects())
8630 S = ExprTemp->getSubExpr();
8631
8632 if (!CollapsedLoopVarDecls.empty()) {
8633 ForSubExprChecker FSEC{CollapsedLoopVarDecls};
8634 if (!FSEC.TraverseStmt(S)) {
8635 SourceRange Range = FSEC.getErrRange();
8636 SemaRef.Diag(Loc: Range.getBegin(), DiagID: diag::err_omp_loop_bad_collapse_var)
8637 << Range.getEnd() << 2 << FSEC.getForbiddenVar();
8638 return true;
8639 }
8640 }
8641
8642 IncrementSrcRange = S->getSourceRange();
8643 S = S->IgnoreParens();
8644 if (auto *UO = dyn_cast<UnaryOperator>(Val: S)) {
8645 if (UO->isIncrementDecrementOp() &&
8646 getInitLCDecl(E: UO->getSubExpr()) == LCDecl)
8647 return setStep(NewStep: SemaRef
8648 .ActOnIntegerConstant(Loc: UO->getBeginLoc(),
8649 Val: (UO->isDecrementOp() ? -1 : 1))
8650 .get(),
8651 /*Subtract=*/false);
8652 } else if (auto *BO = dyn_cast<BinaryOperator>(Val: S)) {
8653 switch (BO->getOpcode()) {
8654 case BO_AddAssign:
8655 case BO_SubAssign:
8656 if (getInitLCDecl(E: BO->getLHS()) == LCDecl)
8657 return setStep(NewStep: BO->getRHS(), Subtract: BO->getOpcode() == BO_SubAssign);
8658 break;
8659 case BO_Assign:
8660 if (getInitLCDecl(E: BO->getLHS()) == LCDecl)
8661 return checkAndSetIncRHS(RHS: BO->getRHS());
8662 break;
8663 default:
8664 break;
8665 }
8666 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(Val: S)) {
8667 switch (CE->getOperator()) {
8668 case OO_PlusPlus:
8669 case OO_MinusMinus:
8670 if (getInitLCDecl(E: CE->getArg(Arg: 0)) == LCDecl)
8671 return setStep(NewStep: SemaRef
8672 .ActOnIntegerConstant(
8673 Loc: CE->getBeginLoc(),
8674 Val: ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
8675 .get(),
8676 /*Subtract=*/false);
8677 break;
8678 case OO_PlusEqual:
8679 case OO_MinusEqual:
8680 if (getInitLCDecl(E: CE->getArg(Arg: 0)) == LCDecl)
8681 return setStep(NewStep: CE->getArg(Arg: 1), Subtract: CE->getOperator() == OO_MinusEqual);
8682 break;
8683 case OO_Equal:
8684 if (getInitLCDecl(E: CE->getArg(Arg: 0)) == LCDecl)
8685 return checkAndSetIncRHS(RHS: CE->getArg(Arg: 1));
8686 break;
8687 default:
8688 break;
8689 }
8690 }
8691 if (dependent() || SemaRef.CurContext->isDependentContext())
8692 return false;
8693 SemaRef.Diag(Loc: S->getBeginLoc(), DiagID: diag::err_omp_loop_not_canonical_incr)
8694 << S->getSourceRange() << LCDecl;
8695 return true;
8696}
8697
8698static ExprResult
8699tryBuildCapture(Sema &SemaRef, Expr *Capture,
8700 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
8701 StringRef Name = ".capture_expr.") {
8702 if (SemaRef.CurContext->isDependentContext() || Capture->containsErrors())
8703 return Capture;
8704 if (Capture->isEvaluatable(Ctx: SemaRef.Context, AllowSideEffects: Expr::SE_AllowSideEffects))
8705 return SemaRef.PerformImplicitConversion(From: Capture->IgnoreImpCasts(),
8706 ToType: Capture->getType(),
8707 Action: AssignmentAction::Converting,
8708 /*AllowExplicit=*/true);
8709 auto I = Captures.find(Key: Capture);
8710 if (I != Captures.end())
8711 return buildCapture(S&: SemaRef, CaptureExpr: Capture, Ref&: I->second, Name);
8712 DeclRefExpr *Ref = nullptr;
8713 ExprResult Res = buildCapture(S&: SemaRef, CaptureExpr: Capture, Ref, Name);
8714 Captures[Capture] = Ref;
8715 return Res;
8716}
8717
8718/// Calculate number of iterations, transforming to unsigned, if number of
8719/// iterations may be larger than the original type.
8720static Expr *
8721calculateNumIters(Sema &SemaRef, Scope *S, SourceLocation DefaultLoc,
8722 Expr *Lower, Expr *Upper, Expr *Step, QualType LCTy,
8723 bool TestIsStrictOp, bool RoundToStep,
8724 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
8725 std::optional<unsigned> InitDependOnLC,
8726 std::optional<unsigned> CondDependOnLC) {
8727 ExprResult NewStep = tryBuildCapture(SemaRef, Capture: Step, Captures, Name: ".new_step");
8728 if (!NewStep.isUsable())
8729 return nullptr;
8730 llvm::APSInt LRes, SRes;
8731 bool IsLowerConst = false, IsStepConst = false;
8732 if (std::optional<llvm::APSInt> Res =
8733 Lower->getIntegerConstantExpr(Ctx: SemaRef.Context)) {
8734 LRes = *Res;
8735 IsLowerConst = true;
8736 }
8737 if (std::optional<llvm::APSInt> Res =
8738 Step->getIntegerConstantExpr(Ctx: SemaRef.Context)) {
8739 SRes = *Res;
8740 IsStepConst = true;
8741 }
8742 bool NoNeedToConvert = IsLowerConst && !RoundToStep &&
8743 ((!TestIsStrictOp && LRes.isNonNegative()) ||
8744 (TestIsStrictOp && LRes.isStrictlyPositive()));
8745 bool NeedToReorganize = false;
8746 // Check if any subexpressions in Lower -Step [+ 1] lead to overflow.
8747 if (!NoNeedToConvert && IsLowerConst &&
8748 (TestIsStrictOp || (RoundToStep && IsStepConst))) {
8749 NoNeedToConvert = true;
8750 if (RoundToStep) {
8751 unsigned BW = LRes.getBitWidth() > SRes.getBitWidth()
8752 ? LRes.getBitWidth()
8753 : SRes.getBitWidth();
8754 LRes = LRes.extend(width: BW + 1);
8755 LRes.setIsSigned(true);
8756 SRes = SRes.extend(width: BW + 1);
8757 SRes.setIsSigned(true);
8758 LRes -= SRes;
8759 NoNeedToConvert = LRes.trunc(width: BW).extend(width: BW + 1) == LRes;
8760 LRes = LRes.trunc(width: BW);
8761 }
8762 if (TestIsStrictOp) {
8763 unsigned BW = LRes.getBitWidth();
8764 LRes = LRes.extend(width: BW + 1);
8765 LRes.setIsSigned(true);
8766 ++LRes;
8767 NoNeedToConvert =
8768 NoNeedToConvert && LRes.trunc(width: BW).extend(width: BW + 1) == LRes;
8769 // truncate to the original bitwidth.
8770 LRes = LRes.trunc(width: BW);
8771 }
8772 NeedToReorganize = NoNeedToConvert;
8773 }
8774 llvm::APSInt URes;
8775 bool IsUpperConst = false;
8776 if (std::optional<llvm::APSInt> Res =
8777 Upper->getIntegerConstantExpr(Ctx: SemaRef.Context)) {
8778 URes = *Res;
8779 IsUpperConst = true;
8780 }
8781 if (NoNeedToConvert && IsLowerConst && IsUpperConst &&
8782 (!RoundToStep || IsStepConst)) {
8783 unsigned BW = LRes.getBitWidth() > URes.getBitWidth() ? LRes.getBitWidth()
8784 : URes.getBitWidth();
8785 LRes = LRes.extend(width: BW + 1);
8786 LRes.setIsSigned(true);
8787 URes = URes.extend(width: BW + 1);
8788 URes.setIsSigned(true);
8789 URes -= LRes;
8790 NoNeedToConvert = URes.trunc(width: BW).extend(width: BW + 1) == URes;
8791 NeedToReorganize = NoNeedToConvert;
8792 }
8793 // If the boundaries are not constant or (Lower - Step [+ 1]) is not constant
8794 // or less than zero (Upper - (Lower - Step [+ 1]) may overflow) - promote to
8795 // unsigned.
8796 if ((!NoNeedToConvert || (LRes.isNegative() && !IsUpperConst)) &&
8797 !LCTy->isDependentType() && LCTy->isIntegerType()) {
8798 QualType LowerTy = Lower->getType();
8799 QualType UpperTy = Upper->getType();
8800 uint64_t LowerSize = SemaRef.Context.getTypeSize(T: LowerTy);
8801 uint64_t UpperSize = SemaRef.Context.getTypeSize(T: UpperTy);
8802 if ((LowerSize <= UpperSize && UpperTy->hasSignedIntegerRepresentation()) ||
8803 (LowerSize > UpperSize && LowerTy->hasSignedIntegerRepresentation())) {
8804 QualType CastType = SemaRef.Context.getIntTypeForBitwidth(
8805 DestWidth: LowerSize > UpperSize ? LowerSize : UpperSize, /*Signed=*/0);
8806 Upper =
8807 SemaRef
8808 .PerformImplicitConversion(
8809 From: SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Upper).get(),
8810 ToType: CastType, Action: AssignmentAction::Converting)
8811 .get();
8812 Lower = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Lower).get();
8813 NewStep = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: NewStep.get());
8814 }
8815 }
8816 if (!Lower || !Upper || NewStep.isInvalid())
8817 return nullptr;
8818
8819 ExprResult Diff;
8820
8821 // For triangular loops, use already computed Upper and Lower bounds to
8822 // calculate the number of iterations: Upper - Lower + 1.
8823 if (TestIsStrictOp && InitDependOnLC.has_value() &&
8824 !CondDependOnLC.has_value()) {
8825 Diff = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Sub, LHSExpr: Upper, RHSExpr: Lower);
8826 if (!Diff.isUsable())
8827 return nullptr;
8828 Diff =
8829 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Add, LHSExpr: Diff.get(),
8830 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: DefaultLoc, Val: 1).get());
8831 if (!Diff.isUsable())
8832 return nullptr;
8833 return Diff.get();
8834 }
8835
8836 // If need to reorganize, then calculate the form as Upper - (Lower - Step [+
8837 // 1]).
8838 if (NeedToReorganize) {
8839 Diff = Lower;
8840
8841 if (RoundToStep) {
8842 // Lower - Step
8843 Diff =
8844 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Sub, LHSExpr: Diff.get(), RHSExpr: NewStep.get());
8845 if (!Diff.isUsable())
8846 return nullptr;
8847 }
8848
8849 // Lower - Step [+ 1]
8850 if (TestIsStrictOp)
8851 Diff = SemaRef.BuildBinOp(
8852 S, OpLoc: DefaultLoc, Opc: BO_Add, LHSExpr: Diff.get(),
8853 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get());
8854 if (!Diff.isUsable())
8855 return nullptr;
8856
8857 Diff = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Diff.get());
8858 if (!Diff.isUsable())
8859 return nullptr;
8860
8861 // Upper - (Lower - Step [+ 1]).
8862 Diff = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Sub, LHSExpr: Upper, RHSExpr: Diff.get());
8863 if (!Diff.isUsable())
8864 return nullptr;
8865 } else {
8866 Diff = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Sub, LHSExpr: Upper, RHSExpr: Lower);
8867
8868 if (!Diff.isUsable() && LCTy->getAsCXXRecordDecl()) {
8869 // BuildBinOp already emitted error, this one is to point user to upper
8870 // and lower bound, and to tell what is passed to 'operator-'.
8871 SemaRef.Diag(Loc: Upper->getBeginLoc(), DiagID: diag::err_omp_loop_diff_cxx)
8872 << Upper->getSourceRange() << Lower->getSourceRange();
8873 return nullptr;
8874 }
8875
8876 if (!Diff.isUsable())
8877 return nullptr;
8878
8879 // Upper - Lower [- 1]
8880 if (TestIsStrictOp)
8881 Diff = SemaRef.BuildBinOp(
8882 S, OpLoc: DefaultLoc, Opc: BO_Sub, LHSExpr: Diff.get(),
8883 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get());
8884 if (!Diff.isUsable())
8885 return nullptr;
8886
8887 if (RoundToStep) {
8888 // Upper - Lower [- 1] + Step
8889 Diff =
8890 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Add, LHSExpr: Diff.get(), RHSExpr: NewStep.get());
8891 if (!Diff.isUsable())
8892 return nullptr;
8893 }
8894 }
8895
8896 // Parentheses (for dumping/debugging purposes only).
8897 Diff = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Diff.get());
8898 if (!Diff.isUsable())
8899 return nullptr;
8900
8901 // (Upper - Lower [- 1] + Step) / Step or (Upper - Lower) / Step
8902 Diff = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Div, LHSExpr: Diff.get(), RHSExpr: NewStep.get());
8903 if (!Diff.isUsable())
8904 return nullptr;
8905
8906 return Diff.get();
8907}
8908
8909/// Build the expression to calculate the number of iterations.
8910Expr *OpenMPIterationSpaceChecker::buildNumIterations(
8911 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType,
8912 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
8913 QualType VarType = LCDecl->getType().getNonReferenceType();
8914 if (!VarType->isIntegerType() && !VarType->isPointerType() &&
8915 !SemaRef.getLangOpts().CPlusPlus)
8916 return nullptr;
8917 Expr *LBVal = LB;
8918 Expr *UBVal = UB;
8919 // OuterVar = (LB = TestIsLessOp.getValue() ? min(LB(MinVal), LB(MaxVal)) :
8920 // max(LB(MinVal), LB(MaxVal)))
8921 if (InitDependOnLC) {
8922 const LoopIterationSpace &IS = ResultIterSpaces[*InitDependOnLC - 1];
8923 if (!IS.MinValue || !IS.MaxValue)
8924 return nullptr;
8925 // OuterVar = Min
8926 ExprResult MinValue =
8927 SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: IS.MinValue);
8928 if (!MinValue.isUsable())
8929 return nullptr;
8930
8931 ExprResult LBMinVal = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Assign,
8932 LHSExpr: IS.CounterVar, RHSExpr: MinValue.get());
8933 if (!LBMinVal.isUsable())
8934 return nullptr;
8935 // OuterVar = Min, LBVal
8936 LBMinVal =
8937 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Comma, LHSExpr: LBMinVal.get(), RHSExpr: LBVal);
8938 if (!LBMinVal.isUsable())
8939 return nullptr;
8940 // (OuterVar = Min, LBVal)
8941 LBMinVal = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: LBMinVal.get());
8942 if (!LBMinVal.isUsable())
8943 return nullptr;
8944
8945 // OuterVar = Max
8946 ExprResult MaxValue =
8947 SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: IS.MaxValue);
8948 if (!MaxValue.isUsable())
8949 return nullptr;
8950
8951 ExprResult LBMaxVal = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Assign,
8952 LHSExpr: IS.CounterVar, RHSExpr: MaxValue.get());
8953 if (!LBMaxVal.isUsable())
8954 return nullptr;
8955 // OuterVar = Max, LBVal
8956 LBMaxVal =
8957 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Comma, LHSExpr: LBMaxVal.get(), RHSExpr: LBVal);
8958 if (!LBMaxVal.isUsable())
8959 return nullptr;
8960 // (OuterVar = Max, LBVal)
8961 LBMaxVal = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: LBMaxVal.get());
8962 if (!LBMaxVal.isUsable())
8963 return nullptr;
8964
8965 Expr *LBMin =
8966 tryBuildCapture(SemaRef, Capture: LBMinVal.get(), Captures, Name: ".lb_min").get();
8967 Expr *LBMax =
8968 tryBuildCapture(SemaRef, Capture: LBMaxVal.get(), Captures, Name: ".lb_max").get();
8969 if (!LBMin || !LBMax)
8970 return nullptr;
8971 // LB(MinVal) < LB(MaxVal)
8972 ExprResult MinLessMaxRes =
8973 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_LT, LHSExpr: LBMin, RHSExpr: LBMax);
8974 if (!MinLessMaxRes.isUsable())
8975 return nullptr;
8976 Expr *MinLessMax =
8977 tryBuildCapture(SemaRef, Capture: MinLessMaxRes.get(), Captures, Name: ".min_less_max")
8978 .get();
8979 if (!MinLessMax)
8980 return nullptr;
8981 if (*TestIsLessOp) {
8982 // LB(MinVal) < LB(MaxVal) ? LB(MinVal) : LB(MaxVal) - min(LB(MinVal),
8983 // LB(MaxVal))
8984 ExprResult MinLB = SemaRef.ActOnConditionalOp(QuestionLoc: DefaultLoc, ColonLoc: DefaultLoc,
8985 CondExpr: MinLessMax, LHSExpr: LBMin, RHSExpr: LBMax);
8986 if (!MinLB.isUsable())
8987 return nullptr;
8988 LBVal = MinLB.get();
8989 } else {
8990 // LB(MinVal) < LB(MaxVal) ? LB(MaxVal) : LB(MinVal) - max(LB(MinVal),
8991 // LB(MaxVal))
8992 ExprResult MaxLB = SemaRef.ActOnConditionalOp(QuestionLoc: DefaultLoc, ColonLoc: DefaultLoc,
8993 CondExpr: MinLessMax, LHSExpr: LBMax, RHSExpr: LBMin);
8994 if (!MaxLB.isUsable())
8995 return nullptr;
8996 LBVal = MaxLB.get();
8997 }
8998 // OuterVar = LB
8999 LBMinVal =
9000 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Assign, LHSExpr: IS.CounterVar, RHSExpr: LBVal);
9001 if (!LBMinVal.isUsable())
9002 return nullptr;
9003 LBVal = LBMinVal.get();
9004 }
9005 // UB = TestIsLessOp.getValue() ? max(UB(MinVal), UB(MaxVal)) :
9006 // min(UB(MinVal), UB(MaxVal))
9007 if (CondDependOnLC) {
9008 const LoopIterationSpace &IS = ResultIterSpaces[*CondDependOnLC - 1];
9009 if (!IS.MinValue || !IS.MaxValue)
9010 return nullptr;
9011 // OuterVar = Min
9012 ExprResult MinValue =
9013 SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: IS.MinValue);
9014 if (!MinValue.isUsable())
9015 return nullptr;
9016
9017 ExprResult UBMinVal = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Assign,
9018 LHSExpr: IS.CounterVar, RHSExpr: MinValue.get());
9019 if (!UBMinVal.isUsable())
9020 return nullptr;
9021 // OuterVar = Min, UBVal
9022 UBMinVal =
9023 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Comma, LHSExpr: UBMinVal.get(), RHSExpr: UBVal);
9024 if (!UBMinVal.isUsable())
9025 return nullptr;
9026 // (OuterVar = Min, UBVal)
9027 UBMinVal = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: UBMinVal.get());
9028 if (!UBMinVal.isUsable())
9029 return nullptr;
9030
9031 // OuterVar = Max
9032 ExprResult MaxValue =
9033 SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: IS.MaxValue);
9034 if (!MaxValue.isUsable())
9035 return nullptr;
9036
9037 ExprResult UBMaxVal = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Assign,
9038 LHSExpr: IS.CounterVar, RHSExpr: MaxValue.get());
9039 if (!UBMaxVal.isUsable())
9040 return nullptr;
9041 // OuterVar = Max, UBVal
9042 UBMaxVal =
9043 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Comma, LHSExpr: UBMaxVal.get(), RHSExpr: UBVal);
9044 if (!UBMaxVal.isUsable())
9045 return nullptr;
9046 // (OuterVar = Max, UBVal)
9047 UBMaxVal = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: UBMaxVal.get());
9048 if (!UBMaxVal.isUsable())
9049 return nullptr;
9050
9051 Expr *UBMin =
9052 tryBuildCapture(SemaRef, Capture: UBMinVal.get(), Captures, Name: ".ub_min").get();
9053 Expr *UBMax =
9054 tryBuildCapture(SemaRef, Capture: UBMaxVal.get(), Captures, Name: ".ub_max").get();
9055 if (!UBMin || !UBMax)
9056 return nullptr;
9057 // UB(MinVal) > UB(MaxVal)
9058 ExprResult MinGreaterMaxRes =
9059 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_GT, LHSExpr: UBMin, RHSExpr: UBMax);
9060 if (!MinGreaterMaxRes.isUsable())
9061 return nullptr;
9062 Expr *MinGreaterMax = tryBuildCapture(SemaRef, Capture: MinGreaterMaxRes.get(),
9063 Captures, Name: ".min_greater_max")
9064 .get();
9065 if (!MinGreaterMax)
9066 return nullptr;
9067 if (*TestIsLessOp) {
9068 // UB(MinVal) > UB(MaxVal) ? UB(MinVal) : UB(MaxVal) - max(UB(MinVal),
9069 // UB(MaxVal))
9070 ExprResult MaxUB = SemaRef.ActOnConditionalOp(
9071 QuestionLoc: DefaultLoc, ColonLoc: DefaultLoc, CondExpr: MinGreaterMax, LHSExpr: UBMin, RHSExpr: UBMax);
9072 if (!MaxUB.isUsable())
9073 return nullptr;
9074 UBVal = MaxUB.get();
9075 } else {
9076 // UB(MinVal) > UB(MaxVal) ? UB(MaxVal) : UB(MinVal) - min(UB(MinVal),
9077 // UB(MaxVal))
9078 ExprResult MinUB = SemaRef.ActOnConditionalOp(
9079 QuestionLoc: DefaultLoc, ColonLoc: DefaultLoc, CondExpr: MinGreaterMax, LHSExpr: UBMax, RHSExpr: UBMin);
9080 if (!MinUB.isUsable())
9081 return nullptr;
9082 UBVal = MinUB.get();
9083 }
9084 }
9085 Expr *UBExpr = *TestIsLessOp ? UBVal : LBVal;
9086 Expr *LBExpr = *TestIsLessOp ? LBVal : UBVal;
9087 Expr *Upper = tryBuildCapture(SemaRef, Capture: UBExpr, Captures, Name: ".upper").get();
9088 Expr *Lower = tryBuildCapture(SemaRef, Capture: LBExpr, Captures, Name: ".lower").get();
9089 if (!Upper || !Lower)
9090 return nullptr;
9091
9092 ExprResult Diff = calculateNumIters(
9093 SemaRef, S, DefaultLoc, Lower, Upper, Step, LCTy: VarType, TestIsStrictOp,
9094 /*RoundToStep=*/true, Captures, InitDependOnLC, CondDependOnLC);
9095 if (!Diff.isUsable())
9096 return nullptr;
9097
9098 // OpenMP runtime requires 32-bit or 64-bit loop variables.
9099 QualType Type = Diff.get()->getType();
9100 ASTContext &C = SemaRef.Context;
9101 bool UseVarType = VarType->hasIntegerRepresentation() &&
9102 C.getTypeSize(T: Type) > C.getTypeSize(T: VarType);
9103 if (!Type->isIntegerType() || UseVarType) {
9104 unsigned NewSize =
9105 UseVarType ? C.getTypeSize(T: VarType) : C.getTypeSize(T: Type);
9106 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
9107 : Type->hasSignedIntegerRepresentation();
9108 Type = C.getIntTypeForBitwidth(DestWidth: NewSize, Signed: IsSigned);
9109 if (!SemaRef.Context.hasSameType(T1: Diff.get()->getType(), T2: Type)) {
9110 Diff = SemaRef.PerformImplicitConversion(From: Diff.get(), ToType: Type,
9111 Action: AssignmentAction::Converting,
9112 /*AllowExplicit=*/true);
9113 if (!Diff.isUsable())
9114 return nullptr;
9115 }
9116 }
9117 if (LimitedType) {
9118 unsigned NewSize = (C.getTypeSize(T: Type) > 32) ? 64 : 32;
9119 if (NewSize != C.getTypeSize(T: Type)) {
9120 if (NewSize < C.getTypeSize(T: Type)) {
9121 assert(NewSize == 64 && "incorrect loop var size");
9122 SemaRef.Diag(Loc: DefaultLoc, DiagID: diag::warn_omp_loop_64_bit_var)
9123 << InitSrcRange << ConditionSrcRange;
9124 }
9125 QualType NewType = C.getIntTypeForBitwidth(
9126 DestWidth: NewSize, Signed: Type->hasSignedIntegerRepresentation() ||
9127 C.getTypeSize(T: Type) < NewSize);
9128 if (!SemaRef.Context.hasSameType(T1: Diff.get()->getType(), T2: NewType)) {
9129 Diff = SemaRef.PerformImplicitConversion(From: Diff.get(), ToType: NewType,
9130 Action: AssignmentAction::Converting,
9131 /*AllowExplicit=*/true);
9132 if (!Diff.isUsable())
9133 return nullptr;
9134 }
9135 }
9136 }
9137
9138 return Diff.get();
9139}
9140
9141std::pair<Expr *, Expr *> OpenMPIterationSpaceChecker::buildMinMaxValues(
9142 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
9143 // Do not build for iterators, they cannot be used in non-rectangular loop
9144 // nests.
9145 if (LCDecl->getType()->isRecordType())
9146 return std::make_pair(x: nullptr, y: nullptr);
9147 // If we subtract, the min is in the condition, otherwise the min is in the
9148 // init value.
9149 Expr *MinExpr = nullptr;
9150 Expr *MaxExpr = nullptr;
9151 Expr *LBExpr = *TestIsLessOp ? LB : UB;
9152 Expr *UBExpr = *TestIsLessOp ? UB : LB;
9153 bool LBNonRect =
9154 *TestIsLessOp ? InitDependOnLC.has_value() : CondDependOnLC.has_value();
9155 bool UBNonRect =
9156 *TestIsLessOp ? CondDependOnLC.has_value() : InitDependOnLC.has_value();
9157 Expr *Lower =
9158 LBNonRect ? LBExpr : tryBuildCapture(SemaRef, Capture: LBExpr, Captures).get();
9159 Expr *Upper =
9160 UBNonRect ? UBExpr : tryBuildCapture(SemaRef, Capture: UBExpr, Captures).get();
9161 if (!Upper || !Lower)
9162 return std::make_pair(x: nullptr, y: nullptr);
9163
9164 if (*TestIsLessOp)
9165 MinExpr = Lower;
9166 else
9167 MaxExpr = Upper;
9168
9169 // Build minimum/maximum value based on number of iterations.
9170 QualType VarType = LCDecl->getType().getNonReferenceType();
9171
9172 ExprResult Diff = calculateNumIters(
9173 SemaRef, S, DefaultLoc, Lower, Upper, Step, LCTy: VarType, TestIsStrictOp,
9174 /*RoundToStep=*/false, Captures, InitDependOnLC, CondDependOnLC);
9175
9176 if (!Diff.isUsable())
9177 return std::make_pair(x: nullptr, y: nullptr);
9178
9179 // ((Upper - Lower [- 1]) / Step) * Step
9180 // Parentheses (for dumping/debugging purposes only).
9181 Diff = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Diff.get());
9182 if (!Diff.isUsable())
9183 return std::make_pair(x: nullptr, y: nullptr);
9184
9185 ExprResult NewStep = tryBuildCapture(SemaRef, Capture: Step, Captures, Name: ".new_step");
9186 if (!NewStep.isUsable())
9187 return std::make_pair(x: nullptr, y: nullptr);
9188 Diff = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Mul, LHSExpr: Diff.get(), RHSExpr: NewStep.get());
9189 if (!Diff.isUsable())
9190 return std::make_pair(x: nullptr, y: nullptr);
9191
9192 // Parentheses (for dumping/debugging purposes only).
9193 Diff = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Diff.get());
9194 if (!Diff.isUsable())
9195 return std::make_pair(x: nullptr, y: nullptr);
9196
9197 // Convert to the ptrdiff_t, if original type is pointer.
9198 if (VarType->isAnyPointerType() &&
9199 !SemaRef.Context.hasSameType(
9200 T1: Diff.get()->getType(),
9201 T2: SemaRef.Context.getUnsignedPointerDiffType())) {
9202 Diff = SemaRef.PerformImplicitConversion(
9203 From: Diff.get(), ToType: SemaRef.Context.getUnsignedPointerDiffType(),
9204 Action: AssignmentAction::Converting, /*AllowExplicit=*/true);
9205 }
9206 if (!Diff.isUsable())
9207 return std::make_pair(x: nullptr, y: nullptr);
9208
9209 if (*TestIsLessOp) {
9210 // MinExpr = Lower;
9211 // MaxExpr = Lower + (((Upper - Lower [- 1]) / Step) * Step)
9212 Diff = SemaRef.BuildBinOp(
9213 S, OpLoc: DefaultLoc, Opc: BO_Add,
9214 LHSExpr: SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Lower).get(),
9215 RHSExpr: Diff.get());
9216 if (!Diff.isUsable())
9217 return std::make_pair(x: nullptr, y: nullptr);
9218 } else {
9219 // MaxExpr = Upper;
9220 // MinExpr = Upper - (((Upper - Lower [- 1]) / Step) * Step)
9221 Diff = SemaRef.BuildBinOp(
9222 S, OpLoc: DefaultLoc, Opc: BO_Sub,
9223 LHSExpr: SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Upper).get(),
9224 RHSExpr: Diff.get());
9225 if (!Diff.isUsable())
9226 return std::make_pair(x: nullptr, y: nullptr);
9227 }
9228
9229 // Convert to the original type.
9230 if (SemaRef.Context.hasSameType(T1: Diff.get()->getType(), T2: VarType))
9231 Diff = SemaRef.PerformImplicitConversion(From: Diff.get(), ToType: VarType,
9232 Action: AssignmentAction::Converting,
9233 /*AllowExplicit=*/true);
9234 if (!Diff.isUsable())
9235 return std::make_pair(x: nullptr, y: nullptr);
9236
9237 Sema::TentativeAnalysisScope Trap(SemaRef);
9238 Diff = SemaRef.ActOnFinishFullExpr(Expr: Diff.get(), /*DiscardedValue=*/false);
9239 if (!Diff.isUsable())
9240 return std::make_pair(x: nullptr, y: nullptr);
9241
9242 if (*TestIsLessOp)
9243 MaxExpr = Diff.get();
9244 else
9245 MinExpr = Diff.get();
9246
9247 return std::make_pair(x&: MinExpr, y&: MaxExpr);
9248}
9249
9250Expr *OpenMPIterationSpaceChecker::buildFinalCondition(Scope *S) const {
9251 if (InitDependOnLC || CondDependOnLC)
9252 return Condition;
9253 return nullptr;
9254}
9255
9256Expr *OpenMPIterationSpaceChecker::buildPreCond(
9257 Scope *S, Expr *Cond,
9258 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
9259 // Do not build a precondition when the condition/initialization is dependent
9260 // to prevent pessimistic early loop exit.
9261 // TODO: this can be improved by calculating min/max values but not sure that
9262 // it will be very effective.
9263 if (CondDependOnLC || InitDependOnLC)
9264 return SemaRef
9265 .PerformImplicitConversion(
9266 From: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get(),
9267 ToType: SemaRef.Context.BoolTy, /*Action=*/AssignmentAction::Casting,
9268 /*AllowExplicit=*/true)
9269 .get();
9270
9271 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
9272 Sema::TentativeAnalysisScope Trap(SemaRef);
9273
9274 ExprResult NewLB = tryBuildCapture(SemaRef, Capture: LB, Captures);
9275 ExprResult NewUB = tryBuildCapture(SemaRef, Capture: UB, Captures);
9276 if (!NewLB.isUsable() || !NewUB.isUsable())
9277 return nullptr;
9278
9279 ExprResult CondExpr =
9280 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc,
9281 Opc: *TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
9282 : (TestIsStrictOp ? BO_GT : BO_GE),
9283 LHSExpr: NewLB.get(), RHSExpr: NewUB.get());
9284 if (CondExpr.isUsable()) {
9285 if (!SemaRef.Context.hasSameUnqualifiedType(T1: CondExpr.get()->getType(),
9286 T2: SemaRef.Context.BoolTy))
9287 CondExpr = SemaRef.PerformImplicitConversion(
9288 From: CondExpr.get(), ToType: SemaRef.Context.BoolTy,
9289 /*Action=*/AssignmentAction::Casting,
9290 /*AllowExplicit=*/true);
9291 }
9292
9293 // Otherwise use original loop condition and evaluate it in runtime.
9294 return CondExpr.isUsable() ? CondExpr.get() : Cond;
9295}
9296
9297/// Build reference expression to the counter be used for codegen.
9298DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
9299 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
9300 DSAStackTy &DSA) const {
9301 auto *VD = dyn_cast<VarDecl>(Val: LCDecl);
9302 if (!VD) {
9303 VD = SemaRef.OpenMP().isOpenMPCapturedDecl(D: LCDecl);
9304 DeclRefExpr *Ref = buildDeclRefExpr(
9305 S&: SemaRef, D: VD, Ty: VD->getType().getNonReferenceType(), Loc: DefaultLoc);
9306 const DSAStackTy::DSAVarData Data =
9307 DSA.getTopDSA(D: LCDecl, /*FromParent=*/false);
9308 // If the loop control decl is explicitly marked as private, do not mark it
9309 // as captured again.
9310 if (!isOpenMPPrivate(Kind: Data.CKind) || !Data.RefExpr)
9311 Captures.insert(KV: std::make_pair(x: LCRef, y&: Ref));
9312 return Ref;
9313 }
9314 return cast<DeclRefExpr>(Val: LCRef);
9315}
9316
9317Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
9318 if (LCDecl && !LCDecl->isInvalidDecl()) {
9319 QualType Type = LCDecl->getType().getNonReferenceType();
9320 VarDecl *PrivateVar = buildVarDecl(
9321 SemaRef, Loc: DefaultLoc, Type, Name: LCDecl->getName(),
9322 Attrs: LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
9323 OrigRef: isa<VarDecl>(Val: LCDecl)
9324 ? buildDeclRefExpr(S&: SemaRef, D: cast<VarDecl>(Val: LCDecl), Ty: Type, Loc: DefaultLoc)
9325 : nullptr);
9326 if (PrivateVar->isInvalidDecl())
9327 return nullptr;
9328 return buildDeclRefExpr(S&: SemaRef, D: PrivateVar, Ty: Type, Loc: DefaultLoc);
9329 }
9330 return nullptr;
9331}
9332
9333/// Build initialization of the counter to be used for codegen.
9334Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
9335
9336/// Build step of the counter be used for codegen.
9337Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
9338
9339Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
9340 Scope *S, Expr *Counter,
9341 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
9342 Expr *Inc, OverloadedOperatorKind OOK) {
9343 Expr *Cnt = SemaRef.DefaultLvalueConversion(E: Counter).get();
9344 if (!Cnt)
9345 return nullptr;
9346 if (Inc) {
9347 assert((OOK == OO_Plus || OOK == OO_Minus) &&
9348 "Expected only + or - operations for depend clauses.");
9349 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
9350 Cnt = SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: BOK, LHSExpr: Cnt, RHSExpr: Inc).get();
9351 if (!Cnt)
9352 return nullptr;
9353 }
9354 QualType VarType = LCDecl->getType().getNonReferenceType();
9355 if (!VarType->isIntegerType() && !VarType->isPointerType() &&
9356 !SemaRef.getLangOpts().CPlusPlus)
9357 return nullptr;
9358 // Upper - Lower
9359 Expr *Upper =
9360 *TestIsLessOp ? Cnt : tryBuildCapture(SemaRef, Capture: LB, Captures).get();
9361 Expr *Lower =
9362 *TestIsLessOp ? tryBuildCapture(SemaRef, Capture: LB, Captures).get() : Cnt;
9363 if (!Upper || !Lower)
9364 return nullptr;
9365
9366 ExprResult Diff =
9367 calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, Step, LCTy: VarType,
9368 /*TestIsStrictOp=*/false, /*RoundToStep=*/false,
9369 Captures, InitDependOnLC, CondDependOnLC);
9370 if (!Diff.isUsable())
9371 return nullptr;
9372
9373 return Diff.get();
9374}
9375} // namespace
9376
9377void SemaOpenMP::ActOnOpenMPLoopInitialization(SourceLocation ForLoc,
9378 Stmt *Init) {
9379 assert(getLangOpts().OpenMP && "OpenMP is not active.");
9380 assert(Init && "Expected loop in canonical form.");
9381 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
9382 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
9383 if (AssociatedLoops == 0 || !isOpenMPLoopDirective(DKind))
9384 return;
9385
9386 DSAStack->loopStart();
9387 llvm::SmallPtrSet<const Decl *, 1> EmptyDeclSet;
9388 OpenMPIterationSpaceChecker ISC(SemaRef, /*SupportsNonRectangular=*/true,
9389 *DSAStack, ForLoc, EmptyDeclSet);
9390 if (!ISC.checkAndSetInit(S: Init, /*EmitDiags=*/false)) {
9391 if (ValueDecl *D = ISC.getLoopDecl()) {
9392 auto *VD = dyn_cast<VarDecl>(Val: D);
9393 DeclRefExpr *PrivateRef = nullptr;
9394 if (!VD) {
9395 if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
9396 VD = Private;
9397 } else {
9398 PrivateRef = buildCapture(S&: SemaRef, D, CaptureExpr: ISC.getLoopDeclRefExpr(),
9399 /*WithInit=*/false);
9400 VD = cast<VarDecl>(Val: PrivateRef->getDecl());
9401 }
9402 }
9403 DSAStack->addLoopControlVariable(D, Capture: VD);
9404 const Decl *LD = DSAStack->getPossiblyLoopCounter();
9405 if (LD != D->getCanonicalDecl()) {
9406 DSAStack->resetPossibleLoopCounter();
9407 if (auto *Var = dyn_cast_or_null<VarDecl>(Val: LD))
9408 SemaRef.MarkDeclarationsReferencedInExpr(E: buildDeclRefExpr(
9409 S&: SemaRef, D: const_cast<VarDecl *>(Var),
9410 Ty: Var->getType().getNonLValueExprType(Context: getASTContext()), Loc: ForLoc,
9411 /*RefersToCapture=*/true));
9412 }
9413 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables
9414 // Referenced in a Construct, C/C++]. The loop iteration variable in the
9415 // associated for-loop of a simd construct with just one associated
9416 // for-loop may be listed in a linear clause with a constant-linear-step
9417 // that is the increment of the associated for-loop. The loop iteration
9418 // variable(s) in the associated for-loop(s) of a for or parallel for
9419 // construct may be listed in a private or lastprivate clause.
9420 DSAStackTy::DSAVarData DVar =
9421 DSAStack->getTopDSA(D, /*FromParent=*/false);
9422 // If LoopVarRefExpr is nullptr it means the corresponding loop variable
9423 // is declared in the loop and it is predetermined as a private.
9424 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
9425 OpenMPClauseKind PredeterminedCKind =
9426 isOpenMPSimdDirective(DKind)
9427 ? (DSAStack->hasMutipleLoops() ? OMPC_lastprivate : OMPC_linear)
9428 : OMPC_private;
9429 auto IsOpenMPTaskloopDirective = [](OpenMPDirectiveKind DK) {
9430 return getLeafConstructsOrSelf(D: DK).back() == OMPD_taskloop;
9431 };
9432 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
9433 DVar.CKind != PredeterminedCKind && DVar.RefExpr &&
9434 (getLangOpts().OpenMP <= 45 ||
9435 (DVar.CKind != OMPC_lastprivate && DVar.CKind != OMPC_private))) ||
9436 ((isOpenMPWorksharingDirective(DKind) ||
9437 IsOpenMPTaskloopDirective(DKind) ||
9438 isOpenMPDistributeDirective(DKind)) &&
9439 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
9440 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
9441 (DVar.CKind != OMPC_private || DVar.RefExpr)) {
9442 unsigned OMPVersion = getLangOpts().OpenMP;
9443 Diag(Loc: Init->getBeginLoc(), DiagID: diag::err_omp_loop_var_dsa)
9444 << getOpenMPClauseNameForDiag(C: DVar.CKind)
9445 << getOpenMPDirectiveName(D: DKind, Ver: OMPVersion)
9446 << getOpenMPClauseNameForDiag(C: PredeterminedCKind);
9447 if (DVar.RefExpr == nullptr)
9448 DVar.CKind = PredeterminedCKind;
9449 reportOriginalDsa(SemaRef, DSAStack, D, DVar, /*IsLoopIterVar=*/true);
9450 } else if (LoopDeclRefExpr) {
9451 // Make the loop iteration variable private (for worksharing
9452 // constructs), linear (for simd directives with the only one
9453 // associated loop) or lastprivate (for simd directives with several
9454 // collapsed or ordered loops).
9455 if (DVar.CKind == OMPC_unknown)
9456 DSAStack->addDSA(D, E: LoopDeclRefExpr, A: PredeterminedCKind, PrivateCopy: PrivateRef);
9457 }
9458 }
9459 }
9460 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
9461}
9462
9463namespace {
9464// Utility for OpenMP doacross clause kind
9465class OMPDoacrossKind {
9466public:
9467 bool isSource(const OMPDoacrossClause *C) {
9468 return C->getDependenceType() == OMPC_DOACROSS_source ||
9469 C->getDependenceType() == OMPC_DOACROSS_source_omp_cur_iteration;
9470 }
9471 bool isSink(const OMPDoacrossClause *C) {
9472 return C->getDependenceType() == OMPC_DOACROSS_sink;
9473 }
9474 bool isSinkIter(const OMPDoacrossClause *C) {
9475 return C->getDependenceType() == OMPC_DOACROSS_sink_omp_cur_iteration;
9476 }
9477};
9478} // namespace
9479/// Called on a for stmt to check and extract its iteration space
9480/// for further processing (such as collapsing).
9481static bool checkOpenMPIterationSpace(
9482 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
9483 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
9484 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
9485 Expr *OrderedLoopCountExpr,
9486 SemaOpenMP::VarsWithInheritedDSAType &VarsWithImplicitDSA,
9487 llvm::MutableArrayRef<LoopIterationSpace> ResultIterSpaces,
9488 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
9489 const llvm::SmallPtrSetImpl<const Decl *> &CollapsedLoopVarDecls) {
9490 bool SupportsNonRectangular = !isOpenMPLoopTransformationDirective(DKind);
9491 // OpenMP [2.9.1, Canonical Loop Form]
9492 // for (init-expr; test-expr; incr-expr) structured-block
9493 // for (range-decl: range-expr) structured-block
9494 if (auto *CanonLoop = dyn_cast_or_null<OMPCanonicalLoop>(Val: S))
9495 S = CanonLoop->getLoopStmt();
9496 auto *For = dyn_cast_or_null<ForStmt>(Val: S);
9497 auto *CXXFor = dyn_cast_or_null<CXXForRangeStmt>(Val: S);
9498 // Ranged for is supported only in OpenMP 5.0.
9499 if (!For && (SemaRef.LangOpts.OpenMP <= 45 || !CXXFor)) {
9500 unsigned OMPVersion = SemaRef.getLangOpts().OpenMP;
9501 SemaRef.Diag(Loc: S->getBeginLoc(), DiagID: diag::err_omp_not_for)
9502 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
9503 << getOpenMPDirectiveName(D: DKind, Ver: OMPVersion) << TotalNestedLoopCount
9504 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
9505 if (TotalNestedLoopCount > 1) {
9506 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
9507 SemaRef.Diag(Loc: DSA.getConstructLoc(),
9508 DiagID: diag::note_omp_collapse_ordered_expr)
9509 << 2 << CollapseLoopCountExpr->getSourceRange()
9510 << OrderedLoopCountExpr->getSourceRange();
9511 else if (CollapseLoopCountExpr)
9512 SemaRef.Diag(Loc: CollapseLoopCountExpr->getExprLoc(),
9513 DiagID: diag::note_omp_collapse_ordered_expr)
9514 << 0 << CollapseLoopCountExpr->getSourceRange();
9515 else if (OrderedLoopCountExpr)
9516 SemaRef.Diag(Loc: OrderedLoopCountExpr->getExprLoc(),
9517 DiagID: diag::note_omp_collapse_ordered_expr)
9518 << 1 << OrderedLoopCountExpr->getSourceRange();
9519 }
9520 return true;
9521 }
9522 assert(((For && For->getBody()) || (CXXFor && CXXFor->getBody())) &&
9523 "No loop body.");
9524 // Postpone analysis in dependent contexts for ranged for loops.
9525 if (CXXFor && SemaRef.CurContext->isDependentContext())
9526 return false;
9527
9528 OpenMPIterationSpaceChecker ISC(SemaRef, SupportsNonRectangular, DSA,
9529 For ? For->getForLoc() : CXXFor->getForLoc(),
9530 CollapsedLoopVarDecls);
9531
9532 // Check init.
9533 Stmt *Init = For ? For->getInit() : CXXFor->getBeginStmt();
9534 if (ISC.checkAndSetInit(S: Init))
9535 return true;
9536
9537 bool HasErrors = false;
9538
9539 // Check loop variable's type.
9540 if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
9541 // OpenMP [2.6, Canonical Loop Form]
9542 // Var is one of the following:
9543 // A variable of signed or unsigned integer type.
9544 // For C++, a variable of a random access iterator type.
9545 // For C, a variable of a pointer type.
9546 QualType VarType = LCDecl->getType().getNonReferenceType();
9547 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
9548 !VarType->isPointerType() &&
9549 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
9550 SemaRef.Diag(Loc: Init->getBeginLoc(), DiagID: diag::err_omp_loop_variable_type)
9551 << SemaRef.getLangOpts().CPlusPlus;
9552 HasErrors = true;
9553 }
9554
9555 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
9556 // a Construct
9557 // The loop iteration variable(s) in the associated for-loop(s) of a for or
9558 // parallel for construct is (are) private.
9559 // The loop iteration variable in the associated for-loop of a simd
9560 // construct with just one associated for-loop is linear with a
9561 // constant-linear-step that is the increment of the associated for-loop.
9562 // Exclude loop var from the list of variables with implicitly defined data
9563 // sharing attributes.
9564 VarsWithImplicitDSA.erase(Val: LCDecl);
9565
9566 assert((isOpenMPLoopDirective(DKind) ||
9567 isOpenMPCanonicalLoopSequenceTransformationDirective(DKind)) &&
9568 "DSA for non-loop vars");
9569
9570 // Check test-expr.
9571 HasErrors |= ISC.checkAndSetCond(S: For ? For->getCond() : CXXFor->getCond());
9572
9573 // Check incr-expr.
9574 HasErrors |= ISC.checkAndSetInc(S: For ? For->getInc() : CXXFor->getInc());
9575 }
9576
9577 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
9578 return HasErrors;
9579
9580 // Build the loop's iteration space representation.
9581 ResultIterSpaces[CurrentNestedLoopCount].PreCond = ISC.buildPreCond(
9582 S: DSA.getCurScope(), Cond: For ? For->getCond() : CXXFor->getCond(), Captures);
9583 ResultIterSpaces[CurrentNestedLoopCount].NumIterations =
9584 ISC.buildNumIterations(S: DSA.getCurScope(), ResultIterSpaces,
9585 LimitedType: (isOpenMPWorksharingDirective(DKind) ||
9586 isOpenMPGenericLoopDirective(DKind) ||
9587 isOpenMPTaskLoopDirective(DKind) ||
9588 isOpenMPDistributeDirective(DKind) ||
9589 isOpenMPLoopTransformationDirective(DKind)),
9590 Captures);
9591 ResultIterSpaces[CurrentNestedLoopCount].CounterVar =
9592 ISC.buildCounterVar(Captures, DSA);
9593 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar =
9594 ISC.buildPrivateCounterVar();
9595 ResultIterSpaces[CurrentNestedLoopCount].CounterInit = ISC.buildCounterInit();
9596 ResultIterSpaces[CurrentNestedLoopCount].CounterStep = ISC.buildCounterStep();
9597 ResultIterSpaces[CurrentNestedLoopCount].InitSrcRange = ISC.getInitSrcRange();
9598 ResultIterSpaces[CurrentNestedLoopCount].CondSrcRange =
9599 ISC.getConditionSrcRange();
9600 ResultIterSpaces[CurrentNestedLoopCount].IncSrcRange =
9601 ISC.getIncrementSrcRange();
9602 ResultIterSpaces[CurrentNestedLoopCount].Subtract = ISC.shouldSubtractStep();
9603 ResultIterSpaces[CurrentNestedLoopCount].IsStrictCompare =
9604 ISC.isStrictTestOp();
9605 std::tie(args&: ResultIterSpaces[CurrentNestedLoopCount].MinValue,
9606 args&: ResultIterSpaces[CurrentNestedLoopCount].MaxValue) =
9607 ISC.buildMinMaxValues(S: DSA.getCurScope(), Captures);
9608 ResultIterSpaces[CurrentNestedLoopCount].FinalCondition =
9609 ISC.buildFinalCondition(S: DSA.getCurScope());
9610 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularLB =
9611 ISC.doesInitDependOnLC();
9612 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularUB =
9613 ISC.doesCondDependOnLC();
9614 ResultIterSpaces[CurrentNestedLoopCount].LoopDependentIdx =
9615 ISC.getLoopDependentIdx();
9616
9617 HasErrors |=
9618 (ResultIterSpaces[CurrentNestedLoopCount].PreCond == nullptr ||
9619 ResultIterSpaces[CurrentNestedLoopCount].NumIterations == nullptr ||
9620 ResultIterSpaces[CurrentNestedLoopCount].CounterVar == nullptr ||
9621 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar == nullptr ||
9622 ResultIterSpaces[CurrentNestedLoopCount].CounterInit == nullptr ||
9623 ResultIterSpaces[CurrentNestedLoopCount].CounterStep == nullptr);
9624 if (!HasErrors && DSA.isOrderedRegion()) {
9625 if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
9626 if (CurrentNestedLoopCount <
9627 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
9628 DSA.getOrderedRegionParam().second->setLoopNumIterations(
9629 NumLoop: CurrentNestedLoopCount,
9630 NumIterations: ResultIterSpaces[CurrentNestedLoopCount].NumIterations);
9631 DSA.getOrderedRegionParam().second->setLoopCounter(
9632 NumLoop: CurrentNestedLoopCount,
9633 Counter: ResultIterSpaces[CurrentNestedLoopCount].CounterVar);
9634 }
9635 }
9636 for (auto &Pair : DSA.getDoacrossDependClauses()) {
9637 auto *DependC = dyn_cast<OMPDependClause>(Val: Pair.first);
9638 auto *DoacrossC = dyn_cast<OMPDoacrossClause>(Val: Pair.first);
9639 unsigned NumLoops =
9640 DependC ? DependC->getNumLoops() : DoacrossC->getNumLoops();
9641 if (CurrentNestedLoopCount >= NumLoops) {
9642 // Erroneous case - clause has some problems.
9643 continue;
9644 }
9645 if (DependC && DependC->getDependencyKind() == OMPC_DEPEND_sink &&
9646 Pair.second.size() <= CurrentNestedLoopCount) {
9647 // Erroneous case - clause has some problems.
9648 DependC->setLoopData(NumLoop: CurrentNestedLoopCount, Cnt: nullptr);
9649 continue;
9650 }
9651 OMPDoacrossKind ODK;
9652 if (DoacrossC && ODK.isSink(C: DoacrossC) &&
9653 Pair.second.size() <= CurrentNestedLoopCount) {
9654 // Erroneous case - clause has some problems.
9655 DoacrossC->setLoopData(NumLoop: CurrentNestedLoopCount, Cnt: nullptr);
9656 continue;
9657 }
9658 Expr *CntValue;
9659 SourceLocation DepLoc =
9660 DependC ? DependC->getDependencyLoc() : DoacrossC->getDependenceLoc();
9661 if ((DependC && DependC->getDependencyKind() == OMPC_DEPEND_source) ||
9662 (DoacrossC && ODK.isSource(C: DoacrossC)))
9663 CntValue = ISC.buildOrderedLoopData(
9664 S: DSA.getCurScope(),
9665 Counter: ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
9666 Loc: DepLoc);
9667 else if (DoacrossC && ODK.isSinkIter(C: DoacrossC)) {
9668 Expr *Cnt = SemaRef
9669 .DefaultLvalueConversion(
9670 E: ResultIterSpaces[CurrentNestedLoopCount].CounterVar)
9671 .get();
9672 if (!Cnt)
9673 continue;
9674 // build CounterVar - 1
9675 Expr *Inc =
9676 SemaRef.ActOnIntegerConstant(Loc: DoacrossC->getColonLoc(), /*Val=*/1)
9677 .get();
9678 CntValue = ISC.buildOrderedLoopData(
9679 S: DSA.getCurScope(),
9680 Counter: ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
9681 Loc: DepLoc, Inc, OOK: clang::OO_Minus);
9682 } else
9683 CntValue = ISC.buildOrderedLoopData(
9684 S: DSA.getCurScope(),
9685 Counter: ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
9686 Loc: DepLoc, Inc: Pair.second[CurrentNestedLoopCount].first,
9687 OOK: Pair.second[CurrentNestedLoopCount].second);
9688 if (DependC)
9689 DependC->setLoopData(NumLoop: CurrentNestedLoopCount, Cnt: CntValue);
9690 else
9691 DoacrossC->setLoopData(NumLoop: CurrentNestedLoopCount, Cnt: CntValue);
9692 }
9693 }
9694
9695 return HasErrors;
9696}
9697
9698/// Build 'VarRef = Start.
9699static ExprResult
9700buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
9701 ExprResult Start, bool IsNonRectangularLB,
9702 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
9703 // Build 'VarRef = Start.
9704 ExprResult NewStart = IsNonRectangularLB
9705 ? Start.get()
9706 : tryBuildCapture(SemaRef, Capture: Start.get(), Captures);
9707 if (!NewStart.isUsable())
9708 return ExprError();
9709 if (!SemaRef.Context.hasSameType(T1: NewStart.get()->getType(),
9710 T2: VarRef.get()->getType())) {
9711 NewStart = SemaRef.PerformImplicitConversion(
9712 From: NewStart.get(), ToType: VarRef.get()->getType(), Action: AssignmentAction::Converting,
9713 /*AllowExplicit=*/true);
9714 if (!NewStart.isUsable())
9715 return ExprError();
9716 }
9717
9718 ExprResult Init =
9719 SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: BO_Assign, LHSExpr: VarRef.get(), RHSExpr: NewStart.get());
9720 return Init;
9721}
9722
9723/// Build 'VarRef = Start + Iter * Step'.
9724static ExprResult buildCounterUpdate(
9725 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
9726 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
9727 bool IsNonRectangularLB,
9728 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
9729 // Add parentheses (for debugging purposes only).
9730 Iter = SemaRef.ActOnParenExpr(L: Loc, R: Loc, E: Iter.get());
9731 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
9732 !Step.isUsable())
9733 return ExprError();
9734
9735 ExprResult NewStep = Step;
9736 if (Captures)
9737 NewStep = tryBuildCapture(SemaRef, Capture: Step.get(), Captures&: *Captures);
9738 if (NewStep.isInvalid())
9739 return ExprError();
9740 ExprResult Update =
9741 SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: BO_Mul, LHSExpr: Iter.get(), RHSExpr: NewStep.get());
9742 if (!Update.isUsable())
9743 return ExprError();
9744
9745 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
9746 // 'VarRef = Start (+|-) Iter * Step'.
9747 if (!Start.isUsable())
9748 return ExprError();
9749 ExprResult NewStart = SemaRef.ActOnParenExpr(L: Loc, R: Loc, E: Start.get());
9750 if (!NewStart.isUsable())
9751 return ExprError();
9752 if (Captures && !IsNonRectangularLB)
9753 NewStart = tryBuildCapture(SemaRef, Capture: Start.get(), Captures&: *Captures);
9754 if (NewStart.isInvalid())
9755 return ExprError();
9756
9757 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
9758 ExprResult SavedUpdate = Update;
9759 ExprResult UpdateVal;
9760 if (VarRef.get()->getType()->isOverloadableType() ||
9761 NewStart.get()->getType()->isOverloadableType() ||
9762 Update.get()->getType()->isOverloadableType()) {
9763 Sema::TentativeAnalysisScope Trap(SemaRef);
9764
9765 Update =
9766 SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: BO_Assign, LHSExpr: VarRef.get(), RHSExpr: NewStart.get());
9767 if (Update.isUsable()) {
9768 UpdateVal =
9769 SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: Subtract ? BO_SubAssign : BO_AddAssign,
9770 LHSExpr: VarRef.get(), RHSExpr: SavedUpdate.get());
9771 if (UpdateVal.isUsable()) {
9772 Update = SemaRef.CreateBuiltinBinOp(OpLoc: Loc, Opc: BO_Comma, LHSExpr: Update.get(),
9773 RHSExpr: UpdateVal.get());
9774 }
9775 }
9776 }
9777
9778 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
9779 if (!Update.isUsable() || !UpdateVal.isUsable()) {
9780 Update = SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: Subtract ? BO_Sub : BO_Add,
9781 LHSExpr: NewStart.get(), RHSExpr: SavedUpdate.get());
9782 if (!Update.isUsable())
9783 return ExprError();
9784
9785 if (!SemaRef.Context.hasSameType(T1: Update.get()->getType(),
9786 T2: VarRef.get()->getType())) {
9787 Update = SemaRef.PerformImplicitConversion(
9788 From: Update.get(), ToType: VarRef.get()->getType(), Action: AssignmentAction::Converting,
9789 /*AllowExplicit=*/true);
9790 if (!Update.isUsable())
9791 return ExprError();
9792 }
9793
9794 Update = SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: BO_Assign, LHSExpr: VarRef.get(), RHSExpr: Update.get());
9795 }
9796 return Update;
9797}
9798
9799/// Convert integer expression \a E to make it have at least \a Bits
9800/// bits.
9801static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
9802 if (E == nullptr)
9803 return ExprError();
9804 ASTContext &C = SemaRef.Context;
9805 QualType OldType = E->getType();
9806 unsigned HasBits = C.getTypeSize(T: OldType);
9807 if (HasBits >= Bits)
9808 return ExprResult(E);
9809 // OK to convert to signed, because new type has more bits than old.
9810 QualType NewType = C.getIntTypeForBitwidth(DestWidth: Bits, /*Signed=*/true);
9811 return SemaRef.PerformImplicitConversion(
9812 From: E, ToType: NewType, Action: AssignmentAction::Converting, /*AllowExplicit=*/true);
9813}
9814
9815/// Check if the given expression \a E is a constant integer that fits
9816/// into \a Bits bits.
9817static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
9818 if (E == nullptr)
9819 return false;
9820 if (std::optional<llvm::APSInt> Result =
9821 E->getIntegerConstantExpr(Ctx: SemaRef.Context))
9822 return Signed ? Result->isSignedIntN(N: Bits) : Result->isIntN(N: Bits);
9823 return false;
9824}
9825
9826/// Build preinits statement for the given declarations.
9827static Stmt *buildPreInits(ASTContext &Context,
9828 MutableArrayRef<Decl *> PreInits) {
9829 if (!PreInits.empty()) {
9830 return new (Context) DeclStmt(
9831 DeclGroupRef::Create(C&: Context, Decls: PreInits.begin(), NumDecls: PreInits.size()),
9832 SourceLocation(), SourceLocation());
9833 }
9834 return nullptr;
9835}
9836
9837/// Append the \p Item or the content of a CompoundStmt to the list \p
9838/// TargetList.
9839///
9840/// A CompoundStmt is used as container in case multiple statements need to be
9841/// stored in lieu of using an explicit list. Flattening is necessary because
9842/// contained DeclStmts need to be visible after the execution of the list. Used
9843/// for OpenMP pre-init declarations/statements.
9844static void appendFlattenedStmtList(SmallVectorImpl<Stmt *> &TargetList,
9845 Stmt *Item) {
9846 // nullptr represents an empty list.
9847 if (!Item)
9848 return;
9849
9850 if (auto *CS = dyn_cast<CompoundStmt>(Val: Item))
9851 llvm::append_range(C&: TargetList, R: CS->body());
9852 else
9853 TargetList.push_back(Elt: Item);
9854}
9855
9856/// Build preinits statement for the given declarations.
9857static Stmt *
9858buildPreInits(ASTContext &Context,
9859 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
9860 if (!Captures.empty()) {
9861 SmallVector<Decl *, 16> PreInits;
9862 for (const auto &Pair : Captures)
9863 PreInits.push_back(Elt: Pair.second->getDecl());
9864 return buildPreInits(Context, PreInits);
9865 }
9866 return nullptr;
9867}
9868
9869/// Build pre-init statement for the given statements.
9870static Stmt *buildPreInits(ASTContext &Context, ArrayRef<Stmt *> PreInits) {
9871 if (PreInits.empty())
9872 return nullptr;
9873
9874 SmallVector<Stmt *> Stmts;
9875 for (Stmt *S : PreInits)
9876 appendFlattenedStmtList(TargetList&: Stmts, Item: S);
9877 return CompoundStmt::Create(C: Context, Stmts: PreInits, FPFeatures: FPOptionsOverride(), LB: {}, RB: {});
9878}
9879
9880/// Build postupdate expression for the given list of postupdates expressions.
9881static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
9882 Expr *PostUpdate = nullptr;
9883 if (!PostUpdates.empty()) {
9884 for (Expr *E : PostUpdates) {
9885 Expr *ConvE = S.BuildCStyleCastExpr(
9886 LParenLoc: E->getExprLoc(),
9887 Ty: S.Context.getTrivialTypeSourceInfo(T: S.Context.VoidTy),
9888 RParenLoc: E->getExprLoc(), Op: E)
9889 .get();
9890 PostUpdate = PostUpdate
9891 ? S.CreateBuiltinBinOp(OpLoc: ConvE->getExprLoc(), Opc: BO_Comma,
9892 LHSExpr: PostUpdate, RHSExpr: ConvE)
9893 .get()
9894 : ConvE;
9895 }
9896 }
9897 return PostUpdate;
9898}
9899
9900/// Look for variables declared in the body parts of a for-loop nest. Used
9901/// for verifying loop nest structure before performing a loop collapse
9902/// operation.
9903class ForVarDeclFinder : public DynamicRecursiveASTVisitor {
9904 int NestingDepth = 0;
9905 llvm::SmallPtrSetImpl<const Decl *> &VarDecls;
9906
9907public:
9908 explicit ForVarDeclFinder(llvm::SmallPtrSetImpl<const Decl *> &VD)
9909 : VarDecls(VD) {}
9910
9911 bool VisitForStmt(ForStmt *F) override {
9912 ++NestingDepth;
9913 TraverseStmt(S: F->getBody());
9914 --NestingDepth;
9915 return false;
9916 }
9917
9918 bool VisitCXXForRangeStmt(CXXForRangeStmt *RF) override {
9919 ++NestingDepth;
9920 TraverseStmt(S: RF->getBody());
9921 --NestingDepth;
9922 return false;
9923 }
9924
9925 bool VisitVarDecl(VarDecl *D) override {
9926 Decl *C = D->getCanonicalDecl();
9927 if (NestingDepth > 0)
9928 VarDecls.insert(Ptr: C);
9929 return true;
9930 }
9931};
9932
9933/// Called on a for stmt to check itself and nested loops (if any).
9934/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
9935/// number of collapsed loops otherwise.
9936static unsigned
9937checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
9938 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
9939 DSAStackTy &DSA,
9940 SemaOpenMP::VarsWithInheritedDSAType &VarsWithImplicitDSA,
9941 OMPLoopBasedDirective::HelperExprs &Built) {
9942 // If either of the loop expressions exist and contain errors, we bail out
9943 // early because diagnostics have already been emitted and we can't reliably
9944 // check more about the loop.
9945 if ((CollapseLoopCountExpr && CollapseLoopCountExpr->containsErrors()) ||
9946 (OrderedLoopCountExpr && OrderedLoopCountExpr->containsErrors()))
9947 return 0;
9948
9949 unsigned NestedLoopCount = 1;
9950 bool SupportsNonPerfectlyNested = (SemaRef.LangOpts.OpenMP >= 50) &&
9951 !isOpenMPLoopTransformationDirective(DKind);
9952 llvm::SmallPtrSet<const Decl *, 4> CollapsedLoopVarDecls;
9953
9954 if (CollapseLoopCountExpr) {
9955 // Found 'collapse' clause - calculate collapse number.
9956 Expr::EvalResult Result;
9957 if (!CollapseLoopCountExpr->isValueDependent() &&
9958 CollapseLoopCountExpr->EvaluateAsInt(Result, Ctx: SemaRef.getASTContext())) {
9959 NestedLoopCount = Result.Val.getInt().getLimitedValue();
9960
9961 ForVarDeclFinder FVDF{CollapsedLoopVarDecls};
9962 FVDF.TraverseStmt(S: AStmt);
9963 } else {
9964 Built.clear(/*Size=*/1);
9965 return 1;
9966 }
9967 }
9968 unsigned OrderedLoopCount = 1;
9969 if (OrderedLoopCountExpr) {
9970 // Found 'ordered' clause - calculate collapse number.
9971 Expr::EvalResult EVResult;
9972 if (!OrderedLoopCountExpr->isValueDependent() &&
9973 OrderedLoopCountExpr->EvaluateAsInt(Result&: EVResult,
9974 Ctx: SemaRef.getASTContext())) {
9975 llvm::APSInt Result = EVResult.Val.getInt();
9976 if (Result.getLimitedValue() < NestedLoopCount) {
9977 SemaRef.Diag(Loc: OrderedLoopCountExpr->getExprLoc(),
9978 DiagID: diag::err_omp_wrong_ordered_loop_count)
9979 << OrderedLoopCountExpr->getSourceRange();
9980 SemaRef.Diag(Loc: CollapseLoopCountExpr->getExprLoc(),
9981 DiagID: diag::note_collapse_loop_count)
9982 << CollapseLoopCountExpr->getSourceRange();
9983 }
9984 OrderedLoopCount = Result.getLimitedValue();
9985 } else {
9986 Built.clear(/*Size=*/1);
9987 return 1;
9988 }
9989 }
9990 // This is helper routine for loop directives (e.g., 'for', 'simd',
9991 // 'for simd', etc.).
9992 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
9993 unsigned NumLoops = std::max(a: OrderedLoopCount, b: NestedLoopCount);
9994 SmallVector<LoopIterationSpace, 4> IterSpaces(NumLoops);
9995 if (!OMPLoopBasedDirective::doForAllLoops(
9996 CurStmt: AStmt->IgnoreContainers(
9997 IgnoreCaptured: !isOpenMPCanonicalLoopNestTransformationDirective(DKind)),
9998 TryImperfectlyNestedLoops: SupportsNonPerfectlyNested, NumLoops,
9999 Callback: [DKind, &SemaRef, &DSA, NumLoops, NestedLoopCount,
10000 CollapseLoopCountExpr, OrderedLoopCountExpr, &VarsWithImplicitDSA,
10001 &IterSpaces, &Captures,
10002 &CollapsedLoopVarDecls](unsigned Cnt, Stmt *CurStmt) {
10003 if (checkOpenMPIterationSpace(
10004 DKind, S: CurStmt, SemaRef, DSA, CurrentNestedLoopCount: Cnt, NestedLoopCount,
10005 TotalNestedLoopCount: NumLoops, CollapseLoopCountExpr, OrderedLoopCountExpr,
10006 VarsWithImplicitDSA, ResultIterSpaces: IterSpaces, Captures,
10007 CollapsedLoopVarDecls))
10008 return true;
10009 if (Cnt > 0 && Cnt >= NestedLoopCount &&
10010 IterSpaces[Cnt].CounterVar) {
10011 // Handle initialization of captured loop iterator variables.
10012 auto *DRE = cast<DeclRefExpr>(Val: IterSpaces[Cnt].CounterVar);
10013 if (isa<OMPCapturedExprDecl>(Val: DRE->getDecl())) {
10014 Captures[DRE] = DRE;
10015 }
10016 }
10017 return false;
10018 },
10019 OnTransformationCallback: [&SemaRef, &Captures](OMPLoopTransformationDirective *Transform) {
10020 Stmt *DependentPreInits = Transform->getPreInits();
10021 if (!DependentPreInits)
10022 return;
10023
10024 // Search for pre-init declared variables that need to be captured
10025 // to be referenceable inside the directive.
10026 SmallVector<Stmt *> Constituents;
10027 appendFlattenedStmtList(TargetList&: Constituents, Item: DependentPreInits);
10028 for (Stmt *S : Constituents) {
10029 if (auto *DC = dyn_cast<DeclStmt>(Val: S)) {
10030 for (Decl *C : DC->decls()) {
10031 auto *D = cast<VarDecl>(Val: C);
10032 DeclRefExpr *Ref = buildDeclRefExpr(
10033 S&: SemaRef, D, Ty: D->getType().getNonReferenceType(),
10034 Loc: cast<OMPExecutableDirective>(Val: Transform->getDirective())
10035 ->getBeginLoc());
10036 Captures[Ref] = Ref;
10037 }
10038 }
10039 }
10040 }))
10041 return 0;
10042
10043 Built.clear(/*size=*/Size: NestedLoopCount);
10044
10045 if (SemaRef.CurContext->isDependentContext())
10046 return NestedLoopCount;
10047
10048 // An example of what is generated for the following code:
10049 //
10050 // #pragma omp simd collapse(2) ordered(2)
10051 // for (i = 0; i < NI; ++i)
10052 // for (k = 0; k < NK; ++k)
10053 // for (j = J0; j < NJ; j+=2) {
10054 // <loop body>
10055 // }
10056 //
10057 // We generate the code below.
10058 // Note: the loop body may be outlined in CodeGen.
10059 // Note: some counters may be C++ classes, operator- is used to find number of
10060 // iterations and operator+= to calculate counter value.
10061 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
10062 // or i64 is currently supported).
10063 //
10064 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
10065 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
10066 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
10067 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
10068 // // similar updates for vars in clauses (e.g. 'linear')
10069 // <loop body (using local i and j)>
10070 // }
10071 // i = NI; // assign final values of counters
10072 // j = NJ;
10073 //
10074
10075 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
10076 // the iteration counts of the collapsed for loops.
10077 // Precondition tests if there is at least one iteration (all conditions are
10078 // true).
10079 auto PreCond = ExprResult(IterSpaces[0].PreCond);
10080 Expr *N0 = IterSpaces[0].NumIterations;
10081 ExprResult LastIteration32 = widenIterationCount(
10082 /*Bits=*/32,
10083 E: SemaRef
10084 .PerformImplicitConversion(From: N0->IgnoreImpCasts(), ToType: N0->getType(),
10085 Action: AssignmentAction::Converting,
10086 /*AllowExplicit=*/true)
10087 .get(),
10088 SemaRef);
10089 ExprResult LastIteration64 = widenIterationCount(
10090 /*Bits=*/64,
10091 E: SemaRef
10092 .PerformImplicitConversion(From: N0->IgnoreImpCasts(), ToType: N0->getType(),
10093 Action: AssignmentAction::Converting,
10094 /*AllowExplicit=*/true)
10095 .get(),
10096 SemaRef);
10097
10098 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
10099 return NestedLoopCount;
10100
10101 ASTContext &C = SemaRef.Context;
10102 bool AllCountsNeedLessThan32Bits = C.getTypeSize(T: N0->getType()) < 32;
10103
10104 Scope *CurScope = DSA.getCurScope();
10105 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
10106 if (PreCond.isUsable()) {
10107 PreCond =
10108 SemaRef.BuildBinOp(S: CurScope, OpLoc: PreCond.get()->getExprLoc(), Opc: BO_LAnd,
10109 LHSExpr: PreCond.get(), RHSExpr: IterSpaces[Cnt].PreCond);
10110 }
10111 Expr *N = IterSpaces[Cnt].NumIterations;
10112 SourceLocation Loc = N->getExprLoc();
10113 AllCountsNeedLessThan32Bits &= C.getTypeSize(T: N->getType()) < 32;
10114 if (LastIteration32.isUsable())
10115 LastIteration32 = SemaRef.BuildBinOp(
10116 S: CurScope, OpLoc: Loc, Opc: BO_Mul, LHSExpr: LastIteration32.get(),
10117 RHSExpr: SemaRef
10118 .PerformImplicitConversion(From: N->IgnoreImpCasts(), ToType: N->getType(),
10119 Action: AssignmentAction::Converting,
10120 /*AllowExplicit=*/true)
10121 .get());
10122 if (LastIteration64.isUsable())
10123 LastIteration64 = SemaRef.BuildBinOp(
10124 S: CurScope, OpLoc: Loc, Opc: BO_Mul, LHSExpr: LastIteration64.get(),
10125 RHSExpr: SemaRef
10126 .PerformImplicitConversion(From: N->IgnoreImpCasts(), ToType: N->getType(),
10127 Action: AssignmentAction::Converting,
10128 /*AllowExplicit=*/true)
10129 .get());
10130 }
10131
10132 // Choose either the 32-bit or 64-bit version.
10133 ExprResult LastIteration = LastIteration64;
10134 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse ||
10135 (LastIteration32.isUsable() &&
10136 C.getTypeSize(T: LastIteration32.get()->getType()) == 32 &&
10137 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
10138 fitsInto(
10139 /*Bits=*/32,
10140 Signed: LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
10141 E: LastIteration64.get(), SemaRef))))
10142 LastIteration = LastIteration32;
10143 QualType VType = LastIteration.get()->getType();
10144 QualType RealVType = VType;
10145 QualType StrideVType = VType;
10146 if (isOpenMPTaskLoopDirective(DKind)) {
10147 VType =
10148 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
10149 StrideVType =
10150 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
10151 }
10152
10153 if (!LastIteration.isUsable())
10154 return 0;
10155
10156 // Save the number of iterations.
10157 ExprResult NumIterations = LastIteration;
10158 {
10159 LastIteration = SemaRef.BuildBinOp(
10160 S: CurScope, OpLoc: LastIteration.get()->getExprLoc(), Opc: BO_Sub,
10161 LHSExpr: LastIteration.get(),
10162 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get());
10163 if (!LastIteration.isUsable())
10164 return 0;
10165 }
10166
10167 // Calculate the last iteration number beforehand instead of doing this on
10168 // each iteration. Do not do this if the number of iterations may be kfold-ed.
10169 bool IsConstant = LastIteration.get()->isIntegerConstantExpr(Ctx: SemaRef.Context);
10170 ExprResult CalcLastIteration;
10171 if (!IsConstant) {
10172 ExprResult SaveRef =
10173 tryBuildCapture(SemaRef, Capture: LastIteration.get(), Captures);
10174 LastIteration = SaveRef;
10175
10176 // Prepare SaveRef + 1.
10177 NumIterations = SemaRef.BuildBinOp(
10178 S: CurScope, OpLoc: SaveRef.get()->getExprLoc(), Opc: BO_Add, LHSExpr: SaveRef.get(),
10179 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get());
10180 if (!NumIterations.isUsable())
10181 return 0;
10182 }
10183
10184 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
10185
10186 // Build variables passed into runtime, necessary for worksharing directives.
10187 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
10188 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
10189 isOpenMPDistributeDirective(DKind) ||
10190 isOpenMPGenericLoopDirective(DKind) ||
10191 isOpenMPLoopTransformationDirective(DKind)) {
10192 // Lower bound variable, initialized with zero.
10193 VarDecl *LBDecl = buildVarDecl(SemaRef, Loc: InitLoc, Type: VType, Name: ".omp.lb");
10194 LB = buildDeclRefExpr(S&: SemaRef, D: LBDecl, Ty: VType, Loc: InitLoc);
10195 SemaRef.AddInitializerToDecl(dcl: LBDecl,
10196 init: SemaRef.ActOnIntegerConstant(Loc: InitLoc, Val: 0).get(),
10197 /*DirectInit=*/false);
10198
10199 // Upper bound variable, initialized with last iteration number.
10200 VarDecl *UBDecl = buildVarDecl(SemaRef, Loc: InitLoc, Type: VType, Name: ".omp.ub");
10201 UB = buildDeclRefExpr(S&: SemaRef, D: UBDecl, Ty: VType, Loc: InitLoc);
10202 SemaRef.AddInitializerToDecl(dcl: UBDecl, init: LastIteration.get(),
10203 /*DirectInit=*/false);
10204
10205 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
10206 // This will be used to implement clause 'lastprivate'.
10207 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(DestWidth: 32, Signed: true);
10208 VarDecl *ILDecl = buildVarDecl(SemaRef, Loc: InitLoc, Type: Int32Ty, Name: ".omp.is_last");
10209 IL = buildDeclRefExpr(S&: SemaRef, D: ILDecl, Ty: Int32Ty, Loc: InitLoc);
10210 SemaRef.AddInitializerToDecl(dcl: ILDecl,
10211 init: SemaRef.ActOnIntegerConstant(Loc: InitLoc, Val: 0).get(),
10212 /*DirectInit=*/false);
10213
10214 // Stride variable returned by runtime (we initialize it to 1 by default).
10215 VarDecl *STDecl =
10216 buildVarDecl(SemaRef, Loc: InitLoc, Type: StrideVType, Name: ".omp.stride");
10217 ST = buildDeclRefExpr(S&: SemaRef, D: STDecl, Ty: StrideVType, Loc: InitLoc);
10218 SemaRef.AddInitializerToDecl(dcl: STDecl,
10219 init: SemaRef.ActOnIntegerConstant(Loc: InitLoc, Val: 1).get(),
10220 /*DirectInit=*/false);
10221
10222 // Build expression: UB = min(UB, LastIteration)
10223 // It is necessary for CodeGen of directives with static scheduling.
10224 ExprResult IsUBGreater = SemaRef.BuildBinOp(S: CurScope, OpLoc: InitLoc, Opc: BO_GT,
10225 LHSExpr: UB.get(), RHSExpr: LastIteration.get());
10226 ExprResult CondOp = SemaRef.ActOnConditionalOp(
10227 QuestionLoc: LastIteration.get()->getExprLoc(), ColonLoc: InitLoc, CondExpr: IsUBGreater.get(),
10228 LHSExpr: LastIteration.get(), RHSExpr: UB.get());
10229 EUB = SemaRef.BuildBinOp(S: CurScope, OpLoc: InitLoc, Opc: BO_Assign, LHSExpr: UB.get(),
10230 RHSExpr: CondOp.get());
10231 EUB = SemaRef.ActOnFinishFullExpr(Expr: EUB.get(), /*DiscardedValue=*/false);
10232
10233 // If we have a combined directive that combines 'distribute', 'for' or
10234 // 'simd' we need to be able to access the bounds of the schedule of the
10235 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
10236 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
10237 if (isOpenMPLoopBoundSharingDirective(Kind: DKind)) {
10238 // Lower bound variable, initialized with zero.
10239 VarDecl *CombLBDecl =
10240 buildVarDecl(SemaRef, Loc: InitLoc, Type: VType, Name: ".omp.comb.lb");
10241 CombLB = buildDeclRefExpr(S&: SemaRef, D: CombLBDecl, Ty: VType, Loc: InitLoc);
10242 SemaRef.AddInitializerToDecl(
10243 dcl: CombLBDecl, init: SemaRef.ActOnIntegerConstant(Loc: InitLoc, Val: 0).get(),
10244 /*DirectInit=*/false);
10245
10246 // Upper bound variable, initialized with last iteration number.
10247 VarDecl *CombUBDecl =
10248 buildVarDecl(SemaRef, Loc: InitLoc, Type: VType, Name: ".omp.comb.ub");
10249 CombUB = buildDeclRefExpr(S&: SemaRef, D: CombUBDecl, Ty: VType, Loc: InitLoc);
10250 SemaRef.AddInitializerToDecl(dcl: CombUBDecl, init: LastIteration.get(),
10251 /*DirectInit=*/false);
10252
10253 ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
10254 S: CurScope, OpLoc: InitLoc, Opc: BO_GT, LHSExpr: CombUB.get(), RHSExpr: LastIteration.get());
10255 ExprResult CombCondOp =
10256 SemaRef.ActOnConditionalOp(QuestionLoc: InitLoc, ColonLoc: InitLoc, CondExpr: CombIsUBGreater.get(),
10257 LHSExpr: LastIteration.get(), RHSExpr: CombUB.get());
10258 CombEUB = SemaRef.BuildBinOp(S: CurScope, OpLoc: InitLoc, Opc: BO_Assign, LHSExpr: CombUB.get(),
10259 RHSExpr: CombCondOp.get());
10260 CombEUB =
10261 SemaRef.ActOnFinishFullExpr(Expr: CombEUB.get(), /*DiscardedValue=*/false);
10262
10263 const CapturedDecl *CD = cast<CapturedStmt>(Val: AStmt)->getCapturedDecl();
10264 // We expect to have at least 2 more parameters than the 'parallel'
10265 // directive does - the lower and upper bounds of the previous schedule.
10266 assert(CD->getNumParams() >= 4 &&
10267 "Unexpected number of parameters in loop combined directive");
10268
10269 // Set the proper type for the bounds given what we learned from the
10270 // enclosed loops.
10271 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/i: 2);
10272 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/i: 3);
10273
10274 // Previous lower and upper bounds are obtained from the region
10275 // parameters.
10276 PrevLB =
10277 buildDeclRefExpr(S&: SemaRef, D: PrevLBDecl, Ty: PrevLBDecl->getType(), Loc: InitLoc);
10278 PrevUB =
10279 buildDeclRefExpr(S&: SemaRef, D: PrevUBDecl, Ty: PrevUBDecl->getType(), Loc: InitLoc);
10280 }
10281 }
10282
10283 // Build the iteration variable and its initialization before loop.
10284 ExprResult IV;
10285 ExprResult Init, CombInit;
10286 {
10287 VarDecl *IVDecl = buildVarDecl(SemaRef, Loc: InitLoc, Type: RealVType, Name: ".omp.iv");
10288 IV = buildDeclRefExpr(S&: SemaRef, D: IVDecl, Ty: RealVType, Loc: InitLoc);
10289 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
10290 isOpenMPGenericLoopDirective(DKind) ||
10291 isOpenMPTaskLoopDirective(DKind) ||
10292 isOpenMPDistributeDirective(DKind) ||
10293 isOpenMPLoopTransformationDirective(DKind))
10294 ? LB.get()
10295 : SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 0).get();
10296 Init = SemaRef.BuildBinOp(S: CurScope, OpLoc: InitLoc, Opc: BO_Assign, LHSExpr: IV.get(), RHSExpr: RHS);
10297 Init = SemaRef.ActOnFinishFullExpr(Expr: Init.get(), /*DiscardedValue=*/false);
10298
10299 if (isOpenMPLoopBoundSharingDirective(Kind: DKind)) {
10300 Expr *CombRHS =
10301 (isOpenMPWorksharingDirective(DKind) ||
10302 isOpenMPGenericLoopDirective(DKind) ||
10303 isOpenMPTaskLoopDirective(DKind) ||
10304 isOpenMPDistributeDirective(DKind))
10305 ? CombLB.get()
10306 : SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 0).get();
10307 CombInit =
10308 SemaRef.BuildBinOp(S: CurScope, OpLoc: InitLoc, Opc: BO_Assign, LHSExpr: IV.get(), RHSExpr: CombRHS);
10309 CombInit =
10310 SemaRef.ActOnFinishFullExpr(Expr: CombInit.get(), /*DiscardedValue=*/false);
10311 }
10312 }
10313
10314 bool UseStrictCompare =
10315 RealVType->hasUnsignedIntegerRepresentation() &&
10316 llvm::all_of(Range&: IterSpaces, P: [](const LoopIterationSpace &LIS) {
10317 return LIS.IsStrictCompare;
10318 });
10319 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for
10320 // unsigned IV)) for worksharing loops.
10321 SourceLocation CondLoc = AStmt->getBeginLoc();
10322 Expr *BoundUB = UB.get();
10323 if (UseStrictCompare) {
10324 BoundUB =
10325 SemaRef
10326 .BuildBinOp(S: CurScope, OpLoc: CondLoc, Opc: BO_Add, LHSExpr: BoundUB,
10327 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get())
10328 .get();
10329 BoundUB =
10330 SemaRef.ActOnFinishFullExpr(Expr: BoundUB, /*DiscardedValue=*/false).get();
10331 }
10332 ExprResult Cond =
10333 (isOpenMPWorksharingDirective(DKind) ||
10334 isOpenMPGenericLoopDirective(DKind) ||
10335 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind) ||
10336 isOpenMPLoopTransformationDirective(DKind))
10337 ? SemaRef.BuildBinOp(S: CurScope, OpLoc: CondLoc,
10338 Opc: UseStrictCompare ? BO_LT : BO_LE, LHSExpr: IV.get(),
10339 RHSExpr: BoundUB)
10340 : SemaRef.BuildBinOp(S: CurScope, OpLoc: CondLoc, Opc: BO_LT, LHSExpr: IV.get(),
10341 RHSExpr: NumIterations.get());
10342 ExprResult CombDistCond;
10343 if (isOpenMPLoopBoundSharingDirective(Kind: DKind)) {
10344 CombDistCond = SemaRef.BuildBinOp(S: CurScope, OpLoc: CondLoc, Opc: BO_LT, LHSExpr: IV.get(),
10345 RHSExpr: NumIterations.get());
10346 }
10347
10348 ExprResult CombCond;
10349 if (isOpenMPLoopBoundSharingDirective(Kind: DKind)) {
10350 Expr *BoundCombUB = CombUB.get();
10351 if (UseStrictCompare) {
10352 BoundCombUB =
10353 SemaRef
10354 .BuildBinOp(
10355 S: CurScope, OpLoc: CondLoc, Opc: BO_Add, LHSExpr: BoundCombUB,
10356 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get())
10357 .get();
10358 BoundCombUB =
10359 SemaRef.ActOnFinishFullExpr(Expr: BoundCombUB, /*DiscardedValue=*/false)
10360 .get();
10361 }
10362 CombCond =
10363 SemaRef.BuildBinOp(S: CurScope, OpLoc: CondLoc, Opc: UseStrictCompare ? BO_LT : BO_LE,
10364 LHSExpr: IV.get(), RHSExpr: BoundCombUB);
10365 }
10366 // Loop increment (IV = IV + 1)
10367 SourceLocation IncLoc = AStmt->getBeginLoc();
10368 ExprResult Inc =
10369 SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Add, LHSExpr: IV.get(),
10370 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: IncLoc, Val: 1).get());
10371 if (!Inc.isUsable())
10372 return 0;
10373 Inc = SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Assign, LHSExpr: IV.get(), RHSExpr: Inc.get());
10374 Inc = SemaRef.ActOnFinishFullExpr(Expr: Inc.get(), /*DiscardedValue=*/false);
10375 if (!Inc.isUsable())
10376 return 0;
10377
10378 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
10379 // Used for directives with static scheduling.
10380 // In combined construct, add combined version that use CombLB and CombUB
10381 // base variables for the update
10382 ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
10383 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
10384 isOpenMPGenericLoopDirective(DKind) ||
10385 isOpenMPDistributeDirective(DKind) ||
10386 isOpenMPLoopTransformationDirective(DKind)) {
10387 // LB + ST
10388 NextLB = SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Add, LHSExpr: LB.get(), RHSExpr: ST.get());
10389 if (!NextLB.isUsable())
10390 return 0;
10391 // LB = LB + ST
10392 NextLB =
10393 SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Assign, LHSExpr: LB.get(), RHSExpr: NextLB.get());
10394 NextLB =
10395 SemaRef.ActOnFinishFullExpr(Expr: NextLB.get(), /*DiscardedValue=*/false);
10396 if (!NextLB.isUsable())
10397 return 0;
10398 // UB + ST
10399 NextUB = SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Add, LHSExpr: UB.get(), RHSExpr: ST.get());
10400 if (!NextUB.isUsable())
10401 return 0;
10402 // UB = UB + ST
10403 NextUB =
10404 SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Assign, LHSExpr: UB.get(), RHSExpr: NextUB.get());
10405 NextUB =
10406 SemaRef.ActOnFinishFullExpr(Expr: NextUB.get(), /*DiscardedValue=*/false);
10407 if (!NextUB.isUsable())
10408 return 0;
10409 if (isOpenMPLoopBoundSharingDirective(Kind: DKind)) {
10410 CombNextLB =
10411 SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Add, LHSExpr: CombLB.get(), RHSExpr: ST.get());
10412 if (!NextLB.isUsable())
10413 return 0;
10414 // LB = LB + ST
10415 CombNextLB = SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Assign, LHSExpr: CombLB.get(),
10416 RHSExpr: CombNextLB.get());
10417 CombNextLB = SemaRef.ActOnFinishFullExpr(Expr: CombNextLB.get(),
10418 /*DiscardedValue=*/false);
10419 if (!CombNextLB.isUsable())
10420 return 0;
10421 // UB + ST
10422 CombNextUB =
10423 SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Add, LHSExpr: CombUB.get(), RHSExpr: ST.get());
10424 if (!CombNextUB.isUsable())
10425 return 0;
10426 // UB = UB + ST
10427 CombNextUB = SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Assign, LHSExpr: CombUB.get(),
10428 RHSExpr: CombNextUB.get());
10429 CombNextUB = SemaRef.ActOnFinishFullExpr(Expr: CombNextUB.get(),
10430 /*DiscardedValue=*/false);
10431 if (!CombNextUB.isUsable())
10432 return 0;
10433 }
10434 }
10435
10436 // Create increment expression for distribute loop when combined in a same
10437 // directive with for as IV = IV + ST; ensure upper bound expression based
10438 // on PrevUB instead of NumIterations - used to implement 'for' when found
10439 // in combination with 'distribute', like in 'distribute parallel for'
10440 SourceLocation DistIncLoc = AStmt->getBeginLoc();
10441 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond;
10442 if (isOpenMPLoopBoundSharingDirective(Kind: DKind)) {
10443 DistCond = SemaRef.BuildBinOp(
10444 S: CurScope, OpLoc: CondLoc, Opc: UseStrictCompare ? BO_LT : BO_LE, LHSExpr: IV.get(), RHSExpr: BoundUB);
10445 assert(DistCond.isUsable() && "distribute cond expr was not built");
10446
10447 DistInc =
10448 SemaRef.BuildBinOp(S: CurScope, OpLoc: DistIncLoc, Opc: BO_Add, LHSExpr: IV.get(), RHSExpr: ST.get());
10449 assert(DistInc.isUsable() && "distribute inc expr was not built");
10450 DistInc = SemaRef.BuildBinOp(S: CurScope, OpLoc: DistIncLoc, Opc: BO_Assign, LHSExpr: IV.get(),
10451 RHSExpr: DistInc.get());
10452 DistInc =
10453 SemaRef.ActOnFinishFullExpr(Expr: DistInc.get(), /*DiscardedValue=*/false);
10454 assert(DistInc.isUsable() && "distribute inc expr was not built");
10455
10456 // Build expression: UB = min(UB, prevUB) for #for in composite or combined
10457 // construct
10458 ExprResult NewPrevUB = PrevUB;
10459 SourceLocation DistEUBLoc = AStmt->getBeginLoc();
10460 if (!SemaRef.Context.hasSameType(T1: UB.get()->getType(),
10461 T2: PrevUB.get()->getType())) {
10462 NewPrevUB = SemaRef.BuildCStyleCastExpr(
10463 LParenLoc: DistEUBLoc,
10464 Ty: SemaRef.Context.getTrivialTypeSourceInfo(T: UB.get()->getType()),
10465 RParenLoc: DistEUBLoc, Op: NewPrevUB.get());
10466 if (!NewPrevUB.isUsable())
10467 return 0;
10468 }
10469 ExprResult IsUBGreater = SemaRef.BuildBinOp(S: CurScope, OpLoc: DistEUBLoc, Opc: BO_GT,
10470 LHSExpr: UB.get(), RHSExpr: NewPrevUB.get());
10471 ExprResult CondOp = SemaRef.ActOnConditionalOp(
10472 QuestionLoc: DistEUBLoc, ColonLoc: DistEUBLoc, CondExpr: IsUBGreater.get(), LHSExpr: NewPrevUB.get(), RHSExpr: UB.get());
10473 PrevEUB = SemaRef.BuildBinOp(S: CurScope, OpLoc: DistIncLoc, Opc: BO_Assign, LHSExpr: UB.get(),
10474 RHSExpr: CondOp.get());
10475 PrevEUB =
10476 SemaRef.ActOnFinishFullExpr(Expr: PrevEUB.get(), /*DiscardedValue=*/false);
10477
10478 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in
10479 // parallel for is in combination with a distribute directive with
10480 // schedule(static, 1)
10481 Expr *BoundPrevUB = PrevUB.get();
10482 if (UseStrictCompare) {
10483 BoundPrevUB =
10484 SemaRef
10485 .BuildBinOp(
10486 S: CurScope, OpLoc: CondLoc, Opc: BO_Add, LHSExpr: BoundPrevUB,
10487 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get())
10488 .get();
10489 BoundPrevUB =
10490 SemaRef.ActOnFinishFullExpr(Expr: BoundPrevUB, /*DiscardedValue=*/false)
10491 .get();
10492 }
10493 ParForInDistCond =
10494 SemaRef.BuildBinOp(S: CurScope, OpLoc: CondLoc, Opc: UseStrictCompare ? BO_LT : BO_LE,
10495 LHSExpr: IV.get(), RHSExpr: BoundPrevUB);
10496 }
10497
10498 // Build updates and final values of the loop counters.
10499 bool HasErrors = false;
10500 Built.Counters.resize(N: NestedLoopCount);
10501 Built.Inits.resize(N: NestedLoopCount);
10502 Built.Updates.resize(N: NestedLoopCount);
10503 Built.Finals.resize(N: NestedLoopCount);
10504 Built.DependentCounters.resize(N: NestedLoopCount);
10505 Built.DependentInits.resize(N: NestedLoopCount);
10506 Built.FinalsConditions.resize(N: NestedLoopCount);
10507 {
10508 // We implement the following algorithm for obtaining the
10509 // original loop iteration variable values based on the
10510 // value of the collapsed loop iteration variable IV.
10511 //
10512 // Let n+1 be the number of collapsed loops in the nest.
10513 // Iteration variables (I0, I1, .... In)
10514 // Iteration counts (N0, N1, ... Nn)
10515 //
10516 // Acc = IV;
10517 //
10518 // To compute Ik for loop k, 0 <= k <= n, generate:
10519 // Prod = N(k+1) * N(k+2) * ... * Nn;
10520 // Ik = Acc / Prod;
10521 // Acc -= Ik * Prod;
10522 //
10523 ExprResult Acc = IV;
10524 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
10525 LoopIterationSpace &IS = IterSpaces[Cnt];
10526 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
10527 ExprResult Iter;
10528
10529 // Compute prod
10530 ExprResult Prod = SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get();
10531 for (unsigned int K = Cnt + 1; K < NestedLoopCount; ++K)
10532 Prod = SemaRef.BuildBinOp(S: CurScope, OpLoc: UpdLoc, Opc: BO_Mul, LHSExpr: Prod.get(),
10533 RHSExpr: IterSpaces[K].NumIterations);
10534
10535 // Iter = Acc / Prod
10536 // If there is at least one more inner loop to avoid
10537 // multiplication by 1.
10538 if (Cnt + 1 < NestedLoopCount)
10539 Iter =
10540 SemaRef.BuildBinOp(S: CurScope, OpLoc: UpdLoc, Opc: BO_Div, LHSExpr: Acc.get(), RHSExpr: Prod.get());
10541 else
10542 Iter = Acc;
10543 if (!Iter.isUsable()) {
10544 HasErrors = true;
10545 break;
10546 }
10547
10548 // Update Acc:
10549 // Acc -= Iter * Prod
10550 // Check if there is at least one more inner loop to avoid
10551 // multiplication by 1.
10552 if (Cnt + 1 < NestedLoopCount)
10553 Prod = SemaRef.BuildBinOp(S: CurScope, OpLoc: UpdLoc, Opc: BO_Mul, LHSExpr: Iter.get(),
10554 RHSExpr: Prod.get());
10555 else
10556 Prod = Iter;
10557 Acc = SemaRef.BuildBinOp(S: CurScope, OpLoc: UpdLoc, Opc: BO_Sub, LHSExpr: Acc.get(), RHSExpr: Prod.get());
10558
10559 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
10560 auto *VD = cast<VarDecl>(Val: cast<DeclRefExpr>(Val: IS.CounterVar)->getDecl());
10561 DeclRefExpr *CounterVar = buildDeclRefExpr(
10562 S&: SemaRef, D: VD, Ty: IS.CounterVar->getType(), Loc: IS.CounterVar->getExprLoc(),
10563 /*RefersToCapture=*/true);
10564 ExprResult Init =
10565 buildCounterInit(SemaRef, S: CurScope, Loc: UpdLoc, VarRef: CounterVar,
10566 Start: IS.CounterInit, IsNonRectangularLB: IS.IsNonRectangularLB, Captures);
10567 if (!Init.isUsable()) {
10568 HasErrors = true;
10569 break;
10570 }
10571 ExprResult Update = buildCounterUpdate(
10572 SemaRef, S: CurScope, Loc: UpdLoc, VarRef: CounterVar, Start: IS.CounterInit, Iter,
10573 Step: IS.CounterStep, Subtract: IS.Subtract, IsNonRectangularLB: IS.IsNonRectangularLB, Captures: &Captures);
10574 if (!Update.isUsable()) {
10575 HasErrors = true;
10576 break;
10577 }
10578
10579 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
10580 ExprResult Final =
10581 buildCounterUpdate(SemaRef, S: CurScope, Loc: UpdLoc, VarRef: CounterVar,
10582 Start: IS.CounterInit, Iter: IS.NumIterations, Step: IS.CounterStep,
10583 Subtract: IS.Subtract, IsNonRectangularLB: IS.IsNonRectangularLB, Captures: &Captures);
10584 if (!Final.isUsable()) {
10585 HasErrors = true;
10586 break;
10587 }
10588
10589 if (!Update.isUsable() || !Final.isUsable()) {
10590 HasErrors = true;
10591 break;
10592 }
10593 // Save results
10594 Built.Counters[Cnt] = IS.CounterVar;
10595 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
10596 Built.Inits[Cnt] = Init.get();
10597 Built.Updates[Cnt] = Update.get();
10598 Built.Finals[Cnt] = Final.get();
10599 Built.DependentCounters[Cnt] = nullptr;
10600 Built.DependentInits[Cnt] = nullptr;
10601 Built.FinalsConditions[Cnt] = nullptr;
10602 if (IS.IsNonRectangularLB || IS.IsNonRectangularUB) {
10603 Built.DependentCounters[Cnt] = Built.Counters[IS.LoopDependentIdx - 1];
10604 Built.DependentInits[Cnt] = Built.Inits[IS.LoopDependentIdx - 1];
10605 Built.FinalsConditions[Cnt] = IS.FinalCondition;
10606 }
10607 }
10608 }
10609
10610 if (HasErrors)
10611 return 0;
10612
10613 // Save results
10614 Built.IterationVarRef = IV.get();
10615 Built.LastIteration = LastIteration.get();
10616 Built.NumIterations = NumIterations.get();
10617 Built.CalcLastIteration = SemaRef
10618 .ActOnFinishFullExpr(Expr: CalcLastIteration.get(),
10619 /*DiscardedValue=*/false)
10620 .get();
10621 Built.PreCond = PreCond.get();
10622 Built.PreInits = buildPreInits(Context&: C, Captures);
10623 Built.Cond = Cond.get();
10624 Built.Init = Init.get();
10625 Built.Inc = Inc.get();
10626 Built.LB = LB.get();
10627 Built.UB = UB.get();
10628 Built.IL = IL.get();
10629 Built.ST = ST.get();
10630 Built.EUB = EUB.get();
10631 Built.NLB = NextLB.get();
10632 Built.NUB = NextUB.get();
10633 Built.PrevLB = PrevLB.get();
10634 Built.PrevUB = PrevUB.get();
10635 Built.DistInc = DistInc.get();
10636 Built.PrevEUB = PrevEUB.get();
10637 Built.DistCombinedFields.LB = CombLB.get();
10638 Built.DistCombinedFields.UB = CombUB.get();
10639 Built.DistCombinedFields.EUB = CombEUB.get();
10640 Built.DistCombinedFields.Init = CombInit.get();
10641 Built.DistCombinedFields.Cond = CombCond.get();
10642 Built.DistCombinedFields.NLB = CombNextLB.get();
10643 Built.DistCombinedFields.NUB = CombNextUB.get();
10644 Built.DistCombinedFields.DistCond = CombDistCond.get();
10645 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get();
10646
10647 return NestedLoopCount;
10648}
10649
10650static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
10651 auto CollapseClauses =
10652 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
10653 if (CollapseClauses.begin() != CollapseClauses.end())
10654 return (*CollapseClauses.begin())->getNumForLoops();
10655 return nullptr;
10656}
10657
10658static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
10659 auto OrderedClauses =
10660 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
10661 if (OrderedClauses.begin() != OrderedClauses.end())
10662 return (*OrderedClauses.begin())->getNumForLoops();
10663 return nullptr;
10664}
10665
10666static bool checkSimdlenSafelenSpecified(Sema &S,
10667 const ArrayRef<OMPClause *> Clauses) {
10668 const OMPSafelenClause *Safelen = nullptr;
10669 const OMPSimdlenClause *Simdlen = nullptr;
10670
10671 for (const OMPClause *Clause : Clauses) {
10672 if (Clause->getClauseKind() == OMPC_safelen)
10673 Safelen = cast<OMPSafelenClause>(Val: Clause);
10674 else if (Clause->getClauseKind() == OMPC_simdlen)
10675 Simdlen = cast<OMPSimdlenClause>(Val: Clause);
10676 if (Safelen && Simdlen)
10677 break;
10678 }
10679
10680 if (Simdlen && Safelen) {
10681 const Expr *SimdlenLength = Simdlen->getSimdlen();
10682 const Expr *SafelenLength = Safelen->getSafelen();
10683 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
10684 SimdlenLength->isInstantiationDependent() ||
10685 SimdlenLength->containsUnexpandedParameterPack())
10686 return false;
10687 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
10688 SafelenLength->isInstantiationDependent() ||
10689 SafelenLength->containsUnexpandedParameterPack())
10690 return false;
10691 Expr::EvalResult SimdlenResult, SafelenResult;
10692 SimdlenLength->EvaluateAsInt(Result&: SimdlenResult, Ctx: S.Context);
10693 SafelenLength->EvaluateAsInt(Result&: SafelenResult, Ctx: S.Context);
10694 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt();
10695 llvm::APSInt SafelenRes = SafelenResult.Val.getInt();
10696 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
10697 // If both simdlen and safelen clauses are specified, the value of the
10698 // simdlen parameter must be less than or equal to the value of the safelen
10699 // parameter.
10700 if (SimdlenRes > SafelenRes) {
10701 S.Diag(Loc: SimdlenLength->getExprLoc(),
10702 DiagID: diag::err_omp_wrong_simdlen_safelen_values)
10703 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
10704 return true;
10705 }
10706 }
10707 return false;
10708}
10709
10710StmtResult SemaOpenMP::ActOnOpenMPSimdDirective(
10711 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10712 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10713 if (!AStmt)
10714 return StmtError();
10715
10716 CapturedStmt *CS = setBranchProtectedScope(SemaRef, DKind: OMPD_simd, AStmt);
10717
10718 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10719 OMPLoopBasedDirective::HelperExprs B;
10720 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
10721 // define the nested loops number.
10722 unsigned NestedLoopCount = checkOpenMPLoop(
10723 DKind: OMPD_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses), OrderedLoopCountExpr: getOrderedNumberExpr(Clauses),
10724 AStmt: CS, SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
10725 if (NestedLoopCount == 0)
10726 return StmtError();
10727
10728 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
10729 return StmtError();
10730
10731 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
10732 return StmtError();
10733
10734 auto *SimdDirective = OMPSimdDirective::Create(
10735 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
10736 return SimdDirective;
10737}
10738
10739StmtResult SemaOpenMP::ActOnOpenMPForDirective(
10740 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10741 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10742 if (!AStmt)
10743 return StmtError();
10744
10745 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10746 OMPLoopBasedDirective::HelperExprs B;
10747 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
10748 // define the nested loops number.
10749 unsigned NestedLoopCount = checkOpenMPLoop(
10750 DKind: OMPD_for, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses), OrderedLoopCountExpr: getOrderedNumberExpr(Clauses),
10751 AStmt, SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
10752 if (NestedLoopCount == 0)
10753 return StmtError();
10754
10755 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
10756 return StmtError();
10757
10758 auto *ForDirective = OMPForDirective::Create(
10759 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
10760 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
10761 return ForDirective;
10762}
10763
10764StmtResult SemaOpenMP::ActOnOpenMPForSimdDirective(
10765 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10766 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10767 if (!AStmt)
10768 return StmtError();
10769
10770 CapturedStmt *CS = setBranchProtectedScope(SemaRef, DKind: OMPD_for_simd, AStmt);
10771
10772 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10773 OMPLoopBasedDirective::HelperExprs B;
10774 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
10775 // define the nested loops number.
10776 unsigned NestedLoopCount =
10777 checkOpenMPLoop(DKind: OMPD_for_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
10778 OrderedLoopCountExpr: getOrderedNumberExpr(Clauses), AStmt: CS, SemaRef, DSA&: *DSAStack,
10779 VarsWithImplicitDSA, Built&: B);
10780 if (NestedLoopCount == 0)
10781 return StmtError();
10782
10783 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
10784 return StmtError();
10785
10786 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
10787 return StmtError();
10788
10789 return OMPForSimdDirective::Create(C: getASTContext(), StartLoc, EndLoc,
10790 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
10791}
10792
10793static bool checkSectionsDirective(Sema &SemaRef, OpenMPDirectiveKind DKind,
10794 Stmt *AStmt, DSAStackTy *Stack) {
10795 if (!AStmt)
10796 return true;
10797
10798 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10799 unsigned OMPVersion = SemaRef.getLangOpts().OpenMP;
10800 auto BaseStmt = AStmt;
10801 while (auto *CS = dyn_cast_or_null<CapturedStmt>(Val: BaseStmt))
10802 BaseStmt = CS->getCapturedStmt();
10803 if (auto *C = dyn_cast_or_null<CompoundStmt>(Val: BaseStmt)) {
10804 auto S = C->children();
10805 if (S.begin() == S.end())
10806 return true;
10807 // All associated statements must be '#pragma omp section' except for
10808 // the first one.
10809 for (Stmt *SectionStmt : llvm::drop_begin(RangeOrContainer&: S)) {
10810 if (!SectionStmt || !isa<OMPSectionDirective>(Val: SectionStmt)) {
10811 if (SectionStmt)
10812 SemaRef.Diag(Loc: SectionStmt->getBeginLoc(),
10813 DiagID: diag::err_omp_sections_substmt_not_section)
10814 << getOpenMPDirectiveName(D: DKind, Ver: OMPVersion);
10815 return true;
10816 }
10817 cast<OMPSectionDirective>(Val: SectionStmt)
10818 ->setHasCancel(Stack->isCancelRegion());
10819 }
10820 } else {
10821 SemaRef.Diag(Loc: AStmt->getBeginLoc(), DiagID: diag::err_omp_sections_not_compound_stmt)
10822 << getOpenMPDirectiveName(D: DKind, Ver: OMPVersion);
10823 return true;
10824 }
10825 return false;
10826}
10827
10828StmtResult
10829SemaOpenMP::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
10830 Stmt *AStmt, SourceLocation StartLoc,
10831 SourceLocation EndLoc) {
10832 if (checkSectionsDirective(SemaRef, DKind: OMPD_sections, AStmt, DSAStack))
10833 return StmtError();
10834
10835 SemaRef.setFunctionHasBranchProtectedScope();
10836
10837 return OMPSectionsDirective::Create(
10838 C: getASTContext(), StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
10839 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
10840}
10841
10842StmtResult SemaOpenMP::ActOnOpenMPSectionDirective(Stmt *AStmt,
10843 SourceLocation StartLoc,
10844 SourceLocation EndLoc) {
10845 if (!AStmt)
10846 return StmtError();
10847
10848 SemaRef.setFunctionHasBranchProtectedScope();
10849 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
10850
10851 return OMPSectionDirective::Create(C: getASTContext(), StartLoc, EndLoc, AssociatedStmt: AStmt,
10852 DSAStack->isCancelRegion());
10853}
10854
10855static Expr *getDirectCallExpr(Expr *E) {
10856 E = E->IgnoreParenCasts()->IgnoreImplicit();
10857 if (auto *CE = dyn_cast<CallExpr>(Val: E))
10858 if (CE->getDirectCallee())
10859 return E;
10860 return nullptr;
10861}
10862
10863StmtResult
10864SemaOpenMP::ActOnOpenMPDispatchDirective(ArrayRef<OMPClause *> Clauses,
10865 Stmt *AStmt, SourceLocation StartLoc,
10866 SourceLocation EndLoc) {
10867 if (!AStmt)
10868 return StmtError();
10869
10870 Stmt *S = cast<CapturedStmt>(Val: AStmt)->getCapturedStmt();
10871
10872 // 5.1 OpenMP
10873 // expression-stmt : an expression statement with one of the following forms:
10874 // expression = target-call ( [expression-list] );
10875 // target-call ( [expression-list] );
10876
10877 SourceLocation TargetCallLoc;
10878
10879 if (!SemaRef.CurContext->isDependentContext()) {
10880 Expr *TargetCall = nullptr;
10881
10882 auto *E = dyn_cast<Expr>(Val: S);
10883 if (!E) {
10884 Diag(Loc: S->getBeginLoc(), DiagID: diag::err_omp_dispatch_statement_call);
10885 return StmtError();
10886 }
10887
10888 E = E->IgnoreParenCasts()->IgnoreImplicit();
10889
10890 if (auto *BO = dyn_cast<BinaryOperator>(Val: E)) {
10891 if (BO->getOpcode() == BO_Assign)
10892 TargetCall = getDirectCallExpr(E: BO->getRHS());
10893 } else {
10894 if (auto *COCE = dyn_cast<CXXOperatorCallExpr>(Val: E))
10895 if (COCE->getOperator() == OO_Equal)
10896 TargetCall = getDirectCallExpr(E: COCE->getArg(Arg: 1));
10897 if (!TargetCall)
10898 TargetCall = getDirectCallExpr(E);
10899 }
10900 if (!TargetCall) {
10901 Diag(Loc: E->getBeginLoc(), DiagID: diag::err_omp_dispatch_statement_call);
10902 return StmtError();
10903 }
10904 TargetCallLoc = TargetCall->getExprLoc();
10905 }
10906
10907 SemaRef.setFunctionHasBranchProtectedScope();
10908
10909 return OMPDispatchDirective::Create(C: getASTContext(), StartLoc, EndLoc,
10910 Clauses, AssociatedStmt: AStmt, TargetCallLoc);
10911}
10912
10913static bool checkGenericLoopLastprivate(Sema &S, ArrayRef<OMPClause *> Clauses,
10914 OpenMPDirectiveKind K,
10915 DSAStackTy *Stack) {
10916 bool ErrorFound = false;
10917 for (OMPClause *C : Clauses) {
10918 if (auto *LPC = dyn_cast<OMPLastprivateClause>(Val: C)) {
10919 for (Expr *RefExpr : LPC->varlist()) {
10920 SourceLocation ELoc;
10921 SourceRange ERange;
10922 Expr *SimpleRefExpr = RefExpr;
10923 auto Res = getPrivateItem(S, RefExpr&: SimpleRefExpr, ELoc, ERange);
10924 if (ValueDecl *D = Res.first) {
10925 auto &&Info = Stack->isLoopControlVariable(D);
10926 if (!Info.first) {
10927 unsigned OMPVersion = S.getLangOpts().OpenMP;
10928 S.Diag(Loc: ELoc, DiagID: diag::err_omp_lastprivate_loop_var_non_loop_iteration)
10929 << getOpenMPDirectiveName(D: K, Ver: OMPVersion);
10930 ErrorFound = true;
10931 }
10932 }
10933 }
10934 }
10935 }
10936 return ErrorFound;
10937}
10938
10939StmtResult SemaOpenMP::ActOnOpenMPGenericLoopDirective(
10940 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10941 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10942 if (!AStmt)
10943 return StmtError();
10944
10945 // OpenMP 5.1 [2.11.7, loop construct, Restrictions]
10946 // A list item may not appear in a lastprivate clause unless it is the
10947 // loop iteration variable of a loop that is associated with the construct.
10948 if (checkGenericLoopLastprivate(S&: SemaRef, Clauses, K: OMPD_loop, DSAStack))
10949 return StmtError();
10950
10951 setBranchProtectedScope(SemaRef, DKind: OMPD_loop, AStmt);
10952
10953 OMPLoopDirective::HelperExprs B;
10954 // In presence of clause 'collapse', it will define the nested loops number.
10955 unsigned NestedLoopCount = checkOpenMPLoop(
10956 DKind: OMPD_loop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses), OrderedLoopCountExpr: getOrderedNumberExpr(Clauses),
10957 AStmt, SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
10958 if (NestedLoopCount == 0)
10959 return StmtError();
10960
10961 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
10962 "omp loop exprs were not built");
10963
10964 return OMPGenericLoopDirective::Create(C: getASTContext(), StartLoc, EndLoc,
10965 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
10966}
10967
10968StmtResult SemaOpenMP::ActOnOpenMPTeamsGenericLoopDirective(
10969 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10970 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10971 if (!AStmt)
10972 return StmtError();
10973
10974 // OpenMP 5.1 [2.11.7, loop construct, Restrictions]
10975 // A list item may not appear in a lastprivate clause unless it is the
10976 // loop iteration variable of a loop that is associated with the construct.
10977 if (checkGenericLoopLastprivate(S&: SemaRef, Clauses, K: OMPD_teams_loop, DSAStack))
10978 return StmtError();
10979
10980 CapturedStmt *CS = setBranchProtectedScope(SemaRef, DKind: OMPD_teams_loop, AStmt);
10981
10982 OMPLoopDirective::HelperExprs B;
10983 // In presence of clause 'collapse', it will define the nested loops number.
10984 unsigned NestedLoopCount =
10985 checkOpenMPLoop(DKind: OMPD_teams_loop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
10986 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
10987 VarsWithImplicitDSA, Built&: B);
10988 if (NestedLoopCount == 0)
10989 return StmtError();
10990
10991 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
10992 "omp loop exprs were not built");
10993
10994 DSAStack->setParentTeamsRegionLoc(StartLoc);
10995
10996 return OMPTeamsGenericLoopDirective::Create(
10997 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
10998}
10999
11000StmtResult SemaOpenMP::ActOnOpenMPTargetTeamsGenericLoopDirective(
11001 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11002 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11003 if (!AStmt)
11004 return StmtError();
11005
11006 // OpenMP 5.1 [2.11.7, loop construct, Restrictions]
11007 // A list item may not appear in a lastprivate clause unless it is the
11008 // loop iteration variable of a loop that is associated with the construct.
11009 if (checkGenericLoopLastprivate(S&: SemaRef, Clauses, K: OMPD_target_teams_loop,
11010 DSAStack))
11011 return StmtError();
11012
11013 CapturedStmt *CS =
11014 setBranchProtectedScope(SemaRef, DKind: OMPD_target_teams_loop, AStmt);
11015
11016 OMPLoopDirective::HelperExprs B;
11017 // In presence of clause 'collapse', it will define the nested loops number.
11018 unsigned NestedLoopCount =
11019 checkOpenMPLoop(DKind: OMPD_target_teams_loop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
11020 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
11021 VarsWithImplicitDSA, Built&: B);
11022 if (NestedLoopCount == 0)
11023 return StmtError();
11024
11025 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
11026 "omp loop exprs were not built");
11027
11028 return OMPTargetTeamsGenericLoopDirective::Create(
11029 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
11030 CanBeParallelFor: teamsLoopCanBeParallelFor(AStmt, SemaRef));
11031}
11032
11033StmtResult SemaOpenMP::ActOnOpenMPParallelGenericLoopDirective(
11034 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11035 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11036 if (!AStmt)
11037 return StmtError();
11038
11039 // OpenMP 5.1 [2.11.7, loop construct, Restrictions]
11040 // A list item may not appear in a lastprivate clause unless it is the
11041 // loop iteration variable of a loop that is associated with the construct.
11042 if (checkGenericLoopLastprivate(S&: SemaRef, Clauses, K: OMPD_parallel_loop,
11043 DSAStack))
11044 return StmtError();
11045
11046 CapturedStmt *CS =
11047 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_loop, AStmt);
11048
11049 OMPLoopDirective::HelperExprs B;
11050 // In presence of clause 'collapse', it will define the nested loops number.
11051 unsigned NestedLoopCount =
11052 checkOpenMPLoop(DKind: OMPD_parallel_loop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
11053 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
11054 VarsWithImplicitDSA, Built&: B);
11055 if (NestedLoopCount == 0)
11056 return StmtError();
11057
11058 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
11059 "omp loop exprs were not built");
11060
11061 return OMPParallelGenericLoopDirective::Create(
11062 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
11063}
11064
11065StmtResult SemaOpenMP::ActOnOpenMPTargetParallelGenericLoopDirective(
11066 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11067 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11068 if (!AStmt)
11069 return StmtError();
11070
11071 // OpenMP 5.1 [2.11.7, loop construct, Restrictions]
11072 // A list item may not appear in a lastprivate clause unless it is the
11073 // loop iteration variable of a loop that is associated with the construct.
11074 if (checkGenericLoopLastprivate(S&: SemaRef, Clauses, K: OMPD_target_parallel_loop,
11075 DSAStack))
11076 return StmtError();
11077
11078 CapturedStmt *CS =
11079 setBranchProtectedScope(SemaRef, DKind: OMPD_target_parallel_loop, AStmt);
11080
11081 OMPLoopDirective::HelperExprs B;
11082 // In presence of clause 'collapse', it will define the nested loops number.
11083 unsigned NestedLoopCount =
11084 checkOpenMPLoop(DKind: OMPD_target_parallel_loop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
11085 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
11086 VarsWithImplicitDSA, Built&: B);
11087 if (NestedLoopCount == 0)
11088 return StmtError();
11089
11090 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
11091 "omp loop exprs were not built");
11092
11093 return OMPTargetParallelGenericLoopDirective::Create(
11094 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
11095}
11096
11097StmtResult SemaOpenMP::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
11098 Stmt *AStmt,
11099 SourceLocation StartLoc,
11100 SourceLocation EndLoc) {
11101 if (!AStmt)
11102 return StmtError();
11103
11104 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11105
11106 SemaRef.setFunctionHasBranchProtectedScope();
11107
11108 // OpenMP [2.7.3, single Construct, Restrictions]
11109 // The copyprivate clause must not be used with the nowait clause.
11110 const OMPClause *Nowait = nullptr;
11111 const OMPClause *Copyprivate = nullptr;
11112 for (const OMPClause *Clause : Clauses) {
11113 if (Clause->getClauseKind() == OMPC_nowait)
11114 Nowait = Clause;
11115 else if (Clause->getClauseKind() == OMPC_copyprivate)
11116 Copyprivate = Clause;
11117 if (Copyprivate && Nowait) {
11118 Diag(Loc: Copyprivate->getBeginLoc(),
11119 DiagID: diag::err_omp_single_copyprivate_with_nowait);
11120 Diag(Loc: Nowait->getBeginLoc(), DiagID: diag::note_omp_nowait_clause_here);
11121 return StmtError();
11122 }
11123 }
11124
11125 return OMPSingleDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
11126 AssociatedStmt: AStmt);
11127}
11128
11129StmtResult SemaOpenMP::ActOnOpenMPMasterDirective(Stmt *AStmt,
11130 SourceLocation StartLoc,
11131 SourceLocation EndLoc) {
11132 if (!AStmt)
11133 return StmtError();
11134
11135 SemaRef.setFunctionHasBranchProtectedScope();
11136
11137 return OMPMasterDirective::Create(C: getASTContext(), StartLoc, EndLoc, AssociatedStmt: AStmt);
11138}
11139
11140StmtResult SemaOpenMP::ActOnOpenMPMaskedDirective(ArrayRef<OMPClause *> Clauses,
11141 Stmt *AStmt,
11142 SourceLocation StartLoc,
11143 SourceLocation EndLoc) {
11144 if (!AStmt)
11145 return StmtError();
11146
11147 SemaRef.setFunctionHasBranchProtectedScope();
11148
11149 return OMPMaskedDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
11150 AssociatedStmt: AStmt);
11151}
11152
11153StmtResult SemaOpenMP::ActOnOpenMPCriticalDirective(
11154 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
11155 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
11156 if (!AStmt)
11157 return StmtError();
11158
11159 bool ErrorFound = false;
11160 llvm::APSInt Hint;
11161 SourceLocation HintLoc;
11162 bool DependentHint = false;
11163 for (const OMPClause *C : Clauses) {
11164 if (C->getClauseKind() == OMPC_hint) {
11165 if (!DirName.getName()) {
11166 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_hint_clause_no_name);
11167 ErrorFound = true;
11168 }
11169 Expr *E = cast<OMPHintClause>(Val: C)->getHint();
11170 if (E->isTypeDependent() || E->isValueDependent() ||
11171 E->isInstantiationDependent()) {
11172 DependentHint = true;
11173 } else {
11174 Hint = E->EvaluateKnownConstInt(Ctx: getASTContext());
11175 HintLoc = C->getBeginLoc();
11176 }
11177 }
11178 }
11179 if (ErrorFound)
11180 return StmtError();
11181 const auto Pair = DSAStack->getCriticalWithHint(Name: DirName);
11182 if (Pair.first && DirName.getName() && !DependentHint) {
11183 if (llvm::APSInt::compareValues(I1: Hint, I2: Pair.second) != 0) {
11184 Diag(Loc: StartLoc, DiagID: diag::err_omp_critical_with_hint);
11185 if (HintLoc.isValid())
11186 Diag(Loc: HintLoc, DiagID: diag::note_omp_critical_hint_here)
11187 << 0 << toString(I: Hint, /*Radix=*/10, /*Signed=*/false);
11188 else
11189 Diag(Loc: StartLoc, DiagID: diag::note_omp_critical_no_hint) << 0;
11190 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
11191 Diag(Loc: C->getBeginLoc(), DiagID: diag::note_omp_critical_hint_here)
11192 << 1
11193 << toString(I: C->getHint()->EvaluateKnownConstInt(Ctx: getASTContext()),
11194 /*Radix=*/10, /*Signed=*/false);
11195 } else {
11196 Diag(Loc: Pair.first->getBeginLoc(), DiagID: diag::note_omp_critical_no_hint) << 1;
11197 }
11198 }
11199 }
11200
11201 SemaRef.setFunctionHasBranchProtectedScope();
11202
11203 auto *Dir = OMPCriticalDirective::Create(C: getASTContext(), Name: DirName, StartLoc,
11204 EndLoc, Clauses, AssociatedStmt: AStmt);
11205 if (!Pair.first && DirName.getName() && !DependentHint)
11206 DSAStack->addCriticalWithHint(D: Dir, Hint);
11207 return Dir;
11208}
11209
11210StmtResult SemaOpenMP::ActOnOpenMPParallelForDirective(
11211 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11212 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11213 if (!AStmt)
11214 return StmtError();
11215
11216 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_for, AStmt);
11217
11218 OMPLoopBasedDirective::HelperExprs B;
11219 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
11220 // define the nested loops number.
11221 unsigned NestedLoopCount =
11222 checkOpenMPLoop(DKind: OMPD_parallel_for, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
11223 OrderedLoopCountExpr: getOrderedNumberExpr(Clauses), AStmt, SemaRef, DSA&: *DSAStack,
11224 VarsWithImplicitDSA, Built&: B);
11225 if (NestedLoopCount == 0)
11226 return StmtError();
11227
11228 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
11229 return StmtError();
11230
11231 return OMPParallelForDirective::Create(
11232 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
11233 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
11234}
11235
11236StmtResult SemaOpenMP::ActOnOpenMPParallelForSimdDirective(
11237 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11238 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11239 if (!AStmt)
11240 return StmtError();
11241
11242 CapturedStmt *CS =
11243 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_for_simd, AStmt);
11244
11245 OMPLoopBasedDirective::HelperExprs B;
11246 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
11247 // define the nested loops number.
11248 unsigned NestedLoopCount =
11249 checkOpenMPLoop(DKind: OMPD_parallel_for_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
11250 OrderedLoopCountExpr: getOrderedNumberExpr(Clauses), AStmt: CS, SemaRef, DSA&: *DSAStack,
11251 VarsWithImplicitDSA, Built&: B);
11252 if (NestedLoopCount == 0)
11253 return StmtError();
11254
11255 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
11256 return StmtError();
11257
11258 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
11259 return StmtError();
11260
11261 return OMPParallelForSimdDirective::Create(
11262 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
11263}
11264
11265StmtResult SemaOpenMP::ActOnOpenMPParallelMasterDirective(
11266 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11267 SourceLocation EndLoc) {
11268 if (!AStmt)
11269 return StmtError();
11270
11271 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_master, AStmt);
11272
11273 return OMPParallelMasterDirective::Create(
11274 C: getASTContext(), StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
11275 DSAStack->getTaskgroupReductionRef());
11276}
11277
11278StmtResult SemaOpenMP::ActOnOpenMPParallelMaskedDirective(
11279 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11280 SourceLocation EndLoc) {
11281 if (!AStmt)
11282 return StmtError();
11283
11284 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_masked, AStmt);
11285
11286 return OMPParallelMaskedDirective::Create(
11287 C: getASTContext(), StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
11288 DSAStack->getTaskgroupReductionRef());
11289}
11290
11291StmtResult SemaOpenMP::ActOnOpenMPParallelSectionsDirective(
11292 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11293 SourceLocation EndLoc) {
11294 if (checkSectionsDirective(SemaRef, DKind: OMPD_parallel_sections, AStmt, DSAStack))
11295 return StmtError();
11296
11297 SemaRef.setFunctionHasBranchProtectedScope();
11298
11299 return OMPParallelSectionsDirective::Create(
11300 C: getASTContext(), StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
11301 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
11302}
11303
11304/// Find and diagnose mutually exclusive clause kinds.
11305static bool checkMutuallyExclusiveClauses(
11306 Sema &S, ArrayRef<OMPClause *> Clauses,
11307 ArrayRef<OpenMPClauseKind> MutuallyExclusiveClauses) {
11308 const OMPClause *PrevClause = nullptr;
11309 bool ErrorFound = false;
11310 for (const OMPClause *C : Clauses) {
11311 if (llvm::is_contained(Range&: MutuallyExclusiveClauses, Element: C->getClauseKind())) {
11312 if (!PrevClause) {
11313 PrevClause = C;
11314 } else if (PrevClause->getClauseKind() != C->getClauseKind()) {
11315 S.Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_clauses_mutually_exclusive)
11316 << getOpenMPClauseNameForDiag(C: C->getClauseKind())
11317 << getOpenMPClauseNameForDiag(C: PrevClause->getClauseKind());
11318 S.Diag(Loc: PrevClause->getBeginLoc(), DiagID: diag::note_omp_previous_clause)
11319 << getOpenMPClauseNameForDiag(C: PrevClause->getClauseKind());
11320 ErrorFound = true;
11321 }
11322 }
11323 }
11324 return ErrorFound;
11325}
11326
11327StmtResult SemaOpenMP::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
11328 Stmt *AStmt,
11329 SourceLocation StartLoc,
11330 SourceLocation EndLoc) {
11331 if (!AStmt)
11332 return StmtError();
11333
11334 // OpenMP 5.0, 2.10.1 task Construct
11335 // If a detach clause appears on the directive, then a mergeable clause cannot
11336 // appear on the same directive.
11337 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
11338 MutuallyExclusiveClauses: {OMPC_detach, OMPC_mergeable}))
11339 return StmtError();
11340
11341 setBranchProtectedScope(SemaRef, DKind: OMPD_task, AStmt);
11342
11343 return OMPTaskDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
11344 AssociatedStmt: AStmt, DSAStack->isCancelRegion());
11345}
11346
11347StmtResult SemaOpenMP::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
11348 SourceLocation EndLoc) {
11349 return OMPTaskyieldDirective::Create(C: getASTContext(), StartLoc, EndLoc);
11350}
11351
11352StmtResult SemaOpenMP::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
11353 SourceLocation EndLoc) {
11354 return OMPBarrierDirective::Create(C: getASTContext(), StartLoc, EndLoc);
11355}
11356
11357StmtResult SemaOpenMP::ActOnOpenMPErrorDirective(ArrayRef<OMPClause *> Clauses,
11358 SourceLocation StartLoc,
11359 SourceLocation EndLoc,
11360 bool InExContext) {
11361 const OMPAtClause *AtC =
11362 OMPExecutableDirective::getSingleClause<OMPAtClause>(Clauses);
11363
11364 if (AtC && !InExContext && AtC->getAtKind() == OMPC_AT_execution) {
11365 Diag(Loc: AtC->getAtKindKwLoc(), DiagID: diag::err_omp_unexpected_execution_modifier);
11366 return StmtError();
11367 }
11368
11369 if (!AtC || AtC->getAtKind() == OMPC_AT_compilation) {
11370 const OMPSeverityClause *SeverityC =
11371 OMPExecutableDirective::getSingleClause<OMPSeverityClause>(Clauses);
11372 const OMPMessageClause *MessageC =
11373 OMPExecutableDirective::getSingleClause<OMPMessageClause>(Clauses);
11374 std::optional<std::string> SL =
11375 MessageC ? MessageC->tryEvaluateString(Ctx&: getASTContext()) : std::nullopt;
11376
11377 if (MessageC && !SL)
11378 Diag(Loc: MessageC->getMessageString()->getBeginLoc(),
11379 DiagID: diag::warn_clause_expected_string)
11380 << getOpenMPClauseNameForDiag(C: OMPC_message) << 1;
11381 if (SeverityC && SeverityC->getSeverityKind() == OMPC_SEVERITY_warning)
11382 Diag(Loc: SeverityC->getSeverityKindKwLoc(), DiagID: diag::warn_diagnose_if_succeeded)
11383 << SL.value_or(u: "WARNING");
11384 else
11385 Diag(Loc: StartLoc, DiagID: diag::err_diagnose_if_succeeded) << SL.value_or(u: "ERROR");
11386 if (!SeverityC || SeverityC->getSeverityKind() != OMPC_SEVERITY_warning)
11387 return StmtError();
11388 }
11389
11390 return OMPErrorDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses);
11391}
11392
11393StmtResult
11394SemaOpenMP::ActOnOpenMPTaskwaitDirective(ArrayRef<OMPClause *> Clauses,
11395 SourceLocation StartLoc,
11396 SourceLocation EndLoc) {
11397 const OMPNowaitClause *NowaitC =
11398 OMPExecutableDirective::getSingleClause<OMPNowaitClause>(Clauses);
11399 bool HasDependC =
11400 !OMPExecutableDirective::getClausesOfKind<OMPDependClause>(Clauses)
11401 .empty();
11402 if (NowaitC && !HasDependC) {
11403 Diag(Loc: StartLoc, DiagID: diag::err_omp_nowait_clause_without_depend);
11404 return StmtError();
11405 }
11406
11407 return OMPTaskwaitDirective::Create(C: getASTContext(), StartLoc, EndLoc,
11408 Clauses);
11409}
11410
11411StmtResult
11412SemaOpenMP::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
11413 Stmt *AStmt, SourceLocation StartLoc,
11414 SourceLocation EndLoc) {
11415 if (!AStmt)
11416 return StmtError();
11417
11418 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11419
11420 SemaRef.setFunctionHasBranchProtectedScope();
11421
11422 return OMPTaskgroupDirective::Create(C: getASTContext(), StartLoc, EndLoc,
11423 Clauses, AssociatedStmt: AStmt,
11424 DSAStack->getTaskgroupReductionRef());
11425}
11426
11427StmtResult SemaOpenMP::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
11428 SourceLocation StartLoc,
11429 SourceLocation EndLoc) {
11430 OMPFlushClause *FC = nullptr;
11431 OMPClause *OrderClause = nullptr;
11432 for (OMPClause *C : Clauses) {
11433 if (C->getClauseKind() == OMPC_flush)
11434 FC = cast<OMPFlushClause>(Val: C);
11435 else
11436 OrderClause = C;
11437 }
11438 unsigned OMPVersion = getLangOpts().OpenMP;
11439 OpenMPClauseKind MemOrderKind = OMPC_unknown;
11440 SourceLocation MemOrderLoc;
11441 for (const OMPClause *C : Clauses) {
11442 if (C->getClauseKind() == OMPC_acq_rel ||
11443 C->getClauseKind() == OMPC_acquire ||
11444 C->getClauseKind() == OMPC_release ||
11445 C->getClauseKind() == OMPC_seq_cst /*OpenMP 5.1*/) {
11446 if (MemOrderKind != OMPC_unknown) {
11447 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_several_mem_order_clauses)
11448 << getOpenMPDirectiveName(D: OMPD_flush, Ver: OMPVersion) << 1
11449 << SourceRange(C->getBeginLoc(), C->getEndLoc());
11450 Diag(Loc: MemOrderLoc, DiagID: diag::note_omp_previous_mem_order_clause)
11451 << getOpenMPClauseNameForDiag(C: MemOrderKind);
11452 } else {
11453 MemOrderKind = C->getClauseKind();
11454 MemOrderLoc = C->getBeginLoc();
11455 }
11456 }
11457 }
11458 if (FC && OrderClause) {
11459 Diag(Loc: FC->getLParenLoc(), DiagID: diag::err_omp_flush_order_clause_and_list)
11460 << getOpenMPClauseNameForDiag(C: OrderClause->getClauseKind());
11461 Diag(Loc: OrderClause->getBeginLoc(), DiagID: diag::note_omp_flush_order_clause_here)
11462 << getOpenMPClauseNameForDiag(C: OrderClause->getClauseKind());
11463 return StmtError();
11464 }
11465 return OMPFlushDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses);
11466}
11467
11468StmtResult SemaOpenMP::ActOnOpenMPDepobjDirective(ArrayRef<OMPClause *> Clauses,
11469 SourceLocation StartLoc,
11470 SourceLocation EndLoc) {
11471 if (Clauses.empty()) {
11472 Diag(Loc: StartLoc, DiagID: diag::err_omp_depobj_expected);
11473 return StmtError();
11474 } else if (Clauses[0]->getClauseKind() != OMPC_depobj) {
11475 Diag(Loc: Clauses[0]->getBeginLoc(), DiagID: diag::err_omp_depobj_expected);
11476 return StmtError();
11477 }
11478 // Only depobj expression and another single clause is allowed.
11479 if (Clauses.size() > 2) {
11480 Diag(Loc: Clauses[2]->getBeginLoc(),
11481 DiagID: diag::err_omp_depobj_single_clause_expected);
11482 return StmtError();
11483 } else if (Clauses.size() < 1) {
11484 Diag(Loc: Clauses[0]->getEndLoc(), DiagID: diag::err_omp_depobj_single_clause_expected);
11485 return StmtError();
11486 }
11487 return OMPDepobjDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses);
11488}
11489
11490StmtResult SemaOpenMP::ActOnOpenMPScanDirective(ArrayRef<OMPClause *> Clauses,
11491 SourceLocation StartLoc,
11492 SourceLocation EndLoc) {
11493 // Check that exactly one clause is specified.
11494 if (Clauses.size() != 1) {
11495 Diag(Loc: Clauses.empty() ? EndLoc : Clauses[1]->getBeginLoc(),
11496 DiagID: diag::err_omp_scan_single_clause_expected);
11497 return StmtError();
11498 }
11499 // Check that scan directive is used in the scope of the OpenMP loop body.
11500 if (Scope *S = DSAStack->getCurScope()) {
11501 Scope *ParentS = S->getParent();
11502 if (!ParentS || ParentS->getParent() != ParentS->getBreakParent() ||
11503 !ParentS->getBreakParent()->isOpenMPLoopScope()) {
11504 unsigned OMPVersion = getLangOpts().OpenMP;
11505 return StmtError(Diag(Loc: StartLoc, DiagID: diag::err_omp_orphaned_device_directive)
11506 << getOpenMPDirectiveName(D: OMPD_scan, Ver: OMPVersion) << 5);
11507 }
11508 }
11509 // Check that only one instance of scan directives is used in the same outer
11510 // region.
11511 if (DSAStack->doesParentHasScanDirective()) {
11512 Diag(Loc: StartLoc, DiagID: diag::err_omp_several_directives_in_region) << "scan";
11513 Diag(DSAStack->getParentScanDirectiveLoc(),
11514 DiagID: diag::note_omp_previous_directive)
11515 << "scan";
11516 return StmtError();
11517 }
11518 DSAStack->setParentHasScanDirective(StartLoc);
11519 return OMPScanDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses);
11520}
11521
11522StmtResult
11523SemaOpenMP::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
11524 Stmt *AStmt, SourceLocation StartLoc,
11525 SourceLocation EndLoc) {
11526 const OMPClause *DependFound = nullptr;
11527 const OMPClause *DependSourceClause = nullptr;
11528 const OMPClause *DependSinkClause = nullptr;
11529 const OMPClause *DoacrossFound = nullptr;
11530 const OMPClause *DoacrossSourceClause = nullptr;
11531 const OMPClause *DoacrossSinkClause = nullptr;
11532 bool ErrorFound = false;
11533 const OMPThreadsClause *TC = nullptr;
11534 const OMPSIMDClause *SC = nullptr;
11535 for (const OMPClause *C : Clauses) {
11536 auto DOC = dyn_cast<OMPDoacrossClause>(Val: C);
11537 auto DC = dyn_cast<OMPDependClause>(Val: C);
11538 if (DC || DOC) {
11539 DependFound = DC ? C : nullptr;
11540 DoacrossFound = DOC ? C : nullptr;
11541 OMPDoacrossKind ODK;
11542 if ((DC && DC->getDependencyKind() == OMPC_DEPEND_source) ||
11543 (DOC && (ODK.isSource(C: DOC)))) {
11544 if ((DC && DependSourceClause) || (DOC && DoacrossSourceClause)) {
11545 unsigned OMPVersion = getLangOpts().OpenMP;
11546 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_more_one_clause)
11547 << getOpenMPDirectiveName(D: OMPD_ordered, Ver: OMPVersion)
11548 << getOpenMPClauseNameForDiag(C: DC ? OMPC_depend : OMPC_doacross)
11549 << 2;
11550 ErrorFound = true;
11551 } else {
11552 if (DC)
11553 DependSourceClause = C;
11554 else
11555 DoacrossSourceClause = C;
11556 }
11557 if ((DC && DependSinkClause) || (DOC && DoacrossSinkClause)) {
11558 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_sink_and_source_not_allowed)
11559 << (DC ? "depend" : "doacross") << 0;
11560 ErrorFound = true;
11561 }
11562 } else if ((DC && DC->getDependencyKind() == OMPC_DEPEND_sink) ||
11563 (DOC && (ODK.isSink(C: DOC) || ODK.isSinkIter(C: DOC)))) {
11564 if (DependSourceClause || DoacrossSourceClause) {
11565 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_sink_and_source_not_allowed)
11566 << (DC ? "depend" : "doacross") << 1;
11567 ErrorFound = true;
11568 }
11569 if (DC)
11570 DependSinkClause = C;
11571 else
11572 DoacrossSinkClause = C;
11573 }
11574 } else if (C->getClauseKind() == OMPC_threads) {
11575 TC = cast<OMPThreadsClause>(Val: C);
11576 } else if (C->getClauseKind() == OMPC_simd) {
11577 SC = cast<OMPSIMDClause>(Val: C);
11578 }
11579 }
11580 if (!ErrorFound && !SC &&
11581 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
11582 // OpenMP [2.8.1,simd Construct, Restrictions]
11583 // An ordered construct with the simd clause is the only OpenMP construct
11584 // that can appear in the simd region.
11585 Diag(Loc: StartLoc, DiagID: diag::err_omp_prohibited_region_simd)
11586 << (getLangOpts().OpenMP >= 50 ? 1 : 0);
11587 ErrorFound = true;
11588 } else if ((DependFound || DoacrossFound) && (TC || SC)) {
11589 SourceLocation Loc =
11590 DependFound ? DependFound->getBeginLoc() : DoacrossFound->getBeginLoc();
11591 Diag(Loc, DiagID: diag::err_omp_depend_clause_thread_simd)
11592 << getOpenMPClauseNameForDiag(C: DependFound ? OMPC_depend : OMPC_doacross)
11593 << getOpenMPClauseNameForDiag(C: TC ? TC->getClauseKind()
11594 : SC->getClauseKind());
11595 ErrorFound = true;
11596 } else if ((DependFound || DoacrossFound) &&
11597 !DSAStack->getParentOrderedRegionParam().first) {
11598 SourceLocation Loc =
11599 DependFound ? DependFound->getBeginLoc() : DoacrossFound->getBeginLoc();
11600 Diag(Loc, DiagID: diag::err_omp_ordered_directive_without_param)
11601 << getOpenMPClauseNameForDiag(C: DependFound ? OMPC_depend
11602 : OMPC_doacross);
11603 ErrorFound = true;
11604 } else if (TC || Clauses.empty()) {
11605 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
11606 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
11607 Diag(Loc: ErrLoc, DiagID: diag::err_omp_ordered_directive_with_param)
11608 << (TC != nullptr);
11609 Diag(Loc: Param->getBeginLoc(), DiagID: diag::note_omp_ordered_param) << 1;
11610 ErrorFound = true;
11611 }
11612 }
11613 if ((!AStmt && !DependFound && !DoacrossFound) || ErrorFound)
11614 return StmtError();
11615
11616 // OpenMP 5.0, 2.17.9, ordered Construct, Restrictions.
11617 // During execution of an iteration of a worksharing-loop or a loop nest
11618 // within a worksharing-loop, simd, or worksharing-loop SIMD region, a thread
11619 // must not execute more than one ordered region corresponding to an ordered
11620 // construct without a depend clause.
11621 if (!DependFound && !DoacrossFound) {
11622 if (DSAStack->doesParentHasOrderedDirective()) {
11623 Diag(Loc: StartLoc, DiagID: diag::err_omp_several_directives_in_region) << "ordered";
11624 Diag(DSAStack->getParentOrderedDirectiveLoc(),
11625 DiagID: diag::note_omp_previous_directive)
11626 << "ordered";
11627 return StmtError();
11628 }
11629 DSAStack->setParentHasOrderedDirective(StartLoc);
11630 }
11631
11632 if (AStmt) {
11633 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11634
11635 SemaRef.setFunctionHasBranchProtectedScope();
11636 }
11637
11638 return OMPOrderedDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
11639 AssociatedStmt: AStmt);
11640}
11641
11642namespace {
11643/// Helper class for checking expression in 'omp atomic [update]'
11644/// construct.
11645class OpenMPAtomicUpdateChecker {
11646 /// Error results for atomic update expressions.
11647 enum ExprAnalysisErrorCode {
11648 /// A statement is not an expression statement.
11649 NotAnExpression,
11650 /// Expression is not builtin binary or unary operation.
11651 NotABinaryOrUnaryExpression,
11652 /// Unary operation is not post-/pre- increment/decrement operation.
11653 NotAnUnaryIncDecExpression,
11654 /// An expression is not of scalar type.
11655 NotAScalarType,
11656 /// A binary operation is not an assignment operation.
11657 NotAnAssignmentOp,
11658 /// RHS part of the binary operation is not a binary expression.
11659 NotABinaryExpression,
11660 /// RHS part is not additive/multiplicative/shift/bitwise binary
11661 /// expression.
11662 NotABinaryOperator,
11663 /// RHS binary operation does not have reference to the updated LHS
11664 /// part.
11665 NotAnUpdateExpression,
11666 /// An expression contains semantical error not related to
11667 /// 'omp atomic [update]'
11668 NotAValidExpression,
11669 /// No errors is found.
11670 NoError
11671 };
11672 /// Reference to Sema.
11673 Sema &SemaRef;
11674 /// A location for note diagnostics (when error is found).
11675 SourceLocation NoteLoc;
11676 /// 'x' lvalue part of the source atomic expression.
11677 Expr *X;
11678 /// 'expr' rvalue part of the source atomic expression.
11679 Expr *E;
11680 /// Helper expression of the form
11681 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
11682 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
11683 Expr *UpdateExpr;
11684 /// Is 'x' a LHS in a RHS part of full update expression. It is
11685 /// important for non-associative operations.
11686 bool IsXLHSInRHSPart;
11687 BinaryOperatorKind Op;
11688 SourceLocation OpLoc;
11689 /// true if the source expression is a postfix unary operation, false
11690 /// if it is a prefix unary operation.
11691 bool IsPostfixUpdate;
11692
11693public:
11694 OpenMPAtomicUpdateChecker(Sema &SemaRef)
11695 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
11696 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
11697 /// Check specified statement that it is suitable for 'atomic update'
11698 /// constructs and extract 'x', 'expr' and Operation from the original
11699 /// expression. If DiagId and NoteId == 0, then only check is performed
11700 /// without error notification.
11701 /// \param DiagId Diagnostic which should be emitted if error is found.
11702 /// \param NoteId Diagnostic note for the main error message.
11703 /// \return true if statement is not an update expression, false otherwise.
11704 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
11705 /// Return the 'x' lvalue part of the source atomic expression.
11706 Expr *getX() const { return X; }
11707 /// Return the 'expr' rvalue part of the source atomic expression.
11708 Expr *getExpr() const { return E; }
11709 /// Return the update expression used in calculation of the updated
11710 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
11711 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
11712 Expr *getUpdateExpr() const { return UpdateExpr; }
11713 /// Return true if 'x' is LHS in RHS part of full update expression,
11714 /// false otherwise.
11715 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
11716
11717 /// true if the source expression is a postfix unary operation, false
11718 /// if it is a prefix unary operation.
11719 bool isPostfixUpdate() const { return IsPostfixUpdate; }
11720
11721private:
11722 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
11723 unsigned NoteId = 0);
11724};
11725
11726bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
11727 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
11728 ExprAnalysisErrorCode ErrorFound = NoError;
11729 SourceLocation ErrorLoc, NoteLoc;
11730 SourceRange ErrorRange, NoteRange;
11731 // Allowed constructs are:
11732 // x = x binop expr;
11733 // x = expr binop x;
11734 if (AtomicBinOp->getOpcode() == BO_Assign) {
11735 X = AtomicBinOp->getLHS();
11736 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
11737 Val: AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
11738 if (AtomicInnerBinOp->isMultiplicativeOp() ||
11739 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
11740 AtomicInnerBinOp->isBitwiseOp()) {
11741 Op = AtomicInnerBinOp->getOpcode();
11742 OpLoc = AtomicInnerBinOp->getOperatorLoc();
11743 Expr *LHS = AtomicInnerBinOp->getLHS();
11744 Expr *RHS = AtomicInnerBinOp->getRHS();
11745 llvm::FoldingSetNodeID XId, LHSId, RHSId;
11746 X->IgnoreParenImpCasts()->Profile(ID&: XId, Context: SemaRef.getASTContext(),
11747 /*Canonical=*/true);
11748 LHS->IgnoreParenImpCasts()->Profile(ID&: LHSId, Context: SemaRef.getASTContext(),
11749 /*Canonical=*/true);
11750 RHS->IgnoreParenImpCasts()->Profile(ID&: RHSId, Context: SemaRef.getASTContext(),
11751 /*Canonical=*/true);
11752 if (XId == LHSId) {
11753 E = RHS;
11754 IsXLHSInRHSPart = true;
11755 } else if (XId == RHSId) {
11756 E = LHS;
11757 IsXLHSInRHSPart = false;
11758 } else {
11759 ErrorLoc = AtomicInnerBinOp->getExprLoc();
11760 ErrorRange = AtomicInnerBinOp->getSourceRange();
11761 NoteLoc = X->getExprLoc();
11762 NoteRange = X->getSourceRange();
11763 ErrorFound = NotAnUpdateExpression;
11764 }
11765 } else {
11766 ErrorLoc = AtomicInnerBinOp->getExprLoc();
11767 ErrorRange = AtomicInnerBinOp->getSourceRange();
11768 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
11769 NoteRange = SourceRange(NoteLoc, NoteLoc);
11770 ErrorFound = NotABinaryOperator;
11771 }
11772 } else {
11773 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
11774 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
11775 ErrorFound = NotABinaryExpression;
11776 }
11777 } else {
11778 ErrorLoc = AtomicBinOp->getExprLoc();
11779 ErrorRange = AtomicBinOp->getSourceRange();
11780 NoteLoc = AtomicBinOp->getOperatorLoc();
11781 NoteRange = SourceRange(NoteLoc, NoteLoc);
11782 ErrorFound = NotAnAssignmentOp;
11783 }
11784 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
11785 SemaRef.Diag(Loc: ErrorLoc, DiagID: DiagId) << ErrorRange;
11786 SemaRef.Diag(Loc: NoteLoc, DiagID: NoteId) << ErrorFound << NoteRange;
11787 return true;
11788 }
11789 if (SemaRef.CurContext->isDependentContext())
11790 E = X = UpdateExpr = nullptr;
11791 return ErrorFound != NoError;
11792}
11793
11794bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
11795 unsigned NoteId) {
11796 ExprAnalysisErrorCode ErrorFound = NoError;
11797 SourceLocation ErrorLoc, NoteLoc;
11798 SourceRange ErrorRange, NoteRange;
11799 // Allowed constructs are:
11800 // x++;
11801 // x--;
11802 // ++x;
11803 // --x;
11804 // x binop= expr;
11805 // x = x binop expr;
11806 // x = expr binop x;
11807 if (auto *AtomicBody = dyn_cast<Expr>(Val: S)) {
11808 AtomicBody = AtomicBody->IgnoreParenImpCasts();
11809 if (AtomicBody->getType()->isScalarType() ||
11810 AtomicBody->isInstantiationDependent()) {
11811 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
11812 Val: AtomicBody->IgnoreParenImpCasts())) {
11813 // Check for Compound Assignment Operation
11814 Op = BinaryOperator::getOpForCompoundAssignment(
11815 Opc: AtomicCompAssignOp->getOpcode());
11816 OpLoc = AtomicCompAssignOp->getOperatorLoc();
11817 E = AtomicCompAssignOp->getRHS();
11818 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
11819 IsXLHSInRHSPart = true;
11820 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
11821 Val: AtomicBody->IgnoreParenImpCasts())) {
11822 // Check for Binary Operation
11823 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
11824 return true;
11825 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
11826 Val: AtomicBody->IgnoreParenImpCasts())) {
11827 // Check for Unary Operation
11828 if (AtomicUnaryOp->isIncrementDecrementOp()) {
11829 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
11830 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
11831 OpLoc = AtomicUnaryOp->getOperatorLoc();
11832 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
11833 E = SemaRef.ActOnIntegerConstant(Loc: OpLoc, /*uint64_t Val=*/Val: 1).get();
11834 IsXLHSInRHSPart = true;
11835 } else {
11836 ErrorFound = NotAnUnaryIncDecExpression;
11837 ErrorLoc = AtomicUnaryOp->getExprLoc();
11838 ErrorRange = AtomicUnaryOp->getSourceRange();
11839 NoteLoc = AtomicUnaryOp->getOperatorLoc();
11840 NoteRange = SourceRange(NoteLoc, NoteLoc);
11841 }
11842 } else if (!AtomicBody->isInstantiationDependent()) {
11843 ErrorFound = NotABinaryOrUnaryExpression;
11844 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
11845 NoteRange = ErrorRange = AtomicBody->getSourceRange();
11846 } else if (AtomicBody->containsErrors()) {
11847 ErrorFound = NotAValidExpression;
11848 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
11849 NoteRange = ErrorRange = AtomicBody->getSourceRange();
11850 }
11851 } else {
11852 ErrorFound = NotAScalarType;
11853 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
11854 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
11855 }
11856 } else {
11857 ErrorFound = NotAnExpression;
11858 NoteLoc = ErrorLoc = S->getBeginLoc();
11859 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
11860 }
11861 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
11862 SemaRef.Diag(Loc: ErrorLoc, DiagID: DiagId) << ErrorRange;
11863 SemaRef.Diag(Loc: NoteLoc, DiagID: NoteId) << ErrorFound << NoteRange;
11864 return true;
11865 }
11866 if (SemaRef.CurContext->isDependentContext())
11867 E = X = UpdateExpr = nullptr;
11868 if (ErrorFound == NoError && E && X) {
11869 // Build an update expression of form 'OpaqueValueExpr(x) binop
11870 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
11871 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
11872 auto *OVEX = new (SemaRef.getASTContext())
11873 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_PRValue);
11874 auto *OVEExpr = new (SemaRef.getASTContext())
11875 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_PRValue);
11876 ExprResult Update =
11877 SemaRef.CreateBuiltinBinOp(OpLoc, Opc: Op, LHSExpr: IsXLHSInRHSPart ? OVEX : OVEExpr,
11878 RHSExpr: IsXLHSInRHSPart ? OVEExpr : OVEX);
11879 if (Update.isInvalid())
11880 return true;
11881 Update = SemaRef.PerformImplicitConversion(From: Update.get(), ToType: X->getType(),
11882 Action: AssignmentAction::Casting);
11883 if (Update.isInvalid())
11884 return true;
11885 UpdateExpr = Update.get();
11886 }
11887 return ErrorFound != NoError;
11888}
11889
11890/// Get the node id of the fixed point of an expression \a S.
11891llvm::FoldingSetNodeID getNodeId(ASTContext &Context, const Expr *S) {
11892 llvm::FoldingSetNodeID Id;
11893 S->IgnoreParenImpCasts()->Profile(ID&: Id, Context, Canonical: true);
11894 return Id;
11895}
11896
11897/// Check if two expressions are same.
11898bool checkIfTwoExprsAreSame(ASTContext &Context, const Expr *LHS,
11899 const Expr *RHS) {
11900 return getNodeId(Context, S: LHS) == getNodeId(Context, S: RHS);
11901}
11902
11903class OpenMPAtomicCompareChecker {
11904public:
11905 /// All kinds of errors that can occur in `atomic compare`
11906 enum ErrorTy {
11907 /// Empty compound statement.
11908 NoStmt = 0,
11909 /// More than one statement in a compound statement.
11910 MoreThanOneStmt,
11911 /// Not an assignment binary operator.
11912 NotAnAssignment,
11913 /// Not a conditional operator.
11914 NotCondOp,
11915 /// Wrong false expr. According to the spec, 'x' should be at the false
11916 /// expression of a conditional expression.
11917 WrongFalseExpr,
11918 /// The condition of a conditional expression is not a binary operator.
11919 NotABinaryOp,
11920 /// Invalid binary operator (not <, >, or ==).
11921 InvalidBinaryOp,
11922 /// Invalid comparison (not x == e, e == x, x ordop expr, or expr ordop x).
11923 InvalidComparison,
11924 /// X is not a lvalue.
11925 XNotLValue,
11926 /// Not a scalar.
11927 NotScalar,
11928 /// Not an integer.
11929 NotInteger,
11930 /// 'else' statement is not expected.
11931 UnexpectedElse,
11932 /// Not an equality operator.
11933 NotEQ,
11934 /// Invalid assignment (not v == x).
11935 InvalidAssignment,
11936 /// Not if statement
11937 NotIfStmt,
11938 /// More than two statements in a compound statement.
11939 MoreThanTwoStmts,
11940 /// Not a compound statement.
11941 NotCompoundStmt,
11942 /// No else statement.
11943 NoElse,
11944 /// Not 'if (r)'.
11945 InvalidCondition,
11946 /// No error.
11947 NoError,
11948 };
11949
11950 struct ErrorInfoTy {
11951 ErrorTy Error;
11952 SourceLocation ErrorLoc;
11953 SourceRange ErrorRange;
11954 SourceLocation NoteLoc;
11955 SourceRange NoteRange;
11956 };
11957
11958 OpenMPAtomicCompareChecker(Sema &S) : ContextRef(S.getASTContext()) {}
11959
11960 /// Check if statement \a S is valid for <tt>atomic compare</tt>.
11961 bool checkStmt(Stmt *S, ErrorInfoTy &ErrorInfo);
11962
11963 Expr *getX() const { return X; }
11964 Expr *getE() const { return E; }
11965 Expr *getD() const { return D; }
11966 Expr *getCond() const { return C; }
11967 bool isXBinopExpr() const { return IsXBinopExpr; }
11968
11969protected:
11970 /// Reference to ASTContext
11971 ASTContext &ContextRef;
11972 /// 'x' lvalue part of the source atomic expression.
11973 Expr *X = nullptr;
11974 /// 'expr' or 'e' rvalue part of the source atomic expression.
11975 Expr *E = nullptr;
11976 /// 'd' rvalue part of the source atomic expression.
11977 Expr *D = nullptr;
11978 /// 'cond' part of the source atomic expression. It is in one of the following
11979 /// forms:
11980 /// expr ordop x
11981 /// x ordop expr
11982 /// x == e
11983 /// e == x
11984 Expr *C = nullptr;
11985 /// True if the cond expr is in the form of 'x ordop expr'.
11986 bool IsXBinopExpr = true;
11987
11988 /// Check if it is a valid conditional update statement (cond-update-stmt).
11989 bool checkCondUpdateStmt(IfStmt *S, ErrorInfoTy &ErrorInfo);
11990
11991 /// Check if it is a valid conditional expression statement (cond-expr-stmt).
11992 bool checkCondExprStmt(Stmt *S, ErrorInfoTy &ErrorInfo);
11993
11994 /// Check if all captured values have right type.
11995 bool checkType(ErrorInfoTy &ErrorInfo) const;
11996
11997 static bool CheckValue(const Expr *E, ErrorInfoTy &ErrorInfo,
11998 bool ShouldBeLValue, bool ShouldBeInteger = false) {
11999 if (E->isInstantiationDependent())
12000 return true;
12001
12002 if (ShouldBeLValue && !E->isLValue()) {
12003 ErrorInfo.Error = ErrorTy::XNotLValue;
12004 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = E->getExprLoc();
12005 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = E->getSourceRange();
12006 return false;
12007 }
12008
12009 QualType QTy = E->getType();
12010 if (!QTy->isScalarType()) {
12011 ErrorInfo.Error = ErrorTy::NotScalar;
12012 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = E->getExprLoc();
12013 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = E->getSourceRange();
12014 return false;
12015 }
12016 if (ShouldBeInteger && !QTy->isIntegerType()) {
12017 ErrorInfo.Error = ErrorTy::NotInteger;
12018 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = E->getExprLoc();
12019 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = E->getSourceRange();
12020 return false;
12021 }
12022
12023 return true;
12024 }
12025};
12026
12027bool OpenMPAtomicCompareChecker::checkCondUpdateStmt(IfStmt *S,
12028 ErrorInfoTy &ErrorInfo) {
12029 auto *Then = S->getThen();
12030 if (auto *CS = dyn_cast<CompoundStmt>(Val: Then)) {
12031 if (CS->body_empty()) {
12032 ErrorInfo.Error = ErrorTy::NoStmt;
12033 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12034 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
12035 return false;
12036 }
12037 if (CS->size() > 1) {
12038 ErrorInfo.Error = ErrorTy::MoreThanOneStmt;
12039 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12040 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getSourceRange();
12041 return false;
12042 }
12043 Then = CS->body_front();
12044 }
12045
12046 auto *BO = dyn_cast<BinaryOperator>(Val: Then);
12047 if (!BO) {
12048 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12049 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = Then->getBeginLoc();
12050 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Then->getSourceRange();
12051 return false;
12052 }
12053 if (BO->getOpcode() != BO_Assign) {
12054 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12055 ErrorInfo.ErrorLoc = BO->getExprLoc();
12056 ErrorInfo.NoteLoc = BO->getOperatorLoc();
12057 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getSourceRange();
12058 return false;
12059 }
12060
12061 X = BO->getLHS();
12062
12063 auto *Cond = dyn_cast<BinaryOperator>(Val: S->getCond());
12064 auto *Call = dyn_cast<CXXOperatorCallExpr>(Val: S->getCond());
12065 Expr *LHS = nullptr;
12066 Expr *RHS = nullptr;
12067 if (Cond) {
12068 LHS = Cond->getLHS();
12069 RHS = Cond->getRHS();
12070 } else if (Call) {
12071 LHS = Call->getArg(Arg: 0);
12072 RHS = Call->getArg(Arg: 1);
12073 } else {
12074 ErrorInfo.Error = ErrorTy::NotABinaryOp;
12075 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
12076 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getCond()->getSourceRange();
12077 return false;
12078 }
12079
12080 if ((Cond && Cond->getOpcode() == BO_EQ) ||
12081 (Call && Call->getOperator() == OverloadedOperatorKind::OO_EqualEqual)) {
12082 C = S->getCond();
12083 D = BO->getRHS();
12084 if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: LHS)) {
12085 E = RHS;
12086 } else if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS)) {
12087 E = LHS;
12088 } else {
12089 ErrorInfo.Error = ErrorTy::InvalidComparison;
12090 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
12091 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12092 S->getCond()->getSourceRange();
12093 return false;
12094 }
12095 } else if ((Cond &&
12096 (Cond->getOpcode() == BO_LT || Cond->getOpcode() == BO_GT)) ||
12097 (Call &&
12098 (Call->getOperator() == OverloadedOperatorKind::OO_Less ||
12099 Call->getOperator() == OverloadedOperatorKind::OO_Greater))) {
12100 E = BO->getRHS();
12101 if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: LHS) &&
12102 checkIfTwoExprsAreSame(Context&: ContextRef, LHS: E, RHS)) {
12103 C = S->getCond();
12104 } else if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: E, RHS: LHS) &&
12105 checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS)) {
12106 C = S->getCond();
12107 IsXBinopExpr = false;
12108 } else {
12109 ErrorInfo.Error = ErrorTy::InvalidComparison;
12110 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
12111 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12112 S->getCond()->getSourceRange();
12113 return false;
12114 }
12115 } else {
12116 ErrorInfo.Error = ErrorTy::InvalidBinaryOp;
12117 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
12118 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getCond()->getSourceRange();
12119 return false;
12120 }
12121
12122 if (S->getElse()) {
12123 ErrorInfo.Error = ErrorTy::UnexpectedElse;
12124 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getElse()->getBeginLoc();
12125 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getElse()->getSourceRange();
12126 return false;
12127 }
12128
12129 return true;
12130}
12131
12132bool OpenMPAtomicCompareChecker::checkCondExprStmt(Stmt *S,
12133 ErrorInfoTy &ErrorInfo) {
12134 auto *BO = dyn_cast<BinaryOperator>(Val: S);
12135 if (!BO) {
12136 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12137 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getBeginLoc();
12138 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getSourceRange();
12139 return false;
12140 }
12141 if (BO->getOpcode() != BO_Assign) {
12142 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12143 ErrorInfo.ErrorLoc = BO->getExprLoc();
12144 ErrorInfo.NoteLoc = BO->getOperatorLoc();
12145 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getSourceRange();
12146 return false;
12147 }
12148
12149 X = BO->getLHS();
12150
12151 auto *CO = dyn_cast<ConditionalOperator>(Val: BO->getRHS()->IgnoreParenImpCasts());
12152 if (!CO) {
12153 ErrorInfo.Error = ErrorTy::NotCondOp;
12154 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = BO->getRHS()->getExprLoc();
12155 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getRHS()->getSourceRange();
12156 return false;
12157 }
12158
12159 if (!checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: CO->getFalseExpr())) {
12160 ErrorInfo.Error = ErrorTy::WrongFalseExpr;
12161 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CO->getFalseExpr()->getExprLoc();
12162 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12163 CO->getFalseExpr()->getSourceRange();
12164 return false;
12165 }
12166
12167 auto *Cond = dyn_cast<BinaryOperator>(Val: CO->getCond());
12168 auto *Call = dyn_cast<CXXOperatorCallExpr>(Val: CO->getCond());
12169 Expr *LHS = nullptr;
12170 Expr *RHS = nullptr;
12171 if (Cond) {
12172 LHS = Cond->getLHS();
12173 RHS = Cond->getRHS();
12174 } else if (Call) {
12175 LHS = Call->getArg(Arg: 0);
12176 RHS = Call->getArg(Arg: 1);
12177 } else {
12178 ErrorInfo.Error = ErrorTy::NotABinaryOp;
12179 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CO->getCond()->getExprLoc();
12180 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12181 CO->getCond()->getSourceRange();
12182 return false;
12183 }
12184
12185 if ((Cond && Cond->getOpcode() == BO_EQ) ||
12186 (Call && Call->getOperator() == OverloadedOperatorKind::OO_EqualEqual)) {
12187 C = CO->getCond();
12188 D = CO->getTrueExpr();
12189 if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: LHS)) {
12190 E = RHS;
12191 } else if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS)) {
12192 E = LHS;
12193 } else {
12194 ErrorInfo.Error = ErrorTy::InvalidComparison;
12195 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CO->getCond()->getExprLoc();
12196 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12197 CO->getCond()->getSourceRange();
12198 return false;
12199 }
12200 } else if ((Cond &&
12201 (Cond->getOpcode() == BO_LT || Cond->getOpcode() == BO_GT)) ||
12202 (Call &&
12203 (Call->getOperator() == OverloadedOperatorKind::OO_Less ||
12204 Call->getOperator() == OverloadedOperatorKind::OO_Greater))) {
12205
12206 E = CO->getTrueExpr();
12207 if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: LHS) &&
12208 checkIfTwoExprsAreSame(Context&: ContextRef, LHS: E, RHS)) {
12209 C = CO->getCond();
12210 } else if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: E, RHS: LHS) &&
12211 checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS)) {
12212 C = CO->getCond();
12213 IsXBinopExpr = false;
12214 } else {
12215 ErrorInfo.Error = ErrorTy::InvalidComparison;
12216 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CO->getCond()->getExprLoc();
12217 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12218 CO->getCond()->getSourceRange();
12219 return false;
12220 }
12221 } else {
12222 ErrorInfo.Error = ErrorTy::InvalidBinaryOp;
12223 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CO->getCond()->getExprLoc();
12224 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12225 CO->getCond()->getSourceRange();
12226 return false;
12227 }
12228
12229 return true;
12230}
12231
12232bool OpenMPAtomicCompareChecker::checkType(ErrorInfoTy &ErrorInfo) const {
12233 // 'x' and 'e' cannot be nullptr
12234 assert(X && E && "X and E cannot be nullptr");
12235
12236 if (!CheckValue(E: X, ErrorInfo, ShouldBeLValue: true))
12237 return false;
12238
12239 if (!CheckValue(E, ErrorInfo, ShouldBeLValue: false))
12240 return false;
12241
12242 if (D && !CheckValue(E: D, ErrorInfo, ShouldBeLValue: false))
12243 return false;
12244
12245 return true;
12246}
12247
12248bool OpenMPAtomicCompareChecker::checkStmt(
12249 Stmt *S, OpenMPAtomicCompareChecker::ErrorInfoTy &ErrorInfo) {
12250 auto *CS = dyn_cast<CompoundStmt>(Val: S);
12251 if (CS) {
12252 if (CS->body_empty()) {
12253 ErrorInfo.Error = ErrorTy::NoStmt;
12254 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12255 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
12256 return false;
12257 }
12258
12259 if (CS->size() != 1) {
12260 ErrorInfo.Error = ErrorTy::MoreThanOneStmt;
12261 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12262 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
12263 return false;
12264 }
12265 S = CS->body_front();
12266 }
12267
12268 auto Res = false;
12269
12270 if (auto *IS = dyn_cast<IfStmt>(Val: S)) {
12271 // Check if the statement is in one of the following forms
12272 // (cond-update-stmt):
12273 // if (expr ordop x) { x = expr; }
12274 // if (x ordop expr) { x = expr; }
12275 // if (x == e) { x = d; }
12276 Res = checkCondUpdateStmt(S: IS, ErrorInfo);
12277 } else {
12278 // Check if the statement is in one of the following forms (cond-expr-stmt):
12279 // x = expr ordop x ? expr : x;
12280 // x = x ordop expr ? expr : x;
12281 // x = x == e ? d : x;
12282 Res = checkCondExprStmt(S, ErrorInfo);
12283 }
12284
12285 if (!Res)
12286 return false;
12287
12288 return checkType(ErrorInfo);
12289}
12290
12291class OpenMPAtomicCompareCaptureChecker final
12292 : public OpenMPAtomicCompareChecker {
12293public:
12294 OpenMPAtomicCompareCaptureChecker(Sema &S) : OpenMPAtomicCompareChecker(S) {}
12295
12296 Expr *getV() const { return V; }
12297 Expr *getR() const { return R; }
12298 bool isFailOnly() const { return IsFailOnly; }
12299 bool isPostfixUpdate() const { return IsPostfixUpdate; }
12300
12301 /// Check if statement \a S is valid for <tt>atomic compare capture</tt>.
12302 bool checkStmt(Stmt *S, ErrorInfoTy &ErrorInfo);
12303
12304private:
12305 bool checkType(ErrorInfoTy &ErrorInfo);
12306
12307 // NOTE: Form 3, 4, 5 in the following comments mean the 3rd, 4th, and 5th
12308 // form of 'conditional-update-capture-atomic' structured block on the v5.2
12309 // spec p.p. 82:
12310 // (1) { v = x; cond-update-stmt }
12311 // (2) { cond-update-stmt v = x; }
12312 // (3) if(x == e) { x = d; } else { v = x; }
12313 // (4) { r = x == e; if(r) { x = d; } }
12314 // (5) { r = x == e; if(r) { x = d; } else { v = x; } }
12315
12316 /// Check if it is valid 'if(x == e) { x = d; } else { v = x; }' (form 3)
12317 bool checkForm3(IfStmt *S, ErrorInfoTy &ErrorInfo);
12318
12319 /// Check if it is valid '{ r = x == e; if(r) { x = d; } }',
12320 /// or '{ r = x == e; if(r) { x = d; } else { v = x; } }' (form 4 and 5)
12321 bool checkForm45(Stmt *S, ErrorInfoTy &ErrorInfo);
12322
12323 /// 'v' lvalue part of the source atomic expression.
12324 Expr *V = nullptr;
12325 /// 'r' lvalue part of the source atomic expression.
12326 Expr *R = nullptr;
12327 /// If 'v' is only updated when the comparison fails.
12328 bool IsFailOnly = false;
12329 /// If original value of 'x' must be stored in 'v', not an updated one.
12330 bool IsPostfixUpdate = false;
12331};
12332
12333bool OpenMPAtomicCompareCaptureChecker::checkType(ErrorInfoTy &ErrorInfo) {
12334 if (!OpenMPAtomicCompareChecker::checkType(ErrorInfo))
12335 return false;
12336
12337 if (V && !CheckValue(E: V, ErrorInfo, ShouldBeLValue: true))
12338 return false;
12339
12340 if (R && !CheckValue(E: R, ErrorInfo, ShouldBeLValue: true, ShouldBeInteger: true))
12341 return false;
12342
12343 return true;
12344}
12345
12346bool OpenMPAtomicCompareCaptureChecker::checkForm3(IfStmt *S,
12347 ErrorInfoTy &ErrorInfo) {
12348 IsFailOnly = true;
12349
12350 auto *Then = S->getThen();
12351 if (auto *CS = dyn_cast<CompoundStmt>(Val: Then)) {
12352 if (CS->body_empty()) {
12353 ErrorInfo.Error = ErrorTy::NoStmt;
12354 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12355 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
12356 return false;
12357 }
12358 if (CS->size() > 1) {
12359 ErrorInfo.Error = ErrorTy::MoreThanOneStmt;
12360 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12361 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
12362 return false;
12363 }
12364 Then = CS->body_front();
12365 }
12366
12367 auto *BO = dyn_cast<BinaryOperator>(Val: Then);
12368 if (!BO) {
12369 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12370 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = Then->getBeginLoc();
12371 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Then->getSourceRange();
12372 return false;
12373 }
12374 if (BO->getOpcode() != BO_Assign) {
12375 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12376 ErrorInfo.ErrorLoc = BO->getExprLoc();
12377 ErrorInfo.NoteLoc = BO->getOperatorLoc();
12378 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getSourceRange();
12379 return false;
12380 }
12381
12382 X = BO->getLHS();
12383 D = BO->getRHS();
12384
12385 auto *Cond = dyn_cast<BinaryOperator>(Val: S->getCond());
12386 auto *Call = dyn_cast<CXXOperatorCallExpr>(Val: S->getCond());
12387 Expr *LHS = nullptr;
12388 Expr *RHS = nullptr;
12389 if (Cond) {
12390 LHS = Cond->getLHS();
12391 RHS = Cond->getRHS();
12392 } else if (Call) {
12393 LHS = Call->getArg(Arg: 0);
12394 RHS = Call->getArg(Arg: 1);
12395 } else {
12396 ErrorInfo.Error = ErrorTy::NotABinaryOp;
12397 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
12398 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getCond()->getSourceRange();
12399 return false;
12400 }
12401 if ((Cond && Cond->getOpcode() != BO_EQ) ||
12402 (Call && Call->getOperator() != OverloadedOperatorKind::OO_EqualEqual)) {
12403 ErrorInfo.Error = ErrorTy::NotEQ;
12404 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
12405 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getCond()->getSourceRange();
12406 return false;
12407 }
12408
12409 if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: LHS)) {
12410 E = RHS;
12411 } else if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS)) {
12412 E = LHS;
12413 } else {
12414 ErrorInfo.Error = ErrorTy::InvalidComparison;
12415 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
12416 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getCond()->getSourceRange();
12417 return false;
12418 }
12419
12420 C = S->getCond();
12421
12422 if (!S->getElse()) {
12423 ErrorInfo.Error = ErrorTy::NoElse;
12424 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getBeginLoc();
12425 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getSourceRange();
12426 return false;
12427 }
12428
12429 auto *Else = S->getElse();
12430 if (auto *CS = dyn_cast<CompoundStmt>(Val: Else)) {
12431 if (CS->body_empty()) {
12432 ErrorInfo.Error = ErrorTy::NoStmt;
12433 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12434 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
12435 return false;
12436 }
12437 if (CS->size() > 1) {
12438 ErrorInfo.Error = ErrorTy::MoreThanOneStmt;
12439 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12440 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getSourceRange();
12441 return false;
12442 }
12443 Else = CS->body_front();
12444 }
12445
12446 auto *ElseBO = dyn_cast<BinaryOperator>(Val: Else);
12447 if (!ElseBO) {
12448 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12449 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = Else->getBeginLoc();
12450 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Else->getSourceRange();
12451 return false;
12452 }
12453 if (ElseBO->getOpcode() != BO_Assign) {
12454 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12455 ErrorInfo.ErrorLoc = ElseBO->getExprLoc();
12456 ErrorInfo.NoteLoc = ElseBO->getOperatorLoc();
12457 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ElseBO->getSourceRange();
12458 return false;
12459 }
12460
12461 if (!checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: ElseBO->getRHS())) {
12462 ErrorInfo.Error = ErrorTy::InvalidAssignment;
12463 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = ElseBO->getRHS()->getExprLoc();
12464 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12465 ElseBO->getRHS()->getSourceRange();
12466 return false;
12467 }
12468
12469 V = ElseBO->getLHS();
12470
12471 return checkType(ErrorInfo);
12472}
12473
12474bool OpenMPAtomicCompareCaptureChecker::checkForm45(Stmt *S,
12475 ErrorInfoTy &ErrorInfo) {
12476 // We don't check here as they should be already done before call this
12477 // function.
12478 auto *CS = cast<CompoundStmt>(Val: S);
12479 assert(CS->size() == 2 && "CompoundStmt size is not expected");
12480 auto *S1 = cast<BinaryOperator>(Val: CS->body_front());
12481 auto *S2 = cast<IfStmt>(Val: CS->body_back());
12482 assert(S1->getOpcode() == BO_Assign && "unexpected binary operator");
12483
12484 if (!checkIfTwoExprsAreSame(Context&: ContextRef, LHS: S1->getLHS(), RHS: S2->getCond())) {
12485 ErrorInfo.Error = ErrorTy::InvalidCondition;
12486 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S2->getCond()->getExprLoc();
12487 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S1->getLHS()->getSourceRange();
12488 return false;
12489 }
12490
12491 R = S1->getLHS();
12492
12493 auto *Then = S2->getThen();
12494 if (auto *ThenCS = dyn_cast<CompoundStmt>(Val: Then)) {
12495 if (ThenCS->body_empty()) {
12496 ErrorInfo.Error = ErrorTy::NoStmt;
12497 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = ThenCS->getBeginLoc();
12498 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ThenCS->getSourceRange();
12499 return false;
12500 }
12501 if (ThenCS->size() > 1) {
12502 ErrorInfo.Error = ErrorTy::MoreThanOneStmt;
12503 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = ThenCS->getBeginLoc();
12504 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ThenCS->getSourceRange();
12505 return false;
12506 }
12507 Then = ThenCS->body_front();
12508 }
12509
12510 auto *ThenBO = dyn_cast<BinaryOperator>(Val: Then);
12511 if (!ThenBO) {
12512 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12513 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S2->getBeginLoc();
12514 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S2->getSourceRange();
12515 return false;
12516 }
12517 if (ThenBO->getOpcode() != BO_Assign) {
12518 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12519 ErrorInfo.ErrorLoc = ThenBO->getExprLoc();
12520 ErrorInfo.NoteLoc = ThenBO->getOperatorLoc();
12521 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ThenBO->getSourceRange();
12522 return false;
12523 }
12524
12525 X = ThenBO->getLHS();
12526 D = ThenBO->getRHS();
12527
12528 auto *BO = cast<BinaryOperator>(Val: S1->getRHS()->IgnoreImpCasts());
12529 if (BO->getOpcode() != BO_EQ) {
12530 ErrorInfo.Error = ErrorTy::NotEQ;
12531 ErrorInfo.ErrorLoc = BO->getExprLoc();
12532 ErrorInfo.NoteLoc = BO->getOperatorLoc();
12533 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getSourceRange();
12534 return false;
12535 }
12536
12537 C = BO;
12538
12539 if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: BO->getLHS())) {
12540 E = BO->getRHS();
12541 } else if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: BO->getRHS())) {
12542 E = BO->getLHS();
12543 } else {
12544 ErrorInfo.Error = ErrorTy::InvalidComparison;
12545 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = BO->getExprLoc();
12546 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getSourceRange();
12547 return false;
12548 }
12549
12550 if (S2->getElse()) {
12551 IsFailOnly = true;
12552
12553 auto *Else = S2->getElse();
12554 if (auto *ElseCS = dyn_cast<CompoundStmt>(Val: Else)) {
12555 if (ElseCS->body_empty()) {
12556 ErrorInfo.Error = ErrorTy::NoStmt;
12557 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = ElseCS->getBeginLoc();
12558 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ElseCS->getSourceRange();
12559 return false;
12560 }
12561 if (ElseCS->size() > 1) {
12562 ErrorInfo.Error = ErrorTy::MoreThanOneStmt;
12563 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = ElseCS->getBeginLoc();
12564 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ElseCS->getSourceRange();
12565 return false;
12566 }
12567 Else = ElseCS->body_front();
12568 }
12569
12570 auto *ElseBO = dyn_cast<BinaryOperator>(Val: Else);
12571 if (!ElseBO) {
12572 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12573 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = Else->getBeginLoc();
12574 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Else->getSourceRange();
12575 return false;
12576 }
12577 if (ElseBO->getOpcode() != BO_Assign) {
12578 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12579 ErrorInfo.ErrorLoc = ElseBO->getExprLoc();
12580 ErrorInfo.NoteLoc = ElseBO->getOperatorLoc();
12581 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ElseBO->getSourceRange();
12582 return false;
12583 }
12584 if (!checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: ElseBO->getRHS())) {
12585 ErrorInfo.Error = ErrorTy::InvalidAssignment;
12586 ErrorInfo.ErrorLoc = ElseBO->getRHS()->getExprLoc();
12587 ErrorInfo.NoteLoc = X->getExprLoc();
12588 ErrorInfo.ErrorRange = ElseBO->getRHS()->getSourceRange();
12589 ErrorInfo.NoteRange = X->getSourceRange();
12590 return false;
12591 }
12592
12593 V = ElseBO->getLHS();
12594 }
12595
12596 return checkType(ErrorInfo);
12597}
12598
12599bool OpenMPAtomicCompareCaptureChecker::checkStmt(Stmt *S,
12600 ErrorInfoTy &ErrorInfo) {
12601 // if(x == e) { x = d; } else { v = x; }
12602 if (auto *IS = dyn_cast<IfStmt>(Val: S))
12603 return checkForm3(S: IS, ErrorInfo);
12604
12605 auto *CS = dyn_cast<CompoundStmt>(Val: S);
12606 if (!CS) {
12607 ErrorInfo.Error = ErrorTy::NotCompoundStmt;
12608 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getBeginLoc();
12609 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getSourceRange();
12610 return false;
12611 }
12612 if (CS->body_empty()) {
12613 ErrorInfo.Error = ErrorTy::NoStmt;
12614 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12615 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
12616 return false;
12617 }
12618
12619 // { if(x == e) { x = d; } else { v = x; } }
12620 if (CS->size() == 1) {
12621 auto *IS = dyn_cast<IfStmt>(Val: CS->body_front());
12622 if (!IS) {
12623 ErrorInfo.Error = ErrorTy::NotIfStmt;
12624 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->body_front()->getBeginLoc();
12625 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12626 CS->body_front()->getSourceRange();
12627 return false;
12628 }
12629
12630 return checkForm3(S: IS, ErrorInfo);
12631 } else if (CS->size() == 2) {
12632 auto *S1 = CS->body_front();
12633 auto *S2 = CS->body_back();
12634
12635 Stmt *UpdateStmt = nullptr;
12636 Stmt *CondUpdateStmt = nullptr;
12637 Stmt *CondExprStmt = nullptr;
12638
12639 if (auto *BO = dyn_cast<BinaryOperator>(Val: S1)) {
12640 // It could be one of the following cases:
12641 // { v = x; cond-update-stmt }
12642 // { v = x; cond-expr-stmt }
12643 // { cond-expr-stmt; v = x; }
12644 // form 45
12645 if (isa<BinaryOperator>(Val: BO->getRHS()->IgnoreImpCasts()) ||
12646 isa<ConditionalOperator>(Val: BO->getRHS()->IgnoreImpCasts())) {
12647 // check if form 45
12648 if (isa<IfStmt>(Val: S2))
12649 return checkForm45(S: CS, ErrorInfo);
12650 // { cond-expr-stmt; v = x; }
12651 CondExprStmt = S1;
12652 UpdateStmt = S2;
12653 } else {
12654 IsPostfixUpdate = true;
12655 UpdateStmt = S1;
12656 if (isa<IfStmt>(Val: S2)) {
12657 // { v = x; cond-update-stmt }
12658 CondUpdateStmt = S2;
12659 } else {
12660 // { v = x; cond-expr-stmt }
12661 CondExprStmt = S2;
12662 }
12663 }
12664 } else {
12665 // { cond-update-stmt v = x; }
12666 UpdateStmt = S2;
12667 CondUpdateStmt = S1;
12668 }
12669
12670 auto CheckCondUpdateStmt = [this, &ErrorInfo](Stmt *CUS) {
12671 auto *IS = dyn_cast<IfStmt>(Val: CUS);
12672 if (!IS) {
12673 ErrorInfo.Error = ErrorTy::NotIfStmt;
12674 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CUS->getBeginLoc();
12675 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CUS->getSourceRange();
12676 return false;
12677 }
12678
12679 return checkCondUpdateStmt(S: IS, ErrorInfo);
12680 };
12681
12682 // CheckUpdateStmt has to be called *after* CheckCondUpdateStmt.
12683 auto CheckUpdateStmt = [this, &ErrorInfo](Stmt *US) {
12684 auto *BO = dyn_cast<BinaryOperator>(Val: US);
12685 if (!BO) {
12686 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12687 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = US->getBeginLoc();
12688 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = US->getSourceRange();
12689 return false;
12690 }
12691 if (BO->getOpcode() != BO_Assign) {
12692 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12693 ErrorInfo.ErrorLoc = BO->getExprLoc();
12694 ErrorInfo.NoteLoc = BO->getOperatorLoc();
12695 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getSourceRange();
12696 return false;
12697 }
12698 if (!checkIfTwoExprsAreSame(Context&: ContextRef, LHS: this->X, RHS: BO->getRHS())) {
12699 ErrorInfo.Error = ErrorTy::InvalidAssignment;
12700 ErrorInfo.ErrorLoc = BO->getRHS()->getExprLoc();
12701 ErrorInfo.NoteLoc = this->X->getExprLoc();
12702 ErrorInfo.ErrorRange = BO->getRHS()->getSourceRange();
12703 ErrorInfo.NoteRange = this->X->getSourceRange();
12704 return false;
12705 }
12706
12707 this->V = BO->getLHS();
12708
12709 return true;
12710 };
12711
12712 if (CondUpdateStmt && !CheckCondUpdateStmt(CondUpdateStmt))
12713 return false;
12714 if (CondExprStmt && !checkCondExprStmt(S: CondExprStmt, ErrorInfo))
12715 return false;
12716 if (!CheckUpdateStmt(UpdateStmt))
12717 return false;
12718 } else {
12719 ErrorInfo.Error = ErrorTy::MoreThanTwoStmts;
12720 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12721 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
12722 return false;
12723 }
12724
12725 return checkType(ErrorInfo);
12726}
12727} // namespace
12728
12729StmtResult SemaOpenMP::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
12730 Stmt *AStmt,
12731 SourceLocation StartLoc,
12732 SourceLocation EndLoc) {
12733 ASTContext &Context = getASTContext();
12734 unsigned OMPVersion = getLangOpts().OpenMP;
12735 // Register location of the first atomic directive.
12736 DSAStack->addAtomicDirectiveLoc(Loc: StartLoc);
12737 if (!AStmt)
12738 return StmtError();
12739
12740 // 1.2.2 OpenMP Language Terminology
12741 // Structured block - An executable statement with a single entry at the
12742 // top and a single exit at the bottom.
12743 // The point of exit cannot be a branch out of the structured block.
12744 // longjmp() and throw() must not violate the entry/exit criteria.
12745 OpenMPClauseKind AtomicKind = OMPC_unknown;
12746 SourceLocation AtomicKindLoc;
12747 OpenMPClauseKind MemOrderKind = OMPC_unknown;
12748 SourceLocation MemOrderLoc;
12749 bool MutexClauseEncountered = false;
12750 llvm::SmallSet<OpenMPClauseKind, 2> EncounteredAtomicKinds;
12751 for (const OMPClause *C : Clauses) {
12752 switch (C->getClauseKind()) {
12753 case OMPC_read:
12754 case OMPC_write:
12755 case OMPC_update:
12756 MutexClauseEncountered = true;
12757 [[fallthrough]];
12758 case OMPC_capture:
12759 case OMPC_compare: {
12760 if (AtomicKind != OMPC_unknown && MutexClauseEncountered) {
12761 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_atomic_several_clauses)
12762 << SourceRange(C->getBeginLoc(), C->getEndLoc());
12763 Diag(Loc: AtomicKindLoc, DiagID: diag::note_omp_previous_mem_order_clause)
12764 << getOpenMPClauseNameForDiag(C: AtomicKind);
12765 } else {
12766 AtomicKind = C->getClauseKind();
12767 AtomicKindLoc = C->getBeginLoc();
12768 if (!EncounteredAtomicKinds.insert(V: C->getClauseKind()).second) {
12769 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_atomic_several_clauses)
12770 << SourceRange(C->getBeginLoc(), C->getEndLoc());
12771 Diag(Loc: AtomicKindLoc, DiagID: diag::note_omp_previous_mem_order_clause)
12772 << getOpenMPClauseNameForDiag(C: AtomicKind);
12773 }
12774 }
12775 break;
12776 }
12777 case OMPC_weak:
12778 case OMPC_fail: {
12779 if (!EncounteredAtomicKinds.contains(V: OMPC_compare)) {
12780 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_atomic_no_compare)
12781 << getOpenMPClauseNameForDiag(C: C->getClauseKind())
12782 << SourceRange(C->getBeginLoc(), C->getEndLoc());
12783 return StmtError();
12784 }
12785 break;
12786 }
12787 case OMPC_seq_cst:
12788 case OMPC_acq_rel:
12789 case OMPC_acquire:
12790 case OMPC_release:
12791 case OMPC_relaxed: {
12792 if (MemOrderKind != OMPC_unknown) {
12793 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_several_mem_order_clauses)
12794 << getOpenMPDirectiveName(D: OMPD_atomic, Ver: OMPVersion) << 0
12795 << SourceRange(C->getBeginLoc(), C->getEndLoc());
12796 Diag(Loc: MemOrderLoc, DiagID: diag::note_omp_previous_mem_order_clause)
12797 << getOpenMPClauseNameForDiag(C: MemOrderKind);
12798 } else {
12799 MemOrderKind = C->getClauseKind();
12800 MemOrderLoc = C->getBeginLoc();
12801 }
12802 break;
12803 }
12804 // The following clauses are allowed, but we don't need to do anything here.
12805 case OMPC_hint:
12806 break;
12807 default:
12808 llvm_unreachable("unknown clause is encountered");
12809 }
12810 }
12811 bool IsCompareCapture = false;
12812 if (EncounteredAtomicKinds.contains(V: OMPC_compare) &&
12813 EncounteredAtomicKinds.contains(V: OMPC_capture)) {
12814 IsCompareCapture = true;
12815 AtomicKind = OMPC_compare;
12816 }
12817 // OpenMP 5.0, 2.17.7 atomic Construct, Restrictions
12818 // If atomic-clause is read then memory-order-clause must not be acq_rel or
12819 // release.
12820 // If atomic-clause is write then memory-order-clause must not be acq_rel or
12821 // acquire.
12822 // If atomic-clause is update or not present then memory-order-clause must not
12823 // be acq_rel or acquire.
12824 if ((AtomicKind == OMPC_read &&
12825 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_release)) ||
12826 ((AtomicKind == OMPC_write || AtomicKind == OMPC_update ||
12827 AtomicKind == OMPC_unknown) &&
12828 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_acquire))) {
12829 SourceLocation Loc = AtomicKindLoc;
12830 if (AtomicKind == OMPC_unknown)
12831 Loc = StartLoc;
12832 Diag(Loc, DiagID: diag::err_omp_atomic_incompatible_mem_order_clause)
12833 << getOpenMPClauseNameForDiag(C: AtomicKind)
12834 << (AtomicKind == OMPC_unknown ? 1 : 0)
12835 << getOpenMPClauseNameForDiag(C: MemOrderKind);
12836 Diag(Loc: MemOrderLoc, DiagID: diag::note_omp_previous_mem_order_clause)
12837 << getOpenMPClauseNameForDiag(C: MemOrderKind);
12838 }
12839
12840 Stmt *Body = AStmt;
12841 if (auto *EWC = dyn_cast<ExprWithCleanups>(Val: Body))
12842 Body = EWC->getSubExpr();
12843
12844 Expr *X = nullptr;
12845 Expr *V = nullptr;
12846 Expr *E = nullptr;
12847 Expr *UE = nullptr;
12848 Expr *D = nullptr;
12849 Expr *CE = nullptr;
12850 Expr *R = nullptr;
12851 bool IsXLHSInRHSPart = false;
12852 bool IsPostfixUpdate = false;
12853 bool IsFailOnly = false;
12854 // OpenMP [2.12.6, atomic Construct]
12855 // In the next expressions:
12856 // * x and v (as applicable) are both l-value expressions with scalar type.
12857 // * During the execution of an atomic region, multiple syntactic
12858 // occurrences of x must designate the same storage location.
12859 // * Neither of v and expr (as applicable) may access the storage location
12860 // designated by x.
12861 // * Neither of x and expr (as applicable) may access the storage location
12862 // designated by v.
12863 // * expr is an expression with scalar type.
12864 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
12865 // * binop, binop=, ++, and -- are not overloaded operators.
12866 // * The expression x binop expr must be numerically equivalent to x binop
12867 // (expr). This requirement is satisfied if the operators in expr have
12868 // precedence greater than binop, or by using parentheses around expr or
12869 // subexpressions of expr.
12870 // * The expression expr binop x must be numerically equivalent to (expr)
12871 // binop x. This requirement is satisfied if the operators in expr have
12872 // precedence equal to or greater than binop, or by using parentheses around
12873 // expr or subexpressions of expr.
12874 // * For forms that allow multiple occurrences of x, the number of times
12875 // that x is evaluated is unspecified.
12876 if (AtomicKind == OMPC_read) {
12877 enum {
12878 NotAnExpression,
12879 NotAnAssignmentOp,
12880 NotAScalarType,
12881 NotAnLValue,
12882 NoError
12883 } ErrorFound = NoError;
12884 SourceLocation ErrorLoc, NoteLoc;
12885 SourceRange ErrorRange, NoteRange;
12886 // If clause is read:
12887 // v = x;
12888 if (const auto *AtomicBody = dyn_cast<Expr>(Val: Body)) {
12889 const auto *AtomicBinOp =
12890 dyn_cast<BinaryOperator>(Val: AtomicBody->IgnoreParenImpCasts());
12891 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
12892 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
12893 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
12894 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
12895 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
12896 if (!X->isLValue() || !V->isLValue()) {
12897 const Expr *NotLValueExpr = X->isLValue() ? V : X;
12898 ErrorFound = NotAnLValue;
12899 ErrorLoc = AtomicBinOp->getExprLoc();
12900 ErrorRange = AtomicBinOp->getSourceRange();
12901 NoteLoc = NotLValueExpr->getExprLoc();
12902 NoteRange = NotLValueExpr->getSourceRange();
12903 }
12904 } else if (!X->isInstantiationDependent() ||
12905 !V->isInstantiationDependent()) {
12906 const Expr *NotScalarExpr =
12907 (X->isInstantiationDependent() || X->getType()->isScalarType())
12908 ? V
12909 : X;
12910 ErrorFound = NotAScalarType;
12911 ErrorLoc = AtomicBinOp->getExprLoc();
12912 ErrorRange = AtomicBinOp->getSourceRange();
12913 NoteLoc = NotScalarExpr->getExprLoc();
12914 NoteRange = NotScalarExpr->getSourceRange();
12915 }
12916 } else if (!AtomicBody->isInstantiationDependent()) {
12917 ErrorFound = NotAnAssignmentOp;
12918 ErrorLoc = AtomicBody->getExprLoc();
12919 ErrorRange = AtomicBody->getSourceRange();
12920 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
12921 : AtomicBody->getExprLoc();
12922 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
12923 : AtomicBody->getSourceRange();
12924 }
12925 } else {
12926 ErrorFound = NotAnExpression;
12927 NoteLoc = ErrorLoc = Body->getBeginLoc();
12928 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
12929 }
12930 if (ErrorFound != NoError) {
12931 Diag(Loc: ErrorLoc, DiagID: diag::err_omp_atomic_read_not_expression_statement)
12932 << ErrorRange;
12933 Diag(Loc: NoteLoc, DiagID: diag::note_omp_atomic_read_write)
12934 << ErrorFound << NoteRange;
12935 return StmtError();
12936 }
12937 if (SemaRef.CurContext->isDependentContext())
12938 V = X = nullptr;
12939 } else if (AtomicKind == OMPC_write) {
12940 enum {
12941 NotAnExpression,
12942 NotAnAssignmentOp,
12943 NotAScalarType,
12944 NotAnLValue,
12945 NoError
12946 } ErrorFound = NoError;
12947 SourceLocation ErrorLoc, NoteLoc;
12948 SourceRange ErrorRange, NoteRange;
12949 // If clause is write:
12950 // x = expr;
12951 if (const auto *AtomicBody = dyn_cast<Expr>(Val: Body)) {
12952 const auto *AtomicBinOp =
12953 dyn_cast<BinaryOperator>(Val: AtomicBody->IgnoreParenImpCasts());
12954 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
12955 X = AtomicBinOp->getLHS();
12956 E = AtomicBinOp->getRHS();
12957 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
12958 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
12959 if (!X->isLValue()) {
12960 ErrorFound = NotAnLValue;
12961 ErrorLoc = AtomicBinOp->getExprLoc();
12962 ErrorRange = AtomicBinOp->getSourceRange();
12963 NoteLoc = X->getExprLoc();
12964 NoteRange = X->getSourceRange();
12965 }
12966 } else if (!X->isInstantiationDependent() ||
12967 !E->isInstantiationDependent()) {
12968 const Expr *NotScalarExpr =
12969 (X->isInstantiationDependent() || X->getType()->isScalarType())
12970 ? E
12971 : X;
12972 ErrorFound = NotAScalarType;
12973 ErrorLoc = AtomicBinOp->getExprLoc();
12974 ErrorRange = AtomicBinOp->getSourceRange();
12975 NoteLoc = NotScalarExpr->getExprLoc();
12976 NoteRange = NotScalarExpr->getSourceRange();
12977 }
12978 } else if (!AtomicBody->isInstantiationDependent()) {
12979 ErrorFound = NotAnAssignmentOp;
12980 ErrorLoc = AtomicBody->getExprLoc();
12981 ErrorRange = AtomicBody->getSourceRange();
12982 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
12983 : AtomicBody->getExprLoc();
12984 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
12985 : AtomicBody->getSourceRange();
12986 }
12987 } else {
12988 ErrorFound = NotAnExpression;
12989 NoteLoc = ErrorLoc = Body->getBeginLoc();
12990 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
12991 }
12992 if (ErrorFound != NoError) {
12993 Diag(Loc: ErrorLoc, DiagID: diag::err_omp_atomic_write_not_expression_statement)
12994 << ErrorRange;
12995 Diag(Loc: NoteLoc, DiagID: diag::note_omp_atomic_read_write)
12996 << ErrorFound << NoteRange;
12997 return StmtError();
12998 }
12999 if (SemaRef.CurContext->isDependentContext())
13000 E = X = nullptr;
13001 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
13002 // If clause is update:
13003 // x++;
13004 // x--;
13005 // ++x;
13006 // --x;
13007 // x binop= expr;
13008 // x = x binop expr;
13009 // x = expr binop x;
13010 OpenMPAtomicUpdateChecker Checker(SemaRef);
13011 if (Checker.checkStatement(
13012 S: Body,
13013 DiagId: (AtomicKind == OMPC_update)
13014 ? diag::err_omp_atomic_update_not_expression_statement
13015 : diag::err_omp_atomic_not_expression_statement,
13016 NoteId: diag::note_omp_atomic_update))
13017 return StmtError();
13018 if (!SemaRef.CurContext->isDependentContext()) {
13019 E = Checker.getExpr();
13020 X = Checker.getX();
13021 UE = Checker.getUpdateExpr();
13022 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
13023 }
13024 } else if (AtomicKind == OMPC_capture) {
13025 enum {
13026 NotAnAssignmentOp,
13027 NotACompoundStatement,
13028 NotTwoSubstatements,
13029 NotASpecificExpression,
13030 NoError
13031 } ErrorFound = NoError;
13032 SourceLocation ErrorLoc, NoteLoc;
13033 SourceRange ErrorRange, NoteRange;
13034 if (const auto *AtomicBody = dyn_cast<Expr>(Val: Body)) {
13035 // If clause is a capture:
13036 // v = x++;
13037 // v = x--;
13038 // v = ++x;
13039 // v = --x;
13040 // v = x binop= expr;
13041 // v = x = x binop expr;
13042 // v = x = expr binop x;
13043 const auto *AtomicBinOp =
13044 dyn_cast<BinaryOperator>(Val: AtomicBody->IgnoreParenImpCasts());
13045 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
13046 V = AtomicBinOp->getLHS();
13047 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
13048 OpenMPAtomicUpdateChecker Checker(SemaRef);
13049 if (Checker.checkStatement(
13050 S: Body, DiagId: diag::err_omp_atomic_capture_not_expression_statement,
13051 NoteId: diag::note_omp_atomic_update))
13052 return StmtError();
13053 E = Checker.getExpr();
13054 X = Checker.getX();
13055 UE = Checker.getUpdateExpr();
13056 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
13057 IsPostfixUpdate = Checker.isPostfixUpdate();
13058 } else if (!AtomicBody->isInstantiationDependent()) {
13059 ErrorLoc = AtomicBody->getExprLoc();
13060 ErrorRange = AtomicBody->getSourceRange();
13061 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
13062 : AtomicBody->getExprLoc();
13063 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
13064 : AtomicBody->getSourceRange();
13065 ErrorFound = NotAnAssignmentOp;
13066 }
13067 if (ErrorFound != NoError) {
13068 Diag(Loc: ErrorLoc, DiagID: diag::err_omp_atomic_capture_not_expression_statement)
13069 << ErrorRange;
13070 Diag(Loc: NoteLoc, DiagID: diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
13071 return StmtError();
13072 }
13073 if (SemaRef.CurContext->isDependentContext())
13074 UE = V = E = X = nullptr;
13075 } else {
13076 // If clause is a capture:
13077 // { v = x; x = expr; }
13078 // { v = x; x++; }
13079 // { v = x; x--; }
13080 // { v = x; ++x; }
13081 // { v = x; --x; }
13082 // { v = x; x binop= expr; }
13083 // { v = x; x = x binop expr; }
13084 // { v = x; x = expr binop x; }
13085 // { x++; v = x; }
13086 // { x--; v = x; }
13087 // { ++x; v = x; }
13088 // { --x; v = x; }
13089 // { x binop= expr; v = x; }
13090 // { x = x binop expr; v = x; }
13091 // { x = expr binop x; v = x; }
13092 if (auto *CS = dyn_cast<CompoundStmt>(Val: Body)) {
13093 // Check that this is { expr1; expr2; }
13094 if (CS->size() == 2) {
13095 Stmt *First = CS->body_front();
13096 Stmt *Second = CS->body_back();
13097 if (auto *EWC = dyn_cast<ExprWithCleanups>(Val: First))
13098 First = EWC->getSubExpr()->IgnoreParenImpCasts();
13099 if (auto *EWC = dyn_cast<ExprWithCleanups>(Val: Second))
13100 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
13101 // Need to find what subexpression is 'v' and what is 'x'.
13102 OpenMPAtomicUpdateChecker Checker(SemaRef);
13103 bool IsUpdateExprFound = !Checker.checkStatement(S: Second);
13104 BinaryOperator *BinOp = nullptr;
13105 if (IsUpdateExprFound) {
13106 BinOp = dyn_cast<BinaryOperator>(Val: First);
13107 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
13108 }
13109 if (IsUpdateExprFound && !SemaRef.CurContext->isDependentContext()) {
13110 // { v = x; x++; }
13111 // { v = x; x--; }
13112 // { v = x; ++x; }
13113 // { v = x; --x; }
13114 // { v = x; x binop= expr; }
13115 // { v = x; x = x binop expr; }
13116 // { v = x; x = expr binop x; }
13117 // Check that the first expression has form v = x.
13118 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
13119 llvm::FoldingSetNodeID XId, PossibleXId;
13120 Checker.getX()->Profile(ID&: XId, Context, /*Canonical=*/true);
13121 PossibleX->Profile(ID&: PossibleXId, Context, /*Canonical=*/true);
13122 IsUpdateExprFound = XId == PossibleXId;
13123 if (IsUpdateExprFound) {
13124 V = BinOp->getLHS();
13125 X = Checker.getX();
13126 E = Checker.getExpr();
13127 UE = Checker.getUpdateExpr();
13128 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
13129 IsPostfixUpdate = true;
13130 }
13131 }
13132 if (!IsUpdateExprFound) {
13133 IsUpdateExprFound = !Checker.checkStatement(S: First);
13134 BinOp = nullptr;
13135 if (IsUpdateExprFound) {
13136 BinOp = dyn_cast<BinaryOperator>(Val: Second);
13137 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
13138 }
13139 if (IsUpdateExprFound &&
13140 !SemaRef.CurContext->isDependentContext()) {
13141 // { x++; v = x; }
13142 // { x--; v = x; }
13143 // { ++x; v = x; }
13144 // { --x; v = x; }
13145 // { x binop= expr; v = x; }
13146 // { x = x binop expr; v = x; }
13147 // { x = expr binop x; v = x; }
13148 // Check that the second expression has form v = x.
13149 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
13150 llvm::FoldingSetNodeID XId, PossibleXId;
13151 Checker.getX()->Profile(ID&: XId, Context, /*Canonical=*/true);
13152 PossibleX->Profile(ID&: PossibleXId, Context, /*Canonical=*/true);
13153 IsUpdateExprFound = XId == PossibleXId;
13154 if (IsUpdateExprFound) {
13155 V = BinOp->getLHS();
13156 X = Checker.getX();
13157 E = Checker.getExpr();
13158 UE = Checker.getUpdateExpr();
13159 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
13160 IsPostfixUpdate = false;
13161 }
13162 }
13163 }
13164 if (!IsUpdateExprFound) {
13165 // { v = x; x = expr; }
13166 auto *FirstExpr = dyn_cast<Expr>(Val: First);
13167 auto *SecondExpr = dyn_cast<Expr>(Val: Second);
13168 if (!FirstExpr || !SecondExpr ||
13169 !(FirstExpr->isInstantiationDependent() ||
13170 SecondExpr->isInstantiationDependent())) {
13171 auto *FirstBinOp = dyn_cast<BinaryOperator>(Val: First);
13172 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
13173 ErrorFound = NotAnAssignmentOp;
13174 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
13175 : First->getBeginLoc();
13176 NoteRange = ErrorRange = FirstBinOp
13177 ? FirstBinOp->getSourceRange()
13178 : SourceRange(ErrorLoc, ErrorLoc);
13179 } else {
13180 auto *SecondBinOp = dyn_cast<BinaryOperator>(Val: Second);
13181 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
13182 ErrorFound = NotAnAssignmentOp;
13183 NoteLoc = ErrorLoc = SecondBinOp
13184 ? SecondBinOp->getOperatorLoc()
13185 : Second->getBeginLoc();
13186 NoteRange = ErrorRange =
13187 SecondBinOp ? SecondBinOp->getSourceRange()
13188 : SourceRange(ErrorLoc, ErrorLoc);
13189 } else {
13190 Expr *PossibleXRHSInFirst =
13191 FirstBinOp->getRHS()->IgnoreParenImpCasts();
13192 Expr *PossibleXLHSInSecond =
13193 SecondBinOp->getLHS()->IgnoreParenImpCasts();
13194 llvm::FoldingSetNodeID X1Id, X2Id;
13195 PossibleXRHSInFirst->Profile(ID&: X1Id, Context,
13196 /*Canonical=*/true);
13197 PossibleXLHSInSecond->Profile(ID&: X2Id, Context,
13198 /*Canonical=*/true);
13199 IsUpdateExprFound = X1Id == X2Id;
13200 if (IsUpdateExprFound) {
13201 V = FirstBinOp->getLHS();
13202 X = SecondBinOp->getLHS();
13203 E = SecondBinOp->getRHS();
13204 UE = nullptr;
13205 IsXLHSInRHSPart = false;
13206 IsPostfixUpdate = true;
13207 } else {
13208 ErrorFound = NotASpecificExpression;
13209 ErrorLoc = FirstBinOp->getExprLoc();
13210 ErrorRange = FirstBinOp->getSourceRange();
13211 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
13212 NoteRange = SecondBinOp->getRHS()->getSourceRange();
13213 }
13214 }
13215 }
13216 }
13217 }
13218 } else {
13219 NoteLoc = ErrorLoc = Body->getBeginLoc();
13220 NoteRange = ErrorRange =
13221 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
13222 ErrorFound = NotTwoSubstatements;
13223 }
13224 } else {
13225 NoteLoc = ErrorLoc = Body->getBeginLoc();
13226 NoteRange = ErrorRange =
13227 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
13228 ErrorFound = NotACompoundStatement;
13229 }
13230 }
13231 if (ErrorFound != NoError) {
13232 Diag(Loc: ErrorLoc, DiagID: diag::err_omp_atomic_capture_not_compound_statement)
13233 << ErrorRange;
13234 Diag(Loc: NoteLoc, DiagID: diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
13235 return StmtError();
13236 }
13237 if (SemaRef.CurContext->isDependentContext())
13238 UE = V = E = X = nullptr;
13239 } else if (AtomicKind == OMPC_compare) {
13240 if (IsCompareCapture) {
13241 OpenMPAtomicCompareCaptureChecker::ErrorInfoTy ErrorInfo;
13242 OpenMPAtomicCompareCaptureChecker Checker(SemaRef);
13243 if (!Checker.checkStmt(S: Body, ErrorInfo)) {
13244 Diag(Loc: ErrorInfo.ErrorLoc, DiagID: diag::err_omp_atomic_compare_capture)
13245 << ErrorInfo.ErrorRange;
13246 Diag(Loc: ErrorInfo.NoteLoc, DiagID: diag::note_omp_atomic_compare)
13247 << ErrorInfo.Error << ErrorInfo.NoteRange;
13248 return StmtError();
13249 }
13250 X = Checker.getX();
13251 E = Checker.getE();
13252 D = Checker.getD();
13253 CE = Checker.getCond();
13254 V = Checker.getV();
13255 R = Checker.getR();
13256 // We reuse IsXLHSInRHSPart to tell if it is in the form 'x ordop expr'.
13257 IsXLHSInRHSPart = Checker.isXBinopExpr();
13258 IsFailOnly = Checker.isFailOnly();
13259 IsPostfixUpdate = Checker.isPostfixUpdate();
13260 } else {
13261 OpenMPAtomicCompareChecker::ErrorInfoTy ErrorInfo;
13262 OpenMPAtomicCompareChecker Checker(SemaRef);
13263 if (!Checker.checkStmt(S: Body, ErrorInfo)) {
13264 Diag(Loc: ErrorInfo.ErrorLoc, DiagID: diag::err_omp_atomic_compare)
13265 << ErrorInfo.ErrorRange;
13266 Diag(Loc: ErrorInfo.NoteLoc, DiagID: diag::note_omp_atomic_compare)
13267 << ErrorInfo.Error << ErrorInfo.NoteRange;
13268 return StmtError();
13269 }
13270 X = Checker.getX();
13271 E = Checker.getE();
13272 D = Checker.getD();
13273 CE = Checker.getCond();
13274 // The weak clause may only appear if the resulting atomic operation is
13275 // an atomic conditional update for which the comparison tests for
13276 // equality. It was not possible to do this check in
13277 // OpenMPAtomicCompareChecker::checkStmt() as the check for OMPC_weak
13278 // could not be performed (Clauses are not available).
13279 auto *It = find_if(Range&: Clauses, P: [](OMPClause *C) {
13280 return C->getClauseKind() == llvm::omp::Clause::OMPC_weak;
13281 });
13282 if (It != Clauses.end()) {
13283 auto *Cond = dyn_cast<BinaryOperator>(Val: CE);
13284 if (Cond->getOpcode() != BO_EQ) {
13285 ErrorInfo.Error = Checker.ErrorTy::NotAnAssignment;
13286 ErrorInfo.ErrorLoc = Cond->getExprLoc();
13287 ErrorInfo.NoteLoc = Cond->getOperatorLoc();
13288 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Cond->getSourceRange();
13289
13290 Diag(Loc: ErrorInfo.ErrorLoc, DiagID: diag::err_omp_atomic_weak_no_equality)
13291 << ErrorInfo.ErrorRange;
13292 return StmtError();
13293 }
13294 }
13295 // We reuse IsXLHSInRHSPart to tell if it is in the form 'x ordop expr'.
13296 IsXLHSInRHSPart = Checker.isXBinopExpr();
13297 }
13298 }
13299
13300 SemaRef.setFunctionHasBranchProtectedScope();
13301
13302 return OMPAtomicDirective::Create(
13303 C: Context, StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
13304 Exprs: {.X: X, .V: V, .R: R, .E: E, .UE: UE, .D: D, .Cond: CE, .IsXLHSInRHSPart: IsXLHSInRHSPart, .IsPostfixUpdate: IsPostfixUpdate, .IsFailOnly: IsFailOnly});
13305}
13306
13307StmtResult SemaOpenMP::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
13308 Stmt *AStmt,
13309 SourceLocation StartLoc,
13310 SourceLocation EndLoc) {
13311 if (!AStmt)
13312 return StmtError();
13313
13314 CapturedStmt *CS = setBranchProtectedScope(SemaRef, DKind: OMPD_target, AStmt);
13315
13316 // OpenMP [2.16, Nesting of Regions]
13317 // If specified, a teams construct must be contained within a target
13318 // construct. That target construct must contain no statements or directives
13319 // outside of the teams construct.
13320 if (DSAStack->hasInnerTeamsRegion()) {
13321 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
13322 bool OMPTeamsFound = true;
13323 if (const auto *CS = dyn_cast<CompoundStmt>(Val: S)) {
13324 auto I = CS->body_begin();
13325 while (I != CS->body_end()) {
13326 const auto *OED = dyn_cast<OMPExecutableDirective>(Val: *I);
13327 bool IsTeams = OED && isOpenMPTeamsDirective(DKind: OED->getDirectiveKind());
13328 if (!IsTeams || I != CS->body_begin()) {
13329 OMPTeamsFound = false;
13330 if (IsTeams && I != CS->body_begin()) {
13331 // This is the two teams case. Since the InnerTeamsRegionLoc will
13332 // point to this second one reset the iterator to the other teams.
13333 --I;
13334 }
13335 break;
13336 }
13337 ++I;
13338 }
13339 assert(I != CS->body_end() && "Not found statement");
13340 S = *I;
13341 } else {
13342 const auto *OED = dyn_cast<OMPExecutableDirective>(Val: S);
13343 OMPTeamsFound = OED && isOpenMPTeamsDirective(DKind: OED->getDirectiveKind());
13344 }
13345 if (!OMPTeamsFound) {
13346 Diag(Loc: StartLoc, DiagID: diag::err_omp_target_contains_not_only_teams);
13347 Diag(DSAStack->getInnerTeamsRegionLoc(),
13348 DiagID: diag::note_omp_nested_teams_construct_here);
13349 Diag(Loc: S->getBeginLoc(), DiagID: diag::note_omp_nested_statement_here)
13350 << isa<OMPExecutableDirective>(Val: S);
13351 return StmtError();
13352 }
13353 }
13354
13355 return OMPTargetDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
13356 AssociatedStmt: AStmt);
13357}
13358
13359StmtResult SemaOpenMP::ActOnOpenMPTargetParallelDirective(
13360 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13361 SourceLocation EndLoc) {
13362 if (!AStmt)
13363 return StmtError();
13364
13365 setBranchProtectedScope(SemaRef, DKind: OMPD_target_parallel, AStmt);
13366
13367 return OMPTargetParallelDirective::Create(
13368 C: getASTContext(), StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
13369 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
13370}
13371
13372StmtResult SemaOpenMP::ActOnOpenMPTargetParallelForDirective(
13373 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13374 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13375 if (!AStmt)
13376 return StmtError();
13377
13378 CapturedStmt *CS =
13379 setBranchProtectedScope(SemaRef, DKind: OMPD_target_parallel_for, AStmt);
13380
13381 OMPLoopBasedDirective::HelperExprs B;
13382 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13383 // define the nested loops number.
13384 unsigned NestedLoopCount =
13385 checkOpenMPLoop(DKind: OMPD_target_parallel_for, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13386 OrderedLoopCountExpr: getOrderedNumberExpr(Clauses), AStmt: CS, SemaRef, DSA&: *DSAStack,
13387 VarsWithImplicitDSA, Built&: B);
13388 if (NestedLoopCount == 0)
13389 return StmtError();
13390
13391 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
13392 return StmtError();
13393
13394 return OMPTargetParallelForDirective::Create(
13395 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
13396 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
13397}
13398
13399/// Check for existence of a map clause in the list of clauses.
13400static bool hasClauses(ArrayRef<OMPClause *> Clauses,
13401 const OpenMPClauseKind K) {
13402 return llvm::any_of(
13403 Range&: Clauses, P: [K](const OMPClause *C) { return C->getClauseKind() == K; });
13404}
13405
13406template <typename... Params>
13407static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
13408 const Params... ClauseTypes) {
13409 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
13410}
13411
13412/// Check if the variables in the mapping clause are externally visible.
13413static bool isClauseMappable(ArrayRef<OMPClause *> Clauses) {
13414 for (const OMPClause *C : Clauses) {
13415 if (auto *TC = dyn_cast<OMPToClause>(Val: C))
13416 return llvm::all_of(Range: TC->all_decls(), P: [](ValueDecl *VD) {
13417 return !VD || !VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
13418 (VD->isExternallyVisible() &&
13419 VD->getVisibility() != HiddenVisibility);
13420 });
13421 else if (auto *FC = dyn_cast<OMPFromClause>(Val: C))
13422 return llvm::all_of(Range: FC->all_decls(), P: [](ValueDecl *VD) {
13423 return !VD || !VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
13424 (VD->isExternallyVisible() &&
13425 VD->getVisibility() != HiddenVisibility);
13426 });
13427 }
13428
13429 return true;
13430}
13431
13432StmtResult
13433SemaOpenMP::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
13434 Stmt *AStmt, SourceLocation StartLoc,
13435 SourceLocation EndLoc) {
13436 if (!AStmt)
13437 return StmtError();
13438
13439 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
13440
13441 // OpenMP [2.12.2, target data Construct, Restrictions]
13442 // At least one map, use_device_addr or use_device_ptr clause must appear on
13443 // the directive.
13444 if (!hasClauses(Clauses, K: OMPC_map, ClauseTypes: OMPC_use_device_ptr) &&
13445 (getLangOpts().OpenMP < 50 ||
13446 !hasClauses(Clauses, K: OMPC_use_device_addr))) {
13447 StringRef Expected;
13448 if (getLangOpts().OpenMP < 50)
13449 Expected = "'map' or 'use_device_ptr'";
13450 else
13451 Expected = "'map', 'use_device_ptr', or 'use_device_addr'";
13452 unsigned OMPVersion = getLangOpts().OpenMP;
13453 Diag(Loc: StartLoc, DiagID: diag::err_omp_no_clause_for_directive)
13454 << Expected << getOpenMPDirectiveName(D: OMPD_target_data, Ver: OMPVersion);
13455 return StmtError();
13456 }
13457
13458 SemaRef.setFunctionHasBranchProtectedScope();
13459
13460 return OMPTargetDataDirective::Create(C: getASTContext(), StartLoc, EndLoc,
13461 Clauses, AssociatedStmt: AStmt);
13462}
13463
13464StmtResult SemaOpenMP::ActOnOpenMPTargetEnterDataDirective(
13465 ArrayRef<OMPClause *> Clauses, SourceLocation StartLoc,
13466 SourceLocation EndLoc, Stmt *AStmt) {
13467 if (!AStmt)
13468 return StmtError();
13469
13470 setBranchProtectedScope(SemaRef, DKind: OMPD_target_enter_data, AStmt);
13471
13472 // OpenMP [2.10.2, Restrictions, p. 99]
13473 // At least one map clause must appear on the directive.
13474 if (!hasClauses(Clauses, K: OMPC_map)) {
13475 unsigned OMPVersion = getLangOpts().OpenMP;
13476 Diag(Loc: StartLoc, DiagID: diag::err_omp_no_clause_for_directive)
13477 << "'map'"
13478 << getOpenMPDirectiveName(D: OMPD_target_enter_data, Ver: OMPVersion);
13479 return StmtError();
13480 }
13481
13482 return OMPTargetEnterDataDirective::Create(C: getASTContext(), StartLoc, EndLoc,
13483 Clauses, AssociatedStmt: AStmt);
13484}
13485
13486StmtResult SemaOpenMP::ActOnOpenMPTargetExitDataDirective(
13487 ArrayRef<OMPClause *> Clauses, SourceLocation StartLoc,
13488 SourceLocation EndLoc, Stmt *AStmt) {
13489 if (!AStmt)
13490 return StmtError();
13491
13492 setBranchProtectedScope(SemaRef, DKind: OMPD_target_exit_data, AStmt);
13493
13494 // OpenMP [2.10.3, Restrictions, p. 102]
13495 // At least one map clause must appear on the directive.
13496 if (!hasClauses(Clauses, K: OMPC_map)) {
13497 unsigned OMPVersion = getLangOpts().OpenMP;
13498 Diag(Loc: StartLoc, DiagID: diag::err_omp_no_clause_for_directive)
13499 << "'map'" << getOpenMPDirectiveName(D: OMPD_target_exit_data, Ver: OMPVersion);
13500 return StmtError();
13501 }
13502
13503 return OMPTargetExitDataDirective::Create(C: getASTContext(), StartLoc, EndLoc,
13504 Clauses, AssociatedStmt: AStmt);
13505}
13506
13507StmtResult SemaOpenMP::ActOnOpenMPTargetUpdateDirective(
13508 ArrayRef<OMPClause *> Clauses, SourceLocation StartLoc,
13509 SourceLocation EndLoc, Stmt *AStmt) {
13510 if (!AStmt)
13511 return StmtError();
13512
13513 setBranchProtectedScope(SemaRef, DKind: OMPD_target_update, AStmt);
13514
13515 if (!hasClauses(Clauses, K: OMPC_to, ClauseTypes: OMPC_from)) {
13516 Diag(Loc: StartLoc, DiagID: diag::err_omp_at_least_one_motion_clause_required);
13517 return StmtError();
13518 }
13519
13520 if (!isClauseMappable(Clauses)) {
13521 Diag(Loc: StartLoc, DiagID: diag::err_omp_cannot_update_with_internal_linkage);
13522 return StmtError();
13523 }
13524
13525 return OMPTargetUpdateDirective::Create(C: getASTContext(), StartLoc, EndLoc,
13526 Clauses, AssociatedStmt: AStmt);
13527}
13528
13529/// This checks whether a \p ClauseType clause \p C has at most \p Max
13530/// expression. If not, a diag of number \p Diag will be emitted.
13531template <typename ClauseType>
13532static bool checkNumExprsInClause(SemaBase &SemaRef,
13533 ArrayRef<OMPClause *> Clauses,
13534 unsigned MaxNum, unsigned Diag) {
13535 auto ClauseItr = llvm::find_if(Clauses, llvm::IsaPred<ClauseType>);
13536 if (ClauseItr == Clauses.end())
13537 return true;
13538 const auto *C = cast<ClauseType>(*ClauseItr);
13539 auto VarList = C->getVarRefs();
13540 if (VarList.size() > MaxNum) {
13541 SemaRef.Diag(VarList[MaxNum]->getBeginLoc(), Diag)
13542 << getOpenMPClauseNameForDiag(C->getClauseKind());
13543 return false;
13544 }
13545 return true;
13546}
13547
13548StmtResult SemaOpenMP::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
13549 Stmt *AStmt,
13550 SourceLocation StartLoc,
13551 SourceLocation EndLoc) {
13552 if (!AStmt)
13553 return StmtError();
13554
13555 if (!checkNumExprsInClause<OMPNumTeamsClause>(
13556 SemaRef&: *this, Clauses, /*MaxNum=*/2,
13557 Diag: diag::err_omp_num_teams_multi_expr_not_allowed) ||
13558 !checkNumExprsInClause<OMPThreadLimitClause>(
13559 SemaRef&: *this, Clauses, /*MaxNum=*/1, Diag: diag::err_omp_multi_expr_not_allowed))
13560 return StmtError();
13561
13562 // Report affected OpenMP target offloading behavior when in HIP lang-mode.
13563 if (getLangOpts().HIP && (DSAStack->getParentDirective() == OMPD_target))
13564 Diag(Loc: StartLoc, DiagID: diag::warn_hip_omp_target_directives);
13565
13566 setBranchProtectedScope(SemaRef, DKind: OMPD_teams, AStmt);
13567
13568 DSAStack->setParentTeamsRegionLoc(StartLoc);
13569
13570 return OMPTeamsDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
13571 AssociatedStmt: AStmt);
13572}
13573
13574StmtResult SemaOpenMP::ActOnOpenMPCancellationPointDirective(
13575 SourceLocation StartLoc, SourceLocation EndLoc,
13576 OpenMPDirectiveKind CancelRegion) {
13577 if (DSAStack->isParentNowaitRegion()) {
13578 Diag(Loc: StartLoc, DiagID: diag::err_omp_parent_cancel_region_nowait) << 0;
13579 return StmtError();
13580 }
13581 if (DSAStack->isParentOrderedRegion()) {
13582 Diag(Loc: StartLoc, DiagID: diag::err_omp_parent_cancel_region_ordered) << 0;
13583 return StmtError();
13584 }
13585 return OMPCancellationPointDirective::Create(C: getASTContext(), StartLoc,
13586 EndLoc, CancelRegion);
13587}
13588
13589StmtResult SemaOpenMP::ActOnOpenMPCancelDirective(
13590 ArrayRef<OMPClause *> Clauses, SourceLocation StartLoc,
13591 SourceLocation EndLoc, OpenMPDirectiveKind CancelRegion) {
13592 if (DSAStack->isParentNowaitRegion()) {
13593 Diag(Loc: StartLoc, DiagID: diag::err_omp_parent_cancel_region_nowait) << 1;
13594 return StmtError();
13595 }
13596 if (DSAStack->isParentOrderedRegion()) {
13597 Diag(Loc: StartLoc, DiagID: diag::err_omp_parent_cancel_region_ordered) << 1;
13598 return StmtError();
13599 }
13600 DSAStack->setParentCancelRegion(/*Cancel=*/true);
13601 return OMPCancelDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
13602 CancelRegion);
13603}
13604
13605static bool checkReductionClauseWithNogroup(Sema &S,
13606 ArrayRef<OMPClause *> Clauses) {
13607 const OMPClause *ReductionClause = nullptr;
13608 const OMPClause *NogroupClause = nullptr;
13609 for (const OMPClause *C : Clauses) {
13610 if (C->getClauseKind() == OMPC_reduction) {
13611 ReductionClause = C;
13612 if (NogroupClause)
13613 break;
13614 continue;
13615 }
13616 if (C->getClauseKind() == OMPC_nogroup) {
13617 NogroupClause = C;
13618 if (ReductionClause)
13619 break;
13620 continue;
13621 }
13622 }
13623 if (ReductionClause && NogroupClause) {
13624 S.Diag(Loc: ReductionClause->getBeginLoc(), DiagID: diag::err_omp_reduction_with_nogroup)
13625 << SourceRange(NogroupClause->getBeginLoc(),
13626 NogroupClause->getEndLoc());
13627 return true;
13628 }
13629 return false;
13630}
13631
13632StmtResult SemaOpenMP::ActOnOpenMPTaskLoopDirective(
13633 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13634 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13635 if (!AStmt)
13636 return StmtError();
13637
13638 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
13639 OMPLoopBasedDirective::HelperExprs B;
13640 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13641 // define the nested loops number.
13642 unsigned NestedLoopCount =
13643 checkOpenMPLoop(DKind: OMPD_taskloop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13644 /*OrderedLoopCountExpr=*/nullptr, AStmt, SemaRef,
13645 DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
13646 if (NestedLoopCount == 0)
13647 return StmtError();
13648
13649 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
13650 "omp for loop exprs were not built");
13651
13652 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13653 // The grainsize clause and num_tasks clause are mutually exclusive and may
13654 // not appear on the same taskloop directive.
13655 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13656 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13657 return StmtError();
13658 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13659 // If a reduction clause is present on the taskloop directive, the nogroup
13660 // clause must not be specified.
13661 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13662 return StmtError();
13663
13664 SemaRef.setFunctionHasBranchProtectedScope();
13665 return OMPTaskLoopDirective::Create(C: getASTContext(), StartLoc, EndLoc,
13666 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
13667 DSAStack->isCancelRegion());
13668}
13669
13670StmtResult SemaOpenMP::ActOnOpenMPTaskLoopSimdDirective(
13671 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13672 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13673 if (!AStmt)
13674 return StmtError();
13675
13676 CapturedStmt *CS =
13677 setBranchProtectedScope(SemaRef, DKind: OMPD_taskloop_simd, AStmt);
13678
13679 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
13680 OMPLoopBasedDirective::HelperExprs B;
13681 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13682 // define the nested loops number.
13683 unsigned NestedLoopCount =
13684 checkOpenMPLoop(DKind: OMPD_taskloop_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13685 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
13686 VarsWithImplicitDSA, Built&: B);
13687 if (NestedLoopCount == 0)
13688 return StmtError();
13689
13690 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
13691 return StmtError();
13692
13693 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13694 // The grainsize clause and num_tasks clause are mutually exclusive and may
13695 // not appear on the same taskloop directive.
13696 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13697 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13698 return StmtError();
13699 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13700 // If a reduction clause is present on the taskloop directive, the nogroup
13701 // clause must not be specified.
13702 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13703 return StmtError();
13704 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
13705 return StmtError();
13706
13707 return OMPTaskLoopSimdDirective::Create(C: getASTContext(), StartLoc, EndLoc,
13708 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
13709}
13710
13711StmtResult SemaOpenMP::ActOnOpenMPMasterTaskLoopDirective(
13712 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13713 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13714 if (!AStmt)
13715 return StmtError();
13716
13717 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
13718 OMPLoopBasedDirective::HelperExprs B;
13719 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13720 // define the nested loops number.
13721 unsigned NestedLoopCount =
13722 checkOpenMPLoop(DKind: OMPD_master_taskloop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13723 /*OrderedLoopCountExpr=*/nullptr, AStmt, SemaRef,
13724 DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
13725 if (NestedLoopCount == 0)
13726 return StmtError();
13727
13728 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
13729 "omp for loop exprs were not built");
13730
13731 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13732 // The grainsize clause and num_tasks clause are mutually exclusive and may
13733 // not appear on the same taskloop directive.
13734 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13735 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13736 return StmtError();
13737 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13738 // If a reduction clause is present on the taskloop directive, the nogroup
13739 // clause must not be specified.
13740 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13741 return StmtError();
13742
13743 SemaRef.setFunctionHasBranchProtectedScope();
13744 return OMPMasterTaskLoopDirective::Create(C: getASTContext(), StartLoc, EndLoc,
13745 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
13746 DSAStack->isCancelRegion());
13747}
13748
13749StmtResult SemaOpenMP::ActOnOpenMPMaskedTaskLoopDirective(
13750 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13751 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13752 if (!AStmt)
13753 return StmtError();
13754
13755 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
13756 OMPLoopBasedDirective::HelperExprs B;
13757 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13758 // define the nested loops number.
13759 unsigned NestedLoopCount =
13760 checkOpenMPLoop(DKind: OMPD_masked_taskloop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13761 /*OrderedLoopCountExpr=*/nullptr, AStmt, SemaRef,
13762 DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
13763 if (NestedLoopCount == 0)
13764 return StmtError();
13765
13766 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
13767 "omp for loop exprs were not built");
13768
13769 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13770 // The grainsize clause and num_tasks clause are mutually exclusive and may
13771 // not appear on the same taskloop directive.
13772 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13773 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13774 return StmtError();
13775 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13776 // If a reduction clause is present on the taskloop directive, the nogroup
13777 // clause must not be specified.
13778 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13779 return StmtError();
13780
13781 SemaRef.setFunctionHasBranchProtectedScope();
13782 return OMPMaskedTaskLoopDirective::Create(C: getASTContext(), StartLoc, EndLoc,
13783 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
13784 DSAStack->isCancelRegion());
13785}
13786
13787StmtResult SemaOpenMP::ActOnOpenMPMasterTaskLoopSimdDirective(
13788 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13789 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13790 if (!AStmt)
13791 return StmtError();
13792
13793 CapturedStmt *CS =
13794 setBranchProtectedScope(SemaRef, DKind: OMPD_master_taskloop_simd, AStmt);
13795
13796 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
13797 OMPLoopBasedDirective::HelperExprs B;
13798 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13799 // define the nested loops number.
13800 unsigned NestedLoopCount =
13801 checkOpenMPLoop(DKind: OMPD_master_taskloop_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13802 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
13803 VarsWithImplicitDSA, Built&: B);
13804 if (NestedLoopCount == 0)
13805 return StmtError();
13806
13807 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
13808 return StmtError();
13809
13810 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13811 // The grainsize clause and num_tasks clause are mutually exclusive and may
13812 // not appear on the same taskloop directive.
13813 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13814 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13815 return StmtError();
13816 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13817 // If a reduction clause is present on the taskloop directive, the nogroup
13818 // clause must not be specified.
13819 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13820 return StmtError();
13821 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
13822 return StmtError();
13823
13824 return OMPMasterTaskLoopSimdDirective::Create(
13825 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
13826}
13827
13828StmtResult SemaOpenMP::ActOnOpenMPMaskedTaskLoopSimdDirective(
13829 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13830 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13831 if (!AStmt)
13832 return StmtError();
13833
13834 CapturedStmt *CS =
13835 setBranchProtectedScope(SemaRef, DKind: OMPD_masked_taskloop_simd, AStmt);
13836
13837 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
13838 OMPLoopBasedDirective::HelperExprs B;
13839 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13840 // define the nested loops number.
13841 unsigned NestedLoopCount =
13842 checkOpenMPLoop(DKind: OMPD_masked_taskloop_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13843 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
13844 VarsWithImplicitDSA, Built&: B);
13845 if (NestedLoopCount == 0)
13846 return StmtError();
13847
13848 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
13849 return StmtError();
13850
13851 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13852 // The grainsize clause and num_tasks clause are mutually exclusive and may
13853 // not appear on the same taskloop directive.
13854 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13855 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13856 return StmtError();
13857 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13858 // If a reduction clause is present on the taskloop directive, the nogroup
13859 // clause must not be specified.
13860 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13861 return StmtError();
13862 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
13863 return StmtError();
13864
13865 return OMPMaskedTaskLoopSimdDirective::Create(
13866 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
13867}
13868
13869StmtResult SemaOpenMP::ActOnOpenMPParallelMasterTaskLoopDirective(
13870 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13871 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13872 if (!AStmt)
13873 return StmtError();
13874
13875 CapturedStmt *CS =
13876 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_master_taskloop, AStmt);
13877
13878 OMPLoopBasedDirective::HelperExprs B;
13879 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13880 // define the nested loops number.
13881 unsigned NestedLoopCount = checkOpenMPLoop(
13882 DKind: OMPD_parallel_master_taskloop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13883 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
13884 VarsWithImplicitDSA, Built&: B);
13885 if (NestedLoopCount == 0)
13886 return StmtError();
13887
13888 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
13889 "omp for loop exprs were not built");
13890
13891 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13892 // The grainsize clause and num_tasks clause are mutually exclusive and may
13893 // not appear on the same taskloop directive.
13894 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13895 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13896 return StmtError();
13897 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13898 // If a reduction clause is present on the taskloop directive, the nogroup
13899 // clause must not be specified.
13900 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13901 return StmtError();
13902
13903 return OMPParallelMasterTaskLoopDirective::Create(
13904 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
13905 DSAStack->isCancelRegion());
13906}
13907
13908StmtResult SemaOpenMP::ActOnOpenMPParallelMaskedTaskLoopDirective(
13909 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13910 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13911 if (!AStmt)
13912 return StmtError();
13913
13914 CapturedStmt *CS =
13915 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_masked_taskloop, AStmt);
13916
13917 OMPLoopBasedDirective::HelperExprs B;
13918 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13919 // define the nested loops number.
13920 unsigned NestedLoopCount = checkOpenMPLoop(
13921 DKind: OMPD_parallel_masked_taskloop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13922 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
13923 VarsWithImplicitDSA, Built&: B);
13924 if (NestedLoopCount == 0)
13925 return StmtError();
13926
13927 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
13928 "omp for loop exprs were not built");
13929
13930 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13931 // The grainsize clause and num_tasks clause are mutually exclusive and may
13932 // not appear on the same taskloop directive.
13933 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13934 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13935 return StmtError();
13936 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13937 // If a reduction clause is present on the taskloop directive, the nogroup
13938 // clause must not be specified.
13939 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13940 return StmtError();
13941
13942 return OMPParallelMaskedTaskLoopDirective::Create(
13943 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
13944 DSAStack->isCancelRegion());
13945}
13946
13947StmtResult SemaOpenMP::ActOnOpenMPParallelMasterTaskLoopSimdDirective(
13948 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13949 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13950 if (!AStmt)
13951 return StmtError();
13952
13953 CapturedStmt *CS = setBranchProtectedScope(
13954 SemaRef, DKind: OMPD_parallel_master_taskloop_simd, AStmt);
13955
13956 OMPLoopBasedDirective::HelperExprs B;
13957 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13958 // define the nested loops number.
13959 unsigned NestedLoopCount = checkOpenMPLoop(
13960 DKind: OMPD_parallel_master_taskloop_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13961 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
13962 VarsWithImplicitDSA, Built&: B);
13963 if (NestedLoopCount == 0)
13964 return StmtError();
13965
13966 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
13967 return StmtError();
13968
13969 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13970 // The grainsize clause and num_tasks clause are mutually exclusive and may
13971 // not appear on the same taskloop directive.
13972 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13973 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13974 return StmtError();
13975 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13976 // If a reduction clause is present on the taskloop directive, the nogroup
13977 // clause must not be specified.
13978 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13979 return StmtError();
13980 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
13981 return StmtError();
13982
13983 return OMPParallelMasterTaskLoopSimdDirective::Create(
13984 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
13985}
13986
13987StmtResult SemaOpenMP::ActOnOpenMPParallelMaskedTaskLoopSimdDirective(
13988 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13989 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13990 if (!AStmt)
13991 return StmtError();
13992
13993 CapturedStmt *CS = setBranchProtectedScope(
13994 SemaRef, DKind: OMPD_parallel_masked_taskloop_simd, AStmt);
13995
13996 OMPLoopBasedDirective::HelperExprs B;
13997 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13998 // define the nested loops number.
13999 unsigned NestedLoopCount = checkOpenMPLoop(
14000 DKind: OMPD_parallel_masked_taskloop_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14001 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
14002 VarsWithImplicitDSA, Built&: B);
14003 if (NestedLoopCount == 0)
14004 return StmtError();
14005
14006 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14007 return StmtError();
14008
14009 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
14010 // The grainsize clause and num_tasks clause are mutually exclusive and may
14011 // not appear on the same taskloop directive.
14012 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
14013 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
14014 return StmtError();
14015 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
14016 // If a reduction clause is present on the taskloop directive, the nogroup
14017 // clause must not be specified.
14018 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
14019 return StmtError();
14020 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14021 return StmtError();
14022
14023 return OMPParallelMaskedTaskLoopSimdDirective::Create(
14024 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14025}
14026
14027StmtResult SemaOpenMP::ActOnOpenMPDistributeDirective(
14028 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14029 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14030 if (!AStmt)
14031 return StmtError();
14032
14033 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
14034 OMPLoopBasedDirective::HelperExprs B;
14035 // In presence of clause 'collapse' with number of loops, it will
14036 // define the nested loops number.
14037 unsigned NestedLoopCount =
14038 checkOpenMPLoop(DKind: OMPD_distribute, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14039 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt,
14040 SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
14041 if (NestedLoopCount == 0)
14042 return StmtError();
14043
14044 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
14045 "omp for loop exprs were not built");
14046
14047 SemaRef.setFunctionHasBranchProtectedScope();
14048 auto *DistributeDirective = OMPDistributeDirective::Create(
14049 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14050 return DistributeDirective;
14051}
14052
14053StmtResult SemaOpenMP::ActOnOpenMPDistributeParallelForDirective(
14054 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14055 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14056 if (!AStmt)
14057 return StmtError();
14058
14059 CapturedStmt *CS =
14060 setBranchProtectedScope(SemaRef, DKind: OMPD_distribute_parallel_for, AStmt);
14061
14062 OMPLoopBasedDirective::HelperExprs B;
14063 // In presence of clause 'collapse' with number of loops, it will
14064 // define the nested loops number.
14065 unsigned NestedLoopCount = checkOpenMPLoop(
14066 DKind: OMPD_distribute_parallel_for, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14067 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
14068 VarsWithImplicitDSA, Built&: B);
14069 if (NestedLoopCount == 0)
14070 return StmtError();
14071
14072 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
14073 "omp for loop exprs were not built");
14074
14075 return OMPDistributeParallelForDirective::Create(
14076 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
14077 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
14078}
14079
14080StmtResult SemaOpenMP::ActOnOpenMPDistributeParallelForSimdDirective(
14081 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14082 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14083 if (!AStmt)
14084 return StmtError();
14085
14086 CapturedStmt *CS = setBranchProtectedScope(
14087 SemaRef, DKind: OMPD_distribute_parallel_for_simd, AStmt);
14088
14089 OMPLoopBasedDirective::HelperExprs B;
14090 // In presence of clause 'collapse' with number of loops, it will
14091 // define the nested loops number.
14092 unsigned NestedLoopCount = checkOpenMPLoop(
14093 DKind: OMPD_distribute_parallel_for_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14094 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
14095 VarsWithImplicitDSA, Built&: B);
14096 if (NestedLoopCount == 0)
14097 return StmtError();
14098
14099 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14100 return StmtError();
14101
14102 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14103 return StmtError();
14104
14105 return OMPDistributeParallelForSimdDirective::Create(
14106 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14107}
14108
14109StmtResult SemaOpenMP::ActOnOpenMPDistributeSimdDirective(
14110 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14111 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14112 if (!AStmt)
14113 return StmtError();
14114
14115 CapturedStmt *CS =
14116 setBranchProtectedScope(SemaRef, DKind: OMPD_distribute_simd, AStmt);
14117
14118 OMPLoopBasedDirective::HelperExprs B;
14119 // In presence of clause 'collapse' with number of loops, it will
14120 // define the nested loops number.
14121 unsigned NestedLoopCount =
14122 checkOpenMPLoop(DKind: OMPD_distribute_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14123 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS,
14124 SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
14125 if (NestedLoopCount == 0)
14126 return StmtError();
14127
14128 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14129 return StmtError();
14130
14131 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14132 return StmtError();
14133
14134 return OMPDistributeSimdDirective::Create(C: getASTContext(), StartLoc, EndLoc,
14135 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14136}
14137
14138StmtResult SemaOpenMP::ActOnOpenMPTargetParallelForSimdDirective(
14139 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14140 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14141 if (!AStmt)
14142 return StmtError();
14143
14144 CapturedStmt *CS =
14145 setBranchProtectedScope(SemaRef, DKind: OMPD_target_parallel_for_simd, AStmt);
14146
14147 OMPLoopBasedDirective::HelperExprs B;
14148 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
14149 // define the nested loops number.
14150 unsigned NestedLoopCount = checkOpenMPLoop(
14151 DKind: OMPD_target_parallel_for_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14152 OrderedLoopCountExpr: getOrderedNumberExpr(Clauses), AStmt: CS, SemaRef, DSA&: *DSAStack,
14153 VarsWithImplicitDSA, Built&: B);
14154 if (NestedLoopCount == 0)
14155 return StmtError();
14156
14157 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14158 return StmtError();
14159
14160 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14161 return StmtError();
14162
14163 return OMPTargetParallelForSimdDirective::Create(
14164 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14165}
14166
14167StmtResult SemaOpenMP::ActOnOpenMPTargetSimdDirective(
14168 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14169 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14170 if (!AStmt)
14171 return StmtError();
14172
14173 CapturedStmt *CS = setBranchProtectedScope(SemaRef, DKind: OMPD_target_simd, AStmt);
14174
14175 OMPLoopBasedDirective::HelperExprs B;
14176 // In presence of clause 'collapse' with number of loops, it will define the
14177 // nested loops number.
14178 unsigned NestedLoopCount =
14179 checkOpenMPLoop(DKind: OMPD_target_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14180 OrderedLoopCountExpr: getOrderedNumberExpr(Clauses), AStmt: CS, SemaRef, DSA&: *DSAStack,
14181 VarsWithImplicitDSA, Built&: B);
14182 if (NestedLoopCount == 0)
14183 return StmtError();
14184
14185 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14186 return StmtError();
14187
14188 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14189 return StmtError();
14190
14191 return OMPTargetSimdDirective::Create(C: getASTContext(), StartLoc, EndLoc,
14192 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14193}
14194
14195StmtResult SemaOpenMP::ActOnOpenMPTeamsDistributeDirective(
14196 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14197 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14198 if (!AStmt)
14199 return StmtError();
14200
14201 CapturedStmt *CS =
14202 setBranchProtectedScope(SemaRef, DKind: OMPD_teams_distribute, AStmt);
14203
14204 OMPLoopBasedDirective::HelperExprs B;
14205 // In presence of clause 'collapse' with number of loops, it will
14206 // define the nested loops number.
14207 unsigned NestedLoopCount =
14208 checkOpenMPLoop(DKind: OMPD_teams_distribute, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14209 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS,
14210 SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
14211 if (NestedLoopCount == 0)
14212 return StmtError();
14213
14214 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
14215 "omp teams distribute loop exprs were not built");
14216
14217 DSAStack->setParentTeamsRegionLoc(StartLoc);
14218
14219 return OMPTeamsDistributeDirective::Create(
14220 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14221}
14222
14223StmtResult SemaOpenMP::ActOnOpenMPTeamsDistributeSimdDirective(
14224 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14225 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14226 if (!AStmt)
14227 return StmtError();
14228
14229 CapturedStmt *CS =
14230 setBranchProtectedScope(SemaRef, DKind: OMPD_teams_distribute_simd, AStmt);
14231
14232 OMPLoopBasedDirective::HelperExprs B;
14233 // In presence of clause 'collapse' with number of loops, it will
14234 // define the nested loops number.
14235 unsigned NestedLoopCount = checkOpenMPLoop(
14236 DKind: OMPD_teams_distribute_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14237 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
14238 VarsWithImplicitDSA, Built&: B);
14239 if (NestedLoopCount == 0)
14240 return StmtError();
14241
14242 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14243 return StmtError();
14244
14245 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14246 return StmtError();
14247
14248 DSAStack->setParentTeamsRegionLoc(StartLoc);
14249
14250 return OMPTeamsDistributeSimdDirective::Create(
14251 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14252}
14253
14254StmtResult SemaOpenMP::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
14255 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14256 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14257 if (!AStmt)
14258 return StmtError();
14259
14260 CapturedStmt *CS = setBranchProtectedScope(
14261 SemaRef, DKind: OMPD_teams_distribute_parallel_for_simd, AStmt);
14262
14263 OMPLoopBasedDirective::HelperExprs B;
14264 // In presence of clause 'collapse' with number of loops, it will
14265 // define the nested loops number.
14266 unsigned NestedLoopCount = checkOpenMPLoop(
14267 DKind: OMPD_teams_distribute_parallel_for_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14268 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
14269 VarsWithImplicitDSA, Built&: B);
14270 if (NestedLoopCount == 0)
14271 return StmtError();
14272
14273 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14274 return StmtError();
14275
14276 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14277 return StmtError();
14278
14279 DSAStack->setParentTeamsRegionLoc(StartLoc);
14280
14281 return OMPTeamsDistributeParallelForSimdDirective::Create(
14282 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14283}
14284
14285StmtResult SemaOpenMP::ActOnOpenMPTeamsDistributeParallelForDirective(
14286 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14287 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14288 if (!AStmt)
14289 return StmtError();
14290
14291 CapturedStmt *CS = setBranchProtectedScope(
14292 SemaRef, DKind: OMPD_teams_distribute_parallel_for, AStmt);
14293
14294 OMPLoopBasedDirective::HelperExprs B;
14295 // In presence of clause 'collapse' with number of loops, it will
14296 // define the nested loops number.
14297 unsigned NestedLoopCount = checkOpenMPLoop(
14298 DKind: OMPD_teams_distribute_parallel_for, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14299 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
14300 VarsWithImplicitDSA, Built&: B);
14301
14302 if (NestedLoopCount == 0)
14303 return StmtError();
14304
14305 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
14306 "omp for loop exprs were not built");
14307
14308 DSAStack->setParentTeamsRegionLoc(StartLoc);
14309
14310 return OMPTeamsDistributeParallelForDirective::Create(
14311 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
14312 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
14313}
14314
14315StmtResult SemaOpenMP::ActOnOpenMPTargetTeamsDirective(
14316 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14317 SourceLocation EndLoc) {
14318 if (!AStmt)
14319 return StmtError();
14320
14321 setBranchProtectedScope(SemaRef, DKind: OMPD_target_teams, AStmt);
14322
14323 const OMPClause *BareClause = nullptr;
14324 bool HasThreadLimitAndNumTeamsClause = hasClauses(Clauses, K: OMPC_num_teams) &&
14325 hasClauses(Clauses, K: OMPC_thread_limit);
14326 bool HasBareClause = llvm::any_of(Range&: Clauses, P: [&](const OMPClause *C) {
14327 BareClause = C;
14328 return C->getClauseKind() == OMPC_ompx_bare;
14329 });
14330
14331 if (HasBareClause && !HasThreadLimitAndNumTeamsClause) {
14332 Diag(Loc: BareClause->getBeginLoc(), DiagID: diag::err_ompx_bare_no_grid);
14333 return StmtError();
14334 }
14335
14336 unsigned ClauseMaxNumExprs = HasBareClause ? 3 : 2;
14337 unsigned NumTeamsDiag = HasBareClause
14338 ? diag::err_ompx_more_than_three_expr_not_allowed
14339 : diag::err_omp_num_teams_multi_expr_not_allowed;
14340 unsigned ThreadLimitDiag =
14341 HasBareClause ? diag::err_ompx_more_than_three_expr_not_allowed
14342 : diag::err_omp_multi_expr_not_allowed;
14343
14344 if (!checkNumExprsInClause<OMPNumTeamsClause>(
14345 SemaRef&: *this, Clauses, MaxNum: ClauseMaxNumExprs, Diag: NumTeamsDiag) ||
14346 !checkNumExprsInClause<OMPThreadLimitClause>(
14347 SemaRef&: *this, Clauses, MaxNum: ClauseMaxNumExprs, Diag: ThreadLimitDiag)) {
14348 return StmtError();
14349 }
14350 return OMPTargetTeamsDirective::Create(C: getASTContext(), StartLoc, EndLoc,
14351 Clauses, AssociatedStmt: AStmt);
14352}
14353
14354StmtResult SemaOpenMP::ActOnOpenMPTargetTeamsDistributeDirective(
14355 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14356 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14357 if (!AStmt)
14358 return StmtError();
14359
14360 if (!checkNumExprsInClause<OMPNumTeamsClause>(
14361 SemaRef&: *this, Clauses, /*MaxNum=*/2,
14362 Diag: diag::err_omp_num_teams_multi_expr_not_allowed) ||
14363 !checkNumExprsInClause<OMPThreadLimitClause>(
14364 SemaRef&: *this, Clauses, /*MaxNum=*/1, Diag: diag::err_omp_multi_expr_not_allowed))
14365 return StmtError();
14366
14367 CapturedStmt *CS =
14368 setBranchProtectedScope(SemaRef, DKind: OMPD_target_teams_distribute, AStmt);
14369
14370 OMPLoopBasedDirective::HelperExprs B;
14371 // In presence of clause 'collapse' with number of loops, it will
14372 // define the nested loops number.
14373 unsigned NestedLoopCount = checkOpenMPLoop(
14374 DKind: OMPD_target_teams_distribute, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14375 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
14376 VarsWithImplicitDSA, Built&: B);
14377 if (NestedLoopCount == 0)
14378 return StmtError();
14379
14380 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
14381 "omp target teams distribute loop exprs were not built");
14382
14383 return OMPTargetTeamsDistributeDirective::Create(
14384 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14385}
14386
14387StmtResult SemaOpenMP::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
14388 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14389 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14390 if (!AStmt)
14391 return StmtError();
14392
14393 if (!checkNumExprsInClause<OMPNumTeamsClause>(
14394 SemaRef&: *this, Clauses, /*MaxNum=*/2,
14395 Diag: diag::err_omp_num_teams_multi_expr_not_allowed) ||
14396 !checkNumExprsInClause<OMPThreadLimitClause>(
14397 SemaRef&: *this, Clauses, /*MaxNum=*/1, Diag: diag::err_omp_multi_expr_not_allowed))
14398 return StmtError();
14399
14400 CapturedStmt *CS = setBranchProtectedScope(
14401 SemaRef, DKind: OMPD_target_teams_distribute_parallel_for, AStmt);
14402
14403 OMPLoopBasedDirective::HelperExprs B;
14404 // In presence of clause 'collapse' with number of loops, it will
14405 // define the nested loops number.
14406 unsigned NestedLoopCount = checkOpenMPLoop(
14407 DKind: OMPD_target_teams_distribute_parallel_for, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14408 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
14409 VarsWithImplicitDSA, Built&: B);
14410 if (NestedLoopCount == 0)
14411 return StmtError();
14412
14413 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14414 return StmtError();
14415
14416 return OMPTargetTeamsDistributeParallelForDirective::Create(
14417 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
14418 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
14419}
14420
14421StmtResult SemaOpenMP::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
14422 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14423 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14424 if (!AStmt)
14425 return StmtError();
14426
14427 if (!checkNumExprsInClause<OMPNumTeamsClause>(
14428 SemaRef&: *this, Clauses, /*MaxNum=*/2,
14429 Diag: diag::err_omp_num_teams_multi_expr_not_allowed) ||
14430 !checkNumExprsInClause<OMPThreadLimitClause>(
14431 SemaRef&: *this, Clauses, /*MaxNum=*/1, Diag: diag::err_omp_multi_expr_not_allowed))
14432 return StmtError();
14433
14434 CapturedStmt *CS = setBranchProtectedScope(
14435 SemaRef, DKind: OMPD_target_teams_distribute_parallel_for_simd, AStmt);
14436
14437 OMPLoopBasedDirective::HelperExprs B;
14438 // In presence of clause 'collapse' with number of loops, it will
14439 // define the nested loops number.
14440 unsigned NestedLoopCount =
14441 checkOpenMPLoop(DKind: OMPD_target_teams_distribute_parallel_for_simd,
14442 CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14443 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS,
14444 SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
14445 if (NestedLoopCount == 0)
14446 return StmtError();
14447
14448 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14449 return StmtError();
14450
14451 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14452 return StmtError();
14453
14454 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
14455 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14456}
14457
14458StmtResult SemaOpenMP::ActOnOpenMPTargetTeamsDistributeSimdDirective(
14459 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14460 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14461 if (!AStmt)
14462 return StmtError();
14463
14464 if (!checkNumExprsInClause<OMPNumTeamsClause>(
14465 SemaRef&: *this, Clauses, /*MaxNum=*/2,
14466 Diag: diag::err_omp_num_teams_multi_expr_not_allowed) ||
14467 !checkNumExprsInClause<OMPThreadLimitClause>(
14468 SemaRef&: *this, Clauses, /*MaxNum=*/1, Diag: diag::err_omp_multi_expr_not_allowed))
14469 return StmtError();
14470
14471 CapturedStmt *CS = setBranchProtectedScope(
14472 SemaRef, DKind: OMPD_target_teams_distribute_simd, AStmt);
14473
14474 OMPLoopBasedDirective::HelperExprs B;
14475 // In presence of clause 'collapse' with number of loops, it will
14476 // define the nested loops number.
14477 unsigned NestedLoopCount = checkOpenMPLoop(
14478 DKind: OMPD_target_teams_distribute_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14479 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
14480 VarsWithImplicitDSA, Built&: B);
14481 if (NestedLoopCount == 0)
14482 return StmtError();
14483
14484 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14485 return StmtError();
14486
14487 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14488 return StmtError();
14489
14490 return OMPTargetTeamsDistributeSimdDirective::Create(
14491 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14492}
14493
14494/// Updates OriginalInits by checking Transform against loop transformation
14495/// directives and appending their pre-inits if a match is found.
14496static void updatePreInits(OMPLoopTransformationDirective *Transform,
14497 SmallVectorImpl<Stmt *> &PreInits) {
14498 Stmt *Dir = Transform->getDirective();
14499 switch (Dir->getStmtClass()) {
14500#define STMT(CLASS, PARENT)
14501#define ABSTRACT_STMT(CLASS)
14502#define COMMON_OMP_LOOP_TRANSFORMATION(CLASS, PARENT) \
14503 case Stmt::CLASS##Class: \
14504 appendFlattenedStmtList(PreInits, \
14505 static_cast<const CLASS *>(Dir)->getPreInits()); \
14506 break;
14507#define OMPCANONICALLOOPNESTTRANSFORMATIONDIRECTIVE(CLASS, PARENT) \
14508 COMMON_OMP_LOOP_TRANSFORMATION(CLASS, PARENT)
14509#define OMPCANONICALLOOPSEQUENCETRANSFORMATIONDIRECTIVE(CLASS, PARENT) \
14510 COMMON_OMP_LOOP_TRANSFORMATION(CLASS, PARENT)
14511#include "clang/AST/StmtNodes.inc"
14512#undef COMMON_OMP_LOOP_TRANSFORMATION
14513 default:
14514 llvm_unreachable("Not a loop transformation");
14515 }
14516}
14517
14518bool SemaOpenMP::checkTransformableLoopNest(
14519 OpenMPDirectiveKind Kind, Stmt *AStmt, int NumLoops,
14520 SmallVectorImpl<OMPLoopBasedDirective::HelperExprs> &LoopHelpers,
14521 Stmt *&Body, SmallVectorImpl<SmallVector<Stmt *>> &OriginalInits) {
14522 OriginalInits.emplace_back();
14523 bool Result = OMPLoopBasedDirective::doForAllLoops(
14524 CurStmt: AStmt->IgnoreContainers(), /*TryImperfectlyNestedLoops=*/false, NumLoops,
14525 Callback: [this, &LoopHelpers, &Body, &OriginalInits, Kind](unsigned Cnt,
14526 Stmt *CurStmt) {
14527 VarsWithInheritedDSAType TmpDSA;
14528 unsigned SingleNumLoops =
14529 checkOpenMPLoop(DKind: Kind, CollapseLoopCountExpr: nullptr, OrderedLoopCountExpr: nullptr, AStmt: CurStmt, SemaRef, DSA&: *DSAStack,
14530 VarsWithImplicitDSA&: TmpDSA, Built&: LoopHelpers[Cnt]);
14531 if (SingleNumLoops == 0)
14532 return true;
14533 assert(SingleNumLoops == 1 && "Expect single loop iteration space");
14534 if (auto *For = dyn_cast<ForStmt>(Val: CurStmt)) {
14535 OriginalInits.back().push_back(Elt: For->getInit());
14536 Body = For->getBody();
14537 } else {
14538 assert(isa<CXXForRangeStmt>(CurStmt) &&
14539 "Expected canonical for or range-based for loops.");
14540 auto *CXXFor = cast<CXXForRangeStmt>(Val: CurStmt);
14541 OriginalInits.back().push_back(Elt: CXXFor->getBeginStmt());
14542 Body = CXXFor->getBody();
14543 }
14544 OriginalInits.emplace_back();
14545 return false;
14546 },
14547 OnTransformationCallback: [&OriginalInits](OMPLoopTransformationDirective *Transform) {
14548 updatePreInits(Transform, PreInits&: OriginalInits.back());
14549 });
14550 assert(OriginalInits.back().empty() && "No preinit after innermost loop");
14551 OriginalInits.pop_back();
14552 return Result;
14553}
14554
14555/// Counts the total number of OpenMP canonical nested loops, including the
14556/// outermost loop (the original loop). PRECONDITION of this visitor is that it
14557/// must be invoked from the original loop to be analyzed. The traversal stops
14558/// for Decl's and Expr's given that they may contain inner loops that must not
14559/// be counted.
14560///
14561/// Example AST structure for the code:
14562///
14563/// int main() {
14564/// #pragma omp fuse
14565/// {
14566/// for (int i = 0; i < 100; i++) { <-- Outer loop
14567/// []() {
14568/// for(int j = 0; j < 100; j++) {} <-- NOT A LOOP (1)
14569/// };
14570/// for(int j = 0; j < 5; ++j) {} <-- Inner loop
14571/// }
14572/// for (int r = 0; i < 100; i++) { <-- Outer loop
14573/// struct LocalClass {
14574/// void bar() {
14575/// for(int j = 0; j < 100; j++) {} <-- NOT A LOOP (2)
14576/// }
14577/// };
14578/// for(int k = 0; k < 10; ++k) {} <-- Inner loop
14579/// {x = 5; for(k = 0; k < 10; ++k) x += k; x}; <-- NOT A LOOP (3)
14580/// }
14581/// }
14582/// }
14583/// (1) because in a different function (here: a lambda)
14584/// (2) because in a different function (here: class method)
14585/// (3) because considered to be intervening-code of non-perfectly nested loop
14586/// Result: Loop 'i' contains 2 loops, Loop 'r' also contains 2 loops.
14587class NestedLoopCounterVisitor final : public DynamicRecursiveASTVisitor {
14588private:
14589 unsigned NestedLoopCount = 0;
14590
14591public:
14592 explicit NestedLoopCounterVisitor() = default;
14593
14594 unsigned getNestedLoopCount() const { return NestedLoopCount; }
14595
14596 bool VisitForStmt(ForStmt *FS) override {
14597 ++NestedLoopCount;
14598 return true;
14599 }
14600
14601 bool VisitCXXForRangeStmt(CXXForRangeStmt *FRS) override {
14602 ++NestedLoopCount;
14603 return true;
14604 }
14605
14606 bool TraverseStmt(Stmt *S) override {
14607 if (!S)
14608 return true;
14609
14610 // Skip traversal of all expressions, including special cases like
14611 // LambdaExpr, StmtExpr, BlockExpr, and RequiresExpr. These expressions
14612 // may contain inner statements (and even loops), but they are not part
14613 // of the syntactic body of the surrounding loop structure.
14614 // Therefore must not be counted.
14615 if (isa<Expr>(Val: S))
14616 return true;
14617
14618 // Only recurse into CompoundStmt (block {}) and loop bodies.
14619 if (isa<CompoundStmt, ForStmt, CXXForRangeStmt>(Val: S)) {
14620 return DynamicRecursiveASTVisitor::TraverseStmt(S);
14621 }
14622
14623 // Stop traversal of the rest of statements, that break perfect
14624 // loop nesting, such as control flow (IfStmt, SwitchStmt...).
14625 return true;
14626 }
14627
14628 bool TraverseDecl(Decl *D) override {
14629 // Stop in the case of finding a declaration, it is not important
14630 // in order to find nested loops (Possible CXXRecordDecl, RecordDecl,
14631 // FunctionDecl...).
14632 return true;
14633 }
14634};
14635
14636bool SemaOpenMP::analyzeLoopSequence(Stmt *LoopSeqStmt,
14637 LoopSequenceAnalysis &SeqAnalysis,
14638 ASTContext &Context,
14639 OpenMPDirectiveKind Kind) {
14640 VarsWithInheritedDSAType TmpDSA;
14641 // Helper Lambda to handle storing initialization and body statements for
14642 // both ForStmt and CXXForRangeStmt.
14643 auto StoreLoopStatements = [](LoopAnalysis &Analysis, Stmt *LoopStmt) {
14644 if (auto *For = dyn_cast<ForStmt>(Val: LoopStmt)) {
14645 Analysis.OriginalInits.push_back(Elt: For->getInit());
14646 Analysis.TheForStmt = For;
14647 } else {
14648 auto *CXXFor = cast<CXXForRangeStmt>(Val: LoopStmt);
14649 Analysis.OriginalInits.push_back(Elt: CXXFor->getBeginStmt());
14650 Analysis.TheForStmt = CXXFor;
14651 }
14652 };
14653
14654 // Helper lambda functions to encapsulate the processing of different
14655 // derivations of the canonical loop sequence grammar
14656 // Modularized code for handling loop generation and transformations.
14657 auto AnalyzeLoopGeneration = [&](Stmt *Child) {
14658 auto *LoopTransform = cast<OMPLoopTransformationDirective>(Val: Child);
14659 Stmt *TransformedStmt = LoopTransform->getTransformedStmt();
14660 unsigned NumGeneratedTopLevelLoops =
14661 LoopTransform->getNumGeneratedTopLevelLoops();
14662 // Handle the case where transformed statement is not available due to
14663 // dependent contexts
14664 if (!TransformedStmt) {
14665 if (NumGeneratedTopLevelLoops > 0) {
14666 SeqAnalysis.LoopSeqSize += NumGeneratedTopLevelLoops;
14667 return true;
14668 }
14669 // Unroll full (0 loops produced)
14670 Diag(Loc: Child->getBeginLoc(), DiagID: diag::err_omp_not_for)
14671 << 0 << getOpenMPDirectiveName(D: Kind);
14672 return false;
14673 }
14674 // Handle loop transformations with multiple loop nests
14675 // Unroll full
14676 if (!NumGeneratedTopLevelLoops) {
14677 Diag(Loc: Child->getBeginLoc(), DiagID: diag::err_omp_not_for)
14678 << 0 << getOpenMPDirectiveName(D: Kind);
14679 return false;
14680 }
14681 // Loop transformatons such as split or loopranged fuse
14682 if (NumGeneratedTopLevelLoops > 1) {
14683 // Get the preinits related to this loop sequence generating
14684 // loop transformation (i.e loopranged fuse, split...)
14685 // These preinits differ slightly from regular inits/pre-inits related
14686 // to single loop generating loop transformations (interchange, unroll)
14687 // given that they are not bounded to a particular loop nest
14688 // so they need to be treated independently
14689 updatePreInits(Transform: LoopTransform, PreInits&: SeqAnalysis.LoopSequencePreInits);
14690 return analyzeLoopSequence(LoopSeqStmt: TransformedStmt, SeqAnalysis, Context, Kind);
14691 }
14692 // Vast majority: (Tile, Unroll, Stripe, Reverse, Interchange, Fuse all)
14693 // Process the transformed loop statement
14694 LoopAnalysis &NewTransformedSingleLoop =
14695 SeqAnalysis.Loops.emplace_back(Args&: Child);
14696 unsigned IsCanonical = checkOpenMPLoop(
14697 DKind: Kind, CollapseLoopCountExpr: nullptr, OrderedLoopCountExpr: nullptr, AStmt: TransformedStmt, SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA&: TmpDSA,
14698 Built&: NewTransformedSingleLoop.HelperExprs);
14699
14700 if (!IsCanonical)
14701 return false;
14702
14703 StoreLoopStatements(NewTransformedSingleLoop, TransformedStmt);
14704 updatePreInits(Transform: LoopTransform, PreInits&: NewTransformedSingleLoop.TransformsPreInits);
14705
14706 SeqAnalysis.LoopSeqSize++;
14707 return true;
14708 };
14709
14710 // Modularized code for handling regular canonical loops.
14711 auto AnalyzeRegularLoop = [&](Stmt *Child) {
14712 LoopAnalysis &NewRegularLoop = SeqAnalysis.Loops.emplace_back(Args&: Child);
14713 unsigned IsCanonical =
14714 checkOpenMPLoop(DKind: Kind, CollapseLoopCountExpr: nullptr, OrderedLoopCountExpr: nullptr, AStmt: Child, SemaRef, DSA&: *DSAStack,
14715 VarsWithImplicitDSA&: TmpDSA, Built&: NewRegularLoop.HelperExprs);
14716
14717 if (!IsCanonical)
14718 return false;
14719
14720 StoreLoopStatements(NewRegularLoop, Child);
14721 NestedLoopCounterVisitor NLCV;
14722 NLCV.TraverseStmt(S: Child);
14723 return true;
14724 };
14725
14726 // High level grammar validation.
14727 for (Stmt *Child : LoopSeqStmt->children()) {
14728 if (!Child)
14729 continue;
14730 // Skip over non-loop-sequence statements.
14731 if (!LoopSequenceAnalysis::isLoopSequenceDerivation(S: Child)) {
14732 Child = Child->IgnoreContainers();
14733 // Ignore empty compound statement.
14734 if (!Child)
14735 continue;
14736 // In the case of a nested loop sequence ignoring containers would not
14737 // be enough, a recurisve transversal of the loop sequence is required.
14738 if (isa<CompoundStmt>(Val: Child)) {
14739 if (!analyzeLoopSequence(LoopSeqStmt: Child, SeqAnalysis, Context, Kind))
14740 return false;
14741 // Already been treated, skip this children
14742 continue;
14743 }
14744 }
14745 // Regular loop sequence handling.
14746 if (LoopSequenceAnalysis::isLoopSequenceDerivation(S: Child)) {
14747 if (LoopAnalysis::isLoopTransformation(S: Child)) {
14748 if (!AnalyzeLoopGeneration(Child))
14749 return false;
14750 // AnalyzeLoopGeneration updates SeqAnalysis.LoopSeqSize accordingly.
14751 } else {
14752 if (!AnalyzeRegularLoop(Child))
14753 return false;
14754 SeqAnalysis.LoopSeqSize++;
14755 }
14756 } else {
14757 // Report error for invalid statement inside canonical loop sequence.
14758 Diag(Loc: Child->getBeginLoc(), DiagID: diag::err_omp_not_for)
14759 << 0 << getOpenMPDirectiveName(D: Kind);
14760 return false;
14761 }
14762 }
14763 return true;
14764}
14765
14766bool SemaOpenMP::checkTransformableLoopSequence(
14767 OpenMPDirectiveKind Kind, Stmt *AStmt, LoopSequenceAnalysis &SeqAnalysis,
14768 ASTContext &Context) {
14769 // Following OpenMP 6.0 API Specification, a Canonical Loop Sequence follows
14770 // the grammar:
14771 //
14772 // canonical-loop-sequence:
14773 // {
14774 // loop-sequence+
14775 // }
14776 // where loop-sequence can be any of the following:
14777 // 1. canonical-loop-sequence
14778 // 2. loop-nest
14779 // 3. loop-sequence-generating-construct (i.e OMPLoopTransformationDirective)
14780 //
14781 // To recognise and traverse this structure the helper function
14782 // analyzeLoopSequence serves as the recurisve entry point
14783 // and tries to match the input AST to the canonical loop sequence grammar
14784 // structure. This function will perform both a semantic and syntactical
14785 // analysis of the given statement according to OpenMP 6.0 definition of
14786 // the aforementioned canonical loop sequence.
14787
14788 // We expect an outer compound statement.
14789 if (!isa<CompoundStmt>(Val: AStmt)) {
14790 Diag(Loc: AStmt->getBeginLoc(), DiagID: diag::err_omp_not_a_loop_sequence)
14791 << getOpenMPDirectiveName(D: Kind);
14792 return false;
14793 }
14794
14795 // Recursive entry point to process the main loop sequence
14796 if (!analyzeLoopSequence(LoopSeqStmt: AStmt, SeqAnalysis, Context, Kind))
14797 return false;
14798
14799 // Diagnose an empty loop sequence.
14800 if (!SeqAnalysis.LoopSeqSize) {
14801 Diag(Loc: AStmt->getBeginLoc(), DiagID: diag::err_omp_empty_loop_sequence)
14802 << getOpenMPDirectiveName(D: Kind);
14803 return false;
14804 }
14805 return true;
14806}
14807
14808/// Add preinit statements that need to be propagated from the selected loop.
14809static void addLoopPreInits(ASTContext &Context,
14810 OMPLoopBasedDirective::HelperExprs &LoopHelper,
14811 Stmt *LoopStmt, ArrayRef<Stmt *> OriginalInit,
14812 SmallVectorImpl<Stmt *> &PreInits) {
14813
14814 // For range-based for-statements, ensure that their syntactic sugar is
14815 // executed by adding them as pre-init statements.
14816 if (auto *CXXRangeFor = dyn_cast<CXXForRangeStmt>(Val: LoopStmt)) {
14817 Stmt *RangeInit = CXXRangeFor->getInit();
14818 if (RangeInit)
14819 PreInits.push_back(Elt: RangeInit);
14820
14821 DeclStmt *RangeStmt = CXXRangeFor->getRangeStmt();
14822 PreInits.push_back(Elt: new (Context) DeclStmt(RangeStmt->getDeclGroup(),
14823 RangeStmt->getBeginLoc(),
14824 RangeStmt->getEndLoc()));
14825
14826 DeclStmt *RangeEnd = CXXRangeFor->getEndStmt();
14827 PreInits.push_back(Elt: new (Context) DeclStmt(RangeEnd->getDeclGroup(),
14828 RangeEnd->getBeginLoc(),
14829 RangeEnd->getEndLoc()));
14830 }
14831
14832 llvm::append_range(C&: PreInits, R&: OriginalInit);
14833
14834 // List of OMPCapturedExprDecl, for __begin, __end, and NumIterations
14835 if (auto *PI = cast_or_null<DeclStmt>(Val: LoopHelper.PreInits)) {
14836 PreInits.push_back(Elt: new (Context) DeclStmt(
14837 PI->getDeclGroup(), PI->getBeginLoc(), PI->getEndLoc()));
14838 }
14839
14840 // Gather declarations for the data members used as counters.
14841 for (Expr *CounterRef : LoopHelper.Counters) {
14842 auto *CounterDecl = cast<DeclRefExpr>(Val: CounterRef)->getDecl();
14843 if (isa<OMPCapturedExprDecl>(Val: CounterDecl))
14844 PreInits.push_back(Elt: new (Context) DeclStmt(
14845 DeclGroupRef(CounterDecl), SourceLocation(), SourceLocation()));
14846 }
14847}
14848
14849/// Collect the loop statements (ForStmt or CXXRangeForStmt) of the affected
14850/// loop of a construct.
14851static void collectLoopStmts(Stmt *AStmt, MutableArrayRef<Stmt *> LoopStmts) {
14852 size_t NumLoops = LoopStmts.size();
14853 OMPLoopBasedDirective::doForAllLoops(
14854 CurStmt: AStmt, /*TryImperfectlyNestedLoops=*/false, NumLoops,
14855 Callback: [LoopStmts](unsigned Cnt, Stmt *CurStmt) {
14856 assert(!LoopStmts[Cnt] && "Loop statement must not yet be assigned");
14857 LoopStmts[Cnt] = CurStmt;
14858 return false;
14859 });
14860 assert(!is_contained(LoopStmts, nullptr) &&
14861 "Expecting a loop statement for each affected loop");
14862}
14863
14864/// Build and return a DeclRefExpr for the floor induction variable using the
14865/// SemaRef and the provided parameters.
14866static Expr *makeFloorIVRef(Sema &SemaRef, ArrayRef<VarDecl *> FloorIndVars,
14867 int I, QualType IVTy, DeclRefExpr *OrigCntVar) {
14868 return buildDeclRefExpr(S&: SemaRef, D: FloorIndVars[I], Ty: IVTy,
14869 Loc: OrigCntVar->getExprLoc());
14870}
14871
14872StmtResult SemaOpenMP::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses,
14873 Stmt *AStmt,
14874 SourceLocation StartLoc,
14875 SourceLocation EndLoc) {
14876 ASTContext &Context = getASTContext();
14877 Scope *CurScope = SemaRef.getCurScope();
14878
14879 const auto *SizesClause =
14880 OMPExecutableDirective::getSingleClause<OMPSizesClause>(Clauses);
14881 if (!SizesClause ||
14882 llvm::any_of(Range: SizesClause->getSizesRefs(), P: [](Expr *E) { return !E; }))
14883 return StmtError();
14884 unsigned NumLoops = SizesClause->getNumSizes();
14885
14886 // Empty statement should only be possible if there already was an error.
14887 if (!AStmt)
14888 return StmtError();
14889
14890 // Verify and diagnose loop nest.
14891 SmallVector<OMPLoopBasedDirective::HelperExprs, 4> LoopHelpers(NumLoops);
14892 Stmt *Body = nullptr;
14893 SmallVector<SmallVector<Stmt *>, 4> OriginalInits;
14894 if (!checkTransformableLoopNest(Kind: OMPD_tile, AStmt, NumLoops, LoopHelpers, Body,
14895 OriginalInits))
14896 return StmtError();
14897
14898 // Delay tiling to when template is completely instantiated.
14899 if (SemaRef.CurContext->isDependentContext())
14900 return OMPTileDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
14901 NumLoops, AssociatedStmt: AStmt, TransformedStmt: nullptr, PreInits: nullptr);
14902
14903 assert(LoopHelpers.size() == NumLoops &&
14904 "Expecting loop iteration space dimensionality to match number of "
14905 "affected loops");
14906 assert(OriginalInits.size() == NumLoops &&
14907 "Expecting loop iteration space dimensionality to match number of "
14908 "affected loops");
14909
14910 // Collect all affected loop statements.
14911 SmallVector<Stmt *> LoopStmts(NumLoops, nullptr);
14912 collectLoopStmts(AStmt, LoopStmts);
14913
14914 SmallVector<Stmt *, 4> PreInits;
14915 CaptureVars CopyTransformer(SemaRef);
14916
14917 // Create iteration variables for the generated loops.
14918 SmallVector<VarDecl *, 4> FloorIndVars;
14919 SmallVector<VarDecl *, 4> TileIndVars;
14920 FloorIndVars.resize(N: NumLoops);
14921 TileIndVars.resize(N: NumLoops);
14922 for (unsigned I = 0; I < NumLoops; ++I) {
14923 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
14924
14925 assert(LoopHelper.Counters.size() == 1 &&
14926 "Expect single-dimensional loop iteration space");
14927 auto *OrigCntVar = cast<DeclRefExpr>(Val: LoopHelper.Counters.front());
14928 std::string OrigVarName = OrigCntVar->getNameInfo().getAsString();
14929 DeclRefExpr *IterVarRef = cast<DeclRefExpr>(Val: LoopHelper.IterationVarRef);
14930 QualType CntTy = IterVarRef->getType();
14931
14932 // Iteration variable for the floor (i.e. outer) loop.
14933 {
14934 std::string FloorCntName =
14935 (Twine(".floor_") + llvm::utostr(X: I) + ".iv." + OrigVarName).str();
14936 VarDecl *FloorCntDecl =
14937 buildVarDecl(SemaRef, Loc: {}, Type: CntTy, Name: FloorCntName, Attrs: nullptr, OrigRef: OrigCntVar);
14938 FloorIndVars[I] = FloorCntDecl;
14939 }
14940
14941 // Iteration variable for the tile (i.e. inner) loop.
14942 {
14943 std::string TileCntName =
14944 (Twine(".tile_") + llvm::utostr(X: I) + ".iv." + OrigVarName).str();
14945
14946 // Reuse the iteration variable created by checkOpenMPLoop. It is also
14947 // used by the expressions to derive the original iteration variable's
14948 // value from the logical iteration number.
14949 auto *TileCntDecl = cast<VarDecl>(Val: IterVarRef->getDecl());
14950 TileCntDecl->setDeclName(
14951 &SemaRef.PP.getIdentifierTable().get(Name: TileCntName));
14952 TileIndVars[I] = TileCntDecl;
14953 }
14954
14955 addLoopPreInits(Context, LoopHelper, LoopStmt: LoopStmts[I], OriginalInit: OriginalInits[I],
14956 PreInits);
14957 }
14958
14959 // Once the original iteration values are set, append the innermost body.
14960 Stmt *Inner = Body;
14961
14962 auto MakeDimTileSize = [&SemaRef = this->SemaRef, &CopyTransformer, &Context,
14963 SizesClause, CurScope](int I) -> Expr * {
14964 Expr *DimTileSizeExpr = SizesClause->getSizesRefs()[I];
14965
14966 if (DimTileSizeExpr->containsErrors())
14967 return nullptr;
14968
14969 if (isa<ConstantExpr>(Val: DimTileSizeExpr))
14970 return AssertSuccess(R: CopyTransformer.TransformExpr(E: DimTileSizeExpr));
14971
14972 // When the tile size is not a constant but a variable, it is possible to
14973 // pass non-positive numbers. For instance:
14974 // \code{c}
14975 // int a = 0;
14976 // #pragma omp tile sizes(a)
14977 // for (int i = 0; i < 42; ++i)
14978 // body(i);
14979 // \endcode
14980 // Although there is no meaningful interpretation of the tile size, the body
14981 // should still be executed 42 times to avoid surprises. To preserve the
14982 // invariant that every loop iteration is executed exactly once and not
14983 // cause an infinite loop, apply a minimum tile size of one.
14984 // Build expr:
14985 // \code{c}
14986 // (TS <= 0) ? 1 : TS
14987 // \endcode
14988 QualType DimTy = DimTileSizeExpr->getType();
14989 uint64_t DimWidth = Context.getTypeSize(T: DimTy);
14990 IntegerLiteral *Zero = IntegerLiteral::Create(
14991 C: Context, V: llvm::APInt::getZero(numBits: DimWidth), type: DimTy, l: {});
14992 IntegerLiteral *One =
14993 IntegerLiteral::Create(C: Context, V: llvm::APInt(DimWidth, 1), type: DimTy, l: {});
14994 Expr *Cond = AssertSuccess(R: SemaRef.BuildBinOp(
14995 S: CurScope, OpLoc: {}, Opc: BO_LE,
14996 LHSExpr: AssertSuccess(R: CopyTransformer.TransformExpr(E: DimTileSizeExpr)), RHSExpr: Zero));
14997 Expr *MinOne = new (Context) ConditionalOperator(
14998 Cond, {}, One, {},
14999 AssertSuccess(R: CopyTransformer.TransformExpr(E: DimTileSizeExpr)), DimTy,
15000 VK_PRValue, OK_Ordinary);
15001 return MinOne;
15002 };
15003
15004 // Create tile loops from the inside to the outside.
15005 for (int I = NumLoops - 1; I >= 0; --I) {
15006 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
15007 Expr *NumIterations = LoopHelper.NumIterations;
15008 auto *OrigCntVar = cast<DeclRefExpr>(Val: LoopHelper.Counters[0]);
15009 QualType IVTy = NumIterations->getType();
15010 Stmt *LoopStmt = LoopStmts[I];
15011
15012 // Commonly used variables. One of the constraints of an AST is that every
15013 // node object must appear at most once, hence we define a lambda that
15014 // creates a new AST node at every use.
15015 auto MakeTileIVRef = [&SemaRef = this->SemaRef, &TileIndVars, I, IVTy,
15016 OrigCntVar]() {
15017 return buildDeclRefExpr(S&: SemaRef, D: TileIndVars[I], Ty: IVTy,
15018 Loc: OrigCntVar->getExprLoc());
15019 };
15020
15021 // For init-statement: auto .tile.iv = .floor.iv
15022 SemaRef.AddInitializerToDecl(
15023 dcl: TileIndVars[I],
15024 init: SemaRef
15025 .DefaultLvalueConversion(
15026 E: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar))
15027 .get(),
15028 /*DirectInit=*/false);
15029 Decl *CounterDecl = TileIndVars[I];
15030 StmtResult InitStmt = new (Context)
15031 DeclStmt(DeclGroupRef::Create(C&: Context, Decls: &CounterDecl, NumDecls: 1),
15032 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc());
15033 if (!InitStmt.isUsable())
15034 return StmtError();
15035
15036 // For cond-expression:
15037 // .tile.iv < min(.floor.iv + DimTileSize, NumIterations)
15038 Expr *DimTileSize = MakeDimTileSize(I);
15039 if (!DimTileSize)
15040 return StmtError();
15041 ExprResult EndOfTile = SemaRef.BuildBinOp(
15042 S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_Add,
15043 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
15044 RHSExpr: DimTileSize);
15045 if (!EndOfTile.isUsable())
15046 return StmtError();
15047 ExprResult IsPartialTile =
15048 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15049 LHSExpr: NumIterations, RHSExpr: EndOfTile.get());
15050 if (!IsPartialTile.isUsable())
15051 return StmtError();
15052 ExprResult MinTileAndIterSpace = SemaRef.ActOnConditionalOp(
15053 QuestionLoc: LoopHelper.Cond->getBeginLoc(), ColonLoc: LoopHelper.Cond->getEndLoc(),
15054 CondExpr: IsPartialTile.get(), LHSExpr: NumIterations, RHSExpr: EndOfTile.get());
15055 if (!MinTileAndIterSpace.isUsable())
15056 return StmtError();
15057 ExprResult CondExpr =
15058 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15059 LHSExpr: MakeTileIVRef(), RHSExpr: MinTileAndIterSpace.get());
15060 if (!CondExpr.isUsable())
15061 return StmtError();
15062
15063 // For incr-statement: ++.tile.iv
15064 ExprResult IncrStmt = SemaRef.BuildUnaryOp(
15065 S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(), Opc: UO_PreInc, Input: MakeTileIVRef());
15066 if (!IncrStmt.isUsable())
15067 return StmtError();
15068
15069 // Statements to set the original iteration variable's value from the
15070 // logical iteration number.
15071 // Generated for loop is:
15072 // \code
15073 // Original_for_init;
15074 // for (auto .tile.iv = .floor.iv;
15075 // .tile.iv < min(.floor.iv + DimTileSize, NumIterations);
15076 // ++.tile.iv) {
15077 // Original_Body;
15078 // Original_counter_update;
15079 // }
15080 // \endcode
15081 // FIXME: If the innermost body is an loop itself, inserting these
15082 // statements stops it being recognized as a perfectly nested loop (e.g.
15083 // for applying tiling again). If this is the case, sink the expressions
15084 // further into the inner loop.
15085 SmallVector<Stmt *, 4> BodyParts;
15086 BodyParts.append(in_start: LoopHelper.Updates.begin(), in_end: LoopHelper.Updates.end());
15087 if (auto *SourceCXXFor = dyn_cast<CXXForRangeStmt>(Val: LoopStmt))
15088 BodyParts.push_back(Elt: SourceCXXFor->getLoopVarStmt());
15089 BodyParts.push_back(Elt: Inner);
15090 Inner = CompoundStmt::Create(C: Context, Stmts: BodyParts, FPFeatures: FPOptionsOverride(),
15091 LB: Inner->getBeginLoc(), RB: Inner->getEndLoc());
15092 Inner = new (Context)
15093 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr,
15094 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(),
15095 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
15096 }
15097
15098 // Create floor loops from the inside to the outside.
15099 for (int I = NumLoops - 1; I >= 0; --I) {
15100 auto &LoopHelper = LoopHelpers[I];
15101 Expr *NumIterations = LoopHelper.NumIterations;
15102 DeclRefExpr *OrigCntVar = cast<DeclRefExpr>(Val: LoopHelper.Counters[0]);
15103 QualType IVTy = NumIterations->getType();
15104
15105 // For init-statement: auto .floor.iv = 0
15106 SemaRef.AddInitializerToDecl(
15107 dcl: FloorIndVars[I],
15108 init: SemaRef.ActOnIntegerConstant(Loc: LoopHelper.Init->getExprLoc(), Val: 0).get(),
15109 /*DirectInit=*/false);
15110 Decl *CounterDecl = FloorIndVars[I];
15111 StmtResult InitStmt = new (Context)
15112 DeclStmt(DeclGroupRef::Create(C&: Context, Decls: &CounterDecl, NumDecls: 1),
15113 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc());
15114 if (!InitStmt.isUsable())
15115 return StmtError();
15116
15117 // For cond-expression: .floor.iv < NumIterations
15118 ExprResult CondExpr = SemaRef.BuildBinOp(
15119 S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15120 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
15121 RHSExpr: NumIterations);
15122 if (!CondExpr.isUsable())
15123 return StmtError();
15124
15125 // For incr-statement: .floor.iv += DimTileSize
15126 Expr *DimTileSize = MakeDimTileSize(I);
15127 if (!DimTileSize)
15128 return StmtError();
15129 ExprResult IncrStmt = SemaRef.BuildBinOp(
15130 S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(), Opc: BO_AddAssign,
15131 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
15132 RHSExpr: DimTileSize);
15133 if (!IncrStmt.isUsable())
15134 return StmtError();
15135
15136 Inner = new (Context)
15137 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr,
15138 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(),
15139 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
15140 }
15141
15142 return OMPTileDirective::Create(C: Context, StartLoc, EndLoc, Clauses, NumLoops,
15143 AssociatedStmt: AStmt, TransformedStmt: Inner,
15144 PreInits: buildPreInits(Context, PreInits));
15145}
15146
15147StmtResult SemaOpenMP::ActOnOpenMPStripeDirective(ArrayRef<OMPClause *> Clauses,
15148 Stmt *AStmt,
15149 SourceLocation StartLoc,
15150 SourceLocation EndLoc) {
15151 ASTContext &Context = getASTContext();
15152 Scope *CurScope = SemaRef.getCurScope();
15153
15154 const auto *SizesClause =
15155 OMPExecutableDirective::getSingleClause<OMPSizesClause>(Clauses);
15156 if (!SizesClause ||
15157 llvm::any_of(Range: SizesClause->getSizesRefs(), P: [](const Expr *SizeExpr) {
15158 return !SizeExpr || SizeExpr->containsErrors();
15159 }))
15160 return StmtError();
15161 unsigned NumLoops = SizesClause->getNumSizes();
15162
15163 // Empty statement should only be possible if there already was an error.
15164 if (!AStmt)
15165 return StmtError();
15166
15167 // Verify and diagnose loop nest.
15168 SmallVector<OMPLoopBasedDirective::HelperExprs, 4> LoopHelpers(NumLoops);
15169 Stmt *Body = nullptr;
15170 SmallVector<SmallVector<Stmt *>, 4> OriginalInits;
15171 if (!checkTransformableLoopNest(Kind: OMPD_stripe, AStmt, NumLoops, LoopHelpers,
15172 Body, OriginalInits))
15173 return StmtError();
15174
15175 // Delay striping to when template is completely instantiated.
15176 if (SemaRef.CurContext->isDependentContext())
15177 return OMPStripeDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
15178 NumLoops, AssociatedStmt: AStmt, TransformedStmt: nullptr, PreInits: nullptr);
15179
15180 assert(LoopHelpers.size() == NumLoops &&
15181 "Expecting loop iteration space dimensionality to match number of "
15182 "affected loops");
15183 assert(OriginalInits.size() == NumLoops &&
15184 "Expecting loop iteration space dimensionality to match number of "
15185 "affected loops");
15186
15187 // Collect all affected loop statements.
15188 SmallVector<Stmt *> LoopStmts(NumLoops, nullptr);
15189 collectLoopStmts(AStmt, LoopStmts);
15190
15191 SmallVector<Stmt *, 4> PreInits;
15192 CaptureVars CopyTransformer(SemaRef);
15193
15194 // Create iteration variables for the generated loops.
15195 SmallVector<VarDecl *, 4> FloorIndVars;
15196 SmallVector<VarDecl *, 4> StripeIndVars;
15197 FloorIndVars.resize(N: NumLoops);
15198 StripeIndVars.resize(N: NumLoops);
15199 for (unsigned I : llvm::seq<unsigned>(Size: NumLoops)) {
15200 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
15201
15202 assert(LoopHelper.Counters.size() == 1 &&
15203 "Expect single-dimensional loop iteration space");
15204 auto *OrigCntVar = cast<DeclRefExpr>(Val: LoopHelper.Counters.front());
15205 std::string OrigVarName = OrigCntVar->getNameInfo().getAsString();
15206 DeclRefExpr *IterVarRef = cast<DeclRefExpr>(Val: LoopHelper.IterationVarRef);
15207 QualType CntTy = IterVarRef->getType();
15208
15209 // Iteration variable for the stripe (i.e. outer) loop.
15210 {
15211 std::string FloorCntName =
15212 (Twine(".floor_") + llvm::utostr(X: I) + ".iv." + OrigVarName).str();
15213 VarDecl *FloorCntDecl =
15214 buildVarDecl(SemaRef, Loc: {}, Type: CntTy, Name: FloorCntName, Attrs: nullptr, OrigRef: OrigCntVar);
15215 FloorIndVars[I] = FloorCntDecl;
15216 }
15217
15218 // Iteration variable for the stripe (i.e. inner) loop.
15219 {
15220 std::string StripeCntName =
15221 (Twine(".stripe_") + llvm::utostr(X: I) + ".iv." + OrigVarName).str();
15222
15223 // Reuse the iteration variable created by checkOpenMPLoop. It is also
15224 // used by the expressions to derive the original iteration variable's
15225 // value from the logical iteration number.
15226 auto *StripeCntDecl = cast<VarDecl>(Val: IterVarRef->getDecl());
15227 StripeCntDecl->setDeclName(
15228 &SemaRef.PP.getIdentifierTable().get(Name: StripeCntName));
15229 StripeIndVars[I] = StripeCntDecl;
15230 }
15231
15232 addLoopPreInits(Context, LoopHelper, LoopStmt: LoopStmts[I], OriginalInit: OriginalInits[I],
15233 PreInits);
15234 }
15235
15236 // Once the original iteration values are set, append the innermost body.
15237 Stmt *Inner = Body;
15238
15239 auto MakeDimStripeSize = [&](int I) -> Expr * {
15240 Expr *DimStripeSizeExpr = SizesClause->getSizesRefs()[I];
15241 if (isa<ConstantExpr>(Val: DimStripeSizeExpr))
15242 return AssertSuccess(R: CopyTransformer.TransformExpr(E: DimStripeSizeExpr));
15243
15244 // When the stripe size is not a constant but a variable, it is possible to
15245 // pass non-positive numbers. For instance:
15246 // \code{c}
15247 // int a = 0;
15248 // #pragma omp stripe sizes(a)
15249 // for (int i = 0; i < 42; ++i)
15250 // body(i);
15251 // \endcode
15252 // Although there is no meaningful interpretation of the stripe size, the
15253 // body should still be executed 42 times to avoid surprises. To preserve
15254 // the invariant that every loop iteration is executed exactly once and not
15255 // cause an infinite loop, apply a minimum stripe size of one.
15256 // Build expr:
15257 // \code{c}
15258 // (TS <= 0) ? 1 : TS
15259 // \endcode
15260 QualType DimTy = DimStripeSizeExpr->getType();
15261 uint64_t DimWidth = Context.getTypeSize(T: DimTy);
15262 IntegerLiteral *Zero = IntegerLiteral::Create(
15263 C: Context, V: llvm::APInt::getZero(numBits: DimWidth), type: DimTy, l: {});
15264 IntegerLiteral *One =
15265 IntegerLiteral::Create(C: Context, V: llvm::APInt(DimWidth, 1), type: DimTy, l: {});
15266 Expr *Cond = AssertSuccess(R: SemaRef.BuildBinOp(
15267 S: CurScope, OpLoc: {}, Opc: BO_LE,
15268 LHSExpr: AssertSuccess(R: CopyTransformer.TransformExpr(E: DimStripeSizeExpr)), RHSExpr: Zero));
15269 Expr *MinOne = new (Context) ConditionalOperator(
15270 Cond, {}, One, {},
15271 AssertSuccess(R: CopyTransformer.TransformExpr(E: DimStripeSizeExpr)), DimTy,
15272 VK_PRValue, OK_Ordinary);
15273 return MinOne;
15274 };
15275
15276 // Create stripe loops from the inside to the outside.
15277 for (int I = NumLoops - 1; I >= 0; --I) {
15278 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
15279 Expr *NumIterations = LoopHelper.NumIterations;
15280 auto *OrigCntVar = cast<DeclRefExpr>(Val: LoopHelper.Counters[0]);
15281 QualType IVTy = NumIterations->getType();
15282 Stmt *LoopStmt = LoopStmts[I];
15283
15284 // For init-statement: auto .stripe.iv = .floor.iv
15285 SemaRef.AddInitializerToDecl(
15286 dcl: StripeIndVars[I],
15287 init: SemaRef
15288 .DefaultLvalueConversion(
15289 E: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar))
15290 .get(),
15291 /*DirectInit=*/false);
15292 Decl *CounterDecl = StripeIndVars[I];
15293 StmtResult InitStmt = new (Context)
15294 DeclStmt(DeclGroupRef::Create(C&: Context, Decls: &CounterDecl, NumDecls: 1),
15295 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc());
15296 if (!InitStmt.isUsable())
15297 return StmtError();
15298
15299 // For cond-expression:
15300 // .stripe.iv < min(.floor.iv + DimStripeSize, NumIterations)
15301 ExprResult EndOfStripe = SemaRef.BuildBinOp(
15302 S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_Add,
15303 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
15304 RHSExpr: MakeDimStripeSize(I));
15305 if (!EndOfStripe.isUsable())
15306 return StmtError();
15307 ExprResult IsPartialStripe =
15308 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15309 LHSExpr: NumIterations, RHSExpr: EndOfStripe.get());
15310 if (!IsPartialStripe.isUsable())
15311 return StmtError();
15312 ExprResult MinStripeAndIterSpace = SemaRef.ActOnConditionalOp(
15313 QuestionLoc: LoopHelper.Cond->getBeginLoc(), ColonLoc: LoopHelper.Cond->getEndLoc(),
15314 CondExpr: IsPartialStripe.get(), LHSExpr: NumIterations, RHSExpr: EndOfStripe.get());
15315 if (!MinStripeAndIterSpace.isUsable())
15316 return StmtError();
15317 ExprResult CondExpr = SemaRef.BuildBinOp(
15318 S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15319 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars: StripeIndVars, I, IVTy, OrigCntVar),
15320 RHSExpr: MinStripeAndIterSpace.get());
15321 if (!CondExpr.isUsable())
15322 return StmtError();
15323
15324 // For incr-statement: ++.stripe.iv
15325 ExprResult IncrStmt = SemaRef.BuildUnaryOp(
15326 S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(), Opc: UO_PreInc,
15327 Input: makeFloorIVRef(SemaRef, FloorIndVars: StripeIndVars, I, IVTy, OrigCntVar));
15328 if (!IncrStmt.isUsable())
15329 return StmtError();
15330
15331 // Statements to set the original iteration variable's value from the
15332 // logical iteration number.
15333 // Generated for loop is:
15334 // \code
15335 // Original_for_init;
15336 // for (auto .stripe.iv = .floor.iv;
15337 // .stripe.iv < min(.floor.iv + DimStripeSize, NumIterations);
15338 // ++.stripe.iv) {
15339 // Original_Body;
15340 // Original_counter_update;
15341 // }
15342 // \endcode
15343 // FIXME: If the innermost body is a loop itself, inserting these
15344 // statements stops it being recognized as a perfectly nested loop (e.g.
15345 // for applying another loop transformation). If this is the case, sink the
15346 // expressions further into the inner loop.
15347 SmallVector<Stmt *, 4> BodyParts;
15348 BodyParts.append(in_start: LoopHelper.Updates.begin(), in_end: LoopHelper.Updates.end());
15349 if (auto *SourceCXXFor = dyn_cast<CXXForRangeStmt>(Val: LoopStmt))
15350 BodyParts.push_back(Elt: SourceCXXFor->getLoopVarStmt());
15351 BodyParts.push_back(Elt: Inner);
15352 Inner = CompoundStmt::Create(C: Context, Stmts: BodyParts, FPFeatures: FPOptionsOverride(),
15353 LB: Inner->getBeginLoc(), RB: Inner->getEndLoc());
15354 Inner = new (Context)
15355 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr,
15356 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(),
15357 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
15358 }
15359
15360 // Create grid loops from the inside to the outside.
15361 for (int I = NumLoops - 1; I >= 0; --I) {
15362 auto &LoopHelper = LoopHelpers[I];
15363 Expr *NumIterations = LoopHelper.NumIterations;
15364 DeclRefExpr *OrigCntVar = cast<DeclRefExpr>(Val: LoopHelper.Counters[0]);
15365 QualType IVTy = NumIterations->getType();
15366
15367 // For init-statement: auto .grid.iv = 0
15368 SemaRef.AddInitializerToDecl(
15369 dcl: FloorIndVars[I],
15370 init: SemaRef.ActOnIntegerConstant(Loc: LoopHelper.Init->getExprLoc(), Val: 0).get(),
15371 /*DirectInit=*/false);
15372 Decl *CounterDecl = FloorIndVars[I];
15373 StmtResult InitStmt = new (Context)
15374 DeclStmt(DeclGroupRef::Create(C&: Context, Decls: &CounterDecl, NumDecls: 1),
15375 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc());
15376 if (!InitStmt.isUsable())
15377 return StmtError();
15378
15379 // For cond-expression: .floor.iv < NumIterations
15380 ExprResult CondExpr = SemaRef.BuildBinOp(
15381 S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15382 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
15383 RHSExpr: NumIterations);
15384 if (!CondExpr.isUsable())
15385 return StmtError();
15386
15387 // For incr-statement: .floor.iv += DimStripeSize
15388 ExprResult IncrStmt = SemaRef.BuildBinOp(
15389 S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(), Opc: BO_AddAssign,
15390 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
15391 RHSExpr: MakeDimStripeSize(I));
15392 if (!IncrStmt.isUsable())
15393 return StmtError();
15394
15395 Inner = new (Context)
15396 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr,
15397 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(),
15398 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
15399 }
15400
15401 return OMPStripeDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
15402 NumLoops, AssociatedStmt: AStmt, TransformedStmt: Inner,
15403 PreInits: buildPreInits(Context, PreInits));
15404}
15405
15406StmtResult SemaOpenMP::ActOnOpenMPUnrollDirective(ArrayRef<OMPClause *> Clauses,
15407 Stmt *AStmt,
15408 SourceLocation StartLoc,
15409 SourceLocation EndLoc) {
15410 ASTContext &Context = getASTContext();
15411 Scope *CurScope = SemaRef.getCurScope();
15412 // Empty statement should only be possible if there already was an error.
15413 if (!AStmt)
15414 return StmtError();
15415
15416 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
15417 MutuallyExclusiveClauses: {OMPC_partial, OMPC_full}))
15418 return StmtError();
15419
15420 const OMPFullClause *FullClause =
15421 OMPExecutableDirective::getSingleClause<OMPFullClause>(Clauses);
15422 const OMPPartialClause *PartialClause =
15423 OMPExecutableDirective::getSingleClause<OMPPartialClause>(Clauses);
15424 assert(!(FullClause && PartialClause) &&
15425 "mutual exclusivity must have been checked before");
15426
15427 constexpr unsigned NumLoops = 1;
15428 Stmt *Body = nullptr;
15429 SmallVector<OMPLoopBasedDirective::HelperExprs, NumLoops> LoopHelpers(
15430 NumLoops);
15431 SmallVector<SmallVector<Stmt *>, NumLoops + 1> OriginalInits;
15432 if (!checkTransformableLoopNest(Kind: OMPD_unroll, AStmt, NumLoops, LoopHelpers,
15433 Body, OriginalInits))
15434 return StmtError();
15435
15436 unsigned NumGeneratedTopLevelLoops = PartialClause ? 1 : 0;
15437
15438 // Delay unrolling to when template is completely instantiated.
15439 if (SemaRef.CurContext->isDependentContext())
15440 return OMPUnrollDirective::Create(C: Context, StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
15441 NumGeneratedTopLevelLoops, TransformedStmt: nullptr,
15442 PreInits: nullptr);
15443
15444 assert(LoopHelpers.size() == NumLoops &&
15445 "Expecting a single-dimensional loop iteration space");
15446 assert(OriginalInits.size() == NumLoops &&
15447 "Expecting a single-dimensional loop iteration space");
15448 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers.front();
15449
15450 if (FullClause) {
15451 if (!VerifyPositiveIntegerConstantInClause(
15452 Op: LoopHelper.NumIterations, CKind: OMPC_full, /*StrictlyPositive=*/false,
15453 /*SuppressExprDiags=*/true)
15454 .isUsable()) {
15455 Diag(Loc: AStmt->getBeginLoc(), DiagID: diag::err_omp_unroll_full_variable_trip_count);
15456 Diag(Loc: FullClause->getBeginLoc(), DiagID: diag::note_omp_directive_here)
15457 << "#pragma omp unroll full";
15458 return StmtError();
15459 }
15460 }
15461
15462 // The generated loop may only be passed to other loop-associated directive
15463 // when a partial clause is specified. Without the requirement it is
15464 // sufficient to generate loop unroll metadata at code-generation.
15465 if (NumGeneratedTopLevelLoops == 0)
15466 return OMPUnrollDirective::Create(C: Context, StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
15467 NumGeneratedTopLevelLoops, TransformedStmt: nullptr,
15468 PreInits: nullptr);
15469
15470 // Otherwise, we need to provide a de-sugared/transformed AST that can be
15471 // associated with another loop directive.
15472 //
15473 // The canonical loop analysis return by checkTransformableLoopNest assumes
15474 // the following structure to be the same loop without transformations or
15475 // directives applied: \code OriginalInits; LoopHelper.PreInits;
15476 // LoopHelper.Counters;
15477 // for (; IV < LoopHelper.NumIterations; ++IV) {
15478 // LoopHelper.Updates;
15479 // Body;
15480 // }
15481 // \endcode
15482 // where IV is a variable declared and initialized to 0 in LoopHelper.PreInits
15483 // and referenced by LoopHelper.IterationVarRef.
15484 //
15485 // The unrolling directive transforms this into the following loop:
15486 // \code
15487 // OriginalInits; \
15488 // LoopHelper.PreInits; > NewPreInits
15489 // LoopHelper.Counters; /
15490 // for (auto UIV = 0; UIV < LoopHelper.NumIterations; UIV+=Factor) {
15491 // #pragma clang loop unroll_count(Factor)
15492 // for (IV = UIV; IV < UIV + Factor && UIV < LoopHelper.NumIterations; ++IV)
15493 // {
15494 // LoopHelper.Updates;
15495 // Body;
15496 // }
15497 // }
15498 // \endcode
15499 // where UIV is a new logical iteration counter. IV must be the same VarDecl
15500 // as the original LoopHelper.IterationVarRef because LoopHelper.Updates
15501 // references it. If the partially unrolled loop is associated with another
15502 // loop directive (like an OMPForDirective), it will use checkOpenMPLoop to
15503 // analyze this loop, i.e. the outer loop must fulfill the constraints of an
15504 // OpenMP canonical loop. The inner loop is not an associable canonical loop
15505 // and only exists to defer its unrolling to LLVM's LoopUnroll instead of
15506 // doing it in the frontend (by adding loop metadata). NewPreInits becomes a
15507 // property of the OMPLoopBasedDirective instead of statements in
15508 // CompoundStatement. This is to allow the loop to become a non-outermost loop
15509 // of a canonical loop nest where these PreInits are emitted before the
15510 // outermost directive.
15511
15512 // Find the loop statement.
15513 Stmt *LoopStmt = nullptr;
15514 collectLoopStmts(AStmt, LoopStmts: {LoopStmt});
15515
15516 // Determine the PreInit declarations.
15517 SmallVector<Stmt *, 4> PreInits;
15518 addLoopPreInits(Context, LoopHelper, LoopStmt, OriginalInit: OriginalInits[0], PreInits);
15519
15520 auto *IterationVarRef = cast<DeclRefExpr>(Val: LoopHelper.IterationVarRef);
15521 QualType IVTy = IterationVarRef->getType();
15522 assert(LoopHelper.Counters.size() == 1 &&
15523 "Expecting a single-dimensional loop iteration space");
15524 auto *OrigVar = cast<DeclRefExpr>(Val: LoopHelper.Counters.front());
15525
15526 // Determine the unroll factor.
15527 uint64_t Factor;
15528 SourceLocation FactorLoc;
15529 if (Expr *FactorVal = PartialClause->getFactor();
15530 FactorVal && !FactorVal->containsErrors()) {
15531 Factor = FactorVal->getIntegerConstantExpr(Ctx: Context)->getZExtValue();
15532 FactorLoc = FactorVal->getExprLoc();
15533 } else {
15534 // TODO: Use a better profitability model.
15535 Factor = 2;
15536 }
15537 assert(Factor > 0 && "Expected positive unroll factor");
15538 auto MakeFactorExpr = [this, Factor, IVTy, FactorLoc]() {
15539 return IntegerLiteral::Create(
15540 C: getASTContext(), V: llvm::APInt(getASTContext().getIntWidth(T: IVTy), Factor),
15541 type: IVTy, l: FactorLoc);
15542 };
15543
15544 // Iteration variable SourceLocations.
15545 SourceLocation OrigVarLoc = OrigVar->getExprLoc();
15546 SourceLocation OrigVarLocBegin = OrigVar->getBeginLoc();
15547 SourceLocation OrigVarLocEnd = OrigVar->getEndLoc();
15548
15549 // Internal variable names.
15550 std::string OrigVarName = OrigVar->getNameInfo().getAsString();
15551 std::string OuterIVName = (Twine(".unrolled.iv.") + OrigVarName).str();
15552 std::string InnerIVName = (Twine(".unroll_inner.iv.") + OrigVarName).str();
15553
15554 // Create the iteration variable for the unrolled loop.
15555 VarDecl *OuterIVDecl =
15556 buildVarDecl(SemaRef, Loc: {}, Type: IVTy, Name: OuterIVName, Attrs: nullptr, OrigRef: OrigVar);
15557 auto MakeOuterRef = [this, OuterIVDecl, IVTy, OrigVarLoc]() {
15558 return buildDeclRefExpr(S&: SemaRef, D: OuterIVDecl, Ty: IVTy, Loc: OrigVarLoc);
15559 };
15560
15561 // Iteration variable for the inner loop: Reuse the iteration variable created
15562 // by checkOpenMPLoop.
15563 auto *InnerIVDecl = cast<VarDecl>(Val: IterationVarRef->getDecl());
15564 InnerIVDecl->setDeclName(&SemaRef.PP.getIdentifierTable().get(Name: InnerIVName));
15565 auto MakeInnerRef = [this, InnerIVDecl, IVTy, OrigVarLoc]() {
15566 return buildDeclRefExpr(S&: SemaRef, D: InnerIVDecl, Ty: IVTy, Loc: OrigVarLoc);
15567 };
15568
15569 // Make a copy of the NumIterations expression for each use: By the AST
15570 // constraints, every expression object in a DeclContext must be unique.
15571 CaptureVars CopyTransformer(SemaRef);
15572 auto MakeNumIterations = [&CopyTransformer, &LoopHelper]() -> Expr * {
15573 return AssertSuccess(
15574 R: CopyTransformer.TransformExpr(E: LoopHelper.NumIterations));
15575 };
15576
15577 // Inner For init-statement: auto .unroll_inner.iv = .unrolled.iv
15578 ExprResult LValueConv = SemaRef.DefaultLvalueConversion(E: MakeOuterRef());
15579 SemaRef.AddInitializerToDecl(dcl: InnerIVDecl, init: LValueConv.get(),
15580 /*DirectInit=*/false);
15581 StmtResult InnerInit = new (Context)
15582 DeclStmt(DeclGroupRef(InnerIVDecl), OrigVarLocBegin, OrigVarLocEnd);
15583 if (!InnerInit.isUsable())
15584 return StmtError();
15585
15586 // Inner For cond-expression:
15587 // \code
15588 // .unroll_inner.iv < .unrolled.iv + Factor &&
15589 // .unroll_inner.iv < NumIterations
15590 // \endcode
15591 // This conjunction of two conditions allows ScalarEvolution to derive the
15592 // maximum trip count of the inner loop.
15593 ExprResult EndOfTile =
15594 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_Add,
15595 LHSExpr: MakeOuterRef(), RHSExpr: MakeFactorExpr());
15596 if (!EndOfTile.isUsable())
15597 return StmtError();
15598 ExprResult InnerCond1 =
15599 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15600 LHSExpr: MakeInnerRef(), RHSExpr: EndOfTile.get());
15601 if (!InnerCond1.isUsable())
15602 return StmtError();
15603 ExprResult InnerCond2 =
15604 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15605 LHSExpr: MakeInnerRef(), RHSExpr: MakeNumIterations());
15606 if (!InnerCond2.isUsable())
15607 return StmtError();
15608 ExprResult InnerCond =
15609 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LAnd,
15610 LHSExpr: InnerCond1.get(), RHSExpr: InnerCond2.get());
15611 if (!InnerCond.isUsable())
15612 return StmtError();
15613
15614 // Inner For incr-statement: ++.unroll_inner.iv
15615 ExprResult InnerIncr = SemaRef.BuildUnaryOp(
15616 S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(), Opc: UO_PreInc, Input: MakeInnerRef());
15617 if (!InnerIncr.isUsable())
15618 return StmtError();
15619
15620 // Inner For statement.
15621 SmallVector<Stmt *> InnerBodyStmts;
15622 InnerBodyStmts.append(in_start: LoopHelper.Updates.begin(), in_end: LoopHelper.Updates.end());
15623 if (auto *CXXRangeFor = dyn_cast<CXXForRangeStmt>(Val: LoopStmt))
15624 InnerBodyStmts.push_back(Elt: CXXRangeFor->getLoopVarStmt());
15625 InnerBodyStmts.push_back(Elt: Body);
15626 CompoundStmt *InnerBody =
15627 CompoundStmt::Create(C: getASTContext(), Stmts: InnerBodyStmts, FPFeatures: FPOptionsOverride(),
15628 LB: Body->getBeginLoc(), RB: Body->getEndLoc());
15629 ForStmt *InnerFor = new (Context)
15630 ForStmt(Context, InnerInit.get(), InnerCond.get(), nullptr,
15631 InnerIncr.get(), InnerBody, LoopHelper.Init->getBeginLoc(),
15632 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
15633
15634 // Unroll metadata for the inner loop.
15635 // This needs to take into account the remainder portion of the unrolled loop,
15636 // hence `unroll(full)` does not apply here, even though the LoopUnroll pass
15637 // supports multiple loop exits. Instead, unroll using a factor equivalent to
15638 // the maximum trip count, which will also generate a remainder loop. Just
15639 // `unroll(enable)` (which could have been useful if the user has not
15640 // specified a concrete factor; even though the outer loop cannot be
15641 // influenced anymore, would avoid more code bloat than necessary) will refuse
15642 // the loop because "Won't unroll; remainder loop could not be generated when
15643 // assuming runtime trip count". Even if it did work, it must not choose a
15644 // larger unroll factor than the maximum loop length, or it would always just
15645 // execute the remainder loop.
15646 LoopHintAttr *UnrollHintAttr =
15647 LoopHintAttr::CreateImplicit(Ctx&: Context, Option: LoopHintAttr::UnrollCount,
15648 State: LoopHintAttr::Numeric, Value: MakeFactorExpr());
15649 AttributedStmt *InnerUnrolled = AttributedStmt::Create(
15650 C: getASTContext(), Loc: StartLoc, Attrs: {UnrollHintAttr}, SubStmt: InnerFor);
15651
15652 // Outer For init-statement: auto .unrolled.iv = 0
15653 SemaRef.AddInitializerToDecl(
15654 dcl: OuterIVDecl,
15655 init: SemaRef.ActOnIntegerConstant(Loc: LoopHelper.Init->getExprLoc(), Val: 0).get(),
15656 /*DirectInit=*/false);
15657 StmtResult OuterInit = new (Context)
15658 DeclStmt(DeclGroupRef(OuterIVDecl), OrigVarLocBegin, OrigVarLocEnd);
15659 if (!OuterInit.isUsable())
15660 return StmtError();
15661
15662 // Outer For cond-expression: .unrolled.iv < NumIterations
15663 ExprResult OuterConde =
15664 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15665 LHSExpr: MakeOuterRef(), RHSExpr: MakeNumIterations());
15666 if (!OuterConde.isUsable())
15667 return StmtError();
15668
15669 // Outer For incr-statement: .unrolled.iv += Factor
15670 ExprResult OuterIncr =
15671 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(), Opc: BO_AddAssign,
15672 LHSExpr: MakeOuterRef(), RHSExpr: MakeFactorExpr());
15673 if (!OuterIncr.isUsable())
15674 return StmtError();
15675
15676 // Outer For statement.
15677 ForStmt *OuterFor = new (Context)
15678 ForStmt(Context, OuterInit.get(), OuterConde.get(), nullptr,
15679 OuterIncr.get(), InnerUnrolled, LoopHelper.Init->getBeginLoc(),
15680 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
15681
15682 return OMPUnrollDirective::Create(C: Context, StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
15683 NumGeneratedTopLevelLoops, TransformedStmt: OuterFor,
15684 PreInits: buildPreInits(Context, PreInits));
15685}
15686
15687StmtResult SemaOpenMP::ActOnOpenMPReverseDirective(Stmt *AStmt,
15688 SourceLocation StartLoc,
15689 SourceLocation EndLoc) {
15690 ASTContext &Context = getASTContext();
15691 Scope *CurScope = SemaRef.getCurScope();
15692
15693 // Empty statement should only be possible if there already was an error.
15694 if (!AStmt)
15695 return StmtError();
15696
15697 constexpr unsigned NumLoops = 1;
15698 Stmt *Body = nullptr;
15699 SmallVector<OMPLoopBasedDirective::HelperExprs, NumLoops> LoopHelpers(
15700 NumLoops);
15701 SmallVector<SmallVector<Stmt *>, NumLoops + 1> OriginalInits;
15702 if (!checkTransformableLoopNest(Kind: OMPD_reverse, AStmt, NumLoops, LoopHelpers,
15703 Body, OriginalInits))
15704 return StmtError();
15705
15706 // Delay applying the transformation to when template is completely
15707 // instantiated.
15708 if (SemaRef.CurContext->isDependentContext())
15709 return OMPReverseDirective::Create(C: Context, StartLoc, EndLoc, AssociatedStmt: AStmt,
15710 NumLoops, TransformedStmt: nullptr, PreInits: nullptr);
15711
15712 assert(LoopHelpers.size() == NumLoops &&
15713 "Expecting a single-dimensional loop iteration space");
15714 assert(OriginalInits.size() == NumLoops &&
15715 "Expecting a single-dimensional loop iteration space");
15716 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers.front();
15717
15718 // Find the loop statement.
15719 Stmt *LoopStmt = nullptr;
15720 collectLoopStmts(AStmt, LoopStmts: {LoopStmt});
15721
15722 // Determine the PreInit declarations.
15723 SmallVector<Stmt *> PreInits;
15724 addLoopPreInits(Context, LoopHelper, LoopStmt, OriginalInit: OriginalInits[0], PreInits);
15725
15726 auto *IterationVarRef = cast<DeclRefExpr>(Val: LoopHelper.IterationVarRef);
15727 QualType IVTy = IterationVarRef->getType();
15728 uint64_t IVWidth = Context.getTypeSize(T: IVTy);
15729 auto *OrigVar = cast<DeclRefExpr>(Val: LoopHelper.Counters.front());
15730
15731 // Iteration variable SourceLocations.
15732 SourceLocation OrigVarLoc = OrigVar->getExprLoc();
15733 SourceLocation OrigVarLocBegin = OrigVar->getBeginLoc();
15734 SourceLocation OrigVarLocEnd = OrigVar->getEndLoc();
15735
15736 // Locations pointing to the transformation.
15737 SourceLocation TransformLoc = StartLoc;
15738 SourceLocation TransformLocBegin = StartLoc;
15739 SourceLocation TransformLocEnd = EndLoc;
15740
15741 // Internal variable names.
15742 std::string OrigVarName = OrigVar->getNameInfo().getAsString();
15743 SmallString<64> ForwardIVName(".forward.iv.");
15744 ForwardIVName += OrigVarName;
15745 SmallString<64> ReversedIVName(".reversed.iv.");
15746 ReversedIVName += OrigVarName;
15747
15748 // LoopHelper.Updates will read the logical iteration number from
15749 // LoopHelper.IterationVarRef, compute the value of the user loop counter of
15750 // that logical iteration from it, then assign it to the user loop counter
15751 // variable. We cannot directly use LoopHelper.IterationVarRef as the
15752 // induction variable of the generated loop because it may cause an underflow:
15753 // \code{.c}
15754 // for (unsigned i = 0; i < n; ++i)
15755 // body(i);
15756 // \endcode
15757 //
15758 // Naive reversal:
15759 // \code{.c}
15760 // for (unsigned i = n-1; i >= 0; --i)
15761 // body(i);
15762 // \endcode
15763 //
15764 // Instead, we introduce a new iteration variable representing the logical
15765 // iteration counter of the original loop, convert it to the logical iteration
15766 // number of the reversed loop, then let LoopHelper.Updates compute the user's
15767 // loop iteration variable from it.
15768 // \code{.cpp}
15769 // for (auto .forward.iv = 0; .forward.iv < n; ++.forward.iv) {
15770 // auto .reversed.iv = n - .forward.iv - 1;
15771 // i = (.reversed.iv + 0) * 1; // LoopHelper.Updates
15772 // body(i); // Body
15773 // }
15774 // \endcode
15775
15776 // Subexpressions with more than one use. One of the constraints of an AST is
15777 // that every node object must appear at most once, hence we define a lambda
15778 // that creates a new AST node at every use.
15779 CaptureVars CopyTransformer(SemaRef);
15780 auto MakeNumIterations = [&CopyTransformer, &LoopHelper]() -> Expr * {
15781 return AssertSuccess(
15782 R: CopyTransformer.TransformExpr(E: LoopHelper.NumIterations));
15783 };
15784
15785 // Create the iteration variable for the forward loop (from 0 to n-1).
15786 VarDecl *ForwardIVDecl =
15787 buildVarDecl(SemaRef, Loc: {}, Type: IVTy, Name: ForwardIVName, Attrs: nullptr, OrigRef: OrigVar);
15788 auto MakeForwardRef = [&SemaRef = this->SemaRef, ForwardIVDecl, IVTy,
15789 OrigVarLoc]() {
15790 return buildDeclRefExpr(S&: SemaRef, D: ForwardIVDecl, Ty: IVTy, Loc: OrigVarLoc);
15791 };
15792
15793 // Iteration variable for the reversed induction variable (from n-1 downto 0):
15794 // Reuse the iteration variable created by checkOpenMPLoop.
15795 auto *ReversedIVDecl = cast<VarDecl>(Val: IterationVarRef->getDecl());
15796 ReversedIVDecl->setDeclName(
15797 &SemaRef.PP.getIdentifierTable().get(Name: ReversedIVName));
15798
15799 // For init-statement:
15800 // \code{.cpp}
15801 // auto .forward.iv = 0;
15802 // \endcode
15803 auto *Zero = IntegerLiteral::Create(C: Context, V: llvm::APInt::getZero(numBits: IVWidth),
15804 type: ForwardIVDecl->getType(), l: OrigVarLoc);
15805 SemaRef.AddInitializerToDecl(dcl: ForwardIVDecl, init: Zero, /*DirectInit=*/false);
15806 StmtResult Init = new (Context)
15807 DeclStmt(DeclGroupRef(ForwardIVDecl), OrigVarLocBegin, OrigVarLocEnd);
15808 if (!Init.isUsable())
15809 return StmtError();
15810
15811 // Forward iv cond-expression:
15812 // \code{.cpp}
15813 // .forward.iv < MakeNumIterations()
15814 // \endcode
15815 ExprResult Cond =
15816 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15817 LHSExpr: MakeForwardRef(), RHSExpr: MakeNumIterations());
15818 if (!Cond.isUsable())
15819 return StmtError();
15820
15821 // Forward incr-statement:
15822 // \code{.c}
15823 // ++.forward.iv
15824 // \endcode
15825 ExprResult Incr = SemaRef.BuildUnaryOp(S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(),
15826 Opc: UO_PreInc, Input: MakeForwardRef());
15827 if (!Incr.isUsable())
15828 return StmtError();
15829
15830 // Reverse the forward-iv:
15831 // \code{.cpp}
15832 // auto .reversed.iv = MakeNumIterations() - 1 - .forward.iv
15833 // \endcode
15834 auto *One = IntegerLiteral::Create(C: Context, V: llvm::APInt(IVWidth, 1), type: IVTy,
15835 l: TransformLoc);
15836 ExprResult Minus = SemaRef.BuildBinOp(S: CurScope, OpLoc: TransformLoc, Opc: BO_Sub,
15837 LHSExpr: MakeNumIterations(), RHSExpr: One);
15838 if (!Minus.isUsable())
15839 return StmtError();
15840 Minus = SemaRef.BuildBinOp(S: CurScope, OpLoc: TransformLoc, Opc: BO_Sub, LHSExpr: Minus.get(),
15841 RHSExpr: MakeForwardRef());
15842 if (!Minus.isUsable())
15843 return StmtError();
15844 StmtResult InitReversed = new (Context) DeclStmt(
15845 DeclGroupRef(ReversedIVDecl), TransformLocBegin, TransformLocEnd);
15846 if (!InitReversed.isUsable())
15847 return StmtError();
15848 SemaRef.AddInitializerToDecl(dcl: ReversedIVDecl, init: Minus.get(),
15849 /*DirectInit=*/false);
15850
15851 // The new loop body.
15852 SmallVector<Stmt *, 4> BodyStmts;
15853 BodyStmts.reserve(N: LoopHelper.Updates.size() + 2 +
15854 (isa<CXXForRangeStmt>(Val: LoopStmt) ? 1 : 0));
15855 BodyStmts.push_back(Elt: InitReversed.get());
15856 llvm::append_range(C&: BodyStmts, R&: LoopHelper.Updates);
15857 if (auto *CXXRangeFor = dyn_cast<CXXForRangeStmt>(Val: LoopStmt))
15858 BodyStmts.push_back(Elt: CXXRangeFor->getLoopVarStmt());
15859 BodyStmts.push_back(Elt: Body);
15860 auto *ReversedBody =
15861 CompoundStmt::Create(C: Context, Stmts: BodyStmts, FPFeatures: FPOptionsOverride(),
15862 LB: Body->getBeginLoc(), RB: Body->getEndLoc());
15863
15864 // Finally create the reversed For-statement.
15865 auto *ReversedFor = new (Context)
15866 ForStmt(Context, Init.get(), Cond.get(), nullptr, Incr.get(),
15867 ReversedBody, LoopHelper.Init->getBeginLoc(),
15868 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
15869 return OMPReverseDirective::Create(C: Context, StartLoc, EndLoc, AssociatedStmt: AStmt, NumLoops,
15870 TransformedStmt: ReversedFor,
15871 PreInits: buildPreInits(Context, PreInits));
15872}
15873
15874StmtResult SemaOpenMP::ActOnOpenMPInterchangeDirective(
15875 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
15876 SourceLocation EndLoc) {
15877 ASTContext &Context = getASTContext();
15878 DeclContext *CurContext = SemaRef.CurContext;
15879 Scope *CurScope = SemaRef.getCurScope();
15880
15881 // Empty statement should only be possible if there already was an error.
15882 if (!AStmt)
15883 return StmtError();
15884
15885 // interchange without permutation clause swaps two loops.
15886 const OMPPermutationClause *PermutationClause =
15887 OMPExecutableDirective::getSingleClause<OMPPermutationClause>(Clauses);
15888 size_t NumLoops = PermutationClause ? PermutationClause->getNumLoops() : 2;
15889
15890 // Verify and diagnose loop nest.
15891 SmallVector<OMPLoopBasedDirective::HelperExprs, 4> LoopHelpers(NumLoops);
15892 Stmt *Body = nullptr;
15893 SmallVector<SmallVector<Stmt *>, 2> OriginalInits;
15894 if (!checkTransformableLoopNest(Kind: OMPD_interchange, AStmt, NumLoops,
15895 LoopHelpers, Body, OriginalInits))
15896 return StmtError();
15897
15898 // Delay interchange to when template is completely instantiated.
15899 if (CurContext->isDependentContext())
15900 return OMPInterchangeDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
15901 NumLoops, AssociatedStmt: AStmt, TransformedStmt: nullptr, PreInits: nullptr);
15902
15903 // An invalid expression in the permutation clause is set to nullptr in
15904 // ActOnOpenMPPermutationClause.
15905 if (PermutationClause &&
15906 llvm::is_contained(Range: PermutationClause->getArgsRefs(), Element: nullptr))
15907 return StmtError();
15908
15909 assert(LoopHelpers.size() == NumLoops &&
15910 "Expecting loop iteration space dimensionaly to match number of "
15911 "affected loops");
15912 assert(OriginalInits.size() == NumLoops &&
15913 "Expecting loop iteration space dimensionaly to match number of "
15914 "affected loops");
15915
15916 // Decode the permutation clause.
15917 SmallVector<uint64_t, 2> Permutation;
15918 if (!PermutationClause) {
15919 Permutation = {1, 0};
15920 } else {
15921 ArrayRef<Expr *> PermArgs = PermutationClause->getArgsRefs();
15922 llvm::BitVector Flags(PermArgs.size());
15923 for (Expr *PermArg : PermArgs) {
15924 std::optional<llvm::APSInt> PermCstExpr =
15925 PermArg->getIntegerConstantExpr(Ctx: Context);
15926 if (!PermCstExpr)
15927 continue;
15928 uint64_t PermInt = PermCstExpr->getZExtValue();
15929 assert(1 <= PermInt && PermInt <= NumLoops &&
15930 "Must be a permutation; diagnostic emitted in "
15931 "ActOnOpenMPPermutationClause");
15932 if (Flags[PermInt - 1]) {
15933 SourceRange ExprRange(PermArg->getBeginLoc(), PermArg->getEndLoc());
15934 Diag(Loc: PermArg->getExprLoc(),
15935 DiagID: diag::err_omp_interchange_permutation_value_repeated)
15936 << PermInt << ExprRange;
15937 continue;
15938 }
15939 Flags[PermInt - 1] = true;
15940
15941 Permutation.push_back(Elt: PermInt - 1);
15942 }
15943
15944 if (Permutation.size() != NumLoops)
15945 return StmtError();
15946 }
15947
15948 // Nothing to transform with trivial permutation.
15949 if (NumLoops <= 1 || llvm::all_of(Range: llvm::enumerate(First&: Permutation), P: [](auto P) {
15950 auto [Idx, Arg] = P;
15951 return Idx == Arg;
15952 }))
15953 return OMPInterchangeDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
15954 NumLoops, AssociatedStmt: AStmt, TransformedStmt: AStmt, PreInits: nullptr);
15955
15956 // Find the affected loops.
15957 SmallVector<Stmt *> LoopStmts(NumLoops, nullptr);
15958 collectLoopStmts(AStmt, LoopStmts);
15959
15960 // Collect pre-init statements on the order before the permuation.
15961 SmallVector<Stmt *> PreInits;
15962 for (auto I : llvm::seq<int>(Size: NumLoops)) {
15963 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
15964
15965 assert(LoopHelper.Counters.size() == 1 &&
15966 "Single-dimensional loop iteration space expected");
15967
15968 addLoopPreInits(Context, LoopHelper, LoopStmt: LoopStmts[I], OriginalInit: OriginalInits[I],
15969 PreInits);
15970 }
15971
15972 SmallVector<VarDecl *> PermutedIndVars(NumLoops);
15973 CaptureVars CopyTransformer(SemaRef);
15974
15975 // Create the permuted loops from the inside to the outside of the
15976 // interchanged loop nest. Body of the innermost new loop is the original
15977 // innermost body.
15978 Stmt *Inner = Body;
15979 for (auto TargetIdx : llvm::reverse(C: llvm::seq<int>(Size: NumLoops))) {
15980 // Get the original loop that belongs to this new position.
15981 uint64_t SourceIdx = Permutation[TargetIdx];
15982 OMPLoopBasedDirective::HelperExprs &SourceHelper = LoopHelpers[SourceIdx];
15983 Stmt *SourceLoopStmt = LoopStmts[SourceIdx];
15984 assert(SourceHelper.Counters.size() == 1 &&
15985 "Single-dimensional loop iteration space expected");
15986 auto *OrigCntVar = cast<DeclRefExpr>(Val: SourceHelper.Counters.front());
15987
15988 // Normalized loop counter variable: From 0 to n-1, always an integer type.
15989 DeclRefExpr *IterVarRef = cast<DeclRefExpr>(Val: SourceHelper.IterationVarRef);
15990 QualType IVTy = IterVarRef->getType();
15991 assert(IVTy->isIntegerType() &&
15992 "Expected the logical iteration counter to be an integer");
15993
15994 std::string OrigVarName = OrigCntVar->getNameInfo().getAsString();
15995 SourceLocation OrigVarLoc = IterVarRef->getExprLoc();
15996
15997 // Make a copy of the NumIterations expression for each use: By the AST
15998 // constraints, every expression object in a DeclContext must be unique.
15999 auto MakeNumIterations = [&CopyTransformer, &SourceHelper]() -> Expr * {
16000 return AssertSuccess(
16001 R: CopyTransformer.TransformExpr(E: SourceHelper.NumIterations));
16002 };
16003
16004 // Iteration variable for the permuted loop. Reuse the one from
16005 // checkOpenMPLoop which will also be used to update the original loop
16006 // variable.
16007 SmallString<64> PermutedCntName(".permuted_");
16008 PermutedCntName.append(Refs: {llvm::utostr(X: TargetIdx), ".iv.", OrigVarName});
16009 auto *PermutedCntDecl = cast<VarDecl>(Val: IterVarRef->getDecl());
16010 PermutedCntDecl->setDeclName(
16011 &SemaRef.PP.getIdentifierTable().get(Name: PermutedCntName));
16012 PermutedIndVars[TargetIdx] = PermutedCntDecl;
16013 auto MakePermutedRef = [this, PermutedCntDecl, IVTy, OrigVarLoc]() {
16014 return buildDeclRefExpr(S&: SemaRef, D: PermutedCntDecl, Ty: IVTy, Loc: OrigVarLoc);
16015 };
16016
16017 // For init-statement:
16018 // \code
16019 // auto .permuted_{target}.iv = 0
16020 // \endcode
16021 ExprResult Zero = SemaRef.ActOnIntegerConstant(Loc: OrigVarLoc, Val: 0);
16022 if (!Zero.isUsable())
16023 return StmtError();
16024 SemaRef.AddInitializerToDecl(dcl: PermutedCntDecl, init: Zero.get(),
16025 /*DirectInit=*/false);
16026 StmtResult InitStmt = new (Context)
16027 DeclStmt(DeclGroupRef(PermutedCntDecl), OrigCntVar->getBeginLoc(),
16028 OrigCntVar->getEndLoc());
16029 if (!InitStmt.isUsable())
16030 return StmtError();
16031
16032 // For cond-expression:
16033 // \code
16034 // .permuted_{target}.iv < MakeNumIterations()
16035 // \endcode
16036 ExprResult CondExpr =
16037 SemaRef.BuildBinOp(S: CurScope, OpLoc: SourceHelper.Cond->getExprLoc(), Opc: BO_LT,
16038 LHSExpr: MakePermutedRef(), RHSExpr: MakeNumIterations());
16039 if (!CondExpr.isUsable())
16040 return StmtError();
16041
16042 // For incr-statement:
16043 // \code
16044 // ++.tile.iv
16045 // \endcode
16046 ExprResult IncrStmt = SemaRef.BuildUnaryOp(
16047 S: CurScope, OpLoc: SourceHelper.Inc->getExprLoc(), Opc: UO_PreInc, Input: MakePermutedRef());
16048 if (!IncrStmt.isUsable())
16049 return StmtError();
16050
16051 SmallVector<Stmt *, 4> BodyParts(SourceHelper.Updates.begin(),
16052 SourceHelper.Updates.end());
16053 if (auto *SourceCXXFor = dyn_cast<CXXForRangeStmt>(Val: SourceLoopStmt))
16054 BodyParts.push_back(Elt: SourceCXXFor->getLoopVarStmt());
16055 BodyParts.push_back(Elt: Inner);
16056 Inner = CompoundStmt::Create(C: Context, Stmts: BodyParts, FPFeatures: FPOptionsOverride(),
16057 LB: Inner->getBeginLoc(), RB: Inner->getEndLoc());
16058 Inner = new (Context) ForStmt(
16059 Context, InitStmt.get(), CondExpr.get(), nullptr, IncrStmt.get(), Inner,
16060 SourceHelper.Init->getBeginLoc(), SourceHelper.Init->getBeginLoc(),
16061 SourceHelper.Inc->getEndLoc());
16062 }
16063
16064 return OMPInterchangeDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
16065 NumLoops, AssociatedStmt: AStmt, TransformedStmt: Inner,
16066 PreInits: buildPreInits(Context, PreInits));
16067}
16068
16069StmtResult SemaOpenMP::ActOnOpenMPFuseDirective(ArrayRef<OMPClause *> Clauses,
16070 Stmt *AStmt,
16071 SourceLocation StartLoc,
16072 SourceLocation EndLoc) {
16073
16074 ASTContext &Context = getASTContext();
16075 DeclContext *CurrContext = SemaRef.CurContext;
16076 Scope *CurScope = SemaRef.getCurScope();
16077 CaptureVars CopyTransformer(SemaRef);
16078
16079 // Ensure the structured block is not empty
16080 if (!AStmt)
16081 return StmtError();
16082
16083 // Defer transformation in dependent contexts
16084 // The NumLoopNests argument is set to a placeholder 1 (even though
16085 // using looprange fuse could yield up to 3 top level loop nests)
16086 // because a dependent context could prevent determining its true value
16087 if (CurrContext->isDependentContext())
16088 return OMPFuseDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
16089 /* NumLoops */ NumGeneratedTopLevelLoops: 1, AssociatedStmt: AStmt, TransformedStmt: nullptr, PreInits: nullptr);
16090
16091 // Validate that the potential loop sequence is transformable for fusion
16092 // Also collect the HelperExprs, Loop Stmts, Inits, and Number of loops
16093 LoopSequenceAnalysis SeqAnalysis;
16094 if (!checkTransformableLoopSequence(Kind: OMPD_fuse, AStmt, SeqAnalysis, Context))
16095 return StmtError();
16096
16097 // SeqAnalysis.LoopSeqSize exists mostly to handle dependent contexts,
16098 // otherwise it must be the same as SeqAnalysis.Loops.size().
16099 assert(SeqAnalysis.LoopSeqSize == SeqAnalysis.Loops.size() &&
16100 "Inconsistent size of the loop sequence and the number of loops "
16101 "found in the sequence");
16102
16103 // Handle clauses, which can be any of the following: [looprange, apply]
16104 const auto *LRC =
16105 OMPExecutableDirective::getSingleClause<OMPLoopRangeClause>(Clauses);
16106
16107 // The clause arguments are invalidated if any error arises
16108 // such as non-constant or non-positive arguments
16109 if (LRC && (!LRC->getFirst() || !LRC->getCount()))
16110 return StmtError();
16111
16112 // Delayed semantic check of LoopRange constraint
16113 // Evaluates the loop range arguments and returns the first and count values
16114 auto EvaluateLoopRangeArguments = [&Context](Expr *First, Expr *Count,
16115 uint64_t &FirstVal,
16116 uint64_t &CountVal) {
16117 llvm::APSInt FirstInt = First->EvaluateKnownConstInt(Ctx: Context);
16118 llvm::APSInt CountInt = Count->EvaluateKnownConstInt(Ctx: Context);
16119 FirstVal = FirstInt.getZExtValue();
16120 CountVal = CountInt.getZExtValue();
16121 };
16122
16123 // OpenMP [6.0, Restrictions]
16124 // first + count - 1 must not evaluate to a value greater than the
16125 // loop sequence length of the associated canonical loop sequence.
16126 auto ValidLoopRange = [](uint64_t FirstVal, uint64_t CountVal,
16127 unsigned NumLoops) -> bool {
16128 return FirstVal + CountVal - 1 <= NumLoops;
16129 };
16130 uint64_t FirstVal = 1, CountVal = 0, LastVal = SeqAnalysis.LoopSeqSize;
16131
16132 // Validates the loop range after evaluating the semantic information
16133 // and ensures that the range is valid for the given loop sequence size.
16134 // Expressions are evaluated at compile time to obtain constant values.
16135 if (LRC) {
16136 EvaluateLoopRangeArguments(LRC->getFirst(), LRC->getCount(), FirstVal,
16137 CountVal);
16138 if (CountVal == 1)
16139 SemaRef.Diag(Loc: LRC->getCountLoc(), DiagID: diag::warn_omp_redundant_fusion)
16140 << getOpenMPDirectiveName(D: OMPD_fuse);
16141
16142 if (!ValidLoopRange(FirstVal, CountVal, SeqAnalysis.LoopSeqSize)) {
16143 SemaRef.Diag(Loc: LRC->getFirstLoc(), DiagID: diag::err_omp_invalid_looprange)
16144 << getOpenMPDirectiveName(D: OMPD_fuse) << FirstVal
16145 << (FirstVal + CountVal - 1) << SeqAnalysis.LoopSeqSize;
16146 return StmtError();
16147 }
16148
16149 LastVal = FirstVal + CountVal - 1;
16150 }
16151
16152 // Complete fusion generates a single canonical loop nest
16153 // However looprange clause may generate several loop nests
16154 unsigned NumGeneratedTopLevelLoops =
16155 LRC ? SeqAnalysis.LoopSeqSize - CountVal + 1 : 1;
16156
16157 // Emit a warning for redundant loop fusion when the sequence contains only
16158 // one loop.
16159 if (SeqAnalysis.LoopSeqSize == 1)
16160 SemaRef.Diag(Loc: AStmt->getBeginLoc(), DiagID: diag::warn_omp_redundant_fusion)
16161 << getOpenMPDirectiveName(D: OMPD_fuse);
16162
16163 // Select the type with the largest bit width among all induction variables
16164 QualType IVType =
16165 SeqAnalysis.Loops[FirstVal - 1].HelperExprs.IterationVarRef->getType();
16166 for (unsigned I : llvm::seq<unsigned>(Begin: FirstVal, End: LastVal)) {
16167 QualType CurrentIVType =
16168 SeqAnalysis.Loops[I].HelperExprs.IterationVarRef->getType();
16169 if (Context.getTypeSize(T: CurrentIVType) > Context.getTypeSize(T: IVType)) {
16170 IVType = CurrentIVType;
16171 }
16172 }
16173 uint64_t IVBitWidth = Context.getIntWidth(T: IVType);
16174
16175 // Create pre-init declarations for all loops lower bounds, upper bounds,
16176 // strides and num-iterations for every top level loop in the fusion
16177 SmallVector<VarDecl *, 4> LBVarDecls;
16178 SmallVector<VarDecl *, 4> STVarDecls;
16179 SmallVector<VarDecl *, 4> NIVarDecls;
16180 SmallVector<VarDecl *, 4> UBVarDecls;
16181 SmallVector<VarDecl *, 4> IVVarDecls;
16182
16183 // Helper lambda to create variables for bounds, strides, and other
16184 // expressions. Generates both the variable declaration and the corresponding
16185 // initialization statement.
16186 auto CreateHelperVarAndStmt =
16187 [&, &SemaRef = SemaRef](Expr *ExprToCopy, const std::string &BaseName,
16188 unsigned I, bool NeedsNewVD = false) {
16189 Expr *TransformedExpr =
16190 AssertSuccess(R: CopyTransformer.TransformExpr(E: ExprToCopy));
16191 if (!TransformedExpr)
16192 return std::pair<VarDecl *, StmtResult>(nullptr, StmtError());
16193
16194 auto Name = (Twine(".omp.") + BaseName + std::to_string(val: I)).str();
16195
16196 VarDecl *VD;
16197 if (NeedsNewVD) {
16198 VD = buildVarDecl(SemaRef, Loc: SourceLocation(), Type: IVType, Name);
16199 SemaRef.AddInitializerToDecl(dcl: VD, init: TransformedExpr, DirectInit: false);
16200 } else {
16201 // Create a unique variable name
16202 DeclRefExpr *DRE = cast<DeclRefExpr>(Val: TransformedExpr);
16203 VD = cast<VarDecl>(Val: DRE->getDecl());
16204 VD->setDeclName(&SemaRef.PP.getIdentifierTable().get(Name));
16205 }
16206 // Create the corresponding declaration statement
16207 StmtResult DeclStmt = new (Context) class DeclStmt(
16208 DeclGroupRef(VD), SourceLocation(), SourceLocation());
16209 return std::make_pair(x&: VD, y&: DeclStmt);
16210 };
16211
16212 // PreInits hold a sequence of variable declarations that must be executed
16213 // before the fused loop begins. These include bounds, strides, and other
16214 // helper variables required for the transformation. Other loop transforms
16215 // also contain their own preinits
16216 SmallVector<Stmt *> PreInits;
16217
16218 // Update the general preinits using the preinits generated by loop sequence
16219 // generating loop transformations. These preinits differ slightly from
16220 // single-loop transformation preinits, as they can be detached from a
16221 // specific loop inside multiple generated loop nests. This happens
16222 // because certain helper variables, like '.omp.fuse.max', are introduced to
16223 // handle fused iteration spaces and may not be directly tied to a single
16224 // original loop. The preinit structure must ensure that hidden variables
16225 // like '.omp.fuse.max' are still properly handled.
16226 // Transformations that apply this concept: Loopranged Fuse, Split
16227 llvm::append_range(C&: PreInits, R&: SeqAnalysis.LoopSequencePreInits);
16228
16229 // Process each single loop to generate and collect declarations
16230 // and statements for all helper expressions related to
16231 // particular single loop nests
16232
16233 // Also In the case of the fused loops, we keep track of their original
16234 // inits by appending them to their preinits statement, and in the case of
16235 // transformations, also append their preinits (which contain the original
16236 // loop initialization statement or other statements)
16237
16238 // Firstly we need to set TransformIndex to match the begining of the
16239 // looprange section
16240 unsigned int TransformIndex = 0;
16241 for (unsigned I : llvm::seq<unsigned>(Size: FirstVal - 1)) {
16242 if (SeqAnalysis.Loops[I].isLoopTransformation())
16243 ++TransformIndex;
16244 }
16245
16246 for (unsigned int I = FirstVal - 1, J = 0; I < LastVal; ++I, ++J) {
16247 if (SeqAnalysis.Loops[I].isRegularLoop()) {
16248 addLoopPreInits(Context, LoopHelper&: SeqAnalysis.Loops[I].HelperExprs,
16249 LoopStmt: SeqAnalysis.Loops[I].TheForStmt,
16250 OriginalInit: SeqAnalysis.Loops[I].OriginalInits, PreInits);
16251 } else if (SeqAnalysis.Loops[I].isLoopTransformation()) {
16252 // For transformed loops, insert both pre-inits and original inits.
16253 // Order matters: pre-inits may define variables used in the original
16254 // inits such as upper bounds...
16255 SmallVector<Stmt *> &TransformPreInit =
16256 SeqAnalysis.Loops[TransformIndex++].TransformsPreInits;
16257 llvm::append_range(C&: PreInits, R&: TransformPreInit);
16258
16259 addLoopPreInits(Context, LoopHelper&: SeqAnalysis.Loops[I].HelperExprs,
16260 LoopStmt: SeqAnalysis.Loops[I].TheForStmt,
16261 OriginalInit: SeqAnalysis.Loops[I].OriginalInits, PreInits);
16262 }
16263 auto [UBVD, UBDStmt] =
16264 CreateHelperVarAndStmt(SeqAnalysis.Loops[I].HelperExprs.UB, "ub", J);
16265 auto [LBVD, LBDStmt] =
16266 CreateHelperVarAndStmt(SeqAnalysis.Loops[I].HelperExprs.LB, "lb", J);
16267 auto [STVD, STDStmt] =
16268 CreateHelperVarAndStmt(SeqAnalysis.Loops[I].HelperExprs.ST, "st", J);
16269 auto [NIVD, NIDStmt] = CreateHelperVarAndStmt(
16270 SeqAnalysis.Loops[I].HelperExprs.NumIterations, "ni", J, true);
16271 auto [IVVD, IVDStmt] = CreateHelperVarAndStmt(
16272 SeqAnalysis.Loops[I].HelperExprs.IterationVarRef, "iv", J);
16273
16274 assert(LBVD && STVD && NIVD && IVVD &&
16275 "OpenMP Fuse Helper variables creation failed");
16276
16277 UBVarDecls.push_back(Elt: UBVD);
16278 LBVarDecls.push_back(Elt: LBVD);
16279 STVarDecls.push_back(Elt: STVD);
16280 NIVarDecls.push_back(Elt: NIVD);
16281 IVVarDecls.push_back(Elt: IVVD);
16282
16283 PreInits.push_back(Elt: LBDStmt.get());
16284 PreInits.push_back(Elt: STDStmt.get());
16285 PreInits.push_back(Elt: NIDStmt.get());
16286 PreInits.push_back(Elt: IVDStmt.get());
16287 }
16288
16289 auto MakeVarDeclRef = [&SemaRef = this->SemaRef](VarDecl *VD) {
16290 return buildDeclRefExpr(S&: SemaRef, D: VD, Ty: VD->getType(), Loc: VD->getLocation(),
16291 RefersToCapture: false);
16292 };
16293
16294 // Following up the creation of the final fused loop will be performed
16295 // which has the following shape (considering the selected loops):
16296 //
16297 // for (fuse.index = 0; fuse.index < max(ni0, ni1..., nik); ++fuse.index) {
16298 // if (fuse.index < ni0){
16299 // iv0 = lb0 + st0 * fuse.index;
16300 // original.index0 = iv0
16301 // body(0);
16302 // }
16303 // if (fuse.index < ni1){
16304 // iv1 = lb1 + st1 * fuse.index;
16305 // original.index1 = iv1
16306 // body(1);
16307 // }
16308 //
16309 // ...
16310 //
16311 // if (fuse.index < nik){
16312 // ivk = lbk + stk * fuse.index;
16313 // original.indexk = ivk
16314 // body(k); Expr *InitVal = IntegerLiteral::Create(Context,
16315 // llvm::APInt(IVWidth, 0),
16316 // }
16317
16318 // 1. Create the initialized fuse index
16319 StringRef IndexName = ".omp.fuse.index";
16320 Expr *InitVal = IntegerLiteral::Create(C: Context, V: llvm::APInt(IVBitWidth, 0),
16321 type: IVType, l: SourceLocation());
16322 VarDecl *IndexDecl =
16323 buildVarDecl(SemaRef, Loc: {}, Type: IVType, Name: IndexName, Attrs: nullptr, OrigRef: nullptr);
16324 SemaRef.AddInitializerToDecl(dcl: IndexDecl, init: InitVal, DirectInit: false);
16325 StmtResult InitStmt = new (Context)
16326 DeclStmt(DeclGroupRef(IndexDecl), SourceLocation(), SourceLocation());
16327
16328 if (!InitStmt.isUsable())
16329 return StmtError();
16330
16331 auto MakeIVRef = [&SemaRef = this->SemaRef, IndexDecl, IVType,
16332 Loc = InitVal->getExprLoc()]() {
16333 return buildDeclRefExpr(S&: SemaRef, D: IndexDecl, Ty: IVType, Loc, RefersToCapture: false);
16334 };
16335
16336 // 2. Iteratively compute the max number of logical iterations Max(NI_1, NI_2,
16337 // ..., NI_k)
16338 //
16339 // This loop accumulates the maximum value across multiple expressions,
16340 // ensuring each step constructs a unique AST node for correctness. By using
16341 // intermediate temporary variables and conditional operators, we maintain
16342 // distinct nodes and avoid duplicating subtrees, For instance, max(a,b,c):
16343 // omp.temp0 = max(a, b)
16344 // omp.temp1 = max(omp.temp0, c)
16345 // omp.fuse.max = max(omp.temp1, omp.temp0)
16346
16347 ExprResult MaxExpr;
16348 // I is the range of loops in the sequence that we fuse.
16349 for (unsigned I = FirstVal - 1, J = 0; I < LastVal; ++I, ++J) {
16350 DeclRefExpr *NIRef = MakeVarDeclRef(NIVarDecls[J]);
16351 QualType NITy = NIRef->getType();
16352
16353 if (MaxExpr.isUnset()) {
16354 // Initialize MaxExpr with the first NI expression
16355 MaxExpr = NIRef;
16356 } else {
16357 // Create a new acummulator variable t_i = MaxExpr
16358 std::string TempName = (Twine(".omp.temp.") + Twine(J)).str();
16359 VarDecl *TempDecl =
16360 buildVarDecl(SemaRef, Loc: {}, Type: NITy, Name: TempName, Attrs: nullptr, OrigRef: nullptr);
16361 TempDecl->setInit(MaxExpr.get());
16362 DeclRefExpr *TempRef =
16363 buildDeclRefExpr(S&: SemaRef, D: TempDecl, Ty: NITy, Loc: SourceLocation(), RefersToCapture: false);
16364 DeclRefExpr *TempRef2 =
16365 buildDeclRefExpr(S&: SemaRef, D: TempDecl, Ty: NITy, Loc: SourceLocation(), RefersToCapture: false);
16366 // Add a DeclStmt to PreInits to ensure the variable is declared.
16367 StmtResult TempStmt = new (Context)
16368 DeclStmt(DeclGroupRef(TempDecl), SourceLocation(), SourceLocation());
16369
16370 if (!TempStmt.isUsable())
16371 return StmtError();
16372 PreInits.push_back(Elt: TempStmt.get());
16373
16374 // Build MaxExpr <-(MaxExpr > NIRef ? MaxExpr : NIRef)
16375 ExprResult Comparison =
16376 SemaRef.BuildBinOp(S: nullptr, OpLoc: SourceLocation(), Opc: BO_GT, LHSExpr: TempRef, RHSExpr: NIRef);
16377 // Handle any errors in Comparison creation
16378 if (!Comparison.isUsable())
16379 return StmtError();
16380
16381 DeclRefExpr *NIRef2 = MakeVarDeclRef(NIVarDecls[J]);
16382 // Update MaxExpr using a conditional expression to hold the max value
16383 MaxExpr = new (Context) ConditionalOperator(
16384 Comparison.get(), SourceLocation(), TempRef2, SourceLocation(),
16385 NIRef2->getExprStmt(), NITy, VK_LValue, OK_Ordinary);
16386
16387 if (!MaxExpr.isUsable())
16388 return StmtError();
16389 }
16390 }
16391 if (!MaxExpr.isUsable())
16392 return StmtError();
16393
16394 // 3. Declare the max variable
16395 const std::string MaxName = Twine(".omp.fuse.max").str();
16396 VarDecl *MaxDecl =
16397 buildVarDecl(SemaRef, Loc: {}, Type: IVType, Name: MaxName, Attrs: nullptr, OrigRef: nullptr);
16398 MaxDecl->setInit(MaxExpr.get());
16399 DeclRefExpr *MaxRef = buildDeclRefExpr(S&: SemaRef, D: MaxDecl, Ty: IVType, Loc: {}, RefersToCapture: false);
16400 StmtResult MaxStmt = new (Context)
16401 DeclStmt(DeclGroupRef(MaxDecl), SourceLocation(), SourceLocation());
16402
16403 if (MaxStmt.isInvalid())
16404 return StmtError();
16405 PreInits.push_back(Elt: MaxStmt.get());
16406
16407 // 4. Create condition Expr: index < n_max
16408 ExprResult CondExpr = SemaRef.BuildBinOp(S: CurScope, OpLoc: SourceLocation(), Opc: BO_LT,
16409 LHSExpr: MakeIVRef(), RHSExpr: MaxRef);
16410 if (!CondExpr.isUsable())
16411 return StmtError();
16412
16413 // 5. Increment Expr: ++index
16414 ExprResult IncrExpr =
16415 SemaRef.BuildUnaryOp(S: CurScope, OpLoc: SourceLocation(), Opc: UO_PreInc, Input: MakeIVRef());
16416 if (!IncrExpr.isUsable())
16417 return StmtError();
16418
16419 // 6. Build the Fused Loop Body
16420 // The final fused loop iterates over the maximum logical range. Inside the
16421 // loop, each original loop's index is calculated dynamically, and its body
16422 // is executed conditionally.
16423 //
16424 // Each sub-loop's body is guarded by a conditional statement to ensure
16425 // it executes only within its logical iteration range:
16426 //
16427 // if (fuse.index < ni_k){
16428 // iv_k = lb_k + st_k * fuse.index;
16429 // original.index = iv_k
16430 // body(k);
16431 // }
16432
16433 CompoundStmt *FusedBody = nullptr;
16434 SmallVector<Stmt *, 4> FusedBodyStmts;
16435 for (unsigned I = FirstVal - 1, J = 0; I < LastVal; ++I, ++J) {
16436 // Assingment of the original sub-loop index to compute the logical index
16437 // IV_k = LB_k + omp.fuse.index * ST_k
16438 ExprResult IdxExpr =
16439 SemaRef.BuildBinOp(S: CurScope, OpLoc: SourceLocation(), Opc: BO_Mul,
16440 LHSExpr: MakeVarDeclRef(STVarDecls[J]), RHSExpr: MakeIVRef());
16441 if (!IdxExpr.isUsable())
16442 return StmtError();
16443 IdxExpr = SemaRef.BuildBinOp(S: CurScope, OpLoc: SourceLocation(), Opc: BO_Add,
16444 LHSExpr: MakeVarDeclRef(LBVarDecls[J]), RHSExpr: IdxExpr.get());
16445
16446 if (!IdxExpr.isUsable())
16447 return StmtError();
16448 IdxExpr = SemaRef.BuildBinOp(S: CurScope, OpLoc: SourceLocation(), Opc: BO_Assign,
16449 LHSExpr: MakeVarDeclRef(IVVarDecls[J]), RHSExpr: IdxExpr.get());
16450 if (!IdxExpr.isUsable())
16451 return StmtError();
16452
16453 // Update the original i_k = IV_k
16454 SmallVector<Stmt *, 4> BodyStmts;
16455 BodyStmts.push_back(Elt: IdxExpr.get());
16456 llvm::append_range(C&: BodyStmts, R&: SeqAnalysis.Loops[I].HelperExprs.Updates);
16457
16458 // If the loop is a CXXForRangeStmt then the iterator variable is needed
16459 if (auto *SourceCXXFor =
16460 dyn_cast<CXXForRangeStmt>(Val: SeqAnalysis.Loops[I].TheForStmt))
16461 BodyStmts.push_back(Elt: SourceCXXFor->getLoopVarStmt());
16462
16463 Stmt *Body =
16464 (isa<ForStmt>(Val: SeqAnalysis.Loops[I].TheForStmt))
16465 ? cast<ForStmt>(Val: SeqAnalysis.Loops[I].TheForStmt)->getBody()
16466 : cast<CXXForRangeStmt>(Val: SeqAnalysis.Loops[I].TheForStmt)->getBody();
16467 BodyStmts.push_back(Elt: Body);
16468
16469 CompoundStmt *CombinedBody =
16470 CompoundStmt::Create(C: Context, Stmts: BodyStmts, FPFeatures: FPOptionsOverride(),
16471 LB: SourceLocation(), RB: SourceLocation());
16472 ExprResult Condition =
16473 SemaRef.BuildBinOp(S: CurScope, OpLoc: SourceLocation(), Opc: BO_LT, LHSExpr: MakeIVRef(),
16474 RHSExpr: MakeVarDeclRef(NIVarDecls[J]));
16475
16476 if (!Condition.isUsable())
16477 return StmtError();
16478
16479 IfStmt *IfStatement = IfStmt::Create(
16480 Ctx: Context, IL: SourceLocation(), Kind: IfStatementKind::Ordinary, Init: nullptr, Var: nullptr,
16481 Cond: Condition.get(), LPL: SourceLocation(), RPL: SourceLocation(), Then: CombinedBody,
16482 EL: SourceLocation(), Else: nullptr);
16483
16484 FusedBodyStmts.push_back(Elt: IfStatement);
16485 }
16486 FusedBody = CompoundStmt::Create(C: Context, Stmts: FusedBodyStmts, FPFeatures: FPOptionsOverride(),
16487 LB: SourceLocation(), RB: SourceLocation());
16488
16489 // 7. Construct the final fused loop
16490 ForStmt *FusedForStmt = new (Context)
16491 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr, IncrExpr.get(),
16492 FusedBody, InitStmt.get()->getBeginLoc(), SourceLocation(),
16493 IncrExpr.get()->getEndLoc());
16494
16495 // In the case of looprange, the result of fuse won't simply
16496 // be a single loop (ForStmt), but rather a loop sequence
16497 // (CompoundStmt) of 3 parts: the pre-fusion loops, the fused loop
16498 // and the post-fusion loops, preserving its original order.
16499 //
16500 // Note: If looprange clause produces a single fused loop nest then
16501 // this compound statement wrapper is unnecessary (Therefore this
16502 // treatment is skipped)
16503
16504 Stmt *FusionStmt = FusedForStmt;
16505 if (LRC && CountVal != SeqAnalysis.LoopSeqSize) {
16506 SmallVector<Stmt *, 4> FinalLoops;
16507
16508 // Reset the transform index
16509 TransformIndex = 0;
16510
16511 // Collect all non-fused loops before and after the fused region.
16512 // Pre-fusion and post-fusion loops are inserted in order exploiting their
16513 // symmetry, along with their corresponding transformation pre-inits if
16514 // needed. The fused loop is added between the two regions.
16515 for (unsigned I : llvm::seq<unsigned>(Size: SeqAnalysis.LoopSeqSize)) {
16516 if (I >= FirstVal - 1 && I < FirstVal + CountVal - 1) {
16517 // Update the Transformation counter to skip already treated
16518 // loop transformations
16519 if (!SeqAnalysis.Loops[I].isLoopTransformation())
16520 ++TransformIndex;
16521 continue;
16522 }
16523
16524 // No need to handle:
16525 // Regular loops: they are kept intact as-is.
16526 // Loop-sequence-generating transformations: already handled earlier.
16527 // Only TransformSingleLoop requires inserting pre-inits here
16528 if (SeqAnalysis.Loops[I].isRegularLoop()) {
16529 const auto &TransformPreInit =
16530 SeqAnalysis.Loops[TransformIndex++].TransformsPreInits;
16531 if (!TransformPreInit.empty())
16532 llvm::append_range(C&: PreInits, R: TransformPreInit);
16533 }
16534
16535 FinalLoops.push_back(Elt: SeqAnalysis.Loops[I].TheForStmt);
16536 }
16537
16538 FinalLoops.insert(I: FinalLoops.begin() + (FirstVal - 1), Elt: FusedForStmt);
16539 FusionStmt = CompoundStmt::Create(C: Context, Stmts: FinalLoops, FPFeatures: FPOptionsOverride(),
16540 LB: SourceLocation(), RB: SourceLocation());
16541 }
16542 return OMPFuseDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
16543 NumGeneratedTopLevelLoops, AssociatedStmt: AStmt, TransformedStmt: FusionStmt,
16544 PreInits: buildPreInits(Context, PreInits));
16545}
16546
16547OMPClause *SemaOpenMP::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind,
16548 Expr *Expr,
16549 SourceLocation StartLoc,
16550 SourceLocation LParenLoc,
16551 SourceLocation EndLoc) {
16552 OMPClause *Res = nullptr;
16553 switch (Kind) {
16554 case OMPC_final:
16555 Res = ActOnOpenMPFinalClause(Condition: Expr, StartLoc, LParenLoc, EndLoc);
16556 break;
16557 case OMPC_safelen:
16558 Res = ActOnOpenMPSafelenClause(Length: Expr, StartLoc, LParenLoc, EndLoc);
16559 break;
16560 case OMPC_simdlen:
16561 Res = ActOnOpenMPSimdlenClause(Length: Expr, StartLoc, LParenLoc, EndLoc);
16562 break;
16563 case OMPC_allocator:
16564 Res = ActOnOpenMPAllocatorClause(Allocator: Expr, StartLoc, LParenLoc, EndLoc);
16565 break;
16566 case OMPC_collapse:
16567 Res = ActOnOpenMPCollapseClause(NumForLoops: Expr, StartLoc, LParenLoc, EndLoc);
16568 break;
16569 case OMPC_ordered:
16570 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, NumForLoops: Expr);
16571 break;
16572 case OMPC_nowait:
16573 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc, LParenLoc, Condition: Expr);
16574 break;
16575 case OMPC_priority:
16576 Res = ActOnOpenMPPriorityClause(Priority: Expr, StartLoc, LParenLoc, EndLoc);
16577 break;
16578 case OMPC_hint:
16579 Res = ActOnOpenMPHintClause(Hint: Expr, StartLoc, LParenLoc, EndLoc);
16580 break;
16581 case OMPC_depobj:
16582 Res = ActOnOpenMPDepobjClause(Depobj: Expr, StartLoc, LParenLoc, EndLoc);
16583 break;
16584 case OMPC_detach:
16585 Res = ActOnOpenMPDetachClause(Evt: Expr, StartLoc, LParenLoc, EndLoc);
16586 break;
16587 case OMPC_novariants:
16588 Res = ActOnOpenMPNovariantsClause(Condition: Expr, StartLoc, LParenLoc, EndLoc);
16589 break;
16590 case OMPC_nocontext:
16591 Res = ActOnOpenMPNocontextClause(Condition: Expr, StartLoc, LParenLoc, EndLoc);
16592 break;
16593 case OMPC_filter:
16594 Res = ActOnOpenMPFilterClause(ThreadID: Expr, StartLoc, LParenLoc, EndLoc);
16595 break;
16596 case OMPC_partial:
16597 Res = ActOnOpenMPPartialClause(FactorExpr: Expr, StartLoc, LParenLoc, EndLoc);
16598 break;
16599 case OMPC_message:
16600 Res = ActOnOpenMPMessageClause(MS: Expr, StartLoc, LParenLoc, EndLoc);
16601 break;
16602 case OMPC_align:
16603 Res = ActOnOpenMPAlignClause(Alignment: Expr, StartLoc, LParenLoc, EndLoc);
16604 break;
16605 case OMPC_ompx_dyn_cgroup_mem:
16606 Res = ActOnOpenMPXDynCGroupMemClause(Size: Expr, StartLoc, LParenLoc, EndLoc);
16607 break;
16608 case OMPC_holds:
16609 Res = ActOnOpenMPHoldsClause(E: Expr, StartLoc, LParenLoc, EndLoc);
16610 break;
16611 case OMPC_transparent:
16612 Res = ActOnOpenMPTransparentClause(Transparent: Expr, StartLoc, LParenLoc, EndLoc);
16613 break;
16614 case OMPC_dyn_groupprivate:
16615 case OMPC_grainsize:
16616 case OMPC_num_tasks:
16617 case OMPC_num_threads:
16618 case OMPC_device:
16619 case OMPC_if:
16620 case OMPC_default:
16621 case OMPC_proc_bind:
16622 case OMPC_schedule:
16623 case OMPC_private:
16624 case OMPC_firstprivate:
16625 case OMPC_lastprivate:
16626 case OMPC_shared:
16627 case OMPC_reduction:
16628 case OMPC_task_reduction:
16629 case OMPC_in_reduction:
16630 case OMPC_linear:
16631 case OMPC_aligned:
16632 case OMPC_copyin:
16633 case OMPC_copyprivate:
16634 case OMPC_untied:
16635 case OMPC_mergeable:
16636 case OMPC_threadprivate:
16637 case OMPC_groupprivate:
16638 case OMPC_sizes:
16639 case OMPC_allocate:
16640 case OMPC_flush:
16641 case OMPC_read:
16642 case OMPC_write:
16643 case OMPC_update:
16644 case OMPC_capture:
16645 case OMPC_compare:
16646 case OMPC_seq_cst:
16647 case OMPC_acq_rel:
16648 case OMPC_acquire:
16649 case OMPC_release:
16650 case OMPC_relaxed:
16651 case OMPC_depend:
16652 case OMPC_threads:
16653 case OMPC_simd:
16654 case OMPC_map:
16655 case OMPC_nogroup:
16656 case OMPC_dist_schedule:
16657 case OMPC_defaultmap:
16658 case OMPC_unknown:
16659 case OMPC_uniform:
16660 case OMPC_to:
16661 case OMPC_from:
16662 case OMPC_use_device_ptr:
16663 case OMPC_use_device_addr:
16664 case OMPC_is_device_ptr:
16665 case OMPC_unified_address:
16666 case OMPC_unified_shared_memory:
16667 case OMPC_reverse_offload:
16668 case OMPC_dynamic_allocators:
16669 case OMPC_atomic_default_mem_order:
16670 case OMPC_self_maps:
16671 case OMPC_device_type:
16672 case OMPC_match:
16673 case OMPC_nontemporal:
16674 case OMPC_order:
16675 case OMPC_at:
16676 case OMPC_severity:
16677 case OMPC_destroy:
16678 case OMPC_inclusive:
16679 case OMPC_exclusive:
16680 case OMPC_uses_allocators:
16681 case OMPC_affinity:
16682 case OMPC_when:
16683 case OMPC_bind:
16684 case OMPC_num_teams:
16685 case OMPC_thread_limit:
16686 default:
16687 llvm_unreachable("Clause is not allowed.");
16688 }
16689 return Res;
16690}
16691
16692// An OpenMP directive such as 'target parallel' has two captured regions:
16693// for the 'target' and 'parallel' respectively. This function returns
16694// the region in which to capture expressions associated with a clause.
16695// A return value of OMPD_unknown signifies that the expression should not
16696// be captured.
16697static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
16698 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, unsigned OpenMPVersion,
16699 OpenMPDirectiveKind NameModifier = OMPD_unknown) {
16700 assert(isAllowedClauseForDirective(DKind, CKind, OpenMPVersion) &&
16701 "Invalid directive with CKind-clause");
16702
16703 // Invalid modifier will be diagnosed separately, just return OMPD_unknown.
16704 if (NameModifier != OMPD_unknown &&
16705 !isAllowedClauseForDirective(D: NameModifier, C: CKind, Version: OpenMPVersion))
16706 return OMPD_unknown;
16707
16708 ArrayRef<OpenMPDirectiveKind> Leafs = getLeafConstructsOrSelf(D: DKind);
16709
16710 // [5.2:341:24-30]
16711 // If the clauses have expressions on them, such as for various clauses where
16712 // the argument of the clause is an expression, or lower-bound, length, or
16713 // stride expressions inside array sections (or subscript and stride
16714 // expressions in subscript-triplet for Fortran), or linear-step or alignment
16715 // expressions, the expressions are evaluated immediately before the construct
16716 // to which the clause has been split or duplicated per the above rules
16717 // (therefore inside of the outer leaf constructs). However, the expressions
16718 // inside the num_teams and thread_limit clauses are always evaluated before
16719 // the outermost leaf construct.
16720
16721 // Process special cases first.
16722 switch (CKind) {
16723 case OMPC_if:
16724 switch (DKind) {
16725 case OMPD_teams_loop:
16726 case OMPD_target_teams_loop:
16727 // For [target] teams loop, assume capture region is 'teams' so it's
16728 // available for codegen later to use if/when necessary.
16729 return OMPD_teams;
16730 case OMPD_target_update:
16731 case OMPD_target_enter_data:
16732 case OMPD_target_exit_data:
16733 return OMPD_task;
16734 default:
16735 break;
16736 }
16737 break;
16738 case OMPC_num_teams:
16739 case OMPC_thread_limit:
16740 case OMPC_ompx_dyn_cgroup_mem:
16741 case OMPC_dyn_groupprivate:
16742 // TODO: This may need to consider teams too.
16743 if (Leafs[0] == OMPD_target)
16744 return OMPD_target;
16745 break;
16746 case OMPC_device:
16747 if (Leafs[0] == OMPD_target ||
16748 llvm::is_contained(Set: {OMPD_dispatch, OMPD_target_update,
16749 OMPD_target_enter_data, OMPD_target_exit_data},
16750 Element: DKind))
16751 return OMPD_task;
16752 break;
16753 case OMPC_novariants:
16754 case OMPC_nocontext:
16755 if (DKind == OMPD_dispatch)
16756 return OMPD_task;
16757 break;
16758 case OMPC_when:
16759 if (DKind == OMPD_metadirective)
16760 return OMPD_metadirective;
16761 break;
16762 case OMPC_filter:
16763 return OMPD_unknown;
16764 default:
16765 break;
16766 }
16767
16768 // If none of the special cases above applied, and DKind is a capturing
16769 // directive, find the innermost enclosing leaf construct that allows the
16770 // clause, and returns the corresponding capture region.
16771
16772 auto GetEnclosingRegion = [&](int EndIdx, OpenMPClauseKind Clause) {
16773 // Find the index in "Leafs" of the last leaf that allows the given
16774 // clause. The search will only include indexes [0, EndIdx).
16775 // EndIdx may be set to the index of the NameModifier, if present.
16776 int InnermostIdx = [&]() {
16777 for (int I = EndIdx - 1; I >= 0; --I) {
16778 if (isAllowedClauseForDirective(D: Leafs[I], C: Clause, Version: OpenMPVersion))
16779 return I;
16780 }
16781 return -1;
16782 }();
16783
16784 // Find the nearest enclosing capture region.
16785 SmallVector<OpenMPDirectiveKind, 2> Regions;
16786 for (int I = InnermostIdx - 1; I >= 0; --I) {
16787 if (!isOpenMPCapturingDirective(DKind: Leafs[I]))
16788 continue;
16789 Regions.clear();
16790 getOpenMPCaptureRegions(CaptureRegions&: Regions, DKind: Leafs[I]);
16791 if (Regions[0] != OMPD_unknown)
16792 return Regions.back();
16793 }
16794 return OMPD_unknown;
16795 };
16796
16797 if (isOpenMPCapturingDirective(DKind)) {
16798 auto GetLeafIndex = [&](OpenMPDirectiveKind Dir) {
16799 for (int I = 0, E = Leafs.size(); I != E; ++I) {
16800 if (Leafs[I] == Dir)
16801 return I + 1;
16802 }
16803 return 0;
16804 };
16805
16806 int End = NameModifier == OMPD_unknown ? Leafs.size()
16807 : GetLeafIndex(NameModifier);
16808 return GetEnclosingRegion(End, CKind);
16809 }
16810
16811 return OMPD_unknown;
16812}
16813
16814OMPClause *SemaOpenMP::ActOnOpenMPIfClause(
16815 OpenMPDirectiveKind NameModifier, Expr *Condition, SourceLocation StartLoc,
16816 SourceLocation LParenLoc, SourceLocation NameModifierLoc,
16817 SourceLocation ColonLoc, SourceLocation EndLoc) {
16818 Expr *ValExpr = Condition;
16819 Stmt *HelperValStmt = nullptr;
16820 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
16821 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
16822 !Condition->isInstantiationDependent() &&
16823 !Condition->containsUnexpandedParameterPack()) {
16824 ExprResult Val = SemaRef.CheckBooleanCondition(Loc: StartLoc, E: Condition);
16825 if (Val.isInvalid())
16826 return nullptr;
16827
16828 ValExpr = Val.get();
16829
16830 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
16831 CaptureRegion = getOpenMPCaptureRegionForClause(
16832 DKind, CKind: OMPC_if, OpenMPVersion: getLangOpts().OpenMP, NameModifier);
16833 if (CaptureRegion != OMPD_unknown &&
16834 !SemaRef.CurContext->isDependentContext()) {
16835 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
16836 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
16837 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
16838 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
16839 }
16840 }
16841
16842 return new (getASTContext())
16843 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
16844 LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
16845}
16846
16847OMPClause *SemaOpenMP::ActOnOpenMPFinalClause(Expr *Condition,
16848 SourceLocation StartLoc,
16849 SourceLocation LParenLoc,
16850 SourceLocation EndLoc) {
16851 Expr *ValExpr = Condition;
16852 Stmt *HelperValStmt = nullptr;
16853 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
16854 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
16855 !Condition->isInstantiationDependent() &&
16856 !Condition->containsUnexpandedParameterPack()) {
16857 ExprResult Val = SemaRef.CheckBooleanCondition(Loc: StartLoc, E: Condition);
16858 if (Val.isInvalid())
16859 return nullptr;
16860
16861 ValExpr = SemaRef.MakeFullExpr(Arg: Val.get()).get();
16862
16863 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
16864 CaptureRegion = getOpenMPCaptureRegionForClause(DKind, CKind: OMPC_final,
16865 OpenMPVersion: getLangOpts().OpenMP);
16866 if (CaptureRegion != OMPD_unknown &&
16867 !SemaRef.CurContext->isDependentContext()) {
16868 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
16869 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
16870 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
16871 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
16872 }
16873 }
16874
16875 return new (getASTContext()) OMPFinalClause(
16876 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
16877}
16878
16879ExprResult
16880SemaOpenMP::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
16881 Expr *Op) {
16882 if (!Op)
16883 return ExprError();
16884
16885 class IntConvertDiagnoser : public Sema::ICEConvertDiagnoser {
16886 public:
16887 IntConvertDiagnoser()
16888 : ICEConvertDiagnoser(/*AllowScopedEnumerations=*/false, false, true) {}
16889 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
16890 QualType T) override {
16891 return S.Diag(Loc, DiagID: diag::err_omp_not_integral) << T;
16892 }
16893 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
16894 QualType T) override {
16895 return S.Diag(Loc, DiagID: diag::err_omp_incomplete_type) << T;
16896 }
16897 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
16898 QualType T,
16899 QualType ConvTy) override {
16900 return S.Diag(Loc, DiagID: diag::err_omp_explicit_conversion) << T << ConvTy;
16901 }
16902 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
16903 QualType ConvTy) override {
16904 return S.Diag(Loc: Conv->getLocation(), DiagID: diag::note_omp_conversion_here)
16905 << ConvTy->isEnumeralType() << ConvTy;
16906 }
16907 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
16908 QualType T) override {
16909 return S.Diag(Loc, DiagID: diag::err_omp_ambiguous_conversion) << T;
16910 }
16911 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
16912 QualType ConvTy) override {
16913 return S.Diag(Loc: Conv->getLocation(), DiagID: diag::note_omp_conversion_here)
16914 << ConvTy->isEnumeralType() << ConvTy;
16915 }
16916 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
16917 QualType) override {
16918 llvm_unreachable("conversion functions are permitted");
16919 }
16920 } ConvertDiagnoser;
16921 return SemaRef.PerformContextualImplicitConversion(Loc, FromE: Op, Converter&: ConvertDiagnoser);
16922}
16923
16924static bool
16925isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, OpenMPClauseKind CKind,
16926 bool StrictlyPositive, bool BuildCapture = false,
16927 OpenMPDirectiveKind DKind = OMPD_unknown,
16928 OpenMPDirectiveKind *CaptureRegion = nullptr,
16929 Stmt **HelperValStmt = nullptr) {
16930 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
16931 !ValExpr->isInstantiationDependent()) {
16932 SourceLocation Loc = ValExpr->getExprLoc();
16933 ExprResult Value =
16934 SemaRef.OpenMP().PerformOpenMPImplicitIntegerConversion(Loc, Op: ValExpr);
16935 if (Value.isInvalid())
16936 return false;
16937
16938 ValExpr = Value.get();
16939 // The expression must evaluate to a non-negative integer value.
16940 if (std::optional<llvm::APSInt> Result =
16941 ValExpr->getIntegerConstantExpr(Ctx: SemaRef.Context)) {
16942 if (Result->isSigned() &&
16943 !((!StrictlyPositive && Result->isNonNegative()) ||
16944 (StrictlyPositive && Result->isStrictlyPositive()))) {
16945 SemaRef.Diag(Loc, DiagID: diag::err_omp_negative_expression_in_clause)
16946 << getOpenMPClauseNameForDiag(C: CKind) << (StrictlyPositive ? 1 : 0)
16947 << ValExpr->getSourceRange();
16948 return false;
16949 }
16950 }
16951 if (!BuildCapture)
16952 return true;
16953 *CaptureRegion =
16954 getOpenMPCaptureRegionForClause(DKind, CKind, OpenMPVersion: SemaRef.LangOpts.OpenMP);
16955 if (*CaptureRegion != OMPD_unknown &&
16956 !SemaRef.CurContext->isDependentContext()) {
16957 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
16958 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
16959 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
16960 *HelperValStmt = buildPreInits(Context&: SemaRef.Context, Captures);
16961 }
16962 }
16963 return true;
16964}
16965
16966static std::string getListOfPossibleValues(OpenMPClauseKind K, unsigned First,
16967 unsigned Last,
16968 ArrayRef<unsigned> Exclude = {}) {
16969 SmallString<256> Buffer;
16970 llvm::raw_svector_ostream Out(Buffer);
16971 unsigned Skipped = Exclude.size();
16972 for (unsigned I = First; I < Last; ++I) {
16973 if (llvm::is_contained(Range&: Exclude, Element: I)) {
16974 --Skipped;
16975 continue;
16976 }
16977 Out << "'" << getOpenMPSimpleClauseTypeName(Kind: K, Type: I) << "'";
16978 if (I + Skipped + 2 == Last)
16979 Out << " or ";
16980 else if (I + Skipped + 1 != Last)
16981 Out << ", ";
16982 }
16983 return std::string(Out.str());
16984}
16985
16986OMPClause *SemaOpenMP::ActOnOpenMPNumThreadsClause(
16987 OpenMPNumThreadsClauseModifier Modifier, Expr *NumThreads,
16988 SourceLocation StartLoc, SourceLocation LParenLoc,
16989 SourceLocation ModifierLoc, SourceLocation EndLoc) {
16990 assert((ModifierLoc.isInvalid() || getLangOpts().OpenMP >= 60) &&
16991 "Unexpected num_threads modifier in OpenMP < 60.");
16992
16993 if (ModifierLoc.isValid() && Modifier == OMPC_NUMTHREADS_unknown) {
16994 std::string Values = getListOfPossibleValues(K: OMPC_num_threads, /*First=*/0,
16995 Last: OMPC_NUMTHREADS_unknown);
16996 Diag(Loc: ModifierLoc, DiagID: diag::err_omp_unexpected_clause_value)
16997 << Values << getOpenMPClauseNameForDiag(C: OMPC_num_threads);
16998 return nullptr;
16999 }
17000
17001 Expr *ValExpr = NumThreads;
17002 Stmt *HelperValStmt = nullptr;
17003
17004 // OpenMP [2.5, Restrictions]
17005 // The num_threads expression must evaluate to a positive integer value.
17006 if (!isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_num_threads,
17007 /*StrictlyPositive=*/true))
17008 return nullptr;
17009
17010 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
17011 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
17012 DKind, CKind: OMPC_num_threads, OpenMPVersion: getLangOpts().OpenMP);
17013 if (CaptureRegion != OMPD_unknown &&
17014 !SemaRef.CurContext->isDependentContext()) {
17015 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
17016 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
17017 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
17018 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
17019 }
17020
17021 return new (getASTContext())
17022 OMPNumThreadsClause(Modifier, ValExpr, HelperValStmt, CaptureRegion,
17023 StartLoc, LParenLoc, ModifierLoc, EndLoc);
17024}
17025
17026ExprResult SemaOpenMP::VerifyPositiveIntegerConstantInClause(
17027 Expr *E, OpenMPClauseKind CKind, bool StrictlyPositive,
17028 bool SuppressExprDiags) {
17029 if (!E)
17030 return ExprError();
17031 if (E->isValueDependent() || E->isTypeDependent() ||
17032 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
17033 return E;
17034
17035 llvm::APSInt Result;
17036 ExprResult ICE;
17037 if (SuppressExprDiags) {
17038 // Use a custom diagnoser that suppresses 'note' diagnostics about the
17039 // expression.
17040 struct SuppressedDiagnoser : public Sema::VerifyICEDiagnoser {
17041 SuppressedDiagnoser() : VerifyICEDiagnoser(/*Suppress=*/true) {}
17042 SemaBase::SemaDiagnosticBuilder
17043 diagnoseNotICE(Sema &S, SourceLocation Loc) override {
17044 llvm_unreachable("Diagnostic suppressed");
17045 }
17046 } Diagnoser;
17047 ICE = SemaRef.VerifyIntegerConstantExpression(E, Result: &Result, Diagnoser,
17048 CanFold: AllowFoldKind::Allow);
17049 } else {
17050 ICE =
17051 SemaRef.VerifyIntegerConstantExpression(E, Result: &Result,
17052 /*FIXME*/ CanFold: AllowFoldKind::Allow);
17053 }
17054 if (ICE.isInvalid())
17055 return ExprError();
17056
17057 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
17058 (!StrictlyPositive && !Result.isNonNegative())) {
17059 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_negative_expression_in_clause)
17060 << getOpenMPClauseNameForDiag(C: CKind) << (StrictlyPositive ? 1 : 0)
17061 << E->getSourceRange();
17062 return ExprError();
17063 }
17064 if ((CKind == OMPC_aligned || CKind == OMPC_align ||
17065 CKind == OMPC_allocate) &&
17066 !Result.isPowerOf2()) {
17067 Diag(Loc: E->getExprLoc(), DiagID: diag::warn_omp_alignment_not_power_of_two)
17068 << E->getSourceRange();
17069 return ExprError();
17070 }
17071
17072 if (!Result.isRepresentableByInt64()) {
17073 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_large_expression_in_clause)
17074 << getOpenMPClauseNameForDiag(C: CKind) << E->getSourceRange();
17075 return ExprError();
17076 }
17077
17078 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
17079 DSAStack->setAssociatedLoops(Result.getExtValue());
17080 else if (CKind == OMPC_ordered)
17081 DSAStack->setAssociatedLoops(Result.getExtValue());
17082 return ICE;
17083}
17084
17085void SemaOpenMP::setOpenMPDeviceNum(int Num) { DeviceNum = Num; }
17086
17087void SemaOpenMP::setOpenMPDeviceNumID(StringRef ID) { DeviceNumID = ID; }
17088
17089int SemaOpenMP::getOpenMPDeviceNum() const { return DeviceNum; }
17090
17091void SemaOpenMP::ActOnOpenMPDeviceNum(Expr *DeviceNumExpr) {
17092 llvm::APSInt Result;
17093 Expr::EvalResult EvalResult;
17094 // Evaluate the expression to an integer value
17095 if (!DeviceNumExpr->isValueDependent() &&
17096 DeviceNumExpr->EvaluateAsInt(Result&: EvalResult, Ctx: SemaRef.Context)) {
17097 // The device expression must evaluate to a non-negative integer value.
17098 Result = EvalResult.Val.getInt();
17099 if (Result.isNonNegative()) {
17100 setOpenMPDeviceNum(Result.getZExtValue());
17101 } else {
17102 Diag(Loc: DeviceNumExpr->getExprLoc(),
17103 DiagID: diag::err_omp_negative_expression_in_clause)
17104 << "device_num" << 0 << DeviceNumExpr->getSourceRange();
17105 }
17106 } else if (auto *DeclRef = dyn_cast<DeclRefExpr>(Val: DeviceNumExpr)) {
17107 // Check if the expression is an identifier
17108 IdentifierInfo *IdInfo = DeclRef->getDecl()->getIdentifier();
17109 if (IdInfo) {
17110 setOpenMPDeviceNumID(IdInfo->getName());
17111 }
17112 } else {
17113 Diag(Loc: DeviceNumExpr->getExprLoc(), DiagID: diag::err_expected_expression);
17114 }
17115}
17116
17117OMPClause *SemaOpenMP::ActOnOpenMPSafelenClause(Expr *Len,
17118 SourceLocation StartLoc,
17119 SourceLocation LParenLoc,
17120 SourceLocation EndLoc) {
17121 // OpenMP [2.8.1, simd construct, Description]
17122 // The parameter of the safelen clause must be a constant
17123 // positive integer expression.
17124 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(E: Len, CKind: OMPC_safelen);
17125 if (Safelen.isInvalid())
17126 return nullptr;
17127 return new (getASTContext())
17128 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
17129}
17130
17131OMPClause *SemaOpenMP::ActOnOpenMPSimdlenClause(Expr *Len,
17132 SourceLocation StartLoc,
17133 SourceLocation LParenLoc,
17134 SourceLocation EndLoc) {
17135 // OpenMP [2.8.1, simd construct, Description]
17136 // The parameter of the simdlen clause must be a constant
17137 // positive integer expression.
17138 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(E: Len, CKind: OMPC_simdlen);
17139 if (Simdlen.isInvalid())
17140 return nullptr;
17141 return new (getASTContext())
17142 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
17143}
17144
17145/// Tries to find omp_allocator_handle_t type.
17146static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc,
17147 DSAStackTy *Stack) {
17148 if (!Stack->getOMPAllocatorHandleT().isNull())
17149 return true;
17150
17151 // Set the allocator handle type.
17152 IdentifierInfo *II = &S.PP.getIdentifierTable().get(Name: "omp_allocator_handle_t");
17153 ParsedType PT = S.getTypeName(II: *II, NameLoc: Loc, S: S.getCurScope());
17154 if (!PT.getAsOpaquePtr() || PT.get().isNull()) {
17155 S.Diag(Loc, DiagID: diag::err_omp_implied_type_not_found)
17156 << "omp_allocator_handle_t";
17157 return false;
17158 }
17159 QualType AllocatorHandleEnumTy = PT.get();
17160 AllocatorHandleEnumTy.addConst();
17161 Stack->setOMPAllocatorHandleT(AllocatorHandleEnumTy);
17162
17163 // Fill the predefined allocator map.
17164 bool ErrorFound = false;
17165 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
17166 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
17167 StringRef Allocator =
17168 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(Val: AllocatorKind);
17169 DeclarationName AllocatorName = &S.getASTContext().Idents.get(Name: Allocator);
17170 auto *VD = dyn_cast_or_null<ValueDecl>(
17171 Val: S.LookupSingleName(S: S.TUScope, Name: AllocatorName, Loc, NameKind: Sema::LookupAnyName));
17172 if (!VD) {
17173 ErrorFound = true;
17174 break;
17175 }
17176 QualType AllocatorType =
17177 VD->getType().getNonLValueExprType(Context: S.getASTContext());
17178 ExprResult Res = S.BuildDeclRefExpr(D: VD, Ty: AllocatorType, VK: VK_LValue, Loc);
17179 if (!Res.isUsable()) {
17180 ErrorFound = true;
17181 break;
17182 }
17183 Res = S.PerformImplicitConversion(From: Res.get(), ToType: AllocatorHandleEnumTy,
17184 Action: AssignmentAction::Initializing,
17185 /*AllowExplicit=*/true);
17186 if (!Res.isUsable()) {
17187 ErrorFound = true;
17188 break;
17189 }
17190 Stack->setAllocator(AllocatorKind, Allocator: Res.get());
17191 }
17192 if (ErrorFound) {
17193 S.Diag(Loc, DiagID: diag::err_omp_implied_type_not_found)
17194 << "omp_allocator_handle_t";
17195 return false;
17196 }
17197
17198 return true;
17199}
17200
17201OMPClause *SemaOpenMP::ActOnOpenMPAllocatorClause(Expr *A,
17202 SourceLocation StartLoc,
17203 SourceLocation LParenLoc,
17204 SourceLocation EndLoc) {
17205 // OpenMP [2.11.3, allocate Directive, Description]
17206 // allocator is an expression of omp_allocator_handle_t type.
17207 if (!findOMPAllocatorHandleT(S&: SemaRef, Loc: A->getExprLoc(), DSAStack))
17208 return nullptr;
17209
17210 ExprResult Allocator = SemaRef.DefaultLvalueConversion(E: A);
17211 if (Allocator.isInvalid())
17212 return nullptr;
17213 Allocator = SemaRef.PerformImplicitConversion(
17214 From: Allocator.get(), DSAStack->getOMPAllocatorHandleT(),
17215 Action: AssignmentAction::Initializing,
17216 /*AllowExplicit=*/true);
17217 if (Allocator.isInvalid())
17218 return nullptr;
17219 return new (getASTContext())
17220 OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc);
17221}
17222
17223OMPClause *SemaOpenMP::ActOnOpenMPCollapseClause(Expr *NumForLoops,
17224 SourceLocation StartLoc,
17225 SourceLocation LParenLoc,
17226 SourceLocation EndLoc) {
17227 // OpenMP [2.7.1, loop construct, Description]
17228 // OpenMP [2.8.1, simd construct, Description]
17229 // OpenMP [2.9.6, distribute construct, Description]
17230 // The parameter of the collapse clause must be a constant
17231 // positive integer expression.
17232 ExprResult NumForLoopsResult =
17233 VerifyPositiveIntegerConstantInClause(E: NumForLoops, CKind: OMPC_collapse);
17234 if (NumForLoopsResult.isInvalid())
17235 return nullptr;
17236 return new (getASTContext())
17237 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
17238}
17239
17240OMPClause *SemaOpenMP::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
17241 SourceLocation EndLoc,
17242 SourceLocation LParenLoc,
17243 Expr *NumForLoops) {
17244 // OpenMP [2.7.1, loop construct, Description]
17245 // OpenMP [2.8.1, simd construct, Description]
17246 // OpenMP [2.9.6, distribute construct, Description]
17247 // The parameter of the ordered clause must be a constant
17248 // positive integer expression if any.
17249 if (NumForLoops && LParenLoc.isValid()) {
17250 ExprResult NumForLoopsResult =
17251 VerifyPositiveIntegerConstantInClause(E: NumForLoops, CKind: OMPC_ordered);
17252 if (NumForLoopsResult.isInvalid())
17253 return nullptr;
17254 NumForLoops = NumForLoopsResult.get();
17255 } else {
17256 NumForLoops = nullptr;
17257 }
17258 auto *Clause =
17259 OMPOrderedClause::Create(C: getASTContext(), Num: NumForLoops,
17260 NumLoops: NumForLoops ? DSAStack->getAssociatedLoops() : 0,
17261 StartLoc, LParenLoc, EndLoc);
17262 DSAStack->setOrderedRegion(/*IsOrdered=*/true, Param: NumForLoops, Clause);
17263 return Clause;
17264}
17265
17266OMPClause *SemaOpenMP::ActOnOpenMPSimpleClause(
17267 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
17268 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
17269 OMPClause *Res = nullptr;
17270 switch (Kind) {
17271 case OMPC_proc_bind:
17272 Res = ActOnOpenMPProcBindClause(Kind: static_cast<ProcBindKind>(Argument),
17273 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
17274 break;
17275 case OMPC_atomic_default_mem_order:
17276 Res = ActOnOpenMPAtomicDefaultMemOrderClause(
17277 Kind: static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument),
17278 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
17279 break;
17280 case OMPC_fail:
17281 Res = ActOnOpenMPFailClause(Kind: static_cast<OpenMPClauseKind>(Argument),
17282 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
17283 break;
17284 case OMPC_update:
17285 Res = ActOnOpenMPUpdateClause(Kind: static_cast<OpenMPDependClauseKind>(Argument),
17286 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
17287 break;
17288 case OMPC_bind:
17289 Res = ActOnOpenMPBindClause(Kind: static_cast<OpenMPBindClauseKind>(Argument),
17290 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
17291 break;
17292 case OMPC_at:
17293 Res = ActOnOpenMPAtClause(Kind: static_cast<OpenMPAtClauseKind>(Argument),
17294 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
17295 break;
17296 case OMPC_severity:
17297 Res = ActOnOpenMPSeverityClause(
17298 Kind: static_cast<OpenMPSeverityClauseKind>(Argument), KindLoc: ArgumentLoc, StartLoc,
17299 LParenLoc, EndLoc);
17300 break;
17301 case OMPC_threadset:
17302 Res = ActOnOpenMPThreadsetClause(Kind: static_cast<OpenMPThreadsetKind>(Argument),
17303 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
17304 break;
17305 case OMPC_if:
17306 case OMPC_final:
17307 case OMPC_num_threads:
17308 case OMPC_safelen:
17309 case OMPC_simdlen:
17310 case OMPC_sizes:
17311 case OMPC_allocator:
17312 case OMPC_collapse:
17313 case OMPC_schedule:
17314 case OMPC_private:
17315 case OMPC_firstprivate:
17316 case OMPC_lastprivate:
17317 case OMPC_shared:
17318 case OMPC_reduction:
17319 case OMPC_task_reduction:
17320 case OMPC_in_reduction:
17321 case OMPC_linear:
17322 case OMPC_aligned:
17323 case OMPC_copyin:
17324 case OMPC_copyprivate:
17325 case OMPC_ordered:
17326 case OMPC_nowait:
17327 case OMPC_untied:
17328 case OMPC_mergeable:
17329 case OMPC_threadprivate:
17330 case OMPC_groupprivate:
17331 case OMPC_allocate:
17332 case OMPC_flush:
17333 case OMPC_depobj:
17334 case OMPC_read:
17335 case OMPC_write:
17336 case OMPC_capture:
17337 case OMPC_compare:
17338 case OMPC_seq_cst:
17339 case OMPC_acq_rel:
17340 case OMPC_acquire:
17341 case OMPC_release:
17342 case OMPC_relaxed:
17343 case OMPC_depend:
17344 case OMPC_device:
17345 case OMPC_threads:
17346 case OMPC_simd:
17347 case OMPC_map:
17348 case OMPC_num_teams:
17349 case OMPC_thread_limit:
17350 case OMPC_priority:
17351 case OMPC_grainsize:
17352 case OMPC_nogroup:
17353 case OMPC_num_tasks:
17354 case OMPC_hint:
17355 case OMPC_dist_schedule:
17356 case OMPC_default:
17357 case OMPC_defaultmap:
17358 case OMPC_unknown:
17359 case OMPC_uniform:
17360 case OMPC_to:
17361 case OMPC_from:
17362 case OMPC_use_device_ptr:
17363 case OMPC_use_device_addr:
17364 case OMPC_is_device_ptr:
17365 case OMPC_has_device_addr:
17366 case OMPC_unified_address:
17367 case OMPC_unified_shared_memory:
17368 case OMPC_reverse_offload:
17369 case OMPC_dynamic_allocators:
17370 case OMPC_self_maps:
17371 case OMPC_device_type:
17372 case OMPC_match:
17373 case OMPC_nontemporal:
17374 case OMPC_destroy:
17375 case OMPC_novariants:
17376 case OMPC_nocontext:
17377 case OMPC_detach:
17378 case OMPC_inclusive:
17379 case OMPC_exclusive:
17380 case OMPC_uses_allocators:
17381 case OMPC_affinity:
17382 case OMPC_when:
17383 case OMPC_message:
17384 default:
17385 llvm_unreachable("Clause is not allowed.");
17386 }
17387 return Res;
17388}
17389
17390OMPClause *SemaOpenMP::ActOnOpenMPDefaultClause(
17391 llvm::omp::DefaultKind M, SourceLocation MLoc,
17392 OpenMPDefaultClauseVariableCategory VCKind, SourceLocation VCKindLoc,
17393 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
17394 if (M == OMP_DEFAULT_unknown) {
17395 Diag(Loc: MLoc, DiagID: diag::err_omp_unexpected_clause_value)
17396 << getListOfPossibleValues(K: OMPC_default, /*First=*/0,
17397 /*Last=*/unsigned(OMP_DEFAULT_unknown))
17398 << getOpenMPClauseNameForDiag(C: OMPC_default);
17399 return nullptr;
17400 }
17401 if (VCKind == OMPC_DEFAULT_VC_unknown) {
17402 Diag(Loc: VCKindLoc, DiagID: diag::err_omp_default_vc)
17403 << getOpenMPSimpleClauseTypeName(Kind: OMPC_default, Type: unsigned(M));
17404 return nullptr;
17405 }
17406
17407 bool IsTargetDefault =
17408 getLangOpts().OpenMP >= 60 &&
17409 isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective());
17410
17411 // OpenMP 6.0, page 224, lines 3-4 default Clause, Semantics
17412 // If data-sharing-attribute is shared then the clause has no effect
17413 // on a target construct;
17414 if (IsTargetDefault && M == OMP_DEFAULT_shared)
17415 return nullptr;
17416
17417 auto SetDefaultClauseAttrs = [&](llvm::omp::DefaultKind M,
17418 OpenMPDefaultClauseVariableCategory VCKind) {
17419 OpenMPDefaultmapClauseModifier DefMapMod;
17420 OpenMPDefaultmapClauseKind DefMapKind;
17421 // default data-sharing-attribute
17422 switch (M) {
17423 case OMP_DEFAULT_none:
17424 if (IsTargetDefault)
17425 DefMapMod = OMPC_DEFAULTMAP_MODIFIER_none;
17426 else
17427 DSAStack->setDefaultDSANone(MLoc);
17428 break;
17429 case OMP_DEFAULT_firstprivate:
17430 if (IsTargetDefault)
17431 DefMapMod = OMPC_DEFAULTMAP_MODIFIER_firstprivate;
17432 else
17433 DSAStack->setDefaultDSAFirstPrivate(MLoc);
17434 break;
17435 case OMP_DEFAULT_private:
17436 if (IsTargetDefault)
17437 DefMapMod = OMPC_DEFAULTMAP_MODIFIER_private;
17438 else
17439 DSAStack->setDefaultDSAPrivate(MLoc);
17440 break;
17441 case OMP_DEFAULT_shared:
17442 assert(!IsTargetDefault && "DSA shared invalid with target directive");
17443 DSAStack->setDefaultDSAShared(MLoc);
17444 break;
17445 default:
17446 llvm_unreachable("unexpected DSA in OpenMP default clause");
17447 }
17448 // default variable-category
17449 switch (VCKind) {
17450 case OMPC_DEFAULT_VC_aggregate:
17451 if (IsTargetDefault)
17452 DefMapKind = OMPC_DEFAULTMAP_aggregate;
17453 else
17454 DSAStack->setDefaultDSAVCAggregate(VCKindLoc);
17455 break;
17456 case OMPC_DEFAULT_VC_pointer:
17457 if (IsTargetDefault)
17458 DefMapKind = OMPC_DEFAULTMAP_pointer;
17459 else
17460 DSAStack->setDefaultDSAVCPointer(VCKindLoc);
17461 break;
17462 case OMPC_DEFAULT_VC_scalar:
17463 if (IsTargetDefault)
17464 DefMapKind = OMPC_DEFAULTMAP_scalar;
17465 else
17466 DSAStack->setDefaultDSAVCScalar(VCKindLoc);
17467 break;
17468 case OMPC_DEFAULT_VC_all:
17469 if (IsTargetDefault)
17470 DefMapKind = OMPC_DEFAULTMAP_all;
17471 else
17472 DSAStack->setDefaultDSAVCAll(VCKindLoc);
17473 break;
17474 default:
17475 llvm_unreachable("unexpected variable category in OpenMP default clause");
17476 }
17477 // OpenMP 6.0, page 224, lines 4-5 default Clause, Semantics
17478 // otherwise, its effect on a target construct is equivalent to
17479 // specifying the defaultmap clause with the same data-sharing-attribute
17480 // and variable-category.
17481 //
17482 // If earlier than OpenMP 6.0, or not a target directive, the default DSA
17483 // is/was set as before.
17484 if (IsTargetDefault) {
17485 if (DefMapKind == OMPC_DEFAULTMAP_all) {
17486 DSAStack->setDefaultDMAAttr(M: DefMapMod, Kind: OMPC_DEFAULTMAP_aggregate, Loc: MLoc);
17487 DSAStack->setDefaultDMAAttr(M: DefMapMod, Kind: OMPC_DEFAULTMAP_scalar, Loc: MLoc);
17488 DSAStack->setDefaultDMAAttr(M: DefMapMod, Kind: OMPC_DEFAULTMAP_pointer, Loc: MLoc);
17489 } else {
17490 DSAStack->setDefaultDMAAttr(M: DefMapMod, Kind: DefMapKind, Loc: MLoc);
17491 }
17492 }
17493 };
17494
17495 SetDefaultClauseAttrs(M, VCKind);
17496 return new (getASTContext())
17497 OMPDefaultClause(M, MLoc, VCKind, VCKindLoc, StartLoc, LParenLoc, EndLoc);
17498}
17499
17500OMPClause *SemaOpenMP::ActOnOpenMPThreadsetClause(OpenMPThreadsetKind Kind,
17501 SourceLocation KindLoc,
17502 SourceLocation StartLoc,
17503 SourceLocation LParenLoc,
17504 SourceLocation EndLoc) {
17505 if (Kind == OMPC_THREADSET_unknown) {
17506 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
17507 << getListOfPossibleValues(K: OMPC_threadset, /*First=*/0,
17508 /*Last=*/unsigned(OMPC_THREADSET_unknown))
17509 << getOpenMPClauseName(C: OMPC_threadset);
17510 return nullptr;
17511 }
17512
17513 return new (getASTContext())
17514 OMPThreadsetClause(Kind, KindLoc, StartLoc, LParenLoc, EndLoc);
17515}
17516
17517static OMPClause *
17518createTransparentClause(Sema &SemaRef, ASTContext &Ctx, Expr *ImpexTypeArg,
17519 Stmt *HelperValStmt, OpenMPDirectiveKind CaptureRegion,
17520 SourceLocation StartLoc, SourceLocation LParenLoc,
17521 SourceLocation EndLoc) {
17522 ExprResult ER = SemaRef.DefaultLvalueConversion(E: ImpexTypeArg);
17523 if (ER.isInvalid())
17524 return nullptr;
17525
17526 return new (Ctx) OMPTransparentClause(ER.get(), HelperValStmt, CaptureRegion,
17527 StartLoc, LParenLoc, EndLoc);
17528}
17529
17530OMPClause *SemaOpenMP::ActOnOpenMPTransparentClause(Expr *ImpexTypeArg,
17531 SourceLocation StartLoc,
17532 SourceLocation LParenLoc,
17533 SourceLocation EndLoc) {
17534 Stmt *HelperValStmt = nullptr;
17535 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
17536 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
17537 DKind, CKind: OMPC_transparent, OpenMPVersion: getLangOpts().OpenMP);
17538 if (CaptureRegion != OMPD_unknown &&
17539 !SemaRef.CurContext->isDependentContext()) {
17540 Expr *ValExpr = SemaRef.MakeFullExpr(Arg: ImpexTypeArg).get();
17541 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
17542 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
17543 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
17544 }
17545 if (!ImpexTypeArg) {
17546 return new (getASTContext())
17547 OMPTransparentClause(ImpexTypeArg, HelperValStmt, CaptureRegion,
17548 StartLoc, LParenLoc, EndLoc);
17549 }
17550 QualType Ty = ImpexTypeArg->getType();
17551
17552 if (const auto *TT = Ty->getAs<TypedefType>()) {
17553 const TypedefNameDecl *TypedefDecl = TT->getDecl();
17554 llvm::StringRef TypedefName = TypedefDecl->getName();
17555 IdentifierInfo &II = SemaRef.PP.getIdentifierTable().get(Name: TypedefName);
17556 ParsedType ImpexTy =
17557 SemaRef.getTypeName(II, NameLoc: StartLoc, S: SemaRef.getCurScope());
17558 if (!ImpexTy.getAsOpaquePtr() || ImpexTy.get().isNull()) {
17559 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_implied_type_not_found)
17560 << TypedefName;
17561 return nullptr;
17562 }
17563 return new (getASTContext())
17564 OMPTransparentClause(ImpexTypeArg, HelperValStmt, CaptureRegion,
17565 StartLoc, LParenLoc, EndLoc);
17566 }
17567
17568 if (Ty->isEnumeralType())
17569 return createTransparentClause(SemaRef, Ctx&: getASTContext(), ImpexTypeArg,
17570 HelperValStmt, CaptureRegion, StartLoc,
17571 LParenLoc, EndLoc);
17572 if (Ty->isIntegerType()) {
17573 if (isNonNegativeIntegerValue(ValExpr&: ImpexTypeArg, SemaRef, CKind: OMPC_transparent,
17574 /*StrictlyPositive=*/false)) {
17575 ExprResult Value =
17576 SemaRef.OpenMP().PerformOpenMPImplicitIntegerConversion(Loc: StartLoc,
17577 Op: ImpexTypeArg);
17578 if (std::optional<llvm::APSInt> Result =
17579 Value.get()->getIntegerConstantExpr(Ctx: SemaRef.Context)) {
17580 if (Result->isNegative() ||
17581 Result >
17582 static_cast<int64_t>(SemaOpenMP::OpenMPImpexType::OMP_Export))
17583 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_transparent_invalid_value);
17584 }
17585 return new (getASTContext())
17586 OMPTransparentClause(ImpexTypeArg, HelperValStmt, CaptureRegion,
17587 StartLoc, LParenLoc, EndLoc);
17588 }
17589 }
17590 if (!isNonNegativeIntegerValue(ValExpr&: ImpexTypeArg, SemaRef, CKind: OMPC_transparent,
17591 /*StrictlyPositive=*/true))
17592 return nullptr;
17593 return new (getASTContext()) OMPTransparentClause(
17594 ImpexTypeArg, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
17595}
17596
17597OMPClause *SemaOpenMP::ActOnOpenMPProcBindClause(ProcBindKind Kind,
17598 SourceLocation KindKwLoc,
17599 SourceLocation StartLoc,
17600 SourceLocation LParenLoc,
17601 SourceLocation EndLoc) {
17602 if (Kind == OMP_PROC_BIND_unknown) {
17603 Diag(Loc: KindKwLoc, DiagID: diag::err_omp_unexpected_clause_value)
17604 << getListOfPossibleValues(K: OMPC_proc_bind,
17605 /*First=*/unsigned(OMP_PROC_BIND_master),
17606 /*Last=*/
17607 unsigned(getLangOpts().OpenMP > 50
17608 ? OMP_PROC_BIND_primary
17609 : OMP_PROC_BIND_spread) +
17610 1)
17611 << getOpenMPClauseNameForDiag(C: OMPC_proc_bind);
17612 return nullptr;
17613 }
17614 if (Kind == OMP_PROC_BIND_primary && getLangOpts().OpenMP < 51)
17615 Diag(Loc: KindKwLoc, DiagID: diag::err_omp_unexpected_clause_value)
17616 << getListOfPossibleValues(K: OMPC_proc_bind,
17617 /*First=*/unsigned(OMP_PROC_BIND_master),
17618 /*Last=*/
17619 unsigned(OMP_PROC_BIND_spread) + 1)
17620 << getOpenMPClauseNameForDiag(C: OMPC_proc_bind);
17621 return new (getASTContext())
17622 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
17623}
17624
17625OMPClause *SemaOpenMP::ActOnOpenMPAtomicDefaultMemOrderClause(
17626 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc,
17627 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
17628 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) {
17629 Diag(Loc: KindKwLoc, DiagID: diag::err_omp_unexpected_clause_value)
17630 << getListOfPossibleValues(
17631 K: OMPC_atomic_default_mem_order, /*First=*/0,
17632 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown)
17633 << getOpenMPClauseNameForDiag(C: OMPC_atomic_default_mem_order);
17634 return nullptr;
17635 }
17636 return new (getASTContext()) OMPAtomicDefaultMemOrderClause(
17637 Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
17638}
17639
17640OMPClause *SemaOpenMP::ActOnOpenMPAtClause(OpenMPAtClauseKind Kind,
17641 SourceLocation KindKwLoc,
17642 SourceLocation StartLoc,
17643 SourceLocation LParenLoc,
17644 SourceLocation EndLoc) {
17645 if (Kind == OMPC_AT_unknown) {
17646 Diag(Loc: KindKwLoc, DiagID: diag::err_omp_unexpected_clause_value)
17647 << getListOfPossibleValues(K: OMPC_at, /*First=*/0,
17648 /*Last=*/OMPC_AT_unknown)
17649 << getOpenMPClauseNameForDiag(C: OMPC_at);
17650 return nullptr;
17651 }
17652 return new (getASTContext())
17653 OMPAtClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
17654}
17655
17656OMPClause *SemaOpenMP::ActOnOpenMPSeverityClause(OpenMPSeverityClauseKind Kind,
17657 SourceLocation KindKwLoc,
17658 SourceLocation StartLoc,
17659 SourceLocation LParenLoc,
17660 SourceLocation EndLoc) {
17661 if (Kind == OMPC_SEVERITY_unknown) {
17662 Diag(Loc: KindKwLoc, DiagID: diag::err_omp_unexpected_clause_value)
17663 << getListOfPossibleValues(K: OMPC_severity, /*First=*/0,
17664 /*Last=*/OMPC_SEVERITY_unknown)
17665 << getOpenMPClauseNameForDiag(C: OMPC_severity);
17666 return nullptr;
17667 }
17668 return new (getASTContext())
17669 OMPSeverityClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
17670}
17671
17672OMPClause *SemaOpenMP::ActOnOpenMPMessageClause(Expr *ME,
17673 SourceLocation StartLoc,
17674 SourceLocation LParenLoc,
17675 SourceLocation EndLoc) {
17676 assert(ME && "NULL expr in Message clause");
17677 QualType Type = ME->getType();
17678 if ((!Type->isPointerType() && !Type->isArrayType()) ||
17679 !Type->getPointeeOrArrayElementType()->isAnyCharacterType()) {
17680 Diag(Loc: ME->getBeginLoc(), DiagID: diag::warn_clause_expected_string)
17681 << getOpenMPClauseNameForDiag(C: OMPC_message) << 0;
17682 return nullptr;
17683 }
17684
17685 Stmt *HelperValStmt = nullptr;
17686
17687 // Depending on whether this clause appears in an executable context or not,
17688 // we may or may not build a capture.
17689 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
17690 OpenMPDirectiveKind CaptureRegion =
17691 DKind == OMPD_unknown ? OMPD_unknown
17692 : getOpenMPCaptureRegionForClause(
17693 DKind, CKind: OMPC_message, OpenMPVersion: getLangOpts().OpenMP);
17694 if (CaptureRegion != OMPD_unknown &&
17695 !SemaRef.CurContext->isDependentContext()) {
17696 ME = SemaRef.MakeFullExpr(Arg: ME).get();
17697 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
17698 ME = tryBuildCapture(SemaRef, Capture: ME, Captures).get();
17699 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
17700 }
17701
17702 // Convert array type to pointer type if needed.
17703 ME = SemaRef.DefaultFunctionArrayLvalueConversion(E: ME).get();
17704
17705 return new (getASTContext()) OMPMessageClause(
17706 ME, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
17707}
17708
17709OMPClause *SemaOpenMP::ActOnOpenMPOrderClause(
17710 OpenMPOrderClauseModifier Modifier, OpenMPOrderClauseKind Kind,
17711 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
17712 SourceLocation KindLoc, SourceLocation EndLoc) {
17713 if (Kind != OMPC_ORDER_concurrent ||
17714 (getLangOpts().OpenMP < 51 && MLoc.isValid())) {
17715 // Kind should be concurrent,
17716 // Modifiers introduced in OpenMP 5.1
17717 static_assert(OMPC_ORDER_unknown > 0,
17718 "OMPC_ORDER_unknown not greater than 0");
17719
17720 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
17721 << getListOfPossibleValues(K: OMPC_order,
17722 /*First=*/0,
17723 /*Last=*/OMPC_ORDER_unknown)
17724 << getOpenMPClauseNameForDiag(C: OMPC_order);
17725 return nullptr;
17726 }
17727 if (getLangOpts().OpenMP >= 51 && Modifier == OMPC_ORDER_MODIFIER_unknown &&
17728 MLoc.isValid()) {
17729 Diag(Loc: MLoc, DiagID: diag::err_omp_unexpected_clause_value)
17730 << getListOfPossibleValues(K: OMPC_order,
17731 /*First=*/OMPC_ORDER_MODIFIER_unknown + 1,
17732 /*Last=*/OMPC_ORDER_MODIFIER_last)
17733 << getOpenMPClauseNameForDiag(C: OMPC_order);
17734 } else if (getLangOpts().OpenMP >= 50) {
17735 DSAStack->setRegionHasOrderConcurrent(/*HasOrderConcurrent=*/true);
17736 if (DSAStack->getCurScope()) {
17737 // mark the current scope with 'order' flag
17738 unsigned existingFlags = DSAStack->getCurScope()->getFlags();
17739 DSAStack->getCurScope()->setFlags(existingFlags |
17740 Scope::OpenMPOrderClauseScope);
17741 }
17742 }
17743 return new (getASTContext()) OMPOrderClause(
17744 Kind, KindLoc, StartLoc, LParenLoc, EndLoc, Modifier, MLoc);
17745}
17746
17747OMPClause *SemaOpenMP::ActOnOpenMPUpdateClause(OpenMPDependClauseKind Kind,
17748 SourceLocation KindKwLoc,
17749 SourceLocation StartLoc,
17750 SourceLocation LParenLoc,
17751 SourceLocation EndLoc) {
17752 if (Kind == OMPC_DEPEND_unknown || Kind == OMPC_DEPEND_source ||
17753 Kind == OMPC_DEPEND_sink || Kind == OMPC_DEPEND_depobj) {
17754 SmallVector<unsigned> Except = {
17755 OMPC_DEPEND_source, OMPC_DEPEND_sink, OMPC_DEPEND_depobj,
17756 OMPC_DEPEND_outallmemory, OMPC_DEPEND_inoutallmemory};
17757 if (getLangOpts().OpenMP < 51)
17758 Except.push_back(Elt: OMPC_DEPEND_inoutset);
17759 Diag(Loc: KindKwLoc, DiagID: diag::err_omp_unexpected_clause_value)
17760 << getListOfPossibleValues(K: OMPC_depend, /*First=*/0,
17761 /*Last=*/OMPC_DEPEND_unknown, Exclude: Except)
17762 << getOpenMPClauseNameForDiag(C: OMPC_update);
17763 return nullptr;
17764 }
17765 return OMPUpdateClause::Create(C: getASTContext(), StartLoc, LParenLoc,
17766 ArgumentLoc: KindKwLoc, DK: Kind, EndLoc);
17767}
17768
17769OMPClause *SemaOpenMP::ActOnOpenMPSizesClause(ArrayRef<Expr *> SizeExprs,
17770 SourceLocation StartLoc,
17771 SourceLocation LParenLoc,
17772 SourceLocation EndLoc) {
17773 SmallVector<Expr *> SanitizedSizeExprs(SizeExprs);
17774
17775 for (Expr *&SizeExpr : SanitizedSizeExprs) {
17776 // Skip if already sanitized, e.g. during a partial template instantiation.
17777 if (!SizeExpr)
17778 continue;
17779
17780 bool IsValid = isNonNegativeIntegerValue(ValExpr&: SizeExpr, SemaRef, CKind: OMPC_sizes,
17781 /*StrictlyPositive=*/true);
17782
17783 // isNonNegativeIntegerValue returns true for non-integral types (but still
17784 // emits error diagnostic), so check for the expected type explicitly.
17785 QualType SizeTy = SizeExpr->getType();
17786 if (!SizeTy->isIntegerType())
17787 IsValid = false;
17788
17789 // Handling in templates is tricky. There are four possibilities to
17790 // consider:
17791 //
17792 // 1a. The expression is valid and we are in a instantiated template or not
17793 // in a template:
17794 // Pass valid expression to be further analysed later in Sema.
17795 // 1b. The expression is valid and we are in a template (including partial
17796 // instantiation):
17797 // isNonNegativeIntegerValue skipped any checks so there is no
17798 // guarantee it will be correct after instantiation.
17799 // ActOnOpenMPSizesClause will be called again at instantiation when
17800 // it is not in a dependent context anymore. This may cause warnings
17801 // to be emitted multiple times.
17802 // 2a. The expression is invalid and we are in an instantiated template or
17803 // not in a template:
17804 // Invalidate the expression with a clearly wrong value (nullptr) so
17805 // later in Sema we do not have to do the same validity analysis again
17806 // or crash from unexpected data. Error diagnostics have already been
17807 // emitted.
17808 // 2b. The expression is invalid and we are in a template (including partial
17809 // instantiation):
17810 // Pass the invalid expression as-is, template instantiation may
17811 // replace unexpected types/values with valid ones. The directives
17812 // with this clause must not try to use these expressions in dependent
17813 // contexts, but delay analysis until full instantiation.
17814 if (!SizeExpr->isInstantiationDependent() && !IsValid)
17815 SizeExpr = nullptr;
17816 }
17817
17818 return OMPSizesClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
17819 Sizes: SanitizedSizeExprs);
17820}
17821
17822OMPClause *SemaOpenMP::ActOnOpenMPPermutationClause(ArrayRef<Expr *> PermExprs,
17823 SourceLocation StartLoc,
17824 SourceLocation LParenLoc,
17825 SourceLocation EndLoc) {
17826 size_t NumLoops = PermExprs.size();
17827 SmallVector<Expr *> SanitizedPermExprs;
17828 llvm::append_range(C&: SanitizedPermExprs, R&: PermExprs);
17829
17830 for (Expr *&PermExpr : SanitizedPermExprs) {
17831 // Skip if template-dependent or already sanitized, e.g. during a partial
17832 // template instantiation.
17833 if (!PermExpr || PermExpr->isInstantiationDependent())
17834 continue;
17835
17836 llvm::APSInt PermVal;
17837 ExprResult PermEvalExpr = SemaRef.VerifyIntegerConstantExpression(
17838 E: PermExpr, Result: &PermVal, CanFold: AllowFoldKind::Allow);
17839 bool IsValid = PermEvalExpr.isUsable();
17840 if (IsValid)
17841 PermExpr = PermEvalExpr.get();
17842
17843 if (IsValid && (PermVal < 1 || NumLoops < PermVal)) {
17844 SourceRange ExprRange(PermEvalExpr.get()->getBeginLoc(),
17845 PermEvalExpr.get()->getEndLoc());
17846 Diag(Loc: PermEvalExpr.get()->getExprLoc(),
17847 DiagID: diag::err_omp_interchange_permutation_value_range)
17848 << NumLoops << ExprRange;
17849 IsValid = false;
17850 }
17851
17852 if (!PermExpr->isInstantiationDependent() && !IsValid)
17853 PermExpr = nullptr;
17854 }
17855
17856 return OMPPermutationClause::Create(C: getASTContext(), StartLoc, LParenLoc,
17857 EndLoc, Args: SanitizedPermExprs);
17858}
17859
17860OMPClause *SemaOpenMP::ActOnOpenMPFullClause(SourceLocation StartLoc,
17861 SourceLocation EndLoc) {
17862 return OMPFullClause::Create(C: getASTContext(), StartLoc, EndLoc);
17863}
17864
17865OMPClause *SemaOpenMP::ActOnOpenMPPartialClause(Expr *FactorExpr,
17866 SourceLocation StartLoc,
17867 SourceLocation LParenLoc,
17868 SourceLocation EndLoc) {
17869 if (FactorExpr) {
17870 // If an argument is specified, it must be a constant (or an unevaluated
17871 // template expression).
17872 ExprResult FactorResult = VerifyPositiveIntegerConstantInClause(
17873 E: FactorExpr, CKind: OMPC_partial, /*StrictlyPositive=*/true);
17874 if (FactorResult.isInvalid())
17875 return nullptr;
17876 FactorExpr = FactorResult.get();
17877 }
17878
17879 return OMPPartialClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
17880 Factor: FactorExpr);
17881}
17882
17883OMPClause *SemaOpenMP::ActOnOpenMPLoopRangeClause(
17884 Expr *First, Expr *Count, SourceLocation StartLoc, SourceLocation LParenLoc,
17885 SourceLocation FirstLoc, SourceLocation CountLoc, SourceLocation EndLoc) {
17886
17887 // OpenMP [6.0, Restrictions]
17888 // First and Count must be integer expressions with positive value
17889 ExprResult FirstVal =
17890 VerifyPositiveIntegerConstantInClause(E: First, CKind: OMPC_looprange);
17891 if (FirstVal.isInvalid())
17892 First = nullptr;
17893
17894 ExprResult CountVal =
17895 VerifyPositiveIntegerConstantInClause(E: Count, CKind: OMPC_looprange);
17896 if (CountVal.isInvalid())
17897 Count = nullptr;
17898
17899 // OpenMP [6.0, Restrictions]
17900 // first + count - 1 must not evaluate to a value greater than the
17901 // loop sequence length of the associated canonical loop sequence.
17902 // This check must be performed afterwards due to the delayed
17903 // parsing and computation of the associated loop sequence
17904 return OMPLoopRangeClause::Create(C: getASTContext(), StartLoc, LParenLoc,
17905 FirstLoc, CountLoc, EndLoc, First, Count);
17906}
17907
17908OMPClause *SemaOpenMP::ActOnOpenMPAlignClause(Expr *A, SourceLocation StartLoc,
17909 SourceLocation LParenLoc,
17910 SourceLocation EndLoc) {
17911 ExprResult AlignVal;
17912 AlignVal = VerifyPositiveIntegerConstantInClause(E: A, CKind: OMPC_align);
17913 if (AlignVal.isInvalid())
17914 return nullptr;
17915 return OMPAlignClause::Create(C: getASTContext(), A: AlignVal.get(), StartLoc,
17916 LParenLoc, EndLoc);
17917}
17918
17919OMPClause *SemaOpenMP::ActOnOpenMPSingleExprWithArgClause(
17920 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
17921 SourceLocation StartLoc, SourceLocation LParenLoc,
17922 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
17923 SourceLocation EndLoc) {
17924 OMPClause *Res = nullptr;
17925 switch (Kind) {
17926 case OMPC_schedule: {
17927 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
17928 assert(Argument.size() == NumberOfElements &&
17929 ArgumentLoc.size() == NumberOfElements);
17930 Res = ActOnOpenMPScheduleClause(
17931 M1: static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
17932 M2: static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
17933 Kind: static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), ChunkSize: Expr,
17934 StartLoc, LParenLoc, M1Loc: ArgumentLoc[Modifier1], M2Loc: ArgumentLoc[Modifier2],
17935 KindLoc: ArgumentLoc[ScheduleKind], CommaLoc: DelimLoc, EndLoc);
17936 break;
17937 }
17938 case OMPC_if:
17939 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
17940 Res = ActOnOpenMPIfClause(NameModifier: static_cast<OpenMPDirectiveKind>(Argument.back()),
17941 Condition: Expr, StartLoc, LParenLoc, NameModifierLoc: ArgumentLoc.back(),
17942 ColonLoc: DelimLoc, EndLoc);
17943 break;
17944 case OMPC_dist_schedule:
17945 Res = ActOnOpenMPDistScheduleClause(
17946 Kind: static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), ChunkSize: Expr,
17947 StartLoc, LParenLoc, KindLoc: ArgumentLoc.back(), CommaLoc: DelimLoc, EndLoc);
17948 break;
17949 case OMPC_default:
17950 enum { DefaultModifier, DefaultVarCategory };
17951 Res = ActOnOpenMPDefaultClause(
17952 M: static_cast<llvm::omp::DefaultKind>(Argument[DefaultModifier]),
17953 MLoc: ArgumentLoc[DefaultModifier],
17954 VCKind: static_cast<OpenMPDefaultClauseVariableCategory>(
17955 Argument[DefaultVarCategory]),
17956 VCKindLoc: ArgumentLoc[DefaultVarCategory], StartLoc, LParenLoc, EndLoc);
17957 break;
17958 case OMPC_defaultmap:
17959 enum { Modifier, DefaultmapKind };
17960 Res = ActOnOpenMPDefaultmapClause(
17961 M: static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
17962 Kind: static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
17963 StartLoc, LParenLoc, MLoc: ArgumentLoc[Modifier], KindLoc: ArgumentLoc[DefaultmapKind],
17964 EndLoc);
17965 break;
17966 case OMPC_order:
17967 enum { OrderModifier, OrderKind };
17968 Res = ActOnOpenMPOrderClause(
17969 Modifier: static_cast<OpenMPOrderClauseModifier>(Argument[OrderModifier]),
17970 Kind: static_cast<OpenMPOrderClauseKind>(Argument[OrderKind]), StartLoc,
17971 LParenLoc, MLoc: ArgumentLoc[OrderModifier], KindLoc: ArgumentLoc[OrderKind], EndLoc);
17972 break;
17973 case OMPC_device:
17974 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
17975 Res = ActOnOpenMPDeviceClause(
17976 Modifier: static_cast<OpenMPDeviceClauseModifier>(Argument.back()), Device: Expr,
17977 StartLoc, LParenLoc, ModifierLoc: ArgumentLoc.back(), EndLoc);
17978 break;
17979 case OMPC_grainsize:
17980 assert(Argument.size() == 1 && ArgumentLoc.size() == 1 &&
17981 "Modifier for grainsize clause and its location are expected.");
17982 Res = ActOnOpenMPGrainsizeClause(
17983 Modifier: static_cast<OpenMPGrainsizeClauseModifier>(Argument.back()), Size: Expr,
17984 StartLoc, LParenLoc, ModifierLoc: ArgumentLoc.back(), EndLoc);
17985 break;
17986 case OMPC_num_tasks:
17987 assert(Argument.size() == 1 && ArgumentLoc.size() == 1 &&
17988 "Modifier for num_tasks clause and its location are expected.");
17989 Res = ActOnOpenMPNumTasksClause(
17990 Modifier: static_cast<OpenMPNumTasksClauseModifier>(Argument.back()), NumTasks: Expr,
17991 StartLoc, LParenLoc, ModifierLoc: ArgumentLoc.back(), EndLoc);
17992 break;
17993 case OMPC_dyn_groupprivate: {
17994 enum { Modifier1, Modifier2, NumberOfElements };
17995 assert(Argument.size() == NumberOfElements &&
17996 ArgumentLoc.size() == NumberOfElements &&
17997 "Modifiers for dyn_groupprivate clause and their locations are "
17998 "expected.");
17999 Res = ActOnOpenMPDynGroupprivateClause(
18000 M1: static_cast<OpenMPDynGroupprivateClauseModifier>(Argument[Modifier1]),
18001 M2: static_cast<OpenMPDynGroupprivateClauseFallbackModifier>(
18002 Argument[Modifier2]),
18003 Size: Expr, StartLoc, LParenLoc, M1Loc: ArgumentLoc[Modifier1],
18004 M2Loc: ArgumentLoc[Modifier2], EndLoc);
18005 break;
18006 }
18007 case OMPC_num_threads:
18008 assert(Argument.size() == 1 && ArgumentLoc.size() == 1 &&
18009 "Modifier for num_threads clause and its location are expected.");
18010 Res = ActOnOpenMPNumThreadsClause(
18011 Modifier: static_cast<OpenMPNumThreadsClauseModifier>(Argument.back()), NumThreads: Expr,
18012 StartLoc, LParenLoc, ModifierLoc: ArgumentLoc.back(), EndLoc);
18013 break;
18014 case OMPC_final:
18015 case OMPC_safelen:
18016 case OMPC_simdlen:
18017 case OMPC_sizes:
18018 case OMPC_allocator:
18019 case OMPC_collapse:
18020 case OMPC_proc_bind:
18021 case OMPC_private:
18022 case OMPC_firstprivate:
18023 case OMPC_lastprivate:
18024 case OMPC_shared:
18025 case OMPC_reduction:
18026 case OMPC_task_reduction:
18027 case OMPC_in_reduction:
18028 case OMPC_linear:
18029 case OMPC_aligned:
18030 case OMPC_copyin:
18031 case OMPC_copyprivate:
18032 case OMPC_ordered:
18033 case OMPC_nowait:
18034 case OMPC_untied:
18035 case OMPC_mergeable:
18036 case OMPC_threadprivate:
18037 case OMPC_groupprivate:
18038 case OMPC_allocate:
18039 case OMPC_flush:
18040 case OMPC_depobj:
18041 case OMPC_read:
18042 case OMPC_write:
18043 case OMPC_update:
18044 case OMPC_capture:
18045 case OMPC_compare:
18046 case OMPC_seq_cst:
18047 case OMPC_acq_rel:
18048 case OMPC_acquire:
18049 case OMPC_release:
18050 case OMPC_relaxed:
18051 case OMPC_depend:
18052 case OMPC_threads:
18053 case OMPC_simd:
18054 case OMPC_map:
18055 case OMPC_num_teams:
18056 case OMPC_thread_limit:
18057 case OMPC_priority:
18058 case OMPC_nogroup:
18059 case OMPC_hint:
18060 case OMPC_unknown:
18061 case OMPC_uniform:
18062 case OMPC_to:
18063 case OMPC_from:
18064 case OMPC_use_device_ptr:
18065 case OMPC_use_device_addr:
18066 case OMPC_is_device_ptr:
18067 case OMPC_has_device_addr:
18068 case OMPC_unified_address:
18069 case OMPC_unified_shared_memory:
18070 case OMPC_reverse_offload:
18071 case OMPC_dynamic_allocators:
18072 case OMPC_atomic_default_mem_order:
18073 case OMPC_self_maps:
18074 case OMPC_device_type:
18075 case OMPC_match:
18076 case OMPC_nontemporal:
18077 case OMPC_at:
18078 case OMPC_severity:
18079 case OMPC_message:
18080 case OMPC_destroy:
18081 case OMPC_novariants:
18082 case OMPC_nocontext:
18083 case OMPC_detach:
18084 case OMPC_inclusive:
18085 case OMPC_exclusive:
18086 case OMPC_uses_allocators:
18087 case OMPC_affinity:
18088 case OMPC_when:
18089 case OMPC_bind:
18090 default:
18091 llvm_unreachable("Clause is not allowed.");
18092 }
18093 return Res;
18094}
18095
18096static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
18097 OpenMPScheduleClauseModifier M2,
18098 SourceLocation M1Loc, SourceLocation M2Loc) {
18099 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
18100 SmallVector<unsigned, 2> Excluded;
18101 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
18102 Excluded.push_back(Elt: M2);
18103 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
18104 Excluded.push_back(Elt: OMPC_SCHEDULE_MODIFIER_monotonic);
18105 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
18106 Excluded.push_back(Elt: OMPC_SCHEDULE_MODIFIER_nonmonotonic);
18107 S.Diag(Loc: M1Loc, DiagID: diag::err_omp_unexpected_clause_value)
18108 << getListOfPossibleValues(K: OMPC_schedule,
18109 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
18110 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
18111 Exclude: Excluded)
18112 << getOpenMPClauseNameForDiag(C: OMPC_schedule);
18113 return true;
18114 }
18115 return false;
18116}
18117
18118OMPClause *SemaOpenMP::ActOnOpenMPScheduleClause(
18119 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
18120 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
18121 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
18122 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
18123 if (checkScheduleModifiers(S&: SemaRef, M1, M2, M1Loc, M2Loc) ||
18124 checkScheduleModifiers(S&: SemaRef, M1: M2, M2: M1, M1Loc: M2Loc, M2Loc: M1Loc))
18125 return nullptr;
18126 // OpenMP, 2.7.1, Loop Construct, Restrictions
18127 // Either the monotonic modifier or the nonmonotonic modifier can be specified
18128 // but not both.
18129 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
18130 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
18131 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
18132 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
18133 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
18134 Diag(Loc: M2Loc, DiagID: diag::err_omp_unexpected_schedule_modifier)
18135 << getOpenMPSimpleClauseTypeName(Kind: OMPC_schedule, Type: M2)
18136 << getOpenMPSimpleClauseTypeName(Kind: OMPC_schedule, Type: M1);
18137 return nullptr;
18138 }
18139 if (Kind == OMPC_SCHEDULE_unknown) {
18140 std::string Values;
18141 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
18142 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
18143 Values = getListOfPossibleValues(K: OMPC_schedule, /*First=*/0,
18144 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
18145 Exclude);
18146 } else {
18147 Values = getListOfPossibleValues(K: OMPC_schedule, /*First=*/0,
18148 /*Last=*/OMPC_SCHEDULE_unknown);
18149 }
18150 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
18151 << Values << getOpenMPClauseNameForDiag(C: OMPC_schedule);
18152 return nullptr;
18153 }
18154 // OpenMP, 2.7.1, Loop Construct, Restrictions
18155 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
18156 // schedule(guided).
18157 // OpenMP 5.0 does not have this restriction.
18158 if (getLangOpts().OpenMP < 50 &&
18159 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
18160 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
18161 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
18162 Diag(Loc: M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
18163 DiagID: diag::err_omp_schedule_nonmonotonic_static);
18164 return nullptr;
18165 }
18166 Expr *ValExpr = ChunkSize;
18167 Stmt *HelperValStmt = nullptr;
18168 if (ChunkSize) {
18169 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
18170 !ChunkSize->isInstantiationDependent() &&
18171 !ChunkSize->containsUnexpandedParameterPack()) {
18172 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
18173 ExprResult Val =
18174 PerformOpenMPImplicitIntegerConversion(Loc: ChunkSizeLoc, Op: ChunkSize);
18175 if (Val.isInvalid())
18176 return nullptr;
18177
18178 ValExpr = Val.get();
18179
18180 // OpenMP [2.7.1, Restrictions]
18181 // chunk_size must be a loop invariant integer expression with a positive
18182 // value.
18183 if (std::optional<llvm::APSInt> Result =
18184 ValExpr->getIntegerConstantExpr(Ctx: getASTContext())) {
18185 if (Result->isSigned() && !Result->isStrictlyPositive()) {
18186 Diag(Loc: ChunkSizeLoc, DiagID: diag::err_omp_negative_expression_in_clause)
18187 << "schedule" << 1 << ChunkSize->getSourceRange();
18188 return nullptr;
18189 }
18190 } else if (getOpenMPCaptureRegionForClause(
18191 DSAStack->getCurrentDirective(), CKind: OMPC_schedule,
18192 OpenMPVersion: getLangOpts().OpenMP) != OMPD_unknown &&
18193 !SemaRef.CurContext->isDependentContext()) {
18194 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
18195 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
18196 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
18197 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
18198 }
18199 }
18200 }
18201
18202 return new (getASTContext())
18203 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
18204 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
18205}
18206
18207OMPClause *SemaOpenMP::ActOnOpenMPClause(OpenMPClauseKind Kind,
18208 SourceLocation StartLoc,
18209 SourceLocation EndLoc) {
18210 OMPClause *Res = nullptr;
18211 switch (Kind) {
18212 case OMPC_ordered:
18213 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
18214 break;
18215 case OMPC_nowait:
18216 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc,
18217 /*LParenLoc=*/SourceLocation(),
18218 /*Condition=*/nullptr);
18219 break;
18220 case OMPC_untied:
18221 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
18222 break;
18223 case OMPC_mergeable:
18224 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
18225 break;
18226 case OMPC_read:
18227 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
18228 break;
18229 case OMPC_write:
18230 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
18231 break;
18232 case OMPC_update:
18233 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
18234 break;
18235 case OMPC_capture:
18236 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
18237 break;
18238 case OMPC_compare:
18239 Res = ActOnOpenMPCompareClause(StartLoc, EndLoc);
18240 break;
18241 case OMPC_fail:
18242 Res = ActOnOpenMPFailClause(StartLoc, EndLoc);
18243 break;
18244 case OMPC_seq_cst:
18245 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
18246 break;
18247 case OMPC_acq_rel:
18248 Res = ActOnOpenMPAcqRelClause(StartLoc, EndLoc);
18249 break;
18250 case OMPC_acquire:
18251 Res = ActOnOpenMPAcquireClause(StartLoc, EndLoc);
18252 break;
18253 case OMPC_release:
18254 Res = ActOnOpenMPReleaseClause(StartLoc, EndLoc);
18255 break;
18256 case OMPC_relaxed:
18257 Res = ActOnOpenMPRelaxedClause(StartLoc, EndLoc);
18258 break;
18259 case OMPC_weak:
18260 Res = ActOnOpenMPWeakClause(StartLoc, EndLoc);
18261 break;
18262 case OMPC_threads:
18263 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
18264 break;
18265 case OMPC_simd:
18266 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
18267 break;
18268 case OMPC_nogroup:
18269 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
18270 break;
18271 case OMPC_unified_address:
18272 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
18273 break;
18274 case OMPC_unified_shared_memory:
18275 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
18276 break;
18277 case OMPC_reverse_offload:
18278 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc);
18279 break;
18280 case OMPC_dynamic_allocators:
18281 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
18282 break;
18283 case OMPC_self_maps:
18284 Res = ActOnOpenMPSelfMapsClause(StartLoc, EndLoc);
18285 break;
18286 case OMPC_destroy:
18287 Res = ActOnOpenMPDestroyClause(/*InteropVar=*/nullptr, StartLoc,
18288 /*LParenLoc=*/SourceLocation(),
18289 /*VarLoc=*/SourceLocation(), EndLoc);
18290 break;
18291 case OMPC_full:
18292 Res = ActOnOpenMPFullClause(StartLoc, EndLoc);
18293 break;
18294 case OMPC_partial:
18295 Res = ActOnOpenMPPartialClause(FactorExpr: nullptr, StartLoc, /*LParenLoc=*/{}, EndLoc);
18296 break;
18297 case OMPC_ompx_bare:
18298 Res = ActOnOpenMPXBareClause(StartLoc, EndLoc);
18299 break;
18300 case OMPC_if:
18301 case OMPC_final:
18302 case OMPC_num_threads:
18303 case OMPC_safelen:
18304 case OMPC_simdlen:
18305 case OMPC_sizes:
18306 case OMPC_allocator:
18307 case OMPC_collapse:
18308 case OMPC_schedule:
18309 case OMPC_private:
18310 case OMPC_firstprivate:
18311 case OMPC_lastprivate:
18312 case OMPC_shared:
18313 case OMPC_reduction:
18314 case OMPC_task_reduction:
18315 case OMPC_in_reduction:
18316 case OMPC_linear:
18317 case OMPC_aligned:
18318 case OMPC_copyin:
18319 case OMPC_copyprivate:
18320 case OMPC_default:
18321 case OMPC_proc_bind:
18322 case OMPC_threadprivate:
18323 case OMPC_groupprivate:
18324 case OMPC_allocate:
18325 case OMPC_flush:
18326 case OMPC_depobj:
18327 case OMPC_depend:
18328 case OMPC_device:
18329 case OMPC_map:
18330 case OMPC_num_teams:
18331 case OMPC_thread_limit:
18332 case OMPC_priority:
18333 case OMPC_grainsize:
18334 case OMPC_num_tasks:
18335 case OMPC_hint:
18336 case OMPC_dist_schedule:
18337 case OMPC_defaultmap:
18338 case OMPC_unknown:
18339 case OMPC_uniform:
18340 case OMPC_to:
18341 case OMPC_from:
18342 case OMPC_use_device_ptr:
18343 case OMPC_use_device_addr:
18344 case OMPC_is_device_ptr:
18345 case OMPC_has_device_addr:
18346 case OMPC_atomic_default_mem_order:
18347 case OMPC_device_type:
18348 case OMPC_match:
18349 case OMPC_nontemporal:
18350 case OMPC_order:
18351 case OMPC_at:
18352 case OMPC_severity:
18353 case OMPC_message:
18354 case OMPC_novariants:
18355 case OMPC_nocontext:
18356 case OMPC_detach:
18357 case OMPC_inclusive:
18358 case OMPC_exclusive:
18359 case OMPC_uses_allocators:
18360 case OMPC_affinity:
18361 case OMPC_when:
18362 case OMPC_ompx_dyn_cgroup_mem:
18363 case OMPC_dyn_groupprivate:
18364 default:
18365 llvm_unreachable("Clause is not allowed.");
18366 }
18367 return Res;
18368}
18369
18370OMPClause *SemaOpenMP::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
18371 SourceLocation EndLoc,
18372 SourceLocation LParenLoc,
18373 Expr *Condition) {
18374 Expr *ValExpr = Condition;
18375 if (Condition && LParenLoc.isValid()) {
18376 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
18377 !Condition->isInstantiationDependent() &&
18378 !Condition->containsUnexpandedParameterPack()) {
18379 ExprResult Val = SemaRef.CheckBooleanCondition(Loc: StartLoc, E: Condition);
18380 if (Val.isInvalid())
18381 return nullptr;
18382
18383 ValExpr = Val.get();
18384 }
18385 }
18386 DSAStack->setNowaitRegion();
18387 return new (getASTContext())
18388 OMPNowaitClause(ValExpr, StartLoc, LParenLoc, EndLoc);
18389}
18390
18391OMPClause *SemaOpenMP::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
18392 SourceLocation EndLoc) {
18393 DSAStack->setUntiedRegion();
18394 return new (getASTContext()) OMPUntiedClause(StartLoc, EndLoc);
18395}
18396
18397OMPClause *SemaOpenMP::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
18398 SourceLocation EndLoc) {
18399 return new (getASTContext()) OMPMergeableClause(StartLoc, EndLoc);
18400}
18401
18402OMPClause *SemaOpenMP::ActOnOpenMPReadClause(SourceLocation StartLoc,
18403 SourceLocation EndLoc) {
18404 return new (getASTContext()) OMPReadClause(StartLoc, EndLoc);
18405}
18406
18407OMPClause *SemaOpenMP::ActOnOpenMPWriteClause(SourceLocation StartLoc,
18408 SourceLocation EndLoc) {
18409 return new (getASTContext()) OMPWriteClause(StartLoc, EndLoc);
18410}
18411
18412OMPClause *SemaOpenMP::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
18413 SourceLocation EndLoc) {
18414 return OMPUpdateClause::Create(C: getASTContext(), StartLoc, EndLoc);
18415}
18416
18417OMPClause *SemaOpenMP::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
18418 SourceLocation EndLoc) {
18419 return new (getASTContext()) OMPCaptureClause(StartLoc, EndLoc);
18420}
18421
18422OMPClause *SemaOpenMP::ActOnOpenMPCompareClause(SourceLocation StartLoc,
18423 SourceLocation EndLoc) {
18424 return new (getASTContext()) OMPCompareClause(StartLoc, EndLoc);
18425}
18426
18427OMPClause *SemaOpenMP::ActOnOpenMPFailClause(SourceLocation StartLoc,
18428 SourceLocation EndLoc) {
18429 return new (getASTContext()) OMPFailClause(StartLoc, EndLoc);
18430}
18431
18432OMPClause *SemaOpenMP::ActOnOpenMPFailClause(OpenMPClauseKind Parameter,
18433 SourceLocation KindLoc,
18434 SourceLocation StartLoc,
18435 SourceLocation LParenLoc,
18436 SourceLocation EndLoc) {
18437
18438 if (!checkFailClauseParameter(FailClauseParameter: Parameter)) {
18439 Diag(Loc: KindLoc, DiagID: diag::err_omp_atomic_fail_wrong_or_no_clauses);
18440 return nullptr;
18441 }
18442 return new (getASTContext())
18443 OMPFailClause(Parameter, KindLoc, StartLoc, LParenLoc, EndLoc);
18444}
18445
18446OMPClause *SemaOpenMP::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
18447 SourceLocation EndLoc) {
18448 return new (getASTContext()) OMPSeqCstClause(StartLoc, EndLoc);
18449}
18450
18451OMPClause *SemaOpenMP::ActOnOpenMPAcqRelClause(SourceLocation StartLoc,
18452 SourceLocation EndLoc) {
18453 return new (getASTContext()) OMPAcqRelClause(StartLoc, EndLoc);
18454}
18455
18456OMPClause *SemaOpenMP::ActOnOpenMPAcquireClause(SourceLocation StartLoc,
18457 SourceLocation EndLoc) {
18458 return new (getASTContext()) OMPAcquireClause(StartLoc, EndLoc);
18459}
18460
18461OMPClause *SemaOpenMP::ActOnOpenMPReleaseClause(SourceLocation StartLoc,
18462 SourceLocation EndLoc) {
18463 return new (getASTContext()) OMPReleaseClause(StartLoc, EndLoc);
18464}
18465
18466OMPClause *SemaOpenMP::ActOnOpenMPRelaxedClause(SourceLocation StartLoc,
18467 SourceLocation EndLoc) {
18468 return new (getASTContext()) OMPRelaxedClause(StartLoc, EndLoc);
18469}
18470
18471OMPClause *SemaOpenMP::ActOnOpenMPWeakClause(SourceLocation StartLoc,
18472 SourceLocation EndLoc) {
18473 return new (getASTContext()) OMPWeakClause(StartLoc, EndLoc);
18474}
18475
18476OMPClause *SemaOpenMP::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
18477 SourceLocation EndLoc) {
18478 return new (getASTContext()) OMPThreadsClause(StartLoc, EndLoc);
18479}
18480
18481OMPClause *SemaOpenMP::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
18482 SourceLocation EndLoc) {
18483 return new (getASTContext()) OMPSIMDClause(StartLoc, EndLoc);
18484}
18485
18486OMPClause *SemaOpenMP::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
18487 SourceLocation EndLoc) {
18488 return new (getASTContext()) OMPNogroupClause(StartLoc, EndLoc);
18489}
18490
18491OMPClause *SemaOpenMP::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
18492 SourceLocation EndLoc) {
18493 return new (getASTContext()) OMPUnifiedAddressClause(StartLoc, EndLoc);
18494}
18495
18496OMPClause *
18497SemaOpenMP::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
18498 SourceLocation EndLoc) {
18499 return new (getASTContext()) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
18500}
18501
18502OMPClause *SemaOpenMP::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc,
18503 SourceLocation EndLoc) {
18504 return new (getASTContext()) OMPReverseOffloadClause(StartLoc, EndLoc);
18505}
18506
18507OMPClause *
18508SemaOpenMP::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
18509 SourceLocation EndLoc) {
18510 return new (getASTContext()) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
18511}
18512
18513OMPClause *SemaOpenMP::ActOnOpenMPSelfMapsClause(SourceLocation StartLoc,
18514 SourceLocation EndLoc) {
18515 return new (getASTContext()) OMPSelfMapsClause(StartLoc, EndLoc);
18516}
18517
18518StmtResult
18519SemaOpenMP::ActOnOpenMPInteropDirective(ArrayRef<OMPClause *> Clauses,
18520 SourceLocation StartLoc,
18521 SourceLocation EndLoc) {
18522
18523 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions]
18524 // At least one action-clause must appear on a directive.
18525 if (!hasClauses(Clauses, K: OMPC_init, ClauseTypes: OMPC_use, ClauseTypes: OMPC_destroy, ClauseTypes: OMPC_nowait)) {
18526 unsigned OMPVersion = getLangOpts().OpenMP;
18527 StringRef Expected = "'init', 'use', 'destroy', or 'nowait'";
18528 Diag(Loc: StartLoc, DiagID: diag::err_omp_no_clause_for_directive)
18529 << Expected << getOpenMPDirectiveName(D: OMPD_interop, Ver: OMPVersion);
18530 return StmtError();
18531 }
18532
18533 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions]
18534 // A depend clause can only appear on the directive if a targetsync
18535 // interop-type is present or the interop-var was initialized with
18536 // the targetsync interop-type.
18537
18538 // If there is any 'init' clause diagnose if there is no 'init' clause with
18539 // interop-type of 'targetsync'. Cases involving other directives cannot be
18540 // diagnosed.
18541 const OMPDependClause *DependClause = nullptr;
18542 bool HasInitClause = false;
18543 bool IsTargetSync = false;
18544 for (const OMPClause *C : Clauses) {
18545 if (IsTargetSync)
18546 break;
18547 if (const auto *InitClause = dyn_cast<OMPInitClause>(Val: C)) {
18548 HasInitClause = true;
18549 if (InitClause->getIsTargetSync())
18550 IsTargetSync = true;
18551 } else if (const auto *DC = dyn_cast<OMPDependClause>(Val: C)) {
18552 DependClause = DC;
18553 }
18554 }
18555 if (DependClause && HasInitClause && !IsTargetSync) {
18556 Diag(Loc: DependClause->getBeginLoc(), DiagID: diag::err_omp_interop_bad_depend_clause);
18557 return StmtError();
18558 }
18559
18560 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions]
18561 // Each interop-var may be specified for at most one action-clause of each
18562 // interop construct.
18563 llvm::SmallPtrSet<const ValueDecl *, 4> InteropVars;
18564 for (OMPClause *C : Clauses) {
18565 OpenMPClauseKind ClauseKind = C->getClauseKind();
18566 std::pair<ValueDecl *, bool> DeclResult;
18567 SourceLocation ELoc;
18568 SourceRange ERange;
18569
18570 if (ClauseKind == OMPC_init) {
18571 auto *E = cast<OMPInitClause>(Val: C)->getInteropVar();
18572 DeclResult = getPrivateItem(S&: SemaRef, RefExpr&: E, ELoc, ERange);
18573 } else if (ClauseKind == OMPC_use) {
18574 auto *E = cast<OMPUseClause>(Val: C)->getInteropVar();
18575 DeclResult = getPrivateItem(S&: SemaRef, RefExpr&: E, ELoc, ERange);
18576 } else if (ClauseKind == OMPC_destroy) {
18577 auto *E = cast<OMPDestroyClause>(Val: C)->getInteropVar();
18578 DeclResult = getPrivateItem(S&: SemaRef, RefExpr&: E, ELoc, ERange);
18579 }
18580
18581 if (DeclResult.first) {
18582 if (!InteropVars.insert(Ptr: DeclResult.first).second) {
18583 Diag(Loc: ELoc, DiagID: diag::err_omp_interop_var_multiple_actions)
18584 << DeclResult.first;
18585 return StmtError();
18586 }
18587 }
18588 }
18589
18590 return OMPInteropDirective::Create(C: getASTContext(), StartLoc, EndLoc,
18591 Clauses);
18592}
18593
18594static bool isValidInteropVariable(Sema &SemaRef, Expr *InteropVarExpr,
18595 SourceLocation VarLoc,
18596 OpenMPClauseKind Kind) {
18597 SourceLocation ELoc;
18598 SourceRange ERange;
18599 Expr *RefExpr = InteropVarExpr;
18600 auto Res = getPrivateItem(S&: SemaRef, RefExpr, ELoc, ERange,
18601 /*AllowArraySection=*/false,
18602 /*AllowAssumedSizeArray=*/false,
18603 /*DiagType=*/"omp_interop_t");
18604
18605 if (Res.second) {
18606 // It will be analyzed later.
18607 return true;
18608 }
18609
18610 if (!Res.first)
18611 return false;
18612
18613 // Interop variable should be of type omp_interop_t.
18614 bool HasError = false;
18615 QualType InteropType;
18616 LookupResult Result(SemaRef, &SemaRef.Context.Idents.get(Name: "omp_interop_t"),
18617 VarLoc, Sema::LookupOrdinaryName);
18618 if (SemaRef.LookupName(R&: Result, S: SemaRef.getCurScope())) {
18619 NamedDecl *ND = Result.getFoundDecl();
18620 if (const auto *TD = dyn_cast<TypeDecl>(Val: ND)) {
18621 InteropType = QualType(TD->getTypeForDecl(), 0);
18622 } else {
18623 HasError = true;
18624 }
18625 } else {
18626 HasError = true;
18627 }
18628
18629 if (HasError) {
18630 SemaRef.Diag(Loc: VarLoc, DiagID: diag::err_omp_implied_type_not_found)
18631 << "omp_interop_t";
18632 return false;
18633 }
18634
18635 QualType VarType = InteropVarExpr->getType().getUnqualifiedType();
18636 if (!SemaRef.Context.hasSameType(T1: InteropType, T2: VarType)) {
18637 SemaRef.Diag(Loc: VarLoc, DiagID: diag::err_omp_interop_variable_wrong_type);
18638 return false;
18639 }
18640
18641 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions]
18642 // The interop-var passed to init or destroy must be non-const.
18643 if ((Kind == OMPC_init || Kind == OMPC_destroy) &&
18644 isConstNotMutableType(SemaRef, Type: InteropVarExpr->getType())) {
18645 SemaRef.Diag(Loc: VarLoc, DiagID: diag::err_omp_interop_variable_expected)
18646 << /*non-const*/ 1;
18647 return false;
18648 }
18649 return true;
18650}
18651
18652OMPClause *SemaOpenMP::ActOnOpenMPInitClause(
18653 Expr *InteropVar, OMPInteropInfo &InteropInfo, SourceLocation StartLoc,
18654 SourceLocation LParenLoc, SourceLocation VarLoc, SourceLocation EndLoc) {
18655
18656 if (!isValidInteropVariable(SemaRef, InteropVarExpr: InteropVar, VarLoc, Kind: OMPC_init))
18657 return nullptr;
18658
18659 // Check prefer_type values. These foreign-runtime-id values are either
18660 // string literals or constant integral expressions.
18661 for (const Expr *E : InteropInfo.PreferTypes) {
18662 if (E->isValueDependent() || E->isTypeDependent() ||
18663 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
18664 continue;
18665 if (E->isIntegerConstantExpr(Ctx: getASTContext()))
18666 continue;
18667 if (isa<StringLiteral>(Val: E))
18668 continue;
18669 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_interop_prefer_type);
18670 return nullptr;
18671 }
18672
18673 return OMPInitClause::Create(C: getASTContext(), InteropVar, InteropInfo,
18674 StartLoc, LParenLoc, VarLoc, EndLoc);
18675}
18676
18677OMPClause *SemaOpenMP::ActOnOpenMPUseClause(Expr *InteropVar,
18678 SourceLocation StartLoc,
18679 SourceLocation LParenLoc,
18680 SourceLocation VarLoc,
18681 SourceLocation EndLoc) {
18682
18683 if (!isValidInteropVariable(SemaRef, InteropVarExpr: InteropVar, VarLoc, Kind: OMPC_use))
18684 return nullptr;
18685
18686 return new (getASTContext())
18687 OMPUseClause(InteropVar, StartLoc, LParenLoc, VarLoc, EndLoc);
18688}
18689
18690OMPClause *SemaOpenMP::ActOnOpenMPDestroyClause(Expr *InteropVar,
18691 SourceLocation StartLoc,
18692 SourceLocation LParenLoc,
18693 SourceLocation VarLoc,
18694 SourceLocation EndLoc) {
18695 if (!InteropVar && getLangOpts().OpenMP >= 52 &&
18696 DSAStack->getCurrentDirective() == OMPD_depobj) {
18697 unsigned OMPVersion = getLangOpts().OpenMP;
18698 Diag(Loc: StartLoc, DiagID: diag::err_omp_expected_clause_argument)
18699 << getOpenMPClauseNameForDiag(C: OMPC_destroy)
18700 << getOpenMPDirectiveName(D: OMPD_depobj, Ver: OMPVersion);
18701 return nullptr;
18702 }
18703 if (InteropVar &&
18704 !isValidInteropVariable(SemaRef, InteropVarExpr: InteropVar, VarLoc, Kind: OMPC_destroy))
18705 return nullptr;
18706
18707 return new (getASTContext())
18708 OMPDestroyClause(InteropVar, StartLoc, LParenLoc, VarLoc, EndLoc);
18709}
18710
18711OMPClause *SemaOpenMP::ActOnOpenMPNovariantsClause(Expr *Condition,
18712 SourceLocation StartLoc,
18713 SourceLocation LParenLoc,
18714 SourceLocation EndLoc) {
18715 Expr *ValExpr = Condition;
18716 Stmt *HelperValStmt = nullptr;
18717 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
18718 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
18719 !Condition->isInstantiationDependent() &&
18720 !Condition->containsUnexpandedParameterPack()) {
18721 ExprResult Val = SemaRef.CheckBooleanCondition(Loc: StartLoc, E: Condition);
18722 if (Val.isInvalid())
18723 return nullptr;
18724
18725 ValExpr = SemaRef.MakeFullExpr(Arg: Val.get()).get();
18726
18727 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
18728 CaptureRegion = getOpenMPCaptureRegionForClause(DKind, CKind: OMPC_novariants,
18729 OpenMPVersion: getLangOpts().OpenMP);
18730 if (CaptureRegion != OMPD_unknown &&
18731 !SemaRef.CurContext->isDependentContext()) {
18732 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
18733 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
18734 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
18735 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
18736 }
18737 }
18738
18739 return new (getASTContext()) OMPNovariantsClause(
18740 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
18741}
18742
18743OMPClause *SemaOpenMP::ActOnOpenMPNocontextClause(Expr *Condition,
18744 SourceLocation StartLoc,
18745 SourceLocation LParenLoc,
18746 SourceLocation EndLoc) {
18747 Expr *ValExpr = Condition;
18748 Stmt *HelperValStmt = nullptr;
18749 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
18750 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
18751 !Condition->isInstantiationDependent() &&
18752 !Condition->containsUnexpandedParameterPack()) {
18753 ExprResult Val = SemaRef.CheckBooleanCondition(Loc: StartLoc, E: Condition);
18754 if (Val.isInvalid())
18755 return nullptr;
18756
18757 ValExpr = SemaRef.MakeFullExpr(Arg: Val.get()).get();
18758
18759 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
18760 CaptureRegion = getOpenMPCaptureRegionForClause(DKind, CKind: OMPC_nocontext,
18761 OpenMPVersion: getLangOpts().OpenMP);
18762 if (CaptureRegion != OMPD_unknown &&
18763 !SemaRef.CurContext->isDependentContext()) {
18764 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
18765 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
18766 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
18767 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
18768 }
18769 }
18770
18771 return new (getASTContext()) OMPNocontextClause(
18772 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
18773}
18774
18775OMPClause *SemaOpenMP::ActOnOpenMPFilterClause(Expr *ThreadID,
18776 SourceLocation StartLoc,
18777 SourceLocation LParenLoc,
18778 SourceLocation EndLoc) {
18779 Expr *ValExpr = ThreadID;
18780 Stmt *HelperValStmt = nullptr;
18781
18782 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
18783 OpenMPDirectiveKind CaptureRegion =
18784 getOpenMPCaptureRegionForClause(DKind, CKind: OMPC_filter, OpenMPVersion: getLangOpts().OpenMP);
18785 if (CaptureRegion != OMPD_unknown &&
18786 !SemaRef.CurContext->isDependentContext()) {
18787 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
18788 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
18789 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
18790 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
18791 }
18792
18793 return new (getASTContext()) OMPFilterClause(
18794 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
18795}
18796
18797OMPClause *SemaOpenMP::ActOnOpenMPVarListClause(OpenMPClauseKind Kind,
18798 ArrayRef<Expr *> VarList,
18799 const OMPVarListLocTy &Locs,
18800 OpenMPVarListDataTy &Data) {
18801 SourceLocation StartLoc = Locs.StartLoc;
18802 SourceLocation LParenLoc = Locs.LParenLoc;
18803 SourceLocation EndLoc = Locs.EndLoc;
18804 OMPClause *Res = nullptr;
18805 int ExtraModifier = Data.ExtraModifier;
18806 int OriginalSharingModifier = Data.OriginalSharingModifier;
18807 SourceLocation ExtraModifierLoc = Data.ExtraModifierLoc;
18808 SourceLocation ColonLoc = Data.ColonLoc;
18809 switch (Kind) {
18810 case OMPC_private:
18811 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
18812 break;
18813 case OMPC_firstprivate:
18814 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
18815 break;
18816 case OMPC_lastprivate:
18817 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LASTPRIVATE_unknown &&
18818 "Unexpected lastprivate modifier.");
18819 Res = ActOnOpenMPLastprivateClause(
18820 VarList, LPKind: static_cast<OpenMPLastprivateModifier>(ExtraModifier),
18821 LPKindLoc: ExtraModifierLoc, ColonLoc, StartLoc, LParenLoc, EndLoc);
18822 break;
18823 case OMPC_shared:
18824 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
18825 break;
18826 case OMPC_reduction:
18827 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_REDUCTION_unknown &&
18828 "Unexpected lastprivate modifier.");
18829 Res = ActOnOpenMPReductionClause(
18830 VarList,
18831 Modifiers: OpenMPVarListDataTy::OpenMPReductionClauseModifiers(
18832 ExtraModifier, OriginalSharingModifier),
18833 StartLoc, LParenLoc, ModifierLoc: ExtraModifierLoc, ColonLoc, EndLoc,
18834 ReductionIdScopeSpec&: Data.ReductionOrMapperIdScopeSpec, ReductionId: Data.ReductionOrMapperId);
18835 break;
18836 case OMPC_task_reduction:
18837 Res = ActOnOpenMPTaskReductionClause(
18838 VarList, StartLoc, LParenLoc, ColonLoc, EndLoc,
18839 ReductionIdScopeSpec&: Data.ReductionOrMapperIdScopeSpec, ReductionId: Data.ReductionOrMapperId);
18840 break;
18841 case OMPC_in_reduction:
18842 Res = ActOnOpenMPInReductionClause(
18843 VarList, StartLoc, LParenLoc, ColonLoc, EndLoc,
18844 ReductionIdScopeSpec&: Data.ReductionOrMapperIdScopeSpec, ReductionId: Data.ReductionOrMapperId);
18845 break;
18846 case OMPC_linear:
18847 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LINEAR_unknown &&
18848 "Unexpected linear modifier.");
18849 Res = ActOnOpenMPLinearClause(
18850 VarList, Step: Data.DepModOrTailExpr, StartLoc, LParenLoc,
18851 LinKind: static_cast<OpenMPLinearClauseKind>(ExtraModifier), LinLoc: ExtraModifierLoc,
18852 ColonLoc, StepModifierLoc: Data.StepModifierLoc, EndLoc);
18853 break;
18854 case OMPC_aligned:
18855 Res = ActOnOpenMPAlignedClause(VarList, Alignment: Data.DepModOrTailExpr, StartLoc,
18856 LParenLoc, ColonLoc, EndLoc);
18857 break;
18858 case OMPC_copyin:
18859 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
18860 break;
18861 case OMPC_copyprivate:
18862 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
18863 break;
18864 case OMPC_flush:
18865 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
18866 break;
18867 case OMPC_depend:
18868 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_DEPEND_unknown &&
18869 "Unexpected depend modifier.");
18870 Res = ActOnOpenMPDependClause(
18871 Data: {.DepKind: static_cast<OpenMPDependClauseKind>(ExtraModifier), .DepLoc: ExtraModifierLoc,
18872 .ColonLoc: ColonLoc, .OmpAllMemoryLoc: Data.OmpAllMemoryLoc},
18873 DepModifier: Data.DepModOrTailExpr, VarList, StartLoc, LParenLoc, EndLoc);
18874 break;
18875 case OMPC_map:
18876 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_MAP_unknown &&
18877 "Unexpected map modifier.");
18878 Res = ActOnOpenMPMapClause(
18879 IteratorModifier: Data.IteratorExpr, MapTypeModifiers: Data.MapTypeModifiers, MapTypeModifiersLoc: Data.MapTypeModifiersLoc,
18880 MapperIdScopeSpec&: Data.ReductionOrMapperIdScopeSpec, MapperId&: Data.ReductionOrMapperId,
18881 MapType: static_cast<OpenMPMapClauseKind>(ExtraModifier), IsMapTypeImplicit: Data.IsMapTypeImplicit,
18882 MapLoc: ExtraModifierLoc, ColonLoc, VarList, Locs);
18883 break;
18884 case OMPC_to:
18885 Res = ActOnOpenMPToClause(
18886 MotionModifiers: Data.MotionModifiers, MotionModifiersLoc: Data.MotionModifiersLoc, IteratorModifier: Data.IteratorExpr,
18887 MapperIdScopeSpec&: Data.ReductionOrMapperIdScopeSpec, MapperId&: Data.ReductionOrMapperId, ColonLoc,
18888 VarList, Locs);
18889 break;
18890 case OMPC_from:
18891 Res = ActOnOpenMPFromClause(
18892 MotionModifiers: Data.MotionModifiers, MotionModifiersLoc: Data.MotionModifiersLoc, IteratorModifier: Data.IteratorExpr,
18893 MapperIdScopeSpec&: Data.ReductionOrMapperIdScopeSpec, MapperId&: Data.ReductionOrMapperId, ColonLoc,
18894 VarList, Locs);
18895 break;
18896 case OMPC_use_device_ptr:
18897 assert(0 <= Data.ExtraModifier &&
18898 Data.ExtraModifier <= OMPC_USE_DEVICE_PTR_FALLBACK_unknown &&
18899 "Unexpected use_device_ptr fallback modifier.");
18900 Res = ActOnOpenMPUseDevicePtrClause(
18901 VarList, Locs,
18902 FallbackModifier: static_cast<OpenMPUseDevicePtrFallbackModifier>(Data.ExtraModifier),
18903 FallbackModifierLoc: Data.ExtraModifierLoc);
18904 break;
18905 case OMPC_use_device_addr:
18906 Res = ActOnOpenMPUseDeviceAddrClause(VarList, Locs);
18907 break;
18908 case OMPC_is_device_ptr:
18909 Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs);
18910 break;
18911 case OMPC_has_device_addr:
18912 Res = ActOnOpenMPHasDeviceAddrClause(VarList, Locs);
18913 break;
18914 case OMPC_allocate: {
18915 OpenMPAllocateClauseModifier Modifier1 = OMPC_ALLOCATE_unknown;
18916 OpenMPAllocateClauseModifier Modifier2 = OMPC_ALLOCATE_unknown;
18917 SourceLocation Modifier1Loc, Modifier2Loc;
18918 if (!Data.AllocClauseModifiers.empty()) {
18919 assert(Data.AllocClauseModifiers.size() <= 2 &&
18920 "More allocate modifiers than expected");
18921 Modifier1 = Data.AllocClauseModifiers[0];
18922 Modifier1Loc = Data.AllocClauseModifiersLoc[0];
18923 if (Data.AllocClauseModifiers.size() == 2) {
18924 Modifier2 = Data.AllocClauseModifiers[1];
18925 Modifier2Loc = Data.AllocClauseModifiersLoc[1];
18926 }
18927 }
18928 Res = ActOnOpenMPAllocateClause(
18929 Allocator: Data.DepModOrTailExpr, Alignment: Data.AllocateAlignment, FirstModifier: Modifier1, FirstModifierLoc: Modifier1Loc,
18930 SecondModifier: Modifier2, SecondModifierLoc: Modifier2Loc, VarList, StartLoc, ColonLoc: LParenLoc, LParenLoc: ColonLoc,
18931 EndLoc);
18932 break;
18933 }
18934 case OMPC_nontemporal:
18935 Res = ActOnOpenMPNontemporalClause(VarList, StartLoc, LParenLoc, EndLoc);
18936 break;
18937 case OMPC_inclusive:
18938 Res = ActOnOpenMPInclusiveClause(VarList, StartLoc, LParenLoc, EndLoc);
18939 break;
18940 case OMPC_exclusive:
18941 Res = ActOnOpenMPExclusiveClause(VarList, StartLoc, LParenLoc, EndLoc);
18942 break;
18943 case OMPC_affinity:
18944 Res = ActOnOpenMPAffinityClause(StartLoc, LParenLoc, ColonLoc, EndLoc,
18945 Modifier: Data.DepModOrTailExpr, Locators: VarList);
18946 break;
18947 case OMPC_doacross:
18948 Res = ActOnOpenMPDoacrossClause(
18949 DepType: static_cast<OpenMPDoacrossClauseModifier>(ExtraModifier),
18950 DepLoc: ExtraModifierLoc, ColonLoc, VarList, StartLoc, LParenLoc, EndLoc);
18951 break;
18952 case OMPC_num_teams:
18953 Res = ActOnOpenMPNumTeamsClause(VarList, StartLoc, LParenLoc, EndLoc);
18954 break;
18955 case OMPC_thread_limit:
18956 Res = ActOnOpenMPThreadLimitClause(VarList, StartLoc, LParenLoc, EndLoc);
18957 break;
18958 case OMPC_if:
18959 case OMPC_depobj:
18960 case OMPC_final:
18961 case OMPC_num_threads:
18962 case OMPC_safelen:
18963 case OMPC_simdlen:
18964 case OMPC_sizes:
18965 case OMPC_allocator:
18966 case OMPC_collapse:
18967 case OMPC_default:
18968 case OMPC_proc_bind:
18969 case OMPC_schedule:
18970 case OMPC_ordered:
18971 case OMPC_nowait:
18972 case OMPC_untied:
18973 case OMPC_mergeable:
18974 case OMPC_threadprivate:
18975 case OMPC_groupprivate:
18976 case OMPC_read:
18977 case OMPC_write:
18978 case OMPC_update:
18979 case OMPC_capture:
18980 case OMPC_compare:
18981 case OMPC_seq_cst:
18982 case OMPC_acq_rel:
18983 case OMPC_acquire:
18984 case OMPC_release:
18985 case OMPC_relaxed:
18986 case OMPC_device:
18987 case OMPC_threads:
18988 case OMPC_simd:
18989 case OMPC_priority:
18990 case OMPC_grainsize:
18991 case OMPC_nogroup:
18992 case OMPC_num_tasks:
18993 case OMPC_hint:
18994 case OMPC_dist_schedule:
18995 case OMPC_defaultmap:
18996 case OMPC_unknown:
18997 case OMPC_uniform:
18998 case OMPC_unified_address:
18999 case OMPC_unified_shared_memory:
19000 case OMPC_reverse_offload:
19001 case OMPC_dynamic_allocators:
19002 case OMPC_atomic_default_mem_order:
19003 case OMPC_self_maps:
19004 case OMPC_device_type:
19005 case OMPC_match:
19006 case OMPC_order:
19007 case OMPC_at:
19008 case OMPC_severity:
19009 case OMPC_message:
19010 case OMPC_destroy:
19011 case OMPC_novariants:
19012 case OMPC_nocontext:
19013 case OMPC_detach:
19014 case OMPC_uses_allocators:
19015 case OMPC_when:
19016 case OMPC_bind:
19017 default:
19018 llvm_unreachable("Clause is not allowed.");
19019 }
19020 return Res;
19021}
19022
19023ExprResult SemaOpenMP::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
19024 ExprObjectKind OK,
19025 SourceLocation Loc) {
19026 ExprResult Res = SemaRef.BuildDeclRefExpr(
19027 D: Capture, Ty: Capture->getType().getNonReferenceType(), VK: VK_LValue, Loc);
19028 if (!Res.isUsable())
19029 return ExprError();
19030 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
19031 Res = SemaRef.CreateBuiltinUnaryOp(OpLoc: Loc, Opc: UO_Deref, InputExpr: Res.get());
19032 if (!Res.isUsable())
19033 return ExprError();
19034 }
19035 if (VK != VK_LValue && Res.get()->isGLValue()) {
19036 Res = SemaRef.DefaultLvalueConversion(E: Res.get());
19037 if (!Res.isUsable())
19038 return ExprError();
19039 }
19040 return Res;
19041}
19042
19043OMPClause *SemaOpenMP::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
19044 SourceLocation StartLoc,
19045 SourceLocation LParenLoc,
19046 SourceLocation EndLoc) {
19047 SmallVector<Expr *, 8> Vars;
19048 SmallVector<Expr *, 8> PrivateCopies;
19049 unsigned OMPVersion = getLangOpts().OpenMP;
19050 bool IsImplicitClause =
19051 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
19052 for (Expr *RefExpr : VarList) {
19053 assert(RefExpr && "NULL expr in OpenMP private clause.");
19054 SourceLocation ELoc;
19055 SourceRange ERange;
19056 Expr *SimpleRefExpr = RefExpr;
19057 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
19058 if (Res.second) {
19059 // It will be analyzed later.
19060 Vars.push_back(Elt: RefExpr);
19061 PrivateCopies.push_back(Elt: nullptr);
19062 }
19063 ValueDecl *D = Res.first;
19064 if (!D)
19065 continue;
19066
19067 QualType Type = D->getType();
19068 auto *VD = dyn_cast<VarDecl>(Val: D);
19069
19070 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
19071 // A variable that appears in a private clause must not have an incomplete
19072 // type or a reference type.
19073 if (SemaRef.RequireCompleteType(Loc: ELoc, T: Type,
19074 DiagID: diag::err_omp_private_incomplete_type))
19075 continue;
19076 Type = Type.getNonReferenceType();
19077
19078 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
19079 // A variable that is privatized must not have a const-qualified type
19080 // unless it is of class type with a mutable member. This restriction does
19081 // not apply to the firstprivate clause.
19082 //
19083 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions]
19084 // A variable that appears in a private clause must not have a
19085 // const-qualified type unless it is of class type with a mutable member.
19086 if (rejectConstNotMutableType(SemaRef, D, Type, CKind: OMPC_private, ELoc))
19087 continue;
19088
19089 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
19090 // in a Construct]
19091 // Variables with the predetermined data-sharing attributes may not be
19092 // listed in data-sharing attributes clauses, except for the cases
19093 // listed below. For these exceptions only, listing a predetermined
19094 // variable in a data-sharing attribute clause is allowed and overrides
19095 // the variable's predetermined data-sharing attributes.
19096 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
19097 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
19098 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
19099 << getOpenMPClauseNameForDiag(C: DVar.CKind)
19100 << getOpenMPClauseNameForDiag(C: OMPC_private);
19101 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19102 continue;
19103 }
19104
19105 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
19106 // Variably modified types are not supported for tasks.
19107 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
19108 isOpenMPTaskingDirective(Kind: CurrDir)) {
19109 Diag(Loc: ELoc, DiagID: diag::err_omp_variably_modified_type_not_supported)
19110 << getOpenMPClauseNameForDiag(C: OMPC_private) << Type
19111 << getOpenMPDirectiveName(D: CurrDir, Ver: OMPVersion);
19112 bool IsDecl = !VD || VD->isThisDeclarationADefinition(getASTContext()) ==
19113 VarDecl::DeclarationOnly;
19114 Diag(Loc: D->getLocation(),
19115 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
19116 << D;
19117 continue;
19118 }
19119
19120 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
19121 // A list item cannot appear in both a map clause and a data-sharing
19122 // attribute clause on the same construct
19123 //
19124 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
19125 // A list item cannot appear in both a map clause and a data-sharing
19126 // attribute clause on the same construct unless the construct is a
19127 // combined construct.
19128 if ((getLangOpts().OpenMP <= 45 &&
19129 isOpenMPTargetExecutionDirective(DKind: CurrDir)) ||
19130 CurrDir == OMPD_target) {
19131 OpenMPClauseKind ConflictKind;
19132 if (DSAStack->checkMappableExprComponentListsForDecl(
19133 VD, /*CurrentRegionOnly=*/true,
19134 Check: [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
19135 OpenMPClauseKind WhereFoundClauseKind) -> bool {
19136 ConflictKind = WhereFoundClauseKind;
19137 return true;
19138 })) {
19139 Diag(Loc: ELoc, DiagID: diag::err_omp_variable_in_given_clause_and_dsa)
19140 << getOpenMPClauseNameForDiag(C: OMPC_private)
19141 << getOpenMPClauseNameForDiag(C: ConflictKind)
19142 << getOpenMPDirectiveName(D: CurrDir, Ver: OMPVersion);
19143 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19144 continue;
19145 }
19146 }
19147
19148 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
19149 // A variable of class type (or array thereof) that appears in a private
19150 // clause requires an accessible, unambiguous default constructor for the
19151 // class type.
19152 // Generate helper private variable and initialize it with the default
19153 // value. The address of the original variable is replaced by the address of
19154 // the new private variable in CodeGen. This new variable is not added to
19155 // IdResolver, so the code in the OpenMP region uses original variable for
19156 // proper diagnostics.
19157 Type = Type.getUnqualifiedType();
19158 VarDecl *VDPrivate =
19159 buildVarDecl(SemaRef, Loc: ELoc, Type, Name: D->getName(),
19160 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr,
19161 OrigRef: VD ? cast<DeclRefExpr>(Val: SimpleRefExpr) : nullptr);
19162 SemaRef.ActOnUninitializedDecl(dcl: VDPrivate);
19163 if (VDPrivate->isInvalidDecl())
19164 continue;
19165 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
19166 S&: SemaRef, D: VDPrivate, Ty: RefExpr->getType().getUnqualifiedType(), Loc: ELoc);
19167
19168 DeclRefExpr *Ref = nullptr;
19169 if (!VD && !SemaRef.CurContext->isDependentContext()) {
19170 auto *FD = dyn_cast<FieldDecl>(Val: D);
19171 VarDecl *VD = FD ? DSAStack->getImplicitFDCapExprDecl(FD) : nullptr;
19172 if (VD)
19173 Ref = buildDeclRefExpr(S&: SemaRef, D: VD, Ty: VD->getType().getNonReferenceType(),
19174 Loc: RefExpr->getExprLoc());
19175 else
19176 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/false);
19177 }
19178 if (!IsImplicitClause)
19179 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_private, PrivateCopy: Ref);
19180 Vars.push_back(Elt: (VD || SemaRef.CurContext->isDependentContext())
19181 ? RefExpr->IgnoreParens()
19182 : Ref);
19183 PrivateCopies.push_back(Elt: VDPrivateRefExpr);
19184 }
19185
19186 if (Vars.empty())
19187 return nullptr;
19188
19189 return OMPPrivateClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
19190 VL: Vars, PrivateVL: PrivateCopies);
19191}
19192
19193OMPClause *SemaOpenMP::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
19194 SourceLocation StartLoc,
19195 SourceLocation LParenLoc,
19196 SourceLocation EndLoc) {
19197 SmallVector<Expr *, 8> Vars;
19198 SmallVector<Expr *, 8> PrivateCopies;
19199 SmallVector<Expr *, 8> Inits;
19200 SmallVector<Decl *, 4> ExprCaptures;
19201 bool IsImplicitClause =
19202 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
19203 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
19204 unsigned OMPVersion = getLangOpts().OpenMP;
19205
19206 for (Expr *RefExpr : VarList) {
19207 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
19208 SourceLocation ELoc;
19209 SourceRange ERange;
19210 Expr *SimpleRefExpr = RefExpr;
19211 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
19212 if (Res.second) {
19213 // It will be analyzed later.
19214 Vars.push_back(Elt: RefExpr);
19215 PrivateCopies.push_back(Elt: nullptr);
19216 Inits.push_back(Elt: nullptr);
19217 }
19218 ValueDecl *D = Res.first;
19219 if (!D)
19220 continue;
19221
19222 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
19223 QualType Type = D->getType();
19224 auto *VD = dyn_cast<VarDecl>(Val: D);
19225
19226 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
19227 // A variable that appears in a private clause must not have an incomplete
19228 // type or a reference type.
19229 if (SemaRef.RequireCompleteType(Loc: ELoc, T: Type,
19230 DiagID: diag::err_omp_firstprivate_incomplete_type))
19231 continue;
19232 Type = Type.getNonReferenceType();
19233
19234 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
19235 // A variable of class type (or array thereof) that appears in a private
19236 // clause requires an accessible, unambiguous copy constructor for the
19237 // class type.
19238 QualType ElemType =
19239 getASTContext().getBaseElementType(QT: Type).getNonReferenceType();
19240
19241 // If an implicit firstprivate variable found it was checked already.
19242 DSAStackTy::DSAVarData TopDVar;
19243 if (!IsImplicitClause) {
19244 DSAStackTy::DSAVarData DVar =
19245 DSAStack->getTopDSA(D, /*FromParent=*/false);
19246 TopDVar = DVar;
19247 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
19248 bool IsConstant = ElemType.isConstant(Ctx: getASTContext());
19249 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
19250 // A list item that specifies a given variable may not appear in more
19251 // than one clause on the same directive, except that a variable may be
19252 // specified in both firstprivate and lastprivate clauses.
19253 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
19254 // A list item may appear in a firstprivate or lastprivate clause but not
19255 // both.
19256 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
19257 (isOpenMPDistributeDirective(DKind: CurrDir) ||
19258 DVar.CKind != OMPC_lastprivate) &&
19259 DVar.RefExpr) {
19260 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
19261 << getOpenMPClauseNameForDiag(C: DVar.CKind)
19262 << getOpenMPClauseNameForDiag(C: OMPC_firstprivate);
19263 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19264 continue;
19265 }
19266
19267 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
19268 // in a Construct]
19269 // Variables with the predetermined data-sharing attributes may not be
19270 // listed in data-sharing attributes clauses, except for the cases
19271 // listed below. For these exceptions only, listing a predetermined
19272 // variable in a data-sharing attribute clause is allowed and overrides
19273 // the variable's predetermined data-sharing attributes.
19274 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
19275 // in a Construct, C/C++, p.2]
19276 // Variables with const-qualified type having no mutable member may be
19277 // listed in a firstprivate clause, even if they are static data members.
19278 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
19279 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
19280 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
19281 << getOpenMPClauseNameForDiag(C: DVar.CKind)
19282 << getOpenMPClauseNameForDiag(C: OMPC_firstprivate);
19283 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19284 continue;
19285 }
19286
19287 // OpenMP [2.9.3.4, Restrictions, p.2]
19288 // A list item that is private within a parallel region must not appear
19289 // in a firstprivate clause on a worksharing construct if any of the
19290 // worksharing regions arising from the worksharing construct ever bind
19291 // to any of the parallel regions arising from the parallel construct.
19292 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
19293 // A list item that is private within a teams region must not appear in a
19294 // firstprivate clause on a distribute construct if any of the distribute
19295 // regions arising from the distribute construct ever bind to any of the
19296 // teams regions arising from the teams construct.
19297 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
19298 // A list item that appears in a reduction clause of a teams construct
19299 // must not appear in a firstprivate clause on a distribute construct if
19300 // any of the distribute regions arising from the distribute construct
19301 // ever bind to any of the teams regions arising from the teams construct.
19302 if ((isOpenMPWorksharingDirective(DKind: CurrDir) ||
19303 isOpenMPDistributeDirective(DKind: CurrDir)) &&
19304 !isOpenMPParallelDirective(DKind: CurrDir) &&
19305 !isOpenMPTeamsDirective(DKind: CurrDir)) {
19306 DVar = DSAStack->getImplicitDSA(D, FromParent: true);
19307 if (DVar.CKind != OMPC_shared &&
19308 (isOpenMPParallelDirective(DKind: DVar.DKind) ||
19309 isOpenMPTeamsDirective(DKind: DVar.DKind) ||
19310 DVar.DKind == OMPD_unknown)) {
19311 Diag(Loc: ELoc, DiagID: diag::err_omp_required_access)
19312 << getOpenMPClauseNameForDiag(C: OMPC_firstprivate)
19313 << getOpenMPClauseNameForDiag(C: OMPC_shared);
19314 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19315 continue;
19316 }
19317 }
19318 // OpenMP [2.9.3.4, Restrictions, p.3]
19319 // A list item that appears in a reduction clause of a parallel construct
19320 // must not appear in a firstprivate clause on a worksharing or task
19321 // construct if any of the worksharing or task regions arising from the
19322 // worksharing or task construct ever bind to any of the parallel regions
19323 // arising from the parallel construct.
19324 // OpenMP [2.9.3.4, Restrictions, p.4]
19325 // A list item that appears in a reduction clause in worksharing
19326 // construct must not appear in a firstprivate clause in a task construct
19327 // encountered during execution of any of the worksharing regions arising
19328 // from the worksharing construct.
19329 if (isOpenMPTaskingDirective(Kind: CurrDir)) {
19330 DVar = DSAStack->hasInnermostDSA(
19331 D,
19332 CPred: [](OpenMPClauseKind C, bool AppliedToPointee) {
19333 return C == OMPC_reduction && !AppliedToPointee;
19334 },
19335 DPred: [](OpenMPDirectiveKind K) {
19336 return isOpenMPParallelDirective(DKind: K) ||
19337 isOpenMPWorksharingDirective(DKind: K) ||
19338 isOpenMPTeamsDirective(DKind: K);
19339 },
19340 /*FromParent=*/true);
19341 if (DVar.CKind == OMPC_reduction &&
19342 (isOpenMPParallelDirective(DKind: DVar.DKind) ||
19343 isOpenMPWorksharingDirective(DKind: DVar.DKind) ||
19344 isOpenMPTeamsDirective(DKind: DVar.DKind))) {
19345 Diag(Loc: ELoc, DiagID: diag::err_omp_parallel_reduction_in_task_firstprivate)
19346 << getOpenMPDirectiveName(D: DVar.DKind, Ver: OMPVersion);
19347 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19348 continue;
19349 }
19350 }
19351
19352 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
19353 // A list item cannot appear in both a map clause and a data-sharing
19354 // attribute clause on the same construct
19355 //
19356 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
19357 // A list item cannot appear in both a map clause and a data-sharing
19358 // attribute clause on the same construct unless the construct is a
19359 // combined construct.
19360 if ((getLangOpts().OpenMP <= 45 &&
19361 isOpenMPTargetExecutionDirective(DKind: CurrDir)) ||
19362 CurrDir == OMPD_target) {
19363 OpenMPClauseKind ConflictKind;
19364 if (DSAStack->checkMappableExprComponentListsForDecl(
19365 VD, /*CurrentRegionOnly=*/true,
19366 Check: [&ConflictKind](
19367 OMPClauseMappableExprCommon::MappableExprComponentListRef,
19368 OpenMPClauseKind WhereFoundClauseKind) {
19369 ConflictKind = WhereFoundClauseKind;
19370 return true;
19371 })) {
19372 Diag(Loc: ELoc, DiagID: diag::err_omp_variable_in_given_clause_and_dsa)
19373 << getOpenMPClauseNameForDiag(C: OMPC_firstprivate)
19374 << getOpenMPClauseNameForDiag(C: ConflictKind)
19375 << getOpenMPDirectiveName(DSAStack->getCurrentDirective(),
19376 Ver: OMPVersion);
19377 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19378 continue;
19379 }
19380 }
19381 }
19382
19383 // Variably modified types are not supported for tasks.
19384 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
19385 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
19386 Diag(Loc: ELoc, DiagID: diag::err_omp_variably_modified_type_not_supported)
19387 << getOpenMPClauseNameForDiag(C: OMPC_firstprivate) << Type
19388 << getOpenMPDirectiveName(DSAStack->getCurrentDirective(),
19389 Ver: OMPVersion);
19390 bool IsDecl = !VD || VD->isThisDeclarationADefinition(getASTContext()) ==
19391 VarDecl::DeclarationOnly;
19392 Diag(Loc: D->getLocation(),
19393 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
19394 << D;
19395 continue;
19396 }
19397
19398 Type = Type.getUnqualifiedType();
19399 VarDecl *VDPrivate =
19400 buildVarDecl(SemaRef, Loc: ELoc, Type, Name: D->getName(),
19401 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr,
19402 OrigRef: VD ? cast<DeclRefExpr>(Val: SimpleRefExpr) : nullptr);
19403 // Generate helper private variable and initialize it with the value of the
19404 // original variable. The address of the original variable is replaced by
19405 // the address of the new private variable in the CodeGen. This new variable
19406 // is not added to IdResolver, so the code in the OpenMP region uses
19407 // original variable for proper diagnostics and variable capturing.
19408 Expr *VDInitRefExpr = nullptr;
19409 // For arrays generate initializer for single element and replace it by the
19410 // original array element in CodeGen.
19411 if (Type->isArrayType()) {
19412 VarDecl *VDInit =
19413 buildVarDecl(SemaRef, Loc: RefExpr->getExprLoc(), Type: ElemType, Name: D->getName());
19414 VDInitRefExpr = buildDeclRefExpr(S&: SemaRef, D: VDInit, Ty: ElemType, Loc: ELoc);
19415 Expr *Init = SemaRef.DefaultLvalueConversion(E: VDInitRefExpr).get();
19416 ElemType = ElemType.getUnqualifiedType();
19417 VarDecl *VDInitTemp = buildVarDecl(SemaRef, Loc: RefExpr->getExprLoc(),
19418 Type: ElemType, Name: ".firstprivate.temp");
19419 InitializedEntity Entity =
19420 InitializedEntity::InitializeVariable(Var: VDInitTemp);
19421 InitializationKind Kind = InitializationKind::CreateCopy(InitLoc: ELoc, EqualLoc: ELoc);
19422
19423 InitializationSequence InitSeq(SemaRef, Entity, Kind, Init);
19424 ExprResult Result = InitSeq.Perform(S&: SemaRef, Entity, Kind, Args: Init);
19425 if (Result.isInvalid())
19426 VDPrivate->setInvalidDecl();
19427 else
19428 VDPrivate->setInit(Result.getAs<Expr>());
19429 // Remove temp variable declaration.
19430 getASTContext().Deallocate(Ptr: VDInitTemp);
19431 } else {
19432 VarDecl *VDInit = buildVarDecl(SemaRef, Loc: RefExpr->getExprLoc(), Type,
19433 Name: ".firstprivate.temp");
19434 VDInitRefExpr = buildDeclRefExpr(S&: SemaRef, D: VDInit, Ty: RefExpr->getType(),
19435 Loc: RefExpr->getExprLoc());
19436 SemaRef.AddInitializerToDecl(
19437 dcl: VDPrivate, init: SemaRef.DefaultLvalueConversion(E: VDInitRefExpr).get(),
19438 /*DirectInit=*/false);
19439 }
19440 if (VDPrivate->isInvalidDecl()) {
19441 if (IsImplicitClause) {
19442 Diag(Loc: RefExpr->getExprLoc(),
19443 DiagID: diag::note_omp_task_predetermined_firstprivate_here);
19444 }
19445 continue;
19446 }
19447 SemaRef.CurContext->addDecl(D: VDPrivate);
19448 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
19449 S&: SemaRef, D: VDPrivate, Ty: RefExpr->getType().getUnqualifiedType(),
19450 Loc: RefExpr->getExprLoc());
19451 DeclRefExpr *Ref = nullptr;
19452 if (!VD && !SemaRef.CurContext->isDependentContext()) {
19453 if (TopDVar.CKind == OMPC_lastprivate) {
19454 Ref = TopDVar.PrivateCopy;
19455 } else {
19456 auto *FD = dyn_cast<FieldDecl>(Val: D);
19457 VarDecl *VD = FD ? DSAStack->getImplicitFDCapExprDecl(FD) : nullptr;
19458 if (VD)
19459 Ref =
19460 buildDeclRefExpr(S&: SemaRef, D: VD, Ty: VD->getType().getNonReferenceType(),
19461 Loc: RefExpr->getExprLoc());
19462 else
19463 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/true);
19464 if (VD || !isOpenMPCapturedDecl(D))
19465 ExprCaptures.push_back(Elt: Ref->getDecl());
19466 }
19467 }
19468 if (!IsImplicitClause)
19469 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_firstprivate, PrivateCopy: Ref);
19470 Vars.push_back(Elt: (VD || SemaRef.CurContext->isDependentContext())
19471 ? RefExpr->IgnoreParens()
19472 : Ref);
19473 PrivateCopies.push_back(Elt: VDPrivateRefExpr);
19474 Inits.push_back(Elt: VDInitRefExpr);
19475 }
19476
19477 if (Vars.empty())
19478 return nullptr;
19479
19480 return OMPFirstprivateClause::Create(
19481 C: getASTContext(), StartLoc, LParenLoc, EndLoc, VL: Vars, PrivateVL: PrivateCopies, InitVL: Inits,
19482 PreInit: buildPreInits(Context&: getASTContext(), PreInits: ExprCaptures));
19483}
19484
19485OMPClause *SemaOpenMP::ActOnOpenMPLastprivateClause(
19486 ArrayRef<Expr *> VarList, OpenMPLastprivateModifier LPKind,
19487 SourceLocation LPKindLoc, SourceLocation ColonLoc, SourceLocation StartLoc,
19488 SourceLocation LParenLoc, SourceLocation EndLoc) {
19489 if (LPKind == OMPC_LASTPRIVATE_unknown && LPKindLoc.isValid()) {
19490 assert(ColonLoc.isValid() && "Colon location must be valid.");
19491 Diag(Loc: LPKindLoc, DiagID: diag::err_omp_unexpected_clause_value)
19492 << getListOfPossibleValues(K: OMPC_lastprivate, /*First=*/0,
19493 /*Last=*/OMPC_LASTPRIVATE_unknown)
19494 << getOpenMPClauseNameForDiag(C: OMPC_lastprivate);
19495 return nullptr;
19496 }
19497
19498 SmallVector<Expr *, 8> Vars;
19499 SmallVector<Expr *, 8> SrcExprs;
19500 SmallVector<Expr *, 8> DstExprs;
19501 SmallVector<Expr *, 8> AssignmentOps;
19502 SmallVector<Decl *, 4> ExprCaptures;
19503 SmallVector<Expr *, 4> ExprPostUpdates;
19504 for (Expr *RefExpr : VarList) {
19505 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
19506 SourceLocation ELoc;
19507 SourceRange ERange;
19508 Expr *SimpleRefExpr = RefExpr;
19509 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
19510 if (Res.second) {
19511 // It will be analyzed later.
19512 Vars.push_back(Elt: RefExpr);
19513 SrcExprs.push_back(Elt: nullptr);
19514 DstExprs.push_back(Elt: nullptr);
19515 AssignmentOps.push_back(Elt: nullptr);
19516 }
19517 ValueDecl *D = Res.first;
19518 if (!D)
19519 continue;
19520
19521 QualType Type = D->getType();
19522 auto *VD = dyn_cast<VarDecl>(Val: D);
19523
19524 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
19525 // A variable that appears in a lastprivate clause must not have an
19526 // incomplete type or a reference type.
19527 if (SemaRef.RequireCompleteType(Loc: ELoc, T: Type,
19528 DiagID: diag::err_omp_lastprivate_incomplete_type))
19529 continue;
19530 Type = Type.getNonReferenceType();
19531
19532 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
19533 // A variable that is privatized must not have a const-qualified type
19534 // unless it is of class type with a mutable member. This restriction does
19535 // not apply to the firstprivate clause.
19536 //
19537 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions]
19538 // A variable that appears in a lastprivate clause must not have a
19539 // const-qualified type unless it is of class type with a mutable member.
19540 if (rejectConstNotMutableType(SemaRef, D, Type, CKind: OMPC_lastprivate, ELoc))
19541 continue;
19542
19543 // OpenMP 5.0 [2.19.4.5 lastprivate Clause, Restrictions]
19544 // A list item that appears in a lastprivate clause with the conditional
19545 // modifier must be a scalar variable.
19546 if (LPKind == OMPC_LASTPRIVATE_conditional && !Type->isScalarType()) {
19547 Diag(Loc: ELoc, DiagID: diag::err_omp_lastprivate_conditional_non_scalar);
19548 bool IsDecl = !VD || VD->isThisDeclarationADefinition(getASTContext()) ==
19549 VarDecl::DeclarationOnly;
19550 Diag(Loc: D->getLocation(),
19551 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
19552 << D;
19553 continue;
19554 }
19555
19556 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
19557 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
19558 // in a Construct]
19559 // Variables with the predetermined data-sharing attributes may not be
19560 // listed in data-sharing attributes clauses, except for the cases
19561 // listed below.
19562 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
19563 // A list item may appear in a firstprivate or lastprivate clause but not
19564 // both.
19565 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
19566 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
19567 (isOpenMPDistributeDirective(DKind: CurrDir) ||
19568 DVar.CKind != OMPC_firstprivate) &&
19569 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
19570 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
19571 << getOpenMPClauseNameForDiag(C: DVar.CKind)
19572 << getOpenMPClauseNameForDiag(C: OMPC_lastprivate);
19573 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19574 continue;
19575 }
19576
19577 // OpenMP [2.14.3.5, Restrictions, p.2]
19578 // A list item that is private within a parallel region, or that appears in
19579 // the reduction clause of a parallel construct, must not appear in a
19580 // lastprivate clause on a worksharing construct if any of the corresponding
19581 // worksharing regions ever binds to any of the corresponding parallel
19582 // regions.
19583 DSAStackTy::DSAVarData TopDVar = DVar;
19584 if (isOpenMPWorksharingDirective(DKind: CurrDir) &&
19585 !isOpenMPParallelDirective(DKind: CurrDir) &&
19586 !isOpenMPTeamsDirective(DKind: CurrDir)) {
19587 DVar = DSAStack->getImplicitDSA(D, FromParent: true);
19588 if (DVar.CKind != OMPC_shared) {
19589 Diag(Loc: ELoc, DiagID: diag::err_omp_required_access)
19590 << getOpenMPClauseNameForDiag(C: OMPC_lastprivate)
19591 << getOpenMPClauseNameForDiag(C: OMPC_shared);
19592 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19593 continue;
19594 }
19595 }
19596
19597 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
19598 // A variable of class type (or array thereof) that appears in a
19599 // lastprivate clause requires an accessible, unambiguous default
19600 // constructor for the class type, unless the list item is also specified
19601 // in a firstprivate clause.
19602 // A variable of class type (or array thereof) that appears in a
19603 // lastprivate clause requires an accessible, unambiguous copy assignment
19604 // operator for the class type.
19605 Type = getASTContext().getBaseElementType(QT: Type).getNonReferenceType();
19606 VarDecl *SrcVD = buildVarDecl(SemaRef, Loc: ERange.getBegin(),
19607 Type: Type.getUnqualifiedType(), Name: ".lastprivate.src",
19608 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
19609 DeclRefExpr *PseudoSrcExpr =
19610 buildDeclRefExpr(S&: SemaRef, D: SrcVD, Ty: Type.getUnqualifiedType(), Loc: ELoc);
19611 VarDecl *DstVD =
19612 buildVarDecl(SemaRef, Loc: ERange.getBegin(), Type, Name: ".lastprivate.dst",
19613 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
19614 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(S&: SemaRef, D: DstVD, Ty: Type, Loc: ELoc);
19615 // For arrays generate assignment operation for single element and replace
19616 // it by the original array element in CodeGen.
19617 ExprResult AssignmentOp = SemaRef.BuildBinOp(/*S=*/nullptr, OpLoc: ELoc, Opc: BO_Assign,
19618 LHSExpr: PseudoDstExpr, RHSExpr: PseudoSrcExpr);
19619 if (AssignmentOp.isInvalid())
19620 continue;
19621 AssignmentOp = SemaRef.ActOnFinishFullExpr(Expr: AssignmentOp.get(), CC: ELoc,
19622 /*DiscardedValue=*/false);
19623 if (AssignmentOp.isInvalid())
19624 continue;
19625
19626 DeclRefExpr *Ref = nullptr;
19627 if (!VD && !SemaRef.CurContext->isDependentContext()) {
19628 if (TopDVar.CKind == OMPC_firstprivate) {
19629 Ref = TopDVar.PrivateCopy;
19630 } else {
19631 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/false);
19632 if (!isOpenMPCapturedDecl(D))
19633 ExprCaptures.push_back(Elt: Ref->getDecl());
19634 }
19635 if ((TopDVar.CKind == OMPC_firstprivate && !TopDVar.PrivateCopy) ||
19636 (!isOpenMPCapturedDecl(D) &&
19637 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
19638 ExprResult RefRes = SemaRef.DefaultLvalueConversion(E: Ref);
19639 if (!RefRes.isUsable())
19640 continue;
19641 ExprResult PostUpdateRes =
19642 SemaRef.BuildBinOp(DSAStack->getCurScope(), OpLoc: ELoc, Opc: BO_Assign,
19643 LHSExpr: SimpleRefExpr, RHSExpr: RefRes.get());
19644 if (!PostUpdateRes.isUsable())
19645 continue;
19646 ExprPostUpdates.push_back(
19647 Elt: SemaRef.IgnoredValueConversions(E: PostUpdateRes.get()).get());
19648 }
19649 }
19650 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_lastprivate, PrivateCopy: Ref);
19651 Vars.push_back(Elt: (VD || SemaRef.CurContext->isDependentContext())
19652 ? RefExpr->IgnoreParens()
19653 : Ref);
19654 SrcExprs.push_back(Elt: PseudoSrcExpr);
19655 DstExprs.push_back(Elt: PseudoDstExpr);
19656 AssignmentOps.push_back(Elt: AssignmentOp.get());
19657 }
19658
19659 if (Vars.empty())
19660 return nullptr;
19661
19662 return OMPLastprivateClause::Create(
19663 C: getASTContext(), StartLoc, LParenLoc, EndLoc, VL: Vars, SrcExprs, DstExprs,
19664 AssignmentOps, LPKind, LPKindLoc, ColonLoc,
19665 PreInit: buildPreInits(Context&: getASTContext(), PreInits: ExprCaptures),
19666 PostUpdate: buildPostUpdate(S&: SemaRef, PostUpdates: ExprPostUpdates));
19667}
19668
19669OMPClause *SemaOpenMP::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
19670 SourceLocation StartLoc,
19671 SourceLocation LParenLoc,
19672 SourceLocation EndLoc) {
19673 SmallVector<Expr *, 8> Vars;
19674 for (Expr *RefExpr : VarList) {
19675 assert(RefExpr && "NULL expr in OpenMP shared clause.");
19676 SourceLocation ELoc;
19677 SourceRange ERange;
19678 Expr *SimpleRefExpr = RefExpr;
19679 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
19680 if (Res.second) {
19681 // It will be analyzed later.
19682 Vars.push_back(Elt: RefExpr);
19683 }
19684 ValueDecl *D = Res.first;
19685 if (!D)
19686 continue;
19687
19688 auto *VD = dyn_cast<VarDecl>(Val: D);
19689 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
19690 // in a Construct]
19691 // Variables with the predetermined data-sharing attributes may not be
19692 // listed in data-sharing attributes clauses, except for the cases
19693 // listed below. For these exceptions only, listing a predetermined
19694 // variable in a data-sharing attribute clause is allowed and overrides
19695 // the variable's predetermined data-sharing attributes.
19696 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
19697 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
19698 DVar.RefExpr) {
19699 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
19700 << getOpenMPClauseNameForDiag(C: DVar.CKind)
19701 << getOpenMPClauseNameForDiag(C: OMPC_shared);
19702 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19703 continue;
19704 }
19705
19706 DeclRefExpr *Ref = nullptr;
19707 if (!VD && isOpenMPCapturedDecl(D) &&
19708 !SemaRef.CurContext->isDependentContext())
19709 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/true);
19710 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_shared, PrivateCopy: Ref);
19711 Vars.push_back(Elt: (VD || !Ref || SemaRef.CurContext->isDependentContext())
19712 ? RefExpr->IgnoreParens()
19713 : Ref);
19714 }
19715
19716 if (Vars.empty())
19717 return nullptr;
19718
19719 return OMPSharedClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
19720 VL: Vars);
19721}
19722
19723namespace {
19724class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
19725 DSAStackTy *Stack;
19726
19727public:
19728 bool VisitDeclRefExpr(DeclRefExpr *E) {
19729 if (auto *VD = dyn_cast<VarDecl>(Val: E->getDecl())) {
19730 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D: VD, /*FromParent=*/false);
19731 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
19732 return false;
19733 if (DVar.CKind != OMPC_unknown)
19734 return true;
19735 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
19736 D: VD,
19737 CPred: [](OpenMPClauseKind C, bool AppliedToPointee, bool) {
19738 return isOpenMPPrivate(Kind: C) && !AppliedToPointee;
19739 },
19740 DPred: [](OpenMPDirectiveKind) { return true; },
19741 /*FromParent=*/true);
19742 return DVarPrivate.CKind != OMPC_unknown;
19743 }
19744 return false;
19745 }
19746 bool VisitStmt(Stmt *S) {
19747 for (Stmt *Child : S->children()) {
19748 if (Child && Visit(S: Child))
19749 return true;
19750 }
19751 return false;
19752 }
19753 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
19754};
19755} // namespace
19756
19757namespace {
19758// Transform MemberExpression for specified FieldDecl of current class to
19759// DeclRefExpr to specified OMPCapturedExprDecl.
19760class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
19761 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
19762 ValueDecl *Field = nullptr;
19763 DeclRefExpr *CapturedExpr = nullptr;
19764
19765public:
19766 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
19767 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
19768
19769 ExprResult TransformMemberExpr(MemberExpr *E) {
19770 if (isa<CXXThisExpr>(Val: E->getBase()->IgnoreParenImpCasts()) &&
19771 E->getMemberDecl() == Field) {
19772 CapturedExpr = buildCapture(S&: SemaRef, D: Field, CaptureExpr: E, /*WithInit=*/false);
19773 return CapturedExpr;
19774 }
19775 return BaseTransform::TransformMemberExpr(E);
19776 }
19777 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
19778};
19779} // namespace
19780
19781template <typename T, typename U>
19782static T filterLookupForUDReductionAndMapper(
19783 SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) {
19784 for (U &Set : Lookups) {
19785 for (auto *D : Set) {
19786 if (T Res = Gen(cast<ValueDecl>(D)))
19787 return Res;
19788 }
19789 }
19790 return T();
19791}
19792
19793static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
19794 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
19795
19796 for (auto *RD : D->redecls()) {
19797 // Don't bother with extra checks if we already know this one isn't visible.
19798 if (RD == D)
19799 continue;
19800
19801 auto ND = cast<NamedDecl>(Val: RD);
19802 if (LookupResult::isVisible(SemaRef, D: ND))
19803 return ND;
19804 }
19805
19806 return nullptr;
19807}
19808
19809static void
19810argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id,
19811 SourceLocation Loc, QualType Ty,
19812 SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
19813 // Find all of the associated namespaces and classes based on the
19814 // arguments we have.
19815 Sema::AssociatedNamespaceSet AssociatedNamespaces;
19816 Sema::AssociatedClassSet AssociatedClasses;
19817 OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
19818 SemaRef.FindAssociatedClassesAndNamespaces(InstantiationLoc: Loc, Args: &OVE, AssociatedNamespaces,
19819 AssociatedClasses);
19820
19821 // C++ [basic.lookup.argdep]p3:
19822 // Let X be the lookup set produced by unqualified lookup (3.4.1)
19823 // and let Y be the lookup set produced by argument dependent
19824 // lookup (defined as follows). If X contains [...] then Y is
19825 // empty. Otherwise Y is the set of declarations found in the
19826 // namespaces associated with the argument types as described
19827 // below. The set of declarations found by the lookup of the name
19828 // is the union of X and Y.
19829 //
19830 // Here, we compute Y and add its members to the overloaded
19831 // candidate set.
19832 for (auto *NS : AssociatedNamespaces) {
19833 // When considering an associated namespace, the lookup is the
19834 // same as the lookup performed when the associated namespace is
19835 // used as a qualifier (3.4.3.2) except that:
19836 //
19837 // -- Any using-directives in the associated namespace are
19838 // ignored.
19839 //
19840 // -- Any namespace-scope friend functions declared in
19841 // associated classes are visible within their respective
19842 // namespaces even if they are not visible during an ordinary
19843 // lookup (11.4).
19844 DeclContext::lookup_result R = NS->lookup(Name: Id.getName());
19845 for (auto *D : R) {
19846 auto *Underlying = D;
19847 if (auto *USD = dyn_cast<UsingShadowDecl>(Val: D))
19848 Underlying = USD->getTargetDecl();
19849
19850 if (!isa<OMPDeclareReductionDecl>(Val: Underlying) &&
19851 !isa<OMPDeclareMapperDecl>(Val: Underlying))
19852 continue;
19853
19854 if (!SemaRef.isVisible(D)) {
19855 D = findAcceptableDecl(SemaRef, D);
19856 if (!D)
19857 continue;
19858 if (auto *USD = dyn_cast<UsingShadowDecl>(Val: D))
19859 Underlying = USD->getTargetDecl();
19860 }
19861 Lookups.emplace_back();
19862 Lookups.back().addDecl(D: Underlying);
19863 }
19864 }
19865}
19866
19867static ExprResult
19868buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
19869 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
19870 const DeclarationNameInfo &ReductionId, QualType Ty,
19871 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
19872 if (ReductionIdScopeSpec.isInvalid())
19873 return ExprError();
19874 SmallVector<UnresolvedSet<8>, 4> Lookups;
19875 if (S) {
19876 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
19877 Lookup.suppressDiagnostics();
19878 while (S && SemaRef.LookupParsedName(R&: Lookup, S, SS: &ReductionIdScopeSpec,
19879 /*ObjectType=*/QualType())) {
19880 NamedDecl *D = Lookup.getRepresentativeDecl();
19881 do {
19882 S = S->getParent();
19883 } while (S && !S->isDeclScope(D));
19884 if (S)
19885 S = S->getParent();
19886 Lookups.emplace_back();
19887 Lookups.back().append(I: Lookup.begin(), E: Lookup.end());
19888 Lookup.clear();
19889 }
19890 } else if (auto *ULE =
19891 cast_or_null<UnresolvedLookupExpr>(Val: UnresolvedReduction)) {
19892 Lookups.push_back(Elt: UnresolvedSet<8>());
19893 Decl *PrevD = nullptr;
19894 for (NamedDecl *D : ULE->decls()) {
19895 if (D == PrevD)
19896 Lookups.push_back(Elt: UnresolvedSet<8>());
19897 else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Val: D))
19898 Lookups.back().addDecl(D: DRD);
19899 PrevD = D;
19900 }
19901 }
19902 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
19903 Ty->isInstantiationDependentType() ||
19904 Ty->containsUnexpandedParameterPack() ||
19905 filterLookupForUDReductionAndMapper<bool>(Lookups, Gen: [](ValueDecl *D) {
19906 return !D->isInvalidDecl() &&
19907 (D->getType()->isDependentType() ||
19908 D->getType()->isInstantiationDependentType() ||
19909 D->getType()->containsUnexpandedParameterPack());
19910 })) {
19911 UnresolvedSet<8> ResSet;
19912 for (const UnresolvedSet<8> &Set : Lookups) {
19913 if (Set.empty())
19914 continue;
19915 ResSet.append(I: Set.begin(), E: Set.end());
19916 // The last item marks the end of all declarations at the specified scope.
19917 ResSet.addDecl(D: Set[Set.size() - 1]);
19918 }
19919 return UnresolvedLookupExpr::Create(
19920 Context: SemaRef.Context, /*NamingClass=*/nullptr,
19921 QualifierLoc: ReductionIdScopeSpec.getWithLocInContext(Context&: SemaRef.Context), NameInfo: ReductionId,
19922 /*ADL=*/RequiresADL: true, Begin: ResSet.begin(), End: ResSet.end(), /*KnownDependent=*/false,
19923 /*KnownInstantiationDependent=*/false);
19924 }
19925 // Lookup inside the classes.
19926 // C++ [over.match.oper]p3:
19927 // For a unary operator @ with an operand of a type whose
19928 // cv-unqualified version is T1, and for a binary operator @ with
19929 // a left operand of a type whose cv-unqualified version is T1 and
19930 // a right operand of a type whose cv-unqualified version is T2,
19931 // three sets of candidate functions, designated member
19932 // candidates, non-member candidates and built-in candidates, are
19933 // constructed as follows:
19934 // -- If T1 is a complete class type or a class currently being
19935 // defined, the set of member candidates is the result of the
19936 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
19937 // the set of member candidates is empty.
19938 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
19939 Lookup.suppressDiagnostics();
19940 if (Ty->isRecordType()) {
19941 // Complete the type if it can be completed.
19942 // If the type is neither complete nor being defined, bail out now.
19943 bool IsComplete = SemaRef.isCompleteType(Loc, T: Ty);
19944 auto *RD = Ty->castAsRecordDecl();
19945 if (IsComplete || RD->isBeingDefined()) {
19946 Lookup.clear();
19947 SemaRef.LookupQualifiedName(R&: Lookup, LookupCtx: RD);
19948 if (Lookup.empty()) {
19949 Lookups.emplace_back();
19950 Lookups.back().append(I: Lookup.begin(), E: Lookup.end());
19951 }
19952 }
19953 }
19954 // Perform ADL.
19955 if (SemaRef.getLangOpts().CPlusPlus)
19956 argumentDependentLookup(SemaRef, Id: ReductionId, Loc, Ty, Lookups);
19957 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
19958 Lookups, Gen: [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
19959 if (!D->isInvalidDecl() &&
19960 SemaRef.Context.hasSameType(T1: D->getType(), T2: Ty))
19961 return D;
19962 return nullptr;
19963 }))
19964 return SemaRef.BuildDeclRefExpr(D: VD, Ty: VD->getType().getNonReferenceType(),
19965 VK: VK_LValue, Loc);
19966 if (SemaRef.getLangOpts().CPlusPlus) {
19967 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
19968 Lookups, Gen: [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
19969 if (!D->isInvalidDecl() &&
19970 SemaRef.IsDerivedFrom(Loc, Derived: Ty, Base: D->getType()) &&
19971 !Ty.isMoreQualifiedThan(other: D->getType(),
19972 Ctx: SemaRef.getASTContext()))
19973 return D;
19974 return nullptr;
19975 })) {
19976 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
19977 /*DetectVirtual=*/false);
19978 if (SemaRef.IsDerivedFrom(Loc, Derived: Ty, Base: VD->getType(), Paths)) {
19979 if (!Paths.isAmbiguous(BaseType: SemaRef.Context.getCanonicalType(
19980 T: VD->getType().getUnqualifiedType()))) {
19981 if (SemaRef.CheckBaseClassAccess(
19982 AccessLoc: Loc, Base: VD->getType(), Derived: Ty, Path: Paths.front(),
19983 /*DiagID=*/0) != Sema::AR_inaccessible) {
19984 SemaRef.BuildBasePathArray(Paths, BasePath);
19985 return SemaRef.BuildDeclRefExpr(
19986 D: VD, Ty: VD->getType().getNonReferenceType(), VK: VK_LValue, Loc);
19987 }
19988 }
19989 }
19990 }
19991 }
19992 if (ReductionIdScopeSpec.isSet()) {
19993 SemaRef.Diag(Loc, DiagID: diag::err_omp_not_resolved_reduction_identifier)
19994 << Ty << Range;
19995 return ExprError();
19996 }
19997 return ExprEmpty();
19998}
19999
20000namespace {
20001/// Data for the reduction-based clauses.
20002struct ReductionData {
20003 /// List of original reduction items.
20004 SmallVector<Expr *, 8> Vars;
20005 /// List of private copies of the reduction items.
20006 SmallVector<Expr *, 8> Privates;
20007 /// LHS expressions for the reduction_op expressions.
20008 SmallVector<Expr *, 8> LHSs;
20009 /// RHS expressions for the reduction_op expressions.
20010 SmallVector<Expr *, 8> RHSs;
20011 /// Reduction operation expression.
20012 SmallVector<Expr *, 8> ReductionOps;
20013 /// inscan copy operation expressions.
20014 SmallVector<Expr *, 8> InscanCopyOps;
20015 /// inscan copy temp array expressions for prefix sums.
20016 SmallVector<Expr *, 8> InscanCopyArrayTemps;
20017 /// inscan copy temp array element expressions for prefix sums.
20018 SmallVector<Expr *, 8> InscanCopyArrayElems;
20019 /// Taskgroup descriptors for the corresponding reduction items in
20020 /// in_reduction clauses.
20021 SmallVector<Expr *, 8> TaskgroupDescriptors;
20022 /// List of captures for clause.
20023 SmallVector<Decl *, 4> ExprCaptures;
20024 /// List of postupdate expressions.
20025 SmallVector<Expr *, 4> ExprPostUpdates;
20026 /// Reduction modifier.
20027 unsigned RedModifier = 0;
20028 /// Original modifier.
20029 unsigned OrigSharingModifier = 0;
20030 /// Private Variable Reduction
20031 SmallVector<bool, 8> IsPrivateVarReduction;
20032 ReductionData() = delete;
20033 /// Reserves required memory for the reduction data.
20034 ReductionData(unsigned Size, unsigned Modifier = 0, unsigned OrgModifier = 0)
20035 : RedModifier(Modifier), OrigSharingModifier(OrgModifier) {
20036 Vars.reserve(N: Size);
20037 Privates.reserve(N: Size);
20038 LHSs.reserve(N: Size);
20039 RHSs.reserve(N: Size);
20040 ReductionOps.reserve(N: Size);
20041 IsPrivateVarReduction.reserve(N: Size);
20042 if (RedModifier == OMPC_REDUCTION_inscan) {
20043 InscanCopyOps.reserve(N: Size);
20044 InscanCopyArrayTemps.reserve(N: Size);
20045 InscanCopyArrayElems.reserve(N: Size);
20046 }
20047 TaskgroupDescriptors.reserve(N: Size);
20048 ExprCaptures.reserve(N: Size);
20049 ExprPostUpdates.reserve(N: Size);
20050 }
20051 /// Stores reduction item and reduction operation only (required for dependent
20052 /// reduction item).
20053 void push(Expr *Item, Expr *ReductionOp) {
20054 Vars.emplace_back(Args&: Item);
20055 Privates.emplace_back(Args: nullptr);
20056 LHSs.emplace_back(Args: nullptr);
20057 RHSs.emplace_back(Args: nullptr);
20058 ReductionOps.emplace_back(Args&: ReductionOp);
20059 IsPrivateVarReduction.emplace_back(Args: false);
20060 TaskgroupDescriptors.emplace_back(Args: nullptr);
20061 if (RedModifier == OMPC_REDUCTION_inscan) {
20062 InscanCopyOps.push_back(Elt: nullptr);
20063 InscanCopyArrayTemps.push_back(Elt: nullptr);
20064 InscanCopyArrayElems.push_back(Elt: nullptr);
20065 }
20066 }
20067 /// Stores reduction data.
20068 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
20069 Expr *TaskgroupDescriptor, Expr *CopyOp, Expr *CopyArrayTemp,
20070 Expr *CopyArrayElem, bool IsPrivate) {
20071 Vars.emplace_back(Args&: Item);
20072 Privates.emplace_back(Args&: Private);
20073 LHSs.emplace_back(Args&: LHS);
20074 RHSs.emplace_back(Args&: RHS);
20075 ReductionOps.emplace_back(Args&: ReductionOp);
20076 TaskgroupDescriptors.emplace_back(Args&: TaskgroupDescriptor);
20077 if (RedModifier == OMPC_REDUCTION_inscan) {
20078 InscanCopyOps.push_back(Elt: CopyOp);
20079 InscanCopyArrayTemps.push_back(Elt: CopyArrayTemp);
20080 InscanCopyArrayElems.push_back(Elt: CopyArrayElem);
20081 } else {
20082 assert(CopyOp == nullptr && CopyArrayTemp == nullptr &&
20083 CopyArrayElem == nullptr &&
20084 "Copy operation must be used for inscan reductions only.");
20085 }
20086 IsPrivateVarReduction.emplace_back(Args&: IsPrivate);
20087 }
20088};
20089} // namespace
20090
20091static bool checkOMPArraySectionConstantForReduction(
20092 ASTContext &Context, const ArraySectionExpr *OASE, bool &SingleElement,
20093 SmallVectorImpl<llvm::APSInt> &ArraySizes) {
20094 const Expr *Length = OASE->getLength();
20095 if (Length == nullptr) {
20096 // For array sections of the form [1:] or [:], we would need to analyze
20097 // the lower bound...
20098 if (OASE->getColonLocFirst().isValid())
20099 return false;
20100
20101 // This is an array subscript which has implicit length 1!
20102 SingleElement = true;
20103 ArraySizes.push_back(Elt: llvm::APSInt::get(X: 1));
20104 } else {
20105 Expr::EvalResult Result;
20106 if (!Length->EvaluateAsInt(Result, Ctx: Context))
20107 return false;
20108
20109 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
20110 SingleElement = (ConstantLengthValue.getSExtValue() == 1);
20111 ArraySizes.push_back(Elt: ConstantLengthValue);
20112 }
20113
20114 // Get the base of this array section and walk up from there.
20115 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
20116
20117 // We require length = 1 for all array sections except the right-most to
20118 // guarantee that the memory region is contiguous and has no holes in it.
20119 while (const auto *TempOASE = dyn_cast<ArraySectionExpr>(Val: Base)) {
20120 Length = TempOASE->getLength();
20121 if (Length == nullptr) {
20122 // For array sections of the form [1:] or [:], we would need to analyze
20123 // the lower bound...
20124 if (OASE->getColonLocFirst().isValid())
20125 return false;
20126
20127 // This is an array subscript which has implicit length 1!
20128 llvm::APSInt ConstantOne = llvm::APSInt::get(X: 1);
20129 ArraySizes.push_back(Elt: ConstantOne);
20130 } else {
20131 Expr::EvalResult Result;
20132 if (!Length->EvaluateAsInt(Result, Ctx: Context))
20133 return false;
20134
20135 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
20136 if (ConstantLengthValue.getSExtValue() != 1)
20137 return false;
20138
20139 ArraySizes.push_back(Elt: ConstantLengthValue);
20140 }
20141 Base = TempOASE->getBase()->IgnoreParenImpCasts();
20142 }
20143
20144 // If we have a single element, we don't need to add the implicit lengths.
20145 if (!SingleElement) {
20146 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Val: Base)) {
20147 // Has implicit length 1!
20148 llvm::APSInt ConstantOne = llvm::APSInt::get(X: 1);
20149 ArraySizes.push_back(Elt: ConstantOne);
20150 Base = TempASE->getBase()->IgnoreParenImpCasts();
20151 }
20152 }
20153
20154 // This array section can be privatized as a single value or as a constant
20155 // sized array.
20156 return true;
20157}
20158
20159static BinaryOperatorKind
20160getRelatedCompoundReductionOp(BinaryOperatorKind BOK) {
20161 if (BOK == BO_Add)
20162 return BO_AddAssign;
20163 if (BOK == BO_Mul)
20164 return BO_MulAssign;
20165 if (BOK == BO_And)
20166 return BO_AndAssign;
20167 if (BOK == BO_Or)
20168 return BO_OrAssign;
20169 if (BOK == BO_Xor)
20170 return BO_XorAssign;
20171 return BOK;
20172}
20173
20174static bool actOnOMPReductionKindClause(
20175 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
20176 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
20177 SourceLocation ColonLoc, SourceLocation EndLoc,
20178 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
20179 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
20180 DeclarationName DN = ReductionId.getName();
20181 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
20182 BinaryOperatorKind BOK = BO_Comma;
20183
20184 ASTContext &Context = S.Context;
20185 // OpenMP [2.14.3.6, reduction clause]
20186 // C
20187 // reduction-identifier is either an identifier or one of the following
20188 // operators: +, -, *, &, |, ^, && and ||
20189 // C++
20190 // reduction-identifier is either an id-expression or one of the following
20191 // operators: +, -, *, &, |, ^, && and ||
20192 switch (OOK) {
20193 case OO_Plus:
20194 BOK = BO_Add;
20195 break;
20196 case OO_Minus:
20197 // Minus(-) operator is not supported in TR11 (OpenMP 6.0). Setting BOK to
20198 // BO_Comma will automatically diagnose it for OpenMP > 52 as not allowed
20199 // reduction identifier.
20200 if (S.LangOpts.OpenMP > 52)
20201 BOK = BO_Comma;
20202 else
20203 BOK = BO_Add;
20204 break;
20205 case OO_Star:
20206 BOK = BO_Mul;
20207 break;
20208 case OO_Amp:
20209 BOK = BO_And;
20210 break;
20211 case OO_Pipe:
20212 BOK = BO_Or;
20213 break;
20214 case OO_Caret:
20215 BOK = BO_Xor;
20216 break;
20217 case OO_AmpAmp:
20218 BOK = BO_LAnd;
20219 break;
20220 case OO_PipePipe:
20221 BOK = BO_LOr;
20222 break;
20223 case OO_New:
20224 case OO_Delete:
20225 case OO_Array_New:
20226 case OO_Array_Delete:
20227 case OO_Slash:
20228 case OO_Percent:
20229 case OO_Tilde:
20230 case OO_Exclaim:
20231 case OO_Equal:
20232 case OO_Less:
20233 case OO_Greater:
20234 case OO_LessEqual:
20235 case OO_GreaterEqual:
20236 case OO_PlusEqual:
20237 case OO_MinusEqual:
20238 case OO_StarEqual:
20239 case OO_SlashEqual:
20240 case OO_PercentEqual:
20241 case OO_CaretEqual:
20242 case OO_AmpEqual:
20243 case OO_PipeEqual:
20244 case OO_LessLess:
20245 case OO_GreaterGreater:
20246 case OO_LessLessEqual:
20247 case OO_GreaterGreaterEqual:
20248 case OO_EqualEqual:
20249 case OO_ExclaimEqual:
20250 case OO_Spaceship:
20251 case OO_PlusPlus:
20252 case OO_MinusMinus:
20253 case OO_Comma:
20254 case OO_ArrowStar:
20255 case OO_Arrow:
20256 case OO_Call:
20257 case OO_Subscript:
20258 case OO_Conditional:
20259 case OO_Coawait:
20260 case NUM_OVERLOADED_OPERATORS:
20261 llvm_unreachable("Unexpected reduction identifier");
20262 case OO_None:
20263 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
20264 if (II->isStr(Str: "max"))
20265 BOK = BO_GT;
20266 else if (II->isStr(Str: "min"))
20267 BOK = BO_LT;
20268 }
20269 break;
20270 }
20271
20272 // OpenMP 5.2, 5.5.5 (see page 627, line 18) reduction Clause, Restrictions
20273 // A reduction clause with the minus (-) operator was deprecated
20274 if (OOK == OO_Minus && S.LangOpts.OpenMP == 52)
20275 S.Diag(Loc: ReductionId.getLoc(), DiagID: diag::warn_omp_minus_in_reduction_deprecated);
20276
20277 SourceRange ReductionIdRange;
20278 if (ReductionIdScopeSpec.isValid())
20279 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
20280 else
20281 ReductionIdRange.setBegin(ReductionId.getBeginLoc());
20282 ReductionIdRange.setEnd(ReductionId.getEndLoc());
20283
20284 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
20285 bool FirstIter = true;
20286 for (Expr *RefExpr : VarList) {
20287 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
20288 // OpenMP [2.1, C/C++]
20289 // A list item is a variable or array section, subject to the restrictions
20290 // specified in Section 2.4 on page 42 and in each of the sections
20291 // describing clauses and directives for which a list appears.
20292 // OpenMP [2.14.3.3, Restrictions, p.1]
20293 // A variable that is part of another variable (as an array or
20294 // structure element) cannot appear in a private clause.
20295 if (!FirstIter && IR != ER)
20296 ++IR;
20297 FirstIter = false;
20298 SourceLocation ELoc;
20299 SourceRange ERange;
20300 bool IsPrivate = false;
20301 Expr *SimpleRefExpr = RefExpr;
20302 auto Res = getPrivateItem(S, RefExpr&: SimpleRefExpr, ELoc, ERange,
20303 /*AllowArraySection=*/true);
20304 if (Res.second) {
20305 // Try to find 'declare reduction' corresponding construct before using
20306 // builtin/overloaded operators.
20307 QualType Type = Context.DependentTy;
20308 CXXCastPath BasePath;
20309 ExprResult DeclareReductionRef = buildDeclareReductionRef(
20310 SemaRef&: S, Loc: ELoc, Range: ERange, S: Stack->getCurScope(), ReductionIdScopeSpec,
20311 ReductionId, Ty: Type, BasePath, UnresolvedReduction: IR == ER ? nullptr : *IR);
20312 Expr *ReductionOp = nullptr;
20313 if (S.CurContext->isDependentContext() &&
20314 (DeclareReductionRef.isUnset() ||
20315 isa<UnresolvedLookupExpr>(Val: DeclareReductionRef.get())))
20316 ReductionOp = DeclareReductionRef.get();
20317 // It will be analyzed later.
20318 RD.push(Item: RefExpr, ReductionOp);
20319 }
20320 ValueDecl *D = Res.first;
20321 if (!D)
20322 continue;
20323
20324 Expr *TaskgroupDescriptor = nullptr;
20325 QualType Type;
20326 auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: RefExpr->IgnoreParens());
20327 auto *OASE = dyn_cast<ArraySectionExpr>(Val: RefExpr->IgnoreParens());
20328 if (ASE) {
20329 Type = ASE->getType().getNonReferenceType();
20330 } else if (OASE) {
20331 QualType BaseType =
20332 ArraySectionExpr::getBaseOriginalType(Base: OASE->getBase());
20333 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
20334 Type = ATy->getElementType();
20335 else
20336 Type = BaseType->getPointeeType();
20337 Type = Type.getNonReferenceType();
20338 } else {
20339 Type = Context.getBaseElementType(QT: D->getType().getNonReferenceType());
20340 }
20341 auto *VD = dyn_cast<VarDecl>(Val: D);
20342
20343 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
20344 // A variable that appears in a private clause must not have an incomplete
20345 // type or a reference type.
20346 if (S.RequireCompleteType(Loc: ELoc, T: D->getType(),
20347 DiagID: diag::err_omp_reduction_incomplete_type))
20348 continue;
20349 // OpenMP [2.14.3.6, reduction clause, Restrictions]
20350 // A list item that appears in a reduction clause must not be
20351 // const-qualified.
20352 if (rejectConstNotMutableType(SemaRef&: S, D, Type, CKind: ClauseKind, ELoc,
20353 /*AcceptIfMutable=*/false, ListItemNotVar: ASE || OASE))
20354 continue;
20355
20356 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
20357 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
20358 // If a list-item is a reference type then it must bind to the same object
20359 // for all threads of the team.
20360 if (!ASE && !OASE) {
20361 if (VD) {
20362 VarDecl *VDDef = VD->getDefinition();
20363 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
20364 DSARefChecker Check(Stack);
20365 if (Check.Visit(S: VDDef->getInit())) {
20366 S.Diag(Loc: ELoc, DiagID: diag::err_omp_reduction_ref_type_arg)
20367 << getOpenMPClauseNameForDiag(C: ClauseKind) << ERange;
20368 S.Diag(Loc: VDDef->getLocation(), DiagID: diag::note_defined_here) << VDDef;
20369 continue;
20370 }
20371 }
20372 }
20373
20374 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
20375 // in a Construct]
20376 // Variables with the predetermined data-sharing attributes may not be
20377 // listed in data-sharing attributes clauses, except for the cases
20378 // listed below. For these exceptions only, listing a predetermined
20379 // variable in a data-sharing attribute clause is allowed and overrides
20380 // the variable's predetermined data-sharing attributes.
20381 // OpenMP [2.14.3.6, Restrictions, p.3]
20382 // Any number of reduction clauses can be specified on the directive,
20383 // but a list item can appear only once in the reduction clauses for that
20384 // directive.
20385 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
20386 if (DVar.CKind == OMPC_reduction) {
20387 S.Diag(Loc: ELoc, DiagID: diag::err_omp_once_referenced)
20388 << getOpenMPClauseNameForDiag(C: ClauseKind);
20389 if (DVar.RefExpr)
20390 S.Diag(Loc: DVar.RefExpr->getExprLoc(), DiagID: diag::note_omp_referenced);
20391 continue;
20392 }
20393 if (DVar.CKind != OMPC_unknown) {
20394 S.Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
20395 << getOpenMPClauseNameForDiag(C: DVar.CKind)
20396 << getOpenMPClauseNameForDiag(C: OMPC_reduction);
20397 reportOriginalDsa(SemaRef&: S, Stack, D, DVar);
20398 continue;
20399 }
20400
20401 // OpenMP [2.14.3.6, Restrictions, p.1]
20402 // A list item that appears in a reduction clause of a worksharing
20403 // construct must be shared in the parallel regions to which any of the
20404 // worksharing regions arising from the worksharing construct bind.
20405
20406 if (S.getLangOpts().OpenMP <= 52 &&
20407 isOpenMPWorksharingDirective(DKind: CurrDir) &&
20408 !isOpenMPParallelDirective(DKind: CurrDir) &&
20409 !isOpenMPTeamsDirective(DKind: CurrDir)) {
20410 DVar = Stack->getImplicitDSA(D, FromParent: true);
20411 if (DVar.CKind != OMPC_shared) {
20412 S.Diag(Loc: ELoc, DiagID: diag::err_omp_required_access)
20413 << getOpenMPClauseNameForDiag(C: OMPC_reduction)
20414 << getOpenMPClauseNameForDiag(C: OMPC_shared);
20415 reportOriginalDsa(SemaRef&: S, Stack, D, DVar);
20416 continue;
20417 }
20418 } else if (isOpenMPWorksharingDirective(DKind: CurrDir) &&
20419 !isOpenMPParallelDirective(DKind: CurrDir) &&
20420 !isOpenMPTeamsDirective(DKind: CurrDir)) {
20421 // OpenMP 6.0 [ 7.6.10 ]
20422 // Support Reduction over private variables with reduction clause.
20423 // A list item in a reduction clause can now be private in the enclosing
20424 // context. For orphaned constructs it is assumed to be shared unless
20425 // the original(private) modifier appears in the clause.
20426 DVar = Stack->getImplicitDSA(D, FromParent: true);
20427 // Determine if the variable should be considered private
20428 IsPrivate = DVar.CKind != OMPC_shared;
20429 bool IsOrphaned = false;
20430 OpenMPDirectiveKind ParentDir = Stack->getParentDirective();
20431 IsOrphaned = ParentDir == OMPD_unknown;
20432 if ((IsOrphaned &&
20433 RD.OrigSharingModifier == OMPC_ORIGINAL_SHARING_private))
20434 IsPrivate = true;
20435 }
20436 } else {
20437 // Threadprivates cannot be shared between threads, so dignose if the base
20438 // is a threadprivate variable.
20439 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
20440 if (DVar.CKind == OMPC_threadprivate) {
20441 S.Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
20442 << getOpenMPClauseNameForDiag(C: DVar.CKind)
20443 << getOpenMPClauseNameForDiag(C: OMPC_reduction);
20444 reportOriginalDsa(SemaRef&: S, Stack, D, DVar);
20445 continue;
20446 }
20447 }
20448
20449 // Try to find 'declare reduction' corresponding construct before using
20450 // builtin/overloaded operators.
20451 CXXCastPath BasePath;
20452 ExprResult DeclareReductionRef = buildDeclareReductionRef(
20453 SemaRef&: S, Loc: ELoc, Range: ERange, S: Stack->getCurScope(), ReductionIdScopeSpec,
20454 ReductionId, Ty: Type, BasePath, UnresolvedReduction: IR == ER ? nullptr : *IR);
20455 if (DeclareReductionRef.isInvalid())
20456 continue;
20457 if (S.CurContext->isDependentContext() &&
20458 (DeclareReductionRef.isUnset() ||
20459 isa<UnresolvedLookupExpr>(Val: DeclareReductionRef.get()))) {
20460 RD.push(Item: RefExpr, ReductionOp: DeclareReductionRef.get());
20461 continue;
20462 }
20463 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
20464 // Not allowed reduction identifier is found.
20465 if (S.LangOpts.OpenMP > 52)
20466 S.Diag(Loc: ReductionId.getBeginLoc(),
20467 DiagID: diag::err_omp_unknown_reduction_identifier_since_omp_6_0)
20468 << Type << ReductionIdRange;
20469 else
20470 S.Diag(Loc: ReductionId.getBeginLoc(),
20471 DiagID: diag::err_omp_unknown_reduction_identifier_prior_omp_6_0)
20472 << Type << ReductionIdRange;
20473 continue;
20474 }
20475
20476 // OpenMP [2.14.3.6, reduction clause, Restrictions]
20477 // The type of a list item that appears in a reduction clause must be valid
20478 // for the reduction-identifier. For a max or min reduction in C, the type
20479 // of the list item must be an allowed arithmetic data type: char, int,
20480 // float, double, or _Bool, possibly modified with long, short, signed, or
20481 // unsigned. For a max or min reduction in C++, the type of the list item
20482 // must be an allowed arithmetic data type: char, wchar_t, int, float,
20483 // double, or bool, possibly modified with long, short, signed, or unsigned.
20484 if (DeclareReductionRef.isUnset()) {
20485 if ((BOK == BO_GT || BOK == BO_LT) &&
20486 !(Type->isScalarType() ||
20487 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
20488 S.Diag(Loc: ELoc, DiagID: diag::err_omp_clause_not_arithmetic_type_arg)
20489 << getOpenMPClauseNameForDiag(C: ClauseKind)
20490 << S.getLangOpts().CPlusPlus;
20491 if (!ASE && !OASE) {
20492 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
20493 VarDecl::DeclarationOnly;
20494 S.Diag(Loc: D->getLocation(),
20495 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
20496 << D;
20497 }
20498 continue;
20499 }
20500 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
20501 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
20502 S.Diag(Loc: ELoc, DiagID: diag::err_omp_clause_floating_type_arg)
20503 << getOpenMPClauseNameForDiag(C: ClauseKind);
20504 if (!ASE && !OASE) {
20505 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
20506 VarDecl::DeclarationOnly;
20507 S.Diag(Loc: D->getLocation(),
20508 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
20509 << D;
20510 }
20511 continue;
20512 }
20513 }
20514
20515 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
20516 VarDecl *LHSVD = buildVarDecl(SemaRef&: S, Loc: ELoc, Type, Name: ".reduction.lhs",
20517 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
20518 VarDecl *RHSVD = buildVarDecl(SemaRef&: S, Loc: ELoc, Type, Name: D->getName(),
20519 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
20520 QualType PrivateTy = Type;
20521
20522 // Try if we can determine constant lengths for all array sections and avoid
20523 // the VLA.
20524 bool ConstantLengthOASE = false;
20525 if (OASE) {
20526 bool SingleElement;
20527 llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
20528 ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
20529 Context, OASE, SingleElement, ArraySizes);
20530
20531 // If we don't have a single element, we must emit a constant array type.
20532 if (ConstantLengthOASE && !SingleElement) {
20533 for (llvm::APSInt &Size : ArraySizes)
20534 PrivateTy = Context.getConstantArrayType(EltTy: PrivateTy, ArySize: Size, SizeExpr: nullptr,
20535 ASM: ArraySizeModifier::Normal,
20536 /*IndexTypeQuals=*/0);
20537 }
20538 }
20539
20540 if ((OASE && !ConstantLengthOASE) ||
20541 (!OASE && !ASE &&
20542 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
20543 if (!Context.getTargetInfo().isVLASupported()) {
20544 if (isOpenMPTargetExecutionDirective(DKind: Stack->getCurrentDirective())) {
20545 S.Diag(Loc: ELoc, DiagID: diag::err_omp_reduction_vla_unsupported) << !!OASE;
20546 S.Diag(Loc: ELoc, DiagID: diag::note_vla_unsupported);
20547 continue;
20548 } else {
20549 S.targetDiag(Loc: ELoc, DiagID: diag::err_omp_reduction_vla_unsupported) << !!OASE;
20550 S.targetDiag(Loc: ELoc, DiagID: diag::note_vla_unsupported);
20551 }
20552 }
20553 // For arrays/array sections only:
20554 // Create pseudo array type for private copy. The size for this array will
20555 // be generated during codegen.
20556 // For array subscripts or single variables Private Ty is the same as Type
20557 // (type of the variable or single array element).
20558 PrivateTy = Context.getVariableArrayType(
20559 EltTy: Type,
20560 NumElts: new (Context)
20561 OpaqueValueExpr(ELoc, Context.getSizeType(), VK_PRValue),
20562 ASM: ArraySizeModifier::Normal, /*IndexTypeQuals=*/0);
20563 } else if (!ASE && !OASE &&
20564 Context.getAsArrayType(T: D->getType().getNonReferenceType())) {
20565 PrivateTy = D->getType().getNonReferenceType();
20566 }
20567 // Private copy.
20568 VarDecl *PrivateVD =
20569 buildVarDecl(SemaRef&: S, Loc: ELoc, Type: PrivateTy, Name: D->getName(),
20570 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr,
20571 OrigRef: VD ? cast<DeclRefExpr>(Val: SimpleRefExpr) : nullptr);
20572 // Add initializer for private variable.
20573 Expr *Init = nullptr;
20574 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, D: LHSVD, Ty: Type, Loc: ELoc);
20575 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, D: RHSVD, Ty: Type, Loc: ELoc);
20576 if (DeclareReductionRef.isUsable()) {
20577 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
20578 auto *DRD = cast<OMPDeclareReductionDecl>(Val: DRDRef->getDecl());
20579 if (DRD->getInitializer()) {
20580 Init = DRDRef;
20581 RHSVD->setInit(DRDRef);
20582 RHSVD->setInitStyle(VarDecl::CallInit);
20583 }
20584 } else {
20585 switch (BOK) {
20586 case BO_Add:
20587 case BO_Xor:
20588 case BO_Or:
20589 case BO_LOr:
20590 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
20591 if (Type->isScalarType() || Type->isAnyComplexType())
20592 Init = S.ActOnIntegerConstant(Loc: ELoc, /*Val=*/0).get();
20593 break;
20594 case BO_Mul:
20595 case BO_LAnd:
20596 if (Type->isScalarType() || Type->isAnyComplexType()) {
20597 // '*' and '&&' reduction ops - initializer is '1'.
20598 Init = S.ActOnIntegerConstant(Loc: ELoc, /*Val=*/1).get();
20599 }
20600 break;
20601 case BO_And: {
20602 // '&' reduction op - initializer is '~0'.
20603 QualType OrigType = Type;
20604 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
20605 Type = ComplexTy->getElementType();
20606 if (Type->isRealFloatingType()) {
20607 llvm::APFloat InitValue = llvm::APFloat::getAllOnesValue(
20608 Semantics: Context.getFloatTypeSemantics(T: Type));
20609 Init = FloatingLiteral::Create(C: Context, V: InitValue, /*isexact=*/true,
20610 Type, L: ELoc);
20611 } else if (Type->isScalarType()) {
20612 uint64_t Size = Context.getTypeSize(T: Type);
20613 QualType IntTy = Context.getIntTypeForBitwidth(DestWidth: Size, /*Signed=*/0);
20614 llvm::APInt InitValue = llvm::APInt::getAllOnes(numBits: Size);
20615 Init = IntegerLiteral::Create(C: Context, V: InitValue, type: IntTy, l: ELoc);
20616 }
20617 if (Init && OrigType->isAnyComplexType()) {
20618 // Init = 0xFFFF + 0xFFFFi;
20619 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
20620 Init = S.CreateBuiltinBinOp(OpLoc: ELoc, Opc: BO_Add, LHSExpr: Init, RHSExpr: Im).get();
20621 }
20622 Type = OrigType;
20623 break;
20624 }
20625 case BO_LT:
20626 case BO_GT: {
20627 // 'min' reduction op - initializer is 'Largest representable number in
20628 // the reduction list item type'.
20629 // 'max' reduction op - initializer is 'Least representable number in
20630 // the reduction list item type'.
20631 if (Type->isIntegerType() || Type->isPointerType()) {
20632 bool IsSigned = Type->hasSignedIntegerRepresentation();
20633 uint64_t Size = Context.getTypeSize(T: Type);
20634 QualType IntTy =
20635 Context.getIntTypeForBitwidth(DestWidth: Size, /*Signed=*/IsSigned);
20636 llvm::APInt InitValue =
20637 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(numBits: Size)
20638 : llvm::APInt::getMinValue(numBits: Size)
20639 : IsSigned ? llvm::APInt::getSignedMaxValue(numBits: Size)
20640 : llvm::APInt::getMaxValue(numBits: Size);
20641 Init = IntegerLiteral::Create(C: Context, V: InitValue, type: IntTy, l: ELoc);
20642 if (Type->isPointerType()) {
20643 // Cast to pointer type.
20644 ExprResult CastExpr = S.BuildCStyleCastExpr(
20645 LParenLoc: ELoc, Ty: Context.getTrivialTypeSourceInfo(T: Type, Loc: ELoc), RParenLoc: ELoc, Op: Init);
20646 if (CastExpr.isInvalid())
20647 continue;
20648 Init = CastExpr.get();
20649 }
20650 } else if (Type->isRealFloatingType()) {
20651 llvm::APFloat InitValue = llvm::APFloat::getLargest(
20652 Sem: Context.getFloatTypeSemantics(T: Type), Negative: BOK != BO_LT);
20653 Init = FloatingLiteral::Create(C: Context, V: InitValue, /*isexact=*/true,
20654 Type, L: ELoc);
20655 }
20656 break;
20657 }
20658 case BO_PtrMemD:
20659 case BO_PtrMemI:
20660 case BO_MulAssign:
20661 case BO_Div:
20662 case BO_Rem:
20663 case BO_Sub:
20664 case BO_Shl:
20665 case BO_Shr:
20666 case BO_LE:
20667 case BO_GE:
20668 case BO_EQ:
20669 case BO_NE:
20670 case BO_Cmp:
20671 case BO_AndAssign:
20672 case BO_XorAssign:
20673 case BO_OrAssign:
20674 case BO_Assign:
20675 case BO_AddAssign:
20676 case BO_SubAssign:
20677 case BO_DivAssign:
20678 case BO_RemAssign:
20679 case BO_ShlAssign:
20680 case BO_ShrAssign:
20681 case BO_Comma:
20682 llvm_unreachable("Unexpected reduction operation");
20683 }
20684 }
20685 if (Init && DeclareReductionRef.isUnset()) {
20686 S.AddInitializerToDecl(dcl: RHSVD, init: Init, /*DirectInit=*/false);
20687 // Store initializer for single element in private copy. Will be used
20688 // during codegen.
20689 PrivateVD->setInit(RHSVD->getInit());
20690 PrivateVD->setInitStyle(RHSVD->getInitStyle());
20691 } else if (!Init) {
20692 S.ActOnUninitializedDecl(dcl: RHSVD);
20693 // Store initializer for single element in private copy. Will be used
20694 // during codegen.
20695 PrivateVD->setInit(RHSVD->getInit());
20696 PrivateVD->setInitStyle(RHSVD->getInitStyle());
20697 }
20698 if (RHSVD->isInvalidDecl())
20699 continue;
20700 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
20701 S.Diag(Loc: ELoc, DiagID: diag::err_omp_reduction_id_not_compatible)
20702 << Type << ReductionIdRange;
20703 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
20704 VarDecl::DeclarationOnly;
20705 S.Diag(Loc: D->getLocation(),
20706 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
20707 << D;
20708 continue;
20709 }
20710 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, D: PrivateVD, Ty: PrivateTy, Loc: ELoc);
20711 ExprResult ReductionOp;
20712 if (DeclareReductionRef.isUsable()) {
20713 QualType RedTy = DeclareReductionRef.get()->getType();
20714 QualType PtrRedTy = Context.getPointerType(T: RedTy);
20715 ExprResult LHS = S.CreateBuiltinUnaryOp(OpLoc: ELoc, Opc: UO_AddrOf, InputExpr: LHSDRE);
20716 ExprResult RHS = S.CreateBuiltinUnaryOp(OpLoc: ELoc, Opc: UO_AddrOf, InputExpr: RHSDRE);
20717 if (!BasePath.empty()) {
20718 LHS = S.DefaultLvalueConversion(E: LHS.get());
20719 RHS = S.DefaultLvalueConversion(E: RHS.get());
20720 LHS = ImplicitCastExpr::Create(
20721 Context, T: PtrRedTy, Kind: CK_UncheckedDerivedToBase, Operand: LHS.get(), BasePath: &BasePath,
20722 Cat: LHS.get()->getValueKind(), FPO: FPOptionsOverride());
20723 RHS = ImplicitCastExpr::Create(
20724 Context, T: PtrRedTy, Kind: CK_UncheckedDerivedToBase, Operand: RHS.get(), BasePath: &BasePath,
20725 Cat: RHS.get()->getValueKind(), FPO: FPOptionsOverride());
20726 }
20727 FunctionProtoType::ExtProtoInfo EPI;
20728 QualType Params[] = {PtrRedTy, PtrRedTy};
20729 QualType FnTy = Context.getFunctionType(ResultTy: Context.VoidTy, Args: Params, EPI);
20730 auto *OVE = new (Context) OpaqueValueExpr(
20731 ELoc, Context.getPointerType(T: FnTy), VK_PRValue, OK_Ordinary,
20732 S.DefaultLvalueConversion(E: DeclareReductionRef.get()).get());
20733 Expr *Args[] = {LHS.get(), RHS.get()};
20734 ReductionOp =
20735 CallExpr::Create(Ctx: Context, Fn: OVE, Args, Ty: Context.VoidTy, VK: VK_PRValue, RParenLoc: ELoc,
20736 FPFeatures: S.CurFPFeatureOverrides());
20737 } else {
20738 BinaryOperatorKind CombBOK = getRelatedCompoundReductionOp(BOK);
20739 if (Type->isRecordType() && CombBOK != BOK) {
20740 Sema::TentativeAnalysisScope Trap(S);
20741 ReductionOp =
20742 S.BuildBinOp(S: Stack->getCurScope(), OpLoc: ReductionId.getBeginLoc(),
20743 Opc: CombBOK, LHSExpr: LHSDRE, RHSExpr: RHSDRE);
20744 }
20745 if (!ReductionOp.isUsable()) {
20746 ReductionOp =
20747 S.BuildBinOp(S: Stack->getCurScope(), OpLoc: ReductionId.getBeginLoc(), Opc: BOK,
20748 LHSExpr: LHSDRE, RHSExpr: RHSDRE);
20749 if (ReductionOp.isUsable()) {
20750 if (BOK != BO_LT && BOK != BO_GT) {
20751 ReductionOp =
20752 S.BuildBinOp(S: Stack->getCurScope(), OpLoc: ReductionId.getBeginLoc(),
20753 Opc: BO_Assign, LHSExpr: LHSDRE, RHSExpr: ReductionOp.get());
20754 } else {
20755 auto *ConditionalOp = new (Context)
20756 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc,
20757 RHSDRE, Type, VK_LValue, OK_Ordinary);
20758 ReductionOp =
20759 S.BuildBinOp(S: Stack->getCurScope(), OpLoc: ReductionId.getBeginLoc(),
20760 Opc: BO_Assign, LHSExpr: LHSDRE, RHSExpr: ConditionalOp);
20761 }
20762 }
20763 }
20764 if (ReductionOp.isUsable())
20765 ReductionOp = S.ActOnFinishFullExpr(Expr: ReductionOp.get(),
20766 /*DiscardedValue=*/false);
20767 if (!ReductionOp.isUsable())
20768 continue;
20769 }
20770
20771 // Add copy operations for inscan reductions.
20772 // LHS = RHS;
20773 ExprResult CopyOpRes, TempArrayRes, TempArrayElem;
20774 if (ClauseKind == OMPC_reduction &&
20775 RD.RedModifier == OMPC_REDUCTION_inscan) {
20776 ExprResult RHS = S.DefaultLvalueConversion(E: RHSDRE);
20777 CopyOpRes = S.BuildBinOp(S: Stack->getCurScope(), OpLoc: ELoc, Opc: BO_Assign, LHSExpr: LHSDRE,
20778 RHSExpr: RHS.get());
20779 if (!CopyOpRes.isUsable())
20780 continue;
20781 CopyOpRes =
20782 S.ActOnFinishFullExpr(Expr: CopyOpRes.get(), /*DiscardedValue=*/true);
20783 if (!CopyOpRes.isUsable())
20784 continue;
20785 // For simd directive and simd-based directives in simd mode no need to
20786 // construct temp array, need just a single temp element.
20787 if (Stack->getCurrentDirective() == OMPD_simd ||
20788 (S.getLangOpts().OpenMPSimd &&
20789 isOpenMPSimdDirective(DKind: Stack->getCurrentDirective()))) {
20790 VarDecl *TempArrayVD =
20791 buildVarDecl(SemaRef&: S, Loc: ELoc, Type: PrivateTy, Name: D->getName(),
20792 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
20793 // Add a constructor to the temp decl.
20794 S.ActOnUninitializedDecl(dcl: TempArrayVD);
20795 TempArrayRes = buildDeclRefExpr(S, D: TempArrayVD, Ty: PrivateTy, Loc: ELoc);
20796 } else {
20797 // Build temp array for prefix sum.
20798 auto *Dim = new (S.Context)
20799 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_PRValue);
20800 QualType ArrayTy = S.Context.getVariableArrayType(
20801 EltTy: PrivateTy, NumElts: Dim, ASM: ArraySizeModifier::Normal,
20802 /*IndexTypeQuals=*/0);
20803 VarDecl *TempArrayVD =
20804 buildVarDecl(SemaRef&: S, Loc: ELoc, Type: ArrayTy, Name: D->getName(),
20805 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
20806 // Add a constructor to the temp decl.
20807 S.ActOnUninitializedDecl(dcl: TempArrayVD);
20808 TempArrayRes = buildDeclRefExpr(S, D: TempArrayVD, Ty: ArrayTy, Loc: ELoc);
20809 TempArrayElem =
20810 S.DefaultFunctionArrayLvalueConversion(E: TempArrayRes.get());
20811 auto *Idx = new (S.Context)
20812 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_PRValue);
20813 TempArrayElem = S.CreateBuiltinArraySubscriptExpr(Base: TempArrayElem.get(),
20814 LLoc: ELoc, Idx, RLoc: ELoc);
20815 }
20816 }
20817
20818 // OpenMP [2.15.4.6, Restrictions, p.2]
20819 // A list item that appears in an in_reduction clause of a task construct
20820 // must appear in a task_reduction clause of a construct associated with a
20821 // taskgroup region that includes the participating task in its taskgroup
20822 // set. The construct associated with the innermost region that meets this
20823 // condition must specify the same reduction-identifier as the in_reduction
20824 // clause.
20825 if (ClauseKind == OMPC_in_reduction) {
20826 SourceRange ParentSR;
20827 BinaryOperatorKind ParentBOK;
20828 const Expr *ParentReductionOp = nullptr;
20829 Expr *ParentBOKTD = nullptr, *ParentReductionOpTD = nullptr;
20830 DSAStackTy::DSAVarData ParentBOKDSA =
20831 Stack->getTopMostTaskgroupReductionData(D, SR&: ParentSR, BOK&: ParentBOK,
20832 TaskgroupDescriptor&: ParentBOKTD);
20833 DSAStackTy::DSAVarData ParentReductionOpDSA =
20834 Stack->getTopMostTaskgroupReductionData(
20835 D, SR&: ParentSR, ReductionRef&: ParentReductionOp, TaskgroupDescriptor&: ParentReductionOpTD);
20836 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
20837 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
20838 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
20839 (DeclareReductionRef.isUsable() && IsParentBOK) ||
20840 (IsParentBOK && BOK != ParentBOK) || IsParentReductionOp) {
20841 bool EmitError = true;
20842 if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
20843 llvm::FoldingSetNodeID RedId, ParentRedId;
20844 ParentReductionOp->Profile(ID&: ParentRedId, Context, /*Canonical=*/true);
20845 DeclareReductionRef.get()->Profile(ID&: RedId, Context,
20846 /*Canonical=*/true);
20847 EmitError = RedId != ParentRedId;
20848 }
20849 if (EmitError) {
20850 S.Diag(Loc: ReductionId.getBeginLoc(),
20851 DiagID: diag::err_omp_reduction_identifier_mismatch)
20852 << ReductionIdRange << RefExpr->getSourceRange();
20853 S.Diag(Loc: ParentSR.getBegin(),
20854 DiagID: diag::note_omp_previous_reduction_identifier)
20855 << ParentSR
20856 << (IsParentBOK ? ParentBOKDSA.RefExpr
20857 : ParentReductionOpDSA.RefExpr)
20858 ->getSourceRange();
20859 continue;
20860 }
20861 }
20862 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
20863 }
20864
20865 DeclRefExpr *Ref = nullptr;
20866 Expr *VarsExpr = RefExpr->IgnoreParens();
20867 if (!VD && !S.CurContext->isDependentContext()) {
20868 if (ASE || OASE) {
20869 TransformExprToCaptures RebuildToCapture(S, D);
20870 VarsExpr =
20871 RebuildToCapture.TransformExpr(E: RefExpr->IgnoreParens()).get();
20872 Ref = RebuildToCapture.getCapturedExpr();
20873 } else {
20874 VarsExpr = Ref = buildCapture(S, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/false);
20875 }
20876 if (!S.OpenMP().isOpenMPCapturedDecl(D)) {
20877 RD.ExprCaptures.emplace_back(Args: Ref->getDecl());
20878 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
20879 ExprResult RefRes = S.DefaultLvalueConversion(E: Ref);
20880 if (!RefRes.isUsable())
20881 continue;
20882 ExprResult PostUpdateRes =
20883 S.BuildBinOp(S: Stack->getCurScope(), OpLoc: ELoc, Opc: BO_Assign, LHSExpr: SimpleRefExpr,
20884 RHSExpr: RefRes.get());
20885 if (!PostUpdateRes.isUsable())
20886 continue;
20887 if (isOpenMPTaskingDirective(Kind: Stack->getCurrentDirective()) ||
20888 Stack->getCurrentDirective() == OMPD_taskgroup) {
20889 S.Diag(Loc: RefExpr->getExprLoc(),
20890 DiagID: diag::err_omp_reduction_non_addressable_expression)
20891 << RefExpr->getSourceRange();
20892 continue;
20893 }
20894 RD.ExprPostUpdates.emplace_back(
20895 Args: S.IgnoredValueConversions(E: PostUpdateRes.get()).get());
20896 }
20897 }
20898 }
20899 // All reduction items are still marked as reduction (to do not increase
20900 // code base size).
20901 unsigned Modifier = RD.RedModifier;
20902 // Consider task_reductions as reductions with task modifier. Required for
20903 // correct analysis of in_reduction clauses.
20904 if (CurrDir == OMPD_taskgroup && ClauseKind == OMPC_task_reduction)
20905 Modifier = OMPC_REDUCTION_task;
20906 Stack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_reduction, PrivateCopy: Ref, Modifier,
20907 AppliedToPointee: ASE || OASE);
20908 if (Modifier == OMPC_REDUCTION_task &&
20909 (CurrDir == OMPD_taskgroup ||
20910 ((isOpenMPParallelDirective(DKind: CurrDir) ||
20911 isOpenMPWorksharingDirective(DKind: CurrDir)) &&
20912 !isOpenMPSimdDirective(DKind: CurrDir)))) {
20913 if (DeclareReductionRef.isUsable())
20914 Stack->addTaskgroupReductionData(D, SR: ReductionIdRange,
20915 ReductionRef: DeclareReductionRef.get());
20916 else
20917 Stack->addTaskgroupReductionData(D, SR: ReductionIdRange, BOK);
20918 }
20919 RD.push(Item: VarsExpr, Private: PrivateDRE, LHS: LHSDRE, RHS: RHSDRE, ReductionOp: ReductionOp.get(),
20920 TaskgroupDescriptor, CopyOp: CopyOpRes.get(), CopyArrayTemp: TempArrayRes.get(),
20921 CopyArrayElem: TempArrayElem.get(), IsPrivate);
20922 }
20923 return RD.Vars.empty();
20924}
20925
20926OMPClause *SemaOpenMP::ActOnOpenMPReductionClause(
20927 ArrayRef<Expr *> VarList,
20928 OpenMPVarListDataTy::OpenMPReductionClauseModifiers Modifiers,
20929 SourceLocation StartLoc, SourceLocation LParenLoc,
20930 SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc,
20931 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
20932 ArrayRef<Expr *> UnresolvedReductions) {
20933 OpenMPReductionClauseModifier Modifier =
20934 static_cast<OpenMPReductionClauseModifier>(Modifiers.ExtraModifier);
20935 OpenMPOriginalSharingModifier OriginalSharingModifier =
20936 static_cast<OpenMPOriginalSharingModifier>(
20937 Modifiers.OriginalSharingModifier);
20938 if (ModifierLoc.isValid() && Modifier == OMPC_REDUCTION_unknown) {
20939 Diag(Loc: LParenLoc, DiagID: diag::err_omp_unexpected_clause_value)
20940 << getListOfPossibleValues(K: OMPC_reduction, /*First=*/0,
20941 /*Last=*/OMPC_REDUCTION_unknown)
20942 << getOpenMPClauseNameForDiag(C: OMPC_reduction);
20943 return nullptr;
20944 }
20945 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions
20946 // A reduction clause with the inscan reduction-modifier may only appear on a
20947 // worksharing-loop construct, a worksharing-loop SIMD construct, a simd
20948 // construct, a parallel worksharing-loop construct or a parallel
20949 // worksharing-loop SIMD construct.
20950 if (Modifier == OMPC_REDUCTION_inscan &&
20951 (DSAStack->getCurrentDirective() != OMPD_for &&
20952 DSAStack->getCurrentDirective() != OMPD_for_simd &&
20953 DSAStack->getCurrentDirective() != OMPD_simd &&
20954 DSAStack->getCurrentDirective() != OMPD_parallel_for &&
20955 DSAStack->getCurrentDirective() != OMPD_parallel_for_simd)) {
20956 Diag(Loc: ModifierLoc, DiagID: diag::err_omp_wrong_inscan_reduction);
20957 return nullptr;
20958 }
20959 ReductionData RD(VarList.size(), Modifier, OriginalSharingModifier);
20960 if (actOnOMPReductionKindClause(S&: SemaRef, DSAStack, ClauseKind: OMPC_reduction, VarList,
20961 StartLoc, LParenLoc, ColonLoc, EndLoc,
20962 ReductionIdScopeSpec, ReductionId,
20963 UnresolvedReductions, RD))
20964 return nullptr;
20965
20966 return OMPReductionClause::Create(
20967 C: getASTContext(), StartLoc, LParenLoc, ModifierLoc, ColonLoc, EndLoc,
20968 Modifier, VL: RD.Vars,
20969 QualifierLoc: ReductionIdScopeSpec.getWithLocInContext(Context&: getASTContext()), NameInfo: ReductionId,
20970 Privates: RD.Privates, LHSExprs: RD.LHSs, RHSExprs: RD.RHSs, ReductionOps: RD.ReductionOps, CopyOps: RD.InscanCopyOps,
20971 CopyArrayTemps: RD.InscanCopyArrayTemps, CopyArrayElems: RD.InscanCopyArrayElems,
20972 PreInit: buildPreInits(Context&: getASTContext(), PreInits: RD.ExprCaptures),
20973 PostUpdate: buildPostUpdate(S&: SemaRef, PostUpdates: RD.ExprPostUpdates), IsPrivateVarReduction: RD.IsPrivateVarReduction,
20974 OriginalSharingModifier);
20975}
20976
20977OMPClause *SemaOpenMP::ActOnOpenMPTaskReductionClause(
20978 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
20979 SourceLocation ColonLoc, SourceLocation EndLoc,
20980 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
20981 ArrayRef<Expr *> UnresolvedReductions) {
20982 ReductionData RD(VarList.size());
20983 if (actOnOMPReductionKindClause(S&: SemaRef, DSAStack, ClauseKind: OMPC_task_reduction,
20984 VarList, StartLoc, LParenLoc, ColonLoc,
20985 EndLoc, ReductionIdScopeSpec, ReductionId,
20986 UnresolvedReductions, RD))
20987 return nullptr;
20988
20989 return OMPTaskReductionClause::Create(
20990 C: getASTContext(), StartLoc, LParenLoc, ColonLoc, EndLoc, VL: RD.Vars,
20991 QualifierLoc: ReductionIdScopeSpec.getWithLocInContext(Context&: getASTContext()), NameInfo: ReductionId,
20992 Privates: RD.Privates, LHSExprs: RD.LHSs, RHSExprs: RD.RHSs, ReductionOps: RD.ReductionOps,
20993 PreInit: buildPreInits(Context&: getASTContext(), PreInits: RD.ExprCaptures),
20994 PostUpdate: buildPostUpdate(S&: SemaRef, PostUpdates: RD.ExprPostUpdates));
20995}
20996
20997OMPClause *SemaOpenMP::ActOnOpenMPInReductionClause(
20998 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
20999 SourceLocation ColonLoc, SourceLocation EndLoc,
21000 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
21001 ArrayRef<Expr *> UnresolvedReductions) {
21002 ReductionData RD(VarList.size());
21003 if (actOnOMPReductionKindClause(S&: SemaRef, DSAStack, ClauseKind: OMPC_in_reduction, VarList,
21004 StartLoc, LParenLoc, ColonLoc, EndLoc,
21005 ReductionIdScopeSpec, ReductionId,
21006 UnresolvedReductions, RD))
21007 return nullptr;
21008
21009 return OMPInReductionClause::Create(
21010 C: getASTContext(), StartLoc, LParenLoc, ColonLoc, EndLoc, VL: RD.Vars,
21011 QualifierLoc: ReductionIdScopeSpec.getWithLocInContext(Context&: getASTContext()), NameInfo: ReductionId,
21012 Privates: RD.Privates, LHSExprs: RD.LHSs, RHSExprs: RD.RHSs, ReductionOps: RD.ReductionOps, TaskgroupDescriptors: RD.TaskgroupDescriptors,
21013 PreInit: buildPreInits(Context&: getASTContext(), PreInits: RD.ExprCaptures),
21014 PostUpdate: buildPostUpdate(S&: SemaRef, PostUpdates: RD.ExprPostUpdates));
21015}
21016
21017bool SemaOpenMP::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
21018 SourceLocation LinLoc) {
21019 if ((!getLangOpts().CPlusPlus && LinKind != OMPC_LINEAR_val) ||
21020 LinKind == OMPC_LINEAR_unknown || LinKind == OMPC_LINEAR_step) {
21021 Diag(Loc: LinLoc, DiagID: diag::err_omp_wrong_linear_modifier)
21022 << getLangOpts().CPlusPlus;
21023 return true;
21024 }
21025 return false;
21026}
21027
21028bool SemaOpenMP::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
21029 OpenMPLinearClauseKind LinKind,
21030 QualType Type, bool IsDeclareSimd) {
21031 const auto *VD = dyn_cast_or_null<VarDecl>(Val: D);
21032 // A variable must not have an incomplete type or a reference type.
21033 if (SemaRef.RequireCompleteType(Loc: ELoc, T: Type,
21034 DiagID: diag::err_omp_linear_incomplete_type))
21035 return true;
21036 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
21037 !Type->isReferenceType()) {
21038 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_linear_modifier_non_reference)
21039 << Type << getOpenMPSimpleClauseTypeName(Kind: OMPC_linear, Type: LinKind);
21040 return true;
21041 }
21042 Type = Type.getNonReferenceType();
21043
21044 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
21045 // A variable that is privatized must not have a const-qualified type
21046 // unless it is of class type with a mutable member. This restriction does
21047 // not apply to the firstprivate clause, nor to the linear clause on
21048 // declarative directives (like declare simd).
21049 if (!IsDeclareSimd &&
21050 rejectConstNotMutableType(SemaRef, D, Type, CKind: OMPC_linear, ELoc))
21051 return true;
21052
21053 // A list item must be of integral or pointer type.
21054 Type = Type.getUnqualifiedType().getCanonicalType();
21055 const auto *Ty = Type.getTypePtrOrNull();
21056 if (!Ty || (LinKind != OMPC_LINEAR_ref && !Ty->isDependentType() &&
21057 !Ty->isIntegralType(Ctx: getASTContext()) && !Ty->isPointerType())) {
21058 Diag(Loc: ELoc, DiagID: diag::err_omp_linear_expected_int_or_ptr) << Type;
21059 if (D) {
21060 bool IsDecl = !VD || VD->isThisDeclarationADefinition(getASTContext()) ==
21061 VarDecl::DeclarationOnly;
21062 Diag(Loc: D->getLocation(),
21063 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
21064 << D;
21065 }
21066 return true;
21067 }
21068 return false;
21069}
21070
21071OMPClause *SemaOpenMP::ActOnOpenMPLinearClause(
21072 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
21073 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
21074 SourceLocation LinLoc, SourceLocation ColonLoc,
21075 SourceLocation StepModifierLoc, SourceLocation EndLoc) {
21076 SmallVector<Expr *, 8> Vars;
21077 SmallVector<Expr *, 8> Privates;
21078 SmallVector<Expr *, 8> Inits;
21079 SmallVector<Decl *, 4> ExprCaptures;
21080 SmallVector<Expr *, 4> ExprPostUpdates;
21081 // OpenMP 5.2 [Section 5.4.6, linear clause]
21082 // step-simple-modifier is exclusive, can't be used with 'val', 'uval', or
21083 // 'ref'
21084 if (LinLoc.isValid() && StepModifierLoc.isInvalid() && Step &&
21085 getLangOpts().OpenMP >= 52)
21086 Diag(Loc: Step->getBeginLoc(), DiagID: diag::err_omp_step_simple_modifier_exclusive);
21087 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
21088 LinKind = OMPC_LINEAR_val;
21089 for (Expr *RefExpr : VarList) {
21090 assert(RefExpr && "NULL expr in OpenMP linear clause.");
21091 SourceLocation ELoc;
21092 SourceRange ERange;
21093 Expr *SimpleRefExpr = RefExpr;
21094 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
21095 if (Res.second) {
21096 // It will be analyzed later.
21097 Vars.push_back(Elt: RefExpr);
21098 Privates.push_back(Elt: nullptr);
21099 Inits.push_back(Elt: nullptr);
21100 }
21101 ValueDecl *D = Res.first;
21102 if (!D)
21103 continue;
21104
21105 QualType Type = D->getType();
21106 auto *VD = dyn_cast<VarDecl>(Val: D);
21107
21108 // OpenMP [2.14.3.7, linear clause]
21109 // A list-item cannot appear in more than one linear clause.
21110 // A list-item that appears in a linear clause cannot appear in any
21111 // other data-sharing attribute clause.
21112 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
21113 if (DVar.RefExpr) {
21114 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
21115 << getOpenMPClauseNameForDiag(C: DVar.CKind)
21116 << getOpenMPClauseNameForDiag(C: OMPC_linear);
21117 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
21118 continue;
21119 }
21120
21121 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
21122 continue;
21123 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
21124
21125 // Build private copy of original var.
21126 VarDecl *Private =
21127 buildVarDecl(SemaRef, Loc: ELoc, Type, Name: D->getName(),
21128 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr,
21129 OrigRef: VD ? cast<DeclRefExpr>(Val: SimpleRefExpr) : nullptr);
21130 DeclRefExpr *PrivateRef = buildDeclRefExpr(S&: SemaRef, D: Private, Ty: Type, Loc: ELoc);
21131 // Build var to save initial value.
21132 VarDecl *Init = buildVarDecl(SemaRef, Loc: ELoc, Type, Name: ".linear.start");
21133 Expr *InitExpr;
21134 DeclRefExpr *Ref = nullptr;
21135 if (!VD && !SemaRef.CurContext->isDependentContext()) {
21136 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/false);
21137 if (!isOpenMPCapturedDecl(D)) {
21138 ExprCaptures.push_back(Elt: Ref->getDecl());
21139 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
21140 ExprResult RefRes = SemaRef.DefaultLvalueConversion(E: Ref);
21141 if (!RefRes.isUsable())
21142 continue;
21143 ExprResult PostUpdateRes =
21144 SemaRef.BuildBinOp(DSAStack->getCurScope(), OpLoc: ELoc, Opc: BO_Assign,
21145 LHSExpr: SimpleRefExpr, RHSExpr: RefRes.get());
21146 if (!PostUpdateRes.isUsable())
21147 continue;
21148 ExprPostUpdates.push_back(
21149 Elt: SemaRef.IgnoredValueConversions(E: PostUpdateRes.get()).get());
21150 }
21151 }
21152 }
21153 if (LinKind == OMPC_LINEAR_uval)
21154 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
21155 else
21156 InitExpr = VD ? SimpleRefExpr : Ref;
21157 SemaRef.AddInitializerToDecl(
21158 dcl: Init, init: SemaRef.DefaultLvalueConversion(E: InitExpr).get(),
21159 /*DirectInit=*/false);
21160 DeclRefExpr *InitRef = buildDeclRefExpr(S&: SemaRef, D: Init, Ty: Type, Loc: ELoc);
21161
21162 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_linear, PrivateCopy: Ref);
21163 Vars.push_back(Elt: (VD || SemaRef.CurContext->isDependentContext())
21164 ? RefExpr->IgnoreParens()
21165 : Ref);
21166 Privates.push_back(Elt: PrivateRef);
21167 Inits.push_back(Elt: InitRef);
21168 }
21169
21170 if (Vars.empty())
21171 return nullptr;
21172
21173 Expr *StepExpr = Step;
21174 Expr *CalcStepExpr = nullptr;
21175 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
21176 !Step->isInstantiationDependent() &&
21177 !Step->containsUnexpandedParameterPack()) {
21178 SourceLocation StepLoc = Step->getBeginLoc();
21179 ExprResult Val = PerformOpenMPImplicitIntegerConversion(Loc: StepLoc, Op: Step);
21180 if (Val.isInvalid())
21181 return nullptr;
21182 StepExpr = Val.get();
21183
21184 // Build var to save the step value.
21185 VarDecl *SaveVar =
21186 buildVarDecl(SemaRef, Loc: StepLoc, Type: StepExpr->getType(), Name: ".linear.step");
21187 ExprResult SaveRef =
21188 buildDeclRefExpr(S&: SemaRef, D: SaveVar, Ty: StepExpr->getType(), Loc: StepLoc);
21189 ExprResult CalcStep = SemaRef.BuildBinOp(
21190 S: SemaRef.getCurScope(), OpLoc: StepLoc, Opc: BO_Assign, LHSExpr: SaveRef.get(), RHSExpr: StepExpr);
21191 CalcStep =
21192 SemaRef.ActOnFinishFullExpr(Expr: CalcStep.get(), /*DiscardedValue=*/false);
21193
21194 // Warn about zero linear step (it would be probably better specified as
21195 // making corresponding variables 'const').
21196 if (std::optional<llvm::APSInt> Result =
21197 StepExpr->getIntegerConstantExpr(Ctx: getASTContext())) {
21198 if (!Result->isNegative() && !Result->isStrictlyPositive())
21199 Diag(Loc: StepLoc, DiagID: diag::warn_omp_linear_step_zero)
21200 << Vars[0] << (Vars.size() > 1);
21201 } else if (CalcStep.isUsable()) {
21202 // Calculate the step beforehand instead of doing this on each iteration.
21203 // (This is not used if the number of iterations may be kfold-ed).
21204 CalcStepExpr = CalcStep.get();
21205 }
21206 }
21207
21208 return OMPLinearClause::Create(C: getASTContext(), StartLoc, LParenLoc, Modifier: LinKind,
21209 ModifierLoc: LinLoc, ColonLoc, StepModifierLoc, EndLoc,
21210 VL: Vars, PL: Privates, IL: Inits, Step: StepExpr, CalcStep: CalcStepExpr,
21211 PreInit: buildPreInits(Context&: getASTContext(), PreInits: ExprCaptures),
21212 PostUpdate: buildPostUpdate(S&: SemaRef, PostUpdates: ExprPostUpdates));
21213}
21214
21215static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
21216 Expr *NumIterations, Sema &SemaRef,
21217 Scope *S, DSAStackTy *Stack) {
21218 // Walk the vars and build update/final expressions for the CodeGen.
21219 SmallVector<Expr *, 8> Updates;
21220 SmallVector<Expr *, 8> Finals;
21221 SmallVector<Expr *, 8> UsedExprs;
21222 Expr *Step = Clause.getStep();
21223 Expr *CalcStep = Clause.getCalcStep();
21224 // OpenMP [2.14.3.7, linear clause]
21225 // If linear-step is not specified it is assumed to be 1.
21226 if (!Step)
21227 Step = SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get();
21228 else if (CalcStep)
21229 Step = cast<BinaryOperator>(Val: CalcStep)->getLHS();
21230 bool HasErrors = false;
21231 auto CurInit = Clause.inits().begin();
21232 auto CurPrivate = Clause.privates().begin();
21233 OpenMPLinearClauseKind LinKind = Clause.getModifier();
21234 for (Expr *RefExpr : Clause.varlist()) {
21235 SourceLocation ELoc;
21236 SourceRange ERange;
21237 Expr *SimpleRefExpr = RefExpr;
21238 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
21239 ValueDecl *D = Res.first;
21240 if (Res.second || !D) {
21241 Updates.push_back(Elt: nullptr);
21242 Finals.push_back(Elt: nullptr);
21243 HasErrors = true;
21244 continue;
21245 }
21246 auto &&Info = Stack->isLoopControlVariable(D);
21247 // OpenMP [2.15.11, distribute simd Construct]
21248 // A list item may not appear in a linear clause, unless it is the loop
21249 // iteration variable.
21250 if (isOpenMPDistributeDirective(DKind: Stack->getCurrentDirective()) &&
21251 isOpenMPSimdDirective(DKind: Stack->getCurrentDirective()) && !Info.first) {
21252 SemaRef.Diag(Loc: ELoc,
21253 DiagID: diag::err_omp_linear_distribute_var_non_loop_iteration);
21254 Updates.push_back(Elt: nullptr);
21255 Finals.push_back(Elt: nullptr);
21256 HasErrors = true;
21257 continue;
21258 }
21259 Expr *InitExpr = *CurInit;
21260
21261 // Build privatized reference to the current linear var.
21262 auto *DE = cast<DeclRefExpr>(Val: SimpleRefExpr);
21263 Expr *CapturedRef;
21264 if (LinKind == OMPC_LINEAR_uval)
21265 CapturedRef = cast<VarDecl>(Val: DE->getDecl())->getInit();
21266 else
21267 CapturedRef =
21268 buildDeclRefExpr(S&: SemaRef, D: cast<VarDecl>(Val: DE->getDecl()),
21269 Ty: DE->getType().getUnqualifiedType(), Loc: DE->getExprLoc(),
21270 /*RefersToCapture=*/true);
21271
21272 // Build update: Var = InitExpr + IV * Step
21273 ExprResult Update;
21274 if (!Info.first)
21275 Update = buildCounterUpdate(
21276 SemaRef, S, Loc: RefExpr->getExprLoc(), VarRef: *CurPrivate, Start: InitExpr, Iter: IV, Step,
21277 /*Subtract=*/false, /*IsNonRectangularLB=*/false);
21278 else
21279 Update = *CurPrivate;
21280 Update = SemaRef.ActOnFinishFullExpr(Expr: Update.get(), CC: DE->getBeginLoc(),
21281 /*DiscardedValue=*/false);
21282
21283 // Build final: Var = PrivCopy;
21284 ExprResult Final;
21285 if (!Info.first)
21286 Final = SemaRef.BuildBinOp(
21287 S, OpLoc: RefExpr->getExprLoc(), Opc: BO_Assign, LHSExpr: CapturedRef,
21288 RHSExpr: SemaRef.DefaultLvalueConversion(E: *CurPrivate).get());
21289 else
21290 Final = *CurPrivate;
21291 Final = SemaRef.ActOnFinishFullExpr(Expr: Final.get(), CC: DE->getBeginLoc(),
21292 /*DiscardedValue=*/false);
21293
21294 if (!Update.isUsable() || !Final.isUsable()) {
21295 Updates.push_back(Elt: nullptr);
21296 Finals.push_back(Elt: nullptr);
21297 UsedExprs.push_back(Elt: nullptr);
21298 HasErrors = true;
21299 } else {
21300 Updates.push_back(Elt: Update.get());
21301 Finals.push_back(Elt: Final.get());
21302 if (!Info.first)
21303 UsedExprs.push_back(Elt: SimpleRefExpr);
21304 }
21305 ++CurInit;
21306 ++CurPrivate;
21307 }
21308 if (Expr *S = Clause.getStep())
21309 UsedExprs.push_back(Elt: S);
21310 // Fill the remaining part with the nullptr.
21311 UsedExprs.append(NumInputs: Clause.varlist_size() + 1 - UsedExprs.size(), Elt: nullptr);
21312 Clause.setUpdates(Updates);
21313 Clause.setFinals(Finals);
21314 Clause.setUsedExprs(UsedExprs);
21315 return HasErrors;
21316}
21317
21318OMPClause *SemaOpenMP::ActOnOpenMPAlignedClause(
21319 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
21320 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
21321 SmallVector<Expr *, 8> Vars;
21322 for (Expr *RefExpr : VarList) {
21323 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
21324 SourceLocation ELoc;
21325 SourceRange ERange;
21326 Expr *SimpleRefExpr = RefExpr;
21327 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
21328 if (Res.second) {
21329 // It will be analyzed later.
21330 Vars.push_back(Elt: RefExpr);
21331 }
21332 ValueDecl *D = Res.first;
21333 if (!D)
21334 continue;
21335
21336 QualType QType = D->getType();
21337 auto *VD = dyn_cast<VarDecl>(Val: D);
21338
21339 // OpenMP [2.8.1, simd construct, Restrictions]
21340 // The type of list items appearing in the aligned clause must be
21341 // array, pointer, reference to array, or reference to pointer.
21342 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
21343 const Type *Ty = QType.getTypePtrOrNull();
21344 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
21345 Diag(Loc: ELoc, DiagID: diag::err_omp_aligned_expected_array_or_ptr)
21346 << QType << getLangOpts().CPlusPlus << ERange;
21347 bool IsDecl = !VD || VD->isThisDeclarationADefinition(getASTContext()) ==
21348 VarDecl::DeclarationOnly;
21349 Diag(Loc: D->getLocation(),
21350 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
21351 << D;
21352 continue;
21353 }
21354
21355 // OpenMP [2.8.1, simd construct, Restrictions]
21356 // A list-item cannot appear in more than one aligned clause.
21357 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, NewDE: SimpleRefExpr)) {
21358 Diag(Loc: ELoc, DiagID: diag::err_omp_used_in_clause_twice)
21359 << 0 << getOpenMPClauseNameForDiag(C: OMPC_aligned) << ERange;
21360 Diag(Loc: PrevRef->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
21361 << getOpenMPClauseNameForDiag(C: OMPC_aligned);
21362 continue;
21363 }
21364
21365 DeclRefExpr *Ref = nullptr;
21366 if (!VD && isOpenMPCapturedDecl(D))
21367 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/true);
21368 Vars.push_back(Elt: SemaRef
21369 .DefaultFunctionArrayConversion(
21370 E: (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
21371 .get());
21372 }
21373
21374 // OpenMP [2.8.1, simd construct, Description]
21375 // The parameter of the aligned clause, alignment, must be a constant
21376 // positive integer expression.
21377 // If no optional parameter is specified, implementation-defined default
21378 // alignments for SIMD instructions on the target platforms are assumed.
21379 if (Alignment != nullptr) {
21380 ExprResult AlignResult =
21381 VerifyPositiveIntegerConstantInClause(E: Alignment, CKind: OMPC_aligned);
21382 if (AlignResult.isInvalid())
21383 return nullptr;
21384 Alignment = AlignResult.get();
21385 }
21386 if (Vars.empty())
21387 return nullptr;
21388
21389 return OMPAlignedClause::Create(C: getASTContext(), StartLoc, LParenLoc,
21390 ColonLoc, EndLoc, VL: Vars, A: Alignment);
21391}
21392
21393OMPClause *SemaOpenMP::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
21394 SourceLocation StartLoc,
21395 SourceLocation LParenLoc,
21396 SourceLocation EndLoc) {
21397 SmallVector<Expr *, 8> Vars;
21398 SmallVector<Expr *, 8> SrcExprs;
21399 SmallVector<Expr *, 8> DstExprs;
21400 SmallVector<Expr *, 8> AssignmentOps;
21401 for (Expr *RefExpr : VarList) {
21402 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
21403 if (isa<DependentScopeDeclRefExpr>(Val: RefExpr)) {
21404 // It will be analyzed later.
21405 Vars.push_back(Elt: RefExpr);
21406 SrcExprs.push_back(Elt: nullptr);
21407 DstExprs.push_back(Elt: nullptr);
21408 AssignmentOps.push_back(Elt: nullptr);
21409 continue;
21410 }
21411
21412 SourceLocation ELoc = RefExpr->getExprLoc();
21413 // OpenMP [2.1, C/C++]
21414 // A list item is a variable name.
21415 // OpenMP [2.14.4.1, Restrictions, p.1]
21416 // A list item that appears in a copyin clause must be threadprivate.
21417 auto *DE = dyn_cast<DeclRefExpr>(Val: RefExpr);
21418 if (!DE || !isa<VarDecl>(Val: DE->getDecl())) {
21419 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_var_name_member_expr)
21420 << 0 << RefExpr->getSourceRange();
21421 continue;
21422 }
21423
21424 Decl *D = DE->getDecl();
21425 auto *VD = cast<VarDecl>(Val: D);
21426
21427 QualType Type = VD->getType();
21428 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
21429 // It will be analyzed later.
21430 Vars.push_back(Elt: DE);
21431 SrcExprs.push_back(Elt: nullptr);
21432 DstExprs.push_back(Elt: nullptr);
21433 AssignmentOps.push_back(Elt: nullptr);
21434 continue;
21435 }
21436
21437 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
21438 // A list item that appears in a copyin clause must be threadprivate.
21439 if (!DSAStack->isThreadPrivate(D: VD)) {
21440 unsigned OMPVersion = getLangOpts().OpenMP;
21441 Diag(Loc: ELoc, DiagID: diag::err_omp_required_access)
21442 << getOpenMPClauseNameForDiag(C: OMPC_copyin)
21443 << getOpenMPDirectiveName(D: OMPD_threadprivate, Ver: OMPVersion);
21444 continue;
21445 }
21446
21447 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
21448 // A variable of class type (or array thereof) that appears in a
21449 // copyin clause requires an accessible, unambiguous copy assignment
21450 // operator for the class type.
21451 QualType ElemType =
21452 getASTContext().getBaseElementType(QT: Type).getNonReferenceType();
21453 VarDecl *SrcVD =
21454 buildVarDecl(SemaRef, Loc: DE->getBeginLoc(), Type: ElemType.getUnqualifiedType(),
21455 Name: ".copyin.src", Attrs: VD->hasAttrs() ? &VD->getAttrs() : nullptr);
21456 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
21457 S&: SemaRef, D: SrcVD, Ty: ElemType.getUnqualifiedType(), Loc: DE->getExprLoc());
21458 VarDecl *DstVD =
21459 buildVarDecl(SemaRef, Loc: DE->getBeginLoc(), Type: ElemType, Name: ".copyin.dst",
21460 Attrs: VD->hasAttrs() ? &VD->getAttrs() : nullptr);
21461 DeclRefExpr *PseudoDstExpr =
21462 buildDeclRefExpr(S&: SemaRef, D: DstVD, Ty: ElemType, Loc: DE->getExprLoc());
21463 // For arrays generate assignment operation for single element and replace
21464 // it by the original array element in CodeGen.
21465 ExprResult AssignmentOp =
21466 SemaRef.BuildBinOp(/*S=*/nullptr, OpLoc: DE->getExprLoc(), Opc: BO_Assign,
21467 LHSExpr: PseudoDstExpr, RHSExpr: PseudoSrcExpr);
21468 if (AssignmentOp.isInvalid())
21469 continue;
21470 AssignmentOp =
21471 SemaRef.ActOnFinishFullExpr(Expr: AssignmentOp.get(), CC: DE->getExprLoc(),
21472 /*DiscardedValue=*/false);
21473 if (AssignmentOp.isInvalid())
21474 continue;
21475
21476 DSAStack->addDSA(D: VD, E: DE, A: OMPC_copyin);
21477 Vars.push_back(Elt: DE);
21478 SrcExprs.push_back(Elt: PseudoSrcExpr);
21479 DstExprs.push_back(Elt: PseudoDstExpr);
21480 AssignmentOps.push_back(Elt: AssignmentOp.get());
21481 }
21482
21483 if (Vars.empty())
21484 return nullptr;
21485
21486 return OMPCopyinClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
21487 VL: Vars, SrcExprs, DstExprs, AssignmentOps);
21488}
21489
21490OMPClause *SemaOpenMP::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
21491 SourceLocation StartLoc,
21492 SourceLocation LParenLoc,
21493 SourceLocation EndLoc) {
21494 SmallVector<Expr *, 8> Vars;
21495 SmallVector<Expr *, 8> SrcExprs;
21496 SmallVector<Expr *, 8> DstExprs;
21497 SmallVector<Expr *, 8> AssignmentOps;
21498 for (Expr *RefExpr : VarList) {
21499 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
21500 SourceLocation ELoc;
21501 SourceRange ERange;
21502 Expr *SimpleRefExpr = RefExpr;
21503 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
21504 if (Res.second) {
21505 // It will be analyzed later.
21506 Vars.push_back(Elt: RefExpr);
21507 SrcExprs.push_back(Elt: nullptr);
21508 DstExprs.push_back(Elt: nullptr);
21509 AssignmentOps.push_back(Elt: nullptr);
21510 }
21511 ValueDecl *D = Res.first;
21512 if (!D)
21513 continue;
21514
21515 QualType Type = D->getType();
21516 auto *VD = dyn_cast<VarDecl>(Val: D);
21517
21518 // OpenMP [2.14.4.2, Restrictions, p.2]
21519 // A list item that appears in a copyprivate clause may not appear in a
21520 // private or firstprivate clause on the single construct.
21521 if (!VD || !DSAStack->isThreadPrivate(D: VD)) {
21522 DSAStackTy::DSAVarData DVar =
21523 DSAStack->getTopDSA(D, /*FromParent=*/false);
21524 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
21525 DVar.RefExpr) {
21526 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
21527 << getOpenMPClauseNameForDiag(C: DVar.CKind)
21528 << getOpenMPClauseNameForDiag(C: OMPC_copyprivate);
21529 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
21530 continue;
21531 }
21532
21533 // OpenMP [2.11.4.2, Restrictions, p.1]
21534 // All list items that appear in a copyprivate clause must be either
21535 // threadprivate or private in the enclosing context.
21536 if (DVar.CKind == OMPC_unknown) {
21537 DVar = DSAStack->getImplicitDSA(D, FromParent: false);
21538 if (DVar.CKind == OMPC_shared) {
21539 Diag(Loc: ELoc, DiagID: diag::err_omp_required_access)
21540 << getOpenMPClauseNameForDiag(C: OMPC_copyprivate)
21541 << "threadprivate or private in the enclosing context";
21542 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
21543 continue;
21544 }
21545 }
21546 }
21547
21548 // Variably modified types are not supported.
21549 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
21550 unsigned OMPVersion = getLangOpts().OpenMP;
21551 Diag(Loc: ELoc, DiagID: diag::err_omp_variably_modified_type_not_supported)
21552 << getOpenMPClauseNameForDiag(C: OMPC_copyprivate) << Type
21553 << getOpenMPDirectiveName(DSAStack->getCurrentDirective(),
21554 Ver: OMPVersion);
21555 bool IsDecl = !VD || VD->isThisDeclarationADefinition(getASTContext()) ==
21556 VarDecl::DeclarationOnly;
21557 Diag(Loc: D->getLocation(),
21558 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
21559 << D;
21560 continue;
21561 }
21562
21563 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
21564 // A variable of class type (or array thereof) that appears in a
21565 // copyin clause requires an accessible, unambiguous copy assignment
21566 // operator for the class type.
21567 Type = getASTContext()
21568 .getBaseElementType(QT: Type.getNonReferenceType())
21569 .getUnqualifiedType();
21570 VarDecl *SrcVD =
21571 buildVarDecl(SemaRef, Loc: RefExpr->getBeginLoc(), Type, Name: ".copyprivate.src",
21572 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
21573 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(S&: SemaRef, D: SrcVD, Ty: Type, Loc: ELoc);
21574 VarDecl *DstVD =
21575 buildVarDecl(SemaRef, Loc: RefExpr->getBeginLoc(), Type, Name: ".copyprivate.dst",
21576 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
21577 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(S&: SemaRef, D: DstVD, Ty: Type, Loc: ELoc);
21578 ExprResult AssignmentOp = SemaRef.BuildBinOp(
21579 DSAStack->getCurScope(), OpLoc: ELoc, Opc: BO_Assign, LHSExpr: PseudoDstExpr, RHSExpr: PseudoSrcExpr);
21580 if (AssignmentOp.isInvalid())
21581 continue;
21582 AssignmentOp = SemaRef.ActOnFinishFullExpr(Expr: AssignmentOp.get(), CC: ELoc,
21583 /*DiscardedValue=*/false);
21584 if (AssignmentOp.isInvalid())
21585 continue;
21586
21587 // No need to mark vars as copyprivate, they are already threadprivate or
21588 // implicitly private.
21589 assert(VD || isOpenMPCapturedDecl(D));
21590 Vars.push_back(
21591 Elt: VD ? RefExpr->IgnoreParens()
21592 : buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/false));
21593 SrcExprs.push_back(Elt: PseudoSrcExpr);
21594 DstExprs.push_back(Elt: PseudoDstExpr);
21595 AssignmentOps.push_back(Elt: AssignmentOp.get());
21596 }
21597
21598 if (Vars.empty())
21599 return nullptr;
21600
21601 return OMPCopyprivateClause::Create(C: getASTContext(), StartLoc, LParenLoc,
21602 EndLoc, VL: Vars, SrcExprs, DstExprs,
21603 AssignmentOps);
21604}
21605
21606OMPClause *SemaOpenMP::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
21607 SourceLocation StartLoc,
21608 SourceLocation LParenLoc,
21609 SourceLocation EndLoc) {
21610 if (VarList.empty())
21611 return nullptr;
21612
21613 return OMPFlushClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
21614 VL: VarList);
21615}
21616
21617/// Tries to find omp_depend_t. type.
21618static bool findOMPDependT(Sema &S, SourceLocation Loc, DSAStackTy *Stack,
21619 bool Diagnose = true) {
21620 QualType OMPDependT = Stack->getOMPDependT();
21621 if (!OMPDependT.isNull())
21622 return true;
21623 IdentifierInfo *II = &S.PP.getIdentifierTable().get(Name: "omp_depend_t");
21624 ParsedType PT = S.getTypeName(II: *II, NameLoc: Loc, S: S.getCurScope());
21625 if (!PT.getAsOpaquePtr() || PT.get().isNull()) {
21626 if (Diagnose)
21627 S.Diag(Loc, DiagID: diag::err_omp_implied_type_not_found) << "omp_depend_t";
21628 return false;
21629 }
21630 Stack->setOMPDependT(PT.get());
21631 return true;
21632}
21633
21634OMPClause *SemaOpenMP::ActOnOpenMPDepobjClause(Expr *Depobj,
21635 SourceLocation StartLoc,
21636 SourceLocation LParenLoc,
21637 SourceLocation EndLoc) {
21638 if (!Depobj)
21639 return nullptr;
21640
21641 bool OMPDependTFound = findOMPDependT(S&: SemaRef, Loc: StartLoc, DSAStack);
21642
21643 // OpenMP 5.0, 2.17.10.1 depobj Construct
21644 // depobj is an lvalue expression of type omp_depend_t.
21645 if (!Depobj->isTypeDependent() && !Depobj->isValueDependent() &&
21646 !Depobj->isInstantiationDependent() &&
21647 !Depobj->containsUnexpandedParameterPack() &&
21648 (OMPDependTFound && !getASTContext().typesAreCompatible(
21649 DSAStack->getOMPDependT(), T2: Depobj->getType(),
21650 /*CompareUnqualified=*/true))) {
21651 Diag(Loc: Depobj->getExprLoc(), DiagID: diag::err_omp_expected_omp_depend_t_lvalue)
21652 << 0 << Depobj->getType() << Depobj->getSourceRange();
21653 }
21654
21655 if (!Depobj->isLValue()) {
21656 Diag(Loc: Depobj->getExprLoc(), DiagID: diag::err_omp_expected_omp_depend_t_lvalue)
21657 << 1 << Depobj->getSourceRange();
21658 }
21659
21660 return OMPDepobjClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
21661 Depobj);
21662}
21663
21664namespace {
21665// Utility struct that gathers the related info for doacross clause.
21666struct DoacrossDataInfoTy {
21667 // The list of expressions.
21668 SmallVector<Expr *, 8> Vars;
21669 // The OperatorOffset for doacross loop.
21670 DSAStackTy::OperatorOffsetTy OpsOffs;
21671 // The depended loop count.
21672 llvm::APSInt TotalDepCount;
21673};
21674} // namespace
21675static DoacrossDataInfoTy
21676ProcessOpenMPDoacrossClauseCommon(Sema &SemaRef, bool IsSource,
21677 ArrayRef<Expr *> VarList, DSAStackTy *Stack,
21678 SourceLocation EndLoc) {
21679
21680 SmallVector<Expr *, 8> Vars;
21681 DSAStackTy::OperatorOffsetTy OpsOffs;
21682 llvm::APSInt DepCounter(/*BitWidth=*/32);
21683 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
21684
21685 if (const Expr *OrderedCountExpr =
21686 Stack->getParentOrderedRegionParam().first) {
21687 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Ctx: SemaRef.Context);
21688 TotalDepCount.setIsUnsigned(/*Val=*/true);
21689 }
21690
21691 for (Expr *RefExpr : VarList) {
21692 assert(RefExpr && "NULL expr in OpenMP doacross clause.");
21693 if (isa<DependentScopeDeclRefExpr>(Val: RefExpr)) {
21694 // It will be analyzed later.
21695 Vars.push_back(Elt: RefExpr);
21696 continue;
21697 }
21698
21699 SourceLocation ELoc = RefExpr->getExprLoc();
21700 Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
21701 if (!IsSource) {
21702 if (Stack->getParentOrderedRegionParam().first &&
21703 DepCounter >= TotalDepCount) {
21704 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_depend_sink_unexpected_expr);
21705 continue;
21706 }
21707 ++DepCounter;
21708 // OpenMP [2.13.9, Summary]
21709 // depend(dependence-type : vec), where dependence-type is:
21710 // 'sink' and where vec is the iteration vector, which has the form:
21711 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
21712 // where n is the value specified by the ordered clause in the loop
21713 // directive, xi denotes the loop iteration variable of the i-th nested
21714 // loop associated with the loop directive, and di is a constant
21715 // non-negative integer.
21716 if (SemaRef.CurContext->isDependentContext()) {
21717 // It will be analyzed later.
21718 Vars.push_back(Elt: RefExpr);
21719 continue;
21720 }
21721 SimpleExpr = SimpleExpr->IgnoreImplicit();
21722 OverloadedOperatorKind OOK = OO_None;
21723 SourceLocation OOLoc;
21724 Expr *LHS = SimpleExpr;
21725 Expr *RHS = nullptr;
21726 if (auto *BO = dyn_cast<BinaryOperator>(Val: SimpleExpr)) {
21727 OOK = BinaryOperator::getOverloadedOperator(Opc: BO->getOpcode());
21728 OOLoc = BO->getOperatorLoc();
21729 LHS = BO->getLHS()->IgnoreParenImpCasts();
21730 RHS = BO->getRHS()->IgnoreParenImpCasts();
21731 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(Val: SimpleExpr)) {
21732 OOK = OCE->getOperator();
21733 OOLoc = OCE->getOperatorLoc();
21734 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
21735 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
21736 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(Val: SimpleExpr)) {
21737 OOK = MCE->getMethodDecl()
21738 ->getNameInfo()
21739 .getName()
21740 .getCXXOverloadedOperator();
21741 OOLoc = MCE->getCallee()->getExprLoc();
21742 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
21743 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
21744 }
21745 SourceLocation ELoc;
21746 SourceRange ERange;
21747 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: LHS, ELoc, ERange);
21748 if (Res.second) {
21749 // It will be analyzed later.
21750 Vars.push_back(Elt: RefExpr);
21751 }
21752 ValueDecl *D = Res.first;
21753 if (!D)
21754 continue;
21755
21756 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
21757 SemaRef.Diag(Loc: OOLoc, DiagID: diag::err_omp_depend_sink_expected_plus_minus);
21758 continue;
21759 }
21760 if (RHS) {
21761 ExprResult RHSRes =
21762 SemaRef.OpenMP().VerifyPositiveIntegerConstantInClause(
21763 E: RHS, CKind: OMPC_depend, /*StrictlyPositive=*/false);
21764 if (RHSRes.isInvalid())
21765 continue;
21766 }
21767 if (!SemaRef.CurContext->isDependentContext() &&
21768 Stack->getParentOrderedRegionParam().first &&
21769 DepCounter != Stack->isParentLoopControlVariable(D).first) {
21770 const ValueDecl *VD =
21771 Stack->getParentLoopControlVariable(I: DepCounter.getZExtValue());
21772 if (VD)
21773 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_depend_sink_expected_loop_iteration)
21774 << 1 << VD;
21775 else
21776 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_depend_sink_expected_loop_iteration)
21777 << 0;
21778 continue;
21779 }
21780 OpsOffs.emplace_back(Args&: RHS, Args&: OOK);
21781 }
21782 Vars.push_back(Elt: RefExpr->IgnoreParenImpCasts());
21783 }
21784 if (!SemaRef.CurContext->isDependentContext() && !IsSource &&
21785 TotalDepCount > VarList.size() &&
21786 Stack->getParentOrderedRegionParam().first &&
21787 Stack->getParentLoopControlVariable(I: VarList.size() + 1)) {
21788 SemaRef.Diag(Loc: EndLoc, DiagID: diag::err_omp_depend_sink_expected_loop_iteration)
21789 << 1 << Stack->getParentLoopControlVariable(I: VarList.size() + 1);
21790 }
21791 return {.Vars: Vars, .OpsOffs: OpsOffs, .TotalDepCount: TotalDepCount};
21792}
21793
21794OMPClause *SemaOpenMP::ActOnOpenMPDependClause(
21795 const OMPDependClause::DependDataTy &Data, Expr *DepModifier,
21796 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
21797 SourceLocation EndLoc) {
21798 OpenMPDependClauseKind DepKind = Data.DepKind;
21799 SourceLocation DepLoc = Data.DepLoc;
21800 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
21801 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
21802 Diag(Loc: DepLoc, DiagID: diag::err_omp_unexpected_clause_value)
21803 << "'source' or 'sink'" << getOpenMPClauseNameForDiag(C: OMPC_depend);
21804 return nullptr;
21805 }
21806 if (DSAStack->getCurrentDirective() == OMPD_taskwait &&
21807 DepKind == OMPC_DEPEND_mutexinoutset) {
21808 Diag(Loc: DepLoc, DiagID: diag::err_omp_taskwait_depend_mutexinoutset_not_allowed);
21809 return nullptr;
21810 }
21811 if ((DSAStack->getCurrentDirective() != OMPD_ordered ||
21812 DSAStack->getCurrentDirective() == OMPD_depobj) &&
21813 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
21814 DepKind == OMPC_DEPEND_sink ||
21815 ((getLangOpts().OpenMP < 50 ||
21816 DSAStack->getCurrentDirective() == OMPD_depobj) &&
21817 DepKind == OMPC_DEPEND_depobj))) {
21818 SmallVector<unsigned, 6> Except = {OMPC_DEPEND_source, OMPC_DEPEND_sink,
21819 OMPC_DEPEND_outallmemory,
21820 OMPC_DEPEND_inoutallmemory};
21821 if (getLangOpts().OpenMP < 50 ||
21822 DSAStack->getCurrentDirective() == OMPD_depobj)
21823 Except.push_back(Elt: OMPC_DEPEND_depobj);
21824 if (getLangOpts().OpenMP < 51)
21825 Except.push_back(Elt: OMPC_DEPEND_inoutset);
21826 std::string Expected = (getLangOpts().OpenMP >= 50 && !DepModifier)
21827 ? "depend modifier(iterator) or "
21828 : "";
21829 Diag(Loc: DepLoc, DiagID: diag::err_omp_unexpected_clause_value)
21830 << Expected + getListOfPossibleValues(K: OMPC_depend, /*First=*/0,
21831 /*Last=*/OMPC_DEPEND_unknown,
21832 Exclude: Except)
21833 << getOpenMPClauseNameForDiag(C: OMPC_depend);
21834 return nullptr;
21835 }
21836 if (DepModifier &&
21837 (DepKind == OMPC_DEPEND_source || DepKind == OMPC_DEPEND_sink)) {
21838 Diag(Loc: DepModifier->getExprLoc(),
21839 DiagID: diag::err_omp_depend_sink_source_with_modifier);
21840 return nullptr;
21841 }
21842 if (DepModifier &&
21843 !DepModifier->getType()->isSpecificBuiltinType(K: BuiltinType::OMPIterator))
21844 Diag(Loc: DepModifier->getExprLoc(), DiagID: diag::err_omp_depend_modifier_not_iterator);
21845
21846 SmallVector<Expr *, 8> Vars;
21847 DSAStackTy::OperatorOffsetTy OpsOffs;
21848 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
21849
21850 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
21851 DoacrossDataInfoTy VarOffset = ProcessOpenMPDoacrossClauseCommon(
21852 SemaRef, IsSource: DepKind == OMPC_DEPEND_source, VarList, DSAStack, EndLoc);
21853 Vars = VarOffset.Vars;
21854 OpsOffs = VarOffset.OpsOffs;
21855 TotalDepCount = VarOffset.TotalDepCount;
21856 } else {
21857 for (Expr *RefExpr : VarList) {
21858 assert(RefExpr && "NULL expr in OpenMP depend clause.");
21859 if (isa<DependentScopeDeclRefExpr>(Val: RefExpr)) {
21860 // It will be analyzed later.
21861 Vars.push_back(Elt: RefExpr);
21862 continue;
21863 }
21864
21865 SourceLocation ELoc = RefExpr->getExprLoc();
21866 Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
21867 if (DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) {
21868 bool OMPDependTFound = getLangOpts().OpenMP >= 50;
21869 if (OMPDependTFound)
21870 OMPDependTFound = findOMPDependT(S&: SemaRef, Loc: StartLoc, DSAStack,
21871 Diagnose: DepKind == OMPC_DEPEND_depobj);
21872 if (DepKind == OMPC_DEPEND_depobj) {
21873 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++
21874 // List items used in depend clauses with the depobj dependence type
21875 // must be expressions of the omp_depend_t type.
21876 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() &&
21877 !RefExpr->isInstantiationDependent() &&
21878 !RefExpr->containsUnexpandedParameterPack() &&
21879 (OMPDependTFound &&
21880 !getASTContext().hasSameUnqualifiedType(
21881 DSAStack->getOMPDependT(), T2: RefExpr->getType()))) {
21882 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_omp_depend_t_lvalue)
21883 << 0 << RefExpr->getType() << RefExpr->getSourceRange();
21884 continue;
21885 }
21886 if (!RefExpr->isLValue()) {
21887 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_omp_depend_t_lvalue)
21888 << 1 << RefExpr->getType() << RefExpr->getSourceRange();
21889 continue;
21890 }
21891 } else {
21892 // OpenMP 5.0 [2.17.11, Restrictions]
21893 // List items used in depend clauses cannot be zero-length array
21894 // sections.
21895 QualType ExprTy = RefExpr->getType().getNonReferenceType();
21896 const auto *OASE = dyn_cast<ArraySectionExpr>(Val: SimpleExpr);
21897 if (OASE) {
21898 QualType BaseType =
21899 ArraySectionExpr::getBaseOriginalType(Base: OASE->getBase());
21900 if (BaseType.isNull())
21901 return nullptr;
21902 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
21903 ExprTy = ATy->getElementType();
21904 else
21905 ExprTy = BaseType->getPointeeType();
21906 if (BaseType.isNull() || ExprTy.isNull())
21907 return nullptr;
21908 ExprTy = ExprTy.getNonReferenceType();
21909 const Expr *Length = OASE->getLength();
21910 Expr::EvalResult Result;
21911 if (Length && !Length->isValueDependent() &&
21912 Length->EvaluateAsInt(Result, Ctx: getASTContext()) &&
21913 Result.Val.getInt().isZero()) {
21914 Diag(Loc: ELoc,
21915 DiagID: diag::err_omp_depend_zero_length_array_section_not_allowed)
21916 << SimpleExpr->getSourceRange();
21917 continue;
21918 }
21919 }
21920
21921 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++
21922 // List items used in depend clauses with the in, out, inout,
21923 // inoutset, or mutexinoutset dependence types cannot be
21924 // expressions of the omp_depend_t type.
21925 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() &&
21926 !RefExpr->isInstantiationDependent() &&
21927 !RefExpr->containsUnexpandedParameterPack() &&
21928 (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
21929 (OMPDependTFound && DSAStack->getOMPDependT().getTypePtr() ==
21930 ExprTy.getTypePtr()))) {
21931 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_addressable_lvalue_or_array_item)
21932 << (getLangOpts().OpenMP >= 50 ? 1 : 0)
21933 << (getLangOpts().OpenMP >= 50 ? 1 : 0)
21934 << RefExpr->getSourceRange();
21935 continue;
21936 }
21937
21938 auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: SimpleExpr);
21939 if (ASE && !ASE->getBase()->isTypeDependent() &&
21940 !ASE->getBase()
21941 ->getType()
21942 .getNonReferenceType()
21943 ->isPointerType() &&
21944 !ASE->getBase()->getType().getNonReferenceType()->isArrayType()) {
21945 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_addressable_lvalue_or_array_item)
21946 << (getLangOpts().OpenMP >= 50 ? 1 : 0)
21947 << (getLangOpts().OpenMP >= 50 ? 1 : 0)
21948 << RefExpr->getSourceRange();
21949 continue;
21950 }
21951
21952 ExprResult Res;
21953 {
21954 Sema::TentativeAnalysisScope Trap(SemaRef);
21955 Res = SemaRef.CreateBuiltinUnaryOp(OpLoc: ELoc, Opc: UO_AddrOf,
21956 InputExpr: RefExpr->IgnoreParenImpCasts());
21957 }
21958 if (!Res.isUsable() && !isa<ArraySectionExpr>(Val: SimpleExpr) &&
21959 !isa<OMPArrayShapingExpr>(Val: SimpleExpr)) {
21960 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_addressable_lvalue_or_array_item)
21961 << (getLangOpts().OpenMP >= 50 ? 1 : 0)
21962 << (getLangOpts().OpenMP >= 50 ? 1 : 0)
21963 << RefExpr->getSourceRange();
21964 continue;
21965 }
21966 }
21967 }
21968 Vars.push_back(Elt: RefExpr->IgnoreParenImpCasts());
21969 }
21970 }
21971
21972 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
21973 DepKind != OMPC_DEPEND_outallmemory &&
21974 DepKind != OMPC_DEPEND_inoutallmemory && Vars.empty())
21975 return nullptr;
21976
21977 auto *C = OMPDependClause::Create(
21978 C: getASTContext(), StartLoc, LParenLoc, EndLoc,
21979 Data: {.DepKind: DepKind, .DepLoc: DepLoc, .ColonLoc: Data.ColonLoc, .OmpAllMemoryLoc: Data.OmpAllMemoryLoc}, DepModifier, VL: Vars,
21980 NumLoops: TotalDepCount.getZExtValue());
21981 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
21982 DSAStack->isParentOrderedRegion())
21983 DSAStack->addDoacrossDependClause(C, OpsOffs);
21984 return C;
21985}
21986
21987OMPClause *SemaOpenMP::ActOnOpenMPDeviceClause(
21988 OpenMPDeviceClauseModifier Modifier, Expr *Device, SourceLocation StartLoc,
21989 SourceLocation LParenLoc, SourceLocation ModifierLoc,
21990 SourceLocation EndLoc) {
21991 assert((ModifierLoc.isInvalid() || getLangOpts().OpenMP >= 50) &&
21992 "Unexpected device modifier in OpenMP < 50.");
21993
21994 bool ErrorFound = false;
21995 if (ModifierLoc.isValid() && Modifier == OMPC_DEVICE_unknown) {
21996 std::string Values =
21997 getListOfPossibleValues(K: OMPC_device, /*First=*/0, Last: OMPC_DEVICE_unknown);
21998 Diag(Loc: ModifierLoc, DiagID: diag::err_omp_unexpected_clause_value)
21999 << Values << getOpenMPClauseNameForDiag(C: OMPC_device);
22000 ErrorFound = true;
22001 }
22002
22003 Expr *ValExpr = Device;
22004 Stmt *HelperValStmt = nullptr;
22005
22006 // OpenMP [2.9.1, Restrictions]
22007 // The device expression must evaluate to a non-negative integer value.
22008 ErrorFound = !isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_device,
22009 /*StrictlyPositive=*/false) ||
22010 ErrorFound;
22011 if (ErrorFound)
22012 return nullptr;
22013
22014 // OpenMP 5.0 [2.12.5, Restrictions]
22015 // In case of ancestor device-modifier, a requires directive with
22016 // the reverse_offload clause must be specified.
22017 if (Modifier == OMPC_DEVICE_ancestor) {
22018 if (!DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>()) {
22019 SemaRef.targetDiag(
22020 Loc: StartLoc,
22021 DiagID: diag::err_omp_device_ancestor_without_requires_reverse_offload);
22022 ErrorFound = true;
22023 }
22024 }
22025
22026 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
22027 OpenMPDirectiveKind CaptureRegion =
22028 getOpenMPCaptureRegionForClause(DKind, CKind: OMPC_device, OpenMPVersion: getLangOpts().OpenMP);
22029 if (CaptureRegion != OMPD_unknown &&
22030 !SemaRef.CurContext->isDependentContext()) {
22031 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
22032 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
22033 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
22034 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
22035 }
22036
22037 return new (getASTContext())
22038 OMPDeviceClause(Modifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
22039 LParenLoc, ModifierLoc, EndLoc);
22040}
22041
22042static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
22043 DSAStackTy *Stack, QualType QTy,
22044 bool FullCheck = true) {
22045 if (SemaRef.RequireCompleteType(Loc: SL, T: QTy, DiagID: diag::err_incomplete_type))
22046 return false;
22047 if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
22048 !QTy.isTriviallyCopyableType(Context: SemaRef.Context))
22049 SemaRef.Diag(Loc: SL, DiagID: diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
22050 return true;
22051}
22052
22053/// Return true if it can be proven that the provided array expression
22054/// (array section or array subscript) does NOT specify the whole size of the
22055/// array whose base type is \a BaseQTy.
22056static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
22057 const Expr *E,
22058 QualType BaseQTy) {
22059 const auto *OASE = dyn_cast<ArraySectionExpr>(Val: E);
22060
22061 // If this is an array subscript, it refers to the whole size if the size of
22062 // the dimension is constant and equals 1. Also, an array section assumes the
22063 // format of an array subscript if no colon is used.
22064 if (isa<ArraySubscriptExpr>(Val: E) ||
22065 (OASE && OASE->getColonLocFirst().isInvalid())) {
22066 if (const auto *ATy = dyn_cast<ConstantArrayType>(Val: BaseQTy.getTypePtr()))
22067 return ATy->getSExtSize() != 1;
22068 // Size can't be evaluated statically.
22069 return false;
22070 }
22071
22072 assert(OASE && "Expecting array section if not an array subscript.");
22073 const Expr *LowerBound = OASE->getLowerBound();
22074 const Expr *Length = OASE->getLength();
22075
22076 // If there is a lower bound that does not evaluates to zero, we are not
22077 // covering the whole dimension.
22078 if (LowerBound) {
22079 Expr::EvalResult Result;
22080 if (!LowerBound->EvaluateAsInt(Result, Ctx: SemaRef.getASTContext()))
22081 return false; // Can't get the integer value as a constant.
22082
22083 llvm::APSInt ConstLowerBound = Result.Val.getInt();
22084 if (ConstLowerBound.getSExtValue())
22085 return true;
22086 }
22087
22088 // If we don't have a length we covering the whole dimension.
22089 if (!Length)
22090 return false;
22091
22092 // If the base is a pointer, we don't have a way to get the size of the
22093 // pointee.
22094 if (BaseQTy->isPointerType())
22095 return false;
22096
22097 // We can only check if the length is the same as the size of the dimension
22098 // if we have a constant array.
22099 const auto *CATy = dyn_cast<ConstantArrayType>(Val: BaseQTy.getTypePtr());
22100 if (!CATy)
22101 return false;
22102
22103 Expr::EvalResult Result;
22104 if (!Length->EvaluateAsInt(Result, Ctx: SemaRef.getASTContext()))
22105 return false; // Can't get the integer value as a constant.
22106
22107 llvm::APSInt ConstLength = Result.Val.getInt();
22108 return CATy->getSExtSize() != ConstLength.getSExtValue();
22109}
22110
22111// Return true if it can be proven that the provided array expression (array
22112// section or array subscript) does NOT specify a single element of the array
22113// whose base type is \a BaseQTy.
22114static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
22115 const Expr *E,
22116 QualType BaseQTy) {
22117 const auto *OASE = dyn_cast<ArraySectionExpr>(Val: E);
22118
22119 // An array subscript always refer to a single element. Also, an array section
22120 // assumes the format of an array subscript if no colon is used.
22121 if (isa<ArraySubscriptExpr>(Val: E) ||
22122 (OASE && OASE->getColonLocFirst().isInvalid()))
22123 return false;
22124
22125 assert(OASE && "Expecting array section if not an array subscript.");
22126 const Expr *Length = OASE->getLength();
22127
22128 // If we don't have a length we have to check if the array has unitary size
22129 // for this dimension. Also, we should always expect a length if the base type
22130 // is pointer.
22131 if (!Length) {
22132 if (const auto *ATy = dyn_cast<ConstantArrayType>(Val: BaseQTy.getTypePtr()))
22133 return ATy->getSExtSize() != 1;
22134 // We cannot assume anything.
22135 return false;
22136 }
22137
22138 // Check if the length evaluates to 1.
22139 Expr::EvalResult Result;
22140 if (!Length->EvaluateAsInt(Result, Ctx: SemaRef.getASTContext()))
22141 return false; // Can't get the integer value as a constant.
22142
22143 llvm::APSInt ConstLength = Result.Val.getInt();
22144 return ConstLength.getSExtValue() != 1;
22145}
22146
22147// The base of elements of list in a map clause have to be either:
22148// - a reference to variable or field.
22149// - a member expression.
22150// - an array expression.
22151//
22152// E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
22153// reference to 'r'.
22154//
22155// If we have:
22156//
22157// struct SS {
22158// Bla S;
22159// foo() {
22160// #pragma omp target map (S.Arr[:12]);
22161// }
22162// }
22163//
22164// We want to retrieve the member expression 'this->S';
22165
22166// OpenMP 5.0 [2.19.7.1, map Clause, Restrictions, p.2]
22167// If a list item is an array section, it must specify contiguous storage.
22168//
22169// For this restriction it is sufficient that we make sure only references
22170// to variables or fields and array expressions, and that no array sections
22171// exist except in the rightmost expression (unless they cover the whole
22172// dimension of the array). E.g. these would be invalid:
22173//
22174// r.ArrS[3:5].Arr[6:7]
22175//
22176// r.ArrS[3:5].x
22177//
22178// but these would be valid:
22179// r.ArrS[3].Arr[6:7]
22180//
22181// r.ArrS[3].x
22182namespace {
22183class MapBaseChecker final : public StmtVisitor<MapBaseChecker, bool> {
22184 Sema &SemaRef;
22185 OpenMPClauseKind CKind = OMPC_unknown;
22186 OpenMPDirectiveKind DKind = OMPD_unknown;
22187 OMPClauseMappableExprCommon::MappableExprComponentList &Components;
22188 bool IsNonContiguous = false;
22189 bool NoDiagnose = false;
22190 const Expr *RelevantExpr = nullptr;
22191 bool AllowUnitySizeArraySection = true;
22192 bool AllowWholeSizeArraySection = true;
22193 bool AllowAnotherPtr = true;
22194 SourceLocation ELoc;
22195 SourceRange ERange;
22196
22197 void emitErrorMsg() {
22198 // If nothing else worked, this is not a valid map clause expression.
22199 if (SemaRef.getLangOpts().OpenMP < 50) {
22200 SemaRef.Diag(Loc: ELoc,
22201 DiagID: diag::err_omp_expected_named_var_member_or_array_expression)
22202 << ERange;
22203 } else {
22204 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_non_lvalue_in_map_or_motion_clauses)
22205 << getOpenMPClauseNameForDiag(C: CKind) << ERange;
22206 }
22207 }
22208
22209public:
22210 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
22211 if (!isa<VarDecl>(Val: DRE->getDecl())) {
22212 emitErrorMsg();
22213 return false;
22214 }
22215 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
22216 RelevantExpr = DRE;
22217 // Record the component.
22218 Components.emplace_back(Args&: DRE, Args: DRE->getDecl(), Args&: IsNonContiguous);
22219 return true;
22220 }
22221
22222 bool VisitMemberExpr(MemberExpr *ME) {
22223 Expr *E = ME;
22224 Expr *BaseE = ME->getBase()->IgnoreParenCasts();
22225
22226 if (isa<CXXThisExpr>(Val: BaseE)) {
22227 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
22228 // We found a base expression: this->Val.
22229 RelevantExpr = ME;
22230 } else {
22231 E = BaseE;
22232 }
22233
22234 if (!isa<FieldDecl>(Val: ME->getMemberDecl())) {
22235 if (!NoDiagnose) {
22236 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_expected_access_to_data_field)
22237 << ME->getSourceRange();
22238 return false;
22239 }
22240 if (RelevantExpr)
22241 return false;
22242 return Visit(S: E);
22243 }
22244
22245 auto *FD = cast<FieldDecl>(Val: ME->getMemberDecl());
22246
22247 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
22248 // A bit-field cannot appear in a map clause.
22249 //
22250 if (FD->isBitField()) {
22251 if (!NoDiagnose) {
22252 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_bit_fields_forbidden_in_clause)
22253 << ME->getSourceRange() << getOpenMPClauseNameForDiag(C: CKind);
22254 return false;
22255 }
22256 if (RelevantExpr)
22257 return false;
22258 return Visit(S: E);
22259 }
22260
22261 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
22262 // If the type of a list item is a reference to a type T then the type
22263 // will be considered to be T for all purposes of this clause.
22264 QualType CurType = BaseE->getType().getNonReferenceType();
22265
22266 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
22267 // A list item cannot be a variable that is a member of a structure with
22268 // a union type.
22269 //
22270 if (CurType->isUnionType()) {
22271 if (!NoDiagnose) {
22272 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_union_type_not_allowed)
22273 << ME->getSourceRange();
22274 return false;
22275 }
22276 return RelevantExpr || Visit(S: E);
22277 }
22278
22279 // If we got a member expression, we should not expect any array section
22280 // before that:
22281 //
22282 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
22283 // If a list item is an element of a structure, only the rightmost symbol
22284 // of the variable reference can be an array section.
22285 //
22286 AllowUnitySizeArraySection = false;
22287 AllowWholeSizeArraySection = false;
22288
22289 // Record the component.
22290 Components.emplace_back(Args&: ME, Args&: FD, Args&: IsNonContiguous);
22291 return RelevantExpr || Visit(S: E);
22292 }
22293
22294 bool VisitArraySubscriptExpr(ArraySubscriptExpr *AE) {
22295 Expr *E = AE->getBase()->IgnoreParenImpCasts();
22296
22297 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
22298 if (!NoDiagnose) {
22299 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_expected_base_var_name)
22300 << 0 << AE->getSourceRange();
22301 return false;
22302 }
22303 return RelevantExpr || Visit(S: E);
22304 }
22305
22306 // If we got an array subscript that express the whole dimension we
22307 // can have any array expressions before. If it only expressing part of
22308 // the dimension, we can only have unitary-size array expressions.
22309 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, E: AE, BaseQTy: E->getType()))
22310 AllowWholeSizeArraySection = false;
22311
22312 if (const auto *TE = dyn_cast<CXXThisExpr>(Val: E->IgnoreParenCasts())) {
22313 Expr::EvalResult Result;
22314 if (!AE->getIdx()->isValueDependent() &&
22315 AE->getIdx()->EvaluateAsInt(Result, Ctx: SemaRef.getASTContext()) &&
22316 !Result.Val.getInt().isZero()) {
22317 SemaRef.Diag(Loc: AE->getIdx()->getExprLoc(),
22318 DiagID: diag::err_omp_invalid_map_this_expr);
22319 SemaRef.Diag(Loc: AE->getIdx()->getExprLoc(),
22320 DiagID: diag::note_omp_invalid_subscript_on_this_ptr_map);
22321 }
22322 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
22323 RelevantExpr = TE;
22324 }
22325
22326 // Record the component - we don't have any declaration associated.
22327 Components.emplace_back(Args&: AE, Args: nullptr, Args&: IsNonContiguous);
22328
22329 return RelevantExpr || Visit(S: E);
22330 }
22331
22332 bool VisitArraySectionExpr(ArraySectionExpr *OASE) {
22333 // After OMP 5.0 Array section in reduction clause will be implicitly
22334 // mapped
22335 assert(!(SemaRef.getLangOpts().OpenMP < 50 && NoDiagnose) &&
22336 "Array sections cannot be implicitly mapped.");
22337 Expr *E = OASE->getBase()->IgnoreParenImpCasts();
22338 QualType CurType =
22339 ArraySectionExpr::getBaseOriginalType(Base: E).getCanonicalType();
22340
22341 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
22342 // If the type of a list item is a reference to a type T then the type
22343 // will be considered to be T for all purposes of this clause.
22344 if (CurType->isReferenceType())
22345 CurType = CurType->getPointeeType();
22346
22347 bool IsPointer = CurType->isAnyPointerType();
22348
22349 if (!IsPointer && !CurType->isArrayType()) {
22350 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_expected_base_var_name)
22351 << 0 << OASE->getSourceRange();
22352 return false;
22353 }
22354
22355 bool NotWhole =
22356 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, E: OASE, BaseQTy: CurType);
22357 bool NotUnity =
22358 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, E: OASE, BaseQTy: CurType);
22359
22360 if (AllowWholeSizeArraySection) {
22361 // Any array section is currently allowed. Allowing a whole size array
22362 // section implies allowing a unity array section as well.
22363 //
22364 // If this array section refers to the whole dimension we can still
22365 // accept other array sections before this one, except if the base is a
22366 // pointer. Otherwise, only unitary sections are accepted.
22367 if (NotWhole || IsPointer)
22368 AllowWholeSizeArraySection = false;
22369 } else if (DKind == OMPD_target_update &&
22370 SemaRef.getLangOpts().OpenMP >= 50) {
22371 if (IsPointer && !AllowAnotherPtr)
22372 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_section_length_undefined)
22373 << /*array of unknown bound */ 1;
22374 else
22375 IsNonContiguous = true;
22376 } else if (AllowUnitySizeArraySection && NotUnity) {
22377 // A unity or whole array section is not allowed and that is not
22378 // compatible with the properties of the current array section.
22379 if (NoDiagnose)
22380 return false;
22381 SemaRef.Diag(Loc: ELoc,
22382 DiagID: diag::err_array_section_does_not_specify_contiguous_storage)
22383 << OASE->getSourceRange();
22384 return false;
22385 }
22386
22387 if (IsPointer)
22388 AllowAnotherPtr = false;
22389
22390 if (const auto *TE = dyn_cast<CXXThisExpr>(Val: E)) {
22391 Expr::EvalResult ResultR;
22392 Expr::EvalResult ResultL;
22393 if (!OASE->getLength()->isValueDependent() &&
22394 OASE->getLength()->EvaluateAsInt(Result&: ResultR, Ctx: SemaRef.getASTContext()) &&
22395 !ResultR.Val.getInt().isOne()) {
22396 SemaRef.Diag(Loc: OASE->getLength()->getExprLoc(),
22397 DiagID: diag::err_omp_invalid_map_this_expr);
22398 SemaRef.Diag(Loc: OASE->getLength()->getExprLoc(),
22399 DiagID: diag::note_omp_invalid_length_on_this_ptr_mapping);
22400 }
22401 if (OASE->getLowerBound() && !OASE->getLowerBound()->isValueDependent() &&
22402 OASE->getLowerBound()->EvaluateAsInt(Result&: ResultL,
22403 Ctx: SemaRef.getASTContext()) &&
22404 !ResultL.Val.getInt().isZero()) {
22405 SemaRef.Diag(Loc: OASE->getLowerBound()->getExprLoc(),
22406 DiagID: diag::err_omp_invalid_map_this_expr);
22407 SemaRef.Diag(Loc: OASE->getLowerBound()->getExprLoc(),
22408 DiagID: diag::note_omp_invalid_lower_bound_on_this_ptr_mapping);
22409 }
22410 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
22411 RelevantExpr = TE;
22412 }
22413
22414 // Record the component - we don't have any declaration associated.
22415 Components.emplace_back(Args&: OASE, Args: nullptr, /*IsNonContiguous=*/Args: false);
22416 return RelevantExpr || Visit(S: E);
22417 }
22418 bool VisitOMPArrayShapingExpr(OMPArrayShapingExpr *E) {
22419 Expr *Base = E->getBase();
22420
22421 // Record the component - we don't have any declaration associated.
22422 Components.emplace_back(Args&: E, Args: nullptr, Args&: IsNonContiguous);
22423
22424 return Visit(S: Base->IgnoreParenImpCasts());
22425 }
22426
22427 bool VisitUnaryOperator(UnaryOperator *UO) {
22428 if (SemaRef.getLangOpts().OpenMP < 50 || !UO->isLValue() ||
22429 UO->getOpcode() != UO_Deref) {
22430 emitErrorMsg();
22431 return false;
22432 }
22433 if (!RelevantExpr) {
22434 // Record the component if haven't found base decl.
22435 Components.emplace_back(Args&: UO, Args: nullptr, /*IsNonContiguous=*/Args: false);
22436 }
22437 return RelevantExpr || Visit(S: UO->getSubExpr()->IgnoreParenImpCasts());
22438 }
22439 bool VisitBinaryOperator(BinaryOperator *BO) {
22440 if (SemaRef.getLangOpts().OpenMP < 50 || !BO->getType()->isPointerType()) {
22441 emitErrorMsg();
22442 return false;
22443 }
22444
22445 // Pointer arithmetic is the only thing we expect to happen here so after we
22446 // make sure the binary operator is a pointer type, the only thing we need
22447 // to do is to visit the subtree that has the same type as root (so that we
22448 // know the other subtree is just an offset)
22449 Expr *LE = BO->getLHS()->IgnoreParenImpCasts();
22450 Expr *RE = BO->getRHS()->IgnoreParenImpCasts();
22451 Components.emplace_back(Args&: BO, Args: nullptr, Args: false);
22452 assert((LE->getType().getTypePtr() == BO->getType().getTypePtr() ||
22453 RE->getType().getTypePtr() == BO->getType().getTypePtr()) &&
22454 "Either LHS or RHS have base decl inside");
22455 if (BO->getType().getTypePtr() == LE->getType().getTypePtr())
22456 return RelevantExpr || Visit(S: LE);
22457 return RelevantExpr || Visit(S: RE);
22458 }
22459 bool VisitCXXThisExpr(CXXThisExpr *CTE) {
22460 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
22461 RelevantExpr = CTE;
22462 Components.emplace_back(Args&: CTE, Args: nullptr, Args&: IsNonContiguous);
22463 return true;
22464 }
22465 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *COCE) {
22466 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
22467 Components.emplace_back(Args&: COCE, Args: nullptr, Args&: IsNonContiguous);
22468 return true;
22469 }
22470 bool VisitOpaqueValueExpr(OpaqueValueExpr *E) {
22471 Expr *Source = E->getSourceExpr();
22472 if (!Source) {
22473 emitErrorMsg();
22474 return false;
22475 }
22476 return Visit(S: Source);
22477 }
22478 bool VisitStmt(Stmt *) {
22479 emitErrorMsg();
22480 return false;
22481 }
22482 const Expr *getFoundBase() const { return RelevantExpr; }
22483 explicit MapBaseChecker(
22484 Sema &SemaRef, OpenMPClauseKind CKind, OpenMPDirectiveKind DKind,
22485 OMPClauseMappableExprCommon::MappableExprComponentList &Components,
22486 bool NoDiagnose, SourceLocation &ELoc, SourceRange &ERange)
22487 : SemaRef(SemaRef), CKind(CKind), DKind(DKind), Components(Components),
22488 NoDiagnose(NoDiagnose), ELoc(ELoc), ERange(ERange) {}
22489};
22490} // namespace
22491
22492/// Return the expression of the base of the mappable expression or null if it
22493/// cannot be determined and do all the necessary checks to see if the
22494/// expression is valid as a standalone mappable expression. In the process,
22495/// record all the components of the expression.
22496static const Expr *checkMapClauseExpressionBase(
22497 Sema &SemaRef, Expr *E,
22498 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
22499 OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, bool NoDiagnose) {
22500 SourceLocation ELoc = E->getExprLoc();
22501 SourceRange ERange = E->getSourceRange();
22502 MapBaseChecker Checker(SemaRef, CKind, DKind, CurComponents, NoDiagnose, ELoc,
22503 ERange);
22504 if (Checker.Visit(S: E->IgnoreParens())) {
22505 // Check if the highest dimension array section has length specified
22506 if (SemaRef.getLangOpts().OpenMP >= 50 && !CurComponents.empty() &&
22507 (CKind == OMPC_to || CKind == OMPC_from)) {
22508 auto CI = CurComponents.rbegin();
22509 auto CE = CurComponents.rend();
22510 for (; CI != CE; ++CI) {
22511 const auto *OASE =
22512 dyn_cast<ArraySectionExpr>(Val: CI->getAssociatedExpression());
22513 if (!OASE)
22514 continue;
22515 if (OASE && OASE->getLength())
22516 break;
22517 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_array_section_does_not_specify_length)
22518 << ERange;
22519 }
22520 }
22521 return Checker.getFoundBase();
22522 }
22523 return nullptr;
22524}
22525
22526// Return true if expression E associated with value VD has conflicts with other
22527// map information.
22528static bool checkMapConflicts(
22529 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
22530 bool CurrentRegionOnly,
22531 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
22532 OpenMPClauseKind CKind) {
22533 assert(VD && E);
22534 SourceLocation ELoc = E->getExprLoc();
22535 SourceRange ERange = E->getSourceRange();
22536
22537 // In order to easily check the conflicts we need to match each component of
22538 // the expression under test with the components of the expressions that are
22539 // already in the stack.
22540
22541 assert(!CurComponents.empty() && "Map clause expression with no components!");
22542 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
22543 "Map clause expression with unexpected base!");
22544
22545 // Variables to help detecting enclosing problems in data environment nests.
22546 bool IsEnclosedByDataEnvironmentExpr = false;
22547 const Expr *EnclosingExpr = nullptr;
22548
22549 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
22550 VD, CurrentRegionOnly,
22551 Check: [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
22552 ERange, CKind, &EnclosingExpr,
22553 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
22554 StackComponents,
22555 OpenMPClauseKind Kind) {
22556 if (CKind == Kind && SemaRef.LangOpts.OpenMP >= 50)
22557 return false;
22558 assert(!StackComponents.empty() &&
22559 "Map clause expression with no components!");
22560 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
22561 "Map clause expression with unexpected base!");
22562 (void)VD;
22563
22564 // The whole expression in the stack.
22565 const Expr *RE = StackComponents.front().getAssociatedExpression();
22566
22567 // Expressions must start from the same base. Here we detect at which
22568 // point both expressions diverge from each other and see if we can
22569 // detect if the memory referred to both expressions is contiguous and
22570 // do not overlap.
22571 auto CI = CurComponents.rbegin();
22572 auto CE = CurComponents.rend();
22573 auto SI = StackComponents.rbegin();
22574 auto SE = StackComponents.rend();
22575 for (; CI != CE && SI != SE; ++CI, ++SI) {
22576
22577 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
22578 // At most one list item can be an array item derived from a given
22579 // variable in map clauses of the same construct.
22580 if (CurrentRegionOnly &&
22581 (isa<ArraySubscriptExpr>(Val: CI->getAssociatedExpression()) ||
22582 isa<ArraySectionExpr>(Val: CI->getAssociatedExpression()) ||
22583 isa<OMPArrayShapingExpr>(Val: CI->getAssociatedExpression())) &&
22584 (isa<ArraySubscriptExpr>(Val: SI->getAssociatedExpression()) ||
22585 isa<ArraySectionExpr>(Val: SI->getAssociatedExpression()) ||
22586 isa<OMPArrayShapingExpr>(Val: SI->getAssociatedExpression()))) {
22587 SemaRef.Diag(Loc: CI->getAssociatedExpression()->getExprLoc(),
22588 DiagID: diag::err_omp_multiple_array_items_in_map_clause)
22589 << CI->getAssociatedExpression()->getSourceRange();
22590 SemaRef.Diag(Loc: SI->getAssociatedExpression()->getExprLoc(),
22591 DiagID: diag::note_used_here)
22592 << SI->getAssociatedExpression()->getSourceRange();
22593 return true;
22594 }
22595
22596 // Do both expressions have the same kind?
22597 if (CI->getAssociatedExpression()->getStmtClass() !=
22598 SI->getAssociatedExpression()->getStmtClass())
22599 break;
22600
22601 // Are we dealing with different variables/fields?
22602 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
22603 break;
22604 }
22605 // Check if the extra components of the expressions in the enclosing
22606 // data environment are redundant for the current base declaration.
22607 // If they are, the maps completely overlap, which is legal.
22608 for (; SI != SE; ++SI) {
22609 QualType Type;
22610 if (const auto *ASE =
22611 dyn_cast<ArraySubscriptExpr>(Val: SI->getAssociatedExpression())) {
22612 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
22613 } else if (const auto *OASE = dyn_cast<ArraySectionExpr>(
22614 Val: SI->getAssociatedExpression())) {
22615 const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
22616 Type = ArraySectionExpr::getBaseOriginalType(Base: E).getCanonicalType();
22617 } else if (const auto *OASE = dyn_cast<OMPArrayShapingExpr>(
22618 Val: SI->getAssociatedExpression())) {
22619 Type = OASE->getBase()->getType()->getPointeeType();
22620 }
22621 if (Type.isNull() || Type->isAnyPointerType() ||
22622 checkArrayExpressionDoesNotReferToWholeSize(
22623 SemaRef, E: SI->getAssociatedExpression(), BaseQTy: Type))
22624 break;
22625 }
22626
22627 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
22628 // List items of map clauses in the same construct must not share
22629 // original storage.
22630 //
22631 // If the expressions are exactly the same or one is a subset of the
22632 // other, it means they are sharing storage.
22633 if (CI == CE && SI == SE) {
22634 if (CurrentRegionOnly) {
22635 if (CKind == OMPC_map) {
22636 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_map_shared_storage) << ERange;
22637 } else {
22638 assert(CKind == OMPC_to || CKind == OMPC_from);
22639 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_once_referenced_in_target_update)
22640 << ERange;
22641 }
22642 SemaRef.Diag(Loc: RE->getExprLoc(), DiagID: diag::note_used_here)
22643 << RE->getSourceRange();
22644 return true;
22645 }
22646 // If we find the same expression in the enclosing data environment,
22647 // that is legal.
22648 IsEnclosedByDataEnvironmentExpr = true;
22649 return false;
22650 }
22651
22652 QualType DerivedType =
22653 std::prev(x: CI)->getAssociatedDeclaration()->getType();
22654 SourceLocation DerivedLoc =
22655 std::prev(x: CI)->getAssociatedExpression()->getExprLoc();
22656
22657 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
22658 // If the type of a list item is a reference to a type T then the type
22659 // will be considered to be T for all purposes of this clause.
22660 DerivedType = DerivedType.getNonReferenceType();
22661
22662 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
22663 // A variable for which the type is pointer and an array section
22664 // derived from that variable must not appear as list items of map
22665 // clauses of the same construct.
22666 //
22667 // Also, cover one of the cases in:
22668 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
22669 // If any part of the original storage of a list item has corresponding
22670 // storage in the device data environment, all of the original storage
22671 // must have corresponding storage in the device data environment.
22672 //
22673 if (DerivedType->isAnyPointerType()) {
22674 if (CI == CE || SI == SE) {
22675 SemaRef.Diag(
22676 Loc: DerivedLoc,
22677 DiagID: diag::err_omp_pointer_mapped_along_with_derived_section)
22678 << DerivedLoc;
22679 SemaRef.Diag(Loc: RE->getExprLoc(), DiagID: diag::note_used_here)
22680 << RE->getSourceRange();
22681 return true;
22682 }
22683 if (CI->getAssociatedExpression()->getStmtClass() !=
22684 SI->getAssociatedExpression()->getStmtClass() ||
22685 CI->getAssociatedDeclaration()->getCanonicalDecl() ==
22686 SI->getAssociatedDeclaration()->getCanonicalDecl()) {
22687 assert(CI != CE && SI != SE);
22688 SemaRef.Diag(Loc: DerivedLoc, DiagID: diag::err_omp_same_pointer_dereferenced)
22689 << DerivedLoc;
22690 SemaRef.Diag(Loc: RE->getExprLoc(), DiagID: diag::note_used_here)
22691 << RE->getSourceRange();
22692 return true;
22693 }
22694 }
22695
22696 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
22697 // List items of map clauses in the same construct must not share
22698 // original storage.
22699 //
22700 // An expression is a subset of the other.
22701 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
22702 if (CKind == OMPC_map) {
22703 if (CI != CE || SI != SE) {
22704 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
22705 // a pointer.
22706 auto Begin =
22707 CI != CE ? CurComponents.begin() : StackComponents.begin();
22708 auto End = CI != CE ? CurComponents.end() : StackComponents.end();
22709 auto It = Begin;
22710 while (It != End && !It->getAssociatedDeclaration())
22711 std::advance(i&: It, n: 1);
22712 assert(It != End &&
22713 "Expected at least one component with the declaration.");
22714 if (It != Begin && It->getAssociatedDeclaration()
22715 ->getType()
22716 .getCanonicalType()
22717 ->isAnyPointerType()) {
22718 IsEnclosedByDataEnvironmentExpr = false;
22719 EnclosingExpr = nullptr;
22720 return false;
22721 }
22722 }
22723 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_map_shared_storage) << ERange;
22724 } else {
22725 assert(CKind == OMPC_to || CKind == OMPC_from);
22726 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_once_referenced_in_target_update)
22727 << ERange;
22728 }
22729 SemaRef.Diag(Loc: RE->getExprLoc(), DiagID: diag::note_used_here)
22730 << RE->getSourceRange();
22731 return true;
22732 }
22733
22734 // The current expression uses the same base as other expression in the
22735 // data environment but does not contain it completely.
22736 if (!CurrentRegionOnly && SI != SE)
22737 EnclosingExpr = RE;
22738
22739 // The current expression is a subset of the expression in the data
22740 // environment.
22741 IsEnclosedByDataEnvironmentExpr |=
22742 (!CurrentRegionOnly && CI != CE && SI == SE);
22743
22744 return false;
22745 });
22746
22747 if (CurrentRegionOnly)
22748 return FoundError;
22749
22750 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
22751 // If any part of the original storage of a list item has corresponding
22752 // storage in the device data environment, all of the original storage must
22753 // have corresponding storage in the device data environment.
22754 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
22755 // If a list item is an element of a structure, and a different element of
22756 // the structure has a corresponding list item in the device data environment
22757 // prior to a task encountering the construct associated with the map clause,
22758 // then the list item must also have a corresponding list item in the device
22759 // data environment prior to the task encountering the construct.
22760 //
22761 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
22762 SemaRef.Diag(Loc: ELoc,
22763 DiagID: diag::err_omp_original_storage_is_shared_and_does_not_contain)
22764 << ERange;
22765 SemaRef.Diag(Loc: EnclosingExpr->getExprLoc(), DiagID: diag::note_used_here)
22766 << EnclosingExpr->getSourceRange();
22767 return true;
22768 }
22769
22770 return FoundError;
22771}
22772
22773// Look up the user-defined mapper given the mapper name and mapped type, and
22774// build a reference to it.
22775static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S,
22776 CXXScopeSpec &MapperIdScopeSpec,
22777 const DeclarationNameInfo &MapperId,
22778 QualType Type,
22779 Expr *UnresolvedMapper) {
22780 if (MapperIdScopeSpec.isInvalid())
22781 return ExprError();
22782 // Get the actual type for the array type.
22783 if (Type->isArrayType()) {
22784 assert(Type->getAsArrayTypeUnsafe() && "Expect to get a valid array type");
22785 Type = Type->getAsArrayTypeUnsafe()->getElementType().getCanonicalType();
22786 }
22787 // Find all user-defined mappers with the given MapperId.
22788 SmallVector<UnresolvedSet<8>, 4> Lookups;
22789 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName);
22790 Lookup.suppressDiagnostics();
22791 if (S) {
22792 while (S && SemaRef.LookupParsedName(R&: Lookup, S, SS: &MapperIdScopeSpec,
22793 /*ObjectType=*/QualType())) {
22794 NamedDecl *D = Lookup.getRepresentativeDecl();
22795 while (S && !S->isDeclScope(D))
22796 S = S->getParent();
22797 if (S)
22798 S = S->getParent();
22799 Lookups.emplace_back();
22800 Lookups.back().append(I: Lookup.begin(), E: Lookup.end());
22801 Lookup.clear();
22802 }
22803 } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(Val: UnresolvedMapper)) {
22804 // Extract the user-defined mappers with the given MapperId.
22805 Lookups.push_back(Elt: UnresolvedSet<8>());
22806 for (NamedDecl *D : ULE->decls()) {
22807 auto *DMD = cast<OMPDeclareMapperDecl>(Val: D);
22808 assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation.");
22809 Lookups.back().addDecl(D: DMD);
22810 }
22811 }
22812 // Defer the lookup for dependent types. The results will be passed through
22813 // UnresolvedMapper on instantiation.
22814 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() ||
22815 Type->isInstantiationDependentType() ||
22816 Type->containsUnexpandedParameterPack() ||
22817 filterLookupForUDReductionAndMapper<bool>(Lookups, Gen: [](ValueDecl *D) {
22818 return !D->isInvalidDecl() &&
22819 (D->getType()->isDependentType() ||
22820 D->getType()->isInstantiationDependentType() ||
22821 D->getType()->containsUnexpandedParameterPack());
22822 })) {
22823 UnresolvedSet<8> URS;
22824 for (const UnresolvedSet<8> &Set : Lookups) {
22825 if (Set.empty())
22826 continue;
22827 URS.append(I: Set.begin(), E: Set.end());
22828 }
22829 return UnresolvedLookupExpr::Create(
22830 Context: SemaRef.Context, /*NamingClass=*/nullptr,
22831 QualifierLoc: MapperIdScopeSpec.getWithLocInContext(Context&: SemaRef.Context), NameInfo: MapperId,
22832 /*ADL=*/RequiresADL: false, Begin: URS.begin(), End: URS.end(), /*KnownDependent=*/false,
22833 /*KnownInstantiationDependent=*/false);
22834 }
22835 SourceLocation Loc = MapperId.getLoc();
22836 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
22837 // The type must be of struct, union or class type in C and C++
22838 if (!Type->isStructureOrClassType() && !Type->isUnionType() &&
22839 (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default")) {
22840 SemaRef.Diag(Loc, DiagID: diag::err_omp_mapper_wrong_type);
22841 return ExprError();
22842 }
22843 // Perform argument dependent lookup.
22844 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet())
22845 argumentDependentLookup(SemaRef, Id: MapperId, Loc, Ty: Type, Lookups);
22846 // Return the first user-defined mapper with the desired type.
22847 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
22848 Lookups, Gen: [&SemaRef, Type](ValueDecl *D) -> ValueDecl * {
22849 if (!D->isInvalidDecl() &&
22850 SemaRef.Context.hasSameType(T1: D->getType(), T2: Type))
22851 return D;
22852 return nullptr;
22853 }))
22854 return SemaRef.BuildDeclRefExpr(D: VD, Ty: Type, VK: VK_LValue, Loc);
22855 // Find the first user-defined mapper with a type derived from the desired
22856 // type.
22857 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
22858 Lookups, Gen: [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * {
22859 if (!D->isInvalidDecl() &&
22860 SemaRef.IsDerivedFrom(Loc, Derived: Type, Base: D->getType()) &&
22861 !Type.isMoreQualifiedThan(other: D->getType(),
22862 Ctx: SemaRef.getASTContext()))
22863 return D;
22864 return nullptr;
22865 })) {
22866 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
22867 /*DetectVirtual=*/false);
22868 if (SemaRef.IsDerivedFrom(Loc, Derived: Type, Base: VD->getType(), Paths)) {
22869 if (!Paths.isAmbiguous(BaseType: SemaRef.Context.getCanonicalType(
22870 T: VD->getType().getUnqualifiedType()))) {
22871 if (SemaRef.CheckBaseClassAccess(
22872 AccessLoc: Loc, Base: VD->getType(), Derived: Type, Path: Paths.front(),
22873 /*DiagID=*/0) != Sema::AR_inaccessible) {
22874 return SemaRef.BuildDeclRefExpr(D: VD, Ty: Type, VK: VK_LValue, Loc);
22875 }
22876 }
22877 }
22878 }
22879 // Report error if a mapper is specified, but cannot be found.
22880 if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") {
22881 SemaRef.Diag(Loc, DiagID: diag::err_omp_invalid_mapper)
22882 << Type << MapperId.getName();
22883 return ExprError();
22884 }
22885 return ExprEmpty();
22886}
22887
22888namespace {
22889// Utility struct that gathers all the related lists associated with a mappable
22890// expression.
22891struct MappableVarListInfo {
22892 // The list of expressions.
22893 ArrayRef<Expr *> VarList;
22894 // The list of processed expressions.
22895 SmallVector<Expr *, 16> ProcessedVarList;
22896 // The mappble components for each expression.
22897 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
22898 // The base declaration of the variable.
22899 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
22900 // The reference to the user-defined mapper associated with every expression.
22901 SmallVector<Expr *, 16> UDMapperList;
22902
22903 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
22904 // We have a list of components and base declarations for each entry in the
22905 // variable list.
22906 VarComponents.reserve(N: VarList.size());
22907 VarBaseDeclarations.reserve(N: VarList.size());
22908 }
22909};
22910} // namespace
22911
22912static DeclRefExpr *buildImplicitMap(Sema &S, QualType BaseType,
22913 DSAStackTy *Stack,
22914 SmallVectorImpl<OMPClause *> &Maps) {
22915
22916 const RecordDecl *RD = BaseType->getAsRecordDecl();
22917 SourceRange Range = RD->getSourceRange();
22918 DeclarationNameInfo ImplicitName;
22919 // Dummy variable _s for Mapper.
22920 VarDecl *VD = buildVarDecl(SemaRef&: S, Loc: Range.getEnd(), Type: BaseType, Name: "_s");
22921 DeclRefExpr *MapperVarRef =
22922 buildDeclRefExpr(S, D: VD, Ty: BaseType, Loc: SourceLocation());
22923
22924 // Create implicit map clause for mapper.
22925 SmallVector<Expr *, 4> SExprs;
22926 for (auto *FD : RD->fields()) {
22927 Expr *BE = S.BuildMemberExpr(
22928 Base: MapperVarRef, /*IsArrow=*/false, OpLoc: Range.getBegin(),
22929 NNS: NestedNameSpecifierLoc(), TemplateKWLoc: Range.getBegin(), Member: FD,
22930 FoundDecl: DeclAccessPair::make(D: FD, AS: FD->getAccess()),
22931 /*HadMultipleCandidates=*/false,
22932 MemberNameInfo: DeclarationNameInfo(FD->getDeclName(), FD->getSourceRange().getBegin()),
22933 Ty: FD->getType(), VK: VK_LValue, OK: OK_Ordinary);
22934 SExprs.push_back(Elt: BE);
22935 }
22936 CXXScopeSpec MapperIdScopeSpec;
22937 DeclarationNameInfo MapperId;
22938 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
22939
22940 OMPClause *MapClause = S.OpenMP().ActOnOpenMPMapClause(
22941 IteratorModifier: nullptr, MapTypeModifiers: OMPC_MAP_MODIFIER_unknown, MapTypeModifiersLoc: SourceLocation(), MapperIdScopeSpec,
22942 MapperId, MapType: DKind == OMPD_target_enter_data ? OMPC_MAP_to : OMPC_MAP_tofrom,
22943 /*IsMapTypeImplicit=*/true, MapLoc: SourceLocation(), ColonLoc: SourceLocation(), VarList: SExprs,
22944 Locs: OMPVarListLocTy());
22945 Maps.push_back(Elt: MapClause);
22946 return MapperVarRef;
22947}
22948
22949static ExprResult buildImplicitMapper(Sema &S, QualType BaseType,
22950 DSAStackTy *Stack) {
22951
22952 // Build impilicit map for mapper
22953 SmallVector<OMPClause *, 4> Maps;
22954 DeclRefExpr *MapperVarRef = buildImplicitMap(S, BaseType, Stack, Maps);
22955
22956 const RecordDecl *RD = BaseType->getAsRecordDecl();
22957 // AST context is RD's ParentASTContext().
22958 ASTContext &Ctx = RD->getParentASTContext();
22959 // DeclContext is RD's DeclContext.
22960 DeclContext *DCT = const_cast<DeclContext *>(RD->getDeclContext());
22961
22962 // Create implicit default mapper for "RD".
22963 DeclarationName MapperId;
22964 auto &DeclNames = Ctx.DeclarationNames;
22965 MapperId = DeclNames.getIdentifier(ID: &Ctx.Idents.get(Name: "default"));
22966 auto *DMD = OMPDeclareMapperDecl::Create(C&: Ctx, DC: DCT, L: SourceLocation(), Name: MapperId,
22967 T: BaseType, VarName: MapperId, Clauses: Maps, PrevDeclInScope: nullptr);
22968 Scope *Scope = S.getScopeForContext(Ctx: DCT);
22969 if (Scope)
22970 S.PushOnScopeChains(D: DMD, S: Scope, /*AddToContext=*/false);
22971 DCT->addDecl(D: DMD);
22972 DMD->setAccess(clang::AS_none);
22973 auto *VD = cast<DeclRefExpr>(Val: MapperVarRef)->getDecl();
22974 VD->setDeclContext(DMD);
22975 VD->setLexicalDeclContext(DMD);
22976 DMD->addDecl(D: VD);
22977 DMD->setMapperVarRef(MapperVarRef);
22978 FieldDecl *FD = *RD->field_begin();
22979 // create mapper refence.
22980 return DeclRefExpr::Create(Context: Ctx, QualifierLoc: NestedNameSpecifierLoc{}, TemplateKWLoc: FD->getLocation(),
22981 D: DMD, RefersToEnclosingVariableOrCapture: false, NameLoc: SourceLocation(), T: BaseType, VK: VK_LValue);
22982}
22983
22984// Look up the user-defined mapper given the mapper name and mapper type,
22985// return true if found one.
22986static bool hasUserDefinedMapper(Sema &SemaRef, Scope *S,
22987 CXXScopeSpec &MapperIdScopeSpec,
22988 const DeclarationNameInfo &MapperId,
22989 QualType Type) {
22990 // Find all user-defined mappers with the given MapperId.
22991 SmallVector<UnresolvedSet<8>, 4> Lookups;
22992 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName);
22993 Lookup.suppressDiagnostics();
22994 while (S && SemaRef.LookupParsedName(R&: Lookup, S, SS: &MapperIdScopeSpec,
22995 /*ObjectType=*/QualType())) {
22996 NamedDecl *D = Lookup.getRepresentativeDecl();
22997 while (S && !S->isDeclScope(D))
22998 S = S->getParent();
22999 if (S)
23000 S = S->getParent();
23001 Lookups.emplace_back();
23002 Lookups.back().append(I: Lookup.begin(), E: Lookup.end());
23003 Lookup.clear();
23004 }
23005 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() ||
23006 Type->isInstantiationDependentType() ||
23007 Type->containsUnexpandedParameterPack() ||
23008 filterLookupForUDReductionAndMapper<bool>(Lookups, Gen: [](ValueDecl *D) {
23009 return !D->isInvalidDecl() &&
23010 (D->getType()->isDependentType() ||
23011 D->getType()->isInstantiationDependentType() ||
23012 D->getType()->containsUnexpandedParameterPack());
23013 }))
23014 return false;
23015 // Perform argument dependent lookup.
23016 SourceLocation Loc = MapperId.getLoc();
23017 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet())
23018 argumentDependentLookup(SemaRef, Id: MapperId, Loc, Ty: Type, Lookups);
23019 if (filterLookupForUDReductionAndMapper<ValueDecl *>(
23020 Lookups, Gen: [&SemaRef, Type](ValueDecl *D) -> ValueDecl * {
23021 if (!D->isInvalidDecl() &&
23022 SemaRef.Context.hasSameType(T1: D->getType(), T2: Type))
23023 return D;
23024 return nullptr;
23025 }))
23026 return true;
23027 // Find the first user-defined mapper with a type derived from the desired
23028 // type.
23029 auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
23030 Lookups, Gen: [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * {
23031 if (!D->isInvalidDecl() &&
23032 SemaRef.IsDerivedFrom(Loc, Derived: Type, Base: D->getType()) &&
23033 !Type.isMoreQualifiedThan(other: D->getType(), Ctx: SemaRef.getASTContext()))
23034 return D;
23035 return nullptr;
23036 });
23037 if (!VD)
23038 return false;
23039 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
23040 /*DetectVirtual=*/false);
23041 if (SemaRef.IsDerivedFrom(Loc, Derived: Type, Base: VD->getType(), Paths)) {
23042 bool IsAmbiguous = !Paths.isAmbiguous(
23043 BaseType: SemaRef.Context.getCanonicalType(T: VD->getType().getUnqualifiedType()));
23044 if (IsAmbiguous)
23045 return false;
23046 if (SemaRef.CheckBaseClassAccess(AccessLoc: Loc, Base: VD->getType(), Derived: Type, Path: Paths.front(),
23047 /*DiagID=*/0) != Sema::AR_inaccessible)
23048 return true;
23049 }
23050 return false;
23051}
23052
23053static bool isImplicitMapperNeeded(Sema &S, DSAStackTy *Stack,
23054 QualType CanonType, const Expr *E) {
23055
23056 // DFS over data members in structures/classes.
23057 SmallVector<std::pair<QualType, FieldDecl *>, 4> Types(1,
23058 {CanonType, nullptr});
23059 llvm::DenseMap<const Type *, bool> Visited;
23060 SmallVector<std::pair<FieldDecl *, unsigned>, 4> ParentChain(1, {nullptr, 1});
23061 while (!Types.empty()) {
23062 auto [BaseType, CurFD] = Types.pop_back_val();
23063 while (ParentChain.back().second == 0)
23064 ParentChain.pop_back();
23065 --ParentChain.back().second;
23066 if (BaseType.isNull())
23067 continue;
23068 // Only structs/classes are allowed to have mappers.
23069 const RecordDecl *RD = BaseType.getCanonicalType()->getAsRecordDecl();
23070 if (!RD)
23071 continue;
23072 auto It = Visited.find(Val: BaseType.getTypePtr());
23073 if (It == Visited.end()) {
23074 // Try to find the associated user-defined mapper.
23075 CXXScopeSpec MapperIdScopeSpec;
23076 DeclarationNameInfo DefaultMapperId;
23077 DefaultMapperId.setName(S.Context.DeclarationNames.getIdentifier(
23078 ID: &S.Context.Idents.get(Name: "default")));
23079 DefaultMapperId.setLoc(E->getExprLoc());
23080 bool HasUDMapper =
23081 hasUserDefinedMapper(SemaRef&: S, S: Stack->getCurScope(), MapperIdScopeSpec,
23082 MapperId: DefaultMapperId, Type: BaseType);
23083 It = Visited.try_emplace(Key: BaseType.getTypePtr(), Args&: HasUDMapper).first;
23084 }
23085 // Found default mapper.
23086 if (It->second)
23087 return true;
23088 // Check for the "default" mapper for data members.
23089 bool FirstIter = true;
23090 for (FieldDecl *FD : RD->fields()) {
23091 if (!FD)
23092 continue;
23093 QualType FieldTy = FD->getType();
23094 if (FieldTy.isNull() ||
23095 !(FieldTy->isStructureOrClassType() || FieldTy->isUnionType()))
23096 continue;
23097 if (FirstIter) {
23098 FirstIter = false;
23099 ParentChain.emplace_back(Args&: CurFD, Args: 1);
23100 } else {
23101 ++ParentChain.back().second;
23102 }
23103 Types.emplace_back(Args&: FieldTy, Args&: FD);
23104 }
23105 }
23106 return false;
23107}
23108
23109// Check the validity of the provided variable list for the provided clause kind
23110// \a CKind. In the check process the valid expressions, mappable expression
23111// components, variables, and user-defined mappers are extracted and used to
23112// fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a
23113// UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec,
23114// and \a MapperId are expected to be valid if the clause kind is 'map'.
23115static void checkMappableExpressionList(
23116 Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind,
23117 MappableVarListInfo &MVLI, SourceLocation StartLoc,
23118 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId,
23119 ArrayRef<Expr *> UnresolvedMappers,
23120 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
23121 ArrayRef<OpenMPMapModifierKind> Modifiers = {},
23122 bool IsMapTypeImplicit = false, bool NoDiagnose = false) {
23123 // We only expect mappable expressions in 'to', 'from', 'map', and
23124 // 'use_device_addr' clauses.
23125 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from ||
23126 CKind == OMPC_use_device_addr) &&
23127 "Unexpected clause kind with mappable expressions!");
23128 unsigned OMPVersion = SemaRef.getLangOpts().OpenMP;
23129
23130 // If the identifier of user-defined mapper is not specified, it is "default".
23131 // We do not change the actual name in this clause to distinguish whether a
23132 // mapper is specified explicitly, i.e., it is not explicitly specified when
23133 // MapperId.getName() is empty.
23134 if (!MapperId.getName() || MapperId.getName().isEmpty()) {
23135 auto &DeclNames = SemaRef.getASTContext().DeclarationNames;
23136 MapperId.setName(DeclNames.getIdentifier(
23137 ID: &SemaRef.getASTContext().Idents.get(Name: "default")));
23138 MapperId.setLoc(StartLoc);
23139 }
23140
23141 // Iterators to find the current unresolved mapper expression.
23142 auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end();
23143 bool UpdateUMIt = false;
23144 Expr *UnresolvedMapper = nullptr;
23145
23146 bool HasHoldModifier =
23147 llvm::is_contained(Range&: Modifiers, Element: OMPC_MAP_MODIFIER_ompx_hold);
23148
23149 // Keep track of the mappable components and base declarations in this clause.
23150 // Each entry in the list is going to have a list of components associated. We
23151 // record each set of the components so that we can build the clause later on.
23152 // In the end we should have the same amount of declarations and component
23153 // lists.
23154
23155 for (Expr *RE : MVLI.VarList) {
23156 assert(RE && "Null expr in omp to/from/map clause");
23157 SourceLocation ELoc = RE->getExprLoc();
23158
23159 // Find the current unresolved mapper expression.
23160 if (UpdateUMIt && UMIt != UMEnd) {
23161 UMIt++;
23162 assert(
23163 UMIt != UMEnd &&
23164 "Expect the size of UnresolvedMappers to match with that of VarList");
23165 }
23166 UpdateUMIt = true;
23167 if (UMIt != UMEnd)
23168 UnresolvedMapper = *UMIt;
23169
23170 const Expr *VE = RE->IgnoreParenLValueCasts();
23171
23172 if (VE->isValueDependent() || VE->isTypeDependent() ||
23173 VE->isInstantiationDependent() ||
23174 VE->containsUnexpandedParameterPack()) {
23175 // Try to find the associated user-defined mapper.
23176 ExprResult ER = buildUserDefinedMapperRef(
23177 SemaRef, S: DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
23178 Type: VE->getType().getCanonicalType(), UnresolvedMapper);
23179 if (ER.isInvalid())
23180 continue;
23181 MVLI.UDMapperList.push_back(Elt: ER.get());
23182 // We can only analyze this information once the missing information is
23183 // resolved.
23184 MVLI.ProcessedVarList.push_back(Elt: RE);
23185 continue;
23186 }
23187
23188 Expr *SimpleExpr = RE->IgnoreParenCasts();
23189
23190 if (!RE->isLValue()) {
23191 if (SemaRef.getLangOpts().OpenMP < 50) {
23192 SemaRef.Diag(
23193 Loc: ELoc, DiagID: diag::err_omp_expected_named_var_member_or_array_expression)
23194 << RE->getSourceRange();
23195 } else {
23196 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_non_lvalue_in_map_or_motion_clauses)
23197 << getOpenMPClauseNameForDiag(C: CKind) << RE->getSourceRange();
23198 }
23199 continue;
23200 }
23201
23202 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
23203 ValueDecl *CurDeclaration = nullptr;
23204
23205 // Obtain the array or member expression bases if required. Also, fill the
23206 // components array with all the components identified in the process.
23207 const Expr *BE =
23208 checkMapClauseExpressionBase(SemaRef, E: SimpleExpr, CurComponents, CKind,
23209 DKind: DSAS->getCurrentDirective(), NoDiagnose);
23210 if (!BE)
23211 continue;
23212
23213 assert(!CurComponents.empty() &&
23214 "Invalid mappable expression information.");
23215
23216 if (const auto *TE = dyn_cast<CXXThisExpr>(Val: BE)) {
23217 // Add store "this" pointer to class in DSAStackTy for future checking
23218 DSAS->addMappedClassesQualTypes(QT: TE->getType());
23219 // Try to find the associated user-defined mapper.
23220 ExprResult ER = buildUserDefinedMapperRef(
23221 SemaRef, S: DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
23222 Type: VE->getType().getCanonicalType(), UnresolvedMapper);
23223 if (ER.isInvalid())
23224 continue;
23225 MVLI.UDMapperList.push_back(Elt: ER.get());
23226 // Skip restriction checking for variable or field declarations
23227 MVLI.ProcessedVarList.push_back(Elt: RE);
23228 MVLI.VarComponents.resize(N: MVLI.VarComponents.size() + 1);
23229 MVLI.VarComponents.back().append(in_start: CurComponents.begin(),
23230 in_end: CurComponents.end());
23231 MVLI.VarBaseDeclarations.push_back(Elt: nullptr);
23232 continue;
23233 }
23234
23235 // For the following checks, we rely on the base declaration which is
23236 // expected to be associated with the last component. The declaration is
23237 // expected to be a variable or a field (if 'this' is being mapped).
23238 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
23239 assert(CurDeclaration && "Null decl on map clause.");
23240 assert(
23241 CurDeclaration->isCanonicalDecl() &&
23242 "Expecting components to have associated only canonical declarations.");
23243
23244 auto *VD = dyn_cast<VarDecl>(Val: CurDeclaration);
23245 const auto *FD = dyn_cast<FieldDecl>(Val: CurDeclaration);
23246
23247 assert((VD || FD) && "Only variables or fields are expected here!");
23248 (void)FD;
23249
23250 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
23251 // threadprivate variables cannot appear in a map clause.
23252 // OpenMP 4.5 [2.10.5, target update Construct]
23253 // threadprivate variables cannot appear in a from clause.
23254 if (VD && DSAS->isThreadPrivate(D: VD)) {
23255 if (NoDiagnose)
23256 continue;
23257 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(D: VD, /*FromParent=*/false);
23258 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_threadprivate_in_clause)
23259 << getOpenMPClauseNameForDiag(C: CKind);
23260 reportOriginalDsa(SemaRef, Stack: DSAS, D: VD, DVar);
23261 continue;
23262 }
23263
23264 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
23265 // A list item cannot appear in both a map clause and a data-sharing
23266 // attribute clause on the same construct.
23267
23268 // Check conflicts with other map clause expressions. We check the conflicts
23269 // with the current construct separately from the enclosing data
23270 // environment, because the restrictions are different. We only have to
23271 // check conflicts across regions for the map clauses.
23272 if (checkMapConflicts(SemaRef, DSAS, VD: CurDeclaration, E: SimpleExpr,
23273 /*CurrentRegionOnly=*/true, CurComponents, CKind))
23274 break;
23275 if (CKind == OMPC_map &&
23276 (SemaRef.getLangOpts().OpenMP <= 45 || StartLoc.isValid()) &&
23277 checkMapConflicts(SemaRef, DSAS, VD: CurDeclaration, E: SimpleExpr,
23278 /*CurrentRegionOnly=*/false, CurComponents, CKind))
23279 break;
23280
23281 // OpenMP 4.5 [2.10.5, target update Construct]
23282 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
23283 // If the type of a list item is a reference to a type T then the type will
23284 // be considered to be T for all purposes of this clause.
23285 auto I = llvm::find_if(
23286 Range&: CurComponents,
23287 P: [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
23288 return MC.getAssociatedDeclaration();
23289 });
23290 assert(I != CurComponents.end() && "Null decl on map clause.");
23291 (void)I;
23292 QualType Type;
23293 auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: VE->IgnoreParens());
23294 auto *OASE = dyn_cast<ArraySectionExpr>(Val: VE->IgnoreParens());
23295 auto *OAShE = dyn_cast<OMPArrayShapingExpr>(Val: VE->IgnoreParens());
23296 if (ASE) {
23297 Type = ASE->getType().getNonReferenceType();
23298 } else if (OASE) {
23299 QualType BaseType =
23300 ArraySectionExpr::getBaseOriginalType(Base: OASE->getBase());
23301 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
23302 Type = ATy->getElementType();
23303 else
23304 Type = BaseType->getPointeeType();
23305 Type = Type.getNonReferenceType();
23306 } else if (OAShE) {
23307 Type = OAShE->getBase()->getType()->getPointeeType();
23308 } else {
23309 Type = VE->getType();
23310 }
23311
23312 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
23313 // A list item in a to or from clause must have a mappable type.
23314 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
23315 // A list item must have a mappable type.
23316 if (!checkTypeMappable(SL: VE->getExprLoc(), SR: VE->getSourceRange(), SemaRef,
23317 Stack: DSAS, QTy: Type, /*FullCheck=*/true))
23318 continue;
23319
23320 if (CKind == OMPC_map) {
23321 // target enter data
23322 // OpenMP [2.10.2, Restrictions, p. 99]
23323 // A map-type must be specified in all map clauses and must be either
23324 // to or alloc. Starting with OpenMP 5.2 the default map type is `to` if
23325 // no map type is present.
23326 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
23327 if (DKind == OMPD_target_enter_data &&
23328 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc ||
23329 SemaRef.getLangOpts().OpenMP >= 52)) {
23330 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_invalid_map_type_for_directive)
23331 << (IsMapTypeImplicit ? 1 : 0)
23332 << getOpenMPSimpleClauseTypeName(Kind: OMPC_map, Type: MapType)
23333 << getOpenMPDirectiveName(D: DKind, Ver: OMPVersion);
23334 continue;
23335 }
23336
23337 // target exit_data
23338 // OpenMP [2.10.3, Restrictions, p. 102]
23339 // A map-type must be specified in all map clauses and must be either
23340 // from, release, or delete. Starting with OpenMP 5.2 the default map
23341 // type is `from` if no map type is present.
23342 if (DKind == OMPD_target_exit_data &&
23343 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
23344 MapType == OMPC_MAP_delete || SemaRef.getLangOpts().OpenMP >= 52)) {
23345 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_invalid_map_type_for_directive)
23346 << (IsMapTypeImplicit ? 1 : 0)
23347 << getOpenMPSimpleClauseTypeName(Kind: OMPC_map, Type: MapType)
23348 << getOpenMPDirectiveName(D: DKind, Ver: OMPVersion);
23349 continue;
23350 }
23351
23352 // The 'ompx_hold' modifier is specifically intended to be used on a
23353 // 'target' or 'target data' directive to prevent data from being unmapped
23354 // during the associated statement. It is not permitted on a 'target
23355 // enter data' or 'target exit data' directive, which have no associated
23356 // statement.
23357 if ((DKind == OMPD_target_enter_data || DKind == OMPD_target_exit_data) &&
23358 HasHoldModifier) {
23359 SemaRef.Diag(Loc: StartLoc,
23360 DiagID: diag::err_omp_invalid_map_type_modifier_for_directive)
23361 << getOpenMPSimpleClauseTypeName(Kind: OMPC_map,
23362 Type: OMPC_MAP_MODIFIER_ompx_hold)
23363 << getOpenMPDirectiveName(D: DKind, Ver: OMPVersion);
23364 continue;
23365 }
23366
23367 // target, target data
23368 // OpenMP 5.0 [2.12.2, Restrictions, p. 163]
23369 // OpenMP 5.0 [2.12.5, Restrictions, p. 174]
23370 // A map-type in a map clause must be to, from, tofrom or alloc
23371 if ((DKind == OMPD_target_data ||
23372 isOpenMPTargetExecutionDirective(DKind)) &&
23373 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_from ||
23374 MapType == OMPC_MAP_tofrom || MapType == OMPC_MAP_alloc)) {
23375 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_invalid_map_type_for_directive)
23376 << (IsMapTypeImplicit ? 1 : 0)
23377 << getOpenMPSimpleClauseTypeName(Kind: OMPC_map, Type: MapType)
23378 << getOpenMPDirectiveName(D: DKind, Ver: OMPVersion);
23379 continue;
23380 }
23381
23382 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
23383 // A list item cannot appear in both a map clause and a data-sharing
23384 // attribute clause on the same construct
23385 //
23386 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
23387 // A list item cannot appear in both a map clause and a data-sharing
23388 // attribute clause on the same construct unless the construct is a
23389 // combined construct.
23390 if (VD && ((SemaRef.LangOpts.OpenMP <= 45 &&
23391 isOpenMPTargetExecutionDirective(DKind)) ||
23392 DKind == OMPD_target)) {
23393 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(D: VD, /*FromParent=*/false);
23394 if (isOpenMPPrivate(Kind: DVar.CKind)) {
23395 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_variable_in_given_clause_and_dsa)
23396 << getOpenMPClauseNameForDiag(C: DVar.CKind)
23397 << getOpenMPClauseNameForDiag(C: OMPC_map)
23398 << getOpenMPDirectiveName(D: DSAS->getCurrentDirective(),
23399 Ver: OMPVersion);
23400 reportOriginalDsa(SemaRef, Stack: DSAS, D: CurDeclaration, DVar);
23401 continue;
23402 }
23403 }
23404 }
23405
23406 // Try to find the associated user-defined mapper.
23407 ExprResult ER = buildUserDefinedMapperRef(
23408 SemaRef, S: DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
23409 Type: Type.getCanonicalType(), UnresolvedMapper);
23410 if (ER.isInvalid())
23411 continue;
23412
23413 // If no user-defined mapper is found, we need to create an implicit one for
23414 // arrays/array-sections on structs that have members that have
23415 // user-defined mappers. This is needed to ensure that the mapper for the
23416 // member is invoked when mapping each element of the array/array-section.
23417 if (!ER.get()) {
23418 QualType BaseType;
23419
23420 if (isa<ArraySectionExpr>(Val: VE)) {
23421 BaseType = VE->getType().getCanonicalType();
23422 if (BaseType->isSpecificBuiltinType(K: BuiltinType::ArraySection)) {
23423 const auto *OASE = cast<ArraySectionExpr>(Val: VE->IgnoreParenImpCasts());
23424 QualType BType =
23425 ArraySectionExpr::getBaseOriginalType(Base: OASE->getBase());
23426 QualType ElemType;
23427 if (const auto *ATy = BType->getAsArrayTypeUnsafe())
23428 ElemType = ATy->getElementType();
23429 else
23430 ElemType = BType->getPointeeType();
23431 BaseType = ElemType.getCanonicalType();
23432 }
23433 } else if (VE->getType()->isArrayType()) {
23434 const ArrayType *AT = VE->getType()->getAsArrayTypeUnsafe();
23435 const QualType ElemType = AT->getElementType();
23436 BaseType = ElemType.getCanonicalType();
23437 }
23438
23439 if (!BaseType.isNull() && BaseType->getAsRecordDecl() &&
23440 isImplicitMapperNeeded(S&: SemaRef, Stack: DSAS, CanonType: BaseType, E: VE)) {
23441 ER = buildImplicitMapper(S&: SemaRef, BaseType, Stack: DSAS);
23442 }
23443 }
23444 MVLI.UDMapperList.push_back(Elt: ER.get());
23445
23446 // Save the current expression.
23447 MVLI.ProcessedVarList.push_back(Elt: RE);
23448
23449 // Store the components in the stack so that they can be used to check
23450 // against other clauses later on.
23451 DSAS->addMappableExpressionComponents(VD: CurDeclaration, Components: CurComponents,
23452 /*WhereFoundClauseKind=*/OMPC_map);
23453
23454 // Save the components and declaration to create the clause. For purposes of
23455 // the clause creation, any component list that has base 'this' uses
23456 // null as base declaration.
23457 MVLI.VarComponents.resize(N: MVLI.VarComponents.size() + 1);
23458 MVLI.VarComponents.back().append(in_start: CurComponents.begin(),
23459 in_end: CurComponents.end());
23460 MVLI.VarBaseDeclarations.push_back(Elt: isa<MemberExpr>(Val: BE) ? nullptr
23461 : CurDeclaration);
23462 }
23463}
23464
23465OMPClause *SemaOpenMP::ActOnOpenMPMapClause(
23466 Expr *IteratorModifier, ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
23467 ArrayRef<SourceLocation> MapTypeModifiersLoc,
23468 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
23469 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc,
23470 SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
23471 const OMPVarListLocTy &Locs, bool NoDiagnose,
23472 ArrayRef<Expr *> UnresolvedMappers) {
23473 OpenMPMapModifierKind Modifiers[] = {
23474 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
23475 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
23476 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
23477 OMPC_MAP_MODIFIER_unknown};
23478 SourceLocation ModifiersLoc[NumberOfOMPMapClauseModifiers];
23479
23480 if (IteratorModifier && !IteratorModifier->getType()->isSpecificBuiltinType(
23481 K: BuiltinType::OMPIterator))
23482 Diag(Loc: IteratorModifier->getExprLoc(),
23483 DiagID: diag::err_omp_map_modifier_not_iterator);
23484
23485 // Process map-type-modifiers, flag errors for duplicate modifiers.
23486 unsigned Count = 0;
23487 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) {
23488 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown &&
23489 llvm::is_contained(Range&: Modifiers, Element: MapTypeModifiers[I])) {
23490 Diag(Loc: MapTypeModifiersLoc[I], DiagID: diag::err_omp_duplicate_map_type_modifier);
23491 continue;
23492 }
23493 assert(Count < NumberOfOMPMapClauseModifiers &&
23494 "Modifiers exceed the allowed number of map type modifiers");
23495 Modifiers[Count] = MapTypeModifiers[I];
23496 ModifiersLoc[Count] = MapTypeModifiersLoc[I];
23497 ++Count;
23498 }
23499
23500 MappableVarListInfo MVLI(VarList);
23501 checkMappableExpressionList(SemaRef, DSAStack, CKind: OMPC_map, MVLI, StartLoc: Locs.StartLoc,
23502 MapperIdScopeSpec, MapperId, UnresolvedMappers,
23503 MapType, Modifiers, IsMapTypeImplicit,
23504 NoDiagnose);
23505
23506 // We need to produce a map clause even if we don't have variables so that
23507 // other diagnostics related with non-existing map clauses are accurate.
23508 return OMPMapClause::Create(
23509 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, Declarations: MVLI.VarBaseDeclarations,
23510 ComponentLists: MVLI.VarComponents, UDMapperRefs: MVLI.UDMapperList, IteratorModifier, MapModifiers: Modifiers,
23511 MapModifiersLoc: ModifiersLoc, UDMQualifierLoc: MapperIdScopeSpec.getWithLocInContext(Context&: getASTContext()),
23512 MapperId, Type: MapType, TypeIsImplicit: IsMapTypeImplicit, TypeLoc: MapLoc);
23513}
23514
23515QualType SemaOpenMP::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
23516 TypeResult ParsedType) {
23517 assert(ParsedType.isUsable());
23518
23519 QualType ReductionType = SemaRef.GetTypeFromParser(Ty: ParsedType.get());
23520 if (ReductionType.isNull())
23521 return QualType();
23522
23523 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
23524 // A type name in a declare reduction directive cannot be a function type, an
23525 // array type, a reference type, or a type qualified with const, volatile or
23526 // restrict.
23527 if (ReductionType.hasQualifiers()) {
23528 Diag(Loc: TyLoc, DiagID: diag::err_omp_reduction_wrong_type) << 0;
23529 return QualType();
23530 }
23531
23532 if (ReductionType->isFunctionType()) {
23533 Diag(Loc: TyLoc, DiagID: diag::err_omp_reduction_wrong_type) << 1;
23534 return QualType();
23535 }
23536 if (ReductionType->isReferenceType()) {
23537 Diag(Loc: TyLoc, DiagID: diag::err_omp_reduction_wrong_type) << 2;
23538 return QualType();
23539 }
23540 if (ReductionType->isArrayType()) {
23541 Diag(Loc: TyLoc, DiagID: diag::err_omp_reduction_wrong_type) << 3;
23542 return QualType();
23543 }
23544 return ReductionType;
23545}
23546
23547SemaOpenMP::DeclGroupPtrTy
23548SemaOpenMP::ActOnOpenMPDeclareReductionDirectiveStart(
23549 Scope *S, DeclContext *DC, DeclarationName Name,
23550 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
23551 AccessSpecifier AS, Decl *PrevDeclInScope) {
23552 SmallVector<Decl *, 8> Decls;
23553 Decls.reserve(N: ReductionTypes.size());
23554
23555 LookupResult Lookup(SemaRef, Name, SourceLocation(),
23556 Sema::LookupOMPReductionName,
23557 SemaRef.forRedeclarationInCurContext());
23558 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
23559 // A reduction-identifier may not be re-declared in the current scope for the
23560 // same type or for a type that is compatible according to the base language
23561 // rules.
23562 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
23563 OMPDeclareReductionDecl *PrevDRD = nullptr;
23564 bool InCompoundScope = true;
23565 if (S != nullptr) {
23566 // Find previous declaration with the same name not referenced in other
23567 // declarations.
23568 FunctionScopeInfo *ParentFn = SemaRef.getEnclosingFunction();
23569 InCompoundScope =
23570 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
23571 SemaRef.LookupName(R&: Lookup, S);
23572 SemaRef.FilterLookupForScope(R&: Lookup, Ctx: DC, S, /*ConsiderLinkage=*/false,
23573 /*AllowInlineNamespace=*/false);
23574 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
23575 LookupResult::Filter Filter = Lookup.makeFilter();
23576 while (Filter.hasNext()) {
23577 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Val: Filter.next());
23578 if (InCompoundScope) {
23579 UsedAsPrevious.try_emplace(Key: PrevDecl, Args: false);
23580 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
23581 UsedAsPrevious[D] = true;
23582 }
23583 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
23584 PrevDecl->getLocation();
23585 }
23586 Filter.done();
23587 if (InCompoundScope) {
23588 for (const auto &PrevData : UsedAsPrevious) {
23589 if (!PrevData.second) {
23590 PrevDRD = PrevData.first;
23591 break;
23592 }
23593 }
23594 }
23595 } else if (PrevDeclInScope != nullptr) {
23596 auto *PrevDRDInScope = PrevDRD =
23597 cast<OMPDeclareReductionDecl>(Val: PrevDeclInScope);
23598 do {
23599 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
23600 PrevDRDInScope->getLocation();
23601 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
23602 } while (PrevDRDInScope != nullptr);
23603 }
23604 for (const auto &TyData : ReductionTypes) {
23605 const auto I = PreviousRedeclTypes.find(Val: TyData.first.getCanonicalType());
23606 bool Invalid = false;
23607 if (I != PreviousRedeclTypes.end()) {
23608 Diag(Loc: TyData.second, DiagID: diag::err_omp_declare_reduction_redefinition)
23609 << TyData.first;
23610 Diag(Loc: I->second, DiagID: diag::note_previous_definition);
23611 Invalid = true;
23612 }
23613 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
23614 auto *DRD = OMPDeclareReductionDecl::Create(
23615 C&: getASTContext(), DC, L: TyData.second, Name, T: TyData.first, PrevDeclInScope: PrevDRD);
23616 DC->addDecl(D: DRD);
23617 DRD->setAccess(AS);
23618 Decls.push_back(Elt: DRD);
23619 if (Invalid)
23620 DRD->setInvalidDecl();
23621 else
23622 PrevDRD = DRD;
23623 }
23624
23625 return DeclGroupPtrTy::make(
23626 P: DeclGroupRef::Create(C&: getASTContext(), Decls: Decls.begin(), NumDecls: Decls.size()));
23627}
23628
23629void SemaOpenMP::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
23630 auto *DRD = cast<OMPDeclareReductionDecl>(Val: D);
23631
23632 // Enter new function scope.
23633 SemaRef.PushFunctionScope();
23634 SemaRef.setFunctionHasBranchProtectedScope();
23635 SemaRef.getCurFunction()->setHasOMPDeclareReductionCombiner();
23636
23637 if (S != nullptr)
23638 SemaRef.PushDeclContext(S, DC: DRD);
23639 else
23640 SemaRef.CurContext = DRD;
23641
23642 SemaRef.PushExpressionEvaluationContext(
23643 NewContext: Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
23644
23645 QualType ReductionType = DRD->getType();
23646 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
23647 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
23648 // uses semantics of argument handles by value, but it should be passed by
23649 // reference. C lang does not support references, so pass all parameters as
23650 // pointers.
23651 // Create 'T omp_in;' variable.
23652 VarDecl *OmpInParm =
23653 buildVarDecl(SemaRef, Loc: D->getLocation(), Type: ReductionType, Name: "omp_in");
23654 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
23655 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
23656 // uses semantics of argument handles by value, but it should be passed by
23657 // reference. C lang does not support references, so pass all parameters as
23658 // pointers.
23659 // Create 'T omp_out;' variable.
23660 VarDecl *OmpOutParm =
23661 buildVarDecl(SemaRef, Loc: D->getLocation(), Type: ReductionType, Name: "omp_out");
23662 if (S != nullptr) {
23663 SemaRef.PushOnScopeChains(D: OmpInParm, S);
23664 SemaRef.PushOnScopeChains(D: OmpOutParm, S);
23665 } else {
23666 DRD->addDecl(D: OmpInParm);
23667 DRD->addDecl(D: OmpOutParm);
23668 }
23669 Expr *InE =
23670 ::buildDeclRefExpr(S&: SemaRef, D: OmpInParm, Ty: ReductionType, Loc: D->getLocation());
23671 Expr *OutE =
23672 ::buildDeclRefExpr(S&: SemaRef, D: OmpOutParm, Ty: ReductionType, Loc: D->getLocation());
23673 DRD->setCombinerData(InE, OutE);
23674}
23675
23676void SemaOpenMP::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D,
23677 Expr *Combiner) {
23678 auto *DRD = cast<OMPDeclareReductionDecl>(Val: D);
23679 SemaRef.DiscardCleanupsInEvaluationContext();
23680 SemaRef.PopExpressionEvaluationContext();
23681
23682 SemaRef.PopDeclContext();
23683 SemaRef.PopFunctionScopeInfo();
23684
23685 if (Combiner != nullptr)
23686 DRD->setCombiner(Combiner);
23687 else
23688 DRD->setInvalidDecl();
23689}
23690
23691VarDecl *SemaOpenMP::ActOnOpenMPDeclareReductionInitializerStart(Scope *S,
23692 Decl *D) {
23693 auto *DRD = cast<OMPDeclareReductionDecl>(Val: D);
23694
23695 // Enter new function scope.
23696 SemaRef.PushFunctionScope();
23697 SemaRef.setFunctionHasBranchProtectedScope();
23698
23699 if (S != nullptr)
23700 SemaRef.PushDeclContext(S, DC: DRD);
23701 else
23702 SemaRef.CurContext = DRD;
23703
23704 SemaRef.PushExpressionEvaluationContext(
23705 NewContext: Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
23706
23707 QualType ReductionType = DRD->getType();
23708 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
23709 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
23710 // uses semantics of argument handles by value, but it should be passed by
23711 // reference. C lang does not support references, so pass all parameters as
23712 // pointers.
23713 // Create 'T omp_priv;' variable.
23714 VarDecl *OmpPrivParm =
23715 buildVarDecl(SemaRef, Loc: D->getLocation(), Type: ReductionType, Name: "omp_priv");
23716 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
23717 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
23718 // uses semantics of argument handles by value, but it should be passed by
23719 // reference. C lang does not support references, so pass all parameters as
23720 // pointers.
23721 // Create 'T omp_orig;' variable.
23722 VarDecl *OmpOrigParm =
23723 buildVarDecl(SemaRef, Loc: D->getLocation(), Type: ReductionType, Name: "omp_orig");
23724 if (S != nullptr) {
23725 SemaRef.PushOnScopeChains(D: OmpPrivParm, S);
23726 SemaRef.PushOnScopeChains(D: OmpOrigParm, S);
23727 } else {
23728 DRD->addDecl(D: OmpPrivParm);
23729 DRD->addDecl(D: OmpOrigParm);
23730 }
23731 Expr *OrigE =
23732 ::buildDeclRefExpr(S&: SemaRef, D: OmpOrigParm, Ty: ReductionType, Loc: D->getLocation());
23733 Expr *PrivE =
23734 ::buildDeclRefExpr(S&: SemaRef, D: OmpPrivParm, Ty: ReductionType, Loc: D->getLocation());
23735 DRD->setInitializerData(OrigE, PrivE);
23736 return OmpPrivParm;
23737}
23738
23739void SemaOpenMP::ActOnOpenMPDeclareReductionInitializerEnd(
23740 Decl *D, Expr *Initializer, VarDecl *OmpPrivParm) {
23741 auto *DRD = cast<OMPDeclareReductionDecl>(Val: D);
23742 SemaRef.DiscardCleanupsInEvaluationContext();
23743 SemaRef.PopExpressionEvaluationContext();
23744
23745 SemaRef.PopDeclContext();
23746 SemaRef.PopFunctionScopeInfo();
23747
23748 if (Initializer != nullptr) {
23749 DRD->setInitializer(E: Initializer, IK: OMPDeclareReductionInitKind::Call);
23750 } else if (OmpPrivParm->hasInit()) {
23751 DRD->setInitializer(E: OmpPrivParm->getInit(),
23752 IK: OmpPrivParm->isDirectInit()
23753 ? OMPDeclareReductionInitKind::Direct
23754 : OMPDeclareReductionInitKind::Copy);
23755 } else {
23756 DRD->setInvalidDecl();
23757 }
23758}
23759
23760SemaOpenMP::DeclGroupPtrTy SemaOpenMP::ActOnOpenMPDeclareReductionDirectiveEnd(
23761 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
23762 for (Decl *D : DeclReductions.get()) {
23763 if (IsValid) {
23764 if (S)
23765 SemaRef.PushOnScopeChains(D: cast<OMPDeclareReductionDecl>(Val: D), S,
23766 /*AddToContext=*/false);
23767 } else {
23768 D->setInvalidDecl();
23769 }
23770 }
23771 return DeclReductions;
23772}
23773
23774TypeResult SemaOpenMP::ActOnOpenMPDeclareMapperVarDecl(Scope *S,
23775 Declarator &D) {
23776 TypeSourceInfo *TInfo = SemaRef.GetTypeForDeclarator(D);
23777 QualType T = TInfo->getType();
23778 if (D.isInvalidType())
23779 return true;
23780
23781 if (getLangOpts().CPlusPlus) {
23782 // Check that there are no default arguments (C++ only).
23783 SemaRef.CheckExtraCXXDefaultArguments(D);
23784 }
23785
23786 return SemaRef.CreateParsedType(T, TInfo);
23787}
23788
23789QualType SemaOpenMP::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc,
23790 TypeResult ParsedType) {
23791 assert(ParsedType.isUsable() && "Expect usable parsed mapper type");
23792
23793 QualType MapperType = SemaRef.GetTypeFromParser(Ty: ParsedType.get());
23794 assert(!MapperType.isNull() && "Expect valid mapper type");
23795
23796 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
23797 // The type must be of struct, union or class type in C and C++
23798 if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) {
23799 Diag(Loc: TyLoc, DiagID: diag::err_omp_mapper_wrong_type);
23800 return QualType();
23801 }
23802 return MapperType;
23803}
23804
23805SemaOpenMP::DeclGroupPtrTy SemaOpenMP::ActOnOpenMPDeclareMapperDirective(
23806 Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType,
23807 SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS,
23808 Expr *MapperVarRef, ArrayRef<OMPClause *> Clauses, Decl *PrevDeclInScope) {
23809 LookupResult Lookup(SemaRef, Name, SourceLocation(),
23810 Sema::LookupOMPMapperName,
23811 SemaRef.forRedeclarationInCurContext());
23812 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
23813 // A mapper-identifier may not be redeclared in the current scope for the
23814 // same type or for a type that is compatible according to the base language
23815 // rules.
23816 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
23817 OMPDeclareMapperDecl *PrevDMD = nullptr;
23818 bool InCompoundScope = true;
23819 if (S != nullptr) {
23820 // Find previous declaration with the same name not referenced in other
23821 // declarations.
23822 FunctionScopeInfo *ParentFn = SemaRef.getEnclosingFunction();
23823 InCompoundScope =
23824 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
23825 SemaRef.LookupName(R&: Lookup, S);
23826 SemaRef.FilterLookupForScope(R&: Lookup, Ctx: DC, S, /*ConsiderLinkage=*/false,
23827 /*AllowInlineNamespace=*/false);
23828 llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious;
23829 LookupResult::Filter Filter = Lookup.makeFilter();
23830 while (Filter.hasNext()) {
23831 auto *PrevDecl = cast<OMPDeclareMapperDecl>(Val: Filter.next());
23832 if (InCompoundScope) {
23833 UsedAsPrevious.try_emplace(Key: PrevDecl, Args: false);
23834 if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope())
23835 UsedAsPrevious[D] = true;
23836 }
23837 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
23838 PrevDecl->getLocation();
23839 }
23840 Filter.done();
23841 if (InCompoundScope) {
23842 for (const auto &PrevData : UsedAsPrevious) {
23843 if (!PrevData.second) {
23844 PrevDMD = PrevData.first;
23845 break;
23846 }
23847 }
23848 }
23849 } else if (PrevDeclInScope) {
23850 auto *PrevDMDInScope = PrevDMD =
23851 cast<OMPDeclareMapperDecl>(Val: PrevDeclInScope);
23852 do {
23853 PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] =
23854 PrevDMDInScope->getLocation();
23855 PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope();
23856 } while (PrevDMDInScope != nullptr);
23857 }
23858 const auto I = PreviousRedeclTypes.find(Val: MapperType.getCanonicalType());
23859 bool Invalid = false;
23860 if (I != PreviousRedeclTypes.end()) {
23861 Diag(Loc: StartLoc, DiagID: diag::err_omp_declare_mapper_redefinition)
23862 << MapperType << Name;
23863 Diag(Loc: I->second, DiagID: diag::note_previous_definition);
23864 Invalid = true;
23865 }
23866 // Build expressions for implicit maps of data members with 'default'
23867 // mappers.
23868 SmallVector<OMPClause *, 4> ClausesWithImplicit(Clauses);
23869 if (getLangOpts().OpenMP >= 50)
23870 processImplicitMapsWithDefaultMappers(S&: SemaRef, DSAStack,
23871 Clauses&: ClausesWithImplicit);
23872 auto *DMD = OMPDeclareMapperDecl::Create(C&: getASTContext(), DC, L: StartLoc, Name,
23873 T: MapperType, VarName: VN, Clauses: ClausesWithImplicit,
23874 PrevDeclInScope: PrevDMD);
23875 if (S)
23876 SemaRef.PushOnScopeChains(D: DMD, S);
23877 else
23878 DC->addDecl(D: DMD);
23879 DMD->setAccess(AS);
23880 if (Invalid)
23881 DMD->setInvalidDecl();
23882
23883 auto *VD = cast<DeclRefExpr>(Val: MapperVarRef)->getDecl();
23884 VD->setDeclContext(DMD);
23885 VD->setLexicalDeclContext(DMD);
23886 DMD->addDecl(D: VD);
23887 DMD->setMapperVarRef(MapperVarRef);
23888
23889 return DeclGroupPtrTy::make(P: DeclGroupRef(DMD));
23890}
23891
23892ExprResult SemaOpenMP::ActOnOpenMPDeclareMapperDirectiveVarDecl(
23893 Scope *S, QualType MapperType, SourceLocation StartLoc,
23894 DeclarationName VN) {
23895 TypeSourceInfo *TInfo =
23896 getASTContext().getTrivialTypeSourceInfo(T: MapperType, Loc: StartLoc);
23897 auto *VD = VarDecl::Create(
23898 C&: getASTContext(), DC: getASTContext().getTranslationUnitDecl(), StartLoc,
23899 IdLoc: StartLoc, Id: VN.getAsIdentifierInfo(), T: MapperType, TInfo, S: SC_None);
23900 if (S)
23901 SemaRef.PushOnScopeChains(D: VD, S, /*AddToContext=*/false);
23902 Expr *E = buildDeclRefExpr(S&: SemaRef, D: VD, Ty: MapperType, Loc: StartLoc);
23903 DSAStack->addDeclareMapperVarRef(Ref: E);
23904 return E;
23905}
23906
23907void SemaOpenMP::ActOnOpenMPIteratorVarDecl(VarDecl *VD) {
23908 bool IsGlobalVar =
23909 !VD->isLocalVarDecl() && VD->getDeclContext()->isTranslationUnit();
23910 if (DSAStack->getDeclareMapperVarRef()) {
23911 if (IsGlobalVar)
23912 SemaRef.Consumer.HandleTopLevelDecl(D: DeclGroupRef(VD));
23913 DSAStack->addIteratorVarDecl(VD);
23914 } else {
23915 // Currently, only declare mapper handles global-scope iterator vars.
23916 assert(!IsGlobalVar && "Only declare mapper handles TU-scope iterators.");
23917 }
23918}
23919
23920bool SemaOpenMP::isOpenMPDeclareMapperVarDeclAllowed(const VarDecl *VD) const {
23921 assert(getLangOpts().OpenMP && "Expected OpenMP mode.");
23922 const Expr *Ref = DSAStack->getDeclareMapperVarRef();
23923 if (const auto *DRE = cast_or_null<DeclRefExpr>(Val: Ref)) {
23924 if (VD->getCanonicalDecl() == DRE->getDecl()->getCanonicalDecl())
23925 return true;
23926 if (VD->isUsableInConstantExpressions(C: getASTContext()))
23927 return true;
23928 if (getLangOpts().OpenMP >= 52 && DSAStack->isIteratorVarDecl(VD))
23929 return true;
23930 return false;
23931 }
23932 return true;
23933}
23934
23935const ValueDecl *SemaOpenMP::getOpenMPDeclareMapperVarName() const {
23936 assert(getLangOpts().OpenMP && "Expected OpenMP mode.");
23937 return cast<DeclRefExpr>(DSAStack->getDeclareMapperVarRef())->getDecl();
23938}
23939
23940OMPClause *SemaOpenMP::ActOnOpenMPNumTeamsClause(ArrayRef<Expr *> VarList,
23941 SourceLocation StartLoc,
23942 SourceLocation LParenLoc,
23943 SourceLocation EndLoc) {
23944 if (VarList.empty())
23945 return nullptr;
23946
23947 for (Expr *ValExpr : VarList) {
23948 // OpenMP [teams Constrcut, Restrictions]
23949 // The num_teams expression must evaluate to a positive integer value.
23950 if (!isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_num_teams,
23951 /*StrictlyPositive=*/true))
23952 return nullptr;
23953 }
23954
23955 // OpenMP 5.2: Validate lower-bound ≤ upper-bound constraint
23956 if (VarList.size() == 2) {
23957 Expr *LowerBound = VarList[0];
23958 Expr *UpperBound = VarList[1];
23959
23960 // Check if both are compile-time constants for validation
23961 if (!LowerBound->isValueDependent() && !UpperBound->isValueDependent() &&
23962 LowerBound->isIntegerConstantExpr(Ctx: getASTContext()) &&
23963 UpperBound->isIntegerConstantExpr(Ctx: getASTContext())) {
23964
23965 // Get the actual constant values
23966 llvm::APSInt LowerVal =
23967 LowerBound->EvaluateKnownConstInt(Ctx: getASTContext());
23968 llvm::APSInt UpperVal =
23969 UpperBound->EvaluateKnownConstInt(Ctx: getASTContext());
23970
23971 if (LowerVal > UpperVal) {
23972 Diag(Loc: LowerBound->getExprLoc(),
23973 DiagID: diag::err_omp_num_teams_lower_bound_larger)
23974 << LowerBound->getSourceRange() << UpperBound->getSourceRange();
23975 return nullptr;
23976 }
23977 }
23978 }
23979
23980 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
23981 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
23982 DKind, CKind: OMPC_num_teams, OpenMPVersion: getLangOpts().OpenMP);
23983 if (CaptureRegion == OMPD_unknown || SemaRef.CurContext->isDependentContext())
23984 return OMPNumTeamsClause::Create(C: getASTContext(), CaptureRegion, StartLoc,
23985 LParenLoc, EndLoc, VL: VarList,
23986 /*PreInit=*/nullptr);
23987
23988 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
23989 SmallVector<Expr *, 3> Vars;
23990 for (Expr *ValExpr : VarList) {
23991 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
23992 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
23993 Vars.push_back(Elt: ValExpr);
23994 }
23995
23996 Stmt *PreInit = buildPreInits(Context&: getASTContext(), Captures);
23997 return OMPNumTeamsClause::Create(C: getASTContext(), CaptureRegion, StartLoc,
23998 LParenLoc, EndLoc, VL: Vars, PreInit);
23999}
24000
24001OMPClause *SemaOpenMP::ActOnOpenMPThreadLimitClause(ArrayRef<Expr *> VarList,
24002 SourceLocation StartLoc,
24003 SourceLocation LParenLoc,
24004 SourceLocation EndLoc) {
24005 if (VarList.empty())
24006 return nullptr;
24007
24008 for (Expr *ValExpr : VarList) {
24009 // OpenMP [teams Constrcut, Restrictions]
24010 // The thread_limit expression must evaluate to a positive integer value.
24011 if (!isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_thread_limit,
24012 /*StrictlyPositive=*/true))
24013 return nullptr;
24014 }
24015
24016 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
24017 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
24018 DKind, CKind: OMPC_thread_limit, OpenMPVersion: getLangOpts().OpenMP);
24019 if (CaptureRegion == OMPD_unknown || SemaRef.CurContext->isDependentContext())
24020 return OMPThreadLimitClause::Create(C: getASTContext(), CaptureRegion,
24021 StartLoc, LParenLoc, EndLoc, VL: VarList,
24022 /*PreInit=*/nullptr);
24023
24024 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
24025 SmallVector<Expr *, 3> Vars;
24026 for (Expr *ValExpr : VarList) {
24027 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
24028 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
24029 Vars.push_back(Elt: ValExpr);
24030 }
24031
24032 Stmt *PreInit = buildPreInits(Context&: getASTContext(), Captures);
24033 return OMPThreadLimitClause::Create(C: getASTContext(), CaptureRegion, StartLoc,
24034 LParenLoc, EndLoc, VL: Vars, PreInit);
24035}
24036
24037OMPClause *SemaOpenMP::ActOnOpenMPPriorityClause(Expr *Priority,
24038 SourceLocation StartLoc,
24039 SourceLocation LParenLoc,
24040 SourceLocation EndLoc) {
24041 Expr *ValExpr = Priority;
24042 Stmt *HelperValStmt = nullptr;
24043 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
24044
24045 // OpenMP [2.9.1, task Constrcut]
24046 // The priority-value is a non-negative numerical scalar expression.
24047 if (!isNonNegativeIntegerValue(
24048 ValExpr, SemaRef, CKind: OMPC_priority,
24049 /*StrictlyPositive=*/false, /*BuildCapture=*/true,
24050 DSAStack->getCurrentDirective(), CaptureRegion: &CaptureRegion, HelperValStmt: &HelperValStmt))
24051 return nullptr;
24052
24053 return new (getASTContext()) OMPPriorityClause(
24054 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
24055}
24056
24057OMPClause *SemaOpenMP::ActOnOpenMPGrainsizeClause(
24058 OpenMPGrainsizeClauseModifier Modifier, Expr *Grainsize,
24059 SourceLocation StartLoc, SourceLocation LParenLoc,
24060 SourceLocation ModifierLoc, SourceLocation EndLoc) {
24061 assert((ModifierLoc.isInvalid() || getLangOpts().OpenMP >= 51) &&
24062 "Unexpected grainsize modifier in OpenMP < 51.");
24063
24064 if (ModifierLoc.isValid() && Modifier == OMPC_GRAINSIZE_unknown) {
24065 std::string Values = getListOfPossibleValues(K: OMPC_grainsize, /*First=*/0,
24066 Last: OMPC_GRAINSIZE_unknown);
24067 Diag(Loc: ModifierLoc, DiagID: diag::err_omp_unexpected_clause_value)
24068 << Values << getOpenMPClauseNameForDiag(C: OMPC_grainsize);
24069 return nullptr;
24070 }
24071
24072 Expr *ValExpr = Grainsize;
24073 Stmt *HelperValStmt = nullptr;
24074 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
24075
24076 // OpenMP [2.9.2, taskloop Constrcut]
24077 // The parameter of the grainsize clause must be a positive integer
24078 // expression.
24079 if (!isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_grainsize,
24080 /*StrictlyPositive=*/true,
24081 /*BuildCapture=*/true,
24082 DSAStack->getCurrentDirective(),
24083 CaptureRegion: &CaptureRegion, HelperValStmt: &HelperValStmt))
24084 return nullptr;
24085
24086 return new (getASTContext())
24087 OMPGrainsizeClause(Modifier, ValExpr, HelperValStmt, CaptureRegion,
24088 StartLoc, LParenLoc, ModifierLoc, EndLoc);
24089}
24090
24091OMPClause *SemaOpenMP::ActOnOpenMPNumTasksClause(
24092 OpenMPNumTasksClauseModifier Modifier, Expr *NumTasks,
24093 SourceLocation StartLoc, SourceLocation LParenLoc,
24094 SourceLocation ModifierLoc, SourceLocation EndLoc) {
24095 assert((ModifierLoc.isInvalid() || getLangOpts().OpenMP >= 51) &&
24096 "Unexpected num_tasks modifier in OpenMP < 51.");
24097
24098 if (ModifierLoc.isValid() && Modifier == OMPC_NUMTASKS_unknown) {
24099 std::string Values = getListOfPossibleValues(K: OMPC_num_tasks, /*First=*/0,
24100 Last: OMPC_NUMTASKS_unknown);
24101 Diag(Loc: ModifierLoc, DiagID: diag::err_omp_unexpected_clause_value)
24102 << Values << getOpenMPClauseNameForDiag(C: OMPC_num_tasks);
24103 return nullptr;
24104 }
24105
24106 Expr *ValExpr = NumTasks;
24107 Stmt *HelperValStmt = nullptr;
24108 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
24109
24110 // OpenMP [2.9.2, taskloop Constrcut]
24111 // The parameter of the num_tasks clause must be a positive integer
24112 // expression.
24113 if (!isNonNegativeIntegerValue(
24114 ValExpr, SemaRef, CKind: OMPC_num_tasks,
24115 /*StrictlyPositive=*/true, /*BuildCapture=*/true,
24116 DSAStack->getCurrentDirective(), CaptureRegion: &CaptureRegion, HelperValStmt: &HelperValStmt))
24117 return nullptr;
24118
24119 return new (getASTContext())
24120 OMPNumTasksClause(Modifier, ValExpr, HelperValStmt, CaptureRegion,
24121 StartLoc, LParenLoc, ModifierLoc, EndLoc);
24122}
24123
24124OMPClause *SemaOpenMP::ActOnOpenMPHintClause(Expr *Hint,
24125 SourceLocation StartLoc,
24126 SourceLocation LParenLoc,
24127 SourceLocation EndLoc) {
24128 // OpenMP [2.13.2, critical construct, Description]
24129 // ... where hint-expression is an integer constant expression that evaluates
24130 // to a valid lock hint.
24131 ExprResult HintExpr =
24132 VerifyPositiveIntegerConstantInClause(E: Hint, CKind: OMPC_hint, StrictlyPositive: false);
24133 if (HintExpr.isInvalid())
24134 return nullptr;
24135 return new (getASTContext())
24136 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
24137}
24138
24139/// Tries to find omp_event_handle_t type.
24140static bool findOMPEventHandleT(Sema &S, SourceLocation Loc,
24141 DSAStackTy *Stack) {
24142 QualType OMPEventHandleT = Stack->getOMPEventHandleT();
24143 if (!OMPEventHandleT.isNull())
24144 return true;
24145 IdentifierInfo *II = &S.PP.getIdentifierTable().get(Name: "omp_event_handle_t");
24146 ParsedType PT = S.getTypeName(II: *II, NameLoc: Loc, S: S.getCurScope());
24147 if (!PT.getAsOpaquePtr() || PT.get().isNull()) {
24148 S.Diag(Loc, DiagID: diag::err_omp_implied_type_not_found) << "omp_event_handle_t";
24149 return false;
24150 }
24151 Stack->setOMPEventHandleT(PT.get());
24152 return true;
24153}
24154
24155OMPClause *SemaOpenMP::ActOnOpenMPDetachClause(Expr *Evt,
24156 SourceLocation StartLoc,
24157 SourceLocation LParenLoc,
24158 SourceLocation EndLoc) {
24159 if (!Evt->isValueDependent() && !Evt->isTypeDependent() &&
24160 !Evt->isInstantiationDependent() &&
24161 !Evt->containsUnexpandedParameterPack()) {
24162 if (!findOMPEventHandleT(S&: SemaRef, Loc: Evt->getExprLoc(), DSAStack))
24163 return nullptr;
24164 // OpenMP 5.0, 2.10.1 task Construct.
24165 // event-handle is a variable of the omp_event_handle_t type.
24166 auto *Ref = dyn_cast<DeclRefExpr>(Val: Evt->IgnoreParenImpCasts());
24167 if (!Ref) {
24168 Diag(Loc: Evt->getExprLoc(), DiagID: diag::err_omp_var_expected)
24169 << "omp_event_handle_t" << 0 << Evt->getSourceRange();
24170 return nullptr;
24171 }
24172 auto *VD = dyn_cast_or_null<VarDecl>(Val: Ref->getDecl());
24173 if (!VD) {
24174 Diag(Loc: Evt->getExprLoc(), DiagID: diag::err_omp_var_expected)
24175 << "omp_event_handle_t" << 0 << Evt->getSourceRange();
24176 return nullptr;
24177 }
24178 if (!getASTContext().hasSameUnqualifiedType(DSAStack->getOMPEventHandleT(),
24179 T2: VD->getType()) ||
24180 VD->getType().isConstant(Ctx: getASTContext())) {
24181 Diag(Loc: Evt->getExprLoc(), DiagID: diag::err_omp_var_expected)
24182 << "omp_event_handle_t" << 1 << VD->getType()
24183 << Evt->getSourceRange();
24184 return nullptr;
24185 }
24186 // OpenMP 5.0, 2.10.1 task Construct
24187 // [detach clause]... The event-handle will be considered as if it was
24188 // specified on a firstprivate clause.
24189 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D: VD, /*FromParent=*/false);
24190 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
24191 DVar.RefExpr) {
24192 Diag(Loc: Evt->getExprLoc(), DiagID: diag::err_omp_wrong_dsa)
24193 << getOpenMPClauseNameForDiag(C: DVar.CKind)
24194 << getOpenMPClauseNameForDiag(C: OMPC_firstprivate);
24195 reportOriginalDsa(SemaRef, DSAStack, D: VD, DVar);
24196 return nullptr;
24197 }
24198 }
24199
24200 return new (getASTContext())
24201 OMPDetachClause(Evt, StartLoc, LParenLoc, EndLoc);
24202}
24203
24204OMPClause *SemaOpenMP::ActOnOpenMPDistScheduleClause(
24205 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
24206 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
24207 SourceLocation EndLoc) {
24208 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
24209 std::string Values;
24210 Values += "'";
24211 Values += getOpenMPSimpleClauseTypeName(Kind: OMPC_dist_schedule, Type: 0);
24212 Values += "'";
24213 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
24214 << Values << getOpenMPClauseNameForDiag(C: OMPC_dist_schedule);
24215 return nullptr;
24216 }
24217 Expr *ValExpr = ChunkSize;
24218 Stmt *HelperValStmt = nullptr;
24219 if (ChunkSize) {
24220 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
24221 !ChunkSize->isInstantiationDependent() &&
24222 !ChunkSize->containsUnexpandedParameterPack()) {
24223 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
24224 ExprResult Val =
24225 PerformOpenMPImplicitIntegerConversion(Loc: ChunkSizeLoc, Op: ChunkSize);
24226 if (Val.isInvalid())
24227 return nullptr;
24228
24229 ValExpr = Val.get();
24230
24231 // OpenMP [2.7.1, Restrictions]
24232 // chunk_size must be a loop invariant integer expression with a positive
24233 // value.
24234 if (std::optional<llvm::APSInt> Result =
24235 ValExpr->getIntegerConstantExpr(Ctx: getASTContext())) {
24236 if (Result->isSigned() && !Result->isStrictlyPositive()) {
24237 Diag(Loc: ChunkSizeLoc, DiagID: diag::err_omp_negative_expression_in_clause)
24238 << "dist_schedule" << /*strictly positive*/ 1
24239 << ChunkSize->getSourceRange();
24240 return nullptr;
24241 }
24242 } else if (getOpenMPCaptureRegionForClause(
24243 DSAStack->getCurrentDirective(), CKind: OMPC_dist_schedule,
24244 OpenMPVersion: getLangOpts().OpenMP) != OMPD_unknown &&
24245 !SemaRef.CurContext->isDependentContext()) {
24246 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
24247 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
24248 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
24249 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
24250 }
24251 }
24252 }
24253
24254 return new (getASTContext())
24255 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
24256 Kind, ValExpr, HelperValStmt);
24257}
24258
24259OMPClause *SemaOpenMP::ActOnOpenMPDefaultmapClause(
24260 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
24261 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
24262 SourceLocation KindLoc, SourceLocation EndLoc) {
24263 if (getLangOpts().OpenMP < 50) {
24264 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
24265 Kind != OMPC_DEFAULTMAP_scalar) {
24266 std::string Value;
24267 SourceLocation Loc;
24268 Value += "'";
24269 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
24270 Value += getOpenMPSimpleClauseTypeName(Kind: OMPC_defaultmap,
24271 Type: OMPC_DEFAULTMAP_MODIFIER_tofrom);
24272 Loc = MLoc;
24273 } else {
24274 Value += getOpenMPSimpleClauseTypeName(Kind: OMPC_defaultmap,
24275 Type: OMPC_DEFAULTMAP_scalar);
24276 Loc = KindLoc;
24277 }
24278 Value += "'";
24279 Diag(Loc, DiagID: diag::err_omp_unexpected_clause_value)
24280 << Value << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24281 return nullptr;
24282 }
24283 } else {
24284 bool isDefaultmapModifier = (M != OMPC_DEFAULTMAP_MODIFIER_unknown);
24285 bool isDefaultmapKind = (Kind != OMPC_DEFAULTMAP_unknown) ||
24286 (getLangOpts().OpenMP >= 50 && KindLoc.isInvalid());
24287 if (!isDefaultmapKind || !isDefaultmapModifier) {
24288 StringRef KindValue = getLangOpts().OpenMP < 52
24289 ? "'scalar', 'aggregate', 'pointer'"
24290 : "'scalar', 'aggregate', 'pointer', 'all'";
24291 if (getLangOpts().OpenMP == 50) {
24292 StringRef ModifierValue = "'alloc', 'from', 'to', 'tofrom', "
24293 "'firstprivate', 'none', 'default'";
24294 if (!isDefaultmapKind && isDefaultmapModifier) {
24295 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
24296 << KindValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24297 } else if (isDefaultmapKind && !isDefaultmapModifier) {
24298 Diag(Loc: MLoc, DiagID: diag::err_omp_unexpected_clause_value)
24299 << ModifierValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24300 } else {
24301 Diag(Loc: MLoc, DiagID: diag::err_omp_unexpected_clause_value)
24302 << ModifierValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24303 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
24304 << KindValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24305 }
24306 } else {
24307 StringRef ModifierValue =
24308 getLangOpts().OpenMP < 60
24309 ? "'alloc', 'from', 'to', 'tofrom', "
24310 "'firstprivate', 'none', 'default', 'present'"
24311 : "'storage', 'from', 'to', 'tofrom', "
24312 "'firstprivate', 'private', 'none', 'default', 'present'";
24313 if (!isDefaultmapKind && isDefaultmapModifier) {
24314 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
24315 << KindValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24316 } else if (isDefaultmapKind && !isDefaultmapModifier) {
24317 Diag(Loc: MLoc, DiagID: diag::err_omp_unexpected_clause_value)
24318 << ModifierValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24319 } else {
24320 Diag(Loc: MLoc, DiagID: diag::err_omp_unexpected_clause_value)
24321 << ModifierValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24322 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
24323 << KindValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24324 }
24325 }
24326 return nullptr;
24327 }
24328
24329 // OpenMP [5.0, 2.12.5, Restrictions, p. 174]
24330 // At most one defaultmap clause for each category can appear on the
24331 // directive.
24332 if (DSAStack->checkDefaultmapCategory(VariableCategory: Kind)) {
24333 Diag(Loc: StartLoc, DiagID: diag::err_omp_one_defaultmap_each_category);
24334 return nullptr;
24335 }
24336 }
24337 if (Kind == OMPC_DEFAULTMAP_unknown || Kind == OMPC_DEFAULTMAP_all) {
24338 // Variable category is not specified - mark all categories.
24339 DSAStack->setDefaultDMAAttr(M, Kind: OMPC_DEFAULTMAP_aggregate, Loc: StartLoc);
24340 DSAStack->setDefaultDMAAttr(M, Kind: OMPC_DEFAULTMAP_scalar, Loc: StartLoc);
24341 DSAStack->setDefaultDMAAttr(M, Kind: OMPC_DEFAULTMAP_pointer, Loc: StartLoc);
24342 } else {
24343 DSAStack->setDefaultDMAAttr(M, Kind, Loc: StartLoc);
24344 }
24345
24346 return new (getASTContext())
24347 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
24348}
24349
24350bool SemaOpenMP::ActOnStartOpenMPDeclareTargetContext(
24351 DeclareTargetContextInfo &DTCI) {
24352 DeclContext *CurLexicalContext = SemaRef.getCurLexicalContext();
24353 if (!CurLexicalContext->isFileContext() &&
24354 !CurLexicalContext->isExternCContext() &&
24355 !CurLexicalContext->isExternCXXContext() &&
24356 !isa<CXXRecordDecl>(Val: CurLexicalContext) &&
24357 !isa<ClassTemplateDecl>(Val: CurLexicalContext) &&
24358 !isa<ClassTemplatePartialSpecializationDecl>(Val: CurLexicalContext) &&
24359 !isa<ClassTemplateSpecializationDecl>(Val: CurLexicalContext)) {
24360 Diag(Loc: DTCI.Loc, DiagID: diag::err_omp_region_not_file_context);
24361 return false;
24362 }
24363
24364 // Report affected OpenMP target offloading behavior when in HIP lang-mode.
24365 if (getLangOpts().HIP)
24366 Diag(Loc: DTCI.Loc, DiagID: diag::warn_hip_omp_target_directives);
24367
24368 DeclareTargetNesting.push_back(Elt: DTCI);
24369 return true;
24370}
24371
24372const SemaOpenMP::DeclareTargetContextInfo
24373SemaOpenMP::ActOnOpenMPEndDeclareTargetDirective() {
24374 assert(!DeclareTargetNesting.empty() &&
24375 "check isInOpenMPDeclareTargetContext() first!");
24376 return DeclareTargetNesting.pop_back_val();
24377}
24378
24379void SemaOpenMP::ActOnFinishedOpenMPDeclareTargetContext(
24380 DeclareTargetContextInfo &DTCI) {
24381 for (auto &It : DTCI.ExplicitlyMapped)
24382 ActOnOpenMPDeclareTargetName(ND: It.first, Loc: It.second.Loc, MT: It.second.MT, DTCI);
24383}
24384
24385void SemaOpenMP::DiagnoseUnterminatedOpenMPDeclareTarget() {
24386 if (DeclareTargetNesting.empty())
24387 return;
24388 DeclareTargetContextInfo &DTCI = DeclareTargetNesting.back();
24389 unsigned OMPVersion = getLangOpts().OpenMP;
24390 Diag(Loc: DTCI.Loc, DiagID: diag::warn_omp_unterminated_declare_target)
24391 << getOpenMPDirectiveName(D: DTCI.Kind, Ver: OMPVersion);
24392}
24393
24394NamedDecl *SemaOpenMP::lookupOpenMPDeclareTargetName(
24395 Scope *CurScope, CXXScopeSpec &ScopeSpec, const DeclarationNameInfo &Id) {
24396 LookupResult Lookup(SemaRef, Id, Sema::LookupOrdinaryName);
24397 SemaRef.LookupParsedName(R&: Lookup, S: CurScope, SS: &ScopeSpec,
24398 /*ObjectType=*/QualType(),
24399 /*AllowBuiltinCreation=*/true);
24400
24401 if (Lookup.isAmbiguous())
24402 return nullptr;
24403 Lookup.suppressDiagnostics();
24404
24405 if (!Lookup.isSingleResult()) {
24406 VarOrFuncDeclFilterCCC CCC(SemaRef);
24407 if (TypoCorrection Corrected =
24408 SemaRef.CorrectTypo(Typo: Id, LookupKind: Sema::LookupOrdinaryName, S: CurScope, SS: nullptr,
24409 CCC, Mode: CorrectTypoKind::ErrorRecovery)) {
24410 SemaRef.diagnoseTypo(Correction: Corrected,
24411 TypoDiag: SemaRef.PDiag(DiagID: diag::err_undeclared_var_use_suggest)
24412 << Id.getName());
24413 checkDeclIsAllowedInOpenMPTarget(E: nullptr, D: Corrected.getCorrectionDecl());
24414 return nullptr;
24415 }
24416
24417 Diag(Loc: Id.getLoc(), DiagID: diag::err_undeclared_var_use) << Id.getName();
24418 return nullptr;
24419 }
24420
24421 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
24422 if (!isa<VarDecl>(Val: ND) && !isa<FunctionDecl>(Val: ND) &&
24423 !isa<FunctionTemplateDecl>(Val: ND)) {
24424 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_invalid_target_decl) << Id.getName();
24425 return nullptr;
24426 }
24427 return ND;
24428}
24429
24430void SemaOpenMP::ActOnOpenMPDeclareTargetName(
24431 NamedDecl *ND, SourceLocation Loc, OMPDeclareTargetDeclAttr::MapTypeTy MT,
24432 DeclareTargetContextInfo &DTCI) {
24433 assert((isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
24434 isa<FunctionTemplateDecl>(ND)) &&
24435 "Expected variable, function or function template.");
24436
24437 if (auto *VD = dyn_cast<VarDecl>(Val: ND)) {
24438 // Only global variables can be marked as declare target.
24439 if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
24440 !VD->isStaticDataMember()) {
24441 Diag(Loc, DiagID: diag::err_omp_declare_target_has_local_vars)
24442 << VD->getNameAsString();
24443 return;
24444 }
24445 }
24446 // Diagnose marking after use as it may lead to incorrect diagnosis and
24447 // codegen.
24448 if (getLangOpts().OpenMP >= 50 &&
24449 (ND->isUsed(/*CheckUsedAttr=*/false) || ND->isReferenced()))
24450 Diag(Loc, DiagID: diag::warn_omp_declare_target_after_first_use);
24451
24452 // Report affected OpenMP target offloading behavior when in HIP lang-mode.
24453 if (getLangOpts().HIP)
24454 Diag(Loc, DiagID: diag::warn_hip_omp_target_directives);
24455
24456 // 'local' is incompatible with 'device_type(host)' because 'local'
24457 // variables exist only on the device.
24458 if (MT == OMPDeclareTargetDeclAttr::MT_Local &&
24459 DTCI.DT == OMPDeclareTargetDeclAttr::DT_Host) {
24460 Diag(Loc, DiagID: diag::err_omp_declare_target_local_host_only);
24461 return;
24462 }
24463
24464 // Explicit declare target lists have precedence.
24465 const unsigned Level = -1;
24466
24467 auto *VD = cast<ValueDecl>(Val: ND);
24468 std::optional<OMPDeclareTargetDeclAttr *> ActiveAttr =
24469 OMPDeclareTargetDeclAttr::getActiveAttr(VD);
24470 if (ActiveAttr && (*ActiveAttr)->getDevType() != DTCI.DT &&
24471 (*ActiveAttr)->getLevel() == Level) {
24472 Diag(Loc, DiagID: diag::err_omp_device_type_mismatch)
24473 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(Val: DTCI.DT)
24474 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(
24475 Val: (*ActiveAttr)->getDevType());
24476 return;
24477 }
24478 if (ActiveAttr && (*ActiveAttr)->getMapType() != MT &&
24479 (*ActiveAttr)->getLevel() == Level) {
24480 Diag(Loc, DiagID: diag::err_omp_declare_target_var_in_both_clauses)
24481 << ND
24482 << OMPDeclareTargetDeclAttr::ConvertMapTypeTyToStr(
24483 Val: (*ActiveAttr)->getMapType())
24484 << OMPDeclareTargetDeclAttr::ConvertMapTypeTyToStr(Val: MT);
24485 return;
24486 }
24487
24488 if (ActiveAttr && (*ActiveAttr)->getLevel() == Level)
24489 return;
24490
24491 Expr *IndirectE = nullptr;
24492 bool IsIndirect = false;
24493 if (DTCI.Indirect) {
24494 IndirectE = *DTCI.Indirect;
24495 if (!IndirectE)
24496 IsIndirect = true;
24497 }
24498 // FIXME: 'local' clause is not yet implemented in CodeGen. For now, it is
24499 // treated as 'enter'. For host compilation, 'local' is a no-op.
24500 if (MT == OMPDeclareTargetDeclAttr::MT_Local &&
24501 getLangOpts().OpenMPIsTargetDevice)
24502 Diag(Loc, DiagID: diag::warn_omp_declare_target_local_not_implemented);
24503 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
24504 Ctx&: getASTContext(), MapType: MT, DevType: DTCI.DT, IndirectExpr: IndirectE, Indirect: IsIndirect, Level,
24505 Range: SourceRange(Loc, Loc));
24506 ND->addAttr(A);
24507 if (ASTMutationListener *ML = getASTContext().getASTMutationListener())
24508 ML->DeclarationMarkedOpenMPDeclareTarget(D: ND, Attr: A);
24509 checkDeclIsAllowedInOpenMPTarget(E: nullptr, D: ND, IdLoc: Loc);
24510 if (auto *VD = dyn_cast<VarDecl>(Val: ND);
24511 getLangOpts().OpenMP && VD && VD->hasAttr<OMPDeclareTargetDeclAttr>() &&
24512 VD->hasGlobalStorage())
24513 ActOnOpenMPDeclareTargetInitializer(D: ND);
24514}
24515
24516static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
24517 Sema &SemaRef, Decl *D) {
24518 if (!D || !isa<VarDecl>(Val: D))
24519 return;
24520 auto *VD = cast<VarDecl>(Val: D);
24521 std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy =
24522 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
24523 if (SemaRef.LangOpts.OpenMP >= 50 &&
24524 (SemaRef.getCurLambda(/*IgnoreNonLambdaCapturingScope=*/true) ||
24525 SemaRef.getCurBlock() || SemaRef.getCurCapturedRegion()) &&
24526 VD->hasGlobalStorage()) {
24527 if (!MapTy || (*MapTy != OMPDeclareTargetDeclAttr::MT_To &&
24528 *MapTy != OMPDeclareTargetDeclAttr::MT_Enter &&
24529 *MapTy != OMPDeclareTargetDeclAttr::MT_Local)) {
24530 // OpenMP 5.0, 2.12.7 declare target Directive, Restrictions
24531 // If a lambda declaration and definition appears between a
24532 // declare target directive and the matching end declare target
24533 // directive, all variables that are captured by the lambda
24534 // expression must also appear in a to clause.
24535 SemaRef.Diag(Loc: VD->getLocation(),
24536 DiagID: diag::err_omp_lambda_capture_in_declare_target_not_to);
24537 SemaRef.Diag(Loc: SL, DiagID: diag::note_var_explicitly_captured_here)
24538 << VD << 0 << SR;
24539 return;
24540 }
24541 }
24542 if (MapTy)
24543 return;
24544 SemaRef.Diag(Loc: VD->getLocation(), DiagID: diag::warn_omp_not_in_target_context);
24545 SemaRef.Diag(Loc: SL, DiagID: diag::note_used_here) << SR;
24546}
24547
24548static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
24549 Sema &SemaRef, DSAStackTy *Stack,
24550 ValueDecl *VD) {
24551 return OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) ||
24552 checkTypeMappable(SL, SR, SemaRef, Stack, QTy: VD->getType(),
24553 /*FullCheck=*/false);
24554}
24555
24556void SemaOpenMP::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
24557 SourceLocation IdLoc) {
24558 if (!D || D->isInvalidDecl())
24559 return;
24560 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
24561 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
24562 if (auto *VD = dyn_cast<VarDecl>(Val: D)) {
24563 // Only global variables can be marked as declare target.
24564 if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
24565 !VD->isStaticDataMember())
24566 return;
24567 // 2.10.6: threadprivate variable cannot appear in a declare target
24568 // directive.
24569 if (DSAStack->isThreadPrivate(D: VD)) {
24570 Diag(Loc: SL, DiagID: diag::err_omp_threadprivate_in_target);
24571 reportOriginalDsa(SemaRef, DSAStack, D: VD, DSAStack->getTopDSA(D: VD, FromParent: false));
24572 return;
24573 }
24574 }
24575 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(Val: D))
24576 D = FTD->getTemplatedDecl();
24577 if (auto *FD = dyn_cast<FunctionDecl>(Val: D)) {
24578 std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
24579 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD: FD);
24580 if (IdLoc.isValid() && Res &&
24581 (*Res == OMPDeclareTargetDeclAttr::MT_Link ||
24582 *Res == OMPDeclareTargetDeclAttr::MT_Local)) {
24583 Diag(Loc: IdLoc, DiagID: diag::err_omp_function_in_target_clause_list)
24584 << OMPDeclareTargetDeclAttr::ConvertMapTypeTyToStr(Val: *Res);
24585 Diag(Loc: FD->getLocation(), DiagID: diag::note_defined_here) << FD;
24586 return;
24587 }
24588 }
24589 if (auto *VD = dyn_cast<ValueDecl>(Val: D)) {
24590 // Problem if any with var declared with incomplete type will be reported
24591 // as normal, so no need to check it here.
24592 if ((E || !VD->getType()->isIncompleteType()) &&
24593 !checkValueDeclInTarget(SL, SR, SemaRef, DSAStack, VD))
24594 return;
24595 if (!E && isInOpenMPDeclareTargetContext()) {
24596 // Checking declaration inside declare target region.
24597 if (isa<VarDecl>(Val: D) || isa<FunctionDecl>(Val: D) ||
24598 isa<FunctionTemplateDecl>(Val: D)) {
24599 std::optional<OMPDeclareTargetDeclAttr *> ActiveAttr =
24600 OMPDeclareTargetDeclAttr::getActiveAttr(VD);
24601 unsigned Level = DeclareTargetNesting.size();
24602 if (ActiveAttr && (*ActiveAttr)->getLevel() >= Level)
24603 return;
24604 DeclareTargetContextInfo &DTCI = DeclareTargetNesting.back();
24605 Expr *IndirectE = nullptr;
24606 bool IsIndirect = false;
24607 if (DTCI.Indirect) {
24608 IndirectE = *DTCI.Indirect;
24609 if (!IndirectE)
24610 IsIndirect = true;
24611 }
24612 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
24613 Ctx&: getASTContext(),
24614 MapType: getLangOpts().OpenMP >= 52 ? OMPDeclareTargetDeclAttr::MT_Enter
24615 : OMPDeclareTargetDeclAttr::MT_To,
24616 DevType: DTCI.DT, IndirectExpr: IndirectE, Indirect: IsIndirect, Level,
24617 Range: SourceRange(DTCI.Loc, DTCI.Loc));
24618 D->addAttr(A);
24619 if (ASTMutationListener *ML = getASTContext().getASTMutationListener())
24620 ML->DeclarationMarkedOpenMPDeclareTarget(D, Attr: A);
24621 }
24622 return;
24623 }
24624 }
24625 if (!E)
24626 return;
24627 checkDeclInTargetContext(SL: E->getExprLoc(), SR: E->getSourceRange(), SemaRef, D);
24628}
24629
24630/// This class visits every VarDecl that the initializer references and adds
24631/// OMPDeclareTargetDeclAttr to each of them.
24632class GlobalDeclRefChecker final : public StmtVisitor<GlobalDeclRefChecker> {
24633 SmallVector<VarDecl *> DeclVector;
24634 Attr *A;
24635
24636public:
24637 /// A StmtVisitor class function that visits all DeclRefExpr and adds
24638 /// OMPDeclareTargetDeclAttr to them.
24639 void VisitDeclRefExpr(DeclRefExpr *Node) {
24640 if (auto *VD = dyn_cast<VarDecl>(Val: Node->getDecl())) {
24641 VD->addAttr(A);
24642 DeclVector.push_back(Elt: VD);
24643 }
24644 }
24645 /// A function that iterates across each of the Expr's children.
24646 void VisitExpr(Expr *Ex) {
24647 for (auto *Child : Ex->children()) {
24648 Visit(S: Child);
24649 }
24650 }
24651 /// A function that keeps a record of all the Decls that are variables, has
24652 /// OMPDeclareTargetDeclAttr, and has global storage in the DeclVector. Pop
24653 /// each Decl one at a time and use the inherited 'visit' functions to look
24654 /// for DeclRefExpr.
24655 void declareTargetInitializer(Decl *TD) {
24656 A = TD->getAttr<OMPDeclareTargetDeclAttr>();
24657 DeclVector.push_back(Elt: cast<VarDecl>(Val: TD));
24658 llvm::SmallDenseSet<Decl *> Visited;
24659 while (!DeclVector.empty()) {
24660 VarDecl *TargetVarDecl = DeclVector.pop_back_val();
24661 if (!Visited.insert(V: TargetVarDecl).second)
24662 continue;
24663
24664 if (TargetVarDecl->hasAttr<OMPDeclareTargetDeclAttr>() &&
24665 TargetVarDecl->hasInit() && TargetVarDecl->hasGlobalStorage()) {
24666 if (Expr *Ex = TargetVarDecl->getInit())
24667 Visit(S: Ex);
24668 }
24669 }
24670 }
24671};
24672
24673/// Adding OMPDeclareTargetDeclAttr to variables with static storage
24674/// duration that are referenced in the initializer expression list of
24675/// variables with static storage duration in declare target directive.
24676void SemaOpenMP::ActOnOpenMPDeclareTargetInitializer(Decl *TargetDecl) {
24677 GlobalDeclRefChecker Checker;
24678 if (isa<VarDecl>(Val: TargetDecl))
24679 Checker.declareTargetInitializer(TD: TargetDecl);
24680}
24681
24682OMPClause *SemaOpenMP::ActOnOpenMPToClause(
24683 ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
24684 ArrayRef<SourceLocation> MotionModifiersLoc, Expr *IteratorExpr,
24685 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
24686 SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
24687 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) {
24688 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown,
24689 OMPC_MOTION_MODIFIER_unknown,
24690 OMPC_MOTION_MODIFIER_unknown};
24691 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers];
24692
24693 // Process motion-modifiers, flag errors for duplicate modifiers.
24694 unsigned Count = 0;
24695 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) {
24696 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown &&
24697 llvm::is_contained(Range&: Modifiers, Element: MotionModifiers[I])) {
24698 Diag(Loc: MotionModifiersLoc[I], DiagID: diag::err_omp_duplicate_motion_modifier);
24699 continue;
24700 }
24701 assert(Count < NumberOfOMPMotionModifiers &&
24702 "Modifiers exceed the allowed number of motion modifiers");
24703 Modifiers[Count] = MotionModifiers[I];
24704 ModifiersLoc[Count] = MotionModifiersLoc[I];
24705 ++Count;
24706 }
24707
24708 MappableVarListInfo MVLI(VarList);
24709 checkMappableExpressionList(SemaRef, DSAStack, CKind: OMPC_to, MVLI, StartLoc: Locs.StartLoc,
24710 MapperIdScopeSpec, MapperId, UnresolvedMappers);
24711 if (MVLI.ProcessedVarList.empty())
24712 return nullptr;
24713 if (IteratorExpr)
24714 if (auto *DRE = dyn_cast<DeclRefExpr>(Val: IteratorExpr))
24715 if (auto *VD = dyn_cast<VarDecl>(Val: DRE->getDecl()))
24716 DSAStack->addIteratorVarDecl(VD);
24717 return OMPToClause::Create(
24718 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, Declarations: MVLI.VarBaseDeclarations,
24719 ComponentLists: MVLI.VarComponents, UDMapperRefs: MVLI.UDMapperList, IteratorModifier: IteratorExpr, MotionModifiers: Modifiers,
24720 MotionModifiersLoc: ModifiersLoc, UDMQualifierLoc: MapperIdScopeSpec.getWithLocInContext(Context&: getASTContext()),
24721 MapperId);
24722}
24723
24724OMPClause *SemaOpenMP::ActOnOpenMPFromClause(
24725 ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
24726 ArrayRef<SourceLocation> MotionModifiersLoc, Expr *IteratorExpr,
24727 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
24728 SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
24729 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) {
24730 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown,
24731 OMPC_MOTION_MODIFIER_unknown,
24732 OMPC_MOTION_MODIFIER_unknown};
24733 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers];
24734
24735 // Process motion-modifiers, flag errors for duplicate modifiers.
24736 unsigned Count = 0;
24737 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) {
24738 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown &&
24739 llvm::is_contained(Range&: Modifiers, Element: MotionModifiers[I])) {
24740 Diag(Loc: MotionModifiersLoc[I], DiagID: diag::err_omp_duplicate_motion_modifier);
24741 continue;
24742 }
24743 assert(Count < NumberOfOMPMotionModifiers &&
24744 "Modifiers exceed the allowed number of motion modifiers");
24745 Modifiers[Count] = MotionModifiers[I];
24746 ModifiersLoc[Count] = MotionModifiersLoc[I];
24747 ++Count;
24748 }
24749
24750 MappableVarListInfo MVLI(VarList);
24751 checkMappableExpressionList(SemaRef, DSAStack, CKind: OMPC_from, MVLI, StartLoc: Locs.StartLoc,
24752 MapperIdScopeSpec, MapperId, UnresolvedMappers);
24753 if (MVLI.ProcessedVarList.empty())
24754 return nullptr;
24755 if (IteratorExpr)
24756 if (auto *DRE = dyn_cast<DeclRefExpr>(Val: IteratorExpr))
24757 if (auto *VD = dyn_cast<VarDecl>(Val: DRE->getDecl()))
24758 DSAStack->addIteratorVarDecl(VD);
24759 return OMPFromClause::Create(
24760 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, Declarations: MVLI.VarBaseDeclarations,
24761 ComponentLists: MVLI.VarComponents, UDMapperRefs: MVLI.UDMapperList, IteratorExpr, MotionModifiers: Modifiers,
24762 MotionModifiersLoc: ModifiersLoc, UDMQualifierLoc: MapperIdScopeSpec.getWithLocInContext(Context&: getASTContext()),
24763 MapperId);
24764}
24765
24766OMPClause *SemaOpenMP::ActOnOpenMPUseDevicePtrClause(
24767 ArrayRef<Expr *> VarList, const OMPVarListLocTy &Locs,
24768 OpenMPUseDevicePtrFallbackModifier FallbackModifier,
24769 SourceLocation FallbackModifierLoc) {
24770 MappableVarListInfo MVLI(VarList);
24771 SmallVector<Expr *, 8> PrivateCopies;
24772 SmallVector<Expr *, 8> Inits;
24773
24774 for (Expr *RefExpr : VarList) {
24775 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
24776 SourceLocation ELoc;
24777 SourceRange ERange;
24778 Expr *SimpleRefExpr = RefExpr;
24779 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
24780 if (Res.second) {
24781 // It will be analyzed later.
24782 MVLI.ProcessedVarList.push_back(Elt: RefExpr);
24783 PrivateCopies.push_back(Elt: nullptr);
24784 Inits.push_back(Elt: nullptr);
24785 }
24786 ValueDecl *D = Res.first;
24787 if (!D)
24788 continue;
24789
24790 QualType Type = D->getType();
24791 Type = Type.getNonReferenceType().getUnqualifiedType();
24792
24793 auto *VD = dyn_cast<VarDecl>(Val: D);
24794
24795 // Item should be a pointer or reference to pointer.
24796 if (!Type->isPointerType()) {
24797 Diag(Loc: ELoc, DiagID: diag::err_omp_usedeviceptr_not_a_pointer)
24798 << 0 << RefExpr->getSourceRange();
24799 continue;
24800 }
24801
24802 // Build the private variable and the expression that refers to it.
24803 auto VDPrivate =
24804 buildVarDecl(SemaRef, Loc: ELoc, Type, Name: D->getName(),
24805 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr,
24806 OrigRef: VD ? cast<DeclRefExpr>(Val: SimpleRefExpr) : nullptr);
24807 if (VDPrivate->isInvalidDecl())
24808 continue;
24809
24810 SemaRef.CurContext->addDecl(D: VDPrivate);
24811 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
24812 S&: SemaRef, D: VDPrivate, Ty: RefExpr->getType().getUnqualifiedType(), Loc: ELoc);
24813
24814 // Add temporary variable to initialize the private copy of the pointer.
24815 VarDecl *VDInit =
24816 buildVarDecl(SemaRef, Loc: RefExpr->getExprLoc(), Type, Name: ".devptr.temp");
24817 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
24818 S&: SemaRef, D: VDInit, Ty: RefExpr->getType(), Loc: RefExpr->getExprLoc());
24819 SemaRef.AddInitializerToDecl(
24820 dcl: VDPrivate, init: SemaRef.DefaultLvalueConversion(E: VDInitRefExpr).get(),
24821 /*DirectInit=*/false);
24822
24823 // If required, build a capture to implement the privatization initialized
24824 // with the current list item value.
24825 DeclRefExpr *Ref = nullptr;
24826 if (!VD)
24827 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/true);
24828 MVLI.ProcessedVarList.push_back(Elt: VD ? RefExpr->IgnoreParens() : Ref);
24829 PrivateCopies.push_back(Elt: VDPrivateRefExpr);
24830 Inits.push_back(Elt: VDInitRefExpr);
24831
24832 // We need to add a data sharing attribute for this variable to make sure it
24833 // is correctly captured. A variable that shows up in a use_device_ptr has
24834 // similar properties of a first private variable.
24835 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_firstprivate, PrivateCopy: Ref);
24836
24837 // Create a mappable component for the list item. List items in this clause
24838 // only need a component.
24839 MVLI.VarBaseDeclarations.push_back(Elt: D);
24840 MVLI.VarComponents.resize(N: MVLI.VarComponents.size() + 1);
24841 MVLI.VarComponents.back().emplace_back(Args&: SimpleRefExpr, Args&: D,
24842 /*IsNonContiguous=*/Args: false);
24843 }
24844
24845 if (MVLI.ProcessedVarList.empty())
24846 return nullptr;
24847
24848 return OMPUseDevicePtrClause::Create(
24849 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, PrivateVars: PrivateCopies, Inits,
24850 Declarations: MVLI.VarBaseDeclarations, ComponentLists: MVLI.VarComponents, FallbackModifier,
24851 FallbackModifierLoc);
24852}
24853
24854OMPClause *
24855SemaOpenMP::ActOnOpenMPUseDeviceAddrClause(ArrayRef<Expr *> VarList,
24856 const OMPVarListLocTy &Locs) {
24857 MappableVarListInfo MVLI(VarList);
24858
24859 for (Expr *RefExpr : VarList) {
24860 assert(RefExpr && "NULL expr in OpenMP use_device_addr clause.");
24861 SourceLocation ELoc;
24862 SourceRange ERange;
24863 Expr *SimpleRefExpr = RefExpr;
24864 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange,
24865 /*AllowArraySection=*/true,
24866 /*AllowAssumedSizeArray=*/true);
24867 if (Res.second) {
24868 // It will be analyzed later.
24869 MVLI.ProcessedVarList.push_back(Elt: RefExpr);
24870 }
24871 ValueDecl *D = Res.first;
24872 if (!D)
24873 continue;
24874 auto *VD = dyn_cast<VarDecl>(Val: D);
24875
24876 // If required, build a capture to implement the privatization initialized
24877 // with the current list item value.
24878 DeclRefExpr *Ref = nullptr;
24879 if (!VD)
24880 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/true);
24881 MVLI.ProcessedVarList.push_back(Elt: VD ? RefExpr->IgnoreParens() : Ref);
24882
24883 // We need to add a data sharing attribute for this variable to make sure it
24884 // is correctly captured. A variable that shows up in a use_device_addr has
24885 // similar properties of a first private variable.
24886 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_firstprivate, PrivateCopy: Ref);
24887
24888 // Use the map-like approach to fully populate VarComponents
24889 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
24890
24891 const Expr *BE = checkMapClauseExpressionBase(
24892 SemaRef, E: RefExpr, CurComponents, CKind: OMPC_use_device_addr,
24893 DSAStack->getCurrentDirective(),
24894 /*NoDiagnose=*/false);
24895
24896 if (!BE)
24897 continue;
24898
24899 assert(!CurComponents.empty() &&
24900 "use_device_addr clause expression with no components!");
24901
24902 // OpenMP use_device_addr: If a list item is an array section, the array
24903 // base must be a base language identifier. We caught the cases where
24904 // the array-section has a base-variable in getPrivateItem. e.g.
24905 // struct S {
24906 // int a[10];
24907 // }; S s1;
24908 // ... use_device_addr(s1.a[0]) // not ok, caught already
24909 //
24910 // But we still neeed to verify that the base-pointer is also a
24911 // base-language identifier, and catch cases like:
24912 // int *pa[10]; *p;
24913 // ... use_device_addr(pa[1][2]) // not ok, base-pointer is pa[1]
24914 // ... use_device_addr(p[1]) // ok
24915 // ... use_device_addr(this->p[1]) // ok
24916 auto AttachPtrResult = OMPClauseMappableExprCommon::findAttachPtrExpr(
24917 Components: CurComponents, DSAStack->getCurrentDirective());
24918 const Expr *AttachPtrExpr = AttachPtrResult.first;
24919
24920 if (AttachPtrExpr) {
24921 const Expr *BaseExpr = AttachPtrExpr->IgnoreParenImpCasts();
24922 bool IsValidBase = false;
24923
24924 if (isa<DeclRefExpr>(Val: BaseExpr))
24925 IsValidBase = true;
24926 else if (const auto *ME = dyn_cast<MemberExpr>(Val: BaseExpr);
24927 ME && isa<CXXThisExpr>(Val: ME->getBase()->IgnoreParenImpCasts()))
24928 IsValidBase = true;
24929
24930 if (!IsValidBase) {
24931 SemaRef.Diag(Loc: ELoc,
24932 DiagID: diag::err_omp_expected_base_pointer_var_name_member_expr)
24933 << (SemaRef.getCurrentThisType().isNull() ? 0 : 1)
24934 << AttachPtrExpr->getSourceRange();
24935 continue;
24936 }
24937 }
24938
24939 // Get the declaration from the components
24940 ValueDecl *CurDeclaration = CurComponents.back().getAssociatedDeclaration();
24941 assert((isa<CXXThisExpr>(BE) || CurDeclaration) &&
24942 "Unexpected null decl for use_device_addr clause.");
24943
24944 MVLI.VarBaseDeclarations.push_back(Elt: CurDeclaration);
24945 MVLI.VarComponents.resize(N: MVLI.VarComponents.size() + 1);
24946 MVLI.VarComponents.back().append(in_start: CurComponents.begin(),
24947 in_end: CurComponents.end());
24948 }
24949
24950 if (MVLI.ProcessedVarList.empty())
24951 return nullptr;
24952
24953 return OMPUseDeviceAddrClause::Create(
24954 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, Declarations: MVLI.VarBaseDeclarations,
24955 ComponentLists: MVLI.VarComponents);
24956}
24957
24958OMPClause *
24959SemaOpenMP::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
24960 const OMPVarListLocTy &Locs) {
24961 MappableVarListInfo MVLI(VarList);
24962 for (Expr *RefExpr : VarList) {
24963 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
24964 SourceLocation ELoc;
24965 SourceRange ERange;
24966 Expr *SimpleRefExpr = RefExpr;
24967 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
24968 if (Res.second) {
24969 // It will be analyzed later.
24970 MVLI.ProcessedVarList.push_back(Elt: RefExpr);
24971 }
24972 ValueDecl *D = Res.first;
24973 if (!D)
24974 continue;
24975
24976 QualType Type = D->getType();
24977 // item should be a pointer or array or reference to pointer or array
24978 if (!Type.getNonReferenceType()->isPointerType() &&
24979 !Type.getNonReferenceType()->isArrayType()) {
24980 Diag(Loc: ELoc, DiagID: diag::err_omp_argument_type_isdeviceptr)
24981 << 0 << RefExpr->getSourceRange();
24982 continue;
24983 }
24984
24985 // Check if the declaration in the clause does not show up in any data
24986 // sharing attribute.
24987 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
24988 if (isOpenMPPrivate(Kind: DVar.CKind)) {
24989 unsigned OMPVersion = getLangOpts().OpenMP;
24990 Diag(Loc: ELoc, DiagID: diag::err_omp_variable_in_given_clause_and_dsa)
24991 << getOpenMPClauseNameForDiag(C: DVar.CKind)
24992 << getOpenMPClauseNameForDiag(C: OMPC_is_device_ptr)
24993 << getOpenMPDirectiveName(DSAStack->getCurrentDirective(),
24994 Ver: OMPVersion);
24995 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
24996 continue;
24997 }
24998
24999 const Expr *ConflictExpr;
25000 if (DSAStack->checkMappableExprComponentListsForDecl(
25001 VD: D, /*CurrentRegionOnly=*/true,
25002 Check: [&ConflictExpr](
25003 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
25004 OpenMPClauseKind) -> bool {
25005 ConflictExpr = R.front().getAssociatedExpression();
25006 return true;
25007 })) {
25008 Diag(Loc: ELoc, DiagID: diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
25009 Diag(Loc: ConflictExpr->getExprLoc(), DiagID: diag::note_used_here)
25010 << ConflictExpr->getSourceRange();
25011 continue;
25012 }
25013
25014 // Store the components in the stack so that they can be used to check
25015 // against other clauses later on.
25016 OMPClauseMappableExprCommon::MappableComponent MC(
25017 SimpleRefExpr, D, /*IsNonContiguous=*/false);
25018 DSAStack->addMappableExpressionComponents(
25019 VD: D, Components: MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
25020
25021 // Record the expression we've just processed.
25022 MVLI.ProcessedVarList.push_back(Elt: SimpleRefExpr);
25023
25024 // Create a mappable component for the list item. List items in this clause
25025 // only need a component. We use a null declaration to signal fields in
25026 // 'this'.
25027 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
25028 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
25029 "Unexpected device pointer expression!");
25030 MVLI.VarBaseDeclarations.push_back(
25031 Elt: isa<DeclRefExpr>(Val: SimpleRefExpr) ? D : nullptr);
25032 MVLI.VarComponents.resize(N: MVLI.VarComponents.size() + 1);
25033 MVLI.VarComponents.back().push_back(Elt: MC);
25034 }
25035
25036 if (MVLI.ProcessedVarList.empty())
25037 return nullptr;
25038
25039 return OMPIsDevicePtrClause::Create(
25040 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, Declarations: MVLI.VarBaseDeclarations,
25041 ComponentLists: MVLI.VarComponents);
25042}
25043
25044OMPClause *
25045SemaOpenMP::ActOnOpenMPHasDeviceAddrClause(ArrayRef<Expr *> VarList,
25046 const OMPVarListLocTy &Locs) {
25047 MappableVarListInfo MVLI(VarList);
25048 for (Expr *RefExpr : VarList) {
25049 assert(RefExpr && "NULL expr in OpenMP has_device_addr clause.");
25050 SourceLocation ELoc;
25051 SourceRange ERange;
25052 Expr *SimpleRefExpr = RefExpr;
25053 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange,
25054 /*AllowArraySection=*/true);
25055 if (Res.second) {
25056 // It will be analyzed later.
25057 MVLI.ProcessedVarList.push_back(Elt: RefExpr);
25058 }
25059 ValueDecl *D = Res.first;
25060 if (!D)
25061 continue;
25062
25063 // Check if the declaration in the clause does not show up in any data
25064 // sharing attribute.
25065 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
25066 if (isOpenMPPrivate(Kind: DVar.CKind)) {
25067 unsigned OMPVersion = getLangOpts().OpenMP;
25068 Diag(Loc: ELoc, DiagID: diag::err_omp_variable_in_given_clause_and_dsa)
25069 << getOpenMPClauseNameForDiag(C: DVar.CKind)
25070 << getOpenMPClauseNameForDiag(C: OMPC_has_device_addr)
25071 << getOpenMPDirectiveName(DSAStack->getCurrentDirective(),
25072 Ver: OMPVersion);
25073 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
25074 continue;
25075 }
25076
25077 const Expr *ConflictExpr;
25078 if (DSAStack->checkMappableExprComponentListsForDecl(
25079 VD: D, /*CurrentRegionOnly=*/true,
25080 Check: [&ConflictExpr](
25081 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
25082 OpenMPClauseKind) -> bool {
25083 ConflictExpr = R.front().getAssociatedExpression();
25084 return true;
25085 })) {
25086 Diag(Loc: ELoc, DiagID: diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
25087 Diag(Loc: ConflictExpr->getExprLoc(), DiagID: diag::note_used_here)
25088 << ConflictExpr->getSourceRange();
25089 continue;
25090 }
25091
25092 // Store the components in the stack so that they can be used to check
25093 // against other clauses later on.
25094 Expr *Component = SimpleRefExpr;
25095 auto *VD = dyn_cast<VarDecl>(Val: D);
25096 if (VD && (isa<ArraySectionExpr>(Val: RefExpr->IgnoreParenImpCasts()) ||
25097 isa<ArraySubscriptExpr>(Val: RefExpr->IgnoreParenImpCasts())))
25098 Component =
25099 SemaRef.DefaultFunctionArrayLvalueConversion(E: SimpleRefExpr).get();
25100 OMPClauseMappableExprCommon::MappableComponent MC(
25101 Component, D, /*IsNonContiguous=*/false);
25102 DSAStack->addMappableExpressionComponents(
25103 VD: D, Components: MC, /*WhereFoundClauseKind=*/OMPC_has_device_addr);
25104
25105 // Record the expression we've just processed.
25106 if (!VD && !SemaRef.CurContext->isDependentContext()) {
25107 DeclRefExpr *Ref =
25108 buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/true);
25109 assert(Ref && "has_device_addr capture failed");
25110 MVLI.ProcessedVarList.push_back(Elt: Ref);
25111 } else
25112 MVLI.ProcessedVarList.push_back(Elt: RefExpr->IgnoreParens());
25113
25114 // Create a mappable component for the list item. List items in this clause
25115 // only need a component. We use a null declaration to signal fields in
25116 // 'this'.
25117 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
25118 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
25119 "Unexpected device pointer expression!");
25120 MVLI.VarBaseDeclarations.push_back(
25121 Elt: isa<DeclRefExpr>(Val: SimpleRefExpr) ? D : nullptr);
25122 MVLI.VarComponents.resize(N: MVLI.VarComponents.size() + 1);
25123 MVLI.VarComponents.back().push_back(Elt: MC);
25124 }
25125
25126 if (MVLI.ProcessedVarList.empty())
25127 return nullptr;
25128
25129 return OMPHasDeviceAddrClause::Create(
25130 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, Declarations: MVLI.VarBaseDeclarations,
25131 ComponentLists: MVLI.VarComponents);
25132}
25133
25134OMPClause *SemaOpenMP::ActOnOpenMPAllocateClause(
25135 Expr *Allocator, Expr *Alignment,
25136 OpenMPAllocateClauseModifier FirstAllocateModifier,
25137 SourceLocation FirstAllocateModifierLoc,
25138 OpenMPAllocateClauseModifier SecondAllocateModifier,
25139 SourceLocation SecondAllocateModifierLoc, ArrayRef<Expr *> VarList,
25140 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
25141 SourceLocation EndLoc) {
25142 if (Allocator) {
25143 // Allocator expression is dependent - skip it for now and build the
25144 // allocator when instantiated.
25145 bool AllocDependent =
25146 (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
25147 Allocator->isInstantiationDependent() ||
25148 Allocator->containsUnexpandedParameterPack());
25149 if (!AllocDependent) {
25150 // OpenMP [2.11.4 allocate Clause, Description]
25151 // allocator is an expression of omp_allocator_handle_t type.
25152 if (!findOMPAllocatorHandleT(S&: SemaRef, Loc: Allocator->getExprLoc(), DSAStack))
25153 return nullptr;
25154
25155 ExprResult AllocatorRes = SemaRef.DefaultLvalueConversion(E: Allocator);
25156 if (AllocatorRes.isInvalid())
25157 return nullptr;
25158 AllocatorRes = SemaRef.PerformImplicitConversion(
25159 From: AllocatorRes.get(), DSAStack->getOMPAllocatorHandleT(),
25160 Action: AssignmentAction::Initializing,
25161 /*AllowExplicit=*/true);
25162 if (AllocatorRes.isInvalid())
25163 return nullptr;
25164 Allocator = AllocatorRes.isUsable() ? AllocatorRes.get() : nullptr;
25165 }
25166 } else {
25167 // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions.
25168 // allocate clauses that appear on a target construct or on constructs in a
25169 // target region must specify an allocator expression unless a requires
25170 // directive with the dynamic_allocators clause is present in the same
25171 // compilation unit.
25172 if (getLangOpts().OpenMPIsTargetDevice &&
25173 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
25174 SemaRef.targetDiag(Loc: StartLoc, DiagID: diag::err_expected_allocator_expression);
25175 }
25176 if (Alignment) {
25177 bool AlignmentDependent = Alignment->isTypeDependent() ||
25178 Alignment->isValueDependent() ||
25179 Alignment->isInstantiationDependent() ||
25180 Alignment->containsUnexpandedParameterPack();
25181 if (!AlignmentDependent) {
25182 ExprResult AlignResult =
25183 VerifyPositiveIntegerConstantInClause(E: Alignment, CKind: OMPC_allocate);
25184 Alignment = AlignResult.isUsable() ? AlignResult.get() : nullptr;
25185 }
25186 }
25187 // Analyze and build list of variables.
25188 SmallVector<Expr *, 8> Vars;
25189 for (Expr *RefExpr : VarList) {
25190 assert(RefExpr && "NULL expr in OpenMP allocate clause.");
25191 SourceLocation ELoc;
25192 SourceRange ERange;
25193 Expr *SimpleRefExpr = RefExpr;
25194 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
25195 if (Res.second) {
25196 // It will be analyzed later.
25197 Vars.push_back(Elt: RefExpr);
25198 }
25199 ValueDecl *D = Res.first;
25200 if (!D)
25201 continue;
25202
25203 auto *VD = dyn_cast<VarDecl>(Val: D);
25204 DeclRefExpr *Ref = nullptr;
25205 if (!VD && !SemaRef.CurContext->isDependentContext())
25206 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/false);
25207 Vars.push_back(Elt: (VD || SemaRef.CurContext->isDependentContext())
25208 ? RefExpr->IgnoreParens()
25209 : Ref);
25210 }
25211
25212 if (Vars.empty())
25213 return nullptr;
25214
25215 if (Allocator)
25216 DSAStack->addInnerAllocatorExpr(E: Allocator);
25217
25218 return OMPAllocateClause::Create(
25219 C: getASTContext(), StartLoc, LParenLoc, Allocator, Alignment, ColonLoc,
25220 Modifier1: FirstAllocateModifier, Modifier1Loc: FirstAllocateModifierLoc, Modifier2: SecondAllocateModifier,
25221 Modifier2Loc: SecondAllocateModifierLoc, EndLoc, VL: Vars);
25222}
25223
25224OMPClause *SemaOpenMP::ActOnOpenMPNontemporalClause(ArrayRef<Expr *> VarList,
25225 SourceLocation StartLoc,
25226 SourceLocation LParenLoc,
25227 SourceLocation EndLoc) {
25228 SmallVector<Expr *, 8> Vars;
25229 for (Expr *RefExpr : VarList) {
25230 assert(RefExpr && "NULL expr in OpenMP nontemporal clause.");
25231 SourceLocation ELoc;
25232 SourceRange ERange;
25233 Expr *SimpleRefExpr = RefExpr;
25234 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
25235 if (Res.second)
25236 // It will be analyzed later.
25237 Vars.push_back(Elt: RefExpr);
25238 ValueDecl *D = Res.first;
25239 if (!D)
25240 continue;
25241
25242 // OpenMP 5.0, 2.9.3.1 simd Construct, Restrictions.
25243 // A list-item cannot appear in more than one nontemporal clause.
25244 if (const Expr *PrevRef =
25245 DSAStack->addUniqueNontemporal(D, NewDE: SimpleRefExpr)) {
25246 Diag(Loc: ELoc, DiagID: diag::err_omp_used_in_clause_twice)
25247 << 0 << getOpenMPClauseNameForDiag(C: OMPC_nontemporal) << ERange;
25248 Diag(Loc: PrevRef->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
25249 << getOpenMPClauseNameForDiag(C: OMPC_nontemporal);
25250 continue;
25251 }
25252
25253 Vars.push_back(Elt: RefExpr);
25254 }
25255
25256 if (Vars.empty())
25257 return nullptr;
25258
25259 return OMPNontemporalClause::Create(C: getASTContext(), StartLoc, LParenLoc,
25260 EndLoc, VL: Vars);
25261}
25262
25263StmtResult SemaOpenMP::ActOnOpenMPScopeDirective(ArrayRef<OMPClause *> Clauses,
25264 Stmt *AStmt,
25265 SourceLocation StartLoc,
25266 SourceLocation EndLoc) {
25267 if (!AStmt)
25268 return StmtError();
25269
25270 SemaRef.setFunctionHasBranchProtectedScope();
25271
25272 return OMPScopeDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
25273 AssociatedStmt: AStmt);
25274}
25275
25276OMPClause *SemaOpenMP::ActOnOpenMPInclusiveClause(ArrayRef<Expr *> VarList,
25277 SourceLocation StartLoc,
25278 SourceLocation LParenLoc,
25279 SourceLocation EndLoc) {
25280 SmallVector<Expr *, 8> Vars;
25281 for (Expr *RefExpr : VarList) {
25282 assert(RefExpr && "NULL expr in OpenMP inclusive clause.");
25283 SourceLocation ELoc;
25284 SourceRange ERange;
25285 Expr *SimpleRefExpr = RefExpr;
25286 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange,
25287 /*AllowArraySection=*/true);
25288 if (Res.second)
25289 // It will be analyzed later.
25290 Vars.push_back(Elt: RefExpr);
25291 ValueDecl *D = Res.first;
25292 if (!D)
25293 continue;
25294
25295 const DSAStackTy::DSAVarData DVar =
25296 DSAStack->getTopDSA(D, /*FromParent=*/true);
25297 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions.
25298 // A list item that appears in the inclusive or exclusive clause must appear
25299 // in a reduction clause with the inscan modifier on the enclosing
25300 // worksharing-loop, worksharing-loop SIMD, or simd construct.
25301 if (DVar.CKind != OMPC_reduction || DVar.Modifier != OMPC_REDUCTION_inscan)
25302 Diag(Loc: ELoc, DiagID: diag::err_omp_inclusive_exclusive_not_reduction)
25303 << RefExpr->getSourceRange();
25304
25305 if (DSAStack->getParentDirective() != OMPD_unknown)
25306 DSAStack->markDeclAsUsedInScanDirective(D);
25307 Vars.push_back(Elt: RefExpr);
25308 }
25309
25310 if (Vars.empty())
25311 return nullptr;
25312
25313 return OMPInclusiveClause::Create(C: getASTContext(), StartLoc, LParenLoc,
25314 EndLoc, VL: Vars);
25315}
25316
25317OMPClause *SemaOpenMP::ActOnOpenMPExclusiveClause(ArrayRef<Expr *> VarList,
25318 SourceLocation StartLoc,
25319 SourceLocation LParenLoc,
25320 SourceLocation EndLoc) {
25321 SmallVector<Expr *, 8> Vars;
25322 for (Expr *RefExpr : VarList) {
25323 assert(RefExpr && "NULL expr in OpenMP exclusive clause.");
25324 SourceLocation ELoc;
25325 SourceRange ERange;
25326 Expr *SimpleRefExpr = RefExpr;
25327 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange,
25328 /*AllowArraySection=*/true);
25329 if (Res.second)
25330 // It will be analyzed later.
25331 Vars.push_back(Elt: RefExpr);
25332 ValueDecl *D = Res.first;
25333 if (!D)
25334 continue;
25335
25336 OpenMPDirectiveKind ParentDirective = DSAStack->getParentDirective();
25337 DSAStackTy::DSAVarData DVar;
25338 if (ParentDirective != OMPD_unknown)
25339 DVar = DSAStack->getTopDSA(D, /*FromParent=*/true);
25340 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions.
25341 // A list item that appears in the inclusive or exclusive clause must appear
25342 // in a reduction clause with the inscan modifier on the enclosing
25343 // worksharing-loop, worksharing-loop SIMD, or simd construct.
25344 if (ParentDirective == OMPD_unknown || DVar.CKind != OMPC_reduction ||
25345 DVar.Modifier != OMPC_REDUCTION_inscan) {
25346 Diag(Loc: ELoc, DiagID: diag::err_omp_inclusive_exclusive_not_reduction)
25347 << RefExpr->getSourceRange();
25348 } else {
25349 DSAStack->markDeclAsUsedInScanDirective(D);
25350 }
25351 Vars.push_back(Elt: RefExpr);
25352 }
25353
25354 if (Vars.empty())
25355 return nullptr;
25356
25357 return OMPExclusiveClause::Create(C: getASTContext(), StartLoc, LParenLoc,
25358 EndLoc, VL: Vars);
25359}
25360
25361/// Tries to find omp_alloctrait_t type.
25362static bool findOMPAlloctraitT(Sema &S, SourceLocation Loc, DSAStackTy *Stack) {
25363 QualType OMPAlloctraitT = Stack->getOMPAlloctraitT();
25364 if (!OMPAlloctraitT.isNull())
25365 return true;
25366 IdentifierInfo &II = S.PP.getIdentifierTable().get(Name: "omp_alloctrait_t");
25367 ParsedType PT = S.getTypeName(II, NameLoc: Loc, S: S.getCurScope());
25368 if (!PT.getAsOpaquePtr() || PT.get().isNull()) {
25369 S.Diag(Loc, DiagID: diag::err_omp_implied_type_not_found) << "omp_alloctrait_t";
25370 return false;
25371 }
25372 Stack->setOMPAlloctraitT(PT.get());
25373 return true;
25374}
25375
25376OMPClause *SemaOpenMP::ActOnOpenMPUsesAllocatorClause(
25377 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc,
25378 ArrayRef<UsesAllocatorsData> Data) {
25379 ASTContext &Context = getASTContext();
25380 // OpenMP [2.12.5, target Construct]
25381 // allocator is an identifier of omp_allocator_handle_t type.
25382 if (!findOMPAllocatorHandleT(S&: SemaRef, Loc: StartLoc, DSAStack))
25383 return nullptr;
25384 // OpenMP [2.12.5, target Construct]
25385 // allocator-traits-array is an identifier of const omp_alloctrait_t * type.
25386 if (llvm::any_of(
25387 Range&: Data,
25388 P: [](const UsesAllocatorsData &D) { return D.AllocatorTraits; }) &&
25389 !findOMPAlloctraitT(S&: SemaRef, Loc: StartLoc, DSAStack))
25390 return nullptr;
25391 llvm::SmallPtrSet<CanonicalDeclPtr<Decl>, 4> PredefinedAllocators;
25392 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
25393 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
25394 StringRef Allocator =
25395 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(Val: AllocatorKind);
25396 DeclarationName AllocatorName = &Context.Idents.get(Name: Allocator);
25397 PredefinedAllocators.insert(Ptr: SemaRef.LookupSingleName(
25398 S: SemaRef.TUScope, Name: AllocatorName, Loc: StartLoc, NameKind: Sema::LookupAnyName));
25399 }
25400
25401 SmallVector<OMPUsesAllocatorsClause::Data, 4> NewData;
25402 for (const UsesAllocatorsData &D : Data) {
25403 Expr *AllocatorExpr = nullptr;
25404 // Check allocator expression.
25405 if (D.Allocator->isTypeDependent()) {
25406 AllocatorExpr = D.Allocator;
25407 } else {
25408 // Traits were specified - need to assign new allocator to the specified
25409 // allocator, so it must be an lvalue.
25410 AllocatorExpr = D.Allocator->IgnoreParenImpCasts();
25411 auto *DRE = dyn_cast<DeclRefExpr>(Val: AllocatorExpr);
25412 bool IsPredefinedAllocator = false;
25413 if (DRE) {
25414 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorTy =
25415 getAllocatorKind(S&: SemaRef, DSAStack, Allocator: AllocatorExpr);
25416 IsPredefinedAllocator =
25417 AllocatorTy !=
25418 OMPAllocateDeclAttr::AllocatorTypeTy::OMPUserDefinedMemAlloc;
25419 }
25420 QualType OMPAllocatorHandleT = DSAStack->getOMPAllocatorHandleT();
25421 QualType AllocatorExprType = AllocatorExpr->getType();
25422 bool IsTypeCompatible = IsPredefinedAllocator;
25423 IsTypeCompatible = IsTypeCompatible ||
25424 Context.hasSameUnqualifiedType(T1: AllocatorExprType,
25425 T2: OMPAllocatorHandleT);
25426 IsTypeCompatible =
25427 IsTypeCompatible ||
25428 Context.typesAreCompatible(T1: AllocatorExprType, T2: OMPAllocatorHandleT);
25429 bool IsNonConstantLValue =
25430 !AllocatorExprType.isConstant(Ctx: Context) && AllocatorExpr->isLValue();
25431 if (!DRE || !IsTypeCompatible ||
25432 (!IsPredefinedAllocator && !IsNonConstantLValue)) {
25433 Diag(Loc: D.Allocator->getExprLoc(), DiagID: diag::err_omp_var_expected)
25434 << "omp_allocator_handle_t" << (DRE ? 1 : 0)
25435 << AllocatorExpr->getType() << D.Allocator->getSourceRange();
25436 continue;
25437 }
25438 // OpenMP [2.12.5, target Construct]
25439 // Predefined allocators appearing in a uses_allocators clause cannot have
25440 // traits specified.
25441 if (IsPredefinedAllocator && D.AllocatorTraits) {
25442 Diag(Loc: D.AllocatorTraits->getExprLoc(),
25443 DiagID: diag::err_omp_predefined_allocator_with_traits)
25444 << D.AllocatorTraits->getSourceRange();
25445 Diag(Loc: D.Allocator->getExprLoc(), DiagID: diag::note_omp_predefined_allocator)
25446 << cast<NamedDecl>(Val: DRE->getDecl())->getName()
25447 << D.Allocator->getSourceRange();
25448 continue;
25449 }
25450 // OpenMP [2.12.5, target Construct]
25451 // Non-predefined allocators appearing in a uses_allocators clause must
25452 // have traits specified.
25453 if (getLangOpts().OpenMP < 52) {
25454 if (!IsPredefinedAllocator && !D.AllocatorTraits) {
25455 Diag(Loc: D.Allocator->getExprLoc(),
25456 DiagID: diag::err_omp_nonpredefined_allocator_without_traits);
25457 continue;
25458 }
25459 }
25460 // No allocator traits - just convert it to rvalue.
25461 if (!D.AllocatorTraits)
25462 AllocatorExpr = SemaRef.DefaultLvalueConversion(E: AllocatorExpr).get();
25463 DSAStack->addUsesAllocatorsDecl(
25464 D: DRE->getDecl(),
25465 Kind: IsPredefinedAllocator
25466 ? DSAStackTy::UsesAllocatorsDeclKind::PredefinedAllocator
25467 : DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator);
25468 }
25469 Expr *AllocatorTraitsExpr = nullptr;
25470 if (D.AllocatorTraits) {
25471 if (D.AllocatorTraits->isTypeDependent()) {
25472 AllocatorTraitsExpr = D.AllocatorTraits;
25473 } else {
25474 // OpenMP [2.12.5, target Construct]
25475 // Arrays that contain allocator traits that appear in a uses_allocators
25476 // clause must be constant arrays, have constant values and be defined
25477 // in the same scope as the construct in which the clause appears.
25478 AllocatorTraitsExpr = D.AllocatorTraits->IgnoreParenImpCasts();
25479 // Check that traits expr is a constant array.
25480 QualType TraitTy;
25481 if (const ArrayType *Ty =
25482 AllocatorTraitsExpr->getType()->getAsArrayTypeUnsafe())
25483 if (const auto *ConstArrayTy = dyn_cast<ConstantArrayType>(Val: Ty))
25484 TraitTy = ConstArrayTy->getElementType();
25485 if (TraitTy.isNull() ||
25486 !(Context.hasSameUnqualifiedType(T1: TraitTy,
25487 DSAStack->getOMPAlloctraitT()) ||
25488 Context.typesAreCompatible(T1: TraitTy, DSAStack->getOMPAlloctraitT(),
25489 /*CompareUnqualified=*/true))) {
25490 Diag(Loc: D.AllocatorTraits->getExprLoc(),
25491 DiagID: diag::err_omp_expected_array_alloctraits)
25492 << AllocatorTraitsExpr->getType();
25493 continue;
25494 }
25495 // Do not map by default allocator traits if it is a standalone
25496 // variable.
25497 if (auto *DRE = dyn_cast<DeclRefExpr>(Val: AllocatorTraitsExpr))
25498 DSAStack->addUsesAllocatorsDecl(
25499 D: DRE->getDecl(),
25500 Kind: DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait);
25501 }
25502 }
25503 OMPUsesAllocatorsClause::Data &NewD = NewData.emplace_back();
25504 NewD.Allocator = AllocatorExpr;
25505 NewD.AllocatorTraits = AllocatorTraitsExpr;
25506 NewD.LParenLoc = D.LParenLoc;
25507 NewD.RParenLoc = D.RParenLoc;
25508 }
25509 return OMPUsesAllocatorsClause::Create(C: getASTContext(), StartLoc, LParenLoc,
25510 EndLoc, Data: NewData);
25511}
25512
25513OMPClause *SemaOpenMP::ActOnOpenMPAffinityClause(
25514 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
25515 SourceLocation EndLoc, Expr *Modifier, ArrayRef<Expr *> Locators) {
25516 SmallVector<Expr *, 8> Vars;
25517 for (Expr *RefExpr : Locators) {
25518 assert(RefExpr && "NULL expr in OpenMP affinity clause.");
25519 if (isa<DependentScopeDeclRefExpr>(Val: RefExpr) || RefExpr->isTypeDependent()) {
25520 // It will be analyzed later.
25521 Vars.push_back(Elt: RefExpr);
25522 continue;
25523 }
25524
25525 SourceLocation ELoc = RefExpr->getExprLoc();
25526 Expr *SimpleExpr = RefExpr->IgnoreParenImpCasts();
25527
25528 if (!SimpleExpr->isLValue()) {
25529 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_addressable_lvalue_or_array_item)
25530 << 1 << 0 << RefExpr->getSourceRange();
25531 continue;
25532 }
25533
25534 ExprResult Res;
25535 {
25536 Sema::TentativeAnalysisScope Trap(SemaRef);
25537 Res = SemaRef.CreateBuiltinUnaryOp(OpLoc: ELoc, Opc: UO_AddrOf, InputExpr: SimpleExpr);
25538 }
25539 if (!Res.isUsable() && !isa<ArraySectionExpr>(Val: SimpleExpr) &&
25540 !isa<OMPArrayShapingExpr>(Val: SimpleExpr)) {
25541 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_addressable_lvalue_or_array_item)
25542 << 1 << 0 << RefExpr->getSourceRange();
25543 continue;
25544 }
25545 Vars.push_back(Elt: SimpleExpr);
25546 }
25547
25548 return OMPAffinityClause::Create(C: getASTContext(), StartLoc, LParenLoc,
25549 ColonLoc, EndLoc, Modifier, Locators: Vars);
25550}
25551
25552OMPClause *SemaOpenMP::ActOnOpenMPBindClause(OpenMPBindClauseKind Kind,
25553 SourceLocation KindLoc,
25554 SourceLocation StartLoc,
25555 SourceLocation LParenLoc,
25556 SourceLocation EndLoc) {
25557 if (Kind == OMPC_BIND_unknown) {
25558 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
25559 << getListOfPossibleValues(K: OMPC_bind, /*First=*/0,
25560 /*Last=*/unsigned(OMPC_BIND_unknown))
25561 << getOpenMPClauseNameForDiag(C: OMPC_bind);
25562 return nullptr;
25563 }
25564
25565 return OMPBindClause::Create(C: getASTContext(), K: Kind, KLoc: KindLoc, StartLoc,
25566 LParenLoc, EndLoc);
25567}
25568
25569OMPClause *SemaOpenMP::ActOnOpenMPXDynCGroupMemClause(Expr *Size,
25570 SourceLocation StartLoc,
25571 SourceLocation LParenLoc,
25572 SourceLocation EndLoc) {
25573 Expr *ValExpr = Size;
25574 Stmt *HelperValStmt = nullptr;
25575
25576 // OpenMP [2.5, Restrictions]
25577 // The ompx_dyn_cgroup_mem expression must evaluate to a positive integer
25578 // value.
25579 if (!isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_ompx_dyn_cgroup_mem,
25580 /*StrictlyPositive=*/false))
25581 return nullptr;
25582
25583 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
25584 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
25585 DKind, CKind: OMPC_ompx_dyn_cgroup_mem, OpenMPVersion: getLangOpts().OpenMP);
25586 if (CaptureRegion != OMPD_unknown &&
25587 !SemaRef.CurContext->isDependentContext()) {
25588 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
25589 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
25590 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
25591 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
25592 }
25593
25594 return new (getASTContext()) OMPXDynCGroupMemClause(
25595 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
25596}
25597
25598OMPClause *SemaOpenMP::ActOnOpenMPDynGroupprivateClause(
25599 OpenMPDynGroupprivateClauseModifier M1,
25600 OpenMPDynGroupprivateClauseFallbackModifier M2, Expr *Size,
25601 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation M1Loc,
25602 SourceLocation M2Loc, SourceLocation EndLoc) {
25603
25604 if ((M1Loc.isValid() && M1 == OMPC_DYN_GROUPPRIVATE_unknown) ||
25605 (M2Loc.isValid() && M2 == OMPC_DYN_GROUPPRIVATE_FALLBACK_unknown)) {
25606 std::string Values = getListOfPossibleValues(
25607 K: OMPC_dyn_groupprivate, /*First=*/0, Last: OMPC_DYN_GROUPPRIVATE_unknown);
25608 Diag(Loc: (M1Loc.isValid() && M1 == OMPC_DYN_GROUPPRIVATE_unknown) ? M1Loc
25609 : M2Loc,
25610 DiagID: diag::err_omp_unexpected_clause_value)
25611 << Values << getOpenMPClauseName(C: OMPC_dyn_groupprivate);
25612 return nullptr;
25613 }
25614
25615 Expr *ValExpr = Size;
25616 Stmt *HelperValStmt = nullptr;
25617
25618 // OpenMP [2.5, Restrictions]
25619 // The dyn_groupprivate expression must evaluate to a positive integer
25620 // value.
25621 if (!isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_dyn_groupprivate,
25622 /*StrictlyPositive=*/false))
25623 return nullptr;
25624
25625 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
25626 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
25627 DKind, CKind: OMPC_dyn_groupprivate, OpenMPVersion: getLangOpts().OpenMP);
25628 if (CaptureRegion != OMPD_unknown &&
25629 !SemaRef.CurContext->isDependentContext()) {
25630 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
25631 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
25632 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
25633 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
25634 }
25635
25636 return new (getASTContext()) OMPDynGroupprivateClause(
25637 StartLoc, LParenLoc, EndLoc, ValExpr, HelperValStmt, CaptureRegion, M1,
25638 M1Loc, M2, M2Loc);
25639}
25640
25641OMPClause *SemaOpenMP::ActOnOpenMPDoacrossClause(
25642 OpenMPDoacrossClauseModifier DepType, SourceLocation DepLoc,
25643 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, SourceLocation StartLoc,
25644 SourceLocation LParenLoc, SourceLocation EndLoc) {
25645
25646 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
25647 DepType != OMPC_DOACROSS_source && DepType != OMPC_DOACROSS_sink &&
25648 DepType != OMPC_DOACROSS_sink_omp_cur_iteration &&
25649 DepType != OMPC_DOACROSS_source_omp_cur_iteration) {
25650 Diag(Loc: DepLoc, DiagID: diag::err_omp_unexpected_clause_value)
25651 << "'source' or 'sink'" << getOpenMPClauseNameForDiag(C: OMPC_doacross);
25652 return nullptr;
25653 }
25654
25655 SmallVector<Expr *, 8> Vars;
25656 DSAStackTy::OperatorOffsetTy OpsOffs;
25657 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
25658 DoacrossDataInfoTy VarOffset = ProcessOpenMPDoacrossClauseCommon(
25659 SemaRef,
25660 IsSource: DepType == OMPC_DOACROSS_source ||
25661 DepType == OMPC_DOACROSS_source_omp_cur_iteration ||
25662 DepType == OMPC_DOACROSS_sink_omp_cur_iteration,
25663 VarList, DSAStack, EndLoc);
25664 Vars = VarOffset.Vars;
25665 OpsOffs = VarOffset.OpsOffs;
25666 TotalDepCount = VarOffset.TotalDepCount;
25667 auto *C = OMPDoacrossClause::Create(C: getASTContext(), StartLoc, LParenLoc,
25668 EndLoc, DepType, DepLoc, ColonLoc, VL: Vars,
25669 NumLoops: TotalDepCount.getZExtValue());
25670 if (DSAStack->isParentOrderedRegion())
25671 DSAStack->addDoacrossDependClause(C, OpsOffs);
25672 return C;
25673}
25674
25675OMPClause *SemaOpenMP::ActOnOpenMPXAttributeClause(ArrayRef<const Attr *> Attrs,
25676 SourceLocation StartLoc,
25677 SourceLocation LParenLoc,
25678 SourceLocation EndLoc) {
25679 return new (getASTContext())
25680 OMPXAttributeClause(Attrs, StartLoc, LParenLoc, EndLoc);
25681}
25682
25683OMPClause *SemaOpenMP::ActOnOpenMPXBareClause(SourceLocation StartLoc,
25684 SourceLocation EndLoc) {
25685 return new (getASTContext()) OMPXBareClause(StartLoc, EndLoc);
25686}
25687
25688OMPClause *SemaOpenMP::ActOnOpenMPHoldsClause(Expr *E, SourceLocation StartLoc,
25689 SourceLocation LParenLoc,
25690 SourceLocation EndLoc) {
25691 return new (getASTContext()) OMPHoldsClause(E, StartLoc, LParenLoc, EndLoc);
25692}
25693
25694OMPClause *SemaOpenMP::ActOnOpenMPDirectivePresenceClause(
25695 OpenMPClauseKind CK, llvm::ArrayRef<OpenMPDirectiveKind> DKVec,
25696 SourceLocation Loc, SourceLocation LLoc, SourceLocation RLoc) {
25697 switch (CK) {
25698 case OMPC_absent:
25699 return OMPAbsentClause::Create(C: getASTContext(), DKVec, Loc, LLoc, RLoc);
25700 case OMPC_contains:
25701 return OMPContainsClause::Create(C: getASTContext(), DKVec, Loc, LLoc, RLoc);
25702 default:
25703 llvm_unreachable("Unexpected OpenMP clause");
25704 }
25705}
25706
25707OMPClause *SemaOpenMP::ActOnOpenMPNullaryAssumptionClause(OpenMPClauseKind CK,
25708 SourceLocation Loc,
25709 SourceLocation RLoc) {
25710 switch (CK) {
25711 case OMPC_no_openmp:
25712 return new (getASTContext()) OMPNoOpenMPClause(Loc, RLoc);
25713 case OMPC_no_openmp_routines:
25714 return new (getASTContext()) OMPNoOpenMPRoutinesClause(Loc, RLoc);
25715 case OMPC_no_parallelism:
25716 return new (getASTContext()) OMPNoParallelismClause(Loc, RLoc);
25717 case OMPC_no_openmp_constructs:
25718 return new (getASTContext()) OMPNoOpenMPConstructsClause(Loc, RLoc);
25719 default:
25720 llvm_unreachable("Unexpected OpenMP clause");
25721 }
25722}
25723
25724ExprResult SemaOpenMP::ActOnOMPArraySectionExpr(
25725 Expr *Base, SourceLocation LBLoc, Expr *LowerBound,
25726 SourceLocation ColonLocFirst, SourceLocation ColonLocSecond, Expr *Length,
25727 Expr *Stride, SourceLocation RBLoc) {
25728 ASTContext &Context = getASTContext();
25729 if (Base->hasPlaceholderType() &&
25730 !Base->hasPlaceholderType(K: BuiltinType::ArraySection)) {
25731 ExprResult Result = SemaRef.CheckPlaceholderExpr(E: Base);
25732 if (Result.isInvalid())
25733 return ExprError();
25734 Base = Result.get();
25735 }
25736 if (LowerBound && LowerBound->getType()->isNonOverloadPlaceholderType()) {
25737 ExprResult Result = SemaRef.CheckPlaceholderExpr(E: LowerBound);
25738 if (Result.isInvalid())
25739 return ExprError();
25740 Result = SemaRef.DefaultLvalueConversion(E: Result.get());
25741 if (Result.isInvalid())
25742 return ExprError();
25743 LowerBound = Result.get();
25744 }
25745 if (Length && Length->getType()->isNonOverloadPlaceholderType()) {
25746 ExprResult Result = SemaRef.CheckPlaceholderExpr(E: Length);
25747 if (Result.isInvalid())
25748 return ExprError();
25749 Result = SemaRef.DefaultLvalueConversion(E: Result.get());
25750 if (Result.isInvalid())
25751 return ExprError();
25752 Length = Result.get();
25753 }
25754 if (Stride && Stride->getType()->isNonOverloadPlaceholderType()) {
25755 ExprResult Result = SemaRef.CheckPlaceholderExpr(E: Stride);
25756 if (Result.isInvalid())
25757 return ExprError();
25758 Result = SemaRef.DefaultLvalueConversion(E: Result.get());
25759 if (Result.isInvalid())
25760 return ExprError();
25761 Stride = Result.get();
25762 }
25763
25764 // Build an unanalyzed expression if either operand is type-dependent.
25765 if (Base->isTypeDependent() ||
25766 (LowerBound &&
25767 (LowerBound->isTypeDependent() || LowerBound->isValueDependent())) ||
25768 (Length && (Length->isTypeDependent() || Length->isValueDependent())) ||
25769 (Stride && (Stride->isTypeDependent() || Stride->isValueDependent()))) {
25770 return new (Context) ArraySectionExpr(
25771 Base, LowerBound, Length, Stride, Context.DependentTy, VK_LValue,
25772 OK_Ordinary, ColonLocFirst, ColonLocSecond, RBLoc);
25773 }
25774
25775 // Perform default conversions.
25776 QualType OriginalTy = ArraySectionExpr::getBaseOriginalType(Base);
25777 QualType ResultTy;
25778 if (OriginalTy->isAnyPointerType()) {
25779 ResultTy = OriginalTy->getPointeeType();
25780 } else if (OriginalTy->isArrayType()) {
25781 ResultTy = OriginalTy->getAsArrayTypeUnsafe()->getElementType();
25782 } else {
25783 return ExprError(
25784 Diag(Loc: Base->getExprLoc(), DiagID: diag::err_omp_typecheck_section_value)
25785 << Base->getSourceRange());
25786 }
25787 // C99 6.5.2.1p1
25788 if (LowerBound) {
25789 auto Res = PerformOpenMPImplicitIntegerConversion(Loc: LowerBound->getExprLoc(),
25790 Op: LowerBound);
25791 if (Res.isInvalid())
25792 return ExprError(Diag(Loc: LowerBound->getExprLoc(),
25793 DiagID: diag::err_omp_typecheck_section_not_integer)
25794 << 0 << LowerBound->getSourceRange());
25795 LowerBound = Res.get();
25796
25797 if (LowerBound->getType()->isSpecificBuiltinType(K: BuiltinType::Char_S) ||
25798 LowerBound->getType()->isSpecificBuiltinType(K: BuiltinType::Char_U))
25799 Diag(Loc: LowerBound->getExprLoc(), DiagID: diag::warn_omp_section_is_char)
25800 << 0 << LowerBound->getSourceRange();
25801 }
25802 if (Length) {
25803 auto Res =
25804 PerformOpenMPImplicitIntegerConversion(Loc: Length->getExprLoc(), Op: Length);
25805 if (Res.isInvalid())
25806 return ExprError(Diag(Loc: Length->getExprLoc(),
25807 DiagID: diag::err_omp_typecheck_section_not_integer)
25808 << 1 << Length->getSourceRange());
25809 Length = Res.get();
25810
25811 if (Length->getType()->isSpecificBuiltinType(K: BuiltinType::Char_S) ||
25812 Length->getType()->isSpecificBuiltinType(K: BuiltinType::Char_U))
25813 Diag(Loc: Length->getExprLoc(), DiagID: diag::warn_omp_section_is_char)
25814 << 1 << Length->getSourceRange();
25815 }
25816 if (Stride) {
25817 ExprResult Res =
25818 PerformOpenMPImplicitIntegerConversion(Loc: Stride->getExprLoc(), Op: Stride);
25819 if (Res.isInvalid())
25820 return ExprError(Diag(Loc: Stride->getExprLoc(),
25821 DiagID: diag::err_omp_typecheck_section_not_integer)
25822 << 1 << Stride->getSourceRange());
25823 Stride = Res.get();
25824
25825 if (Stride->getType()->isSpecificBuiltinType(K: BuiltinType::Char_S) ||
25826 Stride->getType()->isSpecificBuiltinType(K: BuiltinType::Char_U))
25827 Diag(Loc: Stride->getExprLoc(), DiagID: diag::warn_omp_section_is_char)
25828 << 1 << Stride->getSourceRange();
25829 }
25830
25831 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly,
25832 // C++ [expr.sub]p1: The type "T" shall be a completely-defined object
25833 // type. Note that functions are not objects, and that (in C99 parlance)
25834 // incomplete types are not object types.
25835 if (ResultTy->isFunctionType()) {
25836 Diag(Loc: Base->getExprLoc(), DiagID: diag::err_omp_section_function_type)
25837 << ResultTy << Base->getSourceRange();
25838 return ExprError();
25839 }
25840
25841 if (SemaRef.RequireCompleteType(Loc: Base->getExprLoc(), T: ResultTy,
25842 DiagID: diag::err_omp_section_incomplete_type, Args: Base))
25843 return ExprError();
25844
25845 if (LowerBound && !OriginalTy->isAnyPointerType()) {
25846 Expr::EvalResult Result;
25847 if (LowerBound->EvaluateAsInt(Result, Ctx: Context)) {
25848 // OpenMP 5.0, [2.1.5 Array Sections]
25849 // The array section must be a subset of the original array.
25850 llvm::APSInt LowerBoundValue = Result.Val.getInt();
25851 if (LowerBoundValue.isNegative()) {
25852 Diag(Loc: LowerBound->getExprLoc(),
25853 DiagID: diag::err_omp_section_not_subset_of_array)
25854 << LowerBound->getSourceRange();
25855 return ExprError();
25856 }
25857 }
25858 }
25859
25860 if (Length) {
25861 Expr::EvalResult Result;
25862 if (Length->EvaluateAsInt(Result, Ctx: Context)) {
25863 // OpenMP 5.0, [2.1.5 Array Sections]
25864 // The length must evaluate to non-negative integers.
25865 llvm::APSInt LengthValue = Result.Val.getInt();
25866 if (LengthValue.isNegative()) {
25867 Diag(Loc: Length->getExprLoc(), DiagID: diag::err_omp_section_length_negative)
25868 << toString(I: LengthValue, /*Radix=*/10, /*Signed=*/true)
25869 << Length->getSourceRange();
25870 return ExprError();
25871 }
25872 }
25873 } else if (SemaRef.getLangOpts().OpenMP < 60 && ColonLocFirst.isValid() &&
25874 (OriginalTy.isNull() || (!OriginalTy->isConstantArrayType() &&
25875 !OriginalTy->isVariableArrayType()))) {
25876 // OpenMP 5.0, [2.1.5 Array Sections]
25877 // When the size of the array dimension is not known, the length must be
25878 // specified explicitly.
25879 Diag(Loc: ColonLocFirst, DiagID: diag::err_omp_section_length_undefined)
25880 << (!OriginalTy.isNull() && OriginalTy->isArrayType());
25881 return ExprError();
25882 }
25883
25884 if (Stride) {
25885 Expr::EvalResult Result;
25886 if (Stride->EvaluateAsInt(Result, Ctx: Context)) {
25887 // OpenMP 5.0, [2.1.5 Array Sections]
25888 // The stride must evaluate to a positive integer.
25889 llvm::APSInt StrideValue = Result.Val.getInt();
25890 if (!StrideValue.isStrictlyPositive()) {
25891 Diag(Loc: Stride->getExprLoc(), DiagID: diag::err_omp_section_stride_non_positive)
25892 << toString(I: StrideValue, /*Radix=*/10, /*Signed=*/true)
25893 << Stride->getSourceRange();
25894 return ExprError();
25895 }
25896 }
25897 }
25898
25899 if (!Base->hasPlaceholderType(K: BuiltinType::ArraySection)) {
25900 ExprResult Result = SemaRef.DefaultFunctionArrayLvalueConversion(E: Base);
25901 if (Result.isInvalid())
25902 return ExprError();
25903 Base = Result.get();
25904 }
25905 return new (Context) ArraySectionExpr(
25906 Base, LowerBound, Length, Stride, Context.ArraySectionTy, VK_LValue,
25907 OK_Ordinary, ColonLocFirst, ColonLocSecond, RBLoc);
25908}
25909
25910ExprResult SemaOpenMP::ActOnOMPArrayShapingExpr(
25911 Expr *Base, SourceLocation LParenLoc, SourceLocation RParenLoc,
25912 ArrayRef<Expr *> Dims, ArrayRef<SourceRange> Brackets) {
25913 ASTContext &Context = getASTContext();
25914 if (Base->hasPlaceholderType()) {
25915 ExprResult Result = SemaRef.CheckPlaceholderExpr(E: Base);
25916 if (Result.isInvalid())
25917 return ExprError();
25918 Result = SemaRef.DefaultLvalueConversion(E: Result.get());
25919 if (Result.isInvalid())
25920 return ExprError();
25921 Base = Result.get();
25922 }
25923 QualType BaseTy = Base->getType();
25924 // Delay analysis of the types/expressions if instantiation/specialization is
25925 // required.
25926 if (!BaseTy->isPointerType() && Base->isTypeDependent())
25927 return OMPArrayShapingExpr::Create(Context, T: Context.DependentTy, Op: Base,
25928 L: LParenLoc, R: RParenLoc, Dims, BracketRanges: Brackets);
25929 if (!BaseTy->isPointerType() ||
25930 (!Base->isTypeDependent() &&
25931 BaseTy->getPointeeType()->isIncompleteType()))
25932 return ExprError(Diag(Loc: Base->getExprLoc(),
25933 DiagID: diag::err_omp_non_pointer_type_array_shaping_base)
25934 << Base->getSourceRange());
25935
25936 SmallVector<Expr *, 4> NewDims;
25937 bool ErrorFound = false;
25938 for (Expr *Dim : Dims) {
25939 if (Dim->hasPlaceholderType()) {
25940 ExprResult Result = SemaRef.CheckPlaceholderExpr(E: Dim);
25941 if (Result.isInvalid()) {
25942 ErrorFound = true;
25943 continue;
25944 }
25945 Result = SemaRef.DefaultLvalueConversion(E: Result.get());
25946 if (Result.isInvalid()) {
25947 ErrorFound = true;
25948 continue;
25949 }
25950 Dim = Result.get();
25951 }
25952 if (!Dim->isTypeDependent()) {
25953 ExprResult Result =
25954 PerformOpenMPImplicitIntegerConversion(Loc: Dim->getExprLoc(), Op: Dim);
25955 if (Result.isInvalid()) {
25956 ErrorFound = true;
25957 Diag(Loc: Dim->getExprLoc(), DiagID: diag::err_omp_typecheck_shaping_not_integer)
25958 << Dim->getSourceRange();
25959 continue;
25960 }
25961 Dim = Result.get();
25962 Expr::EvalResult EvResult;
25963 if (!Dim->isValueDependent() && Dim->EvaluateAsInt(Result&: EvResult, Ctx: Context)) {
25964 // OpenMP 5.0, [2.1.4 Array Shaping]
25965 // Each si is an integral type expression that must evaluate to a
25966 // positive integer.
25967 llvm::APSInt Value = EvResult.Val.getInt();
25968 if (!Value.isStrictlyPositive()) {
25969 Diag(Loc: Dim->getExprLoc(), DiagID: diag::err_omp_shaping_dimension_not_positive)
25970 << toString(I: Value, /*Radix=*/10, /*Signed=*/true)
25971 << Dim->getSourceRange();
25972 ErrorFound = true;
25973 continue;
25974 }
25975 }
25976 }
25977 NewDims.push_back(Elt: Dim);
25978 }
25979 if (ErrorFound)
25980 return ExprError();
25981 return OMPArrayShapingExpr::Create(Context, T: Context.OMPArrayShapingTy, Op: Base,
25982 L: LParenLoc, R: RParenLoc, Dims: NewDims, BracketRanges: Brackets);
25983}
25984
25985ExprResult SemaOpenMP::ActOnOMPIteratorExpr(Scope *S,
25986 SourceLocation IteratorKwLoc,
25987 SourceLocation LLoc,
25988 SourceLocation RLoc,
25989 ArrayRef<OMPIteratorData> Data) {
25990 ASTContext &Context = getASTContext();
25991 SmallVector<OMPIteratorExpr::IteratorDefinition, 4> ID;
25992 bool IsCorrect = true;
25993 for (const OMPIteratorData &D : Data) {
25994 TypeSourceInfo *TInfo = nullptr;
25995 SourceLocation StartLoc;
25996 QualType DeclTy;
25997 if (!D.Type.getAsOpaquePtr()) {
25998 // OpenMP 5.0, 2.1.6 Iterators
25999 // In an iterator-specifier, if the iterator-type is not specified then
26000 // the type of that iterator is of int type.
26001 DeclTy = Context.IntTy;
26002 StartLoc = D.DeclIdentLoc;
26003 } else {
26004 DeclTy = Sema::GetTypeFromParser(Ty: D.Type, TInfo: &TInfo);
26005 StartLoc = TInfo->getTypeLoc().getBeginLoc();
26006 }
26007
26008 bool IsDeclTyDependent = DeclTy->isDependentType() ||
26009 DeclTy->containsUnexpandedParameterPack() ||
26010 DeclTy->isInstantiationDependentType();
26011 if (!IsDeclTyDependent) {
26012 if (!DeclTy->isIntegralType(Ctx: Context) && !DeclTy->isAnyPointerType()) {
26013 // OpenMP 5.0, 2.1.6 Iterators, Restrictions, C/C++
26014 // The iterator-type must be an integral or pointer type.
26015 Diag(Loc: StartLoc, DiagID: diag::err_omp_iterator_not_integral_or_pointer)
26016 << DeclTy;
26017 IsCorrect = false;
26018 continue;
26019 }
26020 if (DeclTy.isConstant(Ctx: Context)) {
26021 // OpenMP 5.0, 2.1.6 Iterators, Restrictions, C/C++
26022 // The iterator-type must not be const qualified.
26023 Diag(Loc: StartLoc, DiagID: diag::err_omp_iterator_not_integral_or_pointer)
26024 << DeclTy;
26025 IsCorrect = false;
26026 continue;
26027 }
26028 }
26029
26030 // Iterator declaration.
26031 assert(D.DeclIdent && "Identifier expected.");
26032 // Always try to create iterator declarator to avoid extra error messages
26033 // about unknown declarations use.
26034 auto *VD =
26035 VarDecl::Create(C&: Context, DC: SemaRef.CurContext, StartLoc, IdLoc: D.DeclIdentLoc,
26036 Id: D.DeclIdent, T: DeclTy, TInfo, S: SC_None);
26037 VD->setImplicit();
26038 if (S) {
26039 // Check for conflicting previous declaration.
26040 DeclarationNameInfo NameInfo(VD->getDeclName(), D.DeclIdentLoc);
26041 LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
26042 RedeclarationKind::ForVisibleRedeclaration);
26043 Previous.suppressDiagnostics();
26044 SemaRef.LookupName(R&: Previous, S);
26045
26046 SemaRef.FilterLookupForScope(R&: Previous, Ctx: SemaRef.CurContext, S,
26047 /*ConsiderLinkage=*/false,
26048 /*AllowInlineNamespace=*/false);
26049 if (!Previous.empty()) {
26050 NamedDecl *Old = Previous.getRepresentativeDecl();
26051 Diag(Loc: D.DeclIdentLoc, DiagID: diag::err_redefinition) << VD->getDeclName();
26052 Diag(Loc: Old->getLocation(), DiagID: diag::note_previous_definition);
26053 } else {
26054 SemaRef.PushOnScopeChains(D: VD, S);
26055 }
26056 } else {
26057 SemaRef.CurContext->addDecl(D: VD);
26058 }
26059
26060 /// Act on the iterator variable declaration.
26061 ActOnOpenMPIteratorVarDecl(VD);
26062
26063 Expr *Begin = D.Range.Begin;
26064 if (!IsDeclTyDependent && Begin && !Begin->isTypeDependent()) {
26065 ExprResult BeginRes = SemaRef.PerformImplicitConversion(
26066 From: Begin, ToType: DeclTy, Action: AssignmentAction::Converting);
26067 Begin = BeginRes.get();
26068 }
26069 Expr *End = D.Range.End;
26070 if (!IsDeclTyDependent && End && !End->isTypeDependent()) {
26071 ExprResult EndRes = SemaRef.PerformImplicitConversion(
26072 From: End, ToType: DeclTy, Action: AssignmentAction::Converting);
26073 End = EndRes.get();
26074 }
26075 Expr *Step = D.Range.Step;
26076 if (!IsDeclTyDependent && Step && !Step->isTypeDependent()) {
26077 if (!Step->getType()->isIntegralType(Ctx: Context)) {
26078 Diag(Loc: Step->getExprLoc(), DiagID: diag::err_omp_iterator_step_not_integral)
26079 << Step << Step->getSourceRange();
26080 IsCorrect = false;
26081 continue;
26082 }
26083 std::optional<llvm::APSInt> Result =
26084 Step->getIntegerConstantExpr(Ctx: Context);
26085 // OpenMP 5.0, 2.1.6 Iterators, Restrictions
26086 // If the step expression of a range-specification equals zero, the
26087 // behavior is unspecified.
26088 if (Result && Result->isZero()) {
26089 Diag(Loc: Step->getExprLoc(), DiagID: diag::err_omp_iterator_step_constant_zero)
26090 << Step << Step->getSourceRange();
26091 IsCorrect = false;
26092 continue;
26093 }
26094 }
26095 if (!Begin || !End || !IsCorrect) {
26096 IsCorrect = false;
26097 continue;
26098 }
26099 OMPIteratorExpr::IteratorDefinition &IDElem = ID.emplace_back();
26100 IDElem.IteratorDecl = VD;
26101 IDElem.AssignmentLoc = D.AssignLoc;
26102 IDElem.Range.Begin = Begin;
26103 IDElem.Range.End = End;
26104 IDElem.Range.Step = Step;
26105 IDElem.ColonLoc = D.ColonLoc;
26106 IDElem.SecondColonLoc = D.SecColonLoc;
26107 }
26108 if (!IsCorrect) {
26109 // Invalidate all created iterator declarations if error is found.
26110 for (const OMPIteratorExpr::IteratorDefinition &D : ID) {
26111 if (Decl *ID = D.IteratorDecl)
26112 ID->setInvalidDecl();
26113 }
26114 return ExprError();
26115 }
26116 SmallVector<OMPIteratorHelperData, 4> Helpers;
26117 if (!SemaRef.CurContext->isDependentContext()) {
26118 // Build number of ityeration for each iteration range.
26119 // Ni = ((Stepi > 0) ? ((Endi + Stepi -1 - Begini)/Stepi) :
26120 // ((Begini-Stepi-1-Endi) / -Stepi);
26121 for (OMPIteratorExpr::IteratorDefinition &D : ID) {
26122 // (Endi - Begini)
26123 ExprResult Res = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Sub,
26124 LHSExpr: D.Range.End, RHSExpr: D.Range.Begin);
26125 if (!Res.isUsable()) {
26126 IsCorrect = false;
26127 continue;
26128 }
26129 ExprResult St, St1;
26130 if (D.Range.Step) {
26131 St = D.Range.Step;
26132 // (Endi - Begini) + Stepi
26133 Res = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Add, LHSExpr: Res.get(),
26134 RHSExpr: St.get());
26135 if (!Res.isUsable()) {
26136 IsCorrect = false;
26137 continue;
26138 }
26139 // (Endi - Begini) + Stepi - 1
26140 Res = SemaRef.CreateBuiltinBinOp(
26141 OpLoc: D.AssignmentLoc, Opc: BO_Sub, LHSExpr: Res.get(),
26142 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: D.AssignmentLoc, Val: 1).get());
26143 if (!Res.isUsable()) {
26144 IsCorrect = false;
26145 continue;
26146 }
26147 // ((Endi - Begini) + Stepi - 1) / Stepi
26148 Res = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Div, LHSExpr: Res.get(),
26149 RHSExpr: St.get());
26150 if (!Res.isUsable()) {
26151 IsCorrect = false;
26152 continue;
26153 }
26154 St1 = SemaRef.CreateBuiltinUnaryOp(OpLoc: D.AssignmentLoc, Opc: UO_Minus,
26155 InputExpr: D.Range.Step);
26156 // (Begini - Endi)
26157 ExprResult Res1 = SemaRef.CreateBuiltinBinOp(
26158 OpLoc: D.AssignmentLoc, Opc: BO_Sub, LHSExpr: D.Range.Begin, RHSExpr: D.Range.End);
26159 if (!Res1.isUsable()) {
26160 IsCorrect = false;
26161 continue;
26162 }
26163 // (Begini - Endi) - Stepi
26164 Res1 = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Add, LHSExpr: Res1.get(),
26165 RHSExpr: St1.get());
26166 if (!Res1.isUsable()) {
26167 IsCorrect = false;
26168 continue;
26169 }
26170 // (Begini - Endi) - Stepi - 1
26171 Res1 = SemaRef.CreateBuiltinBinOp(
26172 OpLoc: D.AssignmentLoc, Opc: BO_Sub, LHSExpr: Res1.get(),
26173 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: D.AssignmentLoc, Val: 1).get());
26174 if (!Res1.isUsable()) {
26175 IsCorrect = false;
26176 continue;
26177 }
26178 // ((Begini - Endi) - Stepi - 1) / (-Stepi)
26179 Res1 = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Div, LHSExpr: Res1.get(),
26180 RHSExpr: St1.get());
26181 if (!Res1.isUsable()) {
26182 IsCorrect = false;
26183 continue;
26184 }
26185 // Stepi > 0.
26186 ExprResult CmpRes = SemaRef.CreateBuiltinBinOp(
26187 OpLoc: D.AssignmentLoc, Opc: BO_GT, LHSExpr: D.Range.Step,
26188 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: D.AssignmentLoc, Val: 0).get());
26189 if (!CmpRes.isUsable()) {
26190 IsCorrect = false;
26191 continue;
26192 }
26193 Res = SemaRef.ActOnConditionalOp(QuestionLoc: D.AssignmentLoc, ColonLoc: D.AssignmentLoc,
26194 CondExpr: CmpRes.get(), LHSExpr: Res.get(), RHSExpr: Res1.get());
26195 if (!Res.isUsable()) {
26196 IsCorrect = false;
26197 continue;
26198 }
26199 }
26200 Res = SemaRef.ActOnFinishFullExpr(Expr: Res.get(), /*DiscardedValue=*/false);
26201 if (!Res.isUsable()) {
26202 IsCorrect = false;
26203 continue;
26204 }
26205
26206 // Build counter update.
26207 // Build counter.
26208 auto *CounterVD = VarDecl::Create(C&: Context, DC: SemaRef.CurContext,
26209 StartLoc: D.IteratorDecl->getBeginLoc(),
26210 IdLoc: D.IteratorDecl->getBeginLoc(), Id: nullptr,
26211 T: Res.get()->getType(), TInfo: nullptr, S: SC_None);
26212 CounterVD->setImplicit();
26213 ExprResult RefRes =
26214 SemaRef.BuildDeclRefExpr(D: CounterVD, Ty: CounterVD->getType(), VK: VK_LValue,
26215 Loc: D.IteratorDecl->getBeginLoc());
26216 // Build counter update.
26217 // I = Begini + counter * Stepi;
26218 ExprResult UpdateRes;
26219 if (D.Range.Step) {
26220 UpdateRes = SemaRef.CreateBuiltinBinOp(
26221 OpLoc: D.AssignmentLoc, Opc: BO_Mul,
26222 LHSExpr: SemaRef.DefaultLvalueConversion(E: RefRes.get()).get(), RHSExpr: St.get());
26223 } else {
26224 UpdateRes = SemaRef.DefaultLvalueConversion(E: RefRes.get());
26225 }
26226 if (!UpdateRes.isUsable()) {
26227 IsCorrect = false;
26228 continue;
26229 }
26230 UpdateRes = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Add,
26231 LHSExpr: D.Range.Begin, RHSExpr: UpdateRes.get());
26232 if (!UpdateRes.isUsable()) {
26233 IsCorrect = false;
26234 continue;
26235 }
26236 ExprResult VDRes =
26237 SemaRef.BuildDeclRefExpr(D: cast<VarDecl>(Val: D.IteratorDecl),
26238 Ty: cast<VarDecl>(Val: D.IteratorDecl)->getType(),
26239 VK: VK_LValue, Loc: D.IteratorDecl->getBeginLoc());
26240 UpdateRes = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Assign,
26241 LHSExpr: VDRes.get(), RHSExpr: UpdateRes.get());
26242 if (!UpdateRes.isUsable()) {
26243 IsCorrect = false;
26244 continue;
26245 }
26246 UpdateRes =
26247 SemaRef.ActOnFinishFullExpr(Expr: UpdateRes.get(), /*DiscardedValue=*/true);
26248 if (!UpdateRes.isUsable()) {
26249 IsCorrect = false;
26250 continue;
26251 }
26252 ExprResult CounterUpdateRes = SemaRef.CreateBuiltinUnaryOp(
26253 OpLoc: D.AssignmentLoc, Opc: UO_PreInc, InputExpr: RefRes.get());
26254 if (!CounterUpdateRes.isUsable()) {
26255 IsCorrect = false;
26256 continue;
26257 }
26258 CounterUpdateRes = SemaRef.ActOnFinishFullExpr(Expr: CounterUpdateRes.get(),
26259 /*DiscardedValue=*/true);
26260 if (!CounterUpdateRes.isUsable()) {
26261 IsCorrect = false;
26262 continue;
26263 }
26264 OMPIteratorHelperData &HD = Helpers.emplace_back();
26265 HD.CounterVD = CounterVD;
26266 HD.Upper = Res.get();
26267 HD.Update = UpdateRes.get();
26268 HD.CounterUpdate = CounterUpdateRes.get();
26269 }
26270 } else {
26271 Helpers.assign(NumElts: ID.size(), Elt: {});
26272 }
26273 if (!IsCorrect) {
26274 // Invalidate all created iterator declarations if error is found.
26275 for (const OMPIteratorExpr::IteratorDefinition &D : ID) {
26276 if (Decl *ID = D.IteratorDecl)
26277 ID->setInvalidDecl();
26278 }
26279 return ExprError();
26280 }
26281 return OMPIteratorExpr::Create(Context, T: Context.OMPIteratorTy, IteratorKwLoc,
26282 L: LLoc, R: RLoc, Data: ID, Helpers);
26283}
26284
26285/// Check if \p AssumptionStr is a known assumption and warn if not.
26286static void checkOMPAssumeAttr(Sema &S, SourceLocation Loc,
26287 StringRef AssumptionStr) {
26288 if (llvm::getKnownAssumptionStrings().count(Key: AssumptionStr))
26289 return;
26290
26291 unsigned BestEditDistance = 3;
26292 StringRef Suggestion;
26293 for (const auto &KnownAssumptionIt : llvm::getKnownAssumptionStrings()) {
26294 unsigned EditDistance =
26295 AssumptionStr.edit_distance(Other: KnownAssumptionIt.getKey());
26296 if (EditDistance < BestEditDistance) {
26297 Suggestion = KnownAssumptionIt.getKey();
26298 BestEditDistance = EditDistance;
26299 }
26300 }
26301
26302 if (!Suggestion.empty())
26303 S.Diag(Loc, DiagID: diag::warn_omp_assume_attribute_string_unknown_suggested)
26304 << AssumptionStr << Suggestion;
26305 else
26306 S.Diag(Loc, DiagID: diag::warn_omp_assume_attribute_string_unknown)
26307 << AssumptionStr;
26308}
26309
26310void SemaOpenMP::handleOMPAssumeAttr(Decl *D, const ParsedAttr &AL) {
26311 // Handle the case where the attribute has a text message.
26312 StringRef Str;
26313 SourceLocation AttrStrLoc;
26314 if (!SemaRef.checkStringLiteralArgumentAttr(Attr: AL, ArgNum: 0, Str, ArgLocation: &AttrStrLoc))
26315 return;
26316
26317 checkOMPAssumeAttr(S&: SemaRef, Loc: AttrStrLoc, AssumptionStr: Str);
26318
26319 D->addAttr(A: ::new (getASTContext()) OMPAssumeAttr(getASTContext(), AL, Str));
26320}
26321
26322SemaOpenMP::SemaOpenMP(Sema &S)
26323 : SemaBase(S), VarDataSharingAttributesStack(nullptr) {}
26324