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/Attr.h"
21#include "clang/AST/CXXInheritance.h"
22#include "clang/AST/Decl.h"
23#include "clang/AST/DeclCXX.h"
24#include "clang/AST/DeclOpenMP.h"
25#include "clang/AST/DynamicRecursiveASTVisitor.h"
26#include "clang/AST/OpenMPClause.h"
27#include "clang/AST/RecursiveASTVisitor.h"
28#include "clang/AST/Stmt.h"
29#include "clang/AST/StmtCXX.h"
30#include "clang/AST/StmtOpenMP.h"
31#include "clang/AST/StmtVisitor.h"
32#include "clang/Basic/DiagnosticSema.h"
33#include "clang/Basic/OpenMPKinds.h"
34#include "clang/Basic/PartialDiagnostic.h"
35#include "clang/Basic/TargetInfo.h"
36#include "clang/Sema/EnterExpressionEvaluationContext.h"
37#include "clang/Sema/Initialization.h"
38#include "clang/Sema/Lookup.h"
39#include "clang/Sema/ParsedAttr.h"
40#include "clang/Sema/Scope.h"
41#include "clang/Sema/ScopeInfo.h"
42#include "clang/Sema/Sema.h"
43#include "llvm/ADT/IndexedMap.h"
44#include "llvm/ADT/PointerEmbeddedInt.h"
45#include "llvm/ADT/STLExtras.h"
46#include "llvm/ADT/Sequence.h"
47#include "llvm/ADT/SetVector.h"
48#include "llvm/ADT/SmallSet.h"
49#include "llvm/ADT/StringExtras.h"
50#include "llvm/Frontend/OpenMP/OMPAssume.h"
51#include "llvm/Frontend/OpenMP/OMPConstants.h"
52#include "llvm/Frontend/OpenMP/OMPVersion.h"
53#include "llvm/IR/Assumptions.h"
54#include <limits>
55#include <optional>
56
57using namespace clang;
58using namespace llvm::omp;
59
60//===----------------------------------------------------------------------===//
61// Stack of data-sharing attributes for variables
62//===----------------------------------------------------------------------===//
63
64static const Expr *checkMapClauseExpressionBase(
65 Sema &SemaRef, Expr *E,
66 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
67 OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, bool NoDiagnose);
68
69static std::string getOpenMPClauseNameForDiag(OpenMPClauseKind C);
70
71namespace {
72/// Default data sharing attributes, which can be applied to directive.
73enum DefaultDataSharingAttributes {
74 DSA_unspecified = 0, /// Data sharing attribute not specified.
75 DSA_none = 1 << 0, /// Default data sharing attribute 'none'.
76 DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'.
77 DSA_private = 1 << 2, /// Default data sharing attribute 'private'.
78 DSA_firstprivate = 1 << 3, /// Default data sharing attribute 'firstprivate'.
79};
80
81/// Variable Category attributes to restrict the modifier of the
82/// default clause (DefaultDataSharingAttributes)
83/// Not mentioning any Variable category attribute indicates
84/// the modifier (DefaultDataSharingAttributes) is for all variables.
85enum DefaultDataSharingVCAttributes {
86 DSA_VC_all = 0, /// for all variables.
87 DSA_VC_aggregate, /// for aggregate variables.
88 DSA_VC_pointer, /// for pointer variables.
89 DSA_VC_scalar, /// for scalar variables.
90};
91
92/// Stack for tracking declarations used in OpenMP directives and
93/// clauses and their data-sharing attributes.
94class DSAStackTy {
95public:
96 struct DSAVarData {
97 OpenMPDirectiveKind DKind = OMPD_unknown;
98 OpenMPClauseKind CKind = OMPC_unknown;
99 unsigned Modifier = 0;
100 const Expr *RefExpr = nullptr;
101 DeclRefExpr *PrivateCopy = nullptr;
102 SourceLocation ImplicitDSALoc;
103 bool AppliedToPointee = false;
104 DSAVarData() = default;
105 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
106 const Expr *RefExpr, DeclRefExpr *PrivateCopy,
107 SourceLocation ImplicitDSALoc, unsigned Modifier,
108 bool AppliedToPointee)
109 : DKind(DKind), CKind(CKind), Modifier(Modifier), RefExpr(RefExpr),
110 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc),
111 AppliedToPointee(AppliedToPointee) {}
112 };
113 using OperatorOffsetTy =
114 llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>;
115 using DoacrossClauseMapTy = llvm::DenseMap<OMPClause *, OperatorOffsetTy>;
116 /// Kind of the declaration used in the uses_allocators clauses.
117 enum class UsesAllocatorsDeclKind {
118 /// Predefined allocator
119 PredefinedAllocator,
120 /// User-defined allocator
121 UserDefinedAllocator,
122 /// The declaration that represent allocator trait
123 AllocatorTrait,
124 };
125
126private:
127 struct DSAInfo {
128 OpenMPClauseKind Attributes = OMPC_unknown;
129 unsigned Modifier = 0;
130 /// Pointer to a reference expression and a flag which shows that the
131 /// variable is marked as lastprivate(true) or not (false).
132 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr;
133 DeclRefExpr *PrivateCopy = nullptr;
134 /// true if the attribute is applied to the pointee, not the variable
135 /// itself.
136 bool AppliedToPointee = false;
137 };
138 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>;
139 using UsedRefMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>;
140 using LCDeclInfo = std::pair<unsigned, VarDecl *>;
141 using LoopControlVariablesMapTy =
142 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>;
143 /// Struct that associates a component with the clause kind where they are
144 /// found.
145 struct MappedExprComponentTy {
146 OMPClauseMappableExprCommon::MappableExprComponentLists Components;
147 OpenMPClauseKind Kind = OMPC_unknown;
148 };
149 using MappedExprComponentsTy =
150 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>;
151 using CriticalsWithHintsTy =
152 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>;
153 struct ReductionData {
154 using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>;
155 SourceRange ReductionRange;
156 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp;
157 ReductionData() = default;
158 void set(BinaryOperatorKind BO, SourceRange RR) {
159 ReductionRange = RR;
160 ReductionOp = BO;
161 }
162 void set(const Expr *RefExpr, SourceRange RR) {
163 ReductionRange = RR;
164 ReductionOp = RefExpr;
165 }
166 };
167 using DeclReductionMapTy =
168 llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>;
169 struct DefaultmapInfo {
170 OpenMPDefaultmapClauseModifier ImplicitBehavior =
171 OMPC_DEFAULTMAP_MODIFIER_unknown;
172 SourceLocation SLoc;
173 DefaultmapInfo() = default;
174 DefaultmapInfo(OpenMPDefaultmapClauseModifier M, SourceLocation Loc)
175 : ImplicitBehavior(M), SLoc(Loc) {}
176 };
177
178 struct SharingMapTy {
179 DeclSAMapTy SharingMap;
180 DeclReductionMapTy ReductionMap;
181 UsedRefMapTy AlignedMap;
182 UsedRefMapTy NontemporalMap;
183 MappedExprComponentsTy MappedExprComponents;
184 LoopControlVariablesMapTy LCVMap;
185 /// Track DecompositionDecls and their data-sharing attributes to detect
186 /// conflicting clauses on bindings from the same decomposition.
187 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
188 SourceLocation DefaultAttrLoc;
189 DefaultDataSharingVCAttributes DefaultVCAttr = DSA_VC_all;
190 SourceLocation DefaultAttrVCLoc;
191 DefaultmapInfo DefaultmapMap[OMPC_DEFAULTMAP_unknown + 1];
192 OpenMPDirectiveKind Directive = OMPD_unknown;
193 DeclarationNameInfo DirectiveName;
194 Scope *CurScope = nullptr;
195 DeclContext *Context = nullptr;
196 SourceLocation ConstructLoc;
197 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
198 /// get the data (loop counters etc.) about enclosing loop-based construct.
199 /// This data is required during codegen.
200 DoacrossClauseMapTy DoacrossDepends;
201 /// First argument (Expr *) contains optional argument of the
202 /// 'ordered' clause, the second one is true if the regions has 'ordered'
203 /// clause, false otherwise.
204 std::optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion;
205 bool RegionHasOrderConcurrent = false;
206 unsigned AssociatedLoops = 1;
207 bool HasMutipleLoops = false;
208 const Decl *PossiblyLoopCounter = nullptr;
209 bool NowaitRegion = false;
210 bool UntiedRegion = false;
211 bool CancelRegion = false;
212 bool LoopStart = false;
213 bool BodyComplete = false;
214 SourceLocation PrevScanLocation;
215 SourceLocation PrevOrderedLocation;
216 SourceLocation InnerTeamsRegionLoc;
217 /// Reference to the taskgroup task_reduction reference expression.
218 Expr *TaskgroupReductionRef = nullptr;
219 llvm::DenseSet<QualType> MappedClassesQualTypes;
220 SmallVector<Expr *, 4> InnerUsedAllocators;
221 llvm::DenseSet<CanonicalDeclPtr<Decl>> ImplicitTaskFirstprivates;
222 /// List of globals marked as declare target link in this target region
223 /// (isOpenMPTargetExecutionDirective(Directive) == true).
224 llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls;
225 /// List of decls used in inclusive/exclusive clauses of the scan directive.
226 llvm::DenseSet<CanonicalDeclPtr<Decl>> UsedInScanDirective;
227 llvm::DenseMap<CanonicalDeclPtr<const Decl>, UsesAllocatorsDeclKind>
228 UsesAllocatorsDecls;
229 /// Data is required on creating capture fields for implicit
230 /// default first|private clause.
231 struct ImplicitDefaultFDInfoTy {
232 /// Field decl.
233 const FieldDecl *FD = nullptr;
234 /// Nesting stack level
235 size_t StackLevel = 0;
236 /// Capture variable decl.
237 VarDecl *VD = nullptr;
238 ImplicitDefaultFDInfoTy(const FieldDecl *FD, size_t StackLevel,
239 VarDecl *VD)
240 : FD(FD), StackLevel(StackLevel), VD(VD) {}
241 };
242 /// List of captured fields
243 llvm::SmallVector<ImplicitDefaultFDInfoTy, 8>
244 ImplicitDefaultFirstprivateFDs;
245 Expr *DeclareMapperVar = nullptr;
246 SmallVector<VarDecl *, 16> IteratorVarDecls;
247 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
248 Scope *CurScope, SourceLocation Loc)
249 : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
250 ConstructLoc(Loc) {}
251 SharingMapTy() = default;
252 };
253
254 using StackTy = SmallVector<SharingMapTy, 4>;
255
256 /// Stack of used declaration and their data-sharing attributes.
257 DeclSAMapTy Threadprivates;
258 DeclSAMapTy Groupprivates;
259 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
260 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
261 /// true, if check for DSA must be from parent directive, false, if
262 /// from current directive.
263 OpenMPClauseKind ClauseKindMode = OMPC_unknown;
264 Sema &SemaRef;
265 bool ForceCapturing = false;
266 /// true if all the variables in the target executable directives must be
267 /// captured by reference.
268 bool ForceCaptureByReferenceInTargetExecutable = false;
269 CriticalsWithHintsTy Criticals;
270 unsigned IgnoredStackElements = 0;
271
272 /// Iterators over the stack iterate in order from innermost to outermost
273 /// directive.
274 using const_iterator = StackTy::const_reverse_iterator;
275 const_iterator begin() const {
276 return Stack.empty() ? const_iterator()
277 : Stack.back().first.rbegin() + IgnoredStackElements;
278 }
279 const_iterator end() const {
280 return Stack.empty() ? const_iterator() : Stack.back().first.rend();
281 }
282 using iterator = StackTy::reverse_iterator;
283 iterator begin() {
284 return Stack.empty() ? iterator()
285 : Stack.back().first.rbegin() + IgnoredStackElements;
286 }
287 iterator end() {
288 return Stack.empty() ? iterator() : Stack.back().first.rend();
289 }
290
291 // Convenience operations to get at the elements of the stack.
292
293 bool isStackEmpty() const {
294 return Stack.empty() ||
295 Stack.back().second != CurrentNonCapturingFunctionScope ||
296 Stack.back().first.size() <= IgnoredStackElements;
297 }
298 size_t getStackSize() const {
299 return isStackEmpty() ? 0
300 : Stack.back().first.size() - IgnoredStackElements;
301 }
302
303 SharingMapTy *getTopOfStackOrNull() {
304 size_t Size = getStackSize();
305 if (Size == 0)
306 return nullptr;
307 return &Stack.back().first[Size - 1];
308 }
309 const SharingMapTy *getTopOfStackOrNull() const {
310 return const_cast<DSAStackTy &>(*this).getTopOfStackOrNull();
311 }
312 SharingMapTy &getTopOfStack() {
313 assert(!isStackEmpty() && "no current directive");
314 return *getTopOfStackOrNull();
315 }
316 const SharingMapTy &getTopOfStack() const {
317 return const_cast<DSAStackTy &>(*this).getTopOfStack();
318 }
319
320 SharingMapTy *getSecondOnStackOrNull() {
321 size_t Size = getStackSize();
322 if (Size <= 1)
323 return nullptr;
324 return &Stack.back().first[Size - 2];
325 }
326 const SharingMapTy *getSecondOnStackOrNull() const {
327 return const_cast<DSAStackTy &>(*this).getSecondOnStackOrNull();
328 }
329
330 /// Get the stack element at a certain level (previously returned by
331 /// \c getNestingLevel).
332 ///
333 /// Note that nesting levels count from outermost to innermost, and this is
334 /// the reverse of our iteration order where new inner levels are pushed at
335 /// the front of the stack.
336 SharingMapTy &getStackElemAtLevel(unsigned Level) {
337 assert(Level < getStackSize() && "no such stack element");
338 return Stack.back().first[Level];
339 }
340 const SharingMapTy &getStackElemAtLevel(unsigned Level) const {
341 return const_cast<DSAStackTy &>(*this).getStackElemAtLevel(Level);
342 }
343
344 DSAVarData getDSA(const_iterator &Iter, ValueDecl *D) const;
345
346 /// Checks if the variable is a local for OpenMP region.
347 bool isOpenMPLocal(VarDecl *D, const_iterator Iter) const;
348
349 /// Vector of previously declared requires directives
350 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls;
351 /// omp_allocator_handle_t type.
352 QualType OMPAllocatorHandleT;
353 /// omp_depend_t type.
354 QualType OMPDependT;
355 /// omp_event_handle_t type.
356 QualType OMPEventHandleT;
357 /// omp_alloctrait_t type.
358 QualType OMPAlloctraitT;
359 /// Expression for the predefined allocators.
360 Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = {
361 nullptr};
362 /// Vector of previously encountered target directives
363 SmallVector<SourceLocation, 2> TargetLocations;
364 SourceLocation AtomicLocation;
365 /// Vector of declare variant construct traits.
366 SmallVector<llvm::omp::TraitProperty, 8> ConstructTraits;
367
368public:
369 explicit DSAStackTy(Sema &S) : SemaRef(S) {}
370
371 /// Sets omp_allocator_handle_t type.
372 void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; }
373 /// Gets omp_allocator_handle_t type.
374 QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; }
375 /// Sets omp_alloctrait_t type.
376 void setOMPAlloctraitT(QualType Ty) { OMPAlloctraitT = Ty; }
377 /// Gets omp_alloctrait_t type.
378 QualType getOMPAlloctraitT() const { return OMPAlloctraitT; }
379 /// Sets the given default allocator.
380 void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
381 Expr *Allocator) {
382 OMPPredefinedAllocators[AllocatorKind] = Allocator;
383 }
384 /// Returns the specified default allocator.
385 Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const {
386 return OMPPredefinedAllocators[AllocatorKind];
387 }
388 /// Sets omp_depend_t type.
389 void setOMPDependT(QualType Ty) { OMPDependT = Ty; }
390 /// Gets omp_depend_t type.
391 QualType getOMPDependT() const { return OMPDependT; }
392
393 /// Sets omp_event_handle_t type.
394 void setOMPEventHandleT(QualType Ty) { OMPEventHandleT = Ty; }
395 /// Gets omp_event_handle_t type.
396 QualType getOMPEventHandleT() const { return OMPEventHandleT; }
397
398 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
399 OpenMPClauseKind getClauseParsingMode() const {
400 assert(isClauseParsingMode() && "Must be in clause parsing mode.");
401 return ClauseKindMode;
402 }
403 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
404
405 bool isBodyComplete() const {
406 const SharingMapTy *Top = getTopOfStackOrNull();
407 return Top && Top->BodyComplete;
408 }
409 void setBodyComplete() { getTopOfStack().BodyComplete = true; }
410
411 bool isForceVarCapturing() const { return ForceCapturing; }
412 void setForceVarCapturing(bool V) { ForceCapturing = V; }
413
414 void setForceCaptureByReferenceInTargetExecutable(bool V) {
415 ForceCaptureByReferenceInTargetExecutable = V;
416 }
417 bool isForceCaptureByReferenceInTargetExecutable() const {
418 return ForceCaptureByReferenceInTargetExecutable;
419 }
420
421 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
422 Scope *CurScope, SourceLocation Loc) {
423 assert(!IgnoredStackElements &&
424 "cannot change stack while ignoring elements");
425 if (Stack.empty() ||
426 Stack.back().second != CurrentNonCapturingFunctionScope)
427 Stack.emplace_back(Args: StackTy(), Args&: CurrentNonCapturingFunctionScope);
428 Stack.back().first.emplace_back(Args&: DKind, Args: DirName, Args&: CurScope, Args&: Loc);
429 Stack.back().first.back().DefaultAttrLoc = Loc;
430 }
431
432 void pop() {
433 assert(!IgnoredStackElements &&
434 "cannot change stack while ignoring elements");
435 assert(!Stack.back().first.empty() &&
436 "Data-sharing attributes stack is empty!");
437 Stack.back().first.pop_back();
438 }
439
440 /// RAII object to temporarily leave the scope of a directive when we want to
441 /// logically operate in its parent.
442 class ParentDirectiveScope {
443 DSAStackTy &Self;
444 bool Active;
445
446 public:
447 ParentDirectiveScope(DSAStackTy &Self, bool Activate)
448 : Self(Self), Active(false) {
449 if (Activate)
450 enable();
451 }
452 ~ParentDirectiveScope() { disable(); }
453 void disable() {
454 if (Active) {
455 --Self.IgnoredStackElements;
456 Active = false;
457 }
458 }
459 void enable() {
460 if (!Active) {
461 ++Self.IgnoredStackElements;
462 Active = true;
463 }
464 }
465 };
466
467 /// Marks that we're started loop parsing.
468 void loopInit() {
469 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
470 "Expected loop-based directive.");
471 getTopOfStack().LoopStart = true;
472 }
473 /// Start capturing of the variables in the loop context.
474 void loopStart() {
475 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
476 "Expected loop-based directive.");
477 getTopOfStack().LoopStart = false;
478 }
479 /// true, if variables are captured, false otherwise.
480 bool isLoopStarted() const {
481 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
482 "Expected loop-based directive.");
483 return !getTopOfStack().LoopStart;
484 }
485 /// Marks (or clears) declaration as possibly loop counter.
486 void resetPossibleLoopCounter(const Decl *D = nullptr) {
487 getTopOfStack().PossiblyLoopCounter = D ? D->getCanonicalDecl() : D;
488 }
489 /// Gets the possible loop counter decl.
490 const Decl *getPossiblyLoopCounter() const {
491 return getTopOfStack().PossiblyLoopCounter;
492 }
493 /// Start new OpenMP region stack in new non-capturing function.
494 void pushFunction() {
495 assert(!IgnoredStackElements &&
496 "cannot change stack while ignoring elements");
497 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
498 assert(!isa<CapturingScopeInfo>(CurFnScope));
499 CurrentNonCapturingFunctionScope = CurFnScope;
500 }
501 /// Pop region stack for non-capturing function.
502 void popFunction(const FunctionScopeInfo *OldFSI) {
503 assert(!IgnoredStackElements &&
504 "cannot change stack while ignoring elements");
505 if (!Stack.empty() && Stack.back().second == OldFSI) {
506 assert(Stack.back().first.empty());
507 Stack.pop_back();
508 }
509 CurrentNonCapturingFunctionScope = nullptr;
510 for (const FunctionScopeInfo *FSI : llvm::reverse(C&: SemaRef.FunctionScopes)) {
511 if (!isa<CapturingScopeInfo>(Val: FSI)) {
512 CurrentNonCapturingFunctionScope = FSI;
513 break;
514 }
515 }
516 }
517
518 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) {
519 Criticals.try_emplace(Key: D->getDirectiveName().getAsString(), Args&: D, Args&: Hint);
520 }
521 std::pair<const OMPCriticalDirective *, llvm::APSInt>
522 getCriticalWithHint(const DeclarationNameInfo &Name) const {
523 auto I = Criticals.find(Key: Name.getAsString());
524 if (I != Criticals.end())
525 return I->second;
526 return std::make_pair(x: nullptr, y: llvm::APSInt());
527 }
528 /// If 'aligned' declaration for given variable \a D was not seen yet,
529 /// add it and return NULL; otherwise return previous occurrence's expression
530 /// for diagnostics.
531 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE);
532 /// If 'nontemporal' declaration for given variable \a D was not seen yet,
533 /// add it and return NULL; otherwise return previous occurrence's expression
534 /// for diagnostics.
535 const Expr *addUniqueNontemporal(const ValueDecl *D, const Expr *NewDE);
536
537 /// Register specified variable as loop control variable.
538 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture);
539 /// Check if the specified variable is a loop control variable for
540 /// current region.
541 /// \return The index of the loop control variable in the list of associated
542 /// for-loops (from outer to inner).
543 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const;
544 /// Check if the specified variable is a loop control variable for
545 /// parent region.
546 /// \return The index of the loop control variable in the list of associated
547 /// for-loops (from outer to inner).
548 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const;
549 /// Check if the specified variable is a loop control variable for
550 /// current region.
551 /// \return The index of the loop control variable in the list of associated
552 /// for-loops (from outer to inner).
553 const LCDeclInfo isLoopControlVariable(const ValueDecl *D,
554 unsigned Level) const;
555 /// Get the loop control variable for the I-th loop (or nullptr) in
556 /// parent directive.
557 const ValueDecl *getParentLoopControlVariable(unsigned I) const;
558
559 /// Marks the specified decl \p D as used in scan directive.
560 void markDeclAsUsedInScanDirective(ValueDecl *D) {
561 if (SharingMapTy *Stack = getSecondOnStackOrNull())
562 Stack->UsedInScanDirective.insert(V: D);
563 }
564
565 /// Checks if the specified declaration was used in the inner scan directive.
566 bool isUsedInScanDirective(ValueDecl *D) const {
567 if (const SharingMapTy *Stack = getTopOfStackOrNull())
568 return Stack->UsedInScanDirective.contains(V: D);
569 return false;
570 }
571
572 /// Adds explicit data sharing attribute to the specified declaration.
573 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
574 DeclRefExpr *PrivateCopy = nullptr, unsigned Modifier = 0,
575 bool AppliedToPointee = false);
576
577 /// Adds additional information for the reduction items with the reduction id
578 /// represented as an operator.
579 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
580 BinaryOperatorKind BOK);
581 /// Adds additional information for the reduction items with the reduction id
582 /// represented as reduction identifier.
583 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
584 const Expr *ReductionRef);
585 /// Returns the location and reduction operation from the innermost parent
586 /// region for the given \p D.
587 const DSAVarData
588 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
589 BinaryOperatorKind &BOK,
590 Expr *&TaskgroupDescriptor) const;
591 /// Returns the location and reduction operation from the innermost parent
592 /// region for the given \p D.
593 const DSAVarData
594 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
595 const Expr *&ReductionRef,
596 Expr *&TaskgroupDescriptor) const;
597 /// Return reduction reference expression for the current taskgroup or
598 /// parallel/worksharing directives with task reductions.
599 Expr *getTaskgroupReductionRef() const {
600 assert((getTopOfStack().Directive == OMPD_taskgroup ||
601 ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
602 isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
603 !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
604 "taskgroup reference expression requested for non taskgroup or "
605 "parallel/worksharing directive.");
606 return getTopOfStack().TaskgroupReductionRef;
607 }
608 /// Checks if the given \p VD declaration is actually a taskgroup reduction
609 /// descriptor variable at the \p Level of OpenMP regions.
610 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const {
611 return getStackElemAtLevel(Level).TaskgroupReductionRef &&
612 cast<DeclRefExpr>(Val: getStackElemAtLevel(Level).TaskgroupReductionRef)
613 ->getDecl() == VD;
614 }
615
616 /// Returns data sharing attributes from top of the stack for the
617 /// specified declaration.
618 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
619 /// Returns data-sharing attributes for the specified declaration.
620 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const;
621 /// Returns data-sharing attributes for the specified declaration.
622 const DSAVarData getImplicitDSA(ValueDecl *D, unsigned Level) const;
623 /// Checks if the specified variables has data-sharing attributes which
624 /// match specified \a CPred predicate in any directive which matches \a DPred
625 /// predicate.
626 const DSAVarData
627 hasDSA(ValueDecl *D,
628 const llvm::function_ref<bool(OpenMPClauseKind, bool,
629 DefaultDataSharingAttributes)>
630 CPred,
631 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
632 bool FromParent) const;
633 /// Checks if the specified variables has data-sharing attributes which
634 /// match specified \a CPred predicate in any innermost directive which
635 /// matches \a DPred predicate.
636 const DSAVarData
637 hasInnermostDSA(ValueDecl *D,
638 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
639 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
640 bool FromParent) const;
641 /// Checks if the specified variables has explicit data-sharing
642 /// attributes which match specified \a CPred predicate at the specified
643 /// OpenMP region.
644 bool
645 hasExplicitDSA(const ValueDecl *D,
646 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
647 unsigned Level, bool NotLastprivate = false) const;
648
649 /// Returns true if the directive at level \Level matches in the
650 /// specified \a DPred predicate.
651 bool hasExplicitDirective(
652 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
653 unsigned Level) const;
654
655 /// Finds a directive which matches specified \a DPred predicate.
656 bool hasDirective(
657 const llvm::function_ref<bool(
658 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)>
659 DPred,
660 bool FromParent) const;
661
662 /// Returns currently analyzed directive.
663 OpenMPDirectiveKind getCurrentDirective() const {
664 const SharingMapTy *Top = getTopOfStackOrNull();
665 return Top ? Top->Directive : OMPD_unknown;
666 }
667 /// Returns directive kind at specified level.
668 OpenMPDirectiveKind getDirective(unsigned Level) const {
669 assert(!isStackEmpty() && "No directive at specified level.");
670 return getStackElemAtLevel(Level).Directive;
671 }
672 /// Returns the capture region at the specified level.
673 OpenMPDirectiveKind getCaptureRegion(unsigned Level,
674 unsigned OpenMPCaptureLevel) const {
675 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
676 getOpenMPCaptureRegions(CaptureRegions, DKind: getDirective(Level));
677 return CaptureRegions[OpenMPCaptureLevel];
678 }
679 /// Returns parent directive.
680 OpenMPDirectiveKind getParentDirective() const {
681 const SharingMapTy *Parent = getSecondOnStackOrNull();
682 return Parent ? Parent->Directive : OMPD_unknown;
683 }
684
685 /// Add requires decl to internal vector
686 void addRequiresDecl(OMPRequiresDecl *RD) { RequiresDecls.push_back(Elt: RD); }
687
688 ArrayRef<const OMPRequiresDecl *> getRequiresDecls() const {
689 return RequiresDecls;
690 }
691
692 /// Checks if the defined 'requires' directive has specified type of clause.
693 template <typename ClauseType> bool hasRequiresDeclWithClause() const {
694 return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
695 return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
696 return isa<ClauseType>(C);
697 });
698 });
699 }
700
701 /// Checks for a duplicate clause amongst previously declared requires
702 /// directives
703 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const {
704 bool IsDuplicate = false;
705 for (OMPClause *CNew : ClauseList) {
706 for (const OMPRequiresDecl *D : RequiresDecls) {
707 for (const OMPClause *CPrev : D->clauselists()) {
708 if (CNew->getClauseKind() == CPrev->getClauseKind()) {
709 SemaRef.Diag(Loc: CNew->getBeginLoc(),
710 DiagID: diag::err_omp_requires_clause_redeclaration)
711 << getOpenMPClauseNameForDiag(C: CNew->getClauseKind());
712 SemaRef.Diag(Loc: CPrev->getBeginLoc(),
713 DiagID: diag::note_omp_requires_previous_clause)
714 << getOpenMPClauseNameForDiag(C: CPrev->getClauseKind());
715 IsDuplicate = true;
716 }
717 }
718 }
719 }
720 return IsDuplicate;
721 }
722
723 /// Add location of previously encountered target to internal vector
724 void addTargetDirLocation(SourceLocation LocStart) {
725 TargetLocations.push_back(Elt: LocStart);
726 }
727
728 /// Add location for the first encountered atomic directive.
729 void addAtomicDirectiveLoc(SourceLocation Loc) {
730 if (AtomicLocation.isInvalid())
731 AtomicLocation = Loc;
732 }
733
734 /// Returns the location of the first encountered atomic directive in the
735 /// module.
736 SourceLocation getAtomicDirectiveLoc() const { return AtomicLocation; }
737
738 // Return previously encountered target region locations.
739 ArrayRef<SourceLocation> getEncounteredTargetLocs() const {
740 return TargetLocations;
741 }
742
743 /// Set default data sharing attribute to none.
744 void setDefaultDSANone(SourceLocation Loc) {
745 getTopOfStack().DefaultAttr = DSA_none;
746 getTopOfStack().DefaultAttrLoc = Loc;
747 }
748 /// Set default data sharing attribute to shared.
749 void setDefaultDSAShared(SourceLocation Loc) {
750 getTopOfStack().DefaultAttr = DSA_shared;
751 getTopOfStack().DefaultAttrLoc = Loc;
752 }
753 /// Set default data sharing attribute to private.
754 void setDefaultDSAPrivate(SourceLocation Loc) {
755 getTopOfStack().DefaultAttr = DSA_private;
756 getTopOfStack().DefaultAttrLoc = Loc;
757 }
758 /// Set default data sharing attribute to firstprivate.
759 void setDefaultDSAFirstPrivate(SourceLocation Loc) {
760 getTopOfStack().DefaultAttr = DSA_firstprivate;
761 getTopOfStack().DefaultAttrLoc = Loc;
762 }
763 /// Set default data sharing variable category attribute to aggregate.
764 void setDefaultDSAVCAggregate(SourceLocation VCLoc) {
765 getTopOfStack().DefaultVCAttr = DSA_VC_aggregate;
766 getTopOfStack().DefaultAttrVCLoc = VCLoc;
767 }
768 /// Set default data sharing variable category attribute to all.
769 void setDefaultDSAVCAll(SourceLocation VCLoc) {
770 getTopOfStack().DefaultVCAttr = DSA_VC_all;
771 getTopOfStack().DefaultAttrVCLoc = VCLoc;
772 }
773 /// Set default data sharing variable category attribute to pointer.
774 void setDefaultDSAVCPointer(SourceLocation VCLoc) {
775 getTopOfStack().DefaultVCAttr = DSA_VC_pointer;
776 getTopOfStack().DefaultAttrVCLoc = VCLoc;
777 }
778 /// Set default data sharing variable category attribute to scalar.
779 void setDefaultDSAVCScalar(SourceLocation VCLoc) {
780 getTopOfStack().DefaultVCAttr = DSA_VC_scalar;
781 getTopOfStack().DefaultAttrVCLoc = VCLoc;
782 }
783 /// Set default data mapping attribute to Modifier:Kind
784 void setDefaultDMAAttr(OpenMPDefaultmapClauseModifier M,
785 OpenMPDefaultmapClauseKind Kind, SourceLocation Loc) {
786 DefaultmapInfo &DMI = getTopOfStack().DefaultmapMap[Kind];
787 DMI.ImplicitBehavior = M;
788 DMI.SLoc = Loc;
789 }
790 /// Check whether the implicit-behavior has been set in defaultmap
791 bool checkDefaultmapCategory(OpenMPDefaultmapClauseKind VariableCategory) {
792 if (VariableCategory == OMPC_DEFAULTMAP_unknown)
793 return getTopOfStack()
794 .DefaultmapMap[OMPC_DEFAULTMAP_aggregate]
795 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown ||
796 getTopOfStack()
797 .DefaultmapMap[OMPC_DEFAULTMAP_scalar]
798 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown ||
799 getTopOfStack()
800 .DefaultmapMap[OMPC_DEFAULTMAP_pointer]
801 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown;
802 return getTopOfStack().DefaultmapMap[VariableCategory].ImplicitBehavior !=
803 OMPC_DEFAULTMAP_MODIFIER_unknown;
804 }
805
806 ArrayRef<llvm::omp::TraitProperty> getConstructTraits() {
807 return ConstructTraits;
808 }
809 void handleConstructTrait(ArrayRef<llvm::omp::TraitProperty> Traits,
810 bool ScopeEntry) {
811 if (ScopeEntry)
812 ConstructTraits.append(in_start: Traits.begin(), in_end: Traits.end());
813 else
814 for (llvm::omp::TraitProperty Trait : llvm::reverse(C&: Traits)) {
815 llvm::omp::TraitProperty Top = ConstructTraits.pop_back_val();
816 assert(Top == Trait && "Something left a trait on the stack!");
817 (void)Trait;
818 (void)Top;
819 }
820 }
821
822 DefaultDataSharingAttributes getDefaultDSA(unsigned Level) const {
823 return getStackSize() <= Level ? DSA_unspecified
824 : getStackElemAtLevel(Level).DefaultAttr;
825 }
826 DefaultDataSharingAttributes getDefaultDSA() const {
827 return isStackEmpty() ? DSA_unspecified : getTopOfStack().DefaultAttr;
828 }
829 SourceLocation getDefaultDSALocation() const {
830 return isStackEmpty() ? SourceLocation() : getTopOfStack().DefaultAttrLoc;
831 }
832 OpenMPDefaultmapClauseModifier
833 getDefaultmapModifier(OpenMPDefaultmapClauseKind Kind) const {
834 return isStackEmpty()
835 ? OMPC_DEFAULTMAP_MODIFIER_unknown
836 : getTopOfStack().DefaultmapMap[Kind].ImplicitBehavior;
837 }
838 OpenMPDefaultmapClauseModifier
839 getDefaultmapModifierAtLevel(unsigned Level,
840 OpenMPDefaultmapClauseKind Kind) const {
841 return getStackElemAtLevel(Level).DefaultmapMap[Kind].ImplicitBehavior;
842 }
843 bool isDefaultmapCapturedByRef(unsigned Level,
844 OpenMPDefaultmapClauseKind Kind) const {
845 OpenMPDefaultmapClauseModifier M =
846 getDefaultmapModifierAtLevel(Level, Kind);
847 if (Kind == OMPC_DEFAULTMAP_scalar || Kind == OMPC_DEFAULTMAP_pointer) {
848 return (M == OMPC_DEFAULTMAP_MODIFIER_alloc) ||
849 (M == OMPC_DEFAULTMAP_MODIFIER_to) ||
850 (M == OMPC_DEFAULTMAP_MODIFIER_from) ||
851 (M == OMPC_DEFAULTMAP_MODIFIER_tofrom) ||
852 (M == OMPC_DEFAULTMAP_MODIFIER_present) ||
853 (M == OMPC_DEFAULTMAP_MODIFIER_storage);
854 }
855 return true;
856 }
857 static bool mustBeFirstprivateBase(OpenMPDefaultmapClauseModifier M,
858 OpenMPDefaultmapClauseKind Kind) {
859 switch (Kind) {
860 case OMPC_DEFAULTMAP_scalar:
861 case OMPC_DEFAULTMAP_pointer:
862 return (M == OMPC_DEFAULTMAP_MODIFIER_unknown) ||
863 (M == OMPC_DEFAULTMAP_MODIFIER_firstprivate) ||
864 (M == OMPC_DEFAULTMAP_MODIFIER_default);
865 case OMPC_DEFAULTMAP_aggregate:
866 return M == OMPC_DEFAULTMAP_MODIFIER_firstprivate;
867 default:
868 break;
869 }
870 llvm_unreachable("Unexpected OpenMPDefaultmapClauseKind enum");
871 }
872 bool mustBeFirstprivateAtLevel(unsigned Level,
873 OpenMPDefaultmapClauseKind Kind) const {
874 OpenMPDefaultmapClauseModifier M =
875 getDefaultmapModifierAtLevel(Level, Kind);
876 return mustBeFirstprivateBase(M, Kind);
877 }
878 bool mustBeFirstprivate(OpenMPDefaultmapClauseKind Kind) const {
879 OpenMPDefaultmapClauseModifier M = getDefaultmapModifier(Kind);
880 return mustBeFirstprivateBase(M, Kind);
881 }
882
883 /// Checks if the specified variable is a threadprivate.
884 bool isThreadPrivate(VarDecl *D) {
885 const DSAVarData DVar = getTopDSA(D, FromParent: false);
886 return isOpenMPThreadPrivate(Kind: DVar.CKind);
887 }
888
889 /// Marks current region as ordered (it has an 'ordered' clause).
890 void setOrderedRegion(bool IsOrdered, const Expr *Param,
891 OMPOrderedClause *Clause) {
892 if (IsOrdered)
893 getTopOfStack().OrderedRegion.emplace(args&: Param, args&: Clause);
894 else
895 getTopOfStack().OrderedRegion.reset();
896 }
897 /// Returns true, if region is ordered (has associated 'ordered' clause),
898 /// false - otherwise.
899 bool isOrderedRegion() const {
900 if (const SharingMapTy *Top = getTopOfStackOrNull())
901 return Top->OrderedRegion.has_value();
902 return false;
903 }
904 /// Returns optional parameter for the ordered region.
905 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const {
906 if (const SharingMapTy *Top = getTopOfStackOrNull())
907 if (Top->OrderedRegion)
908 return *Top->OrderedRegion;
909 return std::make_pair(x: nullptr, y: nullptr);
910 }
911 /// Returns true, if parent region is ordered (has associated
912 /// 'ordered' clause), false - otherwise.
913 bool isParentOrderedRegion() const {
914 if (const SharingMapTy *Parent = getSecondOnStackOrNull())
915 return Parent->OrderedRegion.has_value();
916 return false;
917 }
918 /// Returns optional parameter for the ordered region.
919 std::pair<const Expr *, OMPOrderedClause *>
920 getParentOrderedRegionParam() const {
921 if (const SharingMapTy *Parent = getSecondOnStackOrNull())
922 if (Parent->OrderedRegion)
923 return *Parent->OrderedRegion;
924 return std::make_pair(x: nullptr, y: nullptr);
925 }
926 /// Marks current region as having an 'order' clause.
927 void setRegionHasOrderConcurrent(bool HasOrderConcurrent) {
928 getTopOfStack().RegionHasOrderConcurrent = HasOrderConcurrent;
929 }
930 /// Returns true if the nearest enclosing directive that is not a
931 /// loop-transforming construct has an order(concurrent) clause.
932 bool isEnclosingOrderConcurrent() const {
933 for (const_iterator I = begin() + std::min<size_t>(a: 1, b: getStackSize()),
934 E = end();
935 I != E; ++I) {
936 if (I->RegionHasOrderConcurrent)
937 return true;
938 if (!isOpenMPLoopTransformationDirective(DKind: I->Directive))
939 return false;
940 }
941 return false;
942 }
943 /// Returns true if the nearest enclosing directive that is not a
944 /// loop-transforming or ordered simd construct is a simd directive.
945 bool isInSimdRegion() const {
946 for (const_iterator I = begin() + std::min<size_t>(a: 1, b: getStackSize()),
947 E = end();
948 I != E; ++I) {
949 if (isOpenMPSimdDirective(DKind: I->Directive))
950 return true;
951 if (!isOpenMPLoopTransformationDirective(DKind: I->Directive) &&
952 I->Directive != OMPD_ordered_blockassoc)
953 return false;
954 }
955 return false;
956 }
957 /// Marks current region as nowait (it has a 'nowait' clause).
958 void setNowaitRegion(bool IsNowait = true) {
959 getTopOfStack().NowaitRegion = IsNowait;
960 }
961 /// Returns true, if parent region is nowait (has associated
962 /// 'nowait' clause), false - otherwise.
963 bool isParentNowaitRegion() const {
964 if (const SharingMapTy *Parent = getSecondOnStackOrNull())
965 return Parent->NowaitRegion;
966 return false;
967 }
968 /// Marks current region as untied (it has a 'untied' clause).
969 void setUntiedRegion(bool IsUntied = true) {
970 getTopOfStack().UntiedRegion = IsUntied;
971 }
972 /// Return true if current region is untied.
973 bool isUntiedRegion() const {
974 const SharingMapTy *Top = getTopOfStackOrNull();
975 return Top ? Top->UntiedRegion : false;
976 }
977 /// Marks parent region as cancel region.
978 void setParentCancelRegion(bool Cancel = true) {
979 if (SharingMapTy *Parent = getSecondOnStackOrNull())
980 Parent->CancelRegion |= Cancel;
981 }
982 /// Return true if current region has inner cancel construct.
983 bool isCancelRegion() const {
984 const SharingMapTy *Top = getTopOfStackOrNull();
985 return Top ? Top->CancelRegion : false;
986 }
987
988 /// Mark that parent region already has scan directive.
989 void setParentHasScanDirective(SourceLocation Loc) {
990 if (SharingMapTy *Parent = getSecondOnStackOrNull())
991 Parent->PrevScanLocation = Loc;
992 }
993 /// Return true if current region has inner cancel construct.
994 bool doesParentHasScanDirective() const {
995 const SharingMapTy *Top = getSecondOnStackOrNull();
996 return Top ? Top->PrevScanLocation.isValid() : false;
997 }
998 /// Return true if current region has inner cancel construct.
999 SourceLocation getParentScanDirectiveLoc() const {
1000 const SharingMapTy *Top = getSecondOnStackOrNull();
1001 return Top ? Top->PrevScanLocation : SourceLocation();
1002 }
1003 /// Mark that parent region already has ordered directive.
1004 void setParentHasOrderedDirective(SourceLocation Loc) {
1005 if (SharingMapTy *Parent = getSecondOnStackOrNull())
1006 Parent->PrevOrderedLocation = Loc;
1007 }
1008 /// Return true if current region has inner ordered construct.
1009 bool doesParentHasOrderedDirective() const {
1010 const SharingMapTy *Top = getSecondOnStackOrNull();
1011 return Top ? Top->PrevOrderedLocation.isValid() : false;
1012 }
1013 /// Returns the location of the previously specified ordered directive.
1014 SourceLocation getParentOrderedDirectiveLoc() const {
1015 const SharingMapTy *Top = getSecondOnStackOrNull();
1016 return Top ? Top->PrevOrderedLocation : SourceLocation();
1017 }
1018
1019 /// Set collapse value for the region.
1020 void setAssociatedLoops(unsigned Val) {
1021 getTopOfStack().AssociatedLoops = Val;
1022 if (Val > 1)
1023 getTopOfStack().HasMutipleLoops = true;
1024 }
1025 /// Return collapse value for region.
1026 unsigned getAssociatedLoops() const {
1027 const SharingMapTy *Top = getTopOfStackOrNull();
1028 return Top ? Top->AssociatedLoops : 0;
1029 }
1030 /// Returns true if the construct is associated with multiple loops.
1031 bool hasMutipleLoops() const {
1032 const SharingMapTy *Top = getTopOfStackOrNull();
1033 return Top ? Top->HasMutipleLoops : false;
1034 }
1035
1036 /// Marks current target region as one with closely nested teams
1037 /// region.
1038 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
1039 if (SharingMapTy *Parent = getSecondOnStackOrNull())
1040 Parent->InnerTeamsRegionLoc = TeamsRegionLoc;
1041 }
1042 /// Returns true, if current region has closely nested teams region.
1043 bool hasInnerTeamsRegion() const {
1044 return getInnerTeamsRegionLoc().isValid();
1045 }
1046 /// Returns location of the nested teams region (if any).
1047 SourceLocation getInnerTeamsRegionLoc() const {
1048 const SharingMapTy *Top = getTopOfStackOrNull();
1049 return Top ? Top->InnerTeamsRegionLoc : SourceLocation();
1050 }
1051
1052 Scope *getCurScope() const {
1053 const SharingMapTy *Top = getTopOfStackOrNull();
1054 return Top ? Top->CurScope : nullptr;
1055 }
1056 void setContext(DeclContext *DC) { getTopOfStack().Context = DC; }
1057 SourceLocation getConstructLoc() const {
1058 const SharingMapTy *Top = getTopOfStackOrNull();
1059 return Top ? Top->ConstructLoc : SourceLocation();
1060 }
1061
1062 /// Do the check specified in \a Check to all component lists and return true
1063 /// if any issue is found.
1064 bool checkMappableExprComponentListsForDecl(
1065 const ValueDecl *VD, bool CurrentRegionOnly,
1066 const llvm::function_ref<
1067 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
1068 OpenMPClauseKind)>
1069 Check) const {
1070 if (isStackEmpty())
1071 return false;
1072 auto SI = begin();
1073 auto SE = end();
1074
1075 if (SI == SE)
1076 return false;
1077
1078 if (CurrentRegionOnly)
1079 SE = std::next(x: SI);
1080 else
1081 std::advance(i&: SI, n: 1);
1082
1083 for (; SI != SE; ++SI) {
1084 auto MI = SI->MappedExprComponents.find(Val: VD);
1085 if (MI != SI->MappedExprComponents.end())
1086 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
1087 MI->second.Components)
1088 if (Check(L, MI->second.Kind))
1089 return true;
1090 }
1091 return false;
1092 }
1093
1094 /// Do the check specified in \a Check to all component lists at a given level
1095 /// and return true if any issue is found.
1096 bool checkMappableExprComponentListsForDeclAtLevel(
1097 const ValueDecl *VD, unsigned Level,
1098 const llvm::function_ref<
1099 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
1100 OpenMPClauseKind)>
1101 Check) const {
1102 if (getStackSize() <= Level)
1103 return false;
1104
1105 const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1106 auto MI = StackElem.MappedExprComponents.find(Val: VD);
1107 if (MI != StackElem.MappedExprComponents.end())
1108 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
1109 MI->second.Components)
1110 if (Check(L, MI->second.Kind))
1111 return true;
1112 return false;
1113 }
1114
1115 /// Create a new mappable expression component list associated with a given
1116 /// declaration and initialize it with the provided list of components.
1117 void addMappableExpressionComponents(
1118 const ValueDecl *VD,
1119 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
1120 OpenMPClauseKind WhereFoundClauseKind) {
1121 MappedExprComponentTy &MEC = getTopOfStack().MappedExprComponents[VD];
1122 // Create new entry and append the new components there.
1123 MEC.Components.resize(N: MEC.Components.size() + 1);
1124 MEC.Components.back().append(in_start: Components.begin(), in_end: Components.end());
1125 MEC.Kind = WhereFoundClauseKind;
1126 }
1127
1128 unsigned getNestingLevel() const {
1129 assert(!isStackEmpty());
1130 return getStackSize() - 1;
1131 }
1132 void addDoacrossDependClause(OMPClause *C, const OperatorOffsetTy &OpsOffs) {
1133 SharingMapTy *Parent = getSecondOnStackOrNull();
1134 assert(Parent && isOpenMPWorksharingDirective(Parent->Directive));
1135 Parent->DoacrossDepends.try_emplace(Key: C, Args: OpsOffs);
1136 }
1137 llvm::iterator_range<DoacrossClauseMapTy::const_iterator>
1138 getDoacrossDependClauses() const {
1139 const SharingMapTy &StackElem = getTopOfStack();
1140 if (isOpenMPWorksharingDirective(DKind: StackElem.Directive)) {
1141 const DoacrossClauseMapTy &Ref = StackElem.DoacrossDepends;
1142 return llvm::make_range(x: Ref.begin(), y: Ref.end());
1143 }
1144 return llvm::make_range(x: StackElem.DoacrossDepends.end(),
1145 y: StackElem.DoacrossDepends.end());
1146 }
1147
1148 // Store types of classes which have been explicitly mapped
1149 void addMappedClassesQualTypes(QualType QT) {
1150 SharingMapTy &StackElem = getTopOfStack();
1151 StackElem.MappedClassesQualTypes.insert(V: QT);
1152 }
1153
1154 // Return set of mapped classes types
1155 bool isClassPreviouslyMapped(QualType QT) const {
1156 const SharingMapTy &StackElem = getTopOfStack();
1157 return StackElem.MappedClassesQualTypes.contains(V: QT);
1158 }
1159
1160 /// Adds global declare target to the parent target region.
1161 void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) {
1162 assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
1163 E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link &&
1164 "Expected declare target link global.");
1165 for (auto &Elem : *this) {
1166 if (isOpenMPTargetExecutionDirective(DKind: Elem.Directive)) {
1167 Elem.DeclareTargetLinkVarDecls.push_back(Elt: E);
1168 return;
1169 }
1170 }
1171 }
1172
1173 /// Returns the list of globals with declare target link if current directive
1174 /// is target.
1175 ArrayRef<DeclRefExpr *> getLinkGlobals() const {
1176 assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) &&
1177 "Expected target executable directive.");
1178 return getTopOfStack().DeclareTargetLinkVarDecls;
1179 }
1180
1181 /// Adds list of allocators expressions.
1182 void addInnerAllocatorExpr(Expr *E) {
1183 getTopOfStack().InnerUsedAllocators.push_back(Elt: E);
1184 }
1185 /// Return list of used allocators.
1186 ArrayRef<Expr *> getInnerAllocators() const {
1187 return getTopOfStack().InnerUsedAllocators;
1188 }
1189 /// Marks the declaration as implicitly firstprivate nin the task-based
1190 /// regions.
1191 void addImplicitTaskFirstprivate(unsigned Level, Decl *D) {
1192 getStackElemAtLevel(Level).ImplicitTaskFirstprivates.insert(V: D);
1193 }
1194 /// Checks if the decl is implicitly firstprivate in the task-based region.
1195 bool isImplicitTaskFirstprivate(Decl *D) const {
1196 return getTopOfStack().ImplicitTaskFirstprivates.contains(V: D);
1197 }
1198
1199 /// Marks decl as used in uses_allocators clause as the allocator.
1200 void addUsesAllocatorsDecl(const Decl *D, UsesAllocatorsDeclKind Kind) {
1201 getTopOfStack().UsesAllocatorsDecls.try_emplace(Key: D, Args&: Kind);
1202 }
1203 /// Checks if specified decl is used in uses allocator clause as the
1204 /// allocator.
1205 std::optional<UsesAllocatorsDeclKind>
1206 isUsesAllocatorsDecl(unsigned Level, const Decl *D) const {
1207 const SharingMapTy &StackElem = getTopOfStack();
1208 auto I = StackElem.UsesAllocatorsDecls.find(Val: D);
1209 if (I == StackElem.UsesAllocatorsDecls.end())
1210 return std::nullopt;
1211 return I->getSecond();
1212 }
1213 std::optional<UsesAllocatorsDeclKind>
1214 isUsesAllocatorsDecl(const Decl *D) const {
1215 const SharingMapTy &StackElem = getTopOfStack();
1216 auto I = StackElem.UsesAllocatorsDecls.find(Val: D);
1217 if (I == StackElem.UsesAllocatorsDecls.end())
1218 return std::nullopt;
1219 return I->getSecond();
1220 }
1221
1222 void addDeclareMapperVarRef(Expr *Ref) {
1223 SharingMapTy &StackElem = getTopOfStack();
1224 StackElem.DeclareMapperVar = Ref;
1225 }
1226 const Expr *getDeclareMapperVarRef() const {
1227 const SharingMapTy *Top = getTopOfStackOrNull();
1228 return Top ? Top->DeclareMapperVar : nullptr;
1229 }
1230
1231 /// Add a new iterator variable.
1232 void addIteratorVarDecl(VarDecl *VD) {
1233 SharingMapTy &StackElem = getTopOfStack();
1234 StackElem.IteratorVarDecls.push_back(Elt: VD->getCanonicalDecl());
1235 }
1236 /// Check if variable declaration is an iterator VarDecl.
1237 bool isIteratorVarDecl(const VarDecl *VD) const {
1238 const SharingMapTy *Top = getTopOfStackOrNull();
1239 if (!Top)
1240 return false;
1241
1242 return llvm::is_contained(Range: Top->IteratorVarDecls, Element: VD->getCanonicalDecl());
1243 }
1244 /// get captured field from ImplicitDefaultFirstprivateFDs
1245 VarDecl *getImplicitFDCapExprDecl(const FieldDecl *FD) const {
1246 const_iterator I = begin();
1247 const_iterator EndI = end();
1248 size_t StackLevel = getStackSize();
1249 for (; I != EndI; ++I) {
1250 if (I->DefaultAttr == DSA_firstprivate || I->DefaultAttr == DSA_private)
1251 break;
1252 StackLevel--;
1253 }
1254 assert((StackLevel > 0 && I != EndI) || (StackLevel == 0 && I == EndI));
1255 if (I == EndI)
1256 return nullptr;
1257 for (const auto &IFD : I->ImplicitDefaultFirstprivateFDs)
1258 if (IFD.FD == FD && IFD.StackLevel == StackLevel)
1259 return IFD.VD;
1260 return nullptr;
1261 }
1262 /// Check if capture decl is field captured in ImplicitDefaultFirstprivateFDs
1263 bool isImplicitDefaultFirstprivateFD(VarDecl *VD) const {
1264 const_iterator I = begin();
1265 const_iterator EndI = end();
1266 for (; I != EndI; ++I)
1267 if (I->DefaultAttr == DSA_firstprivate || I->DefaultAttr == DSA_private)
1268 break;
1269 if (I == EndI)
1270 return false;
1271 for (const auto &IFD : I->ImplicitDefaultFirstprivateFDs)
1272 if (IFD.VD == VD)
1273 return true;
1274 return false;
1275 }
1276 /// Store capture FD info in ImplicitDefaultFirstprivateFDs
1277 void addImplicitDefaultFirstprivateFD(const FieldDecl *FD, VarDecl *VD) {
1278 iterator I = begin();
1279 const_iterator EndI = end();
1280 size_t StackLevel = getStackSize();
1281 for (; I != EndI; ++I) {
1282 if (I->DefaultAttr == DSA_private || I->DefaultAttr == DSA_firstprivate) {
1283 I->ImplicitDefaultFirstprivateFDs.emplace_back(Args&: FD, Args&: StackLevel, Args&: VD);
1284 break;
1285 }
1286 StackLevel--;
1287 }
1288 assert((StackLevel > 0 && I != EndI) || (StackLevel == 0 && I == EndI));
1289 }
1290 void setOrderedToBlockAssociated() {
1291 assert(getCurrentDirective() == OMPD_ordered_standalone);
1292 getTopOfStack().Directive = OMPD_ordered_blockassoc;
1293 }
1294};
1295
1296bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) {
1297 return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind);
1298}
1299
1300bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) {
1301 return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(Kind: DKind) ||
1302 DKind == OMPD_unknown;
1303}
1304
1305} // namespace
1306
1307static const Expr *getExprAsWritten(const Expr *E) {
1308 if (const auto *FE = dyn_cast<FullExpr>(Val: E))
1309 E = FE->getSubExpr();
1310
1311 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(Val: E))
1312 E = MTE->getSubExpr();
1313
1314 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(Val: E))
1315 E = Binder->getSubExpr();
1316
1317 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(Val: E))
1318 E = ICE->getSubExprAsWritten();
1319 return E->IgnoreParens();
1320}
1321
1322static Expr *getExprAsWritten(Expr *E) {
1323 return const_cast<Expr *>(getExprAsWritten(E: const_cast<const Expr *>(E)));
1324}
1325
1326static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
1327 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(Val: D))
1328 if (const auto *ME = dyn_cast<MemberExpr>(Val: getExprAsWritten(E: CED->getInit())))
1329 D = ME->getMemberDecl();
1330
1331 D = cast<ValueDecl>(Val: D->getCanonicalDecl());
1332 return D;
1333}
1334
1335static ValueDecl *getCanonicalDecl(ValueDecl *D) {
1336 return const_cast<ValueDecl *>(
1337 getCanonicalDecl(D: const_cast<const ValueDecl *>(D)));
1338}
1339
1340static std::string getOpenMPClauseNameForDiag(OpenMPClauseKind C) {
1341 if (C == OMPC_threadprivate)
1342 return getOpenMPClauseName(C).str() + " or thread local";
1343 return getOpenMPClauseName(C).str();
1344}
1345
1346DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter,
1347 ValueDecl *D) const {
1348 D = getCanonicalDecl(D);
1349 auto *VD = dyn_cast<VarDecl>(Val: D);
1350 const auto *FD = dyn_cast<FieldDecl>(Val: D);
1351 DSAVarData DVar;
1352 if (Iter == end()) {
1353 // A tuple-like binding has the storage of the variable it decomposes.
1354 const VarDecl *StorageVD = VD;
1355 if (const auto *BD = dyn_cast<BindingDecl>(Val: D); BD && BD->getHoldingVar())
1356 StorageVD = BD->getDecomposedDecl();
1357 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1358 // in a region but not in construct]
1359 // File-scope or namespace-scope variables referenced in called routines
1360 // in the region are shared unless they appear in a threadprivate
1361 // directive.
1362 if (StorageVD && !StorageVD->isFunctionOrMethodVarDecl() &&
1363 !isa<ParmVarDecl>(Val: StorageVD))
1364 DVar.CKind = OMPC_shared;
1365
1366 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
1367 // in a region but not in construct]
1368 // Variables with static storage duration that are declared in called
1369 // routines in the region are shared.
1370 if (StorageVD && StorageVD->hasGlobalStorage())
1371 DVar.CKind = OMPC_shared;
1372
1373 // Non-static data members are shared by default.
1374 if (FD)
1375 DVar.CKind = OMPC_shared;
1376
1377 return DVar;
1378 }
1379
1380 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1381 // in a Construct, C/C++, predetermined, p.1]
1382 // Variables with automatic storage duration that are declared in a scope
1383 // inside the construct are private.
1384 if (VD && isOpenMPLocal(D: VD, Iter) && VD->isLocalVarDecl() &&
1385 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
1386 DVar.CKind = OMPC_private;
1387 return DVar;
1388 }
1389
1390 DVar.DKind = Iter->Directive;
1391 // Explicitly specified attributes and local variables with predetermined
1392 // attributes.
1393 if (Iter->SharingMap.count(Val: D)) {
1394 const DSAInfo &Data = Iter->SharingMap.lookup(Val: D);
1395 DVar.RefExpr = Data.RefExpr.getPointer();
1396 DVar.PrivateCopy = Data.PrivateCopy;
1397 DVar.CKind = Data.Attributes;
1398 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1399 DVar.Modifier = Data.Modifier;
1400 DVar.AppliedToPointee = Data.AppliedToPointee;
1401 // For BindingDecls with OMPC_unknown, fall through to implicit DSA logic
1402 // instead of treating them as explicitly specified.
1403 if (!(isa<BindingDecl>(Val: D) && DVar.CKind == OMPC_unknown)) {
1404 return DVar;
1405 }
1406 }
1407
1408 DefaultDataSharingAttributes IterDA = Iter->DefaultAttr;
1409 switch (Iter->DefaultVCAttr) {
1410 case DSA_VC_aggregate:
1411 if (!D->getType()->isAggregateType())
1412 IterDA = DSA_none;
1413 break;
1414 case DSA_VC_pointer:
1415 if (!D->getType()->isPointerType())
1416 IterDA = DSA_none;
1417 break;
1418 case DSA_VC_scalar:
1419 if (!D->getType()->isScalarType())
1420 IterDA = DSA_none;
1421 break;
1422 case DSA_VC_all:
1423 break;
1424 }
1425
1426 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1427 // in a Construct, C/C++, implicitly determined, p.1]
1428 // In a parallel or task construct, the data-sharing attributes of these
1429 // variables are determined by the default clause, if present.
1430 switch (IterDA) {
1431 case DSA_shared:
1432 DVar.CKind = OMPC_shared;
1433 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1434 return DVar;
1435 case DSA_none:
1436 return DVar;
1437 case DSA_firstprivate:
1438 if (VD && VD->getStorageDuration() == SD_Static &&
1439 VD->getDeclContext()->isFileContext()) {
1440 DVar.CKind = OMPC_unknown;
1441 } else {
1442 DVar.CKind = OMPC_firstprivate;
1443 }
1444 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1445 return DVar;
1446 case DSA_private:
1447 // each variable with static storage duration that is declared
1448 // in a namespace or global scope and referenced in the construct,
1449 // and that does not have a predetermined data-sharing attribute
1450 if (VD && VD->getStorageDuration() == SD_Static &&
1451 VD->getDeclContext()->isFileContext()) {
1452 DVar.CKind = OMPC_unknown;
1453 } else {
1454 DVar.CKind = OMPC_private;
1455 }
1456 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1457 return DVar;
1458 case DSA_unspecified:
1459 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1460 // in a Construct, implicitly determined, p.2]
1461 // In a parallel construct, if no default clause is present, these
1462 // variables are shared.
1463 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1464 if ((isOpenMPParallelDirective(DKind: DVar.DKind) &&
1465 !isOpenMPTaskLoopDirective(DKind: DVar.DKind)) ||
1466 isOpenMPTeamsDirective(DKind: DVar.DKind)) {
1467 DVar.CKind = OMPC_shared;
1468 return DVar;
1469 }
1470
1471 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1472 // in a Construct, implicitly determined, p.4]
1473 // In a task construct, if no default clause is present, a variable that in
1474 // the enclosing context is determined to be shared by all implicit tasks
1475 // bound to the current team is shared.
1476 if (isOpenMPTaskingDirective(Kind: DVar.DKind)) {
1477 DSAVarData DVarTemp;
1478 const_iterator I = Iter, E = end();
1479 do {
1480 ++I;
1481 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
1482 // Referenced in a Construct, implicitly determined, p.6]
1483 // In a task construct, if no default clause is present, a variable
1484 // whose data-sharing attribute is not determined by the rules above is
1485 // firstprivate.
1486 DVarTemp = getDSA(Iter&: I, D);
1487 if (DVarTemp.CKind != OMPC_shared) {
1488 DVar.RefExpr = nullptr;
1489 DVar.CKind = OMPC_firstprivate;
1490 return DVar;
1491 }
1492 } while (I != E && !isImplicitTaskingRegion(DKind: I->Directive));
1493 DVar.CKind =
1494 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
1495 return DVar;
1496 }
1497 }
1498 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1499 // in a Construct, implicitly determined, p.3]
1500 // For constructs other than task, if no default clause is present, these
1501 // variables inherit their data-sharing attributes from the enclosing
1502 // context.
1503 return getDSA(Iter&: ++Iter, D);
1504}
1505
1506const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
1507 const Expr *NewDE) {
1508 assert(!isStackEmpty() && "Data sharing attributes stack is empty");
1509 D = getCanonicalDecl(D);
1510 SharingMapTy &StackElem = getTopOfStack();
1511 auto [It, Inserted] = StackElem.AlignedMap.try_emplace(Key: D, Args&: NewDE);
1512 if (Inserted) {
1513 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
1514 return nullptr;
1515 }
1516 assert(It->second && "Unexpected nullptr expr in the aligned map");
1517 return It->second;
1518}
1519
1520const Expr *DSAStackTy::addUniqueNontemporal(const ValueDecl *D,
1521 const Expr *NewDE) {
1522 assert(!isStackEmpty() && "Data sharing attributes stack is empty");
1523 D = getCanonicalDecl(D);
1524 SharingMapTy &StackElem = getTopOfStack();
1525 auto [It, Inserted] = StackElem.NontemporalMap.try_emplace(Key: D, Args&: NewDE);
1526 if (Inserted) {
1527 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
1528 return nullptr;
1529 }
1530 assert(It->second && "Unexpected nullptr expr in the aligned map");
1531 return It->second;
1532}
1533
1534void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
1535 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1536 D = getCanonicalDecl(D);
1537 SharingMapTy &StackElem = getTopOfStack();
1538 StackElem.LCVMap.try_emplace(
1539 Key: D, Args: LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
1540}
1541
1542const DSAStackTy::LCDeclInfo
1543DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
1544 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1545 D = getCanonicalDecl(D);
1546 const SharingMapTy &StackElem = getTopOfStack();
1547 auto It = StackElem.LCVMap.find(Val: D);
1548 if (It != StackElem.LCVMap.end())
1549 return It->second;
1550 return {0, nullptr};
1551}
1552
1553const DSAStackTy::LCDeclInfo
1554DSAStackTy::isLoopControlVariable(const ValueDecl *D, unsigned Level) const {
1555 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1556 D = getCanonicalDecl(D);
1557 for (unsigned I = Level + 1; I > 0; --I) {
1558 const SharingMapTy &StackElem = getStackElemAtLevel(Level: I - 1);
1559 auto It = StackElem.LCVMap.find(Val: D);
1560 if (It != StackElem.LCVMap.end())
1561 return It->second;
1562 }
1563 return {0, nullptr};
1564}
1565
1566const DSAStackTy::LCDeclInfo
1567DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
1568 const SharingMapTy *Parent = getSecondOnStackOrNull();
1569 assert(Parent && "Data-sharing attributes stack is empty");
1570 D = getCanonicalDecl(D);
1571 auto It = Parent->LCVMap.find(Val: D);
1572 if (It != Parent->LCVMap.end())
1573 return It->second;
1574 return {0, nullptr};
1575}
1576
1577const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
1578 const SharingMapTy *Parent = getSecondOnStackOrNull();
1579 assert(Parent && "Data-sharing attributes stack is empty");
1580 if (Parent->LCVMap.size() < I)
1581 return nullptr;
1582 for (const auto &Pair : Parent->LCVMap)
1583 if (Pair.second.first == I)
1584 return Pair.first;
1585 return nullptr;
1586}
1587
1588void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
1589 DeclRefExpr *PrivateCopy, unsigned Modifier,
1590 bool AppliedToPointee) {
1591 D = getCanonicalDecl(D);
1592 if (A == OMPC_threadprivate) {
1593 DSAInfo &Data = Threadprivates[D];
1594 Data.Attributes = A;
1595 Data.RefExpr.setPointer(E);
1596 Data.PrivateCopy = nullptr;
1597 Data.Modifier = Modifier;
1598 } else if (A == OMPC_groupprivate) {
1599 DSAInfo &Data = Groupprivates[D];
1600 Data.Attributes = A;
1601 Data.RefExpr.setPointer(E);
1602 Data.PrivateCopy = nullptr;
1603 Data.Modifier = Modifier;
1604 } else {
1605 DSAInfo &Data = getTopOfStack().SharingMap[D];
1606 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
1607 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
1608 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
1609 (isLoopControlVariable(D).first && A == OMPC_private));
1610 Data.Modifier = Modifier;
1611 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
1612 Data.RefExpr.setInt(/*IntVal=*/true);
1613 return;
1614 }
1615 const bool IsLastprivate =
1616 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
1617 Data.Attributes = A;
1618 Data.RefExpr.setPointerAndInt(PtrVal: E, IntVal: IsLastprivate);
1619 Data.PrivateCopy = PrivateCopy;
1620 Data.AppliedToPointee = AppliedToPointee;
1621 if (PrivateCopy) {
1622 DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()];
1623 Data.Modifier = Modifier;
1624 Data.Attributes = A;
1625 Data.RefExpr.setPointerAndInt(PtrVal: PrivateCopy, IntVal: IsLastprivate);
1626 Data.PrivateCopy = nullptr;
1627 Data.AppliedToPointee = AppliedToPointee;
1628 }
1629 }
1630}
1631
1632/// Build a variable declaration for OpenMP loop iteration variable.
1633static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
1634 StringRef Name, const AttrVec *Attrs = nullptr,
1635 DeclRefExpr *OrigRef = nullptr) {
1636 DeclContext *DC = SemaRef.CurContext;
1637 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
1638 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(T: Type, Loc);
1639 auto *Decl =
1640 VarDecl::Create(C&: SemaRef.Context, DC, StartLoc: Loc, IdLoc: Loc, Id: II, T: Type, TInfo, S: SC_None);
1641 if (Attrs) {
1642 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
1643 I != E; ++I)
1644 Decl->addAttr(A: *I);
1645 }
1646 Decl->setImplicit();
1647 if (OrigRef) {
1648 Decl->addAttr(
1649 A: OMPReferencedVarAttr::CreateImplicit(Ctx&: SemaRef.Context, Ref: OrigRef));
1650 }
1651 return Decl;
1652}
1653
1654static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
1655 SourceLocation Loc,
1656 bool RefersToCapture = false) {
1657 D->setReferenced();
1658 D->markUsed(C&: S.Context);
1659 return DeclRefExpr::Create(Context: S.getASTContext(), QualifierLoc: NestedNameSpecifierLoc(),
1660 TemplateKWLoc: SourceLocation(), D, RefersToEnclosingVariableOrCapture: RefersToCapture, NameLoc: Loc, T: Ty,
1661 VK: VK_LValue);
1662}
1663
1664void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1665 BinaryOperatorKind BOK) {
1666 D = getCanonicalDecl(D);
1667 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1668 assert(
1669 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1670 "Additional reduction info may be specified only for reduction items.");
1671 ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1672 assert(ReductionData.ReductionRange.isInvalid() &&
1673 (getTopOfStack().Directive == OMPD_taskgroup ||
1674 ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
1675 isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
1676 !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
1677 "Additional reduction info may be specified only once for reduction "
1678 "items.");
1679 ReductionData.set(BO: BOK, RR: SR);
1680 Expr *&TaskgroupReductionRef = getTopOfStack().TaskgroupReductionRef;
1681 if (!TaskgroupReductionRef) {
1682 VarDecl *VD = buildVarDecl(SemaRef, Loc: SR.getBegin(),
1683 Type: SemaRef.Context.VoidPtrTy, Name: ".task_red.");
1684 TaskgroupReductionRef =
1685 buildDeclRefExpr(S&: SemaRef, D: VD, Ty: SemaRef.Context.VoidPtrTy, Loc: SR.getBegin());
1686 }
1687}
1688
1689void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1690 const Expr *ReductionRef) {
1691 D = getCanonicalDecl(D);
1692 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1693 assert(
1694 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1695 "Additional reduction info may be specified only for reduction items.");
1696 ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1697 assert(ReductionData.ReductionRange.isInvalid() &&
1698 (getTopOfStack().Directive == OMPD_taskgroup ||
1699 ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
1700 isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
1701 !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
1702 "Additional reduction info may be specified only once for reduction "
1703 "items.");
1704 ReductionData.set(RefExpr: ReductionRef, RR: SR);
1705 Expr *&TaskgroupReductionRef = getTopOfStack().TaskgroupReductionRef;
1706 if (!TaskgroupReductionRef) {
1707 VarDecl *VD = buildVarDecl(SemaRef, Loc: SR.getBegin(),
1708 Type: SemaRef.Context.VoidPtrTy, Name: ".task_red.");
1709 TaskgroupReductionRef =
1710 buildDeclRefExpr(S&: SemaRef, D: VD, Ty: SemaRef.Context.VoidPtrTy, Loc: SR.getBegin());
1711 }
1712}
1713
1714const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1715 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
1716 Expr *&TaskgroupDescriptor) const {
1717 D = getCanonicalDecl(D);
1718 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1719 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) {
1720 const DSAInfo &Data = I->SharingMap.lookup(Val: D);
1721 if (Data.Attributes != OMPC_reduction ||
1722 Data.Modifier != OMPC_REDUCTION_task)
1723 continue;
1724 const ReductionData &ReductionData = I->ReductionMap.lookup(Val: D);
1725 if (!ReductionData.ReductionOp ||
1726 isa<const Expr *>(Val: ReductionData.ReductionOp))
1727 return DSAVarData();
1728 SR = ReductionData.ReductionRange;
1729 BOK = cast<ReductionData::BOKPtrType>(Val: ReductionData.ReductionOp);
1730 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1731 "expression for the descriptor is not "
1732 "set.");
1733 TaskgroupDescriptor = I->TaskgroupReductionRef;
1734 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(),
1735 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task,
1736 /*AppliedToPointee=*/false);
1737 }
1738 return DSAVarData();
1739}
1740
1741const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1742 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
1743 Expr *&TaskgroupDescriptor) const {
1744 D = getCanonicalDecl(D);
1745 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1746 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) {
1747 const DSAInfo &Data = I->SharingMap.lookup(Val: D);
1748 if (Data.Attributes != OMPC_reduction ||
1749 Data.Modifier != OMPC_REDUCTION_task)
1750 continue;
1751 const ReductionData &ReductionData = I->ReductionMap.lookup(Val: D);
1752 if (!ReductionData.ReductionOp ||
1753 !isa<const Expr *>(Val: ReductionData.ReductionOp))
1754 return DSAVarData();
1755 SR = ReductionData.ReductionRange;
1756 ReductionRef = cast<const Expr *>(Val: ReductionData.ReductionOp);
1757 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1758 "expression for the descriptor is not "
1759 "set.");
1760 TaskgroupDescriptor = I->TaskgroupReductionRef;
1761 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(),
1762 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task,
1763 /*AppliedToPointee=*/false);
1764 }
1765 return DSAVarData();
1766}
1767
1768bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const {
1769 D = D->getCanonicalDecl();
1770 for (const_iterator E = end(); I != E; ++I) {
1771 if (isImplicitOrExplicitTaskingRegion(DKind: I->Directive) ||
1772 isOpenMPTargetExecutionDirective(DKind: I->Directive)) {
1773 if (I->CurScope) {
1774 Scope *TopScope = I->CurScope->getParent();
1775 Scope *CurScope = getCurScope();
1776 while (CurScope && CurScope != TopScope && !CurScope->isDeclScope(D))
1777 CurScope = CurScope->getParent();
1778 return CurScope != TopScope;
1779 }
1780 for (DeclContext *DC = D->getDeclContext(); DC; DC = DC->getParent())
1781 if (I->Context == DC)
1782 return true;
1783 return false;
1784 }
1785 }
1786 return false;
1787}
1788
1789static bool isConstNotMutableType(Sema &SemaRef, QualType Type,
1790 bool AcceptIfMutable = true,
1791 bool *IsClassType = nullptr) {
1792 ASTContext &Context = SemaRef.getASTContext();
1793 Type = Type.getNonReferenceType().getCanonicalType();
1794 bool IsConstant = Type.isConstant(Ctx: Context);
1795 Type = Context.getBaseElementType(QT: Type);
1796 const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus
1797 ? Type->getAsCXXRecordDecl()
1798 : nullptr;
1799 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(Val: RD))
1800 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
1801 RD = CTD->getTemplatedDecl();
1802 if (IsClassType)
1803 *IsClassType = RD;
1804 return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD &&
1805 RD->hasDefinition() && RD->hasMutableFields());
1806}
1807
1808static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D,
1809 QualType Type, OpenMPClauseKind CKind,
1810 SourceLocation ELoc,
1811 bool AcceptIfMutable = true,
1812 bool ListItemNotVar = false) {
1813 ASTContext &Context = SemaRef.getASTContext();
1814 bool IsClassType;
1815 if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, IsClassType: &IsClassType)) {
1816 unsigned Diag = ListItemNotVar ? diag::err_omp_const_list_item
1817 : IsClassType ? diag::err_omp_const_not_mutable_variable
1818 : diag::err_omp_const_variable;
1819 SemaRef.Diag(Loc: ELoc, DiagID: Diag) << getOpenMPClauseNameForDiag(C: CKind);
1820 if (!ListItemNotVar && D) {
1821 const VarDecl *VD = dyn_cast<VarDecl>(Val: D);
1822 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
1823 VarDecl::DeclarationOnly;
1824 SemaRef.Diag(Loc: D->getLocation(),
1825 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1826 << D;
1827 }
1828 return true;
1829 }
1830 return false;
1831}
1832
1833const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1834 bool FromParent) {
1835 D = getCanonicalDecl(D);
1836 DSAVarData DVar;
1837
1838 auto *VD = dyn_cast<VarDecl>(Val: D);
1839 auto TI = Threadprivates.find(Val: D);
1840 if (TI != Threadprivates.end()) {
1841 DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
1842 DVar.CKind = OMPC_threadprivate;
1843 DVar.Modifier = TI->getSecond().Modifier;
1844 return DVar;
1845 }
1846 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
1847 DVar.RefExpr = buildDeclRefExpr(
1848 S&: SemaRef, D: VD, Ty: D->getType().getNonReferenceType(),
1849 Loc: VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1850 DVar.CKind = OMPC_threadprivate;
1851 addDSA(D, E: DVar.RefExpr, A: OMPC_threadprivate);
1852 return DVar;
1853 }
1854 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1855 // in a Construct, C/C++, predetermined, p.1]
1856 // Variables appearing in threadprivate directives are threadprivate.
1857 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
1858 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1859 SemaRef.getLangOpts().OpenMPUseTLS &&
1860 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
1861 (VD && VD->getStorageClass() == SC_Register &&
1862 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
1863 DVar.RefExpr = buildDeclRefExpr(
1864 S&: SemaRef, D: VD, Ty: D->getType().getNonReferenceType(), Loc: D->getLocation());
1865 DVar.CKind = OMPC_threadprivate;
1866 addDSA(D, E: DVar.RefExpr, A: OMPC_threadprivate);
1867 return DVar;
1868 }
1869 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
1870 VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
1871 !isLoopControlVariable(D).first) {
1872 const_iterator IterTarget =
1873 std::find_if(first: begin(), last: end(), pred: [](const SharingMapTy &Data) {
1874 return isOpenMPTargetExecutionDirective(DKind: Data.Directive);
1875 });
1876 if (IterTarget != end()) {
1877 const_iterator ParentIterTarget = IterTarget + 1;
1878 for (const_iterator Iter = begin(); Iter != ParentIterTarget; ++Iter) {
1879 if (isOpenMPLocal(D: VD, I: Iter)) {
1880 DVar.RefExpr =
1881 buildDeclRefExpr(S&: SemaRef, D: VD, Ty: D->getType().getNonReferenceType(),
1882 Loc: D->getLocation());
1883 DVar.CKind = OMPC_threadprivate;
1884 return DVar;
1885 }
1886 }
1887 if (!isClauseParsingMode() || IterTarget != begin()) {
1888 auto DSAIter = IterTarget->SharingMap.find(Val: D);
1889 if (DSAIter != IterTarget->SharingMap.end() &&
1890 isOpenMPPrivate(Kind: DSAIter->getSecond().Attributes)) {
1891 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1892 DVar.CKind = OMPC_threadprivate;
1893 return DVar;
1894 }
1895 const_iterator End = end();
1896 if (!SemaRef.OpenMP().isOpenMPCapturedByRef(
1897 D, Level: std::distance(first: ParentIterTarget, last: End),
1898 /*OpenMPCaptureLevel=*/0)) {
1899 DVar.RefExpr =
1900 buildDeclRefExpr(S&: SemaRef, D: VD, Ty: D->getType().getNonReferenceType(),
1901 Loc: IterTarget->ConstructLoc);
1902 DVar.CKind = OMPC_threadprivate;
1903 return DVar;
1904 }
1905 }
1906 }
1907 }
1908
1909 if (isStackEmpty())
1910 // Not in OpenMP execution region and top scope was already checked.
1911 return DVar;
1912
1913 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1914 // in a Construct, C/C++, predetermined, p.4]
1915 // Static data members are shared.
1916 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1917 // in a Construct, C/C++, predetermined, p.7]
1918 // Variables with static storage duration that are declared in a scope
1919 // inside the construct are shared.
1920 if (VD && VD->isStaticDataMember()) {
1921 // Check for explicitly specified attributes.
1922 const_iterator I = begin();
1923 const_iterator EndI = end();
1924 if (FromParent && I != EndI)
1925 ++I;
1926 if (I != EndI) {
1927 auto It = I->SharingMap.find(Val: D);
1928 if (It != I->SharingMap.end()) {
1929 const DSAInfo &Data = It->getSecond();
1930 DVar.RefExpr = Data.RefExpr.getPointer();
1931 DVar.PrivateCopy = Data.PrivateCopy;
1932 DVar.CKind = Data.Attributes;
1933 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1934 DVar.DKind = I->Directive;
1935 DVar.Modifier = Data.Modifier;
1936 DVar.AppliedToPointee = Data.AppliedToPointee;
1937 return DVar;
1938 }
1939 }
1940
1941 DVar.CKind = OMPC_shared;
1942 return DVar;
1943 }
1944
1945 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
1946 // The predetermined shared attribute for const-qualified types having no
1947 // mutable members was removed after OpenMP 3.1.
1948 if (SemaRef.LangOpts.OpenMP <= 31) {
1949 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1950 // in a Construct, C/C++, predetermined, p.6]
1951 // Variables with const qualified type having no mutable member are
1952 // shared.
1953 if (isConstNotMutableType(SemaRef, Type: D->getType())) {
1954 // Variables with const-qualified type having no mutable member may be
1955 // listed in a firstprivate clause, even if they are static data members.
1956 DSAVarData DVarTemp = hasInnermostDSA(
1957 D,
1958 CPred: [](OpenMPClauseKind C, bool) {
1959 return C == OMPC_firstprivate || C == OMPC_shared;
1960 },
1961 DPred: MatchesAlways, FromParent);
1962 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
1963 return DVarTemp;
1964
1965 DVar.CKind = OMPC_shared;
1966 return DVar;
1967 }
1968 }
1969
1970 // Explicitly specified attributes and local variables with predetermined
1971 // attributes.
1972 const_iterator I = begin();
1973 const_iterator EndI = end();
1974 if (FromParent && I != EndI)
1975 ++I;
1976 if (I == EndI)
1977 return DVar;
1978 auto It = I->SharingMap.find(Val: D);
1979 if (It != I->SharingMap.end()) {
1980 const DSAInfo &Data = It->getSecond();
1981 DVar.RefExpr = Data.RefExpr.getPointer();
1982 DVar.PrivateCopy = Data.PrivateCopy;
1983 DVar.CKind = Data.Attributes;
1984 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1985 DVar.DKind = I->Directive;
1986 DVar.Modifier = Data.Modifier;
1987 DVar.AppliedToPointee = Data.AppliedToPointee;
1988 }
1989
1990 return DVar;
1991}
1992
1993const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1994 bool FromParent) const {
1995 if (isStackEmpty()) {
1996 const_iterator I;
1997 return getDSA(Iter&: I, D);
1998 }
1999 D = getCanonicalDecl(D);
2000 const_iterator StartI = begin();
2001 const_iterator EndI = end();
2002 if (FromParent && StartI != EndI)
2003 ++StartI;
2004 return getDSA(Iter&: StartI, D);
2005}
2006
2007const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
2008 unsigned Level) const {
2009 if (getStackSize() <= Level)
2010 return DSAVarData();
2011 D = getCanonicalDecl(D);
2012 const_iterator StartI = std::next(x: begin(), n: getStackSize() - 1 - Level);
2013 return getDSA(Iter&: StartI, D);
2014}
2015
2016const DSAStackTy::DSAVarData
2017DSAStackTy::hasDSA(ValueDecl *D,
2018 const llvm::function_ref<bool(OpenMPClauseKind, bool,
2019 DefaultDataSharingAttributes)>
2020 CPred,
2021 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
2022 bool FromParent) const {
2023 if (isStackEmpty())
2024 return {};
2025 D = getCanonicalDecl(D);
2026 const_iterator I = begin();
2027 const_iterator EndI = end();
2028 if (FromParent && I != EndI)
2029 ++I;
2030 for (; I != EndI; ++I) {
2031 if (!DPred(I->Directive) &&
2032 !isImplicitOrExplicitTaskingRegion(DKind: I->Directive))
2033 continue;
2034 const_iterator NewI = I;
2035 DSAVarData DVar = getDSA(Iter&: NewI, D);
2036 if (I == NewI && CPred(DVar.CKind, DVar.AppliedToPointee, I->DefaultAttr))
2037 return DVar;
2038 }
2039 return {};
2040}
2041
2042const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
2043 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
2044 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
2045 bool FromParent) const {
2046 if (isStackEmpty())
2047 return {};
2048 D = getCanonicalDecl(D);
2049 const_iterator StartI = begin();
2050 const_iterator EndI = end();
2051 if (FromParent && StartI != EndI)
2052 ++StartI;
2053 if (StartI == EndI || !DPred(StartI->Directive))
2054 return {};
2055 const_iterator NewI = StartI;
2056 DSAVarData DVar = getDSA(Iter&: NewI, D);
2057 return (NewI == StartI && CPred(DVar.CKind, DVar.AppliedToPointee))
2058 ? DVar
2059 : DSAVarData();
2060}
2061
2062bool DSAStackTy::hasExplicitDSA(
2063 const ValueDecl *D,
2064 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
2065 unsigned Level, bool NotLastprivate) const {
2066 if (getStackSize() <= Level)
2067 return false;
2068 D = getCanonicalDecl(D);
2069 const SharingMapTy &StackElem = getStackElemAtLevel(Level);
2070 auto I = StackElem.SharingMap.find(Val: D);
2071 if (I != StackElem.SharingMap.end() && I->getSecond().RefExpr.getPointer() &&
2072 CPred(I->getSecond().Attributes, I->getSecond().AppliedToPointee) &&
2073 (!NotLastprivate || !I->getSecond().RefExpr.getInt()))
2074 return true;
2075 // Check predetermined rules for the loop control variables.
2076 auto LI = StackElem.LCVMap.find(Val: D);
2077 if (LI != StackElem.LCVMap.end())
2078 return CPred(OMPC_private, /*AppliedToPointee=*/false);
2079 return false;
2080}
2081
2082bool DSAStackTy::hasExplicitDirective(
2083 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
2084 unsigned Level) const {
2085 if (getStackSize() <= Level)
2086 return false;
2087 const SharingMapTy &StackElem = getStackElemAtLevel(Level);
2088 return DPred(StackElem.Directive);
2089}
2090
2091bool DSAStackTy::hasDirective(
2092 const llvm::function_ref<bool(OpenMPDirectiveKind,
2093 const DeclarationNameInfo &, SourceLocation)>
2094 DPred,
2095 bool FromParent) const {
2096 // We look only in the enclosing region.
2097 size_t Skip = FromParent ? 2 : 1;
2098 for (const_iterator I = begin() + std::min(a: Skip, b: getStackSize()), E = end();
2099 I != E; ++I) {
2100 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
2101 return true;
2102 }
2103 return false;
2104}
2105
2106void SemaOpenMP::InitDataSharingAttributesStack() {
2107 VarDataSharingAttributesStack = new DSAStackTy(SemaRef);
2108}
2109
2110#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
2111
2112void SemaOpenMP::addRequiresDecl(OMPRequiresDecl *D) {
2113 DSAStack->addRequiresDecl(RD: D);
2114}
2115
2116ArrayRef<const OMPRequiresDecl *> SemaOpenMP::getRequiresDecls() const {
2117 return DSAStack->getRequiresDecls();
2118}
2119
2120void SemaOpenMP::pushOpenMPFunctionRegion() { DSAStack->pushFunction(); }
2121
2122void SemaOpenMP::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
2123 DSAStack->popFunction(OldFSI);
2124}
2125
2126static bool isOpenMPDeviceDelayedContext(Sema &S) {
2127 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsTargetDevice &&
2128 "Expected OpenMP device compilation.");
2129 return !S.OpenMP().isInOpenMPTargetExecutionDirective();
2130}
2131
2132namespace {
2133/// Status of the function emission on the host/device.
2134enum class FunctionEmissionStatus {
2135 Emitted,
2136 Discarded,
2137 Unknown,
2138};
2139} // anonymous namespace
2140
2141SemaBase::SemaDiagnosticBuilder
2142SemaOpenMP::diagIfOpenMPDeviceCode(SourceLocation Loc, unsigned DiagID,
2143 const FunctionDecl *FD) {
2144 assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsTargetDevice &&
2145 "Expected OpenMP device compilation.");
2146
2147 SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop;
2148 if (FD) {
2149 Sema::FunctionEmissionStatus FES = SemaRef.getEmissionStatus(Decl: FD);
2150 switch (FES) {
2151 case Sema::FunctionEmissionStatus::Emitted:
2152 Kind = SemaDiagnosticBuilder::K_Immediate;
2153 break;
2154 case Sema::FunctionEmissionStatus::Unknown:
2155 // TODO: We should always delay diagnostics here in case a target
2156 // region is in a function we do not emit. However, as the
2157 // current diagnostics are associated with the function containing
2158 // the target region and we do not emit that one, we would miss out
2159 // on diagnostics for the target region itself. We need to anchor
2160 // the diagnostics with the new generated function *or* ensure we
2161 // emit diagnostics associated with the surrounding function.
2162 Kind = isOpenMPDeviceDelayedContext(S&: SemaRef)
2163 ? SemaDiagnosticBuilder::K_Deferred
2164 : SemaDiagnosticBuilder::K_Immediate;
2165 break;
2166 case Sema::FunctionEmissionStatus::TemplateDiscarded:
2167 case Sema::FunctionEmissionStatus::OMPDiscarded:
2168 Kind = SemaDiagnosticBuilder::K_Nop;
2169 break;
2170 case Sema::FunctionEmissionStatus::CUDADiscarded:
2171 llvm_unreachable("CUDADiscarded unexpected in OpenMP device compilation");
2172 break;
2173 }
2174 }
2175
2176 return SemaDiagnosticBuilder(Kind, Loc, DiagID, FD, SemaRef);
2177}
2178
2179SemaBase::SemaDiagnosticBuilder
2180SemaOpenMP::diagIfOpenMPHostCode(SourceLocation Loc, unsigned DiagID,
2181 const FunctionDecl *FD) {
2182 assert(getLangOpts().OpenMP && !getLangOpts().OpenMPIsTargetDevice &&
2183 "Expected OpenMP host compilation.");
2184
2185 SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop;
2186 if (FD) {
2187 Sema::FunctionEmissionStatus FES = SemaRef.getEmissionStatus(Decl: FD);
2188 switch (FES) {
2189 case Sema::FunctionEmissionStatus::Emitted:
2190 Kind = SemaDiagnosticBuilder::K_Immediate;
2191 break;
2192 case Sema::FunctionEmissionStatus::Unknown:
2193 Kind = SemaDiagnosticBuilder::K_Deferred;
2194 break;
2195 case Sema::FunctionEmissionStatus::TemplateDiscarded:
2196 case Sema::FunctionEmissionStatus::OMPDiscarded:
2197 case Sema::FunctionEmissionStatus::CUDADiscarded:
2198 Kind = SemaDiagnosticBuilder::K_Nop;
2199 break;
2200 }
2201 }
2202
2203 return SemaDiagnosticBuilder(Kind, Loc, DiagID, FD, SemaRef);
2204}
2205
2206static OpenMPDefaultmapClauseKind
2207getVariableCategoryFromDecl(const LangOptions &LO, const ValueDecl *VD) {
2208 if (LO.OpenMP <= 45) {
2209 if (VD->getType().getNonReferenceType()->isScalarType())
2210 return OMPC_DEFAULTMAP_scalar;
2211 return OMPC_DEFAULTMAP_aggregate;
2212 }
2213 if (VD->getType().getNonReferenceType()->isAnyPointerType())
2214 return OMPC_DEFAULTMAP_pointer;
2215 if (VD->getType().getNonReferenceType()->isScalarType())
2216 return OMPC_DEFAULTMAP_scalar;
2217 return OMPC_DEFAULTMAP_aggregate;
2218}
2219
2220bool SemaOpenMP::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level,
2221 unsigned OpenMPCaptureLevel) const {
2222 assert(getLangOpts().OpenMP && "OpenMP is not allowed");
2223
2224 ASTContext &Ctx = getASTContext();
2225 bool IsByRef = true;
2226
2227 // Find the directive that is associated with the provided scope.
2228 D = cast<ValueDecl>(Val: D->getCanonicalDecl());
2229 QualType Ty = D->getType();
2230 if (const auto *BD = dyn_cast<BindingDecl>(Val: D))
2231 Ty = BD->getDecomposedDecl()->getType();
2232
2233 bool IsVariableUsedInMapClause = false;
2234 if (DSAStack->hasExplicitDirective(DPred: isOpenMPTargetExecutionDirective, Level)) {
2235 // This table summarizes how a given variable should be passed to the device
2236 // given its type and the clauses where it appears. This table is based on
2237 // the description in OpenMP 4.5 [2.10.4, target Construct] and
2238 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
2239 //
2240 // =========================================================================
2241 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
2242 // | |(tofrom:scalar)| | pvt | |has_dv_adr| |
2243 // =========================================================================
2244 // | scl | | | | - | | bycopy|
2245 // | scl | | - | x | - | - | bycopy|
2246 // | scl | | x | - | - | - | null |
2247 // | scl | x | | | - | | byref |
2248 // | scl | x | - | x | - | - | bycopy|
2249 // | scl | x | x | - | - | - | null |
2250 // | scl | | - | - | - | x | byref |
2251 // | scl | x | - | - | - | x | byref |
2252 //
2253 // | agg | n.a. | | | - | | byref |
2254 // | agg | n.a. | - | x | - | - | byref |
2255 // | agg | n.a. | x | - | - | - | null |
2256 // | agg | n.a. | - | - | - | x | byref |
2257 // | agg | n.a. | - | - | - | x[] | byref |
2258 //
2259 // | ptr | n.a. | | | - | | bycopy|
2260 // | ptr | n.a. | - | x | - | - | bycopy|
2261 // | ptr | n.a. | x | - | - | - | null |
2262 // | ptr | n.a. | - | - | - | x | byref |
2263 // | ptr | n.a. | - | - | - | x, x[] | bycopy|
2264 // | ptr | n.a. | - | - | - | x[] | bycopy|
2265 // | ptr | n.a. | - | - | x | | bycopy|
2266 // | ptr | n.a. | - | - | x | x | bycopy|
2267 // | ptr | n.a. | - | - | x | x[] | bycopy|
2268 // =========================================================================
2269 // Legend:
2270 // scl - scalar
2271 // ptr - pointer
2272 // agg - aggregate
2273 // x - applies
2274 // - - invalid in this combination
2275 // [] - mapped with an array section
2276 // byref - should be mapped by reference
2277 // byval - should be mapped by value
2278 // null - initialize a local variable to null on the device
2279 //
2280 // Observations:
2281 // - All scalar declarations that show up in a map clause have to be passed
2282 // by reference, because they may have been mapped in the enclosing data
2283 // environment.
2284 // - If the scalar value does not fit the size of uintptr, it has to be
2285 // passed by reference, regardless the result in the table above.
2286 // - For pointers mapped by value that have either an implicit map or an
2287 // array section, the runtime library may pass the NULL value to the
2288 // device instead of the value passed to it by the compiler.
2289 // - If both a pointer and a dereference of it are mapped, then the pointer
2290 // should be passed by reference.
2291
2292 if (Ty->isReferenceType())
2293 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
2294
2295 // Locate map clauses and see if the variable being captured is mapped by
2296 // itself, or referred to, in any of those clauses. Here we only care about
2297 // variables, not fields, because fields are part of aggregates.
2298 bool IsVariableAssociatedWithSection = false;
2299 bool IsVariableItselfMapped = false;
2300
2301 DSAStack->checkMappableExprComponentListsForDeclAtLevel(
2302 VD: D, Level,
2303 Check: [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection,
2304 &IsVariableItselfMapped,
2305 D](OMPClauseMappableExprCommon::MappableExprComponentListRef
2306 MapExprComponents,
2307 OpenMPClauseKind WhereFoundClauseKind) {
2308 // Both map and has_device_addr clauses information influences how a
2309 // variable is captured. E.g. is_device_ptr does not require changing
2310 // the default behavior.
2311 if (WhereFoundClauseKind != OMPC_map &&
2312 WhereFoundClauseKind != OMPC_has_device_addr)
2313 return false;
2314
2315 auto EI = MapExprComponents.rbegin();
2316 auto EE = MapExprComponents.rend();
2317
2318 assert(EI != EE && "Invalid map expression!");
2319
2320 if (isa<DeclRefExpr>(Val: EI->getAssociatedExpression()) &&
2321 EI->getAssociatedDeclaration() == D) {
2322 IsVariableUsedInMapClause = true;
2323
2324 // If the component list has only one element, it's for mapping the
2325 // variable itself, like map(p). This takes precedence in
2326 // determining how it's captured, so we don't need to look further
2327 // for any other maps that use the variable (like map(p[0]) etc.)
2328 if (MapExprComponents.size() == 1) {
2329 IsVariableItselfMapped = true;
2330 return true;
2331 }
2332 }
2333
2334 ++EI;
2335 if (EI == EE)
2336 return false;
2337 auto Last = std::prev(x: EE);
2338 const auto *UO =
2339 dyn_cast<UnaryOperator>(Val: Last->getAssociatedExpression());
2340 if ((UO && UO->getOpcode() == UO_Deref) ||
2341 isa<ArraySubscriptExpr>(Val: Last->getAssociatedExpression()) ||
2342 isa<ArraySectionExpr>(Val: Last->getAssociatedExpression()) ||
2343 isa<MemberExpr>(Val: EI->getAssociatedExpression()) ||
2344 isa<OMPArrayShapingExpr>(Val: Last->getAssociatedExpression())) {
2345 IsVariableAssociatedWithSection = true;
2346 // We've found a case like map(p[0]) or map(p->a) or map(*p),
2347 // so we are done with this particular map, but we need to keep
2348 // looking in case we find a map(p).
2349 return false;
2350 }
2351
2352 // Keep looking for more map info.
2353 return false;
2354 });
2355
2356 if (IsVariableUsedInMapClause) {
2357 // If variable is identified in a map clause it is always captured by
2358 // reference except if it is a pointer that is dereferenced somehow, but
2359 // not itself mapped.
2360 //
2361 // OpenMP 6.0, 7.1.1: Data sharing attribute rules, variables referenced
2362 // in a construct::
2363 // If a list item in a has_device_addr clause or in a map clause on the
2364 // target construct has a base pointer, and the base pointer is a scalar
2365 // variable *that is not a list item in a map clause on the construct*,
2366 // the base pointer is firstprivate.
2367 //
2368 // OpenMP 4.5, 2.15.1.1: Data-sharing Attribute Rules for Variables
2369 // Referenced in a Construct:
2370 // If an array section is a list item in a map clause on the target
2371 // construct and the array section is derived from a variable for which
2372 // the type is pointer then that variable is firstprivate.
2373 IsByRef = IsVariableItselfMapped ||
2374 !(Ty->isPointerType() && IsVariableAssociatedWithSection);
2375 } else {
2376 // By default, all the data that has a scalar type is mapped by copy
2377 // (except for reduction variables).
2378 // Defaultmap scalar is mutual exclusive to defaultmap pointer
2379 IsByRef = (DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
2380 !Ty->isAnyPointerType()) ||
2381 !Ty->isScalarType() ||
2382 DSAStack->isDefaultmapCapturedByRef(
2383 Level, Kind: getVariableCategoryFromDecl(LO: getLangOpts(), VD: D)) ||
2384 DSAStack->hasExplicitDSA(
2385 D,
2386 CPred: [](OpenMPClauseKind K, bool AppliedToPointee) {
2387 return K == OMPC_reduction && !AppliedToPointee;
2388 },
2389 Level);
2390 }
2391 }
2392
2393 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
2394 IsByRef =
2395 ((IsVariableUsedInMapClause &&
2396 DSAStack->getCaptureRegion(Level, OpenMPCaptureLevel) ==
2397 OMPD_target) ||
2398 !(DSAStack->hasExplicitDSA(
2399 D,
2400 CPred: [](OpenMPClauseKind K, bool AppliedToPointee) -> bool {
2401 return K == OMPC_firstprivate ||
2402 (K == OMPC_reduction && AppliedToPointee);
2403 },
2404 Level, /*NotLastprivate=*/true) ||
2405 DSAStack->isUsesAllocatorsDecl(Level, D))) &&
2406 // If the variable is artificial and must be captured by value - try to
2407 // capture by value.
2408 !(isa<OMPCapturedExprDecl>(Val: D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
2409 !cast<OMPCapturedExprDecl>(Val: D)->getInit()->isGLValue()) &&
2410 // If the variable is implicitly firstprivate and scalar - capture by
2411 // copy. Query the default data-sharing attribute at the capture level
2412 // being considered rather than at the innermost directive: a nested
2413 // default(firstprivate) or default(private) must not make an enclosing
2414 // region capture by copy and lose its writes.
2415 !((DSAStack->getDefaultDSA(Level) == DSA_firstprivate ||
2416 DSAStack->getDefaultDSA(Level) == DSA_private) &&
2417 !DSAStack->hasExplicitDSA(
2418 D, CPred: [](OpenMPClauseKind K, bool) { return K != OMPC_unknown; },
2419 Level) &&
2420 !DSAStack->isLoopControlVariable(D, Level).first);
2421 }
2422
2423 // When passing data by copy, we need to make sure it fits the uintptr size
2424 // and alignment, because the runtime library only deals with uintptr types.
2425 // If it does not fit the uintptr size, we need to pass the data by reference
2426 // instead.
2427 if (!IsByRef && (Ctx.getTypeSizeInChars(T: Ty) >
2428 Ctx.getTypeSizeInChars(T: Ctx.getUIntPtrType()) ||
2429 Ctx.getAlignOfGlobalVarInChars(T: Ty, VD: dyn_cast<VarDecl>(Val: D)) >
2430 Ctx.getTypeAlignInChars(T: Ctx.getUIntPtrType()))) {
2431 IsByRef = true;
2432 }
2433
2434 return IsByRef;
2435}
2436
2437unsigned SemaOpenMP::getOpenMPNestingLevel() const {
2438 assert(getLangOpts().OpenMP);
2439 return DSAStack->getNestingLevel();
2440}
2441
2442bool SemaOpenMP::isInOpenMPTaskUntiedContext() const {
2443 return isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
2444 DSAStack->isUntiedRegion();
2445}
2446
2447bool SemaOpenMP::isInOpenMPTargetExecutionDirective() const {
2448 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
2449 !DSAStack->isClauseParsingMode()) ||
2450 DSAStack->hasDirective(
2451 DPred: [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
2452 SourceLocation) -> bool {
2453 return isOpenMPTargetExecutionDirective(DKind: K);
2454 },
2455 FromParent: false);
2456}
2457
2458bool SemaOpenMP::isOpenMPRebuildMemberExpr(ValueDecl *D) {
2459 // Only rebuild for Field.
2460 if (!isa<FieldDecl>(Val: D))
2461 return false;
2462 DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA(
2463 D,
2464 CPred: [](OpenMPClauseKind C, bool AppliedToPointee,
2465 DefaultDataSharingAttributes DefaultAttr) {
2466 return isOpenMPPrivate(Kind: C) && !AppliedToPointee &&
2467 (DefaultAttr == DSA_firstprivate || DefaultAttr == DSA_private);
2468 },
2469 DPred: [](OpenMPDirectiveKind) { return true; },
2470 DSAStack->isClauseParsingMode());
2471 if (DVarPrivate.CKind != OMPC_unknown)
2472 return true;
2473 return false;
2474}
2475
2476static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
2477 Expr *CaptureExpr, bool WithInit,
2478 DeclContext *CurContext,
2479 bool AsExpression);
2480
2481VarDecl *SemaOpenMP::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo,
2482 unsigned StopAt) {
2483 assert(getLangOpts().OpenMP && "OpenMP is not allowed");
2484 D = getCanonicalDecl(D);
2485
2486 // A tuple-like binding has no private copy. A privatized one reports its
2487 // holding variable instead, so its uses still go through the capture checks.
2488 VarDecl *HoldingVar = nullptr;
2489 if (auto *BD = dyn_cast<BindingDecl>(Val: D)) {
2490 HoldingVar = BD->getHoldingVar();
2491 if (!HoldingVar)
2492 D = cast<VarDecl>(Val: BD->getDecomposedDecl());
2493 }
2494 auto *VD = dyn_cast<VarDecl>(Val: D);
2495 // Do not capture constexpr variables.
2496 if (VD && VD->isConstexpr())
2497 return nullptr;
2498
2499 // If we want to determine whether the variable should be captured from the
2500 // perspective of the current capturing scope, and we've already left all the
2501 // capturing scopes of the top directive on the stack, check from the
2502 // perspective of its parent directive (if any) instead.
2503 DSAStackTy::ParentDirectiveScope InParentDirectiveRAII(
2504 *DSAStack, CheckScopeInfo && DSAStack->isBodyComplete());
2505
2506 // If we are attempting to capture a global variable in a directive with
2507 // 'target' we return true so that this global is also mapped to the device.
2508 //
2509 if (VD && !VD->hasLocalStorage() &&
2510 (SemaRef.getCurCapturedRegion() || SemaRef.getCurBlock() ||
2511 SemaRef.getCurLambda())) {
2512 if (isInOpenMPTargetExecutionDirective()) {
2513 DSAStackTy::DSAVarData DVarTop =
2514 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
2515 if (DVarTop.CKind != OMPC_unknown && DVarTop.RefExpr)
2516 return VD;
2517 // If the declaration is enclosed in a 'declare target' directive,
2518 // then it should not be captured.
2519 //
2520 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
2521 return nullptr;
2522 CapturedRegionScopeInfo *CSI = nullptr;
2523 for (FunctionScopeInfo *FSI : llvm::drop_begin(
2524 RangeOrContainer: llvm::reverse(C&: SemaRef.FunctionScopes),
2525 N: CheckScopeInfo ? (SemaRef.FunctionScopes.size() - (StopAt + 1))
2526 : 0)) {
2527 if (!isa<CapturingScopeInfo>(Val: FSI))
2528 return nullptr;
2529 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(Val: FSI))
2530 if (RSI->CapRegionKind == CR_OpenMP) {
2531 CSI = RSI;
2532 break;
2533 }
2534 }
2535 // Lambdas and blocks at namespace scope have no enclosing function scope.
2536 if (!CSI)
2537 return nullptr;
2538 SmallVector<OpenMPDirectiveKind, 4> Regions;
2539 getOpenMPCaptureRegions(CaptureRegions&: Regions,
2540 DSAStack->getDirective(Level: CSI->OpenMPLevel));
2541 if (Regions[CSI->OpenMPCaptureLevel] != OMPD_task)
2542 return VD;
2543 }
2544 if (isInOpenMPDeclareTargetContext()) {
2545 // Try to mark variable as declare target if it is used in capturing
2546 // regions.
2547 if (getLangOpts().OpenMP <= 45 &&
2548 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
2549 checkDeclIsAllowedInOpenMPTarget(E: nullptr, D: VD);
2550 return nullptr;
2551 }
2552 }
2553
2554 if (CheckScopeInfo) {
2555 bool OpenMPFound = false;
2556 for (unsigned I = StopAt + 1; I > 0; --I) {
2557 FunctionScopeInfo *FSI = SemaRef.FunctionScopes[I - 1];
2558 if (!isa<CapturingScopeInfo>(Val: FSI))
2559 return nullptr;
2560 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(Val: FSI))
2561 if (RSI->CapRegionKind == CR_OpenMP) {
2562 OpenMPFound = true;
2563 break;
2564 }
2565 }
2566 if (!OpenMPFound)
2567 return nullptr;
2568 }
2569
2570 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
2571 (!DSAStack->isClauseParsingMode() ||
2572 DSAStack->getParentDirective() != OMPD_unknown)) {
2573 auto &&Info = DSAStack->isLoopControlVariable(D);
2574 if (Info.first ||
2575 (VD && VD->hasLocalStorage() &&
2576 isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) ||
2577 (VD && DSAStack->isForceVarCapturing()))
2578 return VD ? VD : Info.second;
2579 DSAStackTy::DSAVarData DVarTop =
2580 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
2581 if (DVarTop.CKind != OMPC_unknown && isOpenMPPrivate(Kind: DVarTop.CKind) &&
2582 (!VD || VD->hasLocalStorage() ||
2583 !(DVarTop.AppliedToPointee && DVarTop.CKind != OMPC_reduction)))
2584 return VD ? VD
2585 : HoldingVar ? HoldingVar
2586 : cast<VarDecl>(Val: DVarTop.PrivateCopy->getDecl());
2587 // Threadprivate variables must not be captured.
2588 if (isOpenMPThreadPrivate(Kind: DVarTop.CKind))
2589 return nullptr;
2590 // The variable is not private or it is the variable in the directive with
2591 // default(none) clause and not used in any clause.
2592 DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA(
2593 D,
2594 CPred: [](OpenMPClauseKind C, bool AppliedToPointee, bool) {
2595 return isOpenMPPrivate(Kind: C) && !AppliedToPointee;
2596 },
2597 DPred: [](OpenMPDirectiveKind) { return true; },
2598 DSAStack->isClauseParsingMode());
2599 // Global shared must not be captured.
2600 if (VD && !VD->hasLocalStorage() && DVarPrivate.CKind == OMPC_unknown &&
2601 ((DSAStack->getDefaultDSA() != DSA_none &&
2602 DSAStack->getDefaultDSA() != DSA_private &&
2603 DSAStack->getDefaultDSA() != DSA_firstprivate) ||
2604 DVarTop.CKind == OMPC_shared))
2605 return nullptr;
2606 auto *FD = dyn_cast<FieldDecl>(Val: D);
2607 if (DVarPrivate.CKind != OMPC_unknown && !VD && FD &&
2608 !DVarPrivate.PrivateCopy) {
2609 DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA(
2610 D,
2611 CPred: [](OpenMPClauseKind C, bool AppliedToPointee,
2612 DefaultDataSharingAttributes DefaultAttr) {
2613 return isOpenMPPrivate(Kind: C) && !AppliedToPointee &&
2614 (DefaultAttr == DSA_firstprivate ||
2615 DefaultAttr == DSA_private);
2616 },
2617 DPred: [](OpenMPDirectiveKind) { return true; },
2618 DSAStack->isClauseParsingMode());
2619 if (DVarPrivate.CKind == OMPC_unknown)
2620 return nullptr;
2621
2622 VarDecl *VD = DSAStack->getImplicitFDCapExprDecl(FD);
2623 if (VD)
2624 return VD;
2625 if (SemaRef.getCurrentThisType().isNull())
2626 return nullptr;
2627 Expr *ThisExpr = SemaRef.BuildCXXThisExpr(Loc: SourceLocation(),
2628 Type: SemaRef.getCurrentThisType(),
2629 /*IsImplicit=*/true);
2630 Expr *ME = SemaRef.BuildMemberExpr(
2631 Base: ThisExpr, /*IsArrow=*/true, OpLoc: SourceLocation(),
2632 NNS: NestedNameSpecifierLoc(), TemplateKWLoc: SourceLocation(), Member: FD,
2633 FoundDecl: DeclAccessPair::make(D: FD, AS: FD->getAccess()),
2634 /*HadMultipleCandidates=*/false, MemberNameInfo: DeclarationNameInfo(), Ty: FD->getType(),
2635 VK: VK_LValue, OK: OK_Ordinary);
2636 OMPCapturedExprDecl *CD = buildCaptureDecl(
2637 S&: SemaRef, Id: FD->getIdentifier(), CaptureExpr: ME, WithInit: DVarPrivate.CKind != OMPC_private,
2638 CurContext: SemaRef.CurContext->getParent(), /*AsExpression=*/false);
2639 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
2640 S&: SemaRef, D: CD, Ty: CD->getType().getNonReferenceType(), Loc: SourceLocation());
2641 VD = cast<VarDecl>(Val: VDPrivateRefExpr->getDecl());
2642 DSAStack->addImplicitDefaultFirstprivateFD(FD, VD);
2643 return VD;
2644 }
2645 if (DVarPrivate.CKind != OMPC_unknown ||
2646 (VD && (DSAStack->getDefaultDSA() == DSA_none ||
2647 DSAStack->getDefaultDSA() == DSA_private ||
2648 DSAStack->getDefaultDSA() == DSA_firstprivate)))
2649 return VD ? VD
2650 : HoldingVar ? HoldingVar
2651 : cast<VarDecl>(Val: DVarPrivate.PrivateCopy->getDecl());
2652 }
2653 return nullptr;
2654}
2655
2656void SemaOpenMP::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
2657 unsigned Level) const {
2658 FunctionScopesIndex -= getOpenMPCaptureLevels(DSAStack->getDirective(Level));
2659}
2660
2661void SemaOpenMP::startOpenMPLoop() {
2662 assert(getLangOpts().OpenMP && "OpenMP must be enabled.");
2663 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
2664 DSAStack->loopInit();
2665}
2666
2667void SemaOpenMP::startOpenMPCXXRangeFor() {
2668 assert(getLangOpts().OpenMP && "OpenMP must be enabled.");
2669 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2670 DSAStack->resetPossibleLoopCounter();
2671 DSAStack->loopStart();
2672 }
2673}
2674
2675OpenMPClauseKind SemaOpenMP::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level,
2676 unsigned CapLevel) const {
2677 assert(getLangOpts().OpenMP && "OpenMP is not allowed");
2678 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
2679 (!DSAStack->isClauseParsingMode() ||
2680 DSAStack->getParentDirective() != OMPD_unknown)) {
2681 DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA(
2682 D,
2683 CPred: [](OpenMPClauseKind C, bool AppliedToPointee,
2684 DefaultDataSharingAttributes DefaultAttr) {
2685 return isOpenMPPrivate(Kind: C) && !AppliedToPointee &&
2686 DefaultAttr == DSA_private;
2687 },
2688 DPred: [](OpenMPDirectiveKind) { return true; },
2689 DSAStack->isClauseParsingMode());
2690 if (DVarPrivate.CKind == OMPC_private && isa<OMPCapturedExprDecl>(Val: D) &&
2691 DSAStack->isImplicitDefaultFirstprivateFD(VD: cast<VarDecl>(Val: D)) &&
2692 !DSAStack->isLoopControlVariable(D).first)
2693 return OMPC_private;
2694 }
2695 if (DSAStack->hasExplicitDirective(DPred: isOpenMPTaskingDirective, Level)) {
2696 bool IsTriviallyCopyable =
2697 D->getType().getNonReferenceType().isTriviallyCopyableType(
2698 Context: getASTContext()) &&
2699 !D->getType()
2700 .getNonReferenceType()
2701 .getCanonicalType()
2702 ->getAsCXXRecordDecl();
2703 OpenMPDirectiveKind DKind = DSAStack->getDirective(Level);
2704 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2705 getOpenMPCaptureRegions(CaptureRegions, DKind);
2706 if (isOpenMPTaskingDirective(Kind: CaptureRegions[CapLevel]) &&
2707 (IsTriviallyCopyable ||
2708 !isOpenMPTaskLoopDirective(DKind: CaptureRegions[CapLevel]))) {
2709 if (DSAStack->hasExplicitDSA(
2710 D,
2711 CPred: [](OpenMPClauseKind K, bool) { return K == OMPC_firstprivate; },
2712 Level, /*NotLastprivate=*/true))
2713 return OMPC_firstprivate;
2714 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level);
2715 if (DVar.CKind != OMPC_shared &&
2716 !DSAStack->isLoopControlVariable(D, Level).first && !DVar.RefExpr) {
2717 DSAStack->addImplicitTaskFirstprivate(Level, D);
2718 return OMPC_firstprivate;
2719 }
2720 }
2721 }
2722 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()) &&
2723 !isOpenMPLoopTransformationDirective(DSAStack->getCurrentDirective())) {
2724 if (DSAStack->getAssociatedLoops() > 0 && !DSAStack->isLoopStarted()) {
2725 DSAStack->resetPossibleLoopCounter(D);
2726 DSAStack->loopStart();
2727 return OMPC_private;
2728 }
2729 if ((DSAStack->getPossiblyLoopCounter() == D->getCanonicalDecl() ||
2730 DSAStack->isLoopControlVariable(D).first) &&
2731 !DSAStack->hasExplicitDSA(
2732 D, CPred: [](OpenMPClauseKind K, bool) { return K != OMPC_private; },
2733 Level) &&
2734 !isOpenMPSimdDirective(DSAStack->getCurrentDirective()))
2735 return OMPC_private;
2736 }
2737 if (const auto *VD = dyn_cast<VarDecl>(Val: D)) {
2738 if (DSAStack->isThreadPrivate(D: const_cast<VarDecl *>(VD)) &&
2739 DSAStack->isForceVarCapturing() &&
2740 !DSAStack->hasExplicitDSA(
2741 D, CPred: [](OpenMPClauseKind K, bool) { return K == OMPC_copyin; },
2742 Level))
2743 return OMPC_private;
2744 }
2745 // User-defined allocators are private since they must be defined in the
2746 // context of target region.
2747 if (DSAStack->hasExplicitDirective(DPred: isOpenMPTargetExecutionDirective, Level) &&
2748 DSAStack->isUsesAllocatorsDecl(Level, D).value_or(
2749 u: DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) ==
2750 DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator)
2751 return OMPC_private;
2752 return (DSAStack->hasExplicitDSA(
2753 D, CPred: [](OpenMPClauseKind K, bool) { return K == OMPC_private; },
2754 Level) ||
2755 (DSAStack->isClauseParsingMode() &&
2756 DSAStack->getClauseParsingMode() == OMPC_private) ||
2757 // Consider taskgroup reduction descriptor variable a private
2758 // to avoid possible capture in the region.
2759 (DSAStack->hasExplicitDirective(
2760 DPred: [](OpenMPDirectiveKind K) {
2761 return K == OMPD_taskgroup ||
2762 ((isOpenMPParallelDirective(DKind: K) ||
2763 isOpenMPWorksharingDirective(DKind: K)) &&
2764 !isOpenMPSimdDirective(DKind: K));
2765 },
2766 Level) &&
2767 DSAStack->isTaskgroupReductionRef(VD: D, Level)))
2768 ? OMPC_private
2769 : OMPC_unknown;
2770}
2771
2772void SemaOpenMP::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
2773 unsigned Level) {
2774 assert(getLangOpts().OpenMP && "OpenMP is not allowed");
2775 D = getCanonicalDecl(D);
2776 OpenMPClauseKind OMPC = OMPC_unknown;
2777 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
2778 const unsigned NewLevel = I - 1;
2779 if (DSAStack->hasExplicitDSA(
2780 D,
2781 CPred: [&OMPC](const OpenMPClauseKind K, bool AppliedToPointee) {
2782 if (isOpenMPPrivate(Kind: K) && !AppliedToPointee) {
2783 OMPC = K;
2784 return true;
2785 }
2786 return false;
2787 },
2788 Level: NewLevel))
2789 break;
2790 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
2791 VD: D, Level: NewLevel,
2792 Check: [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
2793 OpenMPClauseKind) { return true; })) {
2794 OMPC = OMPC_map;
2795 break;
2796 }
2797 if (DSAStack->hasExplicitDirective(DPred: isOpenMPTargetExecutionDirective,
2798 Level: NewLevel)) {
2799 OMPC = OMPC_map;
2800 if (DSAStack->mustBeFirstprivateAtLevel(
2801 Level: NewLevel, Kind: getVariableCategoryFromDecl(LO: getLangOpts(), VD: D)))
2802 OMPC = OMPC_firstprivate;
2803 break;
2804 }
2805 }
2806 if (OMPC != OMPC_unknown)
2807 FD->addAttr(
2808 A: OMPCaptureKindAttr::CreateImplicit(Ctx&: getASTContext(), CaptureKindVal: unsigned(OMPC)));
2809}
2810
2811bool SemaOpenMP::isOpenMPTargetCapturedDecl(const ValueDecl *D, unsigned Level,
2812 unsigned CaptureLevel) const {
2813 assert(getLangOpts().OpenMP && "OpenMP is not allowed");
2814 // Return true if the current level is no longer enclosed in a target region.
2815
2816 SmallVector<OpenMPDirectiveKind, 4> Regions;
2817 getOpenMPCaptureRegions(CaptureRegions&: Regions, DSAStack->getDirective(Level));
2818 const auto *VD = dyn_cast<VarDecl>(Val: D);
2819 return VD && !VD->hasLocalStorage() &&
2820 DSAStack->hasExplicitDirective(DPred: isOpenMPTargetExecutionDirective,
2821 Level) &&
2822 Regions[CaptureLevel] != OMPD_task;
2823}
2824
2825bool SemaOpenMP::isOpenMPGlobalCapturedDecl(ValueDecl *D, unsigned Level,
2826 unsigned CaptureLevel) const {
2827 assert(getLangOpts().OpenMP && "OpenMP is not allowed");
2828 // Return true if the current level is no longer enclosed in a target region.
2829
2830 if (const auto *VD = dyn_cast<VarDecl>(Val: D)) {
2831 if (!VD->hasLocalStorage()) {
2832 if (isInOpenMPTargetExecutionDirective())
2833 return true;
2834 DSAStackTy::DSAVarData TopDVar =
2835 DSAStack->getTopDSA(D, /*FromParent=*/false);
2836 unsigned NumLevels =
2837 getOpenMPCaptureLevels(DSAStack->getDirective(Level));
2838 if (Level == 0)
2839 // non-file scope static variable with default(firstprivate)
2840 // should be global captured.
2841 return (NumLevels == CaptureLevel + 1 &&
2842 (TopDVar.CKind != OMPC_shared ||
2843 DSAStack->getDefaultDSA() == DSA_firstprivate));
2844 do {
2845 --Level;
2846 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level);
2847 if (DVar.CKind != OMPC_shared)
2848 return true;
2849 } while (Level > 0);
2850 }
2851 }
2852 return true;
2853}
2854
2855void SemaOpenMP::DestroyDataSharingAttributesStack() { delete DSAStack; }
2856
2857void SemaOpenMP::ActOnOpenMPBeginDeclareVariant(SourceLocation Loc,
2858 OMPTraitInfo &TI) {
2859 OMPDeclareVariantScopes.push_back(Elt: OMPDeclareVariantScope(TI));
2860}
2861
2862void SemaOpenMP::ActOnOpenMPEndDeclareVariant() {
2863 assert(isInOpenMPDeclareVariantScope() &&
2864 "Not in OpenMP declare variant scope!");
2865
2866 OMPDeclareVariantScopes.pop_back();
2867}
2868
2869void SemaOpenMP::finalizeOpenMPDelayedAnalysis(const FunctionDecl *Caller,
2870 const FunctionDecl *Callee,
2871 SourceLocation Loc) {
2872 assert(getLangOpts().OpenMP && "Expected OpenMP compilation mode.");
2873 std::optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
2874 OMPDeclareTargetDeclAttr::getDeviceType(VD: Caller->getMostRecentDecl());
2875 // Ignore host functions during device analysis.
2876 if (getLangOpts().OpenMPIsTargetDevice &&
2877 (!DevTy || *DevTy == OMPDeclareTargetDeclAttr::DT_Host))
2878 return;
2879 // Ignore nohost functions during host analysis.
2880 if (!getLangOpts().OpenMPIsTargetDevice && DevTy &&
2881 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost)
2882 return;
2883 const FunctionDecl *FD = Callee->getMostRecentDecl();
2884 DevTy = OMPDeclareTargetDeclAttr::getDeviceType(VD: FD);
2885 if (getLangOpts().OpenMPIsTargetDevice && DevTy &&
2886 *DevTy == OMPDeclareTargetDeclAttr::DT_Host) {
2887 // Diagnose host function called during device codegen.
2888 StringRef HostDevTy =
2889 getOpenMPSimpleClauseTypeName(Kind: OMPC_device_type, Type: OMPC_DEVICE_TYPE_host);
2890 Diag(Loc, DiagID: diag::err_omp_wrong_device_function_call) << HostDevTy << 0;
2891 Diag(Loc: *OMPDeclareTargetDeclAttr::getLocation(VD: FD),
2892 DiagID: diag::note_omp_marked_device_type_here)
2893 << HostDevTy;
2894 return;
2895 }
2896 if (!getLangOpts().OpenMPIsTargetDevice &&
2897 !getLangOpts().OpenMPOffloadMandatory && DevTy &&
2898 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) {
2899 // In OpenMP 5.2 or later, if the function has a host variant then allow
2900 // that to be called instead
2901 auto &&HasHostAttr = [](const FunctionDecl *Callee) {
2902 for (OMPDeclareVariantAttr *A :
2903 Callee->specific_attrs<OMPDeclareVariantAttr>()) {
2904 auto *DeclRefVariant = cast<DeclRefExpr>(Val: A->getVariantFuncRef());
2905 auto *VariantFD = cast<FunctionDecl>(Val: DeclRefVariant->getDecl());
2906 std::optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
2907 OMPDeclareTargetDeclAttr::getDeviceType(
2908 VD: VariantFD->getMostRecentDecl());
2909 if (!DevTy || *DevTy == OMPDeclareTargetDeclAttr::DT_Host)
2910 return true;
2911 }
2912 return false;
2913 };
2914 if (getLangOpts().OpenMP >= 52 &&
2915 Callee->hasAttr<OMPDeclareVariantAttr>() && HasHostAttr(Callee))
2916 return;
2917 // Diagnose nohost function called during host codegen.
2918 StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName(
2919 Kind: OMPC_device_type, Type: OMPC_DEVICE_TYPE_nohost);
2920 Diag(Loc, DiagID: diag::err_omp_wrong_device_function_call) << NoHostDevTy << 1;
2921 Diag(Loc: *OMPDeclareTargetDeclAttr::getLocation(VD: FD),
2922 DiagID: diag::note_omp_marked_device_type_here)
2923 << NoHostDevTy;
2924 }
2925}
2926
2927void SemaOpenMP::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
2928 const DeclarationNameInfo &DirName,
2929 Scope *CurScope, SourceLocation Loc) {
2930 DSAStack->push(DKind, DirName, CurScope, Loc);
2931 SemaRef.PushExpressionEvaluationContext(
2932 NewContext: Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
2933}
2934
2935void SemaOpenMP::StartOpenMPClause(OpenMPClauseKind K) {
2936 DSAStack->setClauseParsingMode(K);
2937}
2938
2939void SemaOpenMP::EndOpenMPClause() {
2940 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
2941 SemaRef.CleanupVarDeclMarking();
2942}
2943
2944static std::pair<ValueDecl *, bool>
2945getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
2946 SourceRange &ERange, bool AllowArraySection = false,
2947 bool AllowAssumedSizeArray = false, StringRef DiagType = "");
2948
2949/// Check consistency of the reduction clauses.
2950static void checkReductionClauses(Sema &S, DSAStackTy *Stack,
2951 ArrayRef<OMPClause *> Clauses) {
2952 bool InscanFound = false;
2953 SourceLocation InscanLoc;
2954 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions.
2955 // A reduction clause without the inscan reduction-modifier may not appear on
2956 // a construct on which a reduction clause with the inscan reduction-modifier
2957 // appears.
2958 for (OMPClause *C : Clauses) {
2959 if (C->getClauseKind() != OMPC_reduction)
2960 continue;
2961 auto *RC = cast<OMPReductionClause>(Val: C);
2962 if (RC->getModifier() == OMPC_REDUCTION_inscan) {
2963 InscanFound = true;
2964 InscanLoc = RC->getModifierLoc();
2965 continue;
2966 }
2967 if (RC->getModifier() == OMPC_REDUCTION_task) {
2968 // OpenMP 5.0, 2.19.5.4 reduction Clause.
2969 // A reduction clause with the task reduction-modifier may only appear on
2970 // a parallel construct, a worksharing construct or a combined or
2971 // composite construct for which any of the aforementioned constructs is a
2972 // constituent construct and simd or loop are not constituent constructs.
2973 OpenMPDirectiveKind CurDir = Stack->getCurrentDirective();
2974 if (!(isOpenMPParallelDirective(DKind: CurDir) ||
2975 isOpenMPWorksharingDirective(DKind: CurDir)) ||
2976 isOpenMPSimdDirective(DKind: CurDir))
2977 S.Diag(Loc: RC->getModifierLoc(),
2978 DiagID: diag::err_omp_reduction_task_not_parallel_or_worksharing);
2979 continue;
2980 }
2981 }
2982 if (InscanFound) {
2983 for (OMPClause *C : Clauses) {
2984 if (C->getClauseKind() != OMPC_reduction)
2985 continue;
2986 auto *RC = cast<OMPReductionClause>(Val: C);
2987 if (RC->getModifier() != OMPC_REDUCTION_inscan) {
2988 S.Diag(Loc: RC->getModifier() == OMPC_REDUCTION_unknown
2989 ? RC->getBeginLoc()
2990 : RC->getModifierLoc(),
2991 DiagID: diag::err_omp_inscan_reduction_expected);
2992 S.Diag(Loc: InscanLoc, DiagID: diag::note_omp_previous_inscan_reduction);
2993 continue;
2994 }
2995 for (Expr *Ref : RC->varlist()) {
2996 assert(Ref && "NULL expr in OpenMP reduction clause.");
2997 SourceLocation ELoc;
2998 SourceRange ERange;
2999 Expr *SimpleRefExpr = Ref;
3000 auto Res = getPrivateItem(S, RefExpr&: SimpleRefExpr, ELoc, ERange,
3001 /*AllowArraySection=*/true);
3002 ValueDecl *D = Res.first;
3003 if (!D)
3004 continue;
3005 if (!Stack->isUsedInScanDirective(D: getCanonicalDecl(D))) {
3006 S.Diag(Loc: Ref->getExprLoc(),
3007 DiagID: diag::err_omp_reduction_not_inclusive_exclusive)
3008 << Ref->getSourceRange();
3009 }
3010 }
3011 }
3012 }
3013}
3014
3015static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
3016 ArrayRef<OMPClause *> Clauses);
3017static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
3018 bool WithInit);
3019
3020static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
3021 const ValueDecl *D,
3022 const DSAStackTy::DSAVarData &DVar,
3023 bool IsLoopIterVar = false);
3024
3025void SemaOpenMP::EndOpenMPDSABlock(Stmt *CurDirective) {
3026 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
3027 // A variable of class type (or array thereof) that appears in a lastprivate
3028 // clause requires an accessible, unambiguous default constructor for the
3029 // class type, unless the list item is also specified in a firstprivate
3030 // clause.
3031
3032 auto FinalizeLastprivate = [&](OMPLastprivateClause *Clause) {
3033 SmallVector<Expr *, 8> PrivateCopies;
3034 for (Expr *DE : Clause->varlist()) {
3035 if (DE->isValueDependent() || DE->isTypeDependent()) {
3036 PrivateCopies.push_back(Elt: nullptr);
3037 continue;
3038 }
3039 // Data members are captured, and get private copies, on instantiation.
3040 if (isa<MemberExpr>(Val: DE->IgnoreParens())) {
3041 assert(SemaRef.CurContext->isDependentContext() &&
3042 "uncaptured data member in a non-dependent context");
3043 PrivateCopies.push_back(Elt: nullptr);
3044 continue;
3045 }
3046 auto *DRE = cast<DeclRefExpr>(Val: DE->IgnoreParens());
3047 auto *D = DRE->getDecl();
3048 if (auto *BD = dyn_cast<BindingDecl>(Val: D)) {
3049 QualType Type = BD->getType().getNonReferenceType();
3050 const DSAStackTy::DSAVarData DVar =
3051 DSAStack->getTopDSA(D: BD, /*FromParent=*/false);
3052 if (DVar.CKind != OMPC_lastprivate) {
3053 // The variable is also a firstprivate, so initialization sequence
3054 // for private copy is generated already.
3055 PrivateCopies.push_back(Elt: nullptr);
3056 continue;
3057 }
3058 VarDecl *VDPrivate = buildVarDecl(
3059 SemaRef, Loc: DE->getExprLoc(), Type: Type.getUnqualifiedType(), Name: BD->getName(),
3060 Attrs: BD->hasAttrs() ? &BD->getAttrs() : nullptr, OrigRef: DRE);
3061 SemaRef.ActOnUninitializedDecl(dcl: VDPrivate);
3062 if (VDPrivate->isInvalidDecl()) {
3063 PrivateCopies.push_back(Elt: nullptr);
3064 continue;
3065 }
3066 PrivateCopies.push_back(Elt: buildDeclRefExpr(
3067 S&: SemaRef, D: VDPrivate, Ty: DE->getType(), Loc: DE->getExprLoc()));
3068 continue;
3069 }
3070 auto *VD = cast<VarDecl>(Val: D);
3071 QualType Type = VD->getType().getNonReferenceType();
3072 const DSAStackTy::DSAVarData DVar =
3073 DSAStack->getTopDSA(D: VD, /*FromParent=*/false);
3074 if (DVar.CKind != OMPC_lastprivate) {
3075 // The variable is also a firstprivate, so initialization sequence
3076 // for private copy is generated already.
3077 PrivateCopies.push_back(Elt: nullptr);
3078 continue;
3079 }
3080 // Generate helper private variable and initialize it with the
3081 // default value. The address of the original variable is replaced
3082 // by the address of the new private variable in CodeGen. This new
3083 // variable is not added to IdResolver, so the code in the OpenMP
3084 // region uses original variable for proper diagnostics.
3085 VarDecl *VDPrivate = buildVarDecl(
3086 SemaRef, Loc: DE->getExprLoc(), Type: Type.getUnqualifiedType(), Name: VD->getName(),
3087 Attrs: VD->hasAttrs() ? &VD->getAttrs() : nullptr, OrigRef: DRE);
3088 SemaRef.ActOnUninitializedDecl(dcl: VDPrivate);
3089 if (VDPrivate->isInvalidDecl()) {
3090 PrivateCopies.push_back(Elt: nullptr);
3091 continue;
3092 }
3093 PrivateCopies.push_back(Elt: buildDeclRefExpr(
3094 S&: SemaRef, D: VDPrivate, Ty: DE->getType(), Loc: DE->getExprLoc()));
3095 }
3096 Clause->setPrivateCopies(PrivateCopies);
3097 };
3098
3099 auto FinalizeNontemporal = [&](OMPNontemporalClause *Clause) {
3100 // Finalize nontemporal clause by handling private copies, if any.
3101 SmallVector<Expr *, 8> PrivateRefs;
3102 for (Expr *RefExpr : Clause->varlist()) {
3103 assert(RefExpr && "NULL expr in OpenMP nontemporal clause.");
3104 SourceLocation ELoc;
3105 SourceRange ERange;
3106 Expr *SimpleRefExpr = RefExpr;
3107 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
3108 if (Res.second)
3109 // It will be analyzed later.
3110 PrivateRefs.push_back(Elt: RefExpr);
3111 ValueDecl *D = Res.first;
3112 if (!D)
3113 continue;
3114
3115 const DSAStackTy::DSAVarData DVar =
3116 DSAStack->getTopDSA(D, /*FromParent=*/false);
3117 PrivateRefs.push_back(Elt: DVar.PrivateCopy ? DVar.PrivateCopy
3118 : SimpleRefExpr);
3119 }
3120 Clause->setPrivateRefs(PrivateRefs);
3121 };
3122
3123 auto FinalizeAllocators = [&](OMPUsesAllocatorsClause *Clause) {
3124 for (unsigned I = 0, E = Clause->getNumberOfAllocators(); I < E; ++I) {
3125 OMPUsesAllocatorsClause::Data D = Clause->getAllocatorData(I);
3126 auto *DRE = dyn_cast<DeclRefExpr>(Val: D.Allocator->IgnoreParenImpCasts());
3127 if (!DRE)
3128 continue;
3129 ValueDecl *VD = DRE->getDecl();
3130 if (!VD || !isa<VarDecl>(Val: VD))
3131 continue;
3132 DSAStackTy::DSAVarData DVar =
3133 DSAStack->getTopDSA(D: VD, /*FromParent=*/false);
3134 // OpenMP [2.12.5, target Construct]
3135 // Memory allocators that appear in a uses_allocators clause cannot
3136 // appear in other data-sharing attribute clauses or data-mapping
3137 // attribute clauses in the same construct.
3138 Expr *MapExpr = nullptr;
3139 if (DVar.RefExpr ||
3140 DSAStack->checkMappableExprComponentListsForDecl(
3141 VD, /*CurrentRegionOnly=*/true,
3142 Check: [VD, &MapExpr](
3143 OMPClauseMappableExprCommon::MappableExprComponentListRef
3144 MapExprComponents,
3145 OpenMPClauseKind C) {
3146 auto MI = MapExprComponents.rbegin();
3147 auto ME = MapExprComponents.rend();
3148 if (MI != ME &&
3149 MI->getAssociatedDeclaration()->getCanonicalDecl() ==
3150 VD->getCanonicalDecl()) {
3151 MapExpr = MI->getAssociatedExpression();
3152 return true;
3153 }
3154 return false;
3155 })) {
3156 Diag(Loc: D.Allocator->getExprLoc(), DiagID: diag::err_omp_allocator_used_in_clauses)
3157 << D.Allocator->getSourceRange();
3158 if (DVar.RefExpr)
3159 reportOriginalDsa(SemaRef, DSAStack, D: VD, DVar);
3160 else
3161 Diag(Loc: MapExpr->getExprLoc(), DiagID: diag::note_used_here)
3162 << MapExpr->getSourceRange();
3163 }
3164 }
3165 };
3166
3167 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(Val: CurDirective)) {
3168 for (OMPClause *C : D->clauses()) {
3169 if (auto *Clause = dyn_cast<OMPLastprivateClause>(Val: C)) {
3170 FinalizeLastprivate(Clause);
3171 } else if (auto *Clause = dyn_cast<OMPNontemporalClause>(Val: C)) {
3172 FinalizeNontemporal(Clause);
3173 } else if (auto *Clause = dyn_cast<OMPUsesAllocatorsClause>(Val: C)) {
3174 FinalizeAllocators(Clause);
3175 }
3176 }
3177 // Check allocate clauses.
3178 if (!SemaRef.CurContext->isDependentContext())
3179 checkAllocateClauses(S&: SemaRef, DSAStack, Clauses: D->clauses());
3180 checkReductionClauses(S&: SemaRef, DSAStack, Clauses: D->clauses());
3181 }
3182
3183 DSAStack->pop();
3184 SemaRef.DiscardCleanupsInEvaluationContext();
3185 SemaRef.PopExpressionEvaluationContext();
3186}
3187
3188static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
3189 Expr *NumIterations, Sema &SemaRef,
3190 Scope *S, DSAStackTy *Stack);
3191
3192static bool finishLinearClauses(Sema &SemaRef, ArrayRef<OMPClause *> Clauses,
3193 OMPLoopBasedDirective::HelperExprs &B,
3194 DSAStackTy *Stack) {
3195 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
3196 "loop exprs were not built");
3197
3198 if (SemaRef.CurContext->isDependentContext())
3199 return false;
3200
3201 // Finalize the clauses that need pre-built expressions for CodeGen.
3202 for (OMPClause *C : Clauses) {
3203 auto *LC = dyn_cast<OMPLinearClause>(Val: C);
3204 if (!LC)
3205 continue;
3206 if (FinishOpenMPLinearClause(Clause&: *LC, IV: cast<DeclRefExpr>(Val: B.IterationVarRef),
3207 NumIterations: B.NumIterations, SemaRef,
3208 S: SemaRef.getCurScope(), Stack))
3209 return true;
3210 }
3211
3212 return false;
3213}
3214
3215namespace {
3216
3217class VarDeclFilterCCC final : public CorrectionCandidateCallback {
3218private:
3219 Sema &SemaRef;
3220
3221public:
3222 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
3223 bool ValidateCandidate(const TypoCorrection &Candidate) override {
3224 NamedDecl *ND = Candidate.getCorrectionDecl();
3225 if (const auto *VD = dyn_cast_or_null<VarDecl>(Val: ND)) {
3226 return VD->hasGlobalStorage() &&
3227 SemaRef.isDeclInScope(D: ND, Ctx: SemaRef.getCurLexicalContext(),
3228 S: SemaRef.getCurScope());
3229 }
3230 return false;
3231 }
3232
3233 std::unique_ptr<CorrectionCandidateCallback> clone() override {
3234 return std::make_unique<VarDeclFilterCCC>(args&: *this);
3235 }
3236};
3237
3238class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
3239private:
3240 Sema &SemaRef;
3241
3242public:
3243 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
3244 bool ValidateCandidate(const TypoCorrection &Candidate) override {
3245 NamedDecl *ND = Candidate.getCorrectionDecl();
3246 if (ND && ((isa<VarDecl>(Val: ND) && ND->getKind() == Decl::Var) ||
3247 isa<FunctionDecl>(Val: ND))) {
3248 return SemaRef.isDeclInScope(D: ND, Ctx: SemaRef.getCurLexicalContext(),
3249 S: SemaRef.getCurScope());
3250 }
3251 return false;
3252 }
3253
3254 std::unique_ptr<CorrectionCandidateCallback> clone() override {
3255 return std::make_unique<VarOrFuncDeclFilterCCC>(args&: *this);
3256 }
3257};
3258
3259} // namespace
3260
3261ExprResult SemaOpenMP::ActOnOpenMPIdExpression(Scope *CurScope,
3262 CXXScopeSpec &ScopeSpec,
3263 const DeclarationNameInfo &Id,
3264 OpenMPDirectiveKind Kind) {
3265 ASTContext &Context = getASTContext();
3266 llvm::omp::Version OMPVersion = getLangOpts().getOpenMPVersion();
3267 LookupResult Lookup(SemaRef, Id, Sema::LookupOrdinaryName);
3268 SemaRef.LookupParsedName(R&: Lookup, S: CurScope, SS: &ScopeSpec,
3269 /*ObjectType=*/QualType(),
3270 /*AllowBuiltinCreation=*/true);
3271
3272 if (Lookup.isAmbiguous())
3273 return ExprError();
3274
3275 VarDecl *VD;
3276 if (!Lookup.isSingleResult()) {
3277 VarDeclFilterCCC CCC(SemaRef);
3278 if (TypoCorrection Corrected =
3279 SemaRef.CorrectTypo(Typo: Id, LookupKind: Sema::LookupOrdinaryName, S: CurScope, SS: nullptr,
3280 CCC, Mode: CorrectTypoKind::ErrorRecovery)) {
3281 SemaRef.diagnoseTypo(
3282 Correction: Corrected,
3283 TypoDiag: SemaRef.PDiag(DiagID: Lookup.empty() ? diag::err_undeclared_var_use_suggest
3284 : diag::err_omp_expected_var_arg_suggest)
3285 << Id.getName());
3286 VD = Corrected.getCorrectionDeclAs<VarDecl>();
3287 } else {
3288 Diag(Loc: Id.getLoc(), DiagID: Lookup.empty() ? diag::err_undeclared_var_use
3289 : diag::err_omp_expected_var_arg)
3290 << Id.getName();
3291 return ExprError();
3292 }
3293 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
3294 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_expected_var_arg) << Id.getName();
3295 Diag(Loc: Lookup.getFoundDecl()->getLocation(), DiagID: diag::note_declared_at);
3296 return ExprError();
3297 }
3298 Lookup.suppressDiagnostics();
3299
3300 // OpenMP [2.9.2, Syntax, C/C++]
3301 // Variables must be file-scope, namespace-scope, or static block-scope.
3302 if ((Kind == OMPD_threadprivate || Kind == OMPD_groupprivate) &&
3303 !VD->hasGlobalStorage()) {
3304 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_global_var_arg)
3305 << getOpenMPDirectiveName(D: Kind, V: OMPVersion) << !VD->isStaticLocal();
3306 bool IsDecl =
3307 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3308 Diag(Loc: VD->getLocation(),
3309 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3310 << VD;
3311 return ExprError();
3312 }
3313
3314 VarDecl *CanonicalVD = VD->getCanonicalDecl();
3315 NamedDecl *ND = CanonicalVD;
3316 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
3317 // A threadprivate or groupprivate directive for file-scope variables must
3318 // appear outside any definition or declaration.
3319 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
3320 !SemaRef.getCurLexicalContext()->isTranslationUnit()) {
3321 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_var_scope)
3322 << getOpenMPDirectiveName(D: Kind, V: OMPVersion) << VD;
3323 bool IsDecl =
3324 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3325 Diag(Loc: VD->getLocation(),
3326 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3327 << VD;
3328 return ExprError();
3329 }
3330 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
3331 // A threadprivate or groupprivate directive for static class member
3332 // variables must appear in the class definition, in the same scope in which
3333 // the member variables are declared.
3334 if (CanonicalVD->isStaticDataMember() &&
3335 !CanonicalVD->getDeclContext()->Equals(DC: SemaRef.getCurLexicalContext())) {
3336 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_var_scope)
3337 << getOpenMPDirectiveName(D: Kind, V: OMPVersion) << VD;
3338 bool IsDecl =
3339 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3340 Diag(Loc: VD->getLocation(),
3341 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3342 << VD;
3343 return ExprError();
3344 }
3345 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
3346 // A threadprivate or groupprivate directive for namespace-scope variables
3347 // must appear outside any definition or declaration other than the
3348 // namespace definition itself.
3349 if (CanonicalVD->getDeclContext()->isNamespace() &&
3350 (!SemaRef.getCurLexicalContext()->isFileContext() ||
3351 !SemaRef.getCurLexicalContext()->Encloses(
3352 DC: CanonicalVD->getDeclContext()))) {
3353 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_var_scope)
3354 << getOpenMPDirectiveName(D: Kind, V: OMPVersion) << VD;
3355 bool IsDecl =
3356 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3357 Diag(Loc: VD->getLocation(),
3358 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3359 << VD;
3360 return ExprError();
3361 }
3362 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
3363 // A threadprivate or groupprivate directive for static block-scope
3364 // variables must appear in the scope of the variable and not in a nested
3365 // scope.
3366 if (CanonicalVD->isLocalVarDecl() && CurScope &&
3367 !SemaRef.isDeclInScope(D: ND, Ctx: SemaRef.getCurLexicalContext(), S: CurScope)) {
3368 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_var_scope)
3369 << getOpenMPDirectiveName(D: Kind, V: OMPVersion) << VD;
3370 bool IsDecl =
3371 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3372 Diag(Loc: VD->getLocation(),
3373 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3374 << VD;
3375 return ExprError();
3376 }
3377
3378 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
3379 // A threadprivate or groupprivate directive must lexically precede all
3380 // references to any of the variables in its list.
3381 if ((Kind == OMPD_threadprivate && VD->isUsed() &&
3382 !DSAStack->isThreadPrivate(D: VD)) ||
3383 (Kind == OMPD_groupprivate && VD->isUsed())) {
3384 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_var_used)
3385 << getOpenMPDirectiveName(D: Kind, V: OMPVersion) << VD;
3386 return ExprError();
3387 }
3388
3389 QualType ExprType = VD->getType().getNonReferenceType();
3390 return DeclRefExpr::Create(Context, QualifierLoc: NestedNameSpecifierLoc(),
3391 TemplateKWLoc: SourceLocation(), D: VD,
3392 /*RefersToEnclosingVariableOrCapture=*/false,
3393 NameLoc: Id.getLoc(), T: ExprType, VK: VK_LValue);
3394}
3395
3396SemaOpenMP::DeclGroupPtrTy
3397SemaOpenMP::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
3398 ArrayRef<Expr *> VarList) {
3399 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
3400 SemaRef.CurContext->addDecl(D);
3401 return DeclGroupPtrTy::make(P: DeclGroupRef(D));
3402 }
3403 return nullptr;
3404}
3405
3406SemaOpenMP::DeclGroupPtrTy
3407SemaOpenMP::ActOnOpenMPGroupPrivateDirective(SourceLocation Loc,
3408 ArrayRef<Expr *> VarList) {
3409 llvm::omp::Version OMPVersion = getLangOpts().getOpenMPVersion();
3410 if (!OMPVersion || OMPVersion < 60) {
3411 Diag(Loc, DiagID: diag::err_omp_unexpected_directive)
3412 << getOpenMPDirectiveName(D: OMPD_groupprivate, V: OMPVersion);
3413 return nullptr;
3414 }
3415 if (OMPGroupPrivateDecl *D = CheckOMPGroupPrivateDecl(Loc, VarList)) {
3416 SemaRef.CurContext->addDecl(D);
3417 return DeclGroupPtrTy::make(P: DeclGroupRef(D));
3418 }
3419 return nullptr;
3420}
3421
3422namespace {
3423class LocalVarRefChecker final
3424 : public ConstStmtVisitor<LocalVarRefChecker, bool> {
3425 Sema &SemaRef;
3426
3427public:
3428 bool VisitDeclRefExpr(const DeclRefExpr *E) {
3429 if (const auto *VD = dyn_cast<VarDecl>(Val: E->getDecl())) {
3430 if (VD->hasLocalStorage()) {
3431 SemaRef.Diag(Loc: E->getBeginLoc(),
3432 DiagID: diag::err_omp_local_var_in_threadprivate_init)
3433 << E->getSourceRange();
3434 SemaRef.Diag(Loc: VD->getLocation(), DiagID: diag::note_defined_here)
3435 << VD << VD->getSourceRange();
3436 return true;
3437 }
3438 }
3439 return false;
3440 }
3441 bool VisitStmt(const Stmt *S) {
3442 for (const Stmt *Child : S->children()) {
3443 if (Child && Visit(S: Child))
3444 return true;
3445 }
3446 return false;
3447 }
3448 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
3449};
3450} // namespace
3451
3452OMPThreadPrivateDecl *
3453SemaOpenMP::CheckOMPThreadPrivateDecl(SourceLocation Loc,
3454 ArrayRef<Expr *> VarList) {
3455 ASTContext &Context = getASTContext();
3456 SmallVector<Expr *, 8> Vars;
3457 for (Expr *RefExpr : VarList) {
3458 auto *DE = cast<DeclRefExpr>(Val: RefExpr);
3459 auto *VD = cast<VarDecl>(Val: DE->getDecl());
3460 SourceLocation ILoc = DE->getExprLoc();
3461
3462 // Mark variable as used.
3463 VD->setReferenced();
3464 VD->markUsed(C&: Context);
3465
3466 QualType QType = VD->getType();
3467 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
3468 // It will be analyzed later.
3469 Vars.push_back(Elt: DE);
3470 continue;
3471 }
3472
3473 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
3474 // A threadprivate variable must not have an incomplete type.
3475 if (SemaRef.RequireCompleteType(
3476 Loc: ILoc, T: VD->getType(), DiagID: diag::err_omp_threadprivate_incomplete_type)) {
3477 continue;
3478 }
3479
3480 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
3481 // A threadprivate variable must not have a reference type.
3482 if (VD->getType()->isReferenceType()) {
3483 llvm::omp::Version OMPVersion = getLangOpts().getOpenMPVersion();
3484 Diag(Loc: ILoc, DiagID: diag::err_omp_ref_type_arg)
3485 << getOpenMPDirectiveName(D: OMPD_threadprivate, V: OMPVersion)
3486 << VD->getType();
3487 bool IsDecl =
3488 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3489 Diag(Loc: VD->getLocation(),
3490 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3491 << VD;
3492 continue;
3493 }
3494
3495 // Check if this is a TLS variable. If TLS is not being supported, produce
3496 // the corresponding diagnostic.
3497 if ((VD->getTLSKind() != VarDecl::TLS_None &&
3498 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
3499 getLangOpts().OpenMPUseTLS &&
3500 getASTContext().getTargetInfo().isTLSSupported())) ||
3501 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
3502 !VD->isLocalVarDecl())) {
3503 Diag(Loc: ILoc, DiagID: diag::err_omp_var_thread_local)
3504 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
3505 bool IsDecl =
3506 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3507 Diag(Loc: VD->getLocation(),
3508 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3509 << VD;
3510 continue;
3511 }
3512
3513 // Check if initial value of threadprivate variable reference variable with
3514 // local storage (it is not supported by runtime).
3515 if (const Expr *Init = VD->getAnyInitializer()) {
3516 LocalVarRefChecker Checker(SemaRef);
3517 if (Checker.Visit(S: Init))
3518 continue;
3519 }
3520
3521 Vars.push_back(Elt: RefExpr);
3522 DSAStack->addDSA(D: VD, E: DE, A: OMPC_threadprivate);
3523 VD->addAttr(A: OMPThreadPrivateDeclAttr::CreateImplicit(
3524 Ctx&: Context, Range: SourceRange(Loc, Loc)));
3525 if (ASTMutationListener *ML = Context.getASTMutationListener())
3526 ML->DeclarationMarkedOpenMPThreadPrivate(D: VD);
3527 }
3528 OMPThreadPrivateDecl *D = nullptr;
3529 if (!Vars.empty()) {
3530 D = OMPThreadPrivateDecl::Create(C&: Context, DC: SemaRef.getCurLexicalContext(),
3531 L: Loc, VL: Vars);
3532 D->setAccess(AS_public);
3533 }
3534 return D;
3535}
3536
3537OMPGroupPrivateDecl *
3538SemaOpenMP::CheckOMPGroupPrivateDecl(SourceLocation Loc,
3539 ArrayRef<Expr *> VarList) {
3540 ASTContext &Context = getASTContext();
3541 SmallVector<Expr *, 8> Vars;
3542 for (Expr *RefExpr : VarList) {
3543 auto *DE = cast<DeclRefExpr>(Val: RefExpr);
3544 auto *VD = cast<VarDecl>(Val: DE->getDecl());
3545 SourceLocation ILoc = DE->getExprLoc();
3546
3547 // Mark variable as used.
3548 VD->setReferenced();
3549 VD->markUsed(C&: Context);
3550
3551 QualType QType = VD->getType();
3552 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
3553 // It will be analyzed later.
3554 Vars.push_back(Elt: DE);
3555 continue;
3556 }
3557
3558 // OpenMP groupprivate restrictions:
3559 // A groupprivate variable must not have an incomplete type.
3560 if (SemaRef.RequireCompleteType(
3561 Loc: ILoc, T: VD->getType(), DiagID: diag::err_omp_groupprivate_incomplete_type)) {
3562 continue;
3563 }
3564
3565 // A groupprivate variable must not have a reference type.
3566 if (VD->getType()->isReferenceType()) {
3567 Diag(Loc: ILoc, DiagID: diag::err_omp_ref_type_arg)
3568 << getOpenMPDirectiveName(D: OMPD_groupprivate) << VD->getType();
3569 bool IsDecl =
3570 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3571 Diag(Loc: VD->getLocation(),
3572 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3573 << VD;
3574 continue;
3575 }
3576
3577 // A variable that is declared with an initializer must not appear in a
3578 // groupprivate directive.
3579 if (VD->getAnyInitializer()) {
3580 Diag(Loc: ILoc, DiagID: diag::err_omp_groupprivate_with_initializer)
3581 << VD->getDeclName();
3582 bool IsDecl =
3583 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3584 Diag(Loc: VD->getLocation(),
3585 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3586 << VD;
3587 continue;
3588 }
3589
3590 Vars.push_back(Elt: RefExpr);
3591 DSAStack->addDSA(D: VD, E: DE, A: OMPC_groupprivate);
3592 VD->addAttr(A: OMPGroupPrivateDeclAttr::CreateImplicit(Ctx&: Context,
3593 Range: SourceRange(Loc, Loc)));
3594 if (ASTMutationListener *ML = Context.getASTMutationListener())
3595 ML->DeclarationMarkedOpenMPGroupPrivate(D: VD);
3596 }
3597 OMPGroupPrivateDecl *D = nullptr;
3598 if (!Vars.empty()) {
3599 D = OMPGroupPrivateDecl::Create(C&: Context, DC: SemaRef.getCurLexicalContext(),
3600 L: Loc, VL: Vars);
3601 D->setAccess(AS_public);
3602 }
3603 return D;
3604}
3605
3606static OMPAllocateDeclAttr::AllocatorTypeTy
3607getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) {
3608 if (!Allocator)
3609 return OMPAllocateDeclAttr::OMPNullMemAlloc;
3610 if (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
3611 Allocator->isInstantiationDependent() ||
3612 Allocator->containsUnexpandedParameterPack())
3613 return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
3614 auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
3615 llvm::FoldingSetNodeID AEId;
3616 const Expr *AE = Allocator->IgnoreParenImpCasts();
3617 AE->IgnoreImpCasts()->Profile(ID&: AEId, Context: S.getASTContext(), /*Canonical=*/true);
3618 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
3619 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
3620 const Expr *DefAllocator = Stack->getAllocator(AllocatorKind);
3621 llvm::FoldingSetNodeID DAEId;
3622 DefAllocator->IgnoreImpCasts()->Profile(ID&: DAEId, Context: S.getASTContext(),
3623 /*Canonical=*/true);
3624 if (AEId == DAEId) {
3625 AllocatorKindRes = AllocatorKind;
3626 break;
3627 }
3628 }
3629 return AllocatorKindRes;
3630}
3631
3632static bool checkPreviousOMPAllocateAttribute(
3633 Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD,
3634 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) {
3635 if (!VD->hasAttr<OMPAllocateDeclAttr>())
3636 return false;
3637 const auto *A = VD->getAttr<OMPAllocateDeclAttr>();
3638 Expr *PrevAllocator = A->getAllocator();
3639 OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind =
3640 getAllocatorKind(S, Stack, Allocator: PrevAllocator);
3641 bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind;
3642 if (AllocatorsMatch &&
3643 AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc &&
3644 Allocator && PrevAllocator) {
3645 const Expr *AE = Allocator->IgnoreParenImpCasts();
3646 const Expr *PAE = PrevAllocator->IgnoreParenImpCasts();
3647 llvm::FoldingSetNodeID AEId, PAEId;
3648 AE->Profile(ID&: AEId, Context: S.Context, /*Canonical=*/true);
3649 PAE->Profile(ID&: PAEId, Context: S.Context, /*Canonical=*/true);
3650 AllocatorsMatch = AEId == PAEId;
3651 }
3652 if (!AllocatorsMatch) {
3653 SmallString<256> AllocatorBuffer;
3654 llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer);
3655 if (Allocator)
3656 Allocator->printPretty(OS&: AllocatorStream, Helper: nullptr, Policy: S.getPrintingPolicy());
3657 SmallString<256> PrevAllocatorBuffer;
3658 llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer);
3659 if (PrevAllocator)
3660 PrevAllocator->printPretty(OS&: PrevAllocatorStream, Helper: nullptr,
3661 Policy: S.getPrintingPolicy());
3662
3663 SourceLocation AllocatorLoc =
3664 Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc();
3665 SourceRange AllocatorRange =
3666 Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange();
3667 SourceLocation PrevAllocatorLoc =
3668 PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation();
3669 SourceRange PrevAllocatorRange =
3670 PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange();
3671 S.Diag(Loc: AllocatorLoc, DiagID: diag::warn_omp_used_different_allocator)
3672 << (Allocator ? 1 : 0) << AllocatorStream.str()
3673 << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str()
3674 << AllocatorRange;
3675 S.Diag(Loc: PrevAllocatorLoc, DiagID: diag::note_omp_previous_allocator)
3676 << PrevAllocatorRange;
3677 return true;
3678 }
3679 return false;
3680}
3681
3682static void
3683applyOMPAllocateAttribute(Sema &S, VarDecl *VD,
3684 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
3685 Expr *Allocator, Expr *Alignment, SourceRange SR) {
3686 if (VD->hasAttr<OMPAllocateDeclAttr>())
3687 return;
3688 if (Alignment &&
3689 (Alignment->isTypeDependent() || Alignment->isValueDependent() ||
3690 Alignment->isInstantiationDependent() ||
3691 Alignment->containsUnexpandedParameterPack()))
3692 // Apply later when we have a usable value.
3693 return;
3694 if (Allocator &&
3695 (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
3696 Allocator->isInstantiationDependent() ||
3697 Allocator->containsUnexpandedParameterPack()))
3698 return;
3699 auto *A = OMPAllocateDeclAttr::CreateImplicit(Ctx&: S.Context, AllocatorType: AllocatorKind,
3700 Allocator, Alignment, Range: SR);
3701 VD->addAttr(A);
3702 if (ASTMutationListener *ML = S.Context.getASTMutationListener())
3703 ML->DeclarationMarkedOpenMPAllocate(D: VD, A);
3704}
3705
3706SemaOpenMP::DeclGroupPtrTy SemaOpenMP::ActOnOpenMPAllocateDirective(
3707 SourceLocation Loc, ArrayRef<Expr *> VarList, ArrayRef<OMPClause *> Clauses,
3708 DeclContext *Owner) {
3709 assert(Clauses.size() <= 2 && "Expected at most two clauses.");
3710 Expr *Alignment = nullptr;
3711 Expr *Allocator = nullptr;
3712 if (Clauses.empty()) {
3713 // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions.
3714 // allocate directives that appear in a target region must specify an
3715 // allocator clause unless a requires directive with the dynamic_allocators
3716 // clause is present in the same compilation unit.
3717 if (getLangOpts().OpenMPIsTargetDevice &&
3718 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
3719 SemaRef.targetDiag(Loc, DiagID: diag::err_expected_allocator_clause);
3720 } else {
3721 for (const OMPClause *C : Clauses)
3722 if (const auto *AC = dyn_cast<OMPAllocatorClause>(Val: C))
3723 Allocator = AC->getAllocator();
3724 else if (const auto *AC = dyn_cast<OMPAlignClause>(Val: C))
3725 Alignment = AC->getAlignment();
3726 else
3727 llvm_unreachable("Unexpected clause on allocate directive");
3728 }
3729 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
3730 getAllocatorKind(S&: SemaRef, DSAStack, Allocator);
3731 SmallVector<Expr *, 8> Vars;
3732 for (Expr *RefExpr : VarList) {
3733 auto *DE = cast<DeclRefExpr>(Val: RefExpr);
3734 auto *VD = cast<VarDecl>(Val: DE->getDecl());
3735
3736 // Check if this is a TLS variable or global register.
3737 if (VD->getTLSKind() != VarDecl::TLS_None ||
3738 VD->hasAttr<OMPThreadPrivateDeclAttr>() ||
3739 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
3740 !VD->isLocalVarDecl()))
3741 continue;
3742
3743 // If the used several times in the allocate directive, the same allocator
3744 // must be used.
3745 if (checkPreviousOMPAllocateAttribute(S&: SemaRef, DSAStack, RefExpr, VD,
3746 AllocatorKind, Allocator))
3747 continue;
3748
3749 // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++
3750 // If a list item has a static storage type, the allocator expression in the
3751 // allocator clause must be a constant expression that evaluates to one of
3752 // the predefined memory allocator values.
3753 if (Allocator && VD->hasGlobalStorage()) {
3754 if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) {
3755 Diag(Loc: Allocator->getExprLoc(),
3756 DiagID: diag::err_omp_expected_predefined_allocator)
3757 << Allocator->getSourceRange();
3758 bool IsDecl = VD->isThisDeclarationADefinition(getASTContext()) ==
3759 VarDecl::DeclarationOnly;
3760 Diag(Loc: VD->getLocation(),
3761 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3762 << VD;
3763 continue;
3764 }
3765 }
3766
3767 Vars.push_back(Elt: RefExpr);
3768 applyOMPAllocateAttribute(S&: SemaRef, VD, AllocatorKind, Allocator, Alignment,
3769 SR: DE->getSourceRange());
3770 // A jump over the allocation must be diagnosed, as for cleanup variables.
3771 if (VD->hasLocalStorage())
3772 SemaRef.setFunctionHasBranchProtectedScope();
3773 }
3774 if (Vars.empty())
3775 return nullptr;
3776 if (!Owner)
3777 Owner = SemaRef.getCurLexicalContext();
3778 auto *D = OMPAllocateDecl::Create(C&: getASTContext(), DC: Owner, L: Loc, VL: Vars, CL: Clauses);
3779 D->setAccess(AS_public);
3780 Owner->addDecl(D);
3781 return DeclGroupPtrTy::make(P: DeclGroupRef(D));
3782}
3783
3784SemaOpenMP::DeclGroupPtrTy
3785SemaOpenMP::ActOnOpenMPRequiresDirective(SourceLocation Loc,
3786 ArrayRef<OMPClause *> ClauseList) {
3787 OMPRequiresDecl *D = nullptr;
3788 if (!SemaRef.CurContext->isFileContext()) {
3789 Diag(Loc, DiagID: diag::err_omp_invalid_scope) << "requires";
3790 } else {
3791 D = CheckOMPRequiresDecl(Loc, Clauses: ClauseList);
3792 if (D) {
3793 SemaRef.CurContext->addDecl(D);
3794 DSAStack->addRequiresDecl(RD: D);
3795 }
3796 }
3797 return DeclGroupPtrTy::make(P: DeclGroupRef(D));
3798}
3799
3800void SemaOpenMP::ActOnOpenMPAssumesDirective(SourceLocation Loc,
3801 OpenMPDirectiveKind DKind,
3802 ArrayRef<std::string> Assumptions,
3803 bool SkippedClauses) {
3804 if (!SkippedClauses && Assumptions.empty()) {
3805 llvm::omp::Version OMPVersion = getLangOpts().getOpenMPVersion();
3806 Diag(Loc, DiagID: diag::err_omp_no_clause_for_directive)
3807 << llvm::omp::getAllAssumeClauseOptions()
3808 << llvm::omp::getOpenMPDirectiveName(D: DKind, V: OMPVersion);
3809 }
3810
3811 auto *AA =
3812 OMPAssumeAttr::Create(Ctx&: getASTContext(), Assumption: llvm::join(R&: Assumptions, Separator: ","), Range: Loc);
3813 if (DKind == llvm::omp::Directive::OMPD_begin_assumes) {
3814 OMPAssumeScoped.push_back(Elt: AA);
3815 return;
3816 }
3817
3818 // Global assumes without assumption clauses are ignored.
3819 if (Assumptions.empty())
3820 return;
3821
3822 assert(DKind == llvm::omp::Directive::OMPD_assumes &&
3823 "Unexpected omp assumption directive!");
3824 OMPAssumeGlobal.push_back(Elt: AA);
3825
3826 // The OMPAssumeGlobal scope above will take care of new declarations but
3827 // we also want to apply the assumption to existing ones, e.g., to
3828 // declarations in included headers. To this end, we traverse all existing
3829 // declaration contexts and annotate function declarations here.
3830 SmallVector<DeclContext *, 8> DeclContexts;
3831 auto *Ctx = SemaRef.CurContext;
3832 while (Ctx->getLexicalParent())
3833 Ctx = Ctx->getLexicalParent();
3834 DeclContexts.push_back(Elt: Ctx);
3835 while (!DeclContexts.empty()) {
3836 DeclContext *DC = DeclContexts.pop_back_val();
3837 for (auto *SubDC : DC->decls()) {
3838 if (SubDC->isInvalidDecl())
3839 continue;
3840 if (auto *CTD = dyn_cast<ClassTemplateDecl>(Val: SubDC)) {
3841 DeclContexts.push_back(Elt: CTD->getTemplatedDecl());
3842 llvm::append_range(C&: DeclContexts, R: CTD->specializations());
3843 continue;
3844 }
3845 if (auto *DC = dyn_cast<DeclContext>(Val: SubDC))
3846 DeclContexts.push_back(Elt: DC);
3847 if (auto *F = dyn_cast<FunctionDecl>(Val: SubDC)) {
3848 F->addAttr(A: AA);
3849 continue;
3850 }
3851 }
3852 }
3853}
3854
3855void SemaOpenMP::ActOnOpenMPEndAssumesDirective() {
3856 assert(isInOpenMPAssumeScope() && "Not in OpenMP assumes scope!");
3857 OMPAssumeScoped.pop_back();
3858}
3859
3860StmtResult SemaOpenMP::ActOnOpenMPAssumeDirective(ArrayRef<OMPClause *> Clauses,
3861 Stmt *AStmt,
3862 SourceLocation StartLoc,
3863 SourceLocation EndLoc) {
3864 if (!AStmt)
3865 return StmtError();
3866
3867 return OMPAssumeDirective::Create(Ctx: getASTContext(), StartLoc, EndLoc, Clauses,
3868 AStmt);
3869}
3870
3871StmtResult SemaOpenMP::ActOnOpenMPMetaDirective(
3872 SourceLocation StartLoc, SourceLocation EndLoc,
3873 ArrayRef<OMPTraitInfo *> TraitInfos, ArrayRef<Stmt *> Variants) {
3874 // Without an 'otherwise' clause, the last variant is the implicit one.
3875 bool HasImplicitOtherwise = Variants.size() == TraitInfos.size() + 1;
3876 assert((HasImplicitOtherwise || Variants.size() == TraitInfos.size()) &&
3877 "Expected one variant per metadirective clause");
3878 SmallVector<OMPTraitInfo *, 4> AllTraitInfos(TraitInfos);
3879 if (HasImplicitOtherwise)
3880 AllTraitInfos.push_back(Elt: &getASTContext().getNewOMPTraitInfo());
3881 // The user condition of each variant; null for 'otherwise'.
3882 SmallVector<Expr *, 4> Conditions;
3883 for (OMPTraitInfo *TI : AllTraitInfos) {
3884 Expr *Cond = nullptr;
3885 TI->anyScoreOrCondition(Cond: [&](Expr *&E, bool IsScore) {
3886 if (!IsScore)
3887 Cond = E;
3888 return !IsScore;
3889 });
3890 if (Cond) {
3891 ExprResult Val = SemaRef.CheckBooleanCondition(Loc: Cond->getExprLoc(), E: Cond);
3892 if (Val.isInvalid())
3893 return StmtError();
3894 Cond = SemaRef.MakeFullExpr(Arg: Val.get()).get();
3895 }
3896 Conditions.push_back(Elt: Cond);
3897 }
3898 return OMPMetaDirective::Create(C: getASTContext(), StartLoc, EndLoc, Variants,
3899 Conditions, TraitInfos: AllTraitInfos,
3900 HasImplicitOtherwise);
3901}
3902
3903OMPRequiresDecl *
3904SemaOpenMP::CheckOMPRequiresDecl(SourceLocation Loc,
3905 ArrayRef<OMPClause *> ClauseList) {
3906 /// For target specific clauses, the requires directive cannot be
3907 /// specified after the handling of any of the target regions in the
3908 /// current compilation unit.
3909 ArrayRef<SourceLocation> TargetLocations =
3910 DSAStack->getEncounteredTargetLocs();
3911 SourceLocation AtomicLoc = DSAStack->getAtomicDirectiveLoc();
3912 if (!TargetLocations.empty() || !AtomicLoc.isInvalid()) {
3913 for (const OMPClause *CNew : ClauseList) {
3914 // Check if any of the requires clauses affect target regions.
3915 if (isa<OMPUnifiedSharedMemoryClause>(Val: CNew) ||
3916 isa<OMPUnifiedAddressClause>(Val: CNew) ||
3917 isa<OMPReverseOffloadClause>(Val: CNew) ||
3918 isa<OMPDynamicAllocatorsClause>(Val: CNew)) {
3919 Diag(Loc, DiagID: diag::err_omp_directive_before_requires)
3920 << "target" << getOpenMPClauseNameForDiag(C: CNew->getClauseKind());
3921 for (SourceLocation TargetLoc : TargetLocations) {
3922 Diag(Loc: TargetLoc, DiagID: diag::note_omp_requires_encountered_directive)
3923 << "target";
3924 }
3925 } else if (!AtomicLoc.isInvalid() &&
3926 isa<OMPAtomicDefaultMemOrderClause>(Val: CNew)) {
3927 Diag(Loc, DiagID: diag::err_omp_directive_before_requires)
3928 << "atomic" << getOpenMPClauseNameForDiag(C: CNew->getClauseKind());
3929 Diag(Loc: AtomicLoc, DiagID: diag::note_omp_requires_encountered_directive)
3930 << "atomic";
3931 }
3932 }
3933 }
3934
3935 if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
3936 return OMPRequiresDecl::Create(
3937 C&: getASTContext(), DC: SemaRef.getCurLexicalContext(), L: Loc, CL: ClauseList);
3938 return nullptr;
3939}
3940
3941static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
3942 const ValueDecl *D,
3943 const DSAStackTy::DSAVarData &DVar,
3944 bool IsLoopIterVar) {
3945 if (DVar.RefExpr) {
3946 SemaRef.Diag(Loc: DVar.RefExpr->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
3947 << getOpenMPClauseNameForDiag(C: DVar.CKind);
3948 return;
3949 }
3950 enum {
3951 PDSA_StaticMemberShared,
3952 PDSA_StaticLocalVarShared,
3953 PDSA_LoopIterVarPrivate,
3954 PDSA_LoopIterVarLinear,
3955 PDSA_LoopIterVarLastprivate,
3956 PDSA_ConstVarShared,
3957 PDSA_GlobalVarShared,
3958 PDSA_TaskVarFirstprivate,
3959 PDSA_LocalVarPrivate,
3960 PDSA_Implicit
3961 } Reason = PDSA_Implicit;
3962 bool ReportHint = false;
3963 auto ReportLoc = D->getLocation();
3964 auto *VD = dyn_cast<VarDecl>(Val: D);
3965 if (IsLoopIterVar) {
3966 if (DVar.CKind == OMPC_private)
3967 Reason = PDSA_LoopIterVarPrivate;
3968 else if (DVar.CKind == OMPC_lastprivate)
3969 Reason = PDSA_LoopIterVarLastprivate;
3970 else
3971 Reason = PDSA_LoopIterVarLinear;
3972 } else if (isOpenMPTaskingDirective(Kind: DVar.DKind) &&
3973 DVar.CKind == OMPC_firstprivate) {
3974 Reason = PDSA_TaskVarFirstprivate;
3975 ReportLoc = DVar.ImplicitDSALoc;
3976 } else if (VD && VD->isStaticLocal())
3977 Reason = PDSA_StaticLocalVarShared;
3978 else if (VD && VD->isStaticDataMember())
3979 Reason = PDSA_StaticMemberShared;
3980 else if (VD && VD->isFileVarDecl())
3981 Reason = PDSA_GlobalVarShared;
3982 else if (D->getType().isConstant(Ctx: SemaRef.getASTContext()))
3983 Reason = PDSA_ConstVarShared;
3984 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
3985 ReportHint = true;
3986 Reason = PDSA_LocalVarPrivate;
3987 }
3988 if (Reason != PDSA_Implicit) {
3989 llvm::omp::Version OMPVersion = SemaRef.getLangOpts().getOpenMPVersion();
3990 SemaRef.Diag(Loc: ReportLoc, DiagID: diag::note_omp_predetermined_dsa)
3991 << Reason << ReportHint
3992 << getOpenMPDirectiveName(D: Stack->getCurrentDirective(), V: OMPVersion);
3993 } else if (DVar.ImplicitDSALoc.isValid()) {
3994 SemaRef.Diag(Loc: DVar.ImplicitDSALoc, DiagID: diag::note_omp_implicit_dsa)
3995 << getOpenMPClauseNameForDiag(C: DVar.CKind);
3996 }
3997}
3998
3999namespace {
4000/// Visitor to collect variables used in a statement.
4001class VarUsageVisitor : public DynamicRecursiveASTVisitor {
4002 llvm::SmallPtrSet<const VarDecl *, 8> &UsedVars;
4003 llvm::SmallPtrSet<const BindingDecl *, 8> &UsedBindings;
4004
4005public:
4006 VarUsageVisitor(llvm::SmallPtrSet<const VarDecl *, 8> &UsedVars,
4007 llvm::SmallPtrSet<const BindingDecl *, 8> &UsedBindings)
4008 : UsedVars(UsedVars), UsedBindings(UsedBindings) {}
4009
4010 bool VisitDeclRefExpr(DeclRefExpr *DRE) override {
4011 if (auto *VD = dyn_cast<VarDecl>(Val: DRE->getDecl()))
4012 UsedVars.insert(Ptr: cast<VarDecl>(Val: VD->getCanonicalDecl()));
4013 else if (auto *BD = dyn_cast<BindingDecl>(Val: DRE->getDecl()))
4014 UsedBindings.insert(Ptr: cast<BindingDecl>(Val: BD->getCanonicalDecl()));
4015 return true;
4016 }
4017};
4018} // namespace
4019
4020/// Check if bindings from the same structured binding have conflicting
4021/// capture kinds (by-ref vs by-copy). Bindings share the underlying
4022/// DecompositionDecl storage, so mixing by-ref and by-copy clauses on
4023/// different bindings from the same decomposition is not representable.
4024/// For example: map(a) creates by-ref, firstprivate(b) creates by-copy.
4025static bool checkDecompositionCaptureConflict(
4026 Sema &SemaRef, OpenMPDirectiveKind DKind,
4027 llvm::SmallDenseMap<const DecompositionDecl *,
4028 std::pair<bool, SourceLocation>, 4> &SeenDecompositions,
4029 const ValueDecl *D, SourceLocation ELoc, OpenMPClauseKind ClauseKind) {
4030 const auto *BD = dyn_cast<BindingDecl>(Val: D);
4031 if (!BD)
4032 return false;
4033
4034 const auto *DD = dyn_cast<DecompositionDecl>(Val: BD->getDecomposedDecl());
4035 if (!DD)
4036 return false;
4037
4038 // Determine if this clause creates by-ref or by-copy capture.
4039 bool IsByRef = false;
4040 switch (ClauseKind) {
4041 case OMPC_map:
4042 case OMPC_shared:
4043 // Map and shared clauses are by-reference.
4044 IsByRef = true;
4045 break;
4046 case OMPC_firstprivate:
4047 case OMPC_private:
4048 case OMPC_lastprivate:
4049 // These are by-copy.
4050 IsByRef = false;
4051 break;
4052 default:
4053 // Other clauses don't create capture conflicts.
4054 return false;
4055 }
4056 auto [It, Inserted] = SeenDecompositions.insert(KV: {DD, {IsByRef, ELoc}});
4057 if (!Inserted && It->second.first != IsByRef) {
4058 // Conflict: same DecompositionDecl needs both by-ref and by-copy
4059 // Emit diagnostic showing the binding name, not the decomposition.
4060 SemaRef.Diag(Loc: ELoc,
4061 DiagID: diag::err_omp_decomposition_bindings_different_capture_kinds)
4062 << BD;
4063 return true;
4064 }
4065 return false;
4066}
4067
4068/// Helper to check all clauses in a directive for structured binding
4069/// capture conflicts. Returns true if an error was found. If Body is
4070/// provided and the directive has default(shared), bindings used in the
4071/// body but not listed in explicit clauses are treated as implicit
4072/// shared (by-reference).
4073static bool checkClausesForDecompositionConflicts(Sema &SemaRef,
4074 OpenMPDirectiveKind DKind,
4075 ArrayRef<OMPClause *> Clauses,
4076 Stmt *Body = nullptr) {
4077 llvm::SmallDenseMap<const DecompositionDecl *,
4078 std::pair<bool, SourceLocation>, 4>
4079 SeenDecompositions;
4080 llvm::SmallPtrSet<const BindingDecl *, 8> ExplicitBindings;
4081 bool HasError = false;
4082 bool HasDefaultShared = false;
4083 SourceLocation DefaultSharedLoc;
4084 for (OMPClause *C : Clauses) {
4085 if (auto *DC = dyn_cast<OMPDefaultClause>(Val: C)) {
4086 if (DC->getDefaultKind() == llvm::omp::DefaultKind::OMP_DEFAULT_shared) {
4087 HasDefaultShared = true;
4088 DefaultSharedLoc = DC->getBeginLoc();
4089 }
4090 continue;
4091 }
4092 OpenMPClauseKind CK = C->getClauseKind();
4093 if (CK != OMPC_map && CK != OMPC_firstprivate && CK != OMPC_private &&
4094 CK != OMPC_shared && CK != OMPC_lastprivate)
4095 continue;
4096 ArrayRef<Expr *> Varlist;
4097 if (auto *MPC = dyn_cast<OMPMapClause>(Val: C))
4098 Varlist = MPC->varlist();
4099 else if (auto *FPC = dyn_cast<OMPFirstprivateClause>(Val: C))
4100 Varlist = FPC->varlist();
4101 else if (auto *PC = dyn_cast<OMPPrivateClause>(Val: C))
4102 Varlist = PC->varlist();
4103 else if (auto *SC = dyn_cast<OMPSharedClause>(Val: C))
4104 Varlist = SC->varlist();
4105 else if (auto *LPC = dyn_cast<OMPLastprivateClause>(Val: C))
4106 Varlist = LPC->varlist();
4107
4108 for (Expr *VE : Varlist) {
4109 if (auto *DRE = dyn_cast<DeclRefExpr>(Val: VE->IgnoreParenImpCasts())) {
4110 ValueDecl *D = DRE->getDecl();
4111 // Look through OMPCapturedExprDecl (used when clauses reference
4112 // variables from an enclosing captured region) to find the
4113 // underlying BindingDecl.
4114 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(Val: D)) {
4115 if (auto *InitDRE =
4116 dyn_cast<DeclRefExpr>(Val: CED->getInit()->IgnoreParenImpCasts()))
4117 D = InitDRE->getDecl();
4118 }
4119 if (auto *BD = dyn_cast<BindingDecl>(Val: D))
4120 ExplicitBindings.insert(Ptr: cast<BindingDecl>(Val: BD->getCanonicalDecl()));
4121 if (checkDecompositionCaptureConflict(
4122 SemaRef, DKind, SeenDecompositions, D, ELoc: VE->getExprLoc(), ClauseKind: CK))
4123 HasError = true;
4124 }
4125 }
4126 }
4127
4128 // With default(shared), bindings used in the body but not in an explicit
4129 // clause become implicit shared (by-reference). Check those for conflicts
4130 // with explicit by-copy clauses on the same decomposition.
4131 if (HasDefaultShared && Body) {
4132 llvm::SmallPtrSet<const VarDecl *, 8> UsedVars;
4133 llvm::SmallPtrSet<const BindingDecl *, 8> UsedBindings;
4134 VarUsageVisitor Visitor(UsedVars, UsedBindings);
4135 Visitor.TraverseStmt(S: Body);
4136 for (const BindingDecl *BD : UsedBindings) {
4137 if (ExplicitBindings.contains(Ptr: BD))
4138 continue;
4139 if (checkDecompositionCaptureConflict(SemaRef, DKind, SeenDecompositions,
4140 D: BD, ELoc: DefaultSharedLoc, ClauseKind: OMPC_shared))
4141 HasError = true;
4142 }
4143 }
4144 return HasError;
4145}
4146
4147/// Check if original variable is explicitly mapped but only bindings are used.
4148/// Returns true if an error was found.
4149static bool checkOriginalVarMappedButOnlyBindingsUsed(
4150 Sema &SemaRef, ArrayRef<OMPClause *> Clauses, Stmt *Body) {
4151 llvm::SmallDenseMap<const VarDecl *, SourceLocation, 4> MappedOrigVars;
4152
4153 for (OMPClause *C : Clauses) {
4154 if (auto *MPC = dyn_cast<OMPMapClause>(Val: C)) {
4155 for (Expr *VE : MPC->varlist()) {
4156 if (auto *DRE = dyn_cast<DeclRefExpr>(Val: VE->IgnoreParenImpCasts())) {
4157 if (auto *VD = dyn_cast<VarDecl>(Val: DRE->getDecl())) {
4158 MappedOrigVars[VD->getCanonicalDecl()] = VE->getExprLoc();
4159 }
4160 }
4161 }
4162 }
4163 }
4164 if (MappedOrigVars.empty())
4165 return false;
4166
4167 llvm::SmallPtrSet<const VarDecl *, 8> UsedVars;
4168 llvm::SmallPtrSet<const BindingDecl *, 8> UsedBindings;
4169 VarUsageVisitor Visitor(UsedVars, UsedBindings);
4170 Visitor.TraverseStmt(S: Body);
4171 bool HasError = false;
4172 for (const auto &Entry : MappedOrigVars) {
4173 const VarDecl *OrigVar = Entry.first;
4174 SourceLocation Loc = Entry.second;
4175
4176 // Check if this original variable has bindings that are used.
4177 // For reference bindings (`auto &[a, b] = p`), the bindings alias the
4178 // original variable, so mapping `p` covers them correctly.
4179 bool BindingsFromThisVarUsed = false;
4180 for (const BindingDecl *BD : UsedBindings) {
4181 if (auto *DD = dyn_cast<DecompositionDecl>(Val: BD->getDecomposedDecl())) {
4182 if (DD->getType()->isReferenceType())
4183 continue;
4184 if (auto *OrigFromDD = DD->getOriginalVar().Var) {
4185 if (OrigFromDD->getCanonicalDecl() == OrigVar) {
4186 BindingsFromThisVarUsed = true;
4187 break;
4188 }
4189 }
4190 }
4191 }
4192
4193 // Error if: original is mapped, bindings are used, but original is not
4194 // used.
4195 if (BindingsFromThisVarUsed && !UsedVars.count(Ptr: OrigVar)) {
4196 SemaRef.Diag(Loc, DiagID: diag::err_omp_original_var_mapped_bindings_only_used)
4197 << OrigVar;
4198 HasError = true;
4199 }
4200 }
4201
4202 return HasError;
4203}
4204
4205static OpenMPMapClauseKind
4206getMapClauseKindFromModifier(OpenMPDefaultmapClauseModifier M,
4207 bool IsAggregateOrDeclareTarget,
4208 bool HasConstQualifier) {
4209 OpenMPMapClauseKind Kind = OMPC_MAP_unknown;
4210 switch (M) {
4211 case OMPC_DEFAULTMAP_MODIFIER_alloc:
4212 case OMPC_DEFAULTMAP_MODIFIER_storage:
4213 Kind = OMPC_MAP_alloc;
4214 break;
4215 case OMPC_DEFAULTMAP_MODIFIER_to:
4216 Kind = OMPC_MAP_to;
4217 break;
4218 case OMPC_DEFAULTMAP_MODIFIER_from:
4219 Kind = OMPC_MAP_from;
4220 break;
4221 case OMPC_DEFAULTMAP_MODIFIER_tofrom:
4222 Kind = OMPC_MAP_tofrom;
4223 break;
4224 case OMPC_DEFAULTMAP_MODIFIER_present:
4225 // OpenMP 5.1 [2.21.7.3] defaultmap clause, Description]
4226 // If implicit-behavior is present, each variable referenced in the
4227 // construct in the category specified by variable-category is treated as if
4228 // it had been listed in a map clause with the map-type of alloc and
4229 // map-type-modifier of present.
4230 Kind = OMPC_MAP_alloc;
4231 break;
4232 case OMPC_DEFAULTMAP_MODIFIER_firstprivate:
4233 case OMPC_DEFAULTMAP_MODIFIER_private:
4234 case OMPC_DEFAULTMAP_MODIFIER_last:
4235 llvm_unreachable("Unexpected defaultmap implicit behavior");
4236 case OMPC_DEFAULTMAP_MODIFIER_none:
4237 case OMPC_DEFAULTMAP_MODIFIER_default:
4238 case OMPC_DEFAULTMAP_MODIFIER_unknown:
4239 // IsAggregateOrDeclareTarget could be true if:
4240 // 1. the implicit behavior for aggregate is tofrom
4241 // 2. it's a declare target link
4242 if (IsAggregateOrDeclareTarget) {
4243 if (HasConstQualifier)
4244 Kind = OMPC_MAP_to;
4245 else
4246 Kind = OMPC_MAP_tofrom;
4247 break;
4248 }
4249 llvm_unreachable("Unexpected defaultmap implicit behavior");
4250 }
4251 assert(Kind != OMPC_MAP_unknown && "Expect map kind to be known");
4252 return Kind;
4253}
4254
4255static bool hasNoMutableFields(const CXXRecordDecl *RD) {
4256 for (const auto *FD : RD->fields()) {
4257 if (FD->isMutable())
4258 return false;
4259 QualType FT = FD->getType();
4260 while (FT->isArrayType())
4261 FT = FT->getAsArrayTypeUnsafe()->getElementType();
4262 if (const auto *NestedRD = FT->getAsCXXRecordDecl())
4263 if (!hasNoMutableFields(RD: NestedRD))
4264 return false;
4265 }
4266 return true;
4267}
4268
4269static bool hasConstQualifiedMappingType(QualType T) {
4270 while (T->isArrayType())
4271 T = T->getAsArrayTypeUnsafe()->getElementType();
4272 if (!T.isConstQualified())
4273 return false;
4274 if (const auto *RD = T->getAsCXXRecordDecl())
4275 // TODO : Per OpenMP 6.0 p299 lines 3-4, non-mutable members of a
4276 // const-qualified struct should also be ignored for 'from'. This
4277 // requires per-member mapping granularity via compiler-generated
4278 // default mappers and a mechanism to ensure constness to the mapper.
4279 // For now we conservatively treat any struct with mutable members as
4280 // requiring full 'tofrom'.
4281 return hasNoMutableFields(RD);
4282 return true;
4283}
4284
4285namespace {
4286/// Try to extract the original variable from a DecompositionDecl.
4287/// If extraction fails, emit a diagnostic. Returns the original VarDecl* on
4288/// success, nullptr on failure.
4289static const VarDecl *getOriginalVarOrDiagnose(Sema &S,
4290 const DecompositionDecl *DD,
4291 SourceLocation Loc) {
4292 auto Result = DD->getOriginalVar();
4293 if (!Result.Var)
4294 S.Diag(Loc, DiagID: diag::err_omp_unsupported_structured_binding_init)
4295 << Result.DiagKind;
4296 return Result.Var;
4297}
4298
4299struct VariableImplicitInfo {
4300 static const unsigned MapKindNum = OMPC_MAP_unknown;
4301 static const unsigned DefaultmapKindNum = OMPC_DEFAULTMAP_unknown + 1;
4302
4303 llvm::SetVector<Expr *> Privates;
4304 llvm::SetVector<Expr *> Firstprivates;
4305 llvm::SetVector<Expr *> Mappings[DefaultmapKindNum][MapKindNum];
4306 llvm::SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers>
4307 MapModifiers[DefaultmapKindNum];
4308};
4309
4310class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
4311 DSAStackTy *Stack;
4312 Sema &SemaRef;
4313 OpenMPDirectiveKind DKind = OMPD_unknown;
4314 bool ErrorFound = false;
4315 bool TryCaptureCXXThisMembers = false;
4316 CapturedStmt *CS = nullptr;
4317
4318 VariableImplicitInfo ImpInfo;
4319 SemaOpenMP::VarsWithInheritedDSAType VarsWithInheritedDSA;
4320 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
4321
4322 void VisitSubCaptures(OMPExecutableDirective *S) {
4323 // Check implicitly captured variables.
4324 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt())
4325 return;
4326 if (S->getDirectiveKind() == OMPD_atomic ||
4327 S->getDirectiveKind() == OMPD_critical ||
4328 S->getDirectiveKind() == OMPD_section ||
4329 S->getDirectiveKind() == OMPD_master ||
4330 S->getDirectiveKind() == OMPD_masked ||
4331 S->getDirectiveKind() == OMPD_scope ||
4332 S->getDirectiveKind() == OMPD_assume ||
4333 isOpenMPLoopTransformationDirective(DKind: S->getDirectiveKind())) {
4334 Visit(S: S->getAssociatedStmt());
4335 return;
4336 }
4337 visitSubCaptures(S: S->getInnermostCapturedStmt());
4338 // Try to capture inner this->member references to generate correct mappings
4339 // and diagnostics.
4340 if (TryCaptureCXXThisMembers ||
4341 (isOpenMPTargetExecutionDirective(DKind) &&
4342 llvm::any_of(Range: S->getInnermostCapturedStmt()->captures(),
4343 P: [](const CapturedStmt::Capture &C) {
4344 return C.capturesThis();
4345 }))) {
4346 bool SavedTryCaptureCXXThisMembers = TryCaptureCXXThisMembers;
4347 TryCaptureCXXThisMembers = true;
4348 Visit(S: S->getInnermostCapturedStmt()->getCapturedStmt());
4349 TryCaptureCXXThisMembers = SavedTryCaptureCXXThisMembers;
4350 }
4351 // In tasks firstprivates are not captured anymore, need to analyze them
4352 // explicitly.
4353 if (isOpenMPTaskingDirective(Kind: S->getDirectiveKind()) &&
4354 !isOpenMPTaskLoopDirective(DKind: S->getDirectiveKind())) {
4355 for (OMPClause *C : S->clauses())
4356 if (auto *FC = dyn_cast<OMPFirstprivateClause>(Val: C)) {
4357 for (Expr *Ref : FC->varlist())
4358 Visit(S: Ref);
4359 }
4360 }
4361 }
4362
4363public:
4364 void VisitDeclRefExpr(DeclRefExpr *E) {
4365 if (TryCaptureCXXThisMembers || E->isTypeDependent() ||
4366 E->isValueDependent() || E->containsUnexpandedParameterPack() ||
4367 E->isInstantiationDependent() ||
4368 E->isNonOdrUse() == clang::NOUR_Unevaluated)
4369 return;
4370 ValueDecl *D = const_cast<ValueDecl *>(E->getDecl());
4371 BindingDecl *BD = dyn_cast<BindingDecl>(Val: D);
4372 if (BD)
4373 D = BD->getDecomposedDecl();
4374 if (auto *VD = dyn_cast<VarDecl>(Val: D)) {
4375 // For BindingDecls, use the original binding for DSA lookups;
4376 // fall back to DecompositionDecl if lookup fails.
4377 ValueDecl *LookupDecl = BD ? static_cast<ValueDecl *>(BD) : VD;
4378 // Check the datasharing rules for the expressions in the clauses.
4379 if (!CS || (isa<OMPCapturedExprDecl>(Val: VD) && !CS->capturesVariable(Var: VD) &&
4380 !Stack->getTopDSA(D: LookupDecl, /*FromParent=*/false).RefExpr &&
4381 !Stack->isImplicitDefaultFirstprivateFD(VD))) {
4382 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(Val: VD))
4383 if (!CED->hasAttr<OMPCaptureNoInitAttr>()) {
4384 Visit(S: const_cast<Expr *>(CED->getInit()));
4385 return;
4386 }
4387 } else if (VD->isImplicit() || isa<OMPCapturedExprDecl>(Val: VD))
4388 // Do not analyze internal variables and do not enclose them into
4389 // implicit clauses.
4390 if (!Stack->isImplicitDefaultFirstprivateFD(VD))
4391 return;
4392 VD = VD->getCanonicalDecl();
4393 // Skip DecompositionDecls (but not BindingDecls in DSA contexts) -
4394 // they should be handled through explicit mapping of the original
4395 // variable or as member expressions. When bindings are captured,
4396 // the original variable is what needs to be mapped, not the
4397 // decomposition itself. However, BindingDecls need DSA checking for
4398 // default(none)/private/firstprivate contexts, but NOT in target
4399 // offloading contexts (where map clause handles them).
4400 if (isa<DecompositionDecl>(Val: VD)) {
4401 // For BindingDecls, continue checking only if:
4402 // - We are in a DSA context (tasking/parallel/worksharing/teams), or
4403 // - We are in a target context and the binding is scalar (implicit
4404 // firstprivate).
4405 bool InTargetAndScalar = BD &&
4406 isOpenMPTargetExecutionDirective(DKind) &&
4407 BD->getType()->isScalarType();
4408 if (!BD ||
4409 (isOpenMPTargetExecutionDirective(DKind) && !InTargetAndScalar) ||
4410 (!InTargetAndScalar && !isImplicitOrExplicitTaskingRegion(DKind) &&
4411 !isOpenMPParallelDirective(DKind) &&
4412 !isOpenMPWorksharingDirective(DKind) &&
4413 !isOpenMPTeamsDirective(DKind)))
4414 return;
4415 }
4416 // Skip internally declared variables.
4417 if (VD->hasLocalStorage() && CS && !CS->capturesVariable(Var: VD) &&
4418 !Stack->isImplicitDefaultFirstprivateFD(VD) &&
4419 !Stack->isImplicitTaskFirstprivate(D: VD))
4420 return;
4421 // Skip allocators in uses_allocators clauses.
4422 if (Stack->isUsesAllocatorsDecl(D: VD))
4423 return;
4424
4425 DSAStackTy::DSAVarData DVar =
4426 Stack->getTopDSA(D: LookupDecl, /*FromParent=*/false);
4427 // If lookup on BindingDecl failed, try on DecompositionDecl.
4428 if (BD && !DVar.RefExpr)
4429 DVar = Stack->getTopDSA(D: VD, /*FromParent=*/false);
4430 // Check if the variable has explicit DSA set and stop analysis if it so.
4431 if (DVar.RefExpr ||
4432 !ImplicitDeclarations
4433 .insert(V: cast<ValueDecl>(Val: LookupDecl->getCanonicalDecl()))
4434 .second)
4435 return;
4436
4437 // Skip internally declared static variables.
4438 std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
4439 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
4440 if (VD->hasGlobalStorage() && CS && !CS->capturesVariable(Var: VD) &&
4441 (Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
4442 !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link) &&
4443 !Stack->isImplicitDefaultFirstprivateFD(VD) &&
4444 !Stack->isImplicitTaskFirstprivate(D: VD))
4445 return;
4446
4447 SourceLocation ELoc = E->getExprLoc();
4448 // The default(none) clause requires that each variable that is referenced
4449 // in the construct, and does not have a predetermined data-sharing
4450 // attribute, must have its data-sharing attribute explicitly determined
4451 // by being listed in a data-sharing attribute clause.
4452 if (DVar.CKind == OMPC_unknown &&
4453 (Stack->getDefaultDSA() == DSA_none ||
4454 Stack->getDefaultDSA() == DSA_private ||
4455 Stack->getDefaultDSA() == DSA_firstprivate) &&
4456 isImplicitOrExplicitTaskingRegion(DKind) &&
4457 VarsWithInheritedDSA.count(Val: VD) == 0) {
4458 bool InheritedDSA = Stack->getDefaultDSA() == DSA_none;
4459 if (!InheritedDSA && (Stack->getDefaultDSA() == DSA_firstprivate ||
4460 Stack->getDefaultDSA() == DSA_private)) {
4461 DSAStackTy::DSAVarData DVar =
4462 Stack->getImplicitDSA(D: LookupDecl, /*FromParent=*/false);
4463 if (BD && DVar.CKind == OMPC_unknown)
4464 DVar = Stack->getImplicitDSA(D: VD, /*FromParent=*/false);
4465 InheritedDSA = DVar.CKind == OMPC_unknown;
4466 }
4467 if (InheritedDSA)
4468 VarsWithInheritedDSA[cast<ValueDecl>(
4469 Val: LookupDecl->getCanonicalDecl())] = E;
4470 if (Stack->getDefaultDSA() == DSA_none)
4471 return;
4472 }
4473
4474 // OpenMP 5.0 [2.19.7.2, defaultmap clause, Description]
4475 // If implicit-behavior is none, each variable referenced in the
4476 // construct that does not have a predetermined data-sharing attribute
4477 // and does not appear in a to or link clause on a declare target
4478 // directive must be listed in a data-mapping attribute clause, a
4479 // data-sharing attribute clause (including a data-sharing attribute
4480 // clause on a combined construct where target. is one of the
4481 // constituent constructs), or an is_device_ptr clause.
4482 // For BindingDecls, use the binding's type to determine category.
4483 OpenMPDefaultmapClauseKind ClauseKind = getVariableCategoryFromDecl(
4484 LO: SemaRef.getLangOpts(), VD: BD ? static_cast<const ValueDecl *>(BD)
4485 : static_cast<const ValueDecl *>(VD));
4486 if (SemaRef.getLangOpts().OpenMP >= 50) {
4487 bool IsModifierNone = Stack->getDefaultmapModifier(Kind: ClauseKind) ==
4488 OMPC_DEFAULTMAP_MODIFIER_none;
4489 if (DVar.CKind == OMPC_unknown && IsModifierNone &&
4490 VarsWithInheritedDSA.count(Val: VD) == 0 && !Res) {
4491 // Only check for data-mapping attribute and is_device_ptr here
4492 // since we have already make sure that the declaration does not
4493 // have a data-sharing attribute above
4494 if (!Stack->checkMappableExprComponentListsForDecl(
4495 VD, /*CurrentRegionOnly=*/true,
4496 Check: [VD](OMPClauseMappableExprCommon::MappableExprComponentListRef
4497 MapExprComponents,
4498 OpenMPClauseKind) {
4499 auto MI = MapExprComponents.rbegin();
4500 auto ME = MapExprComponents.rend();
4501 return MI != ME && MI->getAssociatedDeclaration() == VD;
4502 })) {
4503 VarsWithInheritedDSA[cast<ValueDecl>(
4504 Val: LookupDecl->getCanonicalDecl())] = E;
4505 return;
4506 }
4507 }
4508 }
4509 if (SemaRef.getLangOpts().OpenMP > 50) {
4510 bool IsModifierPresent = Stack->getDefaultmapModifier(Kind: ClauseKind) ==
4511 OMPC_DEFAULTMAP_MODIFIER_present;
4512 if (IsModifierPresent) {
4513 if (!llvm::is_contained(Range&: ImpInfo.MapModifiers[ClauseKind],
4514 Element: OMPC_MAP_MODIFIER_present)) {
4515 ImpInfo.MapModifiers[ClauseKind].push_back(
4516 Elt: OMPC_MAP_MODIFIER_present);
4517 }
4518 }
4519 }
4520
4521 if (isOpenMPTargetExecutionDirective(DKind) &&
4522 !Stack->isLoopControlVariable(D: VD).first) {
4523 // Check if VD is already mapped. For DecompositionDecls, also check if
4524 // the original variable they decompose has been mapped (via BindingDecl
4525 // map clauses).
4526 bool AlreadyMapped = Stack->checkMappableExprComponentListsForDecl(
4527 VD, /*CurrentRegionOnly=*/true, Check: [this](auto StackComponents, auto) {
4528 if (SemaRef.LangOpts.OpenMP >= 50)
4529 return !StackComponents.empty();
4530 // Variable is used if it has been marked as an array, array
4531 // section, array shaping or the variable itself.
4532 return StackComponents.size() == 1 ||
4533 llvm::all_of(
4534 llvm::drop_begin(llvm::reverse(StackComponents)),
4535 [](const auto &MC) {
4536 return MC.getAssociatedDeclaration() == nullptr &&
4537 (isa<ArraySectionExpr>(
4538 MC.getAssociatedExpression()) ||
4539 isa<OMPArrayShapingExpr>(
4540 MC.getAssociatedExpression()) ||
4541 isa<ArraySubscriptExpr>(
4542 MC.getAssociatedExpression()));
4543 });
4544 });
4545
4546 // For DecompositionDecls, check if the original variable has been
4547 // mapped.
4548 const auto *DD = dyn_cast<DecompositionDecl>(Val: VD);
4549 if (!AlreadyMapped && DD) {
4550 // Don't diagnose here. Just check if we can extract the original
4551 // var. Diagnostics happen when processing explicit map clauses.
4552 if (const VarDecl *OrigVar = DD->getOriginalVar().Var) {
4553 AlreadyMapped = Stack->checkMappableExprComponentListsForDecl(
4554 VD: OrigVar, /*CurrentRegionOnly=*/true,
4555 Check: [this](auto StackComponents, auto) {
4556 if (SemaRef.LangOpts.OpenMP >= 50)
4557 return !StackComponents.empty();
4558 return StackComponents.size() == 1;
4559 });
4560 }
4561 }
4562 if (!AlreadyMapped) {
4563 bool IsFirstprivate = false;
4564 // By default lambdas are captured as firstprivates.
4565 QualType CheckType = BD ? BD->getType() : VD->getType();
4566 if (const auto *RD =
4567 CheckType.getNonReferenceType()->getAsCXXRecordDecl())
4568 IsFirstprivate = RD->isLambda();
4569 IsFirstprivate =
4570 IsFirstprivate || (Stack->mustBeFirstprivate(Kind: ClauseKind) && !Res);
4571 if (IsFirstprivate) {
4572 ImpInfo.Firstprivates.insert(X: E);
4573 } else {
4574 OpenMPDefaultmapClauseModifier M =
4575 Stack->getDefaultmapModifier(Kind: ClauseKind);
4576 if (M == OMPC_DEFAULTMAP_MODIFIER_private) {
4577 ImpInfo.Privates.insert(X: E);
4578 } else {
4579 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier(
4580 M, IsAggregateOrDeclareTarget: ClauseKind == OMPC_DEFAULTMAP_aggregate || Res,
4581 HasConstQualifier: hasConstQualifiedMappingType(T: E->getType()));
4582 ImpInfo.Mappings[ClauseKind][Kind].insert(X: E);
4583 }
4584 }
4585 return;
4586 }
4587 }
4588
4589 // OpenMP [2.9.3.6, Restrictions, p.2]
4590 // A list item that appears in a reduction clause of the innermost
4591 // enclosing worksharing or parallel construct may not be accessed in an
4592 // explicit task.
4593 DVar = Stack->hasInnermostDSA(
4594 D: LookupDecl,
4595 CPred: [](OpenMPClauseKind C, bool AppliedToPointee) {
4596 return C == OMPC_reduction && !AppliedToPointee;
4597 },
4598 DPred: [](OpenMPDirectiveKind K) {
4599 return isOpenMPParallelDirective(DKind: K) ||
4600 isOpenMPWorksharingDirective(DKind: K) || isOpenMPTeamsDirective(DKind: K);
4601 },
4602 /*FromParent=*/true);
4603 if (isOpenMPTaskingDirective(Kind: DKind) && DVar.CKind == OMPC_reduction) {
4604 ErrorFound = true;
4605 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_reduction_in_task);
4606 reportOriginalDsa(SemaRef, Stack, D: BD ? LookupDecl : VD, DVar);
4607 return;
4608 }
4609
4610 // Define implicit data-sharing attributes for task.
4611 DVar = Stack->getImplicitDSA(D: LookupDecl, /*FromParent=*/false);
4612 if (BD && DVar.CKind == OMPC_unknown)
4613 DVar = Stack->getImplicitDSA(D: VD, /*FromParent=*/false);
4614 if (((isOpenMPTaskingDirective(Kind: DKind) && DVar.CKind != OMPC_shared) ||
4615 (((Stack->getDefaultDSA() == DSA_firstprivate &&
4616 DVar.CKind == OMPC_firstprivate) ||
4617 (Stack->getDefaultDSA() == DSA_private &&
4618 DVar.CKind == OMPC_private)) &&
4619 !DVar.RefExpr)) &&
4620 !Stack->isLoopControlVariable(D: VD).first) {
4621 if (Stack->getDefaultDSA() == DSA_private)
4622 ImpInfo.Privates.insert(X: E);
4623 else
4624 ImpInfo.Firstprivates.insert(X: E);
4625 return;
4626 }
4627
4628 // Store implicitly used globals with declare target link for parent
4629 // target.
4630 if (!isOpenMPTargetExecutionDirective(DKind) && Res &&
4631 *Res == OMPDeclareTargetDeclAttr::MT_Link) {
4632 Stack->addToParentTargetRegionLinkGlobals(E);
4633 return;
4634 }
4635 }
4636 }
4637 void VisitMemberExpr(MemberExpr *E) {
4638 if (E->isTypeDependent() || E->isValueDependent() ||
4639 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
4640 return;
4641 auto *FD = dyn_cast<FieldDecl>(Val: E->getMemberDecl());
4642 if (auto *TE = dyn_cast<CXXThisExpr>(Val: E->getBase()->IgnoreParenCasts())) {
4643 if (!FD)
4644 return;
4645 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D: FD, /*FromParent=*/false);
4646 // Check if the variable has explicit DSA set and stop analysis if it
4647 // so.
4648 if (DVar.RefExpr || !ImplicitDeclarations.insert(V: FD).second)
4649 return;
4650
4651 if (isOpenMPTargetExecutionDirective(DKind) &&
4652 !Stack->isLoopControlVariable(D: FD).first &&
4653 !Stack->checkMappableExprComponentListsForDecl(
4654 VD: FD, /*CurrentRegionOnly=*/true,
4655 Check: [](OMPClauseMappableExprCommon::MappableExprComponentListRef
4656 StackComponents,
4657 OpenMPClauseKind) {
4658 return isa<CXXThisExpr>(
4659 Val: cast<MemberExpr>(
4660 Val: StackComponents.back().getAssociatedExpression())
4661 ->getBase()
4662 ->IgnoreParens());
4663 })) {
4664 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
4665 // A bit-field cannot appear in a map clause.
4666 //
4667 if (FD->isBitField())
4668 return;
4669
4670 // Check to see if the member expression is referencing a class that
4671 // has already been explicitly mapped
4672 if (Stack->isClassPreviouslyMapped(QT: TE->getType()))
4673 return;
4674
4675 OpenMPDefaultmapClauseModifier Modifier =
4676 Stack->getDefaultmapModifier(Kind: OMPC_DEFAULTMAP_aggregate);
4677 OpenMPDefaultmapClauseKind ClauseKind =
4678 getVariableCategoryFromDecl(LO: SemaRef.getLangOpts(), VD: FD);
4679 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier(
4680 M: Modifier, /*IsAggregateOrDeclareTarget=*/true,
4681 /*HasConstQualifier=*/false);
4682 ImpInfo.Mappings[ClauseKind][Kind].insert(X: E);
4683 return;
4684 }
4685
4686 SourceLocation ELoc = E->getExprLoc();
4687 // OpenMP [2.9.3.6, Restrictions, p.2]
4688 // A list item that appears in a reduction clause of the innermost
4689 // enclosing worksharing or parallel construct may not be accessed in
4690 // an explicit task.
4691 DVar = Stack->hasInnermostDSA(
4692 D: FD,
4693 CPred: [](OpenMPClauseKind C, bool AppliedToPointee) {
4694 return C == OMPC_reduction && !AppliedToPointee;
4695 },
4696 DPred: [](OpenMPDirectiveKind K) {
4697 return isOpenMPParallelDirective(DKind: K) ||
4698 isOpenMPWorksharingDirective(DKind: K) || isOpenMPTeamsDirective(DKind: K);
4699 },
4700 /*FromParent=*/true);
4701 if (isOpenMPTaskingDirective(Kind: DKind) && DVar.CKind == OMPC_reduction) {
4702 ErrorFound = true;
4703 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_reduction_in_task);
4704 reportOriginalDsa(SemaRef, Stack, D: FD, DVar);
4705 return;
4706 }
4707
4708 // Define implicit data-sharing attributes for task.
4709 DVar = Stack->getImplicitDSA(D: FD, /*FromParent=*/false);
4710 if (isOpenMPTaskingDirective(Kind: DKind) && DVar.CKind != OMPC_shared &&
4711 !Stack->isLoopControlVariable(D: FD).first) {
4712 // Check if there is a captured expression for the current field in the
4713 // region. Do not mark it as firstprivate unless there is no captured
4714 // expression.
4715 // TODO: try to make it firstprivate.
4716 if (DVar.CKind != OMPC_unknown)
4717 ImpInfo.Firstprivates.insert(X: E);
4718 }
4719 return;
4720 }
4721 if (isOpenMPTargetExecutionDirective(DKind)) {
4722 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
4723 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, CKind: OMPC_map,
4724 DKind, /*NoDiagnose=*/true))
4725 return;
4726 const auto *VD = cast<ValueDecl>(
4727 Val: CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
4728 if (!Stack->checkMappableExprComponentListsForDecl(
4729 VD, /*CurrentRegionOnly=*/true,
4730 Check: [&CurComponents](
4731 OMPClauseMappableExprCommon::MappableExprComponentListRef
4732 StackComponents,
4733 OpenMPClauseKind) {
4734 auto CCI = CurComponents.rbegin();
4735 auto CCE = CurComponents.rend();
4736 for (const auto &SC : llvm::reverse(C&: StackComponents)) {
4737 // Do both expressions have the same kind?
4738 if (CCI->getAssociatedExpression()->getStmtClass() !=
4739 SC.getAssociatedExpression()->getStmtClass())
4740 if (!((isa<ArraySectionExpr>(
4741 Val: SC.getAssociatedExpression()) ||
4742 isa<OMPArrayShapingExpr>(
4743 Val: SC.getAssociatedExpression())) &&
4744 isa<ArraySubscriptExpr>(
4745 Val: CCI->getAssociatedExpression())))
4746 return false;
4747
4748 const Decl *CCD = CCI->getAssociatedDeclaration();
4749 const Decl *SCD = SC.getAssociatedDeclaration();
4750 CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
4751 SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
4752 if (SCD != CCD)
4753 return false;
4754 std::advance(i&: CCI, n: 1);
4755 if (CCI == CCE)
4756 break;
4757 }
4758 return true;
4759 })) {
4760 Visit(S: E->getBase());
4761 }
4762 } else if (!TryCaptureCXXThisMembers) {
4763 Visit(S: E->getBase());
4764 }
4765 }
4766 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
4767 for (OMPClause *C : S->clauses()) {
4768 // Skip analysis of arguments of private clauses for task|target
4769 // directives.
4770 if (isa_and_nonnull<OMPPrivateClause>(Val: C))
4771 continue;
4772 // Skip analysis of arguments of implicitly defined firstprivate clause
4773 // for task|target directives.
4774 // Skip analysis of arguments of implicitly defined map clause for target
4775 // directives.
4776 if (C && !((isa<OMPFirstprivateClause>(Val: C) || isa<OMPMapClause>(Val: C)) &&
4777 C->isImplicit() && !isOpenMPTaskingDirective(Kind: DKind))) {
4778 for (Stmt *CC : C->children()) {
4779 if (CC)
4780 Visit(S: CC);
4781 }
4782 }
4783 }
4784 // Check implicitly captured variables.
4785 VisitSubCaptures(S);
4786 }
4787
4788 void VisitOMPCanonicalLoopNestTransformationDirective(
4789 OMPCanonicalLoopNestTransformationDirective *S) {
4790 // Loop transformation directives do not introduce data sharing
4791 VisitStmt(S);
4792 }
4793
4794 // A metadirective has no associated statement; visit its variants and
4795 // conditions.
4796 void VisitOMPMetaDirective(OMPMetaDirective *S) { VisitStmt(S); }
4797
4798 void VisitCallExpr(CallExpr *S) {
4799 for (Stmt *C : S->arguments()) {
4800 if (C) {
4801 // Check implicitly captured variables in the task-based directives to
4802 // check if they must be firstprivatized.
4803 Visit(S: C);
4804 }
4805 }
4806 if (Expr *Callee = S->getCallee()) {
4807 auto *CI = Callee->IgnoreParenImpCasts();
4808 if (auto *CE = dyn_cast<MemberExpr>(Val: CI))
4809 Visit(S: CE->getBase());
4810 else if (auto *CE = dyn_cast<DeclRefExpr>(Val: CI))
4811 Visit(S: CE);
4812 }
4813 }
4814 void VisitStmt(Stmt *S) {
4815 for (Stmt *C : S->children()) {
4816 if (C) {
4817 // Check implicitly captured variables in the task-based directives to
4818 // check if they must be firstprivatized.
4819 Visit(S: C);
4820 }
4821 }
4822 }
4823
4824 void visitSubCaptures(CapturedStmt *S) {
4825 for (const CapturedStmt::Capture &Cap : S->captures()) {
4826 if (!Cap.capturesVariable() && !Cap.capturesVariableByCopy())
4827 continue;
4828 VarDecl *VD = Cap.getCapturedVar();
4829 // Do not try to map the variable if it or its sub-component was mapped
4830 // already.
4831 if (isOpenMPTargetExecutionDirective(DKind) &&
4832 Stack->checkMappableExprComponentListsForDecl(
4833 VD, /*CurrentRegionOnly=*/true,
4834 Check: [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
4835 OpenMPClauseKind) { return true; }))
4836 continue;
4837 DeclRefExpr *DRE = buildDeclRefExpr(
4838 S&: SemaRef, D: VD, Ty: VD->getType().getNonLValueExprType(Context: SemaRef.Context),
4839 Loc: Cap.getLocation(), /*RefersToCapture=*/true);
4840 Visit(S: DRE);
4841 }
4842 }
4843 bool isErrorFound() const { return ErrorFound; }
4844 const VariableImplicitInfo &getImplicitInfo() const { return ImpInfo; }
4845 const SemaOpenMP::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
4846 return VarsWithInheritedDSA;
4847 }
4848
4849 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
4850 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {
4851 DKind = S->getCurrentDirective();
4852 // Process declare target link variables for the target directives.
4853 if (isOpenMPTargetExecutionDirective(DKind)) {
4854 for (DeclRefExpr *E : Stack->getLinkGlobals())
4855 Visit(S: E);
4856 }
4857 }
4858};
4859} // namespace
4860
4861static void handleDeclareVariantConstructTrait(DSAStackTy *Stack,
4862 OpenMPDirectiveKind DKind,
4863 bool ScopeEntry) {
4864 SmallVector<llvm::omp::TraitProperty, 8> Traits;
4865 if (isOpenMPTargetExecutionDirective(DKind))
4866 Traits.emplace_back(Args: llvm::omp::TraitProperty::construct_target_target);
4867 if (DKind == OMPD_dispatch)
4868 Traits.emplace_back(Args: llvm::omp::TraitProperty::construct_dispatch_dispatch);
4869 if (isOpenMPTeamsDirective(DKind))
4870 Traits.emplace_back(Args: llvm::omp::TraitProperty::construct_teams_teams);
4871 if (isOpenMPParallelDirective(DKind))
4872 Traits.emplace_back(Args: llvm::omp::TraitProperty::construct_parallel_parallel);
4873 if (isOpenMPWorksharingDirective(DKind))
4874 Traits.emplace_back(Args: llvm::omp::TraitProperty::construct_for_for);
4875 if (isOpenMPSimdDirective(DKind))
4876 Traits.emplace_back(Args: llvm::omp::TraitProperty::construct_simd_simd);
4877 Stack->handleConstructTrait(Traits, ScopeEntry);
4878}
4879
4880static SmallVector<SemaOpenMP::CapturedParamNameType>
4881getParallelRegionParams(Sema &SemaRef, bool LoopBoundSharing) {
4882 ASTContext &Context = SemaRef.getASTContext();
4883 QualType KmpInt32Ty =
4884 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1).withConst();
4885 QualType KmpInt32PtrTy =
4886 Context.getPointerType(T: KmpInt32Ty).withConst().withRestrict();
4887 SmallVector<SemaOpenMP::CapturedParamNameType> Params{
4888 std::make_pair(x: ".global_tid.", y&: KmpInt32PtrTy),
4889 std::make_pair(x: ".bound_tid.", y&: KmpInt32PtrTy),
4890 };
4891 if (LoopBoundSharing) {
4892 QualType KmpSizeTy = Context.getSizeType().withConst();
4893 Params.push_back(Elt: std::make_pair(x: ".previous.lb.", y&: KmpSizeTy));
4894 Params.push_back(Elt: std::make_pair(x: ".previous.ub.", y&: KmpSizeTy));
4895 }
4896
4897 // __context with shared vars
4898 Params.push_back(Elt: std::make_pair(x: StringRef(), y: QualType()));
4899 return Params;
4900}
4901
4902static SmallVector<SemaOpenMP::CapturedParamNameType>
4903getTeamsRegionParams(Sema &SemaRef) {
4904 return getParallelRegionParams(SemaRef, /*LoopBoundSharing=*/false);
4905}
4906
4907static SmallVector<SemaOpenMP::CapturedParamNameType>
4908getTaskRegionParams(Sema &SemaRef) {
4909 ASTContext &Context = SemaRef.getASTContext();
4910 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(DestWidth: 32, Signed: 1).withConst();
4911 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4912 QualType KmpInt32PtrTy =
4913 Context.getPointerType(T: KmpInt32Ty).withConst().withRestrict();
4914 QualType Args[] = {VoidPtrTy};
4915 FunctionProtoType::ExtProtoInfo EPI;
4916 EPI.Variadic = true;
4917 QualType CopyFnType = Context.getFunctionType(ResultTy: Context.VoidTy, Args, EPI);
4918 SmallVector<SemaOpenMP::CapturedParamNameType> Params{
4919 std::make_pair(x: ".global_tid.", y&: KmpInt32Ty),
4920 std::make_pair(x: ".part_id.", y&: KmpInt32PtrTy),
4921 std::make_pair(x: ".privates.", y&: VoidPtrTy),
4922 std::make_pair(
4923 x: ".copy_fn.",
4924 y: Context.getPointerType(T: CopyFnType).withConst().withRestrict()),
4925 std::make_pair(x: ".task_t.", y: Context.VoidPtrTy.withConst()),
4926 std::make_pair(x: StringRef(), y: QualType()) // __context with shared vars
4927 };
4928 return Params;
4929}
4930
4931static SmallVector<SemaOpenMP::CapturedParamNameType>
4932getTargetRegionParams(Sema &SemaRef) {
4933 ASTContext &Context = SemaRef.getASTContext();
4934 SmallVector<SemaOpenMP::CapturedParamNameType> Params;
4935 // __context with shared vars
4936 Params.push_back(Elt: std::make_pair(x: StringRef(), y: QualType()));
4937 // Implicit dyn_ptr argument, appended as the last parameter. Present on both
4938 // host and device so argument counts match without runtime manipulation.
4939 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4940 Params.push_back(Elt: std::make_pair(x: StringRef("dyn_ptr"), y&: VoidPtrTy));
4941 return Params;
4942}
4943
4944static SmallVector<SemaOpenMP::CapturedParamNameType>
4945getUnknownRegionParams(Sema &SemaRef) {
4946 SmallVector<SemaOpenMP::CapturedParamNameType> Params{
4947 std::make_pair(x: StringRef(), y: QualType()) // __context with shared vars
4948 };
4949 return Params;
4950}
4951
4952static SmallVector<SemaOpenMP::CapturedParamNameType>
4953getTaskloopRegionParams(Sema &SemaRef) {
4954 ASTContext &Context = SemaRef.getASTContext();
4955 QualType KmpInt32Ty =
4956 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1).withConst();
4957 QualType KmpUInt64Ty =
4958 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0).withConst();
4959 QualType KmpInt64Ty =
4960 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1).withConst();
4961 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4962 QualType KmpInt32PtrTy =
4963 Context.getPointerType(T: KmpInt32Ty).withConst().withRestrict();
4964 QualType Args[] = {VoidPtrTy};
4965 FunctionProtoType::ExtProtoInfo EPI;
4966 EPI.Variadic = true;
4967 QualType CopyFnType = Context.getFunctionType(ResultTy: Context.VoidTy, Args, EPI);
4968 SmallVector<SemaOpenMP::CapturedParamNameType> Params{
4969 std::make_pair(x: ".global_tid.", y&: KmpInt32Ty),
4970 std::make_pair(x: ".part_id.", y&: KmpInt32PtrTy),
4971 std::make_pair(x: ".privates.", y&: VoidPtrTy),
4972 std::make_pair(
4973 x: ".copy_fn.",
4974 y: Context.getPointerType(T: CopyFnType).withConst().withRestrict()),
4975 std::make_pair(x: ".task_t.", y: Context.VoidPtrTy.withConst()),
4976 std::make_pair(x: ".lb.", y&: KmpUInt64Ty),
4977 std::make_pair(x: ".ub.", y&: KmpUInt64Ty),
4978 std::make_pair(x: ".st.", y&: KmpInt64Ty),
4979 std::make_pair(x: ".liter.", y&: KmpInt32Ty),
4980 std::make_pair(x: ".reductions.", y&: VoidPtrTy),
4981 std::make_pair(x: StringRef(), y: QualType()) // __context with shared vars
4982 };
4983 return Params;
4984}
4985
4986static void processCapturedRegions(Sema &SemaRef, OpenMPDirectiveKind DKind,
4987 Scope *CurScope, SourceLocation Loc) {
4988 SmallVector<OpenMPDirectiveKind> Regions;
4989 getOpenMPCaptureRegions(CaptureRegions&: Regions, DKind);
4990
4991 bool LoopBoundSharing = isOpenMPLoopBoundSharingDirective(Kind: DKind);
4992
4993 auto MarkAsInlined = [&](CapturedRegionScopeInfo *CSI) {
4994 CSI->TheCapturedDecl->addAttr(A: AlwaysInlineAttr::CreateImplicit(
4995 Ctx&: SemaRef.getASTContext(), Range: {}, S: AlwaysInlineAttr::Keyword_forceinline));
4996 };
4997
4998 for (auto [Level, RKind] : llvm::enumerate(First&: Regions)) {
4999 switch (RKind) {
5000 // All region kinds that can be returned from `getOpenMPCaptureRegions`
5001 // are listed here.
5002 case OMPD_parallel:
5003 SemaRef.ActOnCapturedRegionStart(
5004 Loc, CurScope, Kind: CR_OpenMP,
5005 Params: getParallelRegionParams(SemaRef, LoopBoundSharing), OpenMPCaptureLevel: Level);
5006 break;
5007 case OMPD_teams:
5008 SemaRef.ActOnCapturedRegionStart(Loc, CurScope, Kind: CR_OpenMP,
5009 Params: getTeamsRegionParams(SemaRef), OpenMPCaptureLevel: Level);
5010 break;
5011 case OMPD_task:
5012 SemaRef.ActOnCapturedRegionStart(Loc, CurScope, Kind: CR_OpenMP,
5013 Params: getTaskRegionParams(SemaRef), OpenMPCaptureLevel: Level);
5014 // Mark this captured region as inlined, because we don't use outlined
5015 // function directly.
5016 MarkAsInlined(SemaRef.getCurCapturedRegion());
5017 break;
5018 case OMPD_taskloop:
5019 SemaRef.ActOnCapturedRegionStart(Loc, CurScope, Kind: CR_OpenMP,
5020 Params: getTaskloopRegionParams(SemaRef), OpenMPCaptureLevel: Level);
5021 // Mark this captured region as inlined, because we don't use outlined
5022 // function directly.
5023 MarkAsInlined(SemaRef.getCurCapturedRegion());
5024 break;
5025 case OMPD_target:
5026 SemaRef.ActOnCapturedRegionStart(Loc, CurScope, Kind: CR_OpenMP,
5027 Params: getTargetRegionParams(SemaRef), OpenMPCaptureLevel: Level);
5028 break;
5029 case OMPD_unknown:
5030 SemaRef.ActOnCapturedRegionStart(Loc, CurScope, Kind: CR_OpenMP,
5031 Params: getUnknownRegionParams(SemaRef));
5032 break;
5033 case OMPD_metadirective:
5034 case OMPD_nothing:
5035 default:
5036 llvm_unreachable("Unexpected capture region");
5037 }
5038 }
5039}
5040
5041void SemaOpenMP::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind,
5042 Scope *CurScope) {
5043 if (DKind == OMPD_ordered_blockassoc &&
5044 DSAStack->getCurrentDirective() == OMPD_ordered_standalone) {
5045 DSAStack->setOrderedToBlockAssociated();
5046 }
5047 switch (DKind) {
5048 case OMPD_atomic:
5049 case OMPD_critical:
5050 case OMPD_masked:
5051 case OMPD_master:
5052 case OMPD_section:
5053 case OMPD_tile:
5054 case OMPD_stripe:
5055 case OMPD_unroll:
5056 case OMPD_reverse:
5057 case OMPD_split:
5058 case OMPD_interchange:
5059 case OMPD_flatten:
5060 case OMPD_fuse:
5061 case OMPD_assume:
5062 break;
5063 default:
5064 processCapturedRegions(SemaRef, DKind, CurScope,
5065 DSAStack->getConstructLoc());
5066 break;
5067 }
5068
5069 DSAStack->setContext(SemaRef.CurContext);
5070 handleDeclareVariantConstructTrait(DSAStack, DKind, /*ScopeEntry=*/true);
5071}
5072
5073int SemaOpenMP::getNumberOfConstructScopes(unsigned Level) const {
5074 return getOpenMPCaptureLevels(DSAStack->getDirective(Level));
5075}
5076
5077int SemaOpenMP::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
5078 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
5079 getOpenMPCaptureRegions(CaptureRegions, DKind);
5080 return CaptureRegions.size();
5081}
5082
5083static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
5084 Expr *CaptureExpr, bool WithInit,
5085 DeclContext *CurContext,
5086 bool AsExpression) {
5087 assert(CaptureExpr);
5088 ASTContext &C = S.getASTContext();
5089 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
5090 QualType Ty = Init->getType();
5091 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
5092 if (S.getLangOpts().CPlusPlus) {
5093 Ty = C.getLValueReferenceType(T: Ty);
5094 } else {
5095 Ty = C.getPointerType(T: Ty);
5096 ExprResult Res =
5097 S.CreateBuiltinUnaryOp(OpLoc: CaptureExpr->getExprLoc(), Opc: UO_AddrOf, InputExpr: Init);
5098 if (!Res.isUsable())
5099 return nullptr;
5100 Init = Res.get();
5101 }
5102 WithInit = true;
5103 }
5104 auto *CED = OMPCapturedExprDecl::Create(C, DC: CurContext, Id, T: Ty,
5105 StartLoc: CaptureExpr->getBeginLoc());
5106 if (!WithInit)
5107 CED->addAttr(A: OMPCaptureNoInitAttr::CreateImplicit(Ctx&: C));
5108 CurContext->addHiddenDecl(D: CED);
5109 Sema::TentativeAnalysisScope Trap(S);
5110 S.AddInitializerToDecl(dcl: CED, init: Init, /*DirectInit=*/false);
5111 return CED;
5112}
5113
5114static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
5115 bool WithInit) {
5116 OMPCapturedExprDecl *CD;
5117 // For BindingDecls, always create a new capture instead of reusing the
5118 // decomposed decl, since the decomposed decl is a regular VarDecl, not an
5119 // OMPCapturedExprDecl.
5120 if (!isa<BindingDecl>(Val: D)) {
5121 if (VarDecl *VD = S.OpenMP().isOpenMPCapturedDecl(D))
5122 CD = cast<OMPCapturedExprDecl>(Val: VD);
5123 else
5124 CD = buildCaptureDecl(S, Id: D->getIdentifier(), CaptureExpr, WithInit,
5125 CurContext: S.CurContext,
5126 /*AsExpression=*/false);
5127 } else {
5128 CD = buildCaptureDecl(S, Id: D->getIdentifier(), CaptureExpr, WithInit,
5129 CurContext: S.CurContext,
5130 /*AsExpression=*/false);
5131 }
5132 return buildDeclRefExpr(S, D: CD, Ty: CD->getType().getNonReferenceType(),
5133 Loc: CaptureExpr->getExprLoc());
5134}
5135
5136static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref,
5137 StringRef Name) {
5138 CaptureExpr = S.DefaultLvalueConversion(E: CaptureExpr).get();
5139 if (!Ref) {
5140 OMPCapturedExprDecl *CD = buildCaptureDecl(
5141 S, Id: &S.getASTContext().Idents.get(Name), CaptureExpr,
5142 /*WithInit=*/true, CurContext: S.CurContext, /*AsExpression=*/true);
5143 Ref = buildDeclRefExpr(S, D: CD, Ty: CD->getType().getNonReferenceType(),
5144 Loc: CaptureExpr->getExprLoc());
5145 }
5146 ExprResult Res = Ref;
5147 if (!S.getLangOpts().CPlusPlus &&
5148 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
5149 Ref->getType()->isPointerType()) {
5150 Res = S.CreateBuiltinUnaryOp(OpLoc: CaptureExpr->getExprLoc(), Opc: UO_Deref, InputExpr: Ref);
5151 if (!Res.isUsable())
5152 return ExprError();
5153 }
5154 return S.DefaultLvalueConversion(E: Res.get());
5155}
5156
5157namespace {
5158// OpenMP directives parsed in this section are represented as a
5159// CapturedStatement with an associated statement. If a syntax error
5160// is detected during the parsing of the associated statement, the
5161// compiler must abort processing and close the CapturedStatement.
5162//
5163// Combined directives such as 'target parallel' have more than one
5164// nested CapturedStatements. This RAII ensures that we unwind out
5165// of all the nested CapturedStatements when an error is found.
5166class CaptureRegionUnwinderRAII {
5167private:
5168 Sema &S;
5169 bool &ErrorFound;
5170 OpenMPDirectiveKind DKind = OMPD_unknown;
5171
5172public:
5173 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
5174 OpenMPDirectiveKind DKind)
5175 : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
5176 ~CaptureRegionUnwinderRAII() {
5177 if (ErrorFound) {
5178 int ThisCaptureLevel = S.OpenMP().getOpenMPCaptureLevels(DKind);
5179 while (--ThisCaptureLevel >= 0)
5180 S.ActOnCapturedRegionError();
5181 }
5182 }
5183};
5184} // namespace
5185
5186void SemaOpenMP::tryCaptureOpenMPLambdas(ValueDecl *V) {
5187 // Capture variables captured by reference in lambdas for target-based
5188 // directives.
5189 if (!SemaRef.CurContext->isDependentContext() &&
5190 (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) ||
5191 isOpenMPTargetDataManagementDirective(
5192 DSAStack->getCurrentDirective()))) {
5193 QualType Type = V->getType();
5194 if (const auto *RD = Type.getCanonicalType()
5195 .getNonReferenceType()
5196 ->getAsCXXRecordDecl()) {
5197 bool SavedForceCaptureByReferenceInTargetExecutable =
5198 DSAStack->isForceCaptureByReferenceInTargetExecutable();
5199 DSAStack->setForceCaptureByReferenceInTargetExecutable(
5200 /*V=*/true);
5201 if (RD->isLambda()) {
5202 llvm::DenseMap<const ValueDecl *, FieldDecl *> Captures;
5203 FieldDecl *ThisCapture;
5204 RD->getCaptureFields(Captures, ThisCapture);
5205 for (const LambdaCapture &LC : RD->captures()) {
5206 if (LC.getCaptureKind() == LCK_ByRef) {
5207 VarDecl *VD = cast<VarDecl>(Val: LC.getCapturedVar());
5208 DeclContext *VDC = VD->getDeclContext();
5209 if (!VDC->Encloses(DC: SemaRef.CurContext))
5210 continue;
5211 SemaRef.MarkVariableReferenced(Loc: LC.getLocation(), Var: VD);
5212 } else if (LC.getCaptureKind() == LCK_This) {
5213 QualType ThisTy = SemaRef.getCurrentThisType();
5214 if (!ThisTy.isNull() && getASTContext().typesAreCompatible(
5215 T1: ThisTy, T2: ThisCapture->getType()))
5216 SemaRef.CheckCXXThisCapture(Loc: LC.getLocation());
5217 }
5218 }
5219 }
5220 DSAStack->setForceCaptureByReferenceInTargetExecutable(
5221 SavedForceCaptureByReferenceInTargetExecutable);
5222 }
5223 }
5224}
5225
5226static bool checkOrderedOrderSpecified(Sema &S,
5227 const ArrayRef<OMPClause *> Clauses) {
5228 const OMPOrderedClause *Ordered = nullptr;
5229 const OMPOrderClause *Order = nullptr;
5230
5231 for (const OMPClause *Clause : Clauses) {
5232 if (Clause->getClauseKind() == OMPC_ordered)
5233 Ordered = cast<OMPOrderedClause>(Val: Clause);
5234 else if (Clause->getClauseKind() == OMPC_order) {
5235 Order = cast<OMPOrderClause>(Val: Clause);
5236 if (Order->getKind() != OMPC_ORDER_concurrent)
5237 Order = nullptr;
5238 }
5239 if (Ordered && Order)
5240 break;
5241 }
5242
5243 if (Ordered && Order) {
5244 S.Diag(Loc: Order->getKindKwLoc(),
5245 DiagID: diag::err_omp_simple_clause_incompatible_with_ordered)
5246 << getOpenMPClauseNameForDiag(C: OMPC_order)
5247 << getOpenMPSimpleClauseTypeName(Kind: OMPC_order, Type: OMPC_ORDER_concurrent)
5248 << SourceRange(Order->getBeginLoc(), Order->getEndLoc());
5249 S.Diag(Loc: Ordered->getBeginLoc(), DiagID: diag::note_omp_ordered_param)
5250 << 0 << SourceRange(Ordered->getBeginLoc(), Ordered->getEndLoc());
5251 return true;
5252 }
5253 return false;
5254}
5255
5256StmtResult SemaOpenMP::ActOnOpenMPRegionEnd(StmtResult S,
5257 ArrayRef<OMPClause *> Clauses) {
5258 handleDeclareVariantConstructTrait(DSAStack, DSAStack->getCurrentDirective(),
5259 /*ScopeEntry=*/false);
5260 if (!isOpenMPCapturingDirective(DSAStack->getCurrentDirective()))
5261 return S;
5262
5263 bool ErrorFound = false;
5264 CaptureRegionUnwinderRAII CaptureRegionUnwinder(
5265 SemaRef, ErrorFound, DSAStack->getCurrentDirective());
5266 if (!S.isUsable()) {
5267 ErrorFound = true;
5268 return StmtError();
5269 }
5270
5271 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
5272 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
5273 OMPOrderedClause *OC = nullptr;
5274 OMPScheduleClause *SC = nullptr;
5275 SmallVector<const OMPLinearClause *, 4> LCs;
5276 SmallVector<const OMPClauseWithPreInit *, 4> PICs;
5277 // This is required for proper codegen.
5278 for (OMPClause *Clause : Clauses) {
5279 if (!getLangOpts().OpenMPSimd &&
5280 (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) ||
5281 DSAStack->getCurrentDirective() == OMPD_target) &&
5282 Clause->getClauseKind() == OMPC_in_reduction) {
5283 // Capture taskgroup task_reduction descriptors inside the tasking regions
5284 // with the corresponding in_reduction items.
5285 auto *IRC = cast<OMPInReductionClause>(Val: Clause);
5286 for (Expr *E : IRC->taskgroup_descriptors())
5287 if (E)
5288 SemaRef.MarkDeclarationsReferencedInExpr(E);
5289 }
5290 if (isOpenMPPrivate(Kind: Clause->getClauseKind()) ||
5291 Clause->getClauseKind() == OMPC_copyprivate ||
5292 (getLangOpts().OpenMPUseTLS &&
5293 getASTContext().getTargetInfo().isTLSSupported() &&
5294 Clause->getClauseKind() == OMPC_copyin)) {
5295 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
5296 // Mark all variables in private list clauses as used in inner region.
5297 for (Stmt *VarRef : Clause->children()) {
5298 if (auto *E = cast_or_null<Expr>(Val: VarRef)) {
5299 SemaRef.MarkDeclarationsReferencedInExpr(E);
5300 }
5301 }
5302 DSAStack->setForceVarCapturing(/*V=*/false);
5303 } else if (CaptureRegions.size() > 1 ||
5304 CaptureRegions.back() != OMPD_unknown) {
5305 if (auto *C = OMPClauseWithPreInit::get(C: Clause))
5306 PICs.push_back(Elt: C);
5307 if (auto *C = OMPClauseWithPostUpdate::get(C: Clause)) {
5308 if (Expr *E = C->getPostUpdateExpr())
5309 SemaRef.MarkDeclarationsReferencedInExpr(E);
5310 }
5311 }
5312 if (Clause->getClauseKind() == OMPC_schedule)
5313 SC = cast<OMPScheduleClause>(Val: Clause);
5314 else if (Clause->getClauseKind() == OMPC_ordered)
5315 OC = cast<OMPOrderedClause>(Val: Clause);
5316 else if (Clause->getClauseKind() == OMPC_linear)
5317 LCs.push_back(Elt: cast<OMPLinearClause>(Val: Clause));
5318 }
5319 // Capture allocator expressions if used.
5320 for (Expr *E : DSAStack->getInnerAllocators())
5321 SemaRef.MarkDeclarationsReferencedInExpr(E);
5322 // OpenMP, 2.7.1 Loop Construct, Restrictions
5323 // The nonmonotonic modifier cannot be specified if an ordered clause is
5324 // specified.
5325 if (SC &&
5326 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
5327 SC->getSecondScheduleModifier() ==
5328 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
5329 OC) {
5330 Diag(Loc: SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
5331 ? SC->getFirstScheduleModifierLoc()
5332 : SC->getSecondScheduleModifierLoc(),
5333 DiagID: diag::err_omp_simple_clause_incompatible_with_ordered)
5334 << getOpenMPClauseNameForDiag(C: OMPC_schedule)
5335 << getOpenMPSimpleClauseTypeName(Kind: OMPC_schedule,
5336 Type: OMPC_SCHEDULE_MODIFIER_nonmonotonic)
5337 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
5338 ErrorFound = true;
5339 }
5340 // OpenMP 5.0, 2.9.2 Worksharing-Loop Construct, Restrictions.
5341 // If an order(concurrent) clause is present, an ordered clause may not appear
5342 // on the same directive.
5343 if (checkOrderedOrderSpecified(S&: SemaRef, Clauses))
5344 ErrorFound = true;
5345 if (!LCs.empty() && OC && OC->getNumForLoops()) {
5346 for (const OMPLinearClause *C : LCs) {
5347 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_linear_ordered)
5348 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
5349 }
5350 ErrorFound = true;
5351 }
5352 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
5353 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
5354 OC->getNumForLoops()) {
5355 llvm::omp::Version OMPVersion = getLangOpts().getOpenMPVersion();
5356 Diag(Loc: OC->getBeginLoc(), DiagID: diag::err_omp_ordered_simd)
5357 << getOpenMPDirectiveName(DSAStack->getCurrentDirective(), V: OMPVersion);
5358 ErrorFound = true;
5359 }
5360 if (ErrorFound) {
5361 return StmtError();
5362 }
5363 StmtResult SR = S;
5364 unsigned CompletedRegions = 0;
5365 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(C&: CaptureRegions)) {
5366 // Mark all variables in private list clauses as used in inner region.
5367 // Required for proper codegen of combined directives.
5368 // TODO: add processing for other clauses.
5369 if (ThisCaptureRegion != OMPD_unknown) {
5370 for (const clang::OMPClauseWithPreInit *C : PICs) {
5371 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
5372 // Find the particular capture region for the clause if the
5373 // directive is a combined one with multiple capture regions.
5374 // If the directive is not a combined one, the capture region
5375 // associated with the clause is OMPD_unknown and is generated
5376 // only once.
5377 if (CaptureRegion == ThisCaptureRegion ||
5378 CaptureRegion == OMPD_unknown) {
5379 if (auto *DS = cast_or_null<DeclStmt>(Val: C->getPreInitStmt())) {
5380 for (Decl *D : DS->decls())
5381 SemaRef.MarkVariableReferenced(Loc: D->getLocation(),
5382 Var: cast<VarDecl>(Val: D));
5383 }
5384 }
5385 }
5386 }
5387 if (ThisCaptureRegion == OMPD_target) {
5388 // Capture allocator traits in the target region. They are used implicitly
5389 // and, thus, are not captured by default.
5390 for (OMPClause *C : Clauses) {
5391 if (const auto *UAC = dyn_cast<OMPUsesAllocatorsClause>(Val: C)) {
5392 for (unsigned I = 0, End = UAC->getNumberOfAllocators(); I < End;
5393 ++I) {
5394 OMPUsesAllocatorsClause::Data D = UAC->getAllocatorData(I);
5395 if (Expr *E = D.AllocatorTraits)
5396 SemaRef.MarkDeclarationsReferencedInExpr(E);
5397 }
5398 continue;
5399 }
5400 }
5401 }
5402 if (ThisCaptureRegion == OMPD_parallel) {
5403 // Capture temp arrays for inscan reductions and locals in aligned
5404 // clauses.
5405 for (OMPClause *C : Clauses) {
5406 if (auto *RC = dyn_cast<OMPReductionClause>(Val: C)) {
5407 if (RC->getModifier() != OMPC_REDUCTION_inscan)
5408 continue;
5409 for (Expr *E : RC->copy_array_temps())
5410 if (E)
5411 SemaRef.MarkDeclarationsReferencedInExpr(E);
5412 }
5413 if (auto *AC = dyn_cast<OMPAlignedClause>(Val: C)) {
5414 for (Expr *E : AC->varlist())
5415 SemaRef.MarkDeclarationsReferencedInExpr(E);
5416 }
5417 }
5418 }
5419 if (++CompletedRegions == CaptureRegions.size())
5420 DSAStack->setBodyComplete();
5421 SR = SemaRef.ActOnCapturedRegionEnd(S: SR.get());
5422 }
5423 return SR;
5424}
5425
5426static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
5427 OpenMPDirectiveKind CancelRegion,
5428 SourceLocation StartLoc) {
5429 // CancelRegion is only needed for cancel and cancellation_point.
5430 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
5431 return false;
5432
5433 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
5434 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
5435 return false;
5436
5437 llvm::omp::Version OMPVersion = SemaRef.getLangOpts().getOpenMPVersion();
5438 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_wrong_cancel_region)
5439 << getOpenMPDirectiveName(D: CancelRegion, V: OMPVersion);
5440 return true;
5441}
5442
5443static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
5444 OpenMPDirectiveKind CurrentRegion,
5445 const DeclarationNameInfo &CurrentName,
5446 OpenMPDirectiveKind CancelRegion,
5447 OpenMPBindClauseKind BindKind,
5448 SourceLocation StartLoc) {
5449 if (!Stack->getCurScope())
5450 return false;
5451
5452 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
5453 OpenMPDirectiveKind OffendingRegion = ParentRegion;
5454 bool NestingProhibited = false;
5455 bool CloseNesting = true;
5456 bool OrphanSeen = false;
5457 enum {
5458 NoRecommend,
5459 ShouldBeInParallelRegion,
5460 ShouldBeInOrderedRegion,
5461 ShouldBeInTargetRegion,
5462 ShouldBeInTeamsRegion,
5463 ShouldBeInLoopSimdRegion,
5464 } Recommend = NoRecommend;
5465
5466 SmallVector<OpenMPDirectiveKind, 4> LeafOrComposite;
5467 ArrayRef<OpenMPDirectiveKind> ParentLOC =
5468 getLeafOrCompositeConstructs(D: ParentRegion, Output&: LeafOrComposite);
5469 OpenMPDirectiveKind EnclosingConstruct = ParentLOC.back();
5470 llvm::omp::Version OMPVersion = SemaRef.getLangOpts().getOpenMPVersion();
5471
5472 if (OMPVersion >= 50 && Stack->isEnclosingOrderConcurrent() &&
5473 !isOpenMPOrderConcurrentNestableDirective(DKind: CurrentRegion,
5474 LangOpts: SemaRef.LangOpts)) {
5475 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_prohibited_region_order)
5476 << getOpenMPDirectiveName(D: CurrentRegion, V: OMPVersion);
5477 return true;
5478 }
5479 // A construct nested in a loop-transforming or ordered construct is still
5480 // encountered during execution of an enclosing simd region.
5481 if (Stack->isInSimdRegion() &&
5482 ((OMPVersion <= 45 && CurrentRegion != OMPD_ordered_blockassoc) ||
5483 (OMPVersion >= 50 && !isOpenMPSimdizableDirective(
5484 DKind: CurrentRegion, BindKind, LangOpts: SemaRef.LangOpts)))) {
5485 // OpenMP [2.16, Nesting of Regions]
5486 // OpenMP constructs may not be nested inside a simd region.
5487 // OpenMP [2.8.1,simd Construct, Restrictions]
5488 // An ordered construct with the simd clause is the only OpenMP
5489 // construct that can appear in the simd region.
5490 // Allowing a SIMD construct nested in another SIMD construct is an
5491 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
5492 // message.
5493 // OpenMP 5.0 [2.9.3.1, simd Construct, Restrictions]
5494 // The only OpenMP constructs that can be encountered during execution of
5495 // a simd region are the atomic construct, the loop construct, the simd
5496 // construct and the ordered construct with the simd clause.
5497 // OpenMP 5.2 [10.4, simd Construct, Restrictions]
5498 // Only simdizable constructs can be encountered during execution of a
5499 // simd region.
5500 // OpenMP 6.0 makes all loop-transforming constructs simdizable.
5501 SemaRef.Diag(Loc: StartLoc, DiagID: (CurrentRegion != OMPD_simd)
5502 ? diag::err_omp_prohibited_region_simd
5503 : diag::warn_omp_nesting_simd)
5504 << (OMPVersion >= 60 ? 2
5505 : OMPVersion >= 50 ? 1
5506 : 0);
5507 return CurrentRegion != OMPD_simd;
5508 }
5509 if (EnclosingConstruct == OMPD_atomic) {
5510 // OpenMP [2.16, Nesting of Regions]
5511 // OpenMP constructs may not be nested inside an atomic region.
5512 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_prohibited_region_atomic);
5513 return true;
5514 }
5515 if (CurrentRegion == OMPD_section) {
5516 // OpenMP [2.7.2, sections Construct, Restrictions]
5517 // Orphaned section directives are prohibited. That is, the section
5518 // directives must appear within the sections construct and must not be
5519 // encountered elsewhere in the sections region.
5520 if (EnclosingConstruct != OMPD_sections) {
5521 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_orphaned_section_directive)
5522 << (ParentRegion != OMPD_unknown)
5523 << getOpenMPDirectiveName(D: ParentRegion, V: OMPVersion);
5524 return true;
5525 }
5526 return false;
5527 }
5528 // Allow some constructs (except teams and cancellation constructs) to be
5529 // orphaned (they could be used in functions, called from OpenMP regions
5530 // with the required preconditions).
5531 if (ParentRegion == OMPD_unknown &&
5532 !isOpenMPNestingTeamsDirective(DKind: CurrentRegion) &&
5533 CurrentRegion != OMPD_cancellation_point &&
5534 CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_scan)
5535 return false;
5536 // Checks needed for mapping "loop" construct. Please check mapLoopConstruct
5537 // for a detailed explanation
5538 if (OMPVersion >= 50 && CurrentRegion == OMPD_loop &&
5539 (BindKind == OMPC_BIND_parallel || BindKind == OMPC_BIND_teams) &&
5540 (isOpenMPWorksharingDirective(DKind: ParentRegion) ||
5541 EnclosingConstruct == OMPD_loop)) {
5542 int ErrorMsgNumber = (BindKind == OMPC_BIND_parallel) ? 1 : 4;
5543 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_prohibited_region)
5544 << true << getOpenMPDirectiveName(D: ParentRegion, V: OMPVersion)
5545 << ErrorMsgNumber << getOpenMPDirectiveName(D: CurrentRegion, V: OMPVersion);
5546 return true;
5547 }
5548 if (CurrentRegion == OMPD_cancellation_point ||
5549 CurrentRegion == OMPD_cancel) {
5550 // OpenMP [2.16, Nesting of Regions]
5551 // A cancellation point construct for which construct-type-clause is
5552 // taskgroup must be nested inside a task construct. A cancellation
5553 // point construct for which construct-type-clause is not taskgroup must
5554 // be closely nested inside an OpenMP construct that matches the type
5555 // specified in construct-type-clause.
5556 // A cancel construct for which construct-type-clause is taskgroup must be
5557 // nested inside a task construct. A cancel construct for which
5558 // construct-type-clause is not taskgroup must be closely nested inside an
5559 // OpenMP construct that matches the type specified in
5560 // construct-type-clause.
5561 ArrayRef<OpenMPDirectiveKind> Leafs = getLeafConstructsOrSelf(D: ParentRegion);
5562 if (CancelRegion == OMPD_taskgroup) {
5563 NestingProhibited =
5564 EnclosingConstruct != OMPD_task &&
5565 (OMPVersion < 50 || EnclosingConstruct != OMPD_taskloop);
5566 } else if (CancelRegion == OMPD_sections) {
5567 NestingProhibited = EnclosingConstruct != OMPD_section &&
5568 EnclosingConstruct != OMPD_sections;
5569 } else {
5570 NestingProhibited = CancelRegion != Leafs.back();
5571 }
5572 OrphanSeen = ParentRegion == OMPD_unknown;
5573 } else if (CurrentRegion == OMPD_master || CurrentRegion == OMPD_masked) {
5574 // OpenMP 5.1 [2.22, Nesting of Regions]
5575 // A masked region may not be closely nested inside a worksharing, loop,
5576 // atomic, task, or taskloop region.
5577 NestingProhibited = isOpenMPWorksharingDirective(DKind: ParentRegion) ||
5578 isOpenMPGenericLoopDirective(DKind: ParentRegion) ||
5579 isOpenMPTaskingDirective(Kind: ParentRegion);
5580 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
5581 // OpenMP [2.16, Nesting of Regions]
5582 // A critical region may not be nested (closely or otherwise) inside a
5583 // critical region with the same name. Note that this restriction is not
5584 // sufficient to prevent deadlock.
5585 SourceLocation PreviousCriticalLoc;
5586 bool DeadLock = Stack->hasDirective(
5587 DPred: [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
5588 const DeclarationNameInfo &DNI,
5589 SourceLocation Loc) {
5590 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
5591 PreviousCriticalLoc = Loc;
5592 return true;
5593 }
5594 return false;
5595 },
5596 FromParent: false /* skip top directive */);
5597 if (DeadLock) {
5598 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_prohibited_region_critical_same_name)
5599 << CurrentName.getName();
5600 if (PreviousCriticalLoc.isValid())
5601 SemaRef.Diag(Loc: PreviousCriticalLoc,
5602 DiagID: diag::note_omp_previous_critical_region);
5603 return true;
5604 }
5605 } else if (CurrentRegion == OMPD_barrier || CurrentRegion == OMPD_scope) {
5606 // OpenMP 5.1 [2.22, Nesting of Regions]
5607 // A scope region may not be closely nested inside a worksharing, loop,
5608 // task, taskloop, critical, ordered, atomic, or masked region.
5609 // OpenMP 5.1 [2.22, Nesting of Regions]
5610 // A barrier region may not be closely nested inside a worksharing, loop,
5611 // task, taskloop, critical, ordered, atomic, or masked region.
5612 NestingProhibited =
5613 isOpenMPWorksharingDirective(DKind: ParentRegion) ||
5614 isOpenMPGenericLoopDirective(DKind: ParentRegion) ||
5615 isOpenMPTaskingDirective(Kind: ParentRegion) ||
5616 llvm::is_contained(
5617 Set: {OMPD_masked, OMPD_master, OMPD_critical, OMPD_ordered_blockassoc},
5618 Element: EnclosingConstruct);
5619 } else if (isOpenMPWorksharingDirective(DKind: CurrentRegion) &&
5620 !isOpenMPParallelDirective(DKind: CurrentRegion) &&
5621 !isOpenMPTeamsDirective(DKind: CurrentRegion)) {
5622 // OpenMP 5.1 [2.22, Nesting of Regions]
5623 // A loop region that binds to a parallel region or a worksharing region
5624 // may not be closely nested inside a worksharing, loop, task, taskloop,
5625 // critical, ordered, atomic, or masked region.
5626 NestingProhibited =
5627 isOpenMPWorksharingDirective(DKind: ParentRegion) ||
5628 isOpenMPGenericLoopDirective(DKind: ParentRegion) ||
5629 isOpenMPTaskingDirective(Kind: ParentRegion) ||
5630 llvm::is_contained(
5631 Set: {OMPD_masked, OMPD_master, OMPD_critical, OMPD_ordered_blockassoc},
5632 Element: EnclosingConstruct);
5633 Recommend = ShouldBeInParallelRegion;
5634 } else if (CurrentRegion == OMPD_ordered_blockassoc ||
5635 CurrentRegion == OMPD_ordered_standalone) {
5636 // OpenMP [2.16, Nesting of Regions]
5637 // An ordered region may not be closely nested inside a critical,
5638 // atomic, or explicit task region.
5639 // An ordered region must be closely nested inside a loop region (or
5640 // parallel loop region) with an ordered clause.
5641 // OpenMP [2.8.1,simd Construct, Restrictions]
5642 // An ordered construct with the simd clause is the only OpenMP construct
5643 // that can appear in the simd region.
5644 NestingProhibited = EnclosingConstruct == OMPD_critical ||
5645 isOpenMPTaskingDirective(Kind: ParentRegion) ||
5646 !(isOpenMPSimdDirective(DKind: ParentRegion) ||
5647 Stack->isParentOrderedRegion());
5648 Recommend = ShouldBeInOrderedRegion;
5649 } else if (isOpenMPNestingTeamsDirective(DKind: CurrentRegion)) {
5650 // OpenMP [2.16, Nesting of Regions]
5651 // If specified, a teams construct must be contained within a target
5652 // construct.
5653 NestingProhibited =
5654 (OMPVersion <= 45 && EnclosingConstruct != OMPD_target) ||
5655 (OMPVersion >= 50 && EnclosingConstruct != OMPD_unknown &&
5656 EnclosingConstruct != OMPD_target);
5657 OrphanSeen = ParentRegion == OMPD_unknown;
5658 Recommend = ShouldBeInTargetRegion;
5659 } else if (CurrentRegion == OMPD_scan) {
5660 if (OMPVersion >= 50) {
5661 // OpenMP spec 5.0 and 5.1 require scan to be directly enclosed by for,
5662 // simd, or for simd. This has to take into account combined directives.
5663 // In 5.2 this seems to be implied by the fact that the specified
5664 // separated constructs are do, for, and simd.
5665 NestingProhibited = !llvm::is_contained(
5666 Set: {OMPD_for, OMPD_simd, OMPD_for_simd}, Element: EnclosingConstruct);
5667 } else {
5668 NestingProhibited = true;
5669 }
5670 OrphanSeen = ParentRegion == OMPD_unknown;
5671 Recommend = ShouldBeInLoopSimdRegion;
5672 }
5673 if (!NestingProhibited && !isOpenMPTargetExecutionDirective(DKind: CurrentRegion) &&
5674 !isOpenMPTargetDataManagementDirective(DKind: CurrentRegion) &&
5675 EnclosingConstruct == OMPD_teams) {
5676 // OpenMP [5.1, 2.22, Nesting of Regions]
5677 // distribute, distribute simd, distribute parallel worksharing-loop,
5678 // distribute parallel worksharing-loop SIMD, loop, parallel regions,
5679 // including any parallel regions arising from combined constructs,
5680 // omp_get_num_teams() regions, and omp_get_team_num() regions are the
5681 // only OpenMP regions that may be strictly nested inside the teams
5682 // region.
5683 //
5684 // As an extension, we permit atomic within teams as well.
5685 NestingProhibited = !isOpenMPParallelDirective(DKind: CurrentRegion) &&
5686 !isOpenMPDistributeDirective(DKind: CurrentRegion) &&
5687 CurrentRegion != OMPD_loop &&
5688 !(SemaRef.getLangOpts().OpenMPExtensions &&
5689 CurrentRegion == OMPD_atomic);
5690 Recommend = ShouldBeInParallelRegion;
5691 }
5692 if (!NestingProhibited && CurrentRegion == OMPD_loop) {
5693 // OpenMP [5.1, 2.11.7, loop Construct, Restrictions]
5694 // If the bind clause is present on the loop construct and binding is
5695 // teams then the corresponding loop region must be strictly nested inside
5696 // a teams region.
5697 NestingProhibited =
5698 BindKind == OMPC_BIND_teams && EnclosingConstruct != OMPD_teams;
5699 Recommend = ShouldBeInTeamsRegion;
5700 }
5701 if (!NestingProhibited && isOpenMPNestingDistributeDirective(DKind: CurrentRegion)) {
5702 // OpenMP 4.5 [2.17 Nesting of Regions]
5703 // The region associated with the distribute construct must be strictly
5704 // nested inside a teams region
5705 NestingProhibited = EnclosingConstruct != OMPD_teams;
5706 Recommend = ShouldBeInTeamsRegion;
5707 }
5708 if (!NestingProhibited &&
5709 (isOpenMPTargetExecutionDirective(DKind: CurrentRegion) ||
5710 isOpenMPTargetDataManagementDirective(DKind: CurrentRegion))) {
5711 // OpenMP 4.5 [2.17 Nesting of Regions]
5712 // If a target, target update, target data, target enter data, or
5713 // target exit data construct is encountered during execution of a
5714 // target region, the behavior is unspecified.
5715 NestingProhibited = Stack->hasDirective(
5716 DPred: [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
5717 SourceLocation) {
5718 if (isOpenMPTargetExecutionDirective(DKind: K)) {
5719 OffendingRegion = K;
5720 return true;
5721 }
5722 return false;
5723 },
5724 FromParent: false /* don't skip top directive */);
5725 CloseNesting = false;
5726 }
5727 if (NestingProhibited) {
5728 if (OrphanSeen) {
5729 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_orphaned_device_directive)
5730 << getOpenMPDirectiveName(D: CurrentRegion, V: OMPVersion) << Recommend;
5731 } else {
5732 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_prohibited_region)
5733 << CloseNesting << getOpenMPDirectiveName(D: OffendingRegion, V: OMPVersion)
5734 << Recommend << getOpenMPDirectiveName(D: CurrentRegion, V: OMPVersion);
5735 }
5736 return true;
5737 }
5738 return false;
5739}
5740
5741struct Kind2Unsigned {
5742 using argument_type = OpenMPDirectiveKind;
5743 unsigned operator()(argument_type DK) { return unsigned(DK); }
5744};
5745static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
5746 ArrayRef<OMPClause *> Clauses,
5747 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
5748 bool ErrorFound = false;
5749 unsigned NamedModifiersNumber = 0;
5750 llvm::IndexedMap<const OMPIfClause *, Kind2Unsigned> FoundNameModifiers;
5751 FoundNameModifiers.resize(S: llvm::omp::Directive_enumSize + 1);
5752 SmallVector<SourceLocation, 4> NameModifierLoc;
5753 llvm::omp::Version OMPVersion = S.getLangOpts().getOpenMPVersion();
5754 for (const OMPClause *C : Clauses) {
5755 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(Val: C)) {
5756 // At most one if clause without a directive-name-modifier can appear on
5757 // the directive.
5758 OpenMPDirectiveKind CurNM = IC->getNameModifier();
5759 auto &FNM = FoundNameModifiers[CurNM];
5760 if (FNM) {
5761 S.Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_more_one_clause)
5762 << getOpenMPDirectiveName(D: Kind, V: OMPVersion)
5763 << getOpenMPClauseNameForDiag(C: OMPC_if) << (CurNM != OMPD_unknown)
5764 << getOpenMPDirectiveName(D: CurNM, V: OMPVersion);
5765 ErrorFound = true;
5766 } else if (CurNM != OMPD_unknown) {
5767 NameModifierLoc.push_back(Elt: IC->getNameModifierLoc());
5768 ++NamedModifiersNumber;
5769 }
5770 FNM = IC;
5771 if (CurNM == OMPD_unknown)
5772 continue;
5773 // Check if the specified name modifier is allowed for the current
5774 // directive.
5775 // At most one if clause with the particular directive-name-modifier can
5776 // appear on the directive.
5777 if (!llvm::is_contained(Range&: AllowedNameModifiers, Element: CurNM)) {
5778 S.Diag(Loc: IC->getNameModifierLoc(),
5779 DiagID: diag::err_omp_wrong_if_directive_name_modifier)
5780 << getOpenMPDirectiveName(D: CurNM, V: OMPVersion)
5781 << getOpenMPDirectiveName(D: Kind, V: OMPVersion);
5782 ErrorFound = true;
5783 }
5784 }
5785 }
5786 // If any if clause on the directive includes a directive-name-modifier then
5787 // all if clauses on the directive must include a directive-name-modifier.
5788 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
5789 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
5790 S.Diag(Loc: FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
5791 DiagID: diag::err_omp_no_more_if_clause);
5792 } else {
5793 std::string Values;
5794 std::string Sep(", ");
5795 unsigned AllowedCnt = 0;
5796 unsigned TotalAllowedNum =
5797 AllowedNameModifiers.size() - NamedModifiersNumber;
5798 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
5799 ++Cnt) {
5800 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
5801 if (!FoundNameModifiers[NM]) {
5802 Values += "'";
5803 Values += getOpenMPDirectiveName(D: NM, V: OMPVersion);
5804 Values += "'";
5805 if (AllowedCnt + 2 == TotalAllowedNum)
5806 Values += " or ";
5807 else if (AllowedCnt + 1 != TotalAllowedNum)
5808 Values += Sep;
5809 ++AllowedCnt;
5810 }
5811 }
5812 S.Diag(Loc: FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
5813 DiagID: diag::err_omp_unnamed_if_clause)
5814 << (TotalAllowedNum > 1) << Values;
5815 }
5816 for (SourceLocation Loc : NameModifierLoc) {
5817 S.Diag(Loc, DiagID: diag::note_omp_previous_named_if_clause);
5818 }
5819 ErrorFound = true;
5820 }
5821 return ErrorFound;
5822}
5823
5824static std::pair<ValueDecl *, bool>
5825getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
5826 SourceRange &ERange, bool AllowArraySection,
5827 bool AllowAssumedSizeArray, StringRef DiagType) {
5828 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
5829 RefExpr->containsUnexpandedParameterPack())
5830 return std::make_pair(x: nullptr, y: true);
5831
5832 // OpenMP [3.1, C/C++]
5833 // A list item is a variable name.
5834 // OpenMP [2.9.3.3, Restrictions, p.1]
5835 // A variable that is part of another variable (as an array or
5836 // structure element) cannot appear in a private clause.
5837 //
5838 // OpenMP [6.0]
5839 // 5.2.5 Array Sections, p. 166, L28-29
5840 // When the length is absent and the size of the dimension is not known,
5841 // the array section is an assumed-size array.
5842 // 2 Glossary, p. 23, L4-6
5843 // assumed-size array
5844 // For C/C++, an array section for which the length is absent and the
5845 // size of the dimensions is not known.
5846 // 5.2.5 Array Sections, p. 168, L11
5847 // An assumed-size array can appear only in clauses for which it is
5848 // explicitly allowed.
5849 // 7.4 List Item Privatization, Restrictions, p. 222, L15
5850 // Assumed-size arrays must not be privatized.
5851 RefExpr = RefExpr->IgnoreParens();
5852 enum {
5853 NoArrayExpr = -1,
5854 ArraySubscript = 0,
5855 OMPArraySection = 1
5856 } IsArrayExpr = NoArrayExpr;
5857 if (AllowArraySection) {
5858 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(Val: RefExpr)) {
5859 Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
5860 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Val: Base))
5861 Base = TempASE->getBase()->IgnoreParenImpCasts();
5862 RefExpr = Base;
5863 IsArrayExpr = ArraySubscript;
5864 } else if (auto *OASE = dyn_cast_or_null<ArraySectionExpr>(Val: RefExpr)) {
5865 Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
5866 if (S.getLangOpts().OpenMP >= 60 && !AllowAssumedSizeArray &&
5867 OASE->getColonLocFirst().isValid() && !OASE->getLength()) {
5868 QualType BaseType = ArraySectionExpr::getBaseOriginalType(Base);
5869 if (BaseType.isNull() || (!BaseType->isConstantArrayType() &&
5870 !BaseType->isVariableArrayType())) {
5871 S.Diag(Loc: OASE->getColonLocFirst(),
5872 DiagID: diag::err_omp_section_length_undefined)
5873 << (!BaseType.isNull() && BaseType->isArrayType());
5874 return std::make_pair(x: nullptr, y: false);
5875 }
5876 }
5877 while (auto *TempOASE = dyn_cast<ArraySectionExpr>(Val: Base))
5878 Base = TempOASE->getBase()->IgnoreParenImpCasts();
5879 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Val: Base))
5880 Base = TempASE->getBase()->IgnoreParenImpCasts();
5881 RefExpr = Base;
5882 IsArrayExpr = OMPArraySection;
5883 }
5884 }
5885 ELoc = RefExpr->getExprLoc();
5886 ERange = RefExpr->getSourceRange();
5887 RefExpr = RefExpr->IgnoreParenImpCasts();
5888 auto *DE = dyn_cast_or_null<DeclRefExpr>(Val: RefExpr);
5889 auto *ME = dyn_cast_or_null<MemberExpr>(Val: RefExpr);
5890 if ((!DE || !isa<VarDecl, BindingDecl>(Val: DE->getDecl())) &&
5891 (S.getCurrentThisType().isNull() || !ME ||
5892 !isa<CXXThisExpr>(Val: ME->getBase()->IgnoreParenImpCasts()) ||
5893 !isa<FieldDecl>(Val: ME->getMemberDecl()))) {
5894 if (IsArrayExpr != NoArrayExpr) {
5895 S.Diag(Loc: ELoc, DiagID: diag::err_omp_expected_base_var_name)
5896 << IsArrayExpr << ERange;
5897 } else if (!DiagType.empty()) {
5898 unsigned DiagSelect = S.getLangOpts().CPlusPlus
5899 ? (S.getCurrentThisType().isNull() ? 1 : 2)
5900 : 0;
5901 S.Diag(Loc: ELoc, DiagID: diag::err_omp_expected_var_name_member_expr_with_type)
5902 << DiagSelect << DiagType << ERange;
5903 } else {
5904 S.Diag(Loc: ELoc,
5905 DiagID: AllowArraySection
5906 ? diag::err_omp_expected_var_name_member_expr_or_array_item
5907 : diag::err_omp_expected_var_name_member_expr)
5908 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
5909 }
5910 return std::make_pair(x: nullptr, y: false);
5911 }
5912 return std::make_pair(
5913 x: getCanonicalDecl(D: DE ? DE->getDecl() : ME->getMemberDecl()), y: false);
5914}
5915
5916namespace {
5917/// Checks if the allocator is used in uses_allocators clause to be allowed in
5918/// target regions.
5919class AllocatorChecker final : public ConstStmtVisitor<AllocatorChecker, bool> {
5920 DSAStackTy *S = nullptr;
5921
5922public:
5923 bool VisitDeclRefExpr(const DeclRefExpr *E) {
5924 return S->isUsesAllocatorsDecl(D: E->getDecl())
5925 .value_or(u: DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) ==
5926 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait;
5927 }
5928 bool VisitStmt(const Stmt *S) {
5929 for (const Stmt *Child : S->children()) {
5930 if (Child && Visit(S: Child))
5931 return true;
5932 }
5933 return false;
5934 }
5935 explicit AllocatorChecker(DSAStackTy *S) : S(S) {}
5936};
5937} // namespace
5938
5939static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
5940 ArrayRef<OMPClause *> Clauses) {
5941 assert(!S.CurContext->isDependentContext() &&
5942 "Expected non-dependent context.");
5943 auto AllocateRange =
5944 llvm::make_filter_range(Range&: Clauses, Pred: OMPAllocateClause::classof);
5945 llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>> DeclToCopy;
5946 auto PrivateRange = llvm::make_filter_range(Range&: Clauses, Pred: [](const OMPClause *C) {
5947 return isOpenMPPrivate(Kind: C->getClauseKind());
5948 });
5949 for (OMPClause *Cl : PrivateRange) {
5950 MutableArrayRef<Expr *>::iterator I, It, Et;
5951 if (Cl->getClauseKind() == OMPC_private) {
5952 auto *PC = cast<OMPPrivateClause>(Val: Cl);
5953 I = PC->private_copies().begin();
5954 It = PC->varlist_begin();
5955 Et = PC->varlist_end();
5956 } else if (Cl->getClauseKind() == OMPC_firstprivate) {
5957 auto *PC = cast<OMPFirstprivateClause>(Val: Cl);
5958 I = PC->private_copies().begin();
5959 It = PC->varlist_begin();
5960 Et = PC->varlist_end();
5961 } else if (Cl->getClauseKind() == OMPC_lastprivate) {
5962 auto *PC = cast<OMPLastprivateClause>(Val: Cl);
5963 I = PC->private_copies().begin();
5964 It = PC->varlist_begin();
5965 Et = PC->varlist_end();
5966 } else if (Cl->getClauseKind() == OMPC_linear) {
5967 auto *PC = cast<OMPLinearClause>(Val: Cl);
5968 I = PC->privates().begin();
5969 It = PC->varlist_begin();
5970 Et = PC->varlist_end();
5971 } else if (Cl->getClauseKind() == OMPC_reduction) {
5972 auto *PC = cast<OMPReductionClause>(Val: Cl);
5973 I = PC->privates().begin();
5974 It = PC->varlist_begin();
5975 Et = PC->varlist_end();
5976 } else if (Cl->getClauseKind() == OMPC_task_reduction) {
5977 auto *PC = cast<OMPTaskReductionClause>(Val: Cl);
5978 I = PC->privates().begin();
5979 It = PC->varlist_begin();
5980 Et = PC->varlist_end();
5981 } else if (Cl->getClauseKind() == OMPC_in_reduction) {
5982 auto *PC = cast<OMPInReductionClause>(Val: Cl);
5983 I = PC->privates().begin();
5984 It = PC->varlist_begin();
5985 Et = PC->varlist_end();
5986 } else {
5987 llvm_unreachable("Expected private clause.");
5988 }
5989 for (Expr *E : llvm::make_range(x: It, y: Et)) {
5990 if (!*I) {
5991 ++I;
5992 continue;
5993 }
5994 SourceLocation ELoc;
5995 SourceRange ERange;
5996 Expr *SimpleRefExpr = E;
5997 auto Res = getPrivateItem(S, RefExpr&: SimpleRefExpr, ELoc, ERange,
5998 /*AllowArraySection=*/true);
5999 DeclToCopy.try_emplace(Key: Res.first,
6000 Args: cast<VarDecl>(Val: cast<DeclRefExpr>(Val: *I)->getDecl()));
6001 ++I;
6002 }
6003 }
6004 for (OMPClause *C : AllocateRange) {
6005 auto *AC = cast<OMPAllocateClause>(Val: C);
6006 if (S.getLangOpts().OpenMP >= 50 &&
6007 !Stack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>() &&
6008 isOpenMPTargetExecutionDirective(DKind: Stack->getCurrentDirective()) &&
6009 AC->getAllocator()) {
6010 Expr *Allocator = AC->getAllocator();
6011 // OpenMP, 2.12.5 target Construct
6012 // Memory allocators that do not appear in a uses_allocators clause cannot
6013 // appear as an allocator in an allocate clause or be used in the target
6014 // region unless a requires directive with the dynamic_allocators clause
6015 // is present in the same compilation unit.
6016 AllocatorChecker Checker(Stack);
6017 if (Checker.Visit(S: Allocator))
6018 S.Diag(Loc: Allocator->getExprLoc(),
6019 DiagID: diag::err_omp_allocator_not_in_uses_allocators)
6020 << Allocator->getSourceRange();
6021 }
6022 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
6023 getAllocatorKind(S, Stack, Allocator: AC->getAllocator());
6024 // OpenMP, 2.11.4 allocate Clause, Restrictions.
6025 // For task, taskloop or target directives, allocation requests to memory
6026 // allocators with the trait access set to thread result in unspecified
6027 // behavior.
6028 if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc &&
6029 (isOpenMPTaskingDirective(Kind: Stack->getCurrentDirective()) ||
6030 isOpenMPTargetExecutionDirective(DKind: Stack->getCurrentDirective()))) {
6031 llvm::omp::Version OMPVersion = S.getLangOpts().getOpenMPVersion();
6032 S.Diag(Loc: AC->getAllocator()->getExprLoc(),
6033 DiagID: diag::warn_omp_allocate_thread_on_task_target_directive)
6034 << getOpenMPDirectiveName(D: Stack->getCurrentDirective(), V: OMPVersion);
6035 }
6036 for (Expr *E : AC->varlist()) {
6037 SourceLocation ELoc;
6038 SourceRange ERange;
6039 Expr *SimpleRefExpr = E;
6040 auto Res = getPrivateItem(S, RefExpr&: SimpleRefExpr, ELoc, ERange);
6041 ValueDecl *VD = Res.first;
6042 if (!VD)
6043 continue;
6044 DSAStackTy::DSAVarData Data = Stack->getTopDSA(D: VD, /*FromParent=*/false);
6045 if (!isOpenMPPrivate(Kind: Data.CKind)) {
6046 S.Diag(Loc: E->getExprLoc(),
6047 DiagID: diag::err_omp_expected_private_copy_for_allocate);
6048 continue;
6049 }
6050 VarDecl *PrivateVD = DeclToCopy[VD];
6051 if (checkPreviousOMPAllocateAttribute(S, Stack, RefExpr: E, VD: PrivateVD,
6052 AllocatorKind, Allocator: AC->getAllocator()))
6053 continue;
6054 applyOMPAllocateAttribute(S, VD: PrivateVD, AllocatorKind, Allocator: AC->getAllocator(),
6055 Alignment: AC->getAlignment(), SR: E->getSourceRange());
6056 }
6057 }
6058}
6059
6060namespace {
6061/// Rewrite statements and expressions for Sema \p Actions CurContext.
6062///
6063/// Used to wrap already parsed statements/expressions into a new CapturedStmt
6064/// context. DeclRefExpr used inside the new context are changed to refer to the
6065/// captured variable instead.
6066class CaptureVars : public TreeTransform<CaptureVars> {
6067 using BaseTransform = TreeTransform<CaptureVars>;
6068
6069public:
6070 CaptureVars(Sema &Actions) : BaseTransform(Actions) {}
6071
6072 bool AlwaysRebuild() { return true; }
6073};
6074} // namespace
6075
6076/// Like ASTContext::getIntTypeForBitwidth, but falls back to a _BitInt type
6077/// when no standard integer type has the requested width.
6078static QualType getIntTypeForBitwidthOrBitInt(ASTContext &C, unsigned Bits,
6079 bool Signed) {
6080 QualType Ty = C.getIntTypeForBitwidth(DestWidth: Bits, Signed);
6081 if (Ty.isNull())
6082 Ty = C.getBitIntType(/*IsUnsigned=*/Unsigned: !Signed, NumBits: Bits);
6083 return Ty;
6084}
6085
6086static Expr *buildInitListValue(Sema &SemaRef, Expr *E) {
6087 auto *ILE = dyn_cast<InitListExpr>(Val: E->IgnoreImplicit());
6088 if (!ILE)
6089 return E;
6090 if (ILE->isTransparent())
6091 return buildInitListValue(SemaRef, E: ILE->getInit(Init: 0));
6092 QualType Ty = ILE->getType();
6093 bool IsEmptyScalar = ILE->getNumInits() == 0 && Ty->isScalarType();
6094 SourceLocation LBrace = ILE->getLBraceLoc();
6095 if (!SemaRef.getLangOpts().CPlusPlus) {
6096 if (!IsEmptyScalar)
6097 return E;
6098 ExprResult Zero = SemaRef.PerformImplicitConversion(
6099 From: SemaRef.ActOnIntegerConstant(Loc: LBrace, Val: 0).get(), ToType: Ty,
6100 Action: AssignmentAction::Converting);
6101 return Zero.isUsable() ? Zero.get() : E;
6102 }
6103 Expr *Syntactic = ILE->getSyntacticForm();
6104 if ((!IsEmptyScalar && !Ty->isRecordType()) || !Syntactic)
6105 return E;
6106 Sema::TentativeAnalysisScope Trap(SemaRef);
6107 ExprResult Value = SemaRef.BuildCXXTypeConstructExpr(
6108 Type: SemaRef.Context.getTrivialTypeSourceInfo(T: Ty, Loc: LBrace), LParenLoc: LBrace, Exprs: Syntactic,
6109 RParenLoc: ILE->getRBraceLoc(), /*ListInitialization=*/true);
6110 return Value.isUsable() ? Value.get() : E;
6111}
6112
6113static VarDecl *precomputeExpr(Sema &Actions,
6114 SmallVectorImpl<Stmt *> &BodyStmts, Expr *E,
6115 StringRef Name) {
6116 Expr *NewE = AssertSuccess(R: CaptureVars(Actions).TransformExpr(E));
6117 VarDecl *NewVar = buildVarDecl(SemaRef&: Actions, Loc: {}, Type: NewE->getType(), Name, Attrs: nullptr,
6118 OrigRef: dyn_cast<DeclRefExpr>(Val: E->IgnoreImplicit()));
6119 auto *NewDeclStmt = cast<DeclStmt>(Val: AssertSuccess(
6120 R: Actions.ActOnDeclStmt(Decl: Actions.ConvertDeclToDeclGroup(Ptr: NewVar), StartLoc: {}, EndLoc: {})));
6121 Actions.AddInitializerToDecl(dcl: NewDeclStmt->getSingleDecl(), init: NewE, DirectInit: false);
6122 BodyStmts.push_back(Elt: NewDeclStmt);
6123 return NewVar;
6124}
6125
6126/// Create a closure that computes the number of iterations of a loop.
6127///
6128/// \param Actions The Sema object.
6129/// \param LogicalTy Type for the logical iteration number.
6130/// \param Rel Comparison operator of the loop condition.
6131/// \param StartExpr Value of the loop counter at the first iteration.
6132/// \param StopExpr Expression the loop counter is compared against in the loop
6133/// condition. \param StepExpr Amount of increment after each iteration.
6134///
6135/// \return Closure (CapturedStmt) of the distance calculation.
6136static CapturedStmt *buildDistanceFunc(Sema &Actions, QualType LogicalTy,
6137 BinaryOperator::Opcode Rel,
6138 Expr *StartExpr, Expr *StopExpr,
6139 Expr *StepExpr) {
6140 ASTContext &Ctx = Actions.getASTContext();
6141 TypeSourceInfo *LogicalTSI = Ctx.getTrivialTypeSourceInfo(T: LogicalTy);
6142
6143 // Captured regions currently don't support return values, we use an
6144 // out-parameter instead. All inputs are implicit captures.
6145 // TODO: Instead of capturing each DeclRefExpr occurring in
6146 // StartExpr/StopExpr/Step, these could also be passed as a value capture.
6147 QualType ResultTy = Ctx.getLValueReferenceType(T: LogicalTy);
6148 Sema::CapturedParamNameType Params[] = {{"Distance", ResultTy},
6149 {StringRef(), QualType()}};
6150 Actions.ActOnCapturedRegionStart(Loc: {}, CurScope: nullptr, Kind: CR_Default, Params);
6151
6152 Stmt *Body;
6153 {
6154 Sema::CompoundScopeRAII CompoundScope(Actions);
6155 CapturedDecl *CS = cast<CapturedDecl>(Val: Actions.CurContext);
6156
6157 // Get the LValue expression for the result.
6158 ImplicitParamDecl *DistParam = CS->getParam(i: 0);
6159 DeclRefExpr *DistRef = Actions.BuildDeclRefExpr(
6160 D: DistParam, Ty: LogicalTy, VK: VK_LValue, NameInfo: {}, SS: nullptr, FoundD: nullptr, TemplateKWLoc: {}, TemplateArgs: nullptr);
6161
6162 SmallVector<Stmt *, 4> BodyStmts;
6163
6164 // Capture all referenced variable references.
6165 // TODO: Instead of computing NewStart/NewStop/NewStep inside the
6166 // CapturedStmt, we could compute them before and capture the result, to be
6167 // used jointly with the LoopVar function.
6168 VarDecl *NewStart = precomputeExpr(Actions, BodyStmts, E: StartExpr, Name: ".start");
6169 VarDecl *NewStop = precomputeExpr(Actions, BodyStmts, E: StopExpr, Name: ".stop");
6170 VarDecl *NewStep = precomputeExpr(Actions, BodyStmts, E: StepExpr, Name: ".step");
6171 auto BuildVarRef = [&](VarDecl *VD) {
6172 return buildDeclRefExpr(S&: Actions, D: VD, Ty: VD->getType(), Loc: {});
6173 };
6174
6175 IntegerLiteral *Zero = IntegerLiteral::Create(
6176 C: Ctx, V: llvm::APInt(Ctx.getIntWidth(T: LogicalTy), 0), type: LogicalTy, l: {});
6177 IntegerLiteral *One = IntegerLiteral::Create(
6178 C: Ctx, V: llvm::APInt(Ctx.getIntWidth(T: LogicalTy), 1), type: LogicalTy, l: {});
6179 Expr *Dist;
6180 if (Rel == BO_NE) {
6181 // When using a != comparison, the increment can be +1 or -1. This can be
6182 // dynamic at runtime, so we need to check for the direction.
6183 Expr *IsNegStep = AssertSuccess(
6184 R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_LT, LHSExpr: BuildVarRef(NewStep), RHSExpr: Zero));
6185
6186 // Positive increment.
6187 Expr *ForwardRange = AssertSuccess(R: Actions.BuildBinOp(
6188 S: nullptr, OpLoc: {}, Opc: BO_Sub, LHSExpr: BuildVarRef(NewStop), RHSExpr: BuildVarRef(NewStart)));
6189 ForwardRange = AssertSuccess(
6190 R: Actions.BuildCStyleCastExpr(LParenLoc: {}, Ty: LogicalTSI, RParenLoc: {}, Op: ForwardRange));
6191 Expr *ForwardDist = AssertSuccess(R: Actions.BuildBinOp(
6192 S: nullptr, OpLoc: {}, Opc: BO_Div, LHSExpr: ForwardRange, RHSExpr: BuildVarRef(NewStep)));
6193
6194 // Negative increment.
6195 Expr *BackwardRange = AssertSuccess(R: Actions.BuildBinOp(
6196 S: nullptr, OpLoc: {}, Opc: BO_Sub, LHSExpr: BuildVarRef(NewStart), RHSExpr: BuildVarRef(NewStop)));
6197 BackwardRange = AssertSuccess(
6198 R: Actions.BuildCStyleCastExpr(LParenLoc: {}, Ty: LogicalTSI, RParenLoc: {}, Op: BackwardRange));
6199 Expr *NegIncAmount = AssertSuccess(
6200 R: Actions.BuildUnaryOp(S: nullptr, OpLoc: {}, Opc: UO_Minus, Input: BuildVarRef(NewStep)));
6201 Expr *BackwardDist = AssertSuccess(
6202 R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Div, LHSExpr: BackwardRange, RHSExpr: NegIncAmount));
6203
6204 // Use the appropriate case.
6205 Dist = AssertSuccess(R: Actions.ActOnConditionalOp(
6206 QuestionLoc: {}, ColonLoc: {}, CondExpr: IsNegStep, LHSExpr: BackwardDist, RHSExpr: ForwardDist));
6207 } else {
6208 assert((Rel == BO_LT || Rel == BO_LE || Rel == BO_GE || Rel == BO_GT) &&
6209 "Expected one of these relational operators");
6210
6211 // We can derive the direction from any other comparison operator. It is
6212 // non well-formed OpenMP if Step increments/decrements in the other
6213 // directions. Whether at least the first iteration passes the loop
6214 // condition.
6215 Expr *HasAnyIteration = AssertSuccess(R: Actions.BuildBinOp(
6216 S: nullptr, OpLoc: {}, Opc: Rel, LHSExpr: BuildVarRef(NewStart), RHSExpr: BuildVarRef(NewStop)));
6217
6218 // Compute the range between first and last counter value.
6219 Expr *Range;
6220 if (Rel == BO_GE || Rel == BO_GT)
6221 Range = AssertSuccess(R: Actions.BuildBinOp(
6222 S: nullptr, OpLoc: {}, Opc: BO_Sub, LHSExpr: BuildVarRef(NewStart), RHSExpr: BuildVarRef(NewStop)));
6223 else
6224 Range = AssertSuccess(R: Actions.BuildBinOp(
6225 S: nullptr, OpLoc: {}, Opc: BO_Sub, LHSExpr: BuildVarRef(NewStop), RHSExpr: BuildVarRef(NewStart)));
6226
6227 // Ensure unsigned range space.
6228 Range =
6229 AssertSuccess(R: Actions.BuildCStyleCastExpr(LParenLoc: {}, Ty: LogicalTSI, RParenLoc: {}, Op: Range));
6230
6231 if (Rel == BO_LE || Rel == BO_GE) {
6232 // Add one to the range if the relational operator is inclusive.
6233 Range =
6234 AssertSuccess(R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Add, LHSExpr: Range, RHSExpr: One));
6235 }
6236
6237 // Divide by the absolute step amount. If the range is not a multiple of
6238 // the step size, rounding-up the effective upper bound ensures that the
6239 // last iteration is included.
6240 // Note that the rounding-up may cause an overflow in a temporary that
6241 // could be avoided, but would have occurred in a C-style for-loop as
6242 // well.
6243 Expr *Divisor = BuildVarRef(NewStep);
6244 if (Rel == BO_GE || Rel == BO_GT)
6245 Divisor =
6246 AssertSuccess(R: Actions.BuildUnaryOp(S: nullptr, OpLoc: {}, Opc: UO_Minus, Input: Divisor));
6247 Expr *DivisorMinusOne =
6248 AssertSuccess(R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Sub, LHSExpr: Divisor, RHSExpr: One));
6249 Expr *RangeRoundUp = AssertSuccess(
6250 R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Add, LHSExpr: Range, RHSExpr: DivisorMinusOne));
6251 Dist = AssertSuccess(
6252 R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Div, LHSExpr: RangeRoundUp, RHSExpr: Divisor));
6253
6254 // If there is not at least one iteration, the range contains garbage. Fix
6255 // to zero in this case.
6256 Dist = AssertSuccess(
6257 R: Actions.ActOnConditionalOp(QuestionLoc: {}, ColonLoc: {}, CondExpr: HasAnyIteration, LHSExpr: Dist, RHSExpr: Zero));
6258 }
6259
6260 // Assign the result to the out-parameter.
6261 Stmt *ResultAssign = AssertSuccess(R: Actions.BuildBinOp(
6262 S: Actions.getCurScope(), OpLoc: {}, Opc: BO_Assign, LHSExpr: DistRef, RHSExpr: Dist));
6263 BodyStmts.push_back(Elt: ResultAssign);
6264
6265 Body = AssertSuccess(R: Actions.ActOnCompoundStmt(L: {}, R: {}, Elts: BodyStmts, isStmtExpr: false));
6266 }
6267
6268 return cast<CapturedStmt>(
6269 Val: AssertSuccess(R: Actions.ActOnCapturedRegionEnd(S: Body)));
6270}
6271
6272/// Create a closure that computes the loop variable from the logical iteration
6273/// number.
6274///
6275/// \param Actions The Sema object.
6276/// \param LoopVarTy Type for the loop variable used for result value.
6277/// \param LogicalTy Type for the logical iteration number.
6278/// \param StartExpr Value of the loop counter at the first iteration.
6279/// \param Step Amount of increment after each iteration.
6280/// \param Deref Whether the loop variable is a dereference of the loop
6281/// counter variable.
6282///
6283/// \return Closure (CapturedStmt) of the loop value calculation.
6284static CapturedStmt *buildLoopVarFunc(Sema &Actions, QualType LoopVarTy,
6285 QualType LogicalTy,
6286 DeclRefExpr *StartExpr, Expr *Step,
6287 bool Deref) {
6288 ASTContext &Ctx = Actions.getASTContext();
6289
6290 // Pass the result as an out-parameter. Passing as return value would require
6291 // the OpenMPIRBuilder to know additional C/C++ semantics, such as how to
6292 // invoke a copy constructor.
6293 QualType TargetParamTy = Ctx.getLValueReferenceType(T: LoopVarTy);
6294 SemaOpenMP::CapturedParamNameType Params[] = {{"LoopVar", TargetParamTy},
6295 {"Logical", LogicalTy},
6296 {StringRef(), QualType()}};
6297 Actions.ActOnCapturedRegionStart(Loc: {}, CurScope: nullptr, Kind: CR_Default, Params);
6298
6299 // Capture the initial iterator which represents the LoopVar value at the
6300 // zero's logical iteration. Since the original ForStmt/CXXForRangeStmt update
6301 // it in every iteration, capture it by value before it is modified.
6302 VarDecl *StartVar = cast<VarDecl>(Val: StartExpr->getDecl());
6303 bool Invalid = Actions.tryCaptureVariable(Var: StartVar, Loc: {},
6304 Kind: TryCaptureKind::ExplicitByVal, EllipsisLoc: {});
6305 (void)Invalid;
6306 assert(!Invalid && "Expecting capture-by-value to work.");
6307
6308 Expr *Body;
6309 {
6310 Sema::CompoundScopeRAII CompoundScope(Actions);
6311 auto *CS = cast<CapturedDecl>(Val: Actions.CurContext);
6312
6313 ImplicitParamDecl *TargetParam = CS->getParam(i: 0);
6314 DeclRefExpr *TargetRef = Actions.BuildDeclRefExpr(
6315 D: TargetParam, Ty: LoopVarTy, VK: VK_LValue, NameInfo: {}, SS: nullptr, FoundD: nullptr, TemplateKWLoc: {}, TemplateArgs: nullptr);
6316 ImplicitParamDecl *IndvarParam = CS->getParam(i: 1);
6317 DeclRefExpr *LogicalRef = Actions.BuildDeclRefExpr(
6318 D: IndvarParam, Ty: LogicalTy, VK: VK_LValue, NameInfo: {}, SS: nullptr, FoundD: nullptr, TemplateKWLoc: {}, TemplateArgs: nullptr);
6319
6320 // Capture the Start expression.
6321 CaptureVars Recap(Actions);
6322 Expr *NewStart = AssertSuccess(R: Recap.TransformExpr(E: StartExpr));
6323 Expr *NewStep = AssertSuccess(R: Recap.TransformExpr(E: Step));
6324
6325 Expr *Skip = AssertSuccess(
6326 R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Mul, LHSExpr: NewStep, RHSExpr: LogicalRef));
6327 // TODO: Explicitly cast to the iterator's difference_type instead of
6328 // relying on implicit conversion.
6329 Expr *Advanced =
6330 AssertSuccess(R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Add, LHSExpr: NewStart, RHSExpr: Skip));
6331
6332 if (Deref) {
6333 // For range-based for-loops convert the loop counter value to a concrete
6334 // loop variable value by dereferencing the iterator.
6335 Advanced =
6336 AssertSuccess(R: Actions.BuildUnaryOp(S: nullptr, OpLoc: {}, Opc: UO_Deref, Input: Advanced));
6337 }
6338
6339 // Assign the result to the output parameter.
6340 Body = AssertSuccess(R: Actions.BuildBinOp(S: Actions.getCurScope(), OpLoc: {},
6341 Opc: BO_Assign, LHSExpr: TargetRef, RHSExpr: Advanced));
6342 }
6343 return cast<CapturedStmt>(
6344 Val: AssertSuccess(R: Actions.ActOnCapturedRegionEnd(S: Body)));
6345}
6346
6347StmtResult SemaOpenMP::ActOnOpenMPCanonicalLoop(Stmt *AStmt) {
6348 ASTContext &Ctx = getASTContext();
6349
6350 // Extract the common elements of ForStmt and CXXForRangeStmt:
6351 // Loop variable, repeat condition, increment
6352 Expr *Cond, *Inc;
6353 VarDecl *LIVDecl, *LUVDecl;
6354 if (auto *For = dyn_cast<ForStmt>(Val: AStmt)) {
6355 Stmt *Init = For->getInit();
6356 if (auto *LCVarDeclStmt = dyn_cast<DeclStmt>(Val: Init)) {
6357 // For statement declares loop variable.
6358 LIVDecl = cast<VarDecl>(Val: LCVarDeclStmt->getSingleDecl());
6359 } else if (auto *LCAssign = dyn_cast<BinaryOperator>(Val: Init)) {
6360 // For statement reuses variable.
6361 assert(LCAssign->getOpcode() == BO_Assign &&
6362 "init part must be a loop variable assignment");
6363 auto *CounterRef = cast<DeclRefExpr>(Val: LCAssign->getLHS());
6364 LIVDecl = cast<VarDecl>(Val: CounterRef->getDecl());
6365 } else
6366 llvm_unreachable("Cannot determine loop variable");
6367 LUVDecl = LIVDecl;
6368
6369 Cond = For->getCond();
6370 Inc = For->getInc();
6371 } else if (auto *RangeFor = dyn_cast<CXXForRangeStmt>(Val: AStmt)) {
6372 DeclStmt *BeginStmt = RangeFor->getBeginStmt();
6373 LIVDecl = cast<VarDecl>(Val: BeginStmt->getSingleDecl());
6374 LUVDecl = RangeFor->getLoopVariable();
6375
6376 Cond = RangeFor->getCond();
6377 Inc = RangeFor->getInc();
6378 } else
6379 llvm_unreachable("unhandled kind of loop");
6380
6381 QualType CounterTy = LIVDecl->getType();
6382 QualType LVTy = LUVDecl->getType();
6383
6384 // Analyze the loop condition.
6385 Expr *LHS, *RHS;
6386 BinaryOperator::Opcode CondRel;
6387 Cond = Cond->IgnoreImplicit();
6388 if (auto *CondBinExpr = dyn_cast<BinaryOperator>(Val: Cond)) {
6389 LHS = CondBinExpr->getLHS();
6390 RHS = CondBinExpr->getRHS();
6391 CondRel = CondBinExpr->getOpcode();
6392 } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Val: Cond)) {
6393 assert(CondCXXOp->getNumArgs() == 2 && "Comparison should have 2 operands");
6394 LHS = CondCXXOp->getArg(Arg: 0);
6395 RHS = CondCXXOp->getArg(Arg: 1);
6396 switch (CondCXXOp->getOperator()) {
6397 case OO_ExclaimEqual:
6398 CondRel = BO_NE;
6399 break;
6400 case OO_Less:
6401 CondRel = BO_LT;
6402 break;
6403 case OO_LessEqual:
6404 CondRel = BO_LE;
6405 break;
6406 case OO_Greater:
6407 CondRel = BO_GT;
6408 break;
6409 case OO_GreaterEqual:
6410 CondRel = BO_GE;
6411 break;
6412 default:
6413 llvm_unreachable("unexpected iterator operator");
6414 }
6415 } else
6416 llvm_unreachable("unexpected loop condition");
6417
6418 // Normalize such that the loop counter is on the LHS.
6419 if (!isa<DeclRefExpr>(Val: LHS->IgnoreImplicit()) ||
6420 cast<DeclRefExpr>(Val: LHS->IgnoreImplicit())->getDecl() != LIVDecl) {
6421 std::swap(a&: LHS, b&: RHS);
6422 CondRel = BinaryOperator::reverseComparisonOp(Opc: CondRel);
6423 }
6424 auto *CounterRef = cast<DeclRefExpr>(Val: LHS->IgnoreImplicit());
6425
6426 // Decide the bit width for the logical iteration counter. By default use the
6427 // unsigned ptrdiff_t integer size (for iterators and pointers).
6428 // TODO: For iterators, use iterator::difference_type,
6429 // std::iterator_traits<>::difference_type or decltype(it - end).
6430 QualType LogicalTy = Ctx.getUnsignedPointerDiffType();
6431 if (CounterTy->isIntegerType()) {
6432 unsigned BitWidth = Ctx.getIntWidth(T: CounterTy);
6433 LogicalTy = getIntTypeForBitwidthOrBitInt(C&: Ctx, Bits: BitWidth, /*Signed=*/false);
6434 }
6435
6436 // Analyze the loop increment.
6437 Expr *Step;
6438 if (auto *IncUn = dyn_cast<UnaryOperator>(Val: Inc)) {
6439 int Direction;
6440 switch (IncUn->getOpcode()) {
6441 case UO_PreInc:
6442 case UO_PostInc:
6443 Direction = 1;
6444 break;
6445 case UO_PreDec:
6446 case UO_PostDec:
6447 Direction = -1;
6448 break;
6449 default:
6450 llvm_unreachable("unhandled unary increment operator");
6451 }
6452 Step = IntegerLiteral::Create(
6453 C: Ctx,
6454 V: llvm::APInt(Ctx.getIntWidth(T: LogicalTy), Direction, /*isSigned=*/true),
6455 type: LogicalTy, l: {});
6456 } else if (auto *IncBin = dyn_cast<BinaryOperator>(Val: Inc)) {
6457 Expr *IncRHS = buildInitListValue(SemaRef, E: IncBin->getRHS());
6458 if (IncBin->getOpcode() == BO_AddAssign) {
6459 Step = IncRHS;
6460 } else if (IncBin->getOpcode() == BO_SubAssign) {
6461 Step = AssertSuccess(R: SemaRef.BuildUnaryOp(S: nullptr, OpLoc: {}, Opc: UO_Minus, Input: IncRHS));
6462 } else
6463 llvm_unreachable("unhandled binary increment operator");
6464 } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Val: Inc)) {
6465 switch (CondCXXOp->getOperator()) {
6466 case OO_PlusPlus:
6467 Step = IntegerLiteral::Create(
6468 C: Ctx, V: llvm::APInt(Ctx.getIntWidth(T: LogicalTy), 1), type: LogicalTy, l: {});
6469 break;
6470 case OO_MinusMinus:
6471 Step = IntegerLiteral::Create(
6472 C: Ctx, V: llvm::APInt(Ctx.getIntWidth(T: LogicalTy), -1), type: LogicalTy, l: {});
6473 break;
6474 case OO_PlusEqual:
6475 Step = buildInitListValue(SemaRef, E: CondCXXOp->getArg(Arg: 1));
6476 break;
6477 case OO_MinusEqual:
6478 Step = AssertSuccess(R: SemaRef.BuildUnaryOp(
6479 S: nullptr, OpLoc: {}, Opc: UO_Minus,
6480 Input: buildInitListValue(SemaRef, E: CondCXXOp->getArg(Arg: 1))));
6481 break;
6482 default:
6483 llvm_unreachable("unhandled overloaded increment operator");
6484 }
6485 } else
6486 llvm_unreachable("unknown increment expression");
6487
6488 CapturedStmt *DistanceFunc =
6489 buildDistanceFunc(Actions&: SemaRef, LogicalTy, Rel: CondRel, StartExpr: LHS, StopExpr: RHS, StepExpr: Step);
6490 CapturedStmt *LoopVarFunc = buildLoopVarFunc(
6491 Actions&: SemaRef, LoopVarTy: LVTy, LogicalTy, StartExpr: CounterRef, Step, Deref: isa<CXXForRangeStmt>(Val: AStmt));
6492 DeclRefExpr *LVRef =
6493 SemaRef.BuildDeclRefExpr(D: LUVDecl, Ty: LUVDecl->getType(), VK: VK_LValue, NameInfo: {},
6494 SS: nullptr, FoundD: nullptr, TemplateKWLoc: {}, TemplateArgs: nullptr);
6495 return OMPCanonicalLoop::create(Ctx: getASTContext(), LoopStmt: AStmt, DistanceFunc,
6496 LoopVarFunc, LoopVarRef: LVRef);
6497}
6498
6499StmtResult SemaOpenMP::ActOnOpenMPLoopnest(Stmt *AStmt) {
6500 // Handle a literal loop.
6501 if (isa<ForStmt>(Val: AStmt) || isa<CXXForRangeStmt>(Val: AStmt))
6502 return ActOnOpenMPCanonicalLoop(AStmt);
6503
6504 // If not a literal loop, it must be the result of a loop transformation.
6505 OMPExecutableDirective *LoopTransform = cast<OMPExecutableDirective>(Val: AStmt);
6506 assert(
6507 isOpenMPLoopTransformationDirective(LoopTransform->getDirectiveKind()) &&
6508 "Loop transformation directive expected");
6509 return LoopTransform;
6510}
6511
6512static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S,
6513 CXXScopeSpec &MapperIdScopeSpec,
6514 const DeclarationNameInfo &MapperId,
6515 QualType Type,
6516 Expr *UnresolvedMapper,
6517 SourceLocation ItemLoc);
6518
6519/// Perform DFS through the structure/class data members trying to find
6520/// member(s) with user-defined 'default' mapper and generate implicit map
6521/// clauses for such members with the found 'default' mapper.
6522static void
6523processImplicitMapsWithDefaultMappers(Sema &S, DSAStackTy *Stack,
6524 SmallVectorImpl<OMPClause *> &Clauses) {
6525 // Check for the default mapper for data members.
6526 if (S.getLangOpts().OpenMP < 50)
6527 return;
6528 for (int Cnt = 0, EndCnt = Clauses.size(); Cnt < EndCnt; ++Cnt) {
6529 auto *C = dyn_cast<OMPMapClause>(Val: Clauses[Cnt]);
6530 if (!C)
6531 continue;
6532 SmallVector<Expr *, 4> SubExprs;
6533 auto *MI = C->mapperlist_begin();
6534 for (auto I = C->varlist_begin(), End = C->varlist_end(); I != End;
6535 ++I, ++MI) {
6536 // Expression is mapped using mapper - skip it.
6537 if (*MI)
6538 continue;
6539 Expr *E = *I;
6540 // Expression is dependent - skip it, build the mapper when it gets
6541 // instantiated.
6542 if (E->isTypeDependent() || E->isValueDependent() ||
6543 E->containsUnexpandedParameterPack())
6544 continue;
6545 // Array section - need to check for the mapping of the array section
6546 // element.
6547 QualType CanonType = E->getType().getCanonicalType();
6548 if (CanonType->isSpecificBuiltinType(K: BuiltinType::ArraySection)) {
6549 const auto *OASE = cast<ArraySectionExpr>(Val: E->IgnoreParenImpCasts());
6550 QualType BaseType =
6551 ArraySectionExpr::getBaseOriginalType(Base: OASE->getBase());
6552 QualType ElemType;
6553 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
6554 ElemType = ATy->getElementType();
6555 else
6556 ElemType = BaseType->getPointeeType();
6557 CanonType = ElemType;
6558 }
6559
6560 // DFS over data members in structures/classes.
6561 SmallVector<std::pair<QualType, FieldDecl *>, 4> Types(
6562 1, {CanonType, nullptr});
6563 llvm::DenseMap<const Type *, Expr *> Visited;
6564 SmallVector<std::pair<FieldDecl *, unsigned>, 4> ParentChain(
6565 1, {nullptr, 1});
6566 while (!Types.empty()) {
6567 QualType BaseType;
6568 FieldDecl *CurFD;
6569 std::tie(args&: BaseType, args&: CurFD) = Types.pop_back_val();
6570 while (ParentChain.back().second == 0)
6571 ParentChain.pop_back();
6572 --ParentChain.back().second;
6573 if (BaseType.isNull())
6574 continue;
6575 // Only structs/classes are allowed to have mappers.
6576 const RecordDecl *RD = BaseType.getCanonicalType()->getAsRecordDecl();
6577 if (!RD)
6578 continue;
6579 auto It = Visited.find(Val: BaseType.getTypePtr());
6580 if (It == Visited.end()) {
6581 // Try to find the associated user-defined mapper.
6582 CXXScopeSpec MapperIdScopeSpec;
6583 DeclarationNameInfo DefaultMapperId;
6584 DefaultMapperId.setName(S.Context.DeclarationNames.getIdentifier(
6585 ID: &S.Context.Idents.get(Name: "default")));
6586 DefaultMapperId.setLoc(E->getExprLoc());
6587 ExprResult ER = buildUserDefinedMapperRef(
6588 SemaRef&: S, S: Stack->getCurScope(), MapperIdScopeSpec, MapperId: DefaultMapperId,
6589 Type: BaseType, /*UnresolvedMapper=*/nullptr, ItemLoc: E->getExprLoc());
6590 if (ER.isInvalid())
6591 continue;
6592 It = Visited.try_emplace(Key: BaseType.getTypePtr(), Args: ER.get()).first;
6593 }
6594 // Found default mapper.
6595 if (It->second) {
6596 auto *OE = new (S.Context) OpaqueValueExpr(E->getExprLoc(), CanonType,
6597 VK_LValue, OK_Ordinary, E);
6598 OE->setIsUnique(/*V=*/true);
6599 Expr *BaseExpr = OE;
6600 for (const auto &P : ParentChain) {
6601 if (P.first) {
6602 BaseExpr = S.BuildMemberExpr(
6603 Base: BaseExpr, /*IsArrow=*/false, OpLoc: E->getExprLoc(),
6604 NNS: NestedNameSpecifierLoc(), TemplateKWLoc: SourceLocation(), Member: P.first,
6605 FoundDecl: DeclAccessPair::make(D: P.first, AS: P.first->getAccess()),
6606 /*HadMultipleCandidates=*/false, MemberNameInfo: DeclarationNameInfo(),
6607 Ty: P.first->getType(), VK: VK_LValue, OK: OK_Ordinary);
6608 BaseExpr = S.DefaultLvalueConversion(E: BaseExpr).get();
6609 }
6610 }
6611 if (CurFD)
6612 BaseExpr = S.BuildMemberExpr(
6613 Base: BaseExpr, /*IsArrow=*/false, OpLoc: E->getExprLoc(),
6614 NNS: NestedNameSpecifierLoc(), TemplateKWLoc: SourceLocation(), Member: CurFD,
6615 FoundDecl: DeclAccessPair::make(D: CurFD, AS: CurFD->getAccess()),
6616 /*HadMultipleCandidates=*/false, MemberNameInfo: DeclarationNameInfo(),
6617 Ty: CurFD->getType(), VK: VK_LValue, OK: OK_Ordinary);
6618 SubExprs.push_back(Elt: BaseExpr);
6619 continue;
6620 }
6621 // Check for the "default" mapper for data members.
6622 bool FirstIter = true;
6623 for (FieldDecl *FD : RD->fields()) {
6624 if (!FD)
6625 continue;
6626 QualType FieldTy = FD->getType();
6627 if (FieldTy.isNull() ||
6628 !(FieldTy->isStructureOrClassType() || FieldTy->isUnionType()))
6629 continue;
6630 if (FirstIter) {
6631 FirstIter = false;
6632 ParentChain.emplace_back(Args&: CurFD, Args: 1);
6633 } else {
6634 ++ParentChain.back().second;
6635 }
6636 Types.emplace_back(Args&: FieldTy, Args&: FD);
6637 }
6638 }
6639 }
6640 if (SubExprs.empty())
6641 continue;
6642 CXXScopeSpec MapperIdScopeSpec;
6643 DeclarationNameInfo MapperId;
6644 if (OMPClause *NewClause = S.OpenMP().ActOnOpenMPMapClause(
6645 IteratorModifier: nullptr, MapTypeModifiers: C->getMapTypeModifiers(), MapTypeModifiersLoc: C->getMapTypeModifiersLoc(),
6646 MapperIdScopeSpec, MapperId, MapType: C->getMapType(),
6647 /*IsMapTypeImplicit=*/true, MapLoc: SourceLocation(), ColonLoc: SourceLocation(),
6648 VarList: SubExprs, Locs: OMPVarListLocTy()))
6649 Clauses.push_back(Elt: NewClause);
6650 }
6651}
6652
6653namespace {
6654/// A 'teams loop' with a nested 'loop bind(parallel)' or generic function
6655/// call in the associated loop-nest cannot be a 'parallel for'.
6656class TeamsLoopChecker final : public ConstStmtVisitor<TeamsLoopChecker> {
6657 Sema &SemaRef;
6658
6659public:
6660 bool teamsLoopCanBeParallelFor() const { return TeamsLoopCanBeParallelFor; }
6661
6662 // Is there a nested OpenMP loop bind(parallel)
6663 void VisitOMPExecutableDirective(const OMPExecutableDirective *D) {
6664 if (D->getDirectiveKind() == llvm::omp::Directive::OMPD_loop) {
6665 if (const auto *C = D->getSingleClause<OMPBindClause>())
6666 if (C->getBindKind() == OMPC_BIND_parallel) {
6667 TeamsLoopCanBeParallelFor = false;
6668 // No need to continue visiting any more
6669 return;
6670 }
6671 }
6672 for (const Stmt *Child : D->children())
6673 if (Child)
6674 Visit(S: Child);
6675 }
6676
6677 void VisitCallExpr(const CallExpr *C) {
6678 // Function calls inhibit parallel loop translation of 'target teams loop'
6679 // unless the assume-no-nested-parallelism flag has been specified.
6680 // OpenMP API runtime library calls do not inhibit parallel loop
6681 // translation, regardless of the assume-no-nested-parallelism.
6682 bool IsOpenMPAPI = false;
6683 auto *FD = dyn_cast_or_null<FunctionDecl>(Val: C->getCalleeDecl());
6684 if (FD) {
6685 std::string Name = FD->getNameInfo().getAsString();
6686 IsOpenMPAPI = Name.find(s: "omp_") == 0;
6687 }
6688 TeamsLoopCanBeParallelFor =
6689 IsOpenMPAPI || SemaRef.getLangOpts().OpenMPNoNestedParallelism;
6690 if (!TeamsLoopCanBeParallelFor)
6691 return;
6692
6693 for (const Stmt *Child : C->children())
6694 if (Child)
6695 Visit(S: Child);
6696 }
6697
6698 void VisitCapturedStmt(const CapturedStmt *S) {
6699 if (!S)
6700 return;
6701 Visit(S: S->getCapturedDecl()->getBody());
6702 }
6703
6704 void VisitStmt(const Stmt *S) {
6705 if (!S)
6706 return;
6707 for (const Stmt *Child : S->children())
6708 if (Child)
6709 Visit(S: Child);
6710 }
6711 explicit TeamsLoopChecker(Sema &SemaRef)
6712 : SemaRef(SemaRef), TeamsLoopCanBeParallelFor(true) {}
6713
6714private:
6715 bool TeamsLoopCanBeParallelFor;
6716};
6717} // namespace
6718
6719static bool teamsLoopCanBeParallelFor(Stmt *AStmt, Sema &SemaRef) {
6720 TeamsLoopChecker Checker(SemaRef);
6721 Checker.Visit(S: AStmt);
6722 return Checker.teamsLoopCanBeParallelFor();
6723}
6724
6725StmtResult SemaOpenMP::ActOnOpenMPExecutableDirective(
6726 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
6727 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
6728 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
6729 assert(isOpenMPExecutableDirective(Kind) && "Unexpected directive category");
6730
6731 StmtResult Res = StmtError();
6732 OpenMPBindClauseKind BindKind = OMPC_BIND_unknown;
6733 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
6734
6735 if (const OMPBindClause *BC =
6736 OMPExecutableDirective::getSingleClause<OMPBindClause>(Clauses))
6737 BindKind = BC->getBindKind();
6738
6739 if (Kind == OMPD_loop && BindKind == OMPC_BIND_unknown) {
6740 const OpenMPDirectiveKind ParentDirective = DSAStack->getParentDirective();
6741
6742 // Setting the enclosing teams or parallel construct for the loop
6743 // directive without bind clause.
6744 // [5.0:129:25-28] If the bind clause is not present on the construct and
6745 // the loop construct is closely nested inside a teams or parallel
6746 // construct, the binding region is the corresponding teams or parallel
6747 // region. If none of those conditions hold, the binding region is not
6748 // defined.
6749 BindKind = OMPC_BIND_thread; // Default bind(thread) if binding is unknown
6750 ArrayRef<OpenMPDirectiveKind> ParentLeafs =
6751 getLeafConstructsOrSelf(D: ParentDirective);
6752
6753 if (ParentDirective == OMPD_unknown) {
6754 Diag(DSAStack->getDefaultDSALocation(),
6755 DiagID: diag::err_omp_bind_required_on_loop);
6756 } else if (ParentLeafs.back() == OMPD_parallel) {
6757 BindKind = OMPC_BIND_parallel;
6758 } else if (ParentLeafs.back() == OMPD_teams) {
6759 BindKind = OMPC_BIND_teams;
6760 }
6761
6762 assert(BindKind != OMPC_BIND_unknown && "Expecting BindKind");
6763
6764 OMPClause *C =
6765 ActOnOpenMPBindClause(Kind: BindKind, KindLoc: SourceLocation(), StartLoc: SourceLocation(),
6766 LParenLoc: SourceLocation(), EndLoc: SourceLocation());
6767 ClausesWithImplicit.push_back(Elt: C);
6768 }
6769
6770 // Diagnose "loop bind(teams)" with "reduction".
6771 if (Kind == OMPD_loop && BindKind == OMPC_BIND_teams) {
6772 for (OMPClause *C : Clauses) {
6773 if (C->getClauseKind() == OMPC_reduction)
6774 Diag(DSAStack->getDefaultDSALocation(),
6775 DiagID: diag::err_omp_loop_reduction_clause);
6776 }
6777 }
6778
6779 // First check CancelRegion which is then used in checkNestingOfRegions.
6780 if (checkCancelRegion(SemaRef, CurrentRegion: Kind, CancelRegion, StartLoc) ||
6781 checkNestingOfRegions(SemaRef, DSAStack, CurrentRegion: Kind, CurrentName: DirName, CancelRegion,
6782 BindKind, StartLoc)) {
6783 return StmtError();
6784 }
6785
6786 // Report affected OpenMP target offloading behavior when in HIP lang-mode.
6787 if (getLangOpts().HIP && (isOpenMPTargetExecutionDirective(DKind: Kind) ||
6788 isOpenMPTargetDataManagementDirective(DKind: Kind)))
6789 Diag(Loc: StartLoc, DiagID: diag::warn_hip_omp_target_directives);
6790
6791 VarsWithInheritedDSAType VarsWithInheritedDSA;
6792 bool ErrorFound = false;
6793 ClausesWithImplicit.append(in_start: Clauses.begin(), in_end: Clauses.end());
6794
6795 if (AStmt && !SemaRef.CurContext->isDependentContext() &&
6796 isOpenMPCapturingDirective(DKind: Kind)) {
6797 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6798
6799 // Check default data sharing attributes for referenced variables.
6800 DSAAttrChecker DSAChecker(DSAStack, SemaRef, cast<CapturedStmt>(Val: AStmt));
6801 int ThisCaptureLevel = getOpenMPCaptureLevels(DKind: Kind);
6802 Stmt *S = AStmt;
6803 while (--ThisCaptureLevel >= 0)
6804 S = cast<CapturedStmt>(Val: S)->getCapturedStmt();
6805 DSAChecker.Visit(S);
6806 if (!isOpenMPTargetDataManagementDirective(DKind: Kind) &&
6807 !isOpenMPTaskingDirective(Kind)) {
6808 // Visit subcaptures to generate implicit clauses for captured vars.
6809 auto *CS = cast<CapturedStmt>(Val: AStmt);
6810 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
6811 getOpenMPCaptureRegions(CaptureRegions, DKind: Kind);
6812 // Ignore outer tasking regions for target directives.
6813 if (CaptureRegions.size() > 1 && CaptureRegions.front() == OMPD_task)
6814 CS = cast<CapturedStmt>(Val: CS->getCapturedStmt());
6815 DSAChecker.visitSubCaptures(S: CS);
6816 }
6817 if (DSAChecker.isErrorFound())
6818 return StmtError();
6819 // Generate list of implicitly defined firstprivate variables.
6820 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
6821 VariableImplicitInfo ImpInfo = DSAChecker.getImplicitInfo();
6822
6823 SmallVector<SourceLocation, NumberOfOMPMapClauseModifiers>
6824 ImplicitMapModifiersLoc[VariableImplicitInfo::DefaultmapKindNum];
6825 // Get the original location of present modifier from Defaultmap clause.
6826 SourceLocation PresentModifierLocs[VariableImplicitInfo::DefaultmapKindNum];
6827 for (OMPClause *C : Clauses) {
6828 if (auto *DMC = dyn_cast<OMPDefaultmapClause>(Val: C))
6829 if (DMC->getDefaultmapModifier() == OMPC_DEFAULTMAP_MODIFIER_present)
6830 PresentModifierLocs[DMC->getDefaultmapKind()] =
6831 DMC->getDefaultmapModifierLoc();
6832 }
6833
6834 for (OpenMPDefaultmapClauseKind K :
6835 llvm::enum_seq_inclusive<OpenMPDefaultmapClauseKind>(
6836 Begin: OpenMPDefaultmapClauseKind(), End: OMPC_DEFAULTMAP_unknown)) {
6837 std::fill_n(first: std::back_inserter(x&: ImplicitMapModifiersLoc[K]),
6838 n: ImpInfo.MapModifiers[K].size(), value: PresentModifierLocs[K]);
6839 }
6840 // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
6841 for (OMPClause *C : Clauses) {
6842 if (auto *IRC = dyn_cast<OMPInReductionClause>(Val: C)) {
6843 for (Expr *E : IRC->taskgroup_descriptors())
6844 if (E)
6845 ImpInfo.Firstprivates.insert(X: E);
6846 }
6847 // OpenMP 5.0, 2.10.1 task Construct
6848 // [detach clause]... The event-handle will be considered as if it was
6849 // specified on a firstprivate clause.
6850 if (auto *DC = dyn_cast<OMPDetachClause>(Val: C))
6851 ImpInfo.Firstprivates.insert(X: DC->getEventHandler());
6852 }
6853 if (!ImpInfo.Firstprivates.empty()) {
6854 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
6855 VarList: ImpInfo.Firstprivates.getArrayRef(), StartLoc: SourceLocation(),
6856 LParenLoc: SourceLocation(), EndLoc: SourceLocation())) {
6857 ClausesWithImplicit.push_back(Elt: Implicit);
6858 ErrorFound = cast<OMPFirstprivateClause>(Val: Implicit)->varlist_size() !=
6859 ImpInfo.Firstprivates.size();
6860 } else {
6861 ErrorFound = true;
6862 }
6863 }
6864 if (!ImpInfo.Privates.empty()) {
6865 if (OMPClause *Implicit = ActOnOpenMPPrivateClause(
6866 VarList: ImpInfo.Privates.getArrayRef(), StartLoc: SourceLocation(),
6867 LParenLoc: SourceLocation(), EndLoc: SourceLocation())) {
6868 ClausesWithImplicit.push_back(Elt: Implicit);
6869 ErrorFound = cast<OMPPrivateClause>(Val: Implicit)->varlist_size() !=
6870 ImpInfo.Privates.size();
6871 } else {
6872 ErrorFound = true;
6873 }
6874 }
6875 // OpenMP 5.0 [2.19.7]
6876 // If a list item appears in a reduction, lastprivate or linear
6877 // clause on a combined target construct then it is treated as
6878 // if it also appears in a map clause with a map-type of tofrom
6879 if (getLangOpts().OpenMP >= 50 && Kind != OMPD_target &&
6880 isOpenMPTargetExecutionDirective(DKind: Kind)) {
6881 SmallVector<Expr *, 4> ImplicitExprs;
6882 for (OMPClause *C : Clauses) {
6883 if (auto *RC = dyn_cast<OMPReductionClause>(Val: C))
6884 for (Expr *E : RC->varlist())
6885 if (!isa<DeclRefExpr>(Val: E->IgnoreParenImpCasts()))
6886 ImplicitExprs.emplace_back(Args&: E);
6887 }
6888 if (!ImplicitExprs.empty()) {
6889 ArrayRef<Expr *> Exprs = ImplicitExprs;
6890 CXXScopeSpec MapperIdScopeSpec;
6891 DeclarationNameInfo MapperId;
6892 if (OMPClause *Implicit = ActOnOpenMPMapClause(
6893 IteratorModifier: nullptr, MapTypeModifiers: OMPC_MAP_MODIFIER_unknown, MapTypeModifiersLoc: SourceLocation(),
6894 MapperIdScopeSpec, MapperId, MapType: OMPC_MAP_tofrom,
6895 /*IsMapTypeImplicit=*/true, MapLoc: SourceLocation(), ColonLoc: SourceLocation(),
6896 VarList: Exprs, Locs: OMPVarListLocTy(), /*NoDiagnose=*/true))
6897 ClausesWithImplicit.emplace_back(Args&: Implicit);
6898 }
6899 }
6900 for (unsigned I = 0; I < VariableImplicitInfo::DefaultmapKindNum; ++I) {
6901 int ClauseKindCnt = -1;
6902 for (unsigned J = 0; J < VariableImplicitInfo::MapKindNum; ++J) {
6903 ArrayRef<Expr *> ImplicitMap = ImpInfo.Mappings[I][J].getArrayRef();
6904 ++ClauseKindCnt;
6905 if (ImplicitMap.empty())
6906 continue;
6907 CXXScopeSpec MapperIdScopeSpec;
6908 DeclarationNameInfo MapperId;
6909 auto K = static_cast<OpenMPMapClauseKind>(ClauseKindCnt);
6910 if (OMPClause *Implicit = ActOnOpenMPMapClause(
6911 IteratorModifier: nullptr, MapTypeModifiers: ImpInfo.MapModifiers[I], MapTypeModifiersLoc: ImplicitMapModifiersLoc[I],
6912 MapperIdScopeSpec, MapperId, MapType: K, /*IsMapTypeImplicit=*/true,
6913 MapLoc: SourceLocation(), ColonLoc: SourceLocation(), VarList: ImplicitMap,
6914 Locs: OMPVarListLocTy())) {
6915 ClausesWithImplicit.emplace_back(Args&: Implicit);
6916 ErrorFound |= cast<OMPMapClause>(Val: Implicit)->varlist_size() !=
6917 ImplicitMap.size();
6918 } else {
6919 ErrorFound = true;
6920 }
6921 }
6922 }
6923 // Build expressions for implicit maps of data members with 'default'
6924 // mappers.
6925 if (getLangOpts().OpenMP >= 50)
6926 processImplicitMapsWithDefaultMappers(S&: SemaRef, DSAStack,
6927 Clauses&: ClausesWithImplicit);
6928 }
6929
6930 switch (Kind) {
6931 case OMPD_parallel:
6932 Res = ActOnOpenMPParallelDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6933 EndLoc);
6934 break;
6935 case OMPD_simd:
6936 Res = ActOnOpenMPSimdDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc,
6937 VarsWithImplicitDSA&: VarsWithInheritedDSA);
6938 break;
6939 case OMPD_tile:
6940 Res =
6941 ActOnOpenMPTileDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc);
6942 break;
6943 case OMPD_stripe:
6944 Res = ActOnOpenMPStripeDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6945 EndLoc);
6946 break;
6947 case OMPD_unroll:
6948 Res = ActOnOpenMPUnrollDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6949 EndLoc);
6950 break;
6951 case OMPD_reverse:
6952 assert(ClausesWithImplicit.empty() &&
6953 "reverse directive does not support any clauses");
6954 Res = ActOnOpenMPReverseDirective(AStmt, StartLoc, EndLoc);
6955 break;
6956 case OMPD_split:
6957 Res =
6958 ActOnOpenMPSplitDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc);
6959 break;
6960 case OMPD_interchange:
6961 Res = ActOnOpenMPInterchangeDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6962 EndLoc);
6963 break;
6964 case OMPD_flatten:
6965 Res = ActOnOpenMPFlattenDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6966 EndLoc);
6967 break;
6968 case OMPD_fuse:
6969 Res =
6970 ActOnOpenMPFuseDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc);
6971 break;
6972 case OMPD_for:
6973 Res = ActOnOpenMPForDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc,
6974 VarsWithImplicitDSA&: VarsWithInheritedDSA);
6975 break;
6976 case OMPD_for_simd:
6977 Res = ActOnOpenMPForSimdDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6978 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6979 break;
6980 case OMPD_sections:
6981 Res = ActOnOpenMPSectionsDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6982 EndLoc);
6983 break;
6984 case OMPD_section:
6985 assert(ClausesWithImplicit.empty() &&
6986 "No clauses are allowed for 'omp section' directive");
6987 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
6988 break;
6989 case OMPD_single:
6990 Res = ActOnOpenMPSingleDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6991 EndLoc);
6992 break;
6993 case OMPD_master:
6994 assert(ClausesWithImplicit.empty() &&
6995 "No clauses are allowed for 'omp master' directive");
6996 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
6997 break;
6998 case OMPD_masked:
6999 Res = ActOnOpenMPMaskedDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
7000 EndLoc);
7001 break;
7002 case OMPD_critical:
7003 Res = ActOnOpenMPCriticalDirective(DirName, Clauses: ClausesWithImplicit, AStmt,
7004 StartLoc, EndLoc);
7005 break;
7006 case OMPD_parallel_for:
7007 Res = ActOnOpenMPParallelForDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
7008 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7009 break;
7010 case OMPD_parallel_for_simd:
7011 Res = ActOnOpenMPParallelForSimdDirective(
7012 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7013 break;
7014 case OMPD_scope:
7015 Res =
7016 ActOnOpenMPScopeDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc);
7017 break;
7018 case OMPD_parallel_master:
7019 Res = ActOnOpenMPParallelMasterDirective(Clauses: ClausesWithImplicit, AStmt,
7020 StartLoc, EndLoc);
7021 break;
7022 case OMPD_parallel_masked:
7023 Res = ActOnOpenMPParallelMaskedDirective(Clauses: ClausesWithImplicit, AStmt,
7024 StartLoc, EndLoc);
7025 break;
7026 case OMPD_parallel_sections:
7027 Res = ActOnOpenMPParallelSectionsDirective(Clauses: ClausesWithImplicit, AStmt,
7028 StartLoc, EndLoc);
7029 break;
7030 case OMPD_task:
7031 Res =
7032 ActOnOpenMPTaskDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc);
7033 break;
7034 case OMPD_taskyield:
7035 assert(ClausesWithImplicit.empty() &&
7036 "No clauses are allowed for 'omp taskyield' directive");
7037 assert(AStmt == nullptr &&
7038 "No associated statement allowed for 'omp taskyield' directive");
7039 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
7040 break;
7041 case OMPD_error:
7042 assert(AStmt == nullptr &&
7043 "No associated statement allowed for 'omp error' directive");
7044 Res = ActOnOpenMPErrorDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc);
7045 break;
7046 case OMPD_barrier:
7047 assert(ClausesWithImplicit.empty() &&
7048 "No clauses are allowed for 'omp barrier' directive");
7049 assert(AStmt == nullptr &&
7050 "No associated statement allowed for 'omp barrier' directive");
7051 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
7052 break;
7053 case OMPD_taskwait:
7054 assert(AStmt == nullptr &&
7055 "No associated statement allowed for 'omp taskwait' directive");
7056 Res = ActOnOpenMPTaskwaitDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc);
7057 break;
7058 case OMPD_taskgroup:
7059 Res = ActOnOpenMPTaskgroupDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
7060 EndLoc);
7061 break;
7062 case OMPD_flush:
7063 assert(AStmt == nullptr &&
7064 "No associated statement allowed for 'omp flush' directive");
7065 Res = ActOnOpenMPFlushDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc);
7066 break;
7067 case OMPD_depobj:
7068 assert(AStmt == nullptr &&
7069 "No associated statement allowed for 'omp depobj' directive");
7070 Res = ActOnOpenMPDepobjDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc);
7071 break;
7072 case OMPD_scan:
7073 assert(AStmt == nullptr &&
7074 "No associated statement allowed for 'omp scan' directive");
7075 Res = ActOnOpenMPScanDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc);
7076 break;
7077 case OMPD_ordered_blockassoc:
7078 case OMPD_ordered_standalone:
7079 Res = ActOnOpenMPOrderedDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
7080 EndLoc);
7081 break;
7082 case OMPD_atomic:
7083 Res = ActOnOpenMPAtomicDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
7084 EndLoc);
7085 break;
7086 case OMPD_teams:
7087 Res =
7088 ActOnOpenMPTeamsDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc);
7089 break;
7090 case OMPD_target:
7091 Res = ActOnOpenMPTargetDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
7092 EndLoc);
7093 break;
7094 case OMPD_target_parallel:
7095 Res = ActOnOpenMPTargetParallelDirective(Clauses: ClausesWithImplicit, AStmt,
7096 StartLoc, EndLoc);
7097 break;
7098 case OMPD_target_parallel_for:
7099 Res = ActOnOpenMPTargetParallelForDirective(
7100 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7101 break;
7102 case OMPD_cancellation_point:
7103 assert(ClausesWithImplicit.empty() &&
7104 "No clauses are allowed for 'omp cancellation point' directive");
7105 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
7106 "cancellation point' directive");
7107 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
7108 break;
7109 case OMPD_cancel:
7110 assert(AStmt == nullptr &&
7111 "No associated statement allowed for 'omp cancel' directive");
7112 Res = ActOnOpenMPCancelDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc,
7113 CancelRegion);
7114 break;
7115 case OMPD_target_data:
7116 Res = ActOnOpenMPTargetDataDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
7117 EndLoc);
7118 break;
7119 case OMPD_target_enter_data:
7120 Res = ActOnOpenMPTargetEnterDataDirective(Clauses: ClausesWithImplicit, StartLoc,
7121 EndLoc, AStmt);
7122 break;
7123 case OMPD_target_exit_data:
7124 Res = ActOnOpenMPTargetExitDataDirective(Clauses: ClausesWithImplicit, StartLoc,
7125 EndLoc, AStmt);
7126 break;
7127 case OMPD_taskloop:
7128 Res = ActOnOpenMPTaskLoopDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
7129 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7130 break;
7131 case OMPD_taskloop_simd:
7132 Res = ActOnOpenMPTaskLoopSimdDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
7133 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7134 break;
7135 case OMPD_master_taskloop:
7136 Res = ActOnOpenMPMasterTaskLoopDirective(
7137 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7138 break;
7139 case OMPD_masked_taskloop:
7140 Res = ActOnOpenMPMaskedTaskLoopDirective(
7141 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7142 break;
7143 case OMPD_master_taskloop_simd:
7144 Res = ActOnOpenMPMasterTaskLoopSimdDirective(
7145 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7146 break;
7147 case OMPD_masked_taskloop_simd:
7148 Res = ActOnOpenMPMaskedTaskLoopSimdDirective(
7149 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7150 break;
7151 case OMPD_parallel_master_taskloop:
7152 Res = ActOnOpenMPParallelMasterTaskLoopDirective(
7153 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7154 break;
7155 case OMPD_parallel_masked_taskloop:
7156 Res = ActOnOpenMPParallelMaskedTaskLoopDirective(
7157 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7158 break;
7159 case OMPD_parallel_master_taskloop_simd:
7160 Res = ActOnOpenMPParallelMasterTaskLoopSimdDirective(
7161 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7162 break;
7163 case OMPD_parallel_masked_taskloop_simd:
7164 Res = ActOnOpenMPParallelMaskedTaskLoopSimdDirective(
7165 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7166 break;
7167 case OMPD_distribute:
7168 Res = ActOnOpenMPDistributeDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
7169 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7170 break;
7171 case OMPD_target_update:
7172 Res = ActOnOpenMPTargetUpdateDirective(Clauses: ClausesWithImplicit, StartLoc,
7173 EndLoc, AStmt);
7174 break;
7175 case OMPD_distribute_parallel_for:
7176 Res = ActOnOpenMPDistributeParallelForDirective(
7177 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7178 break;
7179 case OMPD_distribute_parallel_for_simd:
7180 Res = ActOnOpenMPDistributeParallelForSimdDirective(
7181 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7182 break;
7183 case OMPD_distribute_simd:
7184 Res = ActOnOpenMPDistributeSimdDirective(
7185 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7186 break;
7187 case OMPD_target_parallel_for_simd:
7188 Res = ActOnOpenMPTargetParallelForSimdDirective(
7189 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7190 break;
7191 case OMPD_target_simd:
7192 Res = ActOnOpenMPTargetSimdDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
7193 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7194 break;
7195 case OMPD_teams_distribute:
7196 Res = ActOnOpenMPTeamsDistributeDirective(
7197 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7198 break;
7199 case OMPD_teams_distribute_simd:
7200 Res = ActOnOpenMPTeamsDistributeSimdDirective(
7201 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7202 break;
7203 case OMPD_teams_distribute_parallel_for_simd:
7204 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
7205 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7206 break;
7207 case OMPD_teams_distribute_parallel_for:
7208 Res = ActOnOpenMPTeamsDistributeParallelForDirective(
7209 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7210 break;
7211 case OMPD_target_teams:
7212 Res = ActOnOpenMPTargetTeamsDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
7213 EndLoc);
7214 break;
7215 case OMPD_target_teams_distribute:
7216 Res = ActOnOpenMPTargetTeamsDistributeDirective(
7217 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7218 break;
7219 case OMPD_target_teams_distribute_parallel_for:
7220 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
7221 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7222 break;
7223 case OMPD_target_teams_distribute_parallel_for_simd:
7224 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
7225 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7226 break;
7227 case OMPD_target_teams_distribute_simd:
7228 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
7229 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7230 break;
7231 case OMPD_interop:
7232 assert(AStmt == nullptr &&
7233 "No associated statement allowed for 'omp interop' directive");
7234 Res = ActOnOpenMPInteropDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc);
7235 break;
7236 case OMPD_dispatch:
7237 Res = ActOnOpenMPDispatchDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
7238 EndLoc);
7239 break;
7240 case OMPD_loop:
7241 Res = ActOnOpenMPGenericLoopDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
7242 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7243 break;
7244 case OMPD_teams_loop:
7245 Res = ActOnOpenMPTeamsGenericLoopDirective(
7246 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7247 break;
7248 case OMPD_target_teams_loop:
7249 Res = ActOnOpenMPTargetTeamsGenericLoopDirective(
7250 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7251 break;
7252 case OMPD_parallel_loop:
7253 Res = ActOnOpenMPParallelGenericLoopDirective(
7254 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7255 break;
7256 case OMPD_target_parallel_loop:
7257 Res = ActOnOpenMPTargetParallelGenericLoopDirective(
7258 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7259 break;
7260 case OMPD_target_loop:
7261 Res = ActOnOpenMPTargetGenericLoopDirective(
7262 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
7263 break;
7264 case OMPD_declare_target:
7265 case OMPD_end_declare_target:
7266 case OMPD_threadprivate:
7267 case OMPD_allocate:
7268 case OMPD_declare_reduction:
7269 case OMPD_declare_mapper:
7270 case OMPD_declare_simd:
7271 case OMPD_requires:
7272 case OMPD_declare_variant:
7273 case OMPD_begin_declare_variant:
7274 case OMPD_end_declare_variant:
7275 llvm_unreachable("OpenMP Directive is not allowed");
7276 case OMPD_taskgraph:
7277 Diag(Loc: StartLoc, DiagID: diag::err_omp_unexpected_directive)
7278 << 1 << getOpenMPDirectiveName(D: OMPD_taskgraph);
7279 return StmtError();
7280 case OMPD_unknown:
7281 default:
7282 llvm_unreachable("Unknown OpenMP directive");
7283 }
7284
7285 ErrorFound = Res.isInvalid() || ErrorFound;
7286
7287 // Check variables in the clauses if default(none) or
7288 // default(firstprivate) was specified.
7289 if (DSAStack->getDefaultDSA() == DSA_none ||
7290 DSAStack->getDefaultDSA() == DSA_private ||
7291 DSAStack->getDefaultDSA() == DSA_firstprivate) {
7292 DSAAttrChecker DSAChecker(DSAStack, SemaRef, nullptr);
7293 for (OMPClause *C : Clauses) {
7294 switch (C->getClauseKind()) {
7295 case OMPC_num_threads:
7296 case OMPC_dist_schedule:
7297 // Do not analyze if no parent teams directive.
7298 if (isOpenMPTeamsDirective(DKind: Kind))
7299 break;
7300 continue;
7301 case OMPC_if:
7302 if (isOpenMPTeamsDirective(DKind: Kind) &&
7303 cast<OMPIfClause>(Val: C)->getNameModifier() != OMPD_target)
7304 break;
7305 if (isOpenMPParallelDirective(DKind: Kind) &&
7306 isOpenMPTaskLoopDirective(DKind: Kind) &&
7307 cast<OMPIfClause>(Val: C)->getNameModifier() != OMPD_parallel)
7308 break;
7309 continue;
7310 case OMPC_schedule:
7311 case OMPC_detach:
7312 break;
7313 case OMPC_grainsize:
7314 case OMPC_num_tasks:
7315 case OMPC_final:
7316 case OMPC_priority:
7317 case OMPC_novariants:
7318 case OMPC_nocontext:
7319 // Do not analyze if no parent parallel directive.
7320 if (isOpenMPParallelDirective(DKind: Kind))
7321 break;
7322 continue;
7323 case OMPC_ordered:
7324 case OMPC_device:
7325 case OMPC_num_teams:
7326 case OMPC_thread_limit:
7327 case OMPC_hint:
7328 case OMPC_collapse:
7329 case OMPC_safelen:
7330 case OMPC_simdlen:
7331 case OMPC_sizes:
7332 case OMPC_depth:
7333 case OMPC_default:
7334 case OMPC_proc_bind:
7335 case OMPC_private:
7336 case OMPC_firstprivate:
7337 case OMPC_lastprivate:
7338 case OMPC_shared:
7339 case OMPC_reduction:
7340 case OMPC_task_reduction:
7341 case OMPC_in_reduction:
7342 case OMPC_linear:
7343 case OMPC_aligned:
7344 case OMPC_copyin:
7345 case OMPC_copyprivate:
7346 case OMPC_nowait:
7347 case OMPC_untied:
7348 case OMPC_mergeable:
7349 case OMPC_allocate:
7350 case OMPC_read:
7351 case OMPC_write:
7352 case OMPC_update:
7353 case OMPC_capture:
7354 case OMPC_compare:
7355 case OMPC_seq_cst:
7356 case OMPC_acq_rel:
7357 case OMPC_acquire:
7358 case OMPC_release:
7359 case OMPC_relaxed:
7360 case OMPC_depend:
7361 case OMPC_threads:
7362 case OMPC_simd:
7363 case OMPC_map:
7364 case OMPC_nogroup:
7365 case OMPC_defaultmap:
7366 case OMPC_to:
7367 case OMPC_from:
7368 case OMPC_use_device_ptr:
7369 case OMPC_use_device_addr:
7370 case OMPC_is_device_ptr:
7371 case OMPC_has_device_addr:
7372 case OMPC_nontemporal:
7373 case OMPC_order:
7374 case OMPC_destroy:
7375 case OMPC_inclusive:
7376 case OMPC_exclusive:
7377 case OMPC_uses_allocators:
7378 case OMPC_affinity:
7379 case OMPC_bind:
7380 case OMPC_filter:
7381 case OMPC_severity:
7382 case OMPC_message:
7383 continue;
7384 case OMPC_allocator:
7385 case OMPC_flush:
7386 case OMPC_depobj:
7387 case OMPC_threadprivate:
7388 case OMPC_groupprivate:
7389 case OMPC_uniform:
7390 case OMPC_unknown:
7391 case OMPC_unified_address:
7392 case OMPC_unified_shared_memory:
7393 case OMPC_reverse_offload:
7394 case OMPC_dynamic_allocators:
7395 case OMPC_atomic_default_mem_order:
7396 case OMPC_self_maps:
7397 case OMPC_device_type:
7398 case OMPC_match:
7399 case OMPC_when:
7400 case OMPC_at:
7401 default:
7402 llvm_unreachable("Unexpected clause");
7403 }
7404 for (Stmt *CC : C->children()) {
7405 if (CC)
7406 DSAChecker.Visit(S: CC);
7407 }
7408 }
7409 for (const auto &P : DSAChecker.getVarsWithInheritedDSA())
7410 VarsWithInheritedDSA[P.getFirst()] = P.getSecond();
7411 }
7412 for (const auto &P : VarsWithInheritedDSA) {
7413 if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(Val: P.getFirst()))
7414 continue;
7415 ErrorFound = true;
7416 if (DSAStack->getDefaultDSA() == DSA_none ||
7417 DSAStack->getDefaultDSA() == DSA_private ||
7418 DSAStack->getDefaultDSA() == DSA_firstprivate) {
7419 Diag(Loc: P.second->getExprLoc(), DiagID: diag::err_omp_no_dsa_for_variable)
7420 << P.first << P.second->getSourceRange();
7421 Diag(DSAStack->getDefaultDSALocation(), DiagID: diag::note_omp_default_dsa_none);
7422 } else if (getLangOpts().OpenMP >= 50) {
7423 Diag(Loc: P.second->getExprLoc(),
7424 DiagID: diag::err_omp_defaultmap_no_attr_for_variable)
7425 << P.first << P.second->getSourceRange();
7426 Diag(DSAStack->getDefaultDSALocation(),
7427 DiagID: diag::note_omp_defaultmap_attr_none);
7428 }
7429 }
7430
7431 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
7432 for (OpenMPDirectiveKind D : getLeafConstructsOrSelf(D: Kind)) {
7433 if (isAllowedClauseForDirective(D, C: OMPC_if,
7434 V: getLangOpts().getOpenMPVersion()))
7435 AllowedNameModifiers.push_back(Elt: D);
7436 }
7437 if (!AllowedNameModifiers.empty())
7438 ErrorFound = checkIfClauses(S&: SemaRef, Kind, Clauses, AllowedNameModifiers) ||
7439 ErrorFound;
7440
7441 if (ErrorFound)
7442 return StmtError();
7443
7444 if (!SemaRef.CurContext->isDependentContext() &&
7445 isOpenMPTargetExecutionDirective(DKind: Kind) &&
7446 !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
7447 DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() ||
7448 DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() ||
7449 DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) {
7450 // Register target to DSA Stack.
7451 DSAStack->addTargetDirLocation(LocStart: StartLoc);
7452 }
7453
7454 return Res;
7455}
7456
7457SemaOpenMP::DeclGroupPtrTy SemaOpenMP::ActOnOpenMPDeclareSimdDirective(
7458 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
7459 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
7460 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
7461 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
7462 assert(Aligneds.size() == Alignments.size());
7463 assert(Linears.size() == LinModifiers.size());
7464 assert(Linears.size() == Steps.size());
7465 if (!DG || DG.get().isNull())
7466 return DeclGroupPtrTy();
7467
7468 const int SimdId = 0;
7469 if (!DG.get().isSingleDecl()) {
7470 Diag(Loc: SR.getBegin(), DiagID: diag::err_omp_single_decl_in_declare_simd_variant)
7471 << SimdId;
7472 return DG;
7473 }
7474 Decl *ADecl = DG.get().getSingleDecl();
7475 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(Val: ADecl))
7476 ADecl = FTD->getTemplatedDecl();
7477
7478 auto *FD = dyn_cast<FunctionDecl>(Val: ADecl);
7479 if (!FD) {
7480 Diag(Loc: ADecl->getLocation(), DiagID: diag::err_omp_function_expected) << SimdId;
7481 return DeclGroupPtrTy();
7482 }
7483
7484 // OpenMP [2.8.2, declare simd construct, Description]
7485 // The parameter of the simdlen clause must be a constant positive integer
7486 // expression.
7487 ExprResult SL;
7488 if (Simdlen)
7489 SL = VerifyPositiveIntegerConstantInClause(Op: Simdlen, CKind: OMPC_simdlen);
7490 // OpenMP [2.8.2, declare simd construct, Description]
7491 // The special this pointer can be used as if was one of the arguments to the
7492 // function in any of the linear, aligned, or uniform clauses.
7493 // The uniform clause declares one or more arguments to have an invariant
7494 // value for all concurrent invocations of the function in the execution of a
7495 // single SIMD loop.
7496 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
7497 const Expr *UniformedLinearThis = nullptr;
7498 for (const Expr *E : Uniforms) {
7499 E = E->IgnoreParenImpCasts();
7500 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: E))
7501 if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: DRE->getDecl()))
7502 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
7503 FD->getParamDecl(i: PVD->getFunctionScopeIndex())
7504 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
7505 UniformedArgs.try_emplace(Key: PVD->getCanonicalDecl(), Args&: E);
7506 continue;
7507 }
7508 if (isa<CXXThisExpr>(Val: E)) {
7509 UniformedLinearThis = E;
7510 continue;
7511 }
7512 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_param_or_this_in_clause)
7513 << FD->getDeclName() << (isa<CXXMethodDecl>(Val: ADecl) ? 1 : 0);
7514 }
7515 // OpenMP [2.8.2, declare simd construct, Description]
7516 // The aligned clause declares that the object to which each list item points
7517 // is aligned to the number of bytes expressed in the optional parameter of
7518 // the aligned clause.
7519 // The special this pointer can be used as if was one of the arguments to the
7520 // function in any of the linear, aligned, or uniform clauses.
7521 // The type of list items appearing in the aligned clause must be array,
7522 // pointer, reference to array, or reference to pointer.
7523 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
7524 const Expr *AlignedThis = nullptr;
7525 for (const Expr *E : Aligneds) {
7526 E = E->IgnoreParenImpCasts();
7527 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: E))
7528 if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: DRE->getDecl())) {
7529 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
7530 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
7531 FD->getParamDecl(i: PVD->getFunctionScopeIndex())
7532 ->getCanonicalDecl() == CanonPVD) {
7533 // OpenMP [2.8.1, simd construct, Restrictions]
7534 // A list-item cannot appear in more than one aligned clause.
7535 auto [It, Inserted] = AlignedArgs.try_emplace(Key: CanonPVD, Args&: E);
7536 if (!Inserted) {
7537 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_used_in_clause_twice)
7538 << 1 << getOpenMPClauseNameForDiag(C: OMPC_aligned)
7539 << E->getSourceRange();
7540 Diag(Loc: It->second->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
7541 << getOpenMPClauseNameForDiag(C: OMPC_aligned);
7542 continue;
7543 }
7544 QualType QTy = PVD->getType()
7545 .getNonReferenceType()
7546 .getUnqualifiedType()
7547 .getCanonicalType();
7548 const Type *Ty = QTy.getTypePtrOrNull();
7549 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
7550 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_aligned_expected_array_or_ptr)
7551 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
7552 Diag(Loc: PVD->getLocation(), DiagID: diag::note_previous_decl) << PVD;
7553 }
7554 continue;
7555 }
7556 }
7557 if (isa<CXXThisExpr>(Val: E)) {
7558 if (AlignedThis) {
7559 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_used_in_clause_twice)
7560 << 2 << getOpenMPClauseNameForDiag(C: OMPC_aligned)
7561 << E->getSourceRange();
7562 Diag(Loc: AlignedThis->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
7563 << getOpenMPClauseNameForDiag(C: OMPC_aligned);
7564 }
7565 AlignedThis = E;
7566 continue;
7567 }
7568 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_param_or_this_in_clause)
7569 << FD->getDeclName() << (isa<CXXMethodDecl>(Val: ADecl) ? 1 : 0);
7570 }
7571 // The optional parameter of the aligned clause, alignment, must be a constant
7572 // positive integer expression. If no optional parameter is specified,
7573 // implementation-defined default alignments for SIMD instructions on the
7574 // target platforms are assumed.
7575 SmallVector<const Expr *, 4> NewAligns;
7576 for (Expr *E : Alignments) {
7577 ExprResult Align;
7578 if (E)
7579 Align = VerifyPositiveIntegerConstantInClause(Op: E, CKind: OMPC_aligned);
7580 NewAligns.push_back(Elt: Align.get());
7581 }
7582 // OpenMP [2.8.2, declare simd construct, Description]
7583 // The linear clause declares one or more list items to be private to a SIMD
7584 // lane and to have a linear relationship with respect to the iteration space
7585 // of a loop.
7586 // The special this pointer can be used as if was one of the arguments to the
7587 // function in any of the linear, aligned, or uniform clauses.
7588 // When a linear-step expression is specified in a linear clause it must be
7589 // either a constant integer expression or an integer-typed parameter that is
7590 // specified in a uniform clause on the directive.
7591 llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
7592 const bool IsUniformedThis = UniformedLinearThis != nullptr;
7593 auto MI = LinModifiers.begin();
7594 for (const Expr *E : Linears) {
7595 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
7596 ++MI;
7597 E = E->IgnoreParenImpCasts();
7598 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: E))
7599 if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: DRE->getDecl())) {
7600 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
7601 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
7602 FD->getParamDecl(i: PVD->getFunctionScopeIndex())
7603 ->getCanonicalDecl() == CanonPVD) {
7604 // OpenMP [2.15.3.7, linear Clause, Restrictions]
7605 // A list-item cannot appear in more than one linear clause.
7606 if (auto It = LinearArgs.find(Val: CanonPVD); It != LinearArgs.end()) {
7607 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_wrong_dsa)
7608 << getOpenMPClauseNameForDiag(C: OMPC_linear)
7609 << getOpenMPClauseNameForDiag(C: OMPC_linear)
7610 << E->getSourceRange();
7611 Diag(Loc: It->second->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
7612 << getOpenMPClauseNameForDiag(C: OMPC_linear);
7613 continue;
7614 }
7615 // Each argument can appear in at most one uniform or linear clause.
7616 if (auto It = UniformedArgs.find(Val: CanonPVD);
7617 It != UniformedArgs.end()) {
7618 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_wrong_dsa)
7619 << getOpenMPClauseNameForDiag(C: OMPC_linear)
7620 << getOpenMPClauseNameForDiag(C: OMPC_uniform)
7621 << E->getSourceRange();
7622 Diag(Loc: It->second->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
7623 << getOpenMPClauseNameForDiag(C: OMPC_uniform);
7624 continue;
7625 }
7626 LinearArgs[CanonPVD] = E;
7627 if (E->isValueDependent() || E->isTypeDependent() ||
7628 E->isInstantiationDependent() ||
7629 E->containsUnexpandedParameterPack())
7630 continue;
7631 (void)CheckOpenMPLinearDecl(D: CanonPVD, ELoc: E->getExprLoc(), LinKind,
7632 Type: PVD->getOriginalType(),
7633 /*IsDeclareSimd=*/true);
7634 continue;
7635 }
7636 }
7637 if (isa<CXXThisExpr>(Val: E)) {
7638 if (UniformedLinearThis) {
7639 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_wrong_dsa)
7640 << getOpenMPClauseNameForDiag(C: OMPC_linear)
7641 << getOpenMPClauseNameForDiag(C: IsUniformedThis ? OMPC_uniform
7642 : OMPC_linear)
7643 << E->getSourceRange();
7644 Diag(Loc: UniformedLinearThis->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
7645 << getOpenMPClauseNameForDiag(C: IsUniformedThis ? OMPC_uniform
7646 : OMPC_linear);
7647 continue;
7648 }
7649 UniformedLinearThis = E;
7650 if (E->isValueDependent() || E->isTypeDependent() ||
7651 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
7652 continue;
7653 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, ELoc: E->getExprLoc(), LinKind,
7654 Type: E->getType(), /*IsDeclareSimd=*/true);
7655 continue;
7656 }
7657 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_param_or_this_in_clause)
7658 << FD->getDeclName() << (isa<CXXMethodDecl>(Val: ADecl) ? 1 : 0);
7659 }
7660 Expr *Step = nullptr;
7661 Expr *NewStep = nullptr;
7662 SmallVector<Expr *, 4> NewSteps;
7663 for (Expr *E : Steps) {
7664 // Skip the same step expression, it was checked already.
7665 if (Step == E || !E) {
7666 NewSteps.push_back(Elt: E ? NewStep : nullptr);
7667 continue;
7668 }
7669 Step = E;
7670 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: Step))
7671 if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: DRE->getDecl())) {
7672 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
7673 if (UniformedArgs.count(Val: CanonPVD) == 0) {
7674 Diag(Loc: Step->getExprLoc(), DiagID: diag::err_omp_expected_uniform_param)
7675 << Step->getSourceRange();
7676 } else if (E->isValueDependent() || E->isTypeDependent() ||
7677 E->isInstantiationDependent() ||
7678 E->containsUnexpandedParameterPack() ||
7679 CanonPVD->getType()->hasIntegerRepresentation()) {
7680 NewSteps.push_back(Elt: Step);
7681 } else {
7682 Diag(Loc: Step->getExprLoc(), DiagID: diag::err_omp_expected_int_param)
7683 << Step->getSourceRange();
7684 }
7685 continue;
7686 }
7687 NewStep = Step;
7688 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
7689 !Step->isInstantiationDependent() &&
7690 !Step->containsUnexpandedParameterPack()) {
7691 NewStep = PerformOpenMPImplicitIntegerConversion(OpLoc: Step->getExprLoc(), Op: Step)
7692 .get();
7693 if (NewStep)
7694 NewStep = SemaRef
7695 .VerifyIntegerConstantExpression(
7696 E: NewStep, /*FIXME*/ CanFold: AllowFoldKind::Allow)
7697 .get();
7698 }
7699 NewSteps.push_back(Elt: NewStep);
7700 }
7701 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
7702 Ctx&: getASTContext(), BranchState: BS, Simdlen: SL.get(), Uniforms: const_cast<Expr **>(Uniforms.data()),
7703 UniformsSize: Uniforms.size(), Aligneds: const_cast<Expr **>(Aligneds.data()), AlignedsSize: Aligneds.size(),
7704 Alignments: const_cast<Expr **>(NewAligns.data()), AlignmentsSize: NewAligns.size(),
7705 Linears: const_cast<Expr **>(Linears.data()), LinearsSize: Linears.size(),
7706 Modifiers: const_cast<unsigned *>(LinModifiers.data()), ModifiersSize: LinModifiers.size(),
7707 Steps: NewSteps.data(), StepsSize: NewSteps.size(), Range: SR);
7708 ADecl->addAttr(A: NewAttr);
7709 return DG;
7710}
7711
7712StmtResult SemaOpenMP::ActOnOpenMPInformationalDirective(
7713 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
7714 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7715 SourceLocation EndLoc) {
7716 assert(isOpenMPInformationalDirective(Kind) &&
7717 "Unexpected directive category");
7718
7719 StmtResult Res = StmtError();
7720
7721 switch (Kind) {
7722 case OMPD_assume:
7723 Res = ActOnOpenMPAssumeDirective(Clauses, AStmt, StartLoc, EndLoc);
7724 break;
7725 default:
7726 llvm_unreachable("Unknown OpenMP directive");
7727 }
7728
7729 return Res;
7730}
7731
7732static void setPrototype(Sema &S, FunctionDecl *FD, FunctionDecl *FDWithProto,
7733 QualType NewType) {
7734 assert(NewType->isFunctionProtoType() &&
7735 "Expected function type with prototype.");
7736 assert(FD->getType()->isFunctionNoProtoType() &&
7737 "Expected function with type with no prototype.");
7738 assert(FDWithProto->getType()->isFunctionProtoType() &&
7739 "Expected function with prototype.");
7740 // Synthesize parameters with the same types.
7741 FD->setType(NewType);
7742 SmallVector<ParmVarDecl *, 16> Params;
7743 for (const ParmVarDecl *P : FDWithProto->parameters()) {
7744 auto *Param = ParmVarDecl::Create(C&: S.getASTContext(), DC: FD, StartLoc: SourceLocation(),
7745 IdLoc: SourceLocation(), Id: nullptr, T: P->getType(),
7746 /*TInfo=*/nullptr, S: SC_None, DefArg: nullptr);
7747 Param->setScopeInfo(scopeDepth: 0, parameterIndex: Params.size());
7748 Param->setImplicit();
7749 Params.push_back(Elt: Param);
7750 }
7751
7752 FD->setParams(Params);
7753}
7754
7755void SemaOpenMP::ActOnFinishedFunctionDefinitionInOpenMPAssumeScope(Decl *D) {
7756 if (D->isInvalidDecl())
7757 return;
7758 FunctionDecl *FD = nullptr;
7759 if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(Val: D))
7760 FD = UTemplDecl->getTemplatedDecl();
7761 else
7762 FD = cast<FunctionDecl>(Val: D);
7763 assert(FD && "Expected a function declaration!");
7764
7765 // If we are instantiating templates we do *not* apply scoped assumptions but
7766 // only global ones. We apply scoped assumption to the template definition
7767 // though.
7768 if (!SemaRef.inTemplateInstantiation()) {
7769 for (OMPAssumeAttr *AA : OMPAssumeScoped)
7770 FD->addAttr(A: AA);
7771 }
7772 for (OMPAssumeAttr *AA : OMPAssumeGlobal)
7773 FD->addAttr(A: AA);
7774}
7775
7776SemaOpenMP::OMPDeclareVariantScope::OMPDeclareVariantScope(OMPTraitInfo &TI)
7777 : TI(&TI), NameSuffix(TI.getMangledName()) {}
7778
7779void SemaOpenMP::ActOnStartOfFunctionDefinitionInOpenMPDeclareVariantScope(
7780 Scope *S, Declarator &D, MultiTemplateParamsArg TemplateParamLists,
7781 SmallVectorImpl<FunctionDecl *> &Bases) {
7782 if (!D.getIdentifier())
7783 return;
7784
7785 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back();
7786
7787 // Template specialization is an extension, check if we do it.
7788 bool IsTemplated = !TemplateParamLists.empty();
7789 if (IsTemplated &&
7790 !DVScope.TI->isExtensionActive(
7791 TP: llvm::omp::TraitProperty::implementation_extension_allow_templates))
7792 return;
7793
7794 const IdentifierInfo *BaseII = D.getIdentifier();
7795 LookupResult Lookup(SemaRef, DeclarationName(BaseII), D.getIdentifierLoc(),
7796 Sema::LookupOrdinaryName);
7797 SemaRef.LookupParsedName(R&: Lookup, S, SS: &D.getCXXScopeSpec(),
7798 /*ObjectType=*/QualType());
7799
7800 TypeSourceInfo *TInfo = SemaRef.GetTypeForDeclarator(D);
7801 QualType FType = TInfo->getType();
7802
7803 bool IsConstexpr =
7804 D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Constexpr;
7805 bool IsConsteval =
7806 D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Consteval;
7807
7808 for (auto *Candidate : Lookup) {
7809 auto *CandidateDecl = Candidate->getUnderlyingDecl();
7810 FunctionDecl *UDecl = nullptr;
7811 if (IsTemplated && isa<FunctionTemplateDecl>(Val: CandidateDecl)) {
7812 auto *FTD = cast<FunctionTemplateDecl>(Val: CandidateDecl);
7813 // FIXME: Should this compare the template parameter lists on all levels?
7814 if (SemaRef.Context.isSameTemplateParameterList(
7815 X: FTD->getTemplateParameters(), Y: TemplateParamLists.back()))
7816 UDecl = FTD->getTemplatedDecl();
7817 } else if (!IsTemplated)
7818 UDecl = dyn_cast<FunctionDecl>(Val: CandidateDecl);
7819 if (!UDecl)
7820 continue;
7821
7822 // Don't specialize constexpr/consteval functions with
7823 // non-constexpr/consteval functions.
7824 if (UDecl->isConstexpr() && !IsConstexpr)
7825 continue;
7826 if (UDecl->isConsteval() && !IsConsteval)
7827 continue;
7828
7829 QualType UDeclTy = UDecl->getType();
7830 if (!UDeclTy->isDependentType()) {
7831 QualType NewType = getASTContext().mergeFunctionTypes(
7832 FType, UDeclTy, /*OfBlockPointer=*/false,
7833 /*Unqualified=*/false, /*AllowCXX=*/true);
7834 if (NewType.isNull())
7835 continue;
7836 }
7837
7838 // Found a base!
7839 Bases.push_back(Elt: UDecl);
7840 }
7841
7842 bool UseImplicitBase = !DVScope.TI->isExtensionActive(
7843 TP: llvm::omp::TraitProperty::implementation_extension_disable_implicit_base);
7844 // If no base was found we create a declaration that we use as base.
7845 if (Bases.empty() && UseImplicitBase) {
7846 D.setFunctionDefinitionKind(FunctionDefinitionKind::Declaration);
7847 Decl *BaseD = SemaRef.HandleDeclarator(S, D, TemplateParameterLists: TemplateParamLists);
7848 BaseD->setImplicit(true);
7849 if (auto *BaseTemplD = dyn_cast<FunctionTemplateDecl>(Val: BaseD))
7850 Bases.push_back(Elt: BaseTemplD->getTemplatedDecl());
7851 else
7852 Bases.push_back(Elt: cast<FunctionDecl>(Val: BaseD));
7853 }
7854
7855 std::string MangledName;
7856 MangledName += D.getIdentifier()->getName();
7857 MangledName += getOpenMPVariantManglingSeparatorStr();
7858 MangledName += DVScope.NameSuffix;
7859 IdentifierInfo &VariantII = getASTContext().Idents.get(Name: MangledName);
7860
7861 VariantII.setMangledOpenMPVariantName(true);
7862 D.SetIdentifier(Id: &VariantII, IdLoc: D.getBeginLoc());
7863}
7864
7865void SemaOpenMP::ActOnFinishedFunctionDefinitionInOpenMPDeclareVariantScope(
7866 Decl *D, SmallVectorImpl<FunctionDecl *> &Bases) {
7867 // Do not mark function as is used to prevent its emission if this is the
7868 // only place where it is used.
7869 EnterExpressionEvaluationContext Unevaluated(
7870 SemaRef, Sema::ExpressionEvaluationContext::Unevaluated);
7871
7872 FunctionDecl *FD = nullptr;
7873 if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(Val: D))
7874 FD = UTemplDecl->getTemplatedDecl();
7875 else
7876 FD = cast<FunctionDecl>(Val: D);
7877 auto *VariantFuncRef = DeclRefExpr::Create(
7878 Context: getASTContext(), QualifierLoc: NestedNameSpecifierLoc(), TemplateKWLoc: SourceLocation(), D: FD,
7879 /*RefersToEnclosingVariableOrCapture=*/false,
7880 /*NameLoc=*/FD->getLocation(), T: FD->getType(), VK: ExprValueKind::VK_PRValue);
7881
7882 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back();
7883 auto *OMPDeclareVariantA = OMPDeclareVariantAttr::CreateImplicit(
7884 Ctx&: getASTContext(), VariantFuncRef, TraitInfos: DVScope.TI,
7885 /*AdjustArgs=*/nullptr, /*AdjustArgsSize=*/0,
7886 /*AppendArgs=*/nullptr, /*AppendArgsSize=*/0);
7887 for (FunctionDecl *BaseFD : Bases)
7888 BaseFD->addAttr(A: OMPDeclareVariantA);
7889}
7890
7891ExprResult SemaOpenMP::ActOnOpenMPCall(ExprResult Call, Scope *Scope,
7892 SourceLocation LParenLoc,
7893 MultiExprArg ArgExprs,
7894 SourceLocation RParenLoc,
7895 Expr *ExecConfig) {
7896 // The common case is a regular call we do not want to specialize at all. Try
7897 // to make that case fast by bailing early.
7898 CallExpr *CE = dyn_cast<CallExpr>(Val: Call.get());
7899 if (!CE)
7900 return Call;
7901
7902 FunctionDecl *CalleeFnDecl = CE->getDirectCallee();
7903
7904 // Mark indirect calls inside target regions, to allow for insertion of
7905 // __llvm_omp_indirect_call_lookup calls during codegen.
7906 if (!CalleeFnDecl) {
7907 if (isInOpenMPTargetExecutionDirective()) {
7908 Expr *E = CE->getCallee()->IgnoreParenImpCasts();
7909 DeclRefExpr *DRE = nullptr;
7910 while (E) {
7911 if ((DRE = dyn_cast<DeclRefExpr>(Val: E)))
7912 break;
7913 if (auto *ME = dyn_cast<MemberExpr>(Val: E))
7914 E = ME->getBase()->IgnoreParenImpCasts();
7915 else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: E))
7916 E = ASE->getBase()->IgnoreParenImpCasts();
7917 else
7918 break;
7919 }
7920 VarDecl *VD = DRE ? dyn_cast<VarDecl>(Val: DRE->getDecl()) : nullptr;
7921 if (VD && !VD->hasAttr<OMPTargetIndirectCallAttr>()) {
7922 VD->addAttr(A: OMPTargetIndirectCallAttr::CreateImplicit(Ctx&: getASTContext()));
7923 if (ASTMutationListener *ML = getASTContext().getASTMutationListener())
7924 ML->DeclarationMarkedOpenMPIndirectCall(D: VD);
7925 }
7926 }
7927
7928 return Call;
7929 }
7930
7931 if (getLangOpts().OpenMP >= 50 && getLangOpts().OpenMP <= 60 &&
7932 CalleeFnDecl->getIdentifier() &&
7933 CalleeFnDecl->getName().starts_with_insensitive(Prefix: "omp_")) {
7934 // checking for any calls inside an Order region
7935 if (Scope && Scope->isOpenMPOrderClauseScope())
7936 Diag(Loc: LParenLoc, DiagID: diag::err_omp_unexpected_call_to_omp_runtime_api);
7937 }
7938
7939 if (!CalleeFnDecl->hasAttr<OMPDeclareVariantAttr>())
7940 return Call;
7941
7942 ASTContext &Context = getASTContext();
7943 std::function<void(StringRef)> DiagUnknownTrait = [this,
7944 CE](StringRef ISATrait) {
7945 // TODO Track the selector locations in a way that is accessible here to
7946 // improve the diagnostic location.
7947 Diag(Loc: CE->getBeginLoc(), DiagID: diag::warn_unknown_declare_variant_isa_trait)
7948 << ISATrait;
7949 };
7950 TargetOMPContext OMPCtx(Context, std::move(DiagUnknownTrait),
7951 SemaRef.getCurFunctionDecl(),
7952 DSAStack->getConstructTraits(), getOpenMPDeviceNum());
7953
7954 QualType CalleeFnType = CalleeFnDecl->getType();
7955
7956 SmallVector<Expr *, 4> Exprs;
7957 SmallVector<VariantMatchInfo, 4> VMIs;
7958 while (CalleeFnDecl) {
7959 for (OMPDeclareVariantAttr *A :
7960 CalleeFnDecl->specific_attrs<OMPDeclareVariantAttr>()) {
7961 Expr *VariantRef = A->getVariantFuncRef();
7962
7963 VariantMatchInfo VMI;
7964 OMPTraitInfo &TI = A->getTraitInfo();
7965 TI.getAsVariantMatchInfo(ASTCtx&: Context, VMI);
7966 if (!isVariantApplicableInContext(VMI, Ctx: OMPCtx,
7967 /*DeviceSetOnly=*/DeviceOrImplementationSetOnly: false))
7968 continue;
7969
7970 VMIs.push_back(Elt: VMI);
7971 Exprs.push_back(Elt: VariantRef);
7972 }
7973
7974 CalleeFnDecl = CalleeFnDecl->getPreviousDecl();
7975 }
7976
7977 ExprResult NewCall;
7978 do {
7979 int BestIdx = getBestVariantMatchForContext(VMIs, Ctx: OMPCtx);
7980 if (BestIdx < 0)
7981 return Call;
7982 Expr *BestExpr = cast<DeclRefExpr>(Val: Exprs[BestIdx]);
7983 Decl *BestDecl = cast<DeclRefExpr>(Val: BestExpr)->getDecl();
7984
7985 {
7986 // Try to build a (member) call expression for the current best applicable
7987 // variant expression. We allow this to fail in which case we continue
7988 // with the next best variant expression. The fail case is part of the
7989 // implementation defined behavior in the OpenMP standard when it talks
7990 // about what differences in the function prototypes: "Any differences
7991 // that the specific OpenMP context requires in the prototype of the
7992 // variant from the base function prototype are implementation defined."
7993 // This wording is there to allow the specialized variant to have a
7994 // different type than the base function. This is intended and OK but if
7995 // we cannot create a call the difference is not in the "implementation
7996 // defined range" we allow.
7997 Sema::TentativeAnalysisScope Trap(SemaRef);
7998
7999 if (auto *SpecializedMethod = dyn_cast<CXXMethodDecl>(Val: BestDecl)) {
8000 auto *MemberCall = dyn_cast<CXXMemberCallExpr>(Val: CE);
8001 BestExpr = MemberExpr::CreateImplicit(
8002 C: Context, Base: MemberCall->getImplicitObjectArgument(),
8003 /*IsArrow=*/false, MemberDecl: SpecializedMethod, T: Context.BoundMemberTy,
8004 VK: MemberCall->getValueKind(), OK: MemberCall->getObjectKind());
8005 }
8006 NewCall = SemaRef.BuildCallExpr(S: Scope, Fn: BestExpr, LParenLoc, ArgExprs,
8007 RParenLoc, ExecConfig);
8008 if (NewCall.isUsable()) {
8009 if (CallExpr *NCE = dyn_cast<CallExpr>(Val: NewCall.get())) {
8010 FunctionDecl *NewCalleeFnDecl = NCE->getDirectCallee();
8011 QualType NewType = getASTContext().mergeFunctionTypes(
8012 CalleeFnType, NewCalleeFnDecl->getType(),
8013 /*OfBlockPointer=*/false,
8014 /*Unqualified=*/false, /*AllowCXX=*/true);
8015 if (!NewType.isNull())
8016 break;
8017 // Don't use the call if the function type was not compatible.
8018 NewCall = nullptr;
8019 }
8020 }
8021 }
8022
8023 VMIs.erase(CI: VMIs.begin() + BestIdx);
8024 Exprs.erase(CI: Exprs.begin() + BestIdx);
8025 } while (!VMIs.empty());
8026
8027 if (!NewCall.isUsable())
8028 return Call;
8029 return PseudoObjectExpr::Create(Context: getASTContext(), syntactic: CE, semantic: {NewCall.get()}, resultIndex: 0);
8030}
8031
8032std::optional<std::pair<FunctionDecl *, Expr *>>
8033SemaOpenMP::checkOpenMPDeclareVariantFunction(SemaOpenMP::DeclGroupPtrTy DG,
8034 Expr *VariantRef,
8035 OMPTraitInfo &TI,
8036 unsigned NumAppendArgs,
8037 SourceRange SR) {
8038 ASTContext &Context = getASTContext();
8039 if (!DG || DG.get().isNull())
8040 return std::nullopt;
8041
8042 const int VariantId = 1;
8043 // Must be applied only to single decl.
8044 if (!DG.get().isSingleDecl()) {
8045 Diag(Loc: SR.getBegin(), DiagID: diag::err_omp_single_decl_in_declare_simd_variant)
8046 << VariantId << SR;
8047 return std::nullopt;
8048 }
8049 Decl *ADecl = DG.get().getSingleDecl();
8050 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(Val: ADecl))
8051 ADecl = FTD->getTemplatedDecl();
8052
8053 // Decl must be a function.
8054 auto *FD = dyn_cast<FunctionDecl>(Val: ADecl);
8055 if (!FD) {
8056 Diag(Loc: ADecl->getLocation(), DiagID: diag::err_omp_function_expected)
8057 << VariantId << SR;
8058 return std::nullopt;
8059 }
8060
8061 auto &&HasMultiVersionAttributes = [](const FunctionDecl *FD) {
8062 // The 'target' attribute needs to be separately checked because it does
8063 // not always signify a multiversion function declaration.
8064 return FD->isMultiVersion() || FD->hasAttr<TargetAttr>();
8065 };
8066 // OpenMP is not compatible with multiversion function attributes.
8067 if (HasMultiVersionAttributes(FD)) {
8068 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_incompat_attributes)
8069 << SR;
8070 return std::nullopt;
8071 }
8072
8073 // Allow #pragma omp declare variant only if the function is not used.
8074 if (FD->isUsed(CheckUsedAttr: false))
8075 Diag(Loc: SR.getBegin(), DiagID: diag::warn_omp_declare_variant_after_used)
8076 << FD->getLocation();
8077
8078 // Check if the function was emitted already.
8079 const FunctionDecl *Definition;
8080 if (!FD->isThisDeclarationADefinition() && FD->isDefined(Definition) &&
8081 (getLangOpts().EmitAllDecls || Context.DeclMustBeEmitted(D: Definition)))
8082 Diag(Loc: SR.getBegin(), DiagID: diag::warn_omp_declare_variant_after_emitted)
8083 << FD->getLocation();
8084
8085 // The VariantRef must point to function.
8086 if (!VariantRef) {
8087 Diag(Loc: SR.getBegin(), DiagID: diag::err_omp_function_expected) << VariantId;
8088 return std::nullopt;
8089 }
8090
8091 auto ShouldDelayChecks = [](Expr *&E, bool) {
8092 return E && (E->isTypeDependent() || E->isValueDependent() ||
8093 E->containsUnexpandedParameterPack() ||
8094 E->isInstantiationDependent());
8095 };
8096 // Do not check templates, wait until instantiation.
8097 if (FD->isDependentContext() || ShouldDelayChecks(VariantRef, false) ||
8098 TI.anyScoreOrCondition(Cond: ShouldDelayChecks))
8099 return std::make_pair(x&: FD, y&: VariantRef);
8100
8101 // Deal with non-constant score and user condition expressions.
8102 auto HandleNonConstantScoresAndConditions = [this](Expr *&E,
8103 bool IsScore) -> bool {
8104 if (!E || E->isIntegerConstantExpr(Ctx: getASTContext()))
8105 return false;
8106
8107 if (IsScore) {
8108 // We warn on non-constant scores and pretend they were not present.
8109 Diag(Loc: E->getExprLoc(), DiagID: diag::warn_omp_declare_variant_score_not_constant)
8110 << E;
8111 E = nullptr;
8112 } else {
8113 // We could replace a non-constant user condition with "false" but we
8114 // will soon need to handle these anyway for the dynamic version of
8115 // OpenMP context selectors.
8116 Diag(Loc: E->getExprLoc(),
8117 DiagID: diag::err_omp_declare_variant_user_condition_not_constant)
8118 << E;
8119 }
8120 return true;
8121 };
8122 if (TI.anyScoreOrCondition(Cond: HandleNonConstantScoresAndConditions))
8123 return std::nullopt;
8124
8125 QualType AdjustedFnType = FD->getType();
8126 if (NumAppendArgs) {
8127 const auto *PTy = AdjustedFnType->getAsAdjusted<FunctionProtoType>();
8128 if (!PTy) {
8129 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_prototype_required)
8130 << SR;
8131 return std::nullopt;
8132 }
8133 // Adjust the function type to account for an extra omp_interop_t for each
8134 // specified in the append_args clause.
8135 const TypeDecl *TD = nullptr;
8136 LookupResult Result(SemaRef, &Context.Idents.get(Name: "omp_interop_t"),
8137 SR.getBegin(), Sema::LookupOrdinaryName);
8138 if (SemaRef.LookupName(R&: Result, S: SemaRef.getCurScope())) {
8139 NamedDecl *ND = Result.getFoundDecl();
8140 TD = dyn_cast_or_null<TypeDecl>(Val: ND);
8141 }
8142 if (!TD) {
8143 Diag(Loc: SR.getBegin(), DiagID: diag::err_omp_interop_type_not_found) << SR;
8144 return std::nullopt;
8145 }
8146 QualType InteropType =
8147 Context.getTypeDeclType(Keyword: ElaboratedTypeKeyword::None,
8148 /*Qualifier=*/std::nullopt, Decl: TD);
8149 if (PTy->isVariadic()) {
8150 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_append_args_with_varargs) << SR;
8151 return std::nullopt;
8152 }
8153 llvm::SmallVector<QualType, 8> Params;
8154 Params.append(in_start: PTy->param_type_begin(), in_end: PTy->param_type_end());
8155 Params.insert(I: Params.end(), NumToInsert: NumAppendArgs, Elt: InteropType);
8156 AdjustedFnType = Context.getFunctionType(ResultTy: PTy->getReturnType(), Args: Params,
8157 EPI: PTy->getExtProtoInfo());
8158 }
8159
8160 // Convert VariantRef expression to the type of the original function to
8161 // resolve possible conflicts.
8162 ExprResult VariantRefCast = VariantRef;
8163 if (getLangOpts().CPlusPlus) {
8164 QualType FnPtrType;
8165 auto *Method = dyn_cast<CXXMethodDecl>(Val: FD);
8166 if (Method && !Method->isStatic()) {
8167 FnPtrType = Context.getMemberPointerType(
8168 T: AdjustedFnType, /*Qualifier=*/std::nullopt, Cls: Method->getParent());
8169 ExprResult ER;
8170 {
8171 // Build addr_of unary op to correctly handle type checks for member
8172 // functions.
8173 Sema::TentativeAnalysisScope Trap(SemaRef);
8174 ER = SemaRef.CreateBuiltinUnaryOp(OpLoc: VariantRef->getBeginLoc(), Opc: UO_AddrOf,
8175 InputExpr: VariantRef);
8176 }
8177 if (!ER.isUsable()) {
8178 Diag(Loc: VariantRef->getExprLoc(), DiagID: diag::err_omp_function_expected)
8179 << VariantId << VariantRef->getSourceRange();
8180 return std::nullopt;
8181 }
8182 VariantRef = ER.get();
8183 } else {
8184 FnPtrType = Context.getPointerType(T: AdjustedFnType);
8185 }
8186 QualType VarianPtrType = Context.getPointerType(T: VariantRef->getType());
8187 if (VarianPtrType.getUnqualifiedType() != FnPtrType.getUnqualifiedType()) {
8188 ImplicitConversionSequence ICS = SemaRef.TryImplicitConversion(
8189 From: VariantRef, ToType: FnPtrType.getUnqualifiedType(),
8190 /*SuppressUserConversions=*/false, AllowExplicit: Sema::AllowedExplicit::None,
8191 /*InOverloadResolution=*/false,
8192 /*CStyle=*/false,
8193 /*AllowObjCWritebackConversion=*/false);
8194 if (ICS.isFailure()) {
8195 Diag(Loc: VariantRef->getExprLoc(),
8196 DiagID: diag::err_omp_declare_variant_incompat_types)
8197 << VariantRef->getType()
8198 << ((Method && !Method->isStatic()) ? FnPtrType : FD->getType())
8199 << (NumAppendArgs ? 1 : 0) << VariantRef->getSourceRange();
8200 return std::nullopt;
8201 }
8202 VariantRefCast = SemaRef.PerformImplicitConversion(
8203 From: VariantRef, ToType: FnPtrType.getUnqualifiedType(),
8204 Action: AssignmentAction::Converting);
8205 if (!VariantRefCast.isUsable())
8206 return std::nullopt;
8207 }
8208 // Drop previously built artificial addr_of unary op for member functions.
8209 if (Method && !Method->isStatic()) {
8210 Expr *PossibleAddrOfVariantRef = VariantRefCast.get();
8211 if (auto *UO = dyn_cast<UnaryOperator>(
8212 Val: PossibleAddrOfVariantRef->IgnoreImplicit()))
8213 VariantRefCast = UO->getSubExpr();
8214 }
8215 }
8216
8217 ExprResult ER = SemaRef.CheckPlaceholderExpr(E: VariantRefCast.get());
8218 if (!ER.isUsable() ||
8219 !ER.get()->IgnoreParenImpCasts()->getType()->isFunctionType()) {
8220 Diag(Loc: VariantRef->getExprLoc(), DiagID: diag::err_omp_function_expected)
8221 << VariantId << VariantRef->getSourceRange();
8222 return std::nullopt;
8223 }
8224
8225 // The VariantRef must point to function.
8226 auto *DRE = dyn_cast<DeclRefExpr>(Val: ER.get()->IgnoreParenImpCasts());
8227 if (!DRE) {
8228 Diag(Loc: VariantRef->getExprLoc(), DiagID: diag::err_omp_function_expected)
8229 << VariantId << VariantRef->getSourceRange();
8230 return std::nullopt;
8231 }
8232 auto *NewFD = dyn_cast_or_null<FunctionDecl>(Val: DRE->getDecl());
8233 if (!NewFD) {
8234 Diag(Loc: VariantRef->getExprLoc(), DiagID: diag::err_omp_function_expected)
8235 << VariantId << VariantRef->getSourceRange();
8236 return std::nullopt;
8237 }
8238
8239 if (FD->getCanonicalDecl() == NewFD->getCanonicalDecl()) {
8240 Diag(Loc: VariantRef->getExprLoc(),
8241 DiagID: diag::err_omp_declare_variant_same_base_function)
8242 << VariantRef->getSourceRange();
8243 return std::nullopt;
8244 }
8245
8246 // Check if function types are compatible in C.
8247 if (!getLangOpts().CPlusPlus) {
8248 QualType NewType =
8249 Context.mergeFunctionTypes(AdjustedFnType, NewFD->getType());
8250 if (NewType.isNull()) {
8251 Diag(Loc: VariantRef->getExprLoc(),
8252 DiagID: diag::err_omp_declare_variant_incompat_types)
8253 << NewFD->getType() << FD->getType() << (NumAppendArgs ? 1 : 0)
8254 << VariantRef->getSourceRange();
8255 return std::nullopt;
8256 }
8257 if (NewType->isFunctionProtoType()) {
8258 if (FD->getType()->isFunctionNoProtoType())
8259 setPrototype(S&: SemaRef, FD, FDWithProto: NewFD, NewType);
8260 else if (NewFD->getType()->isFunctionNoProtoType())
8261 setPrototype(S&: SemaRef, FD: NewFD, FDWithProto: FD, NewType);
8262 }
8263 }
8264
8265 // Check if variant function is not marked with declare variant directive.
8266 if (NewFD->hasAttrs() && NewFD->hasAttr<OMPDeclareVariantAttr>()) {
8267 Diag(Loc: VariantRef->getExprLoc(),
8268 DiagID: diag::warn_omp_declare_variant_marked_as_declare_variant)
8269 << VariantRef->getSourceRange();
8270 SourceRange SR =
8271 NewFD->specific_attr_begin<OMPDeclareVariantAttr>()->getRange();
8272 Diag(Loc: SR.getBegin(), DiagID: diag::note_omp_marked_declare_variant_here) << SR;
8273 return std::nullopt;
8274 }
8275
8276 enum DoesntSupport {
8277 VirtFuncs = 1,
8278 Constructors = 3,
8279 Destructors = 4,
8280 DeletedFuncs = 5,
8281 DefaultedFuncs = 6,
8282 ConstexprFuncs = 7,
8283 ConstevalFuncs = 8,
8284 };
8285 if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(Val: FD)) {
8286 if (CXXFD->isVirtual()) {
8287 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_doesnt_support)
8288 << VirtFuncs;
8289 return std::nullopt;
8290 }
8291
8292 if (isa<CXXConstructorDecl>(Val: FD)) {
8293 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_doesnt_support)
8294 << Constructors;
8295 return std::nullopt;
8296 }
8297
8298 if (isa<CXXDestructorDecl>(Val: FD)) {
8299 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_doesnt_support)
8300 << Destructors;
8301 return std::nullopt;
8302 }
8303 }
8304
8305 if (FD->isDeleted()) {
8306 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_doesnt_support)
8307 << DeletedFuncs;
8308 return std::nullopt;
8309 }
8310
8311 if (FD->isDefaulted()) {
8312 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_doesnt_support)
8313 << DefaultedFuncs;
8314 return std::nullopt;
8315 }
8316
8317 if (FD->isConstexpr()) {
8318 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_doesnt_support)
8319 << (NewFD->isConsteval() ? ConstevalFuncs : ConstexprFuncs);
8320 return std::nullopt;
8321 }
8322
8323 // Check general compatibility.
8324 if (SemaRef.areMultiversionVariantFunctionsCompatible(
8325 OldFD: FD, NewFD, NoProtoDiagID: PartialDiagnostic::NullDiagnostic(),
8326 NoteCausedDiagIDAt: PartialDiagnosticAt(SourceLocation(),
8327 PartialDiagnostic::NullDiagnostic()),
8328 NoSupportDiagIDAt: PartialDiagnosticAt(
8329 VariantRef->getExprLoc(),
8330 SemaRef.PDiag(DiagID: diag::err_omp_declare_variant_doesnt_support)),
8331 DiffDiagIDAt: PartialDiagnosticAt(VariantRef->getExprLoc(),
8332 SemaRef.PDiag(DiagID: diag::err_omp_declare_variant_diff)
8333 << FD->getLocation()),
8334 /*TemplatesSupported=*/true, /*ConstexprSupported=*/false,
8335 /*CLinkageMayDiffer=*/true))
8336 return std::nullopt;
8337 return std::make_pair(x&: FD, y: cast<Expr>(Val: DRE));
8338}
8339
8340/// Validate prefer_type fr() and attr() arguments in an OMPInteropInfo.
8341/// fr() must be a string literal or constant integer expression.
8342/// attr() must be a string literal starting with "ompx_" and containing no
8343/// commas. Returns true if valid; emits diagnostic and returns false on first
8344/// error.
8345static bool checkPreferTypeArgs(SemaOpenMP &S, const OMPInteropInfo &Info) {
8346 auto isDependent = [](const Expr *E) {
8347 return E->isValueDependent() || E->isTypeDependent() ||
8348 E->isInstantiationDependent() ||
8349 E->containsUnexpandedParameterPack();
8350 };
8351 for (const OMPInteropPref &P : Info.Prefs) {
8352 const Expr *E = P.Fr;
8353 if (!E) {
8354 assert(Info.HasPreferAttrs && "null Fr requires OMP 6.0 syntax");
8355 } else if (!isDependent(E)) {
8356 if (!E->isIntegerConstantExpr(Ctx: S.getASTContext()) &&
8357 !isa<StringLiteral>(Val: E)) {
8358 S.Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_interop_prefer_type);
8359 return false;
8360 }
8361 }
8362 for (const Expr *A : P.Attrs) {
8363 if (isDependent(A))
8364 continue;
8365 const auto *SL = dyn_cast<StringLiteral>(Val: A);
8366 if (!SL) {
8367 S.Diag(Loc: A->getExprLoc(), DiagID: diag::err_omp_interop_attr_not_string);
8368 return false;
8369 }
8370 StringRef Str = SL->getString();
8371 if (!Str.starts_with(Prefix: "ompx_")) {
8372 S.Diag(Loc: A->getExprLoc(), DiagID: diag::err_omp_interop_attr_missing_ompx_prefix)
8373 << Str;
8374 return false;
8375 }
8376 if (Str.contains(C: ',')) {
8377 S.Diag(Loc: A->getExprLoc(), DiagID: diag::err_omp_interop_attr_contains_comma)
8378 << Str;
8379 return false;
8380 }
8381 }
8382 }
8383 return true;
8384}
8385
8386enum class OMPAdjustArgsVal { Known, Dependent, Invalid };
8387
8388/// Check one 'adjust_args' integer expression for the constant property plus
8389/// either the positive or the non-negative property (OpenMP 6.0 [5.2.1]).
8390static OMPAdjustArgsVal checkOMPAdjustArgsValue(SemaOpenMP &S, Expr *E,
8391 bool StrictlyPositive,
8392 llvm::APSInt &Result) {
8393 if (E->isInstantiationDependent())
8394 return OMPAdjustArgsVal::Dependent; // re-checked on instantiation
8395 if (S.SemaRef.VerifyIntegerConstantExpression(E, Result: &Result).isInvalid())
8396 return OMPAdjustArgsVal::Invalid; // already diagnosed
8397 if (StrictlyPositive ? !Result.isStrictlyPositive() : Result.isNegative()) {
8398 S.Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_negative_expression_in_clause)
8399 << getOpenMPClauseNameForDiag(C: OMPC_adjust_args)
8400 << (StrictlyPositive ? 1 : 0) << E->getSourceRange();
8401 return OMPAdjustArgsVal::Invalid;
8402 }
8403 if (!Result.isRepresentableByInt64()) {
8404 S.Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_large_expression_in_clause)
8405 << getOpenMPClauseNameForDiag(C: OMPC_adjust_args) << E->getSourceRange();
8406 return OMPAdjustArgsVal::Invalid;
8407 }
8408 return OMPAdjustArgsVal::Known;
8409}
8410
8411/// Check one bound of a parameter range 'lb:ub' (OpenMP 6.0 [5.2.1]).
8412/// A plain bound has the positive property, checked via
8413/// checkOMPAdjustArgsValue. An 'omp_num_args' bound's logical_offset has the
8414/// non-negative property instead, so it is checked with a different lower
8415/// bound.
8416static bool checkOMPAdjustArgsBound(SemaOpenMP &S,
8417 const OMPAdjustArgsItem::Bound &Bound) {
8418 if (Bound.Kind == OMPAdjustArgsItem::Bound::Omitted)
8419 return true;
8420 if (Bound.Kind == OMPAdjustArgsItem::Bound::NumArgs && !Bound.E)
8421 return true;
8422
8423 assert((Bound.Kind == OMPAdjustArgsItem::Bound::Expression ||
8424 Bound.Kind == OMPAdjustArgsItem::Bound::NumArgs) &&
8425 Bound.E && "expected an adjust_args bound expression");
8426 llvm::APSInt Result;
8427 bool StrictlyPositive = Bound.Kind == OMPAdjustArgsItem::Bound::Expression;
8428 return checkOMPAdjustArgsValue(S, E: Bound.E, StrictlyPositive, Result) !=
8429 OMPAdjustArgsVal::Invalid;
8430}
8431
8432static bool checkOMPAdjustArgsRange(SemaOpenMP &S,
8433 const OMPAdjustArgsItem &Range) {
8434 assert(Range.Kind == OMPAdjustArgsItem::Range &&
8435 "expected an adjust_args range");
8436 return checkOMPAdjustArgsBound(S, Bound: Range.Lower) &&
8437 checkOMPAdjustArgsBound(S, Bound: Range.Upper);
8438}
8439
8440static SourceLocation getOMPAdjustArgsItemLoc(const OMPAdjustArgsItem &Item,
8441 SourceLocation FallbackLoc) {
8442 if (Item.E)
8443 return Item.E->getExprLoc();
8444 if (Item.Lower.E)
8445 return Item.Lower.E->getExprLoc();
8446 if (Item.Upper.E)
8447 return Item.Upper.E->getExprLoc();
8448 return FallbackLoc;
8449}
8450
8451OMPAdjustArgsDescriptor *SemaOpenMP::ActOnOpenMPAdjustArgsClause(
8452 OpenMPAdjustArgsOpKind AdjustOp,
8453 OpenMPNeedDevicePtrModifier NeedDevicePtrModifier,
8454 ArrayRef<OMPAdjustArgsItem> Items, SourceLocation Loc) {
8455 // OpenMP 6.0 [9.6.2]: the fallback modifier is only valid with
8456 // need_device_ptr.
8457 if (NeedDevicePtrModifier != OMPC_NEED_DEVICE_PTR_unknown &&
8458 AdjustOp != OMPC_ADJUST_ARGS_need_device_ptr) {
8459 Diag(Loc, DiagID: diag::err_omp_unexpected_clause_modifier)
8460 << getOpenMPClauseName(C: OMPC_adjust_args);
8461 return nullptr;
8462 }
8463 return OMPAdjustArgsDescriptor::Create(C: getASTContext(), AdjustOp,
8464 NeedDevicePtrModifier, Items);
8465}
8466
8467void SemaOpenMP::ActOnOpenMPDeclareVariantDirective(
8468 FunctionDecl *FD, Expr *VariantRef, OMPTraitInfo &TI,
8469 ArrayRef<OMPAdjustArgsDescriptor *> AdjustArgs,
8470 ArrayRef<OMPInteropInfo> AppendArgs, SourceLocation AdjustArgsLoc,
8471 SourceLocation AppendArgsLoc, SourceRange SR) {
8472
8473 // OpenMP 5.1 [2.3.5, declare variant directive, Restrictions]
8474 // An adjust_args clause or append_args clause can only be specified if the
8475 // dispatch selector of the construct selector set appears in the match
8476 // clause.
8477
8478 if (!AdjustArgs.empty() || !AppendArgs.empty()) {
8479 VariantMatchInfo VMI;
8480 TI.getAsVariantMatchInfo(ASTCtx&: getASTContext(), VMI);
8481 if (!llvm::is_contained(
8482 Range&: VMI.ConstructTraits,
8483 Element: llvm::omp::TraitProperty::construct_dispatch_dispatch)) {
8484 if (!AdjustArgs.empty())
8485 Diag(Loc: AdjustArgsLoc, DiagID: diag::err_omp_clause_requires_dispatch_construct)
8486 << getOpenMPClauseNameForDiag(C: OMPC_adjust_args);
8487 if (!AppendArgs.empty())
8488 Diag(Loc: AppendArgsLoc, DiagID: diag::err_omp_clause_requires_dispatch_construct)
8489 << getOpenMPClauseNameForDiag(C: OMPC_append_args);
8490 return;
8491 }
8492 }
8493
8494 // OpenMP 6.0 [5.2.1]: each parameter list item may be specified only once
8495 // across all clauses of the same type in a directive. A range has the effect
8496 // of specifying each of its parameter positions individually.
8497 llvm::SmallPtrSet<const VarDecl *, 4> AdjustVars; // named items
8498 llvm::SmallSet<uint64_t, 4> AdjustPositions; // positions, including ranges
8499
8500 for (const OMPAdjustArgsDescriptor *Desc : AdjustArgs) {
8501 for (const OMPAdjustArgsItem &ItemInfo : Desc->items()) {
8502 if (ItemInfo.Kind == OMPAdjustArgsItem::Range) {
8503 if (!checkOMPAdjustArgsRange(S&: *this, Range: ItemInfo))
8504 continue;
8505 SmallVector<unsigned, 8> Positions;
8506 if (resolveOMPAdjustArgsItem(Item: ItemInfo, FD, NumArgs: FD->getNumParams(),
8507 Ctx: getASTContext(), Positions)) {
8508 for (unsigned Pos : Positions) {
8509 if (!AdjustPositions.insert(V: Pos).second) {
8510 Diag(Loc: getOMPAdjustArgsItemLoc(Item: ItemInfo, FallbackLoc: AdjustArgsLoc),
8511 DiagID: diag::err_omp_adjust_arg_multiple_clauses)
8512 << Pos;
8513 return;
8514 }
8515 }
8516 }
8517 continue;
8518 }
8519
8520 assert(ItemInfo.E && "expected an adjust_args expression");
8521 Expr *Item = ItemInfo.E->IgnoreParenImpCasts();
8522
8523 // A named parameter list item — unchanged from OpenMP 5.1.
8524 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: Item)) {
8525 if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: DRE->getDecl())) {
8526 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
8527 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
8528 FD->getParamDecl(i: PVD->getFunctionScopeIndex())
8529 ->getCanonicalDecl() == CanonPVD) {
8530 if (!AdjustVars.insert(Ptr: CanonPVD).second) {
8531 Diag(Loc: DRE->getLocation(),
8532 DiagID: diag::err_omp_adjust_arg_multiple_clauses)
8533 << PVD;
8534 return;
8535 }
8536 continue;
8537 }
8538 }
8539 }
8540
8541 if (getLangOpts().OpenMP < 60) {
8542 // Anything that is not a function parameter is an error before 6.0.
8543 Diag(Loc: Item->getExprLoc(), DiagID: diag::err_omp_param_or_this_in_clause)
8544 << FD << 0;
8545 return;
8546 }
8547
8548 // OpenMP 6.0 [5.2.1]: the position of a parameter, given as a
8549 // positive constant integer expression. A dependent item is skipped
8550 // here and rechecked when the template is instantiated.
8551 if (Item->isTypeDependent() || Item->getType()->isIntegerType()) {
8552 llvm::APSInt Pos;
8553 switch (checkOMPAdjustArgsValue(S&: *this, E: Item,
8554 /*StrictlyPositive=*/true, Result&: Pos)) {
8555 case OMPAdjustArgsVal::Invalid:
8556 return;
8557 case OMPAdjustArgsVal::Dependent:
8558 continue;
8559 case OMPAdjustArgsVal::Known:
8560 if (!AdjustPositions.insert(V: Pos.getZExtValue()).second) {
8561 Diag(Loc: Item->getExprLoc(), DiagID: diag::err_omp_adjust_arg_multiple_clauses)
8562 << Pos;
8563 return;
8564 }
8565 continue;
8566 }
8567 }
8568
8569 if (isa<DeclRefExpr>(Val: Item)) {
8570 Diag(Loc: Item->getExprLoc(), DiagID: diag::err_omp_param_or_this_in_clause)
8571 << FD << 0;
8572 return;
8573 }
8574
8575 // Not a name or a position: neither form OpenMP 6.0 allows for a
8576 // non-range item.
8577 Diag(Loc: Item->getExprLoc(), DiagID: diag::err_omp_adjust_args_invalid_item);
8578 return;
8579 }
8580 }
8581
8582 // OpenMP 6.0 [9.6.2]: if the need_device_addr adjust-op modifier
8583 // is present, each list item that appears in the clause must refer to an
8584 // argument in the declaration of the function variant that has a reference
8585 // type. Unlike the need_device_ptr restriction, this one is not scoped to
8586 // named items ("that refers to a specific named argument" is absent here),
8587 // so positions and ranges are checked too, via the shared item-to-positions
8588 // resolver.
8589 if (getLangOpts().OpenMP >= 60) {
8590 for (const OMPAdjustArgsDescriptor *Desc : AdjustArgs) {
8591 if (Desc->AdjustOp != OMPC_ADJUST_ARGS_need_device_addr)
8592 continue;
8593 for (const OMPAdjustArgsItem &Item : Desc->items()) {
8594 SmallVector<unsigned, 8> Positions;
8595 // With no call site in hand, 'omp_num_args' is the declared parameter
8596 // count (OpenMP 6.0 [20.1]). Positions past it denote variadic
8597 // actuals, which have no declared parameter to check against, and are
8598 // dropped by the resolver (OpenMP 6.0 [9.6.2]).
8599 resolveOMPAdjustArgsItem(Item, FD, NumArgs: FD->getNumParams(), Ctx: getASTContext(),
8600 Positions);
8601 for (unsigned Pos : Positions) {
8602 QualType ParamTy = FD->getParamDecl(i: Pos - 1)->getType();
8603 if (ParamTy->isReferenceType() || ParamTy->isDependentType())
8604 continue;
8605 Diag(Loc: getOMPAdjustArgsItemLoc(Item, FallbackLoc: AdjustArgsLoc.isValid()
8606 ? AdjustArgsLoc
8607 : SR.getBegin()),
8608 DiagID: diag::err_omp_non_by_ref_need_device_addr_modifier_argument);
8609 break; // One diagnostic per written item.
8610 }
8611 }
8612 }
8613 }
8614
8615 // OpenMP 6.0 [16.1.3] Check prefer_type fr()/attr() arguments in
8616 // append_args.
8617 for (const OMPInteropInfo &Info : AppendArgs) {
8618 if (!checkPreferTypeArgs(S&: *this, Info))
8619 return;
8620 }
8621
8622 auto *NewAttr = OMPDeclareVariantAttr::CreateImplicit(
8623 Ctx&: getASTContext(), VariantFuncRef: VariantRef, TraitInfos: &TI,
8624 AdjustArgs: const_cast<OMPAdjustArgsDescriptor **>(AdjustArgs.data()),
8625 AdjustArgsSize: AdjustArgs.size(), AppendArgs: const_cast<OMPInteropInfo *>(AppendArgs.data()),
8626 AppendArgsSize: AppendArgs.size(), Range: SR);
8627 FD->addAttr(A: NewAttr);
8628}
8629
8630static CapturedStmt *
8631setBranchProtectedScope(Sema &SemaRef, OpenMPDirectiveKind DKind, Stmt *AStmt) {
8632 auto *CS = dyn_cast<CapturedStmt>(Val: AStmt);
8633 assert(CS && "Captured statement expected");
8634 // 1.2.2 OpenMP Language Terminology
8635 // Structured block - An executable statement with a single entry at the
8636 // top and a single exit at the bottom.
8637 // The point of exit cannot be a branch out of the structured block.
8638 // longjmp() and throw() must not violate the entry/exit criteria.
8639 CS->getCapturedDecl()->setNothrow();
8640
8641 for (int ThisCaptureLevel = SemaRef.OpenMP().getOpenMPCaptureLevels(DKind);
8642 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8643 CS = cast<CapturedStmt>(Val: CS->getCapturedStmt());
8644 // 1.2.2 OpenMP Language Terminology
8645 // Structured block - An executable statement with a single entry at the
8646 // top and a single exit at the bottom.
8647 // The point of exit cannot be a branch out of the structured block.
8648 // longjmp() and throw() must not violate the entry/exit criteria.
8649 CS->getCapturedDecl()->setNothrow();
8650 }
8651 SemaRef.setFunctionHasBranchProtectedScope();
8652 return CS;
8653}
8654
8655StmtResult
8656SemaOpenMP::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
8657 Stmt *AStmt, SourceLocation StartLoc,
8658 SourceLocation EndLoc) {
8659 if (!AStmt)
8660 return StmtError();
8661
8662 // Check for conflicting capture kinds on structured bindings.
8663 if (checkClausesForDecompositionConflicts(SemaRef, DKind: OMPD_parallel, Clauses,
8664 Body: AStmt))
8665 return StmtError();
8666
8667 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel, AStmt);
8668
8669 return OMPParallelDirective::Create(
8670 C: getASTContext(), StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
8671 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
8672}
8673
8674namespace {
8675/// Iteration space of a single for loop.
8676struct LoopIterationSpace final {
8677 /// True if the condition operator is the strict compare operator (<, > or
8678 /// !=).
8679 bool IsStrictCompare = false;
8680 /// Condition of the loop.
8681 Expr *PreCond = nullptr;
8682 /// This expression calculates the number of iterations in the loop.
8683 /// It is always possible to calculate it before starting the loop.
8684 Expr *NumIterations = nullptr;
8685 /// The loop counter variable.
8686 Expr *CounterVar = nullptr;
8687 /// Private loop counter variable.
8688 Expr *PrivateCounterVar = nullptr;
8689 /// This is initializer for the initial value of #CounterVar.
8690 Expr *CounterInit = nullptr;
8691 /// This is step for the #CounterVar used to generate its update:
8692 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
8693 Expr *CounterStep = nullptr;
8694 /// Should step be subtracted?
8695 bool Subtract = false;
8696 /// Source range of the loop init.
8697 SourceRange InitSrcRange;
8698 /// Source range of the loop condition.
8699 SourceRange CondSrcRange;
8700 /// Source range of the loop increment.
8701 SourceRange IncSrcRange;
8702 /// Minimum value that can have the loop control variable. Used to support
8703 /// non-rectangular loops. Applied only for LCV with the non-iterator types,
8704 /// since only such variables can be used in non-loop invariant expressions.
8705 Expr *MinValue = nullptr;
8706 /// Maximum value that can have the loop control variable. Used to support
8707 /// non-rectangular loops. Applied only for LCV with the non-iterator type,
8708 /// since only such variables can be used in non-loop invariant expressions.
8709 Expr *MaxValue = nullptr;
8710 /// true, if the lower bound depends on the outer loop control var.
8711 bool IsNonRectangularLB = false;
8712 /// true, if the upper bound depends on the outer loop control var.
8713 bool IsNonRectangularUB = false;
8714 /// Index of the loop this loop depends on and forms non-rectangular loop
8715 /// nest.
8716 unsigned LoopDependentIdx = 0;
8717 /// Final condition for the non-rectangular loop nest support. It is used to
8718 /// check that the number of iterations for this particular counter must be
8719 /// finished.
8720 Expr *FinalCondition = nullptr;
8721};
8722
8723/// Scan an AST subtree, checking that no decls in the CollapsedLoopVarDecls
8724/// set are referenced. Used for verifying loop nest structure before
8725/// performing a loop collapse operation.
8726class ForSubExprChecker : public DynamicRecursiveASTVisitor {
8727 const llvm::SmallPtrSetImpl<const Decl *> &CollapsedLoopVarDecls;
8728 VarDecl *ForbiddenVar = nullptr;
8729 SourceRange ErrLoc;
8730
8731public:
8732 explicit ForSubExprChecker(
8733 const llvm::SmallPtrSetImpl<const Decl *> &CollapsedLoopVarDecls)
8734 : CollapsedLoopVarDecls(CollapsedLoopVarDecls) {
8735 // We want to visit implicit code, i.e. synthetic initialisation statements
8736 // created during range-for lowering.
8737 ShouldVisitImplicitCode = true;
8738 }
8739
8740 bool VisitDeclRefExpr(DeclRefExpr *E) override {
8741 ValueDecl *VD = E->getDecl();
8742 if (!isa<VarDecl, BindingDecl>(Val: VD))
8743 return true;
8744 VarDecl *V = VD->getPotentiallyDecomposedVarDecl();
8745 if (V->getType()->isReferenceType()) {
8746 VarDecl *VD = V->getDefinition();
8747 if (VD && VD->hasInit()) {
8748 Expr *I = VD->getInit();
8749 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Val: I);
8750 if (!DRE)
8751 return true;
8752 V = DRE->getDecl()->getPotentiallyDecomposedVarDecl();
8753 }
8754 }
8755 Decl *Canon = V->getCanonicalDecl();
8756 if (CollapsedLoopVarDecls.contains(Ptr: Canon)) {
8757 ForbiddenVar = V;
8758 ErrLoc = E->getSourceRange();
8759 return false;
8760 }
8761
8762 return true;
8763 }
8764
8765 VarDecl *getForbiddenVar() const { return ForbiddenVar; }
8766 SourceRange getErrRange() const { return ErrLoc; }
8767};
8768
8769/// Helper class for checking canonical form of the OpenMP loops and
8770/// extracting iteration space of each loop in the loop nest, that will be used
8771/// for IR generation.
8772class OpenMPIterationSpaceChecker {
8773 /// Reference to Sema.
8774 Sema &SemaRef;
8775 /// Does the loop associated directive support non-rectangular loops?
8776 bool SupportsNonRectangular;
8777 /// Data-sharing stack.
8778 DSAStackTy &Stack;
8779 /// A location for diagnostics (when there is no some better location).
8780 SourceLocation DefaultLoc;
8781 /// A location for diagnostics (when increment is not compatible).
8782 SourceLocation ConditionLoc;
8783 /// The set of variables declared within the (to be collapsed) loop nest.
8784 const llvm::SmallPtrSetImpl<const Decl *> &CollapsedLoopVarDecls;
8785 /// The set of induction variables from outer collapsed loops.
8786 llvm::SmallPtrSetImpl<const Decl *> &CollapsedLoopInductionVars;
8787 /// A source location for referring to loop init later.
8788 SourceRange InitSrcRange;
8789 /// A source location for referring to condition later.
8790 SourceRange ConditionSrcRange;
8791 /// A source location for referring to increment later.
8792 SourceRange IncrementSrcRange;
8793 /// Loop variable.
8794 ValueDecl *LCDecl = nullptr;
8795 /// Reference to loop variable.
8796 Expr *LCRef = nullptr;
8797 /// Lower bound (initializer for the var).
8798 Expr *LB = nullptr;
8799 /// Upper bound.
8800 Expr *UB = nullptr;
8801 /// Loop step (increment).
8802 Expr *Step = nullptr;
8803 /// This flag is true when condition is one of:
8804 /// Var < UB
8805 /// Var <= UB
8806 /// UB > Var
8807 /// UB >= Var
8808 /// This will have no value when the condition is !=
8809 std::optional<bool> TestIsLessOp;
8810 /// This flag is true when condition is strict ( < or > ).
8811 bool TestIsStrictOp = false;
8812 /// This flag is true when step is subtracted on each iteration.
8813 bool SubtractStep = false;
8814 /// The outer loop counter this loop depends on (if any).
8815 const ValueDecl *DepDecl = nullptr;
8816 /// Contains number of loop (starts from 1) on which loop counter init
8817 /// expression of this loop depends on.
8818 std::optional<unsigned> InitDependOnLC;
8819 /// Contains number of loop (starts from 1) on which loop counter condition
8820 /// expression of this loop depends on.
8821 std::optional<unsigned> CondDependOnLC;
8822 /// Checks if the provide statement depends on the loop counter.
8823 std::optional<unsigned> doesDependOnLoopCounter(const Stmt *S,
8824 bool IsInitializer);
8825 /// Original condition required for checking of the exit condition for
8826 /// non-rectangular loop.
8827 Expr *Condition = nullptr;
8828
8829public:
8830 OpenMPIterationSpaceChecker(
8831 Sema &SemaRef, bool SupportsNonRectangular, DSAStackTy &Stack,
8832 SourceLocation DefaultLoc,
8833 const llvm::SmallPtrSetImpl<const Decl *> &CollapsedLoopDecls,
8834 llvm::SmallPtrSetImpl<const Decl *> &CollapsedLoopInductionVars)
8835 : SemaRef(SemaRef), SupportsNonRectangular(SupportsNonRectangular),
8836 Stack(Stack), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
8837 CollapsedLoopVarDecls(CollapsedLoopDecls),
8838 CollapsedLoopInductionVars(CollapsedLoopInductionVars) {}
8839 /// Check init-expr for canonical loop form and save loop counter
8840 /// variable - #Var and its initialization value - #LB.
8841 bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
8842 /// Check test-expr for canonical form, save upper-bound (#UB), flags
8843 /// for less/greater and for strict/non-strict comparison.
8844 bool checkAndSetCond(Expr *S);
8845 /// Check incr-expr for canonical loop form and return true if it
8846 /// does not conform, otherwise save loop step (#Step).
8847 bool checkAndSetInc(Expr *S);
8848 /// Return the loop counter variable.
8849 ValueDecl *getLoopDecl() const { return LCDecl; }
8850 /// Return the reference expression to loop counter variable.
8851 Expr *getLoopDeclRefExpr() const { return LCRef; }
8852 /// Source range of the loop init.
8853 SourceRange getInitSrcRange() const { return InitSrcRange; }
8854 /// Source range of the loop condition.
8855 SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
8856 /// Source range of the loop increment.
8857 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
8858 /// True if the step should be subtracted.
8859 bool shouldSubtractStep() const { return SubtractStep; }
8860 /// True, if the compare operator is strict (<, > or !=).
8861 bool isStrictTestOp() const { return TestIsStrictOp; }
8862 /// Build the expression to calculate the number of iterations.
8863 Expr *buildNumIterations(
8864 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType,
8865 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
8866 /// Build the precondition expression for the loops.
8867 Expr *
8868 buildPreCond(Scope *S, Expr *Cond,
8869 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
8870 /// Build reference expression to the counter be used for codegen.
8871 DeclRefExpr *
8872 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
8873 DSAStackTy &DSA) const;
8874 /// Build reference expression to the private counter be used for
8875 /// codegen.
8876 Expr *buildPrivateCounterVar() const;
8877 /// Build initialization of the counter be used for codegen.
8878 Expr *buildCounterInit() const;
8879 /// Build step of the counter be used for codegen.
8880 Expr *buildCounterStep() const;
8881 /// Build loop data with counter value for depend clauses in ordered
8882 /// directives.
8883 Expr *
8884 buildOrderedLoopData(Scope *S, Expr *Counter,
8885 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
8886 SourceLocation Loc, Expr *Inc = nullptr,
8887 OverloadedOperatorKind OOK = OO_Amp);
8888 /// Builds the minimum value for the loop counter.
8889 std::pair<Expr *, Expr *> buildMinMaxValues(
8890 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
8891 /// Builds final condition for the non-rectangular loops.
8892 Expr *buildFinalCondition(Scope *S) const;
8893 /// Return true if any expression is dependent.
8894 bool dependent() const;
8895 /// Returns true if the initializer forms non-rectangular loop.
8896 bool doesInitDependOnLC() const { return InitDependOnLC.has_value(); }
8897 /// Returns true if the condition forms non-rectangular loop.
8898 bool doesCondDependOnLC() const { return CondDependOnLC.has_value(); }
8899 /// Returns index of the loop we depend on (starting from 1), or 0 otherwise.
8900 unsigned getLoopDependentIdx() const {
8901 return InitDependOnLC.value_or(u: CondDependOnLC.value_or(u: 0));
8902 }
8903
8904private:
8905 /// Check the right-hand side of an assignment in the increment
8906 /// expression.
8907 bool checkAndSetIncRHS(Expr *RHS);
8908 /// Helper to set loop counter variable and its initializer.
8909 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB,
8910 bool EmitDiags);
8911 /// Helper to set upper bound.
8912 bool setUB(Expr *NewUB, std::optional<bool> LessOp, bool StrictOp,
8913 SourceRange SR, SourceLocation SL);
8914 /// Helper to set loop increment.
8915 bool setStep(Expr *NewStep, bool Subtract);
8916 /// Diagnose a statement expression in a bound of a non-rectangular loop.
8917 bool checkNonRectangularBound(const Expr *E, bool IsInitializer) const;
8918};
8919
8920bool OpenMPIterationSpaceChecker::dependent() const {
8921 if (!LCDecl) {
8922 assert(!LB && !UB && !Step);
8923 return false;
8924 }
8925 return LCDecl->getType()->isDependentType() ||
8926 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
8927 (Step && Step->isValueDependent());
8928}
8929
8930/// Find a statement expression in \p E.
8931static const StmtExpr *findStmtExpr(const Expr *E) {
8932 SmallVector<const Stmt *, 8> Worklist{E};
8933 while (!Worklist.empty()) {
8934 const Stmt *S = Worklist.pop_back_val();
8935 if (!S)
8936 continue;
8937 if (const auto *SE = dyn_cast<StmtExpr>(Val: S))
8938 return SE;
8939 llvm::append_range(C&: Worklist, R: S->children());
8940 }
8941 return nullptr;
8942}
8943
8944bool OpenMPIterationSpaceChecker::checkNonRectangularBound(
8945 const Expr *E, bool IsInitializer) const {
8946 // Such a bound is emitted at every use, its declarations cannot be.
8947 const StmtExpr *SE = findStmtExpr(E);
8948 if (!SE)
8949 return false;
8950 SemaRef.Diag(Loc: SE->getBeginLoc(),
8951 DiagID: diag::err_omp_stmt_expr_in_non_rectangular_loop)
8952 << (IsInitializer ? 0 : 1);
8953 return true;
8954}
8955
8956bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
8957 Expr *NewLCRefExpr,
8958 Expr *NewLB, bool EmitDiags) {
8959 // State consistency checking to ensure correct usage.
8960 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
8961 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
8962 if (!NewLCDecl || !NewLB || NewLB->containsErrors())
8963 return true;
8964 NewLB = buildInitListValue(SemaRef, E: NewLB);
8965 LCDecl = getCanonicalDecl(D: NewLCDecl);
8966 LCRef = NewLCRefExpr;
8967 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(Val: NewLB))
8968 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
8969 if ((Ctor->isCopyOrMoveConstructor() ||
8970 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
8971 CE->getNumArgs() > 0 && CE->getArg(Arg: 0) != nullptr)
8972 NewLB = CE->getArg(Arg: 0)->IgnoreParenImpCasts();
8973 LB = NewLB;
8974 if (EmitDiags) {
8975 InitDependOnLC = doesDependOnLoopCounter(S: LB, /*IsInitializer=*/true);
8976 if (InitDependOnLC && checkNonRectangularBound(E: LB, /*IsInitializer=*/true))
8977 return true;
8978 }
8979 return false;
8980}
8981
8982bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, std::optional<bool> LessOp,
8983 bool StrictOp, SourceRange SR,
8984 SourceLocation SL) {
8985 // State consistency checking to ensure correct usage.
8986 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
8987 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
8988 if (!NewUB || NewUB->containsErrors())
8989 return true;
8990 UB = NewUB;
8991 if (LessOp)
8992 TestIsLessOp = LessOp;
8993 TestIsStrictOp = StrictOp;
8994 ConditionSrcRange = SR;
8995 ConditionLoc = SL;
8996 CondDependOnLC = doesDependOnLoopCounter(S: UB, /*IsInitializer=*/false);
8997 // The condition is also emitted as the body guard of a non-rectangular loop.
8998 if ((InitDependOnLC || CondDependOnLC) &&
8999 checkNonRectangularBound(E: UB, /*IsInitializer=*/false))
9000 return true;
9001 return false;
9002}
9003
9004bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
9005 // State consistency checking to ensure correct usage.
9006 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
9007 if (!NewStep || NewStep->containsErrors())
9008 return true;
9009 NewStep = buildInitListValue(SemaRef, E: NewStep);
9010 if (!NewStep->isValueDependent()) {
9011 // Check that the step is integer expression.
9012 SourceLocation StepLoc = NewStep->getBeginLoc();
9013 ExprResult Val = SemaRef.OpenMP().PerformOpenMPImplicitIntegerConversion(
9014 OpLoc: StepLoc, Op: getExprAsWritten(E: NewStep));
9015 if (Val.isInvalid())
9016 return true;
9017 NewStep = Val.get();
9018
9019 // OpenMP [2.6, Canonical Loop Form, Restrictions]
9020 // If test-expr is of form var relational-op b and relational-op is < or
9021 // <= then incr-expr must cause var to increase on each iteration of the
9022 // loop. If test-expr is of form var relational-op b and relational-op is
9023 // > or >= then incr-expr must cause var to decrease on each iteration of
9024 // the loop.
9025 // If test-expr is of form b relational-op var and relational-op is < or
9026 // <= then incr-expr must cause var to decrease on each iteration of the
9027 // loop. If test-expr is of form b relational-op var and relational-op is
9028 // > or >= then incr-expr must cause var to increase on each iteration of
9029 // the loop.
9030 std::optional<llvm::APSInt> Result =
9031 NewStep->getIntegerConstantExpr(Ctx: SemaRef.Context);
9032 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
9033 bool IsConstNeg =
9034 Result && Result->isSigned() && (Subtract != Result->isNegative());
9035 bool IsConstPos =
9036 Result && Result->isSigned() && (Subtract == Result->isNegative());
9037 bool IsConstZero = Result && !Result->getBoolValue();
9038
9039 // != with increment is treated as <; != with decrement is treated as >
9040 if (!TestIsLessOp)
9041 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract);
9042 if (UB && (IsConstZero ||
9043 (*TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
9044 : (IsConstPos || (IsUnsigned && !Subtract))))) {
9045 SemaRef.Diag(Loc: NewStep->getExprLoc(),
9046 DiagID: diag::err_omp_loop_incr_not_compatible)
9047 << LCDecl << *TestIsLessOp << NewStep->getSourceRange();
9048 SemaRef.Diag(Loc: ConditionLoc,
9049 DiagID: diag::note_omp_loop_cond_requires_compatible_incr)
9050 << *TestIsLessOp << ConditionSrcRange;
9051 return true;
9052 }
9053 if (*TestIsLessOp == Subtract) {
9054 NewStep =
9055 SemaRef.CreateBuiltinUnaryOp(OpLoc: NewStep->getExprLoc(), Opc: UO_Minus, InputExpr: NewStep)
9056 .get();
9057 Subtract = !Subtract;
9058 }
9059 }
9060
9061 Step = NewStep;
9062 SubtractStep = Subtract;
9063 return false;
9064}
9065
9066namespace {
9067/// Checker for the non-rectangular loops. Checks if the initializer or
9068/// condition expression references loop counter variable.
9069class LoopCounterRefChecker final
9070 : public ConstStmtVisitor<LoopCounterRefChecker, bool> {
9071 Sema &SemaRef;
9072 DSAStackTy &Stack;
9073 const ValueDecl *CurLCDecl = nullptr;
9074 const ValueDecl *DepDecl = nullptr;
9075 const ValueDecl *PrevDepDecl = nullptr;
9076 bool IsInitializer = true;
9077 bool SupportsNonRectangular;
9078 unsigned BaseLoopId = 0;
9079 bool checkDecl(const Expr *E, const ValueDecl *VD) {
9080 if (getCanonicalDecl(D: VD) == getCanonicalDecl(D: CurLCDecl)) {
9081 SemaRef.Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_stmt_depends_on_loop_counter)
9082 << (IsInitializer ? 0 : 1);
9083 return false;
9084 }
9085 const auto &&Data = Stack.isLoopControlVariable(D: VD);
9086 // OpenMP, 2.9.1 Canonical Loop Form, Restrictions.
9087 // The type of the loop iterator on which we depend may not have a random
9088 // access iterator type.
9089 if (Data.first && VD->getType()->isRecordType()) {
9090 SmallString<128> Name;
9091 llvm::raw_svector_ostream OS(Name);
9092 VD->getNameForDiagnostic(OS, Policy: SemaRef.getPrintingPolicy(),
9093 /*Qualified=*/true);
9094 SemaRef.Diag(Loc: E->getExprLoc(),
9095 DiagID: diag::err_omp_wrong_dependency_iterator_type)
9096 << OS.str();
9097 SemaRef.Diag(Loc: VD->getLocation(), DiagID: diag::note_previous_decl) << VD;
9098 return false;
9099 }
9100 if (Data.first && !SupportsNonRectangular) {
9101 SemaRef.Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_invariant_dependency);
9102 return false;
9103 }
9104 if (Data.first &&
9105 (DepDecl || (PrevDepDecl &&
9106 getCanonicalDecl(D: VD) != getCanonicalDecl(D: PrevDepDecl)))) {
9107 if (!DepDecl && PrevDepDecl)
9108 DepDecl = PrevDepDecl;
9109 SmallString<128> Name;
9110 llvm::raw_svector_ostream OS(Name);
9111 DepDecl->getNameForDiagnostic(OS, Policy: SemaRef.getPrintingPolicy(),
9112 /*Qualified=*/true);
9113 SemaRef.Diag(Loc: E->getExprLoc(),
9114 DiagID: diag::err_omp_invariant_or_linear_dependency)
9115 << OS.str();
9116 return false;
9117 }
9118 if (Data.first) {
9119 DepDecl = VD;
9120 BaseLoopId = Data.first;
9121 }
9122 return Data.first;
9123 }
9124
9125public:
9126 bool VisitDeclRefExpr(const DeclRefExpr *E) {
9127 const ValueDecl *VD = E->getDecl();
9128 if (isa<VarDecl>(Val: VD))
9129 return checkDecl(E, VD);
9130 return false;
9131 }
9132 bool VisitMemberExpr(const MemberExpr *E) {
9133 if (isa<CXXThisExpr>(Val: E->getBase()->IgnoreParens())) {
9134 const ValueDecl *VD = E->getMemberDecl();
9135 if (isa<VarDecl>(Val: VD) || isa<FieldDecl>(Val: VD))
9136 return checkDecl(E, VD);
9137 }
9138 return false;
9139 }
9140 bool VisitStmt(const Stmt *S) {
9141 bool Res = false;
9142 for (const Stmt *Child : S->children())
9143 Res = (Child && Visit(S: Child)) || Res;
9144 return Res;
9145 }
9146 explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack,
9147 const ValueDecl *CurLCDecl, bool IsInitializer,
9148 const ValueDecl *PrevDepDecl = nullptr,
9149 bool SupportsNonRectangular = true)
9150 : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl),
9151 PrevDepDecl(PrevDepDecl), IsInitializer(IsInitializer),
9152 SupportsNonRectangular(SupportsNonRectangular) {}
9153 unsigned getBaseLoopId() const {
9154 assert(CurLCDecl && "Expected loop dependency.");
9155 return BaseLoopId;
9156 }
9157 const ValueDecl *getDepDecl() const {
9158 assert(CurLCDecl && "Expected loop dependency.");
9159 return DepDecl;
9160 }
9161};
9162} // namespace
9163
9164std::optional<unsigned>
9165OpenMPIterationSpaceChecker::doesDependOnLoopCounter(const Stmt *S,
9166 bool IsInitializer) {
9167 // Check for the non-rectangular loops.
9168 LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer,
9169 DepDecl, SupportsNonRectangular);
9170 if (LoopStmtChecker.Visit(S)) {
9171 DepDecl = LoopStmtChecker.getDepDecl();
9172 return LoopStmtChecker.getBaseLoopId();
9173 }
9174 return std::nullopt;
9175}
9176
9177bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
9178 // Check init-expr for canonical loop form and save loop counter
9179 // variable - #Var and its initialization value - #LB.
9180 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
9181 // var = lb
9182 // integer-type var = lb
9183 // random-access-iterator-type var = lb
9184 // pointer-type var = lb
9185 //
9186 if (!S) {
9187 if (EmitDiags) {
9188 SemaRef.Diag(Loc: DefaultLoc, DiagID: diag::err_omp_loop_not_canonical_init);
9189 }
9190 return true;
9191 }
9192 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(Val: S))
9193 if (!ExprTemp->cleanupsHaveSideEffects())
9194 S = ExprTemp->getSubExpr();
9195
9196 if (!CollapsedLoopVarDecls.empty()) {
9197 ForSubExprChecker FSEC{CollapsedLoopVarDecls};
9198 if (!FSEC.TraverseStmt(S)) {
9199 SourceRange Range = FSEC.getErrRange();
9200 SemaRef.Diag(Loc: Range.getBegin(), DiagID: diag::err_omp_loop_bad_collapse_var)
9201 << Range.getEnd() << 0 << FSEC.getForbiddenVar();
9202 return true;
9203 }
9204 }
9205
9206 // Helper lambda to reject structured bindings used as OpenMP loop
9207 // control variables. Loop counters are implicitly private, but bindings
9208 // share storage with their decomposition, so this is not representable.
9209 auto CheckBindingAsLoopVar = [&](ValueDecl *LoopVar,
9210 SourceLocation Loc) -> bool {
9211 if (!isa<BindingDecl>(Val: LoopVar))
9212 return false;
9213 if (EmitDiags)
9214 SemaRef.Diag(Loc, DiagID: diag::err_omp_loop_var_is_structured_binding)
9215 << LoopVar;
9216 return true;
9217 };
9218
9219 // Helper lambda to check if a loop variable is already used in an outer
9220 // loop.
9221 auto CheckLoopVarReuse = [&](ValueDecl *LoopVar, SourceLocation Loc) -> bool {
9222 if (EmitDiags &&
9223 CollapsedLoopInductionVars.count(Ptr: LoopVar->getCanonicalDecl())) {
9224 SemaRef.Diag(Loc, DiagID: diag::err_omp_loop_var_reused_in_collapsed_loop)
9225 << LoopVar;
9226 return true;
9227 }
9228 return false;
9229 };
9230
9231 InitSrcRange = S->getSourceRange();
9232 if (Expr *E = dyn_cast<Expr>(Val: S))
9233 S = E->IgnoreParens();
9234 if (auto *BO = dyn_cast<BinaryOperator>(Val: S)) {
9235 if (BO->getOpcode() == BO_Assign) {
9236 Expr *LHS = BO->getLHS()->IgnoreParens();
9237 if (auto *DRE = dyn_cast<DeclRefExpr>(Val: LHS)) {
9238 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(Val: DRE->getDecl()))
9239 if (auto *ME =
9240 dyn_cast<MemberExpr>(Val: getExprAsWritten(E: CED->getInit()))) {
9241 ValueDecl *LoopVar = ME->getMemberDecl();
9242 if (CheckLoopVarReuse(LoopVar, DRE->getLocation()))
9243 return true;
9244 return setLCDeclAndLB(NewLCDecl: LoopVar, NewLCRefExpr: ME, NewLB: BO->getRHS(), EmitDiags);
9245 }
9246 ValueDecl *LoopVar = DRE->getDecl();
9247 if (CheckBindingAsLoopVar(LoopVar, DRE->getLocation()))
9248 return true;
9249 if (CheckLoopVarReuse(LoopVar, DRE->getLocation()))
9250 return true;
9251 return setLCDeclAndLB(NewLCDecl: LoopVar, NewLCRefExpr: DRE, NewLB: BO->getRHS(), EmitDiags);
9252 }
9253 if (auto *ME = dyn_cast<MemberExpr>(Val: LHS)) {
9254 if (ME->isArrow() &&
9255 isa<CXXThisExpr>(Val: ME->getBase()->IgnoreParenImpCasts())) {
9256 ValueDecl *LoopVar = ME->getMemberDecl();
9257 if (CheckLoopVarReuse(LoopVar, LHS->getBeginLoc()))
9258 return true;
9259 return setLCDeclAndLB(NewLCDecl: LoopVar, NewLCRefExpr: ME, NewLB: BO->getRHS(), EmitDiags);
9260 }
9261 }
9262 }
9263 } else if (auto *DS = dyn_cast<DeclStmt>(Val: S)) {
9264 if (DS->isSingleDecl()) {
9265 if (auto *Var = dyn_cast_or_null<VarDecl>(Val: DS->getSingleDecl())) {
9266 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
9267 // Accept non-canonical init form here but emit ext. warning.
9268 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
9269 SemaRef.Diag(Loc: S->getBeginLoc(),
9270 DiagID: diag::ext_omp_loop_not_canonical_init)
9271 << S->getSourceRange();
9272 if (CheckLoopVarReuse(Var, Var->getLocation()))
9273 return true;
9274 return setLCDeclAndLB(
9275 NewLCDecl: Var,
9276 NewLCRefExpr: buildDeclRefExpr(S&: SemaRef, D: Var,
9277 Ty: Var->getType().getNonReferenceType(),
9278 Loc: DS->getBeginLoc()),
9279 NewLB: Var->getInit(), EmitDiags);
9280 }
9281 }
9282 }
9283 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(Val: S)) {
9284 if (CE->getOperator() == OO_Equal) {
9285 Expr *LHS = CE->getArg(Arg: 0);
9286 if (auto *DRE = dyn_cast<DeclRefExpr>(Val: LHS)) {
9287 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(Val: DRE->getDecl()))
9288 if (auto *ME =
9289 dyn_cast<MemberExpr>(Val: getExprAsWritten(E: CED->getInit()))) {
9290 ValueDecl *LoopVar = ME->getMemberDecl();
9291 if (CheckLoopVarReuse(LoopVar, DRE->getLocation()))
9292 return true;
9293 return setLCDeclAndLB(NewLCDecl: LoopVar, NewLCRefExpr: ME, NewLB: CE->getArg(Arg: 1), EmitDiags);
9294 }
9295 ValueDecl *LoopVar = DRE->getDecl();
9296 if (CheckBindingAsLoopVar(LoopVar, DRE->getLocation()))
9297 return true;
9298 if (CheckLoopVarReuse(LoopVar, DRE->getLocation()))
9299 return true;
9300 return setLCDeclAndLB(NewLCDecl: LoopVar, NewLCRefExpr: DRE, NewLB: CE->getArg(Arg: 1), EmitDiags);
9301 }
9302 if (auto *ME = dyn_cast<MemberExpr>(Val: LHS)) {
9303 if (ME->isArrow() &&
9304 isa<CXXThisExpr>(Val: ME->getBase()->IgnoreParenImpCasts())) {
9305 ValueDecl *LoopVar = ME->getMemberDecl();
9306 if (CheckLoopVarReuse(LoopVar, LHS->getBeginLoc()))
9307 return true;
9308 return setLCDeclAndLB(NewLCDecl: LoopVar, NewLCRefExpr: ME, NewLB: CE->getArg(Arg: 1), EmitDiags);
9309 }
9310 }
9311 }
9312 }
9313
9314 if (dependent() || SemaRef.CurContext->isDependentContext())
9315 return false;
9316 if (EmitDiags) {
9317 SemaRef.Diag(Loc: S->getBeginLoc(), DiagID: diag::err_omp_loop_not_canonical_init)
9318 << S->getSourceRange();
9319 }
9320 return true;
9321}
9322
9323/// Ignore parenthesizes, implicit casts, copy constructor and return the
9324/// variable (which may be the loop variable) if possible.
9325static const ValueDecl *getInitLCDecl(const Expr *E) {
9326 if (!E)
9327 return nullptr;
9328 E = getExprAsWritten(E);
9329 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(Val: E))
9330 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
9331 if ((Ctor->isCopyOrMoveConstructor() ||
9332 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
9333 CE->getNumArgs() > 0 && CE->getArg(Arg: 0) != nullptr)
9334 E = CE->getArg(Arg: 0)->IgnoreParenImpCasts();
9335 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(Val: E)) {
9336 if (const auto *VD = dyn_cast<VarDecl>(Val: DRE->getDecl()))
9337 return getCanonicalDecl(D: VD);
9338 }
9339 if (const auto *ME = dyn_cast_or_null<MemberExpr>(Val: E))
9340 if (ME->isArrow() && isa<CXXThisExpr>(Val: ME->getBase()->IgnoreParenImpCasts()))
9341 return getCanonicalDecl(D: ME->getMemberDecl());
9342 return nullptr;
9343}
9344
9345bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
9346 // Check test-expr for canonical form, save upper-bound UB, flags for
9347 // less/greater and for strict/non-strict comparison.
9348 // OpenMP [2.9] Canonical loop form. Test-expr may be one of the following:
9349 // var relational-op b
9350 // b relational-op var
9351 //
9352 bool IneqCondIsCanonical = SemaRef.getLangOpts().OpenMP >= 50;
9353 if (!S) {
9354 SemaRef.Diag(Loc: DefaultLoc, DiagID: diag::err_omp_loop_not_canonical_cond)
9355 << (IneqCondIsCanonical ? 1 : 0) << LCDecl;
9356 return true;
9357 }
9358 Condition = S;
9359 S = getExprAsWritten(E: S);
9360
9361 if (!CollapsedLoopVarDecls.empty()) {
9362 ForSubExprChecker FSEC{CollapsedLoopVarDecls};
9363 if (!FSEC.TraverseStmt(S)) {
9364 SourceRange Range = FSEC.getErrRange();
9365 SemaRef.Diag(Loc: Range.getBegin(), DiagID: diag::err_omp_loop_bad_collapse_var)
9366 << Range.getEnd() << 1 << FSEC.getForbiddenVar();
9367 return true;
9368 }
9369 }
9370
9371 SourceLocation CondLoc = S->getBeginLoc();
9372 auto &&CheckAndSetCond =
9373 [this, IneqCondIsCanonical](BinaryOperatorKind Opcode, const Expr *LHS,
9374 const Expr *RHS, SourceRange SR,
9375 SourceLocation OpLoc) -> std::optional<bool> {
9376 if (BinaryOperator::isRelationalOp(Opc: Opcode)) {
9377 if (getInitLCDecl(E: LHS) == LCDecl)
9378 return setUB(NewUB: const_cast<Expr *>(RHS),
9379 LessOp: (Opcode == BO_LT || Opcode == BO_LE),
9380 StrictOp: (Opcode == BO_LT || Opcode == BO_GT), SR, SL: OpLoc);
9381 if (getInitLCDecl(E: RHS) == LCDecl)
9382 return setUB(NewUB: const_cast<Expr *>(LHS),
9383 LessOp: (Opcode == BO_GT || Opcode == BO_GE),
9384 StrictOp: (Opcode == BO_LT || Opcode == BO_GT), SR, SL: OpLoc);
9385 } else if (IneqCondIsCanonical && Opcode == BO_NE) {
9386 return setUB(NewUB: const_cast<Expr *>(getInitLCDecl(E: LHS) == LCDecl ? RHS : LHS),
9387 /*LessOp=*/std::nullopt,
9388 /*StrictOp=*/true, SR, SL: OpLoc);
9389 }
9390 return std::nullopt;
9391 };
9392 std::optional<bool> Res;
9393 if (auto *RBO = dyn_cast<CXXRewrittenBinaryOperator>(Val: S)) {
9394 CXXRewrittenBinaryOperator::DecomposedForm DF = RBO->getDecomposedForm();
9395 Res = CheckAndSetCond(DF.Opcode, DF.LHS, DF.RHS, RBO->getSourceRange(),
9396 RBO->getOperatorLoc());
9397 } else if (auto *BO = dyn_cast<BinaryOperator>(Val: S)) {
9398 Res = CheckAndSetCond(BO->getOpcode(), BO->getLHS(), BO->getRHS(),
9399 BO->getSourceRange(), BO->getOperatorLoc());
9400 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(Val: S)) {
9401 if (CE->getNumArgs() == 2) {
9402 Res = CheckAndSetCond(
9403 BinaryOperator::getOverloadedOpcode(OO: CE->getOperator()), CE->getArg(Arg: 0),
9404 CE->getArg(Arg: 1), CE->getSourceRange(), CE->getOperatorLoc());
9405 }
9406 }
9407 if (Res)
9408 return *Res;
9409 if (dependent() || SemaRef.CurContext->isDependentContext())
9410 return false;
9411 SemaRef.Diag(Loc: CondLoc, DiagID: diag::err_omp_loop_not_canonical_cond)
9412 << (IneqCondIsCanonical ? 1 : 0) << S->getSourceRange() << LCDecl;
9413 return true;
9414}
9415
9416bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
9417 // RHS of canonical loop form increment can be:
9418 // var + incr
9419 // incr + var
9420 // var - incr
9421 //
9422 RHS = RHS->IgnoreParenImpCasts();
9423 if (auto *BO = dyn_cast<BinaryOperator>(Val: RHS)) {
9424 if (BO->isAdditiveOp()) {
9425 bool IsAdd = BO->getOpcode() == BO_Add;
9426 if (getInitLCDecl(E: BO->getLHS()) == LCDecl)
9427 return setStep(NewStep: BO->getRHS(), Subtract: !IsAdd);
9428 if (IsAdd && getInitLCDecl(E: BO->getRHS()) == LCDecl)
9429 return setStep(NewStep: BO->getLHS(), /*Subtract=*/false);
9430 }
9431 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(Val: RHS)) {
9432 bool IsAdd = CE->getOperator() == OO_Plus;
9433 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
9434 if (getInitLCDecl(E: CE->getArg(Arg: 0)) == LCDecl)
9435 return setStep(NewStep: CE->getArg(Arg: 1), Subtract: !IsAdd);
9436 if (IsAdd && getInitLCDecl(E: CE->getArg(Arg: 1)) == LCDecl)
9437 return setStep(NewStep: CE->getArg(Arg: 0), /*Subtract=*/false);
9438 }
9439 }
9440 if (dependent() || SemaRef.CurContext->isDependentContext())
9441 return false;
9442 SemaRef.Diag(Loc: RHS->getBeginLoc(), DiagID: diag::err_omp_loop_not_canonical_incr)
9443 << RHS->getSourceRange() << LCDecl;
9444 return true;
9445}
9446
9447bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
9448 // Check incr-expr for canonical loop form and return true if it
9449 // does not conform.
9450 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
9451 // ++var
9452 // var++
9453 // --var
9454 // var--
9455 // var += incr
9456 // var -= incr
9457 // var = var + incr
9458 // var = incr + var
9459 // var = var - incr
9460 //
9461 if (!S) {
9462 SemaRef.Diag(Loc: DefaultLoc, DiagID: diag::err_omp_loop_not_canonical_incr) << LCDecl;
9463 return true;
9464 }
9465 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(Val: S))
9466 if (!ExprTemp->cleanupsHaveSideEffects())
9467 S = ExprTemp->getSubExpr();
9468
9469 if (!CollapsedLoopVarDecls.empty()) {
9470 ForSubExprChecker FSEC{CollapsedLoopVarDecls};
9471 if (!FSEC.TraverseStmt(S)) {
9472 SourceRange Range = FSEC.getErrRange();
9473 SemaRef.Diag(Loc: Range.getBegin(), DiagID: diag::err_omp_loop_bad_collapse_var)
9474 << Range.getEnd() << 2 << FSEC.getForbiddenVar();
9475 return true;
9476 }
9477 }
9478
9479 IncrementSrcRange = S->getSourceRange();
9480 S = S->IgnoreParens();
9481 if (auto *UO = dyn_cast<UnaryOperator>(Val: S)) {
9482 if (UO->isIncrementDecrementOp() &&
9483 getInitLCDecl(E: UO->getSubExpr()) == LCDecl)
9484 return setStep(NewStep: SemaRef
9485 .ActOnIntegerConstant(Loc: UO->getBeginLoc(),
9486 Val: (UO->isDecrementOp() ? -1 : 1))
9487 .get(),
9488 /*Subtract=*/false);
9489 } else if (auto *BO = dyn_cast<BinaryOperator>(Val: S)) {
9490 switch (BO->getOpcode()) {
9491 case BO_AddAssign:
9492 case BO_SubAssign:
9493 if (getInitLCDecl(E: BO->getLHS()) == LCDecl)
9494 return setStep(NewStep: BO->getRHS(), Subtract: BO->getOpcode() == BO_SubAssign);
9495 break;
9496 case BO_Assign:
9497 if (getInitLCDecl(E: BO->getLHS()) == LCDecl)
9498 return checkAndSetIncRHS(RHS: BO->getRHS());
9499 break;
9500 default:
9501 break;
9502 }
9503 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(Val: S)) {
9504 switch (CE->getOperator()) {
9505 case OO_PlusPlus:
9506 case OO_MinusMinus:
9507 if (getInitLCDecl(E: CE->getArg(Arg: 0)) == LCDecl)
9508 return setStep(NewStep: SemaRef
9509 .ActOnIntegerConstant(
9510 Loc: CE->getBeginLoc(),
9511 Val: ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
9512 .get(),
9513 /*Subtract=*/false);
9514 break;
9515 case OO_PlusEqual:
9516 case OO_MinusEqual:
9517 if (getInitLCDecl(E: CE->getArg(Arg: 0)) == LCDecl)
9518 return setStep(NewStep: CE->getArg(Arg: 1), Subtract: CE->getOperator() == OO_MinusEqual);
9519 break;
9520 case OO_Equal:
9521 if (getInitLCDecl(E: CE->getArg(Arg: 0)) == LCDecl)
9522 return checkAndSetIncRHS(RHS: CE->getArg(Arg: 1));
9523 break;
9524 default:
9525 break;
9526 }
9527 }
9528 if (dependent() || SemaRef.CurContext->isDependentContext())
9529 return false;
9530 SemaRef.Diag(Loc: S->getBeginLoc(), DiagID: diag::err_omp_loop_not_canonical_incr)
9531 << S->getSourceRange() << LCDecl;
9532 return true;
9533}
9534
9535static ExprResult
9536tryBuildCapture(Sema &SemaRef, Expr *Capture,
9537 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
9538 StringRef Name = ".capture_expr.") {
9539 if (SemaRef.CurContext->isDependentContext() || Capture->containsErrors())
9540 return Capture;
9541 // A statement expression must be captured even if it is constant, since the
9542 // declarations inside it can only be emitted once.
9543 if (Capture->isEvaluatable(Ctx: SemaRef.Context, AllowSideEffects: Expr::SE_AllowSideEffects) &&
9544 !findStmtExpr(E: Capture))
9545 return SemaRef.PerformImplicitConversion(From: Capture->IgnoreImpCasts(),
9546 ToType: Capture->getType(),
9547 Action: AssignmentAction::Converting,
9548 /*AllowExplicit=*/true);
9549 auto I = Captures.find(Key: Capture);
9550 if (I != Captures.end())
9551 return buildCapture(S&: SemaRef, CaptureExpr: Capture, Ref&: I->second, Name);
9552 DeclRefExpr *Ref = nullptr;
9553 ExprResult Res = buildCapture(S&: SemaRef, CaptureExpr: Capture, Ref, Name);
9554 Captures[Capture] = Ref;
9555 return Res;
9556}
9557
9558/// Calculate number of iterations, transforming to unsigned, if number of
9559/// iterations may be larger than the original type.
9560static Expr *
9561calculateNumIters(Sema &SemaRef, Scope *S, SourceLocation DefaultLoc,
9562 Expr *Lower, Expr *Upper, Expr *Step, QualType LCTy,
9563 bool TestIsStrictOp, bool RoundToStep,
9564 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
9565 std::optional<unsigned> InitDependOnLC,
9566 std::optional<unsigned> CondDependOnLC) {
9567 ExprResult NewStep = tryBuildCapture(SemaRef, Capture: Step, Captures, Name: ".new_step");
9568 if (!NewStep.isUsable())
9569 return nullptr;
9570 llvm::APSInt LRes, SRes;
9571 bool IsLowerConst = false, IsStepConst = false;
9572 if (std::optional<llvm::APSInt> Res =
9573 Lower->getIntegerConstantExpr(Ctx: SemaRef.Context)) {
9574 LRes = *Res;
9575 IsLowerConst = true;
9576 }
9577 if (std::optional<llvm::APSInt> Res =
9578 Step->getIntegerConstantExpr(Ctx: SemaRef.Context)) {
9579 SRes = *Res;
9580 IsStepConst = true;
9581 }
9582 bool NoNeedToConvert = IsLowerConst && !RoundToStep &&
9583 ((!TestIsStrictOp && LRes.isNonNegative()) ||
9584 (TestIsStrictOp && LRes.isStrictlyPositive()));
9585 bool NeedToReorganize = false;
9586 // Check if any subexpressions in Lower -Step [+ 1] lead to overflow.
9587 if (!NoNeedToConvert && IsLowerConst &&
9588 (TestIsStrictOp || (RoundToStep && IsStepConst))) {
9589 NoNeedToConvert = true;
9590 if (RoundToStep) {
9591 unsigned BW = LRes.getBitWidth() > SRes.getBitWidth()
9592 ? LRes.getBitWidth()
9593 : SRes.getBitWidth();
9594 LRes = LRes.extend(width: BW + 1);
9595 LRes.setIsSigned(true);
9596 SRes = SRes.extend(width: BW + 1);
9597 SRes.setIsSigned(true);
9598 LRes -= SRes;
9599 NoNeedToConvert = LRes.trunc(width: BW).extend(width: BW + 1) == LRes;
9600 LRes = LRes.trunc(width: BW);
9601 }
9602 if (TestIsStrictOp) {
9603 unsigned BW = LRes.getBitWidth();
9604 LRes = LRes.extend(width: BW + 1);
9605 LRes.setIsSigned(true);
9606 ++LRes;
9607 NoNeedToConvert =
9608 NoNeedToConvert && LRes.trunc(width: BW).extend(width: BW + 1) == LRes;
9609 // truncate to the original bitwidth.
9610 LRes = LRes.trunc(width: BW);
9611 }
9612 NeedToReorganize = NoNeedToConvert;
9613 }
9614 llvm::APSInt URes;
9615 bool IsUpperConst = false;
9616 if (std::optional<llvm::APSInt> Res =
9617 Upper->getIntegerConstantExpr(Ctx: SemaRef.Context)) {
9618 URes = *Res;
9619 IsUpperConst = true;
9620 }
9621 if (NoNeedToConvert && IsLowerConst && IsUpperConst &&
9622 (!RoundToStep || IsStepConst)) {
9623 unsigned BW = LRes.getBitWidth() > URes.getBitWidth() ? LRes.getBitWidth()
9624 : URes.getBitWidth();
9625 LRes = LRes.extend(width: BW + 1);
9626 LRes.setIsSigned(true);
9627 URes = URes.extend(width: BW + 1);
9628 URes.setIsSigned(true);
9629 URes -= LRes;
9630 NoNeedToConvert = URes.trunc(width: BW).extend(width: BW + 1) == URes;
9631 NeedToReorganize = NoNeedToConvert;
9632 }
9633 // If the boundaries are not constant or (Lower - Step [+ 1]) is not constant
9634 // or less than zero (Upper - (Lower - Step [+ 1]) may overflow) - promote to
9635 // unsigned.
9636 if ((!NoNeedToConvert || (LRes.isNegative() && !IsUpperConst)) &&
9637 !LCTy->isDependentType() && LCTy->isIntegerType()) {
9638 QualType LowerTy = Lower->getType();
9639 QualType UpperTy = Upper->getType();
9640 uint64_t LowerSize = SemaRef.Context.getTypeSize(T: LowerTy);
9641 uint64_t UpperSize = SemaRef.Context.getTypeSize(T: UpperTy);
9642 if ((LowerSize <= UpperSize && UpperTy->hasSignedIntegerRepresentation()) ||
9643 (LowerSize > UpperSize && LowerTy->hasSignedIntegerRepresentation())) {
9644 QualType CastType = getIntTypeForBitwidthOrBitInt(
9645 C&: SemaRef.Context, Bits: LowerSize > UpperSize ? LowerSize : UpperSize,
9646 /*Signed=*/false);
9647 Upper =
9648 SemaRef
9649 .PerformImplicitConversion(
9650 From: SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Upper).get(),
9651 ToType: CastType, Action: AssignmentAction::Converting)
9652 .get();
9653 Lower = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Lower).get();
9654 NewStep = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: NewStep.get());
9655 }
9656 }
9657 if (!Lower || !Upper || NewStep.isInvalid())
9658 return nullptr;
9659
9660 ExprResult Diff;
9661
9662 // For nested triangular loops (depth >= 2), use already computed Upper and
9663 // Lower bounds to calculate the number of iterations: Upper - Lower + 1.
9664 // Don't apply to first-level triangular loops as the standard formula handles
9665 // those correctly.
9666 if (TestIsStrictOp && InitDependOnLC.has_value() &&
9667 InitDependOnLC.value() >= 2 && !CondDependOnLC.has_value()) {
9668 Diff = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Sub, LHSExpr: Upper, RHSExpr: Lower);
9669 if (!Diff.isUsable())
9670 return nullptr;
9671
9672 Diff =
9673 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Add, LHSExpr: Diff.get(),
9674 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: DefaultLoc, Val: 1).get());
9675 if (!Diff.isUsable())
9676 return nullptr;
9677
9678 return Diff.get();
9679 }
9680
9681 // If need to reorganize, then calculate the form as Upper - (Lower - Step [+
9682 // 1]).
9683 if (NeedToReorganize) {
9684 Diff = Lower;
9685
9686 // (Lower - Step [+ 1]) may be negative. Compute it in at least Upper's
9687 // width: in a narrower unsigned type it would wrap around and then be
9688 // zero-extended in the subtraction from Upper, giving a wrong number of
9689 // iterations, e.g. for
9690 // for (unsigned i = 0; i < (size_t)N; i += 2)
9691 QualType LowerTy = Lower->getType();
9692 QualType UpperTy = Upper->getType();
9693 if (!LowerTy->isDependentType() && !UpperTy->isDependentType() &&
9694 LowerTy->isIntegerType() && UpperTy->isIntegerType() &&
9695 SemaRef.Context.getTypeSize(T: LowerTy) <
9696 SemaRef.Context.getTypeSize(T: UpperTy)) {
9697 QualType WideTy = getIntTypeForBitwidthOrBitInt(
9698 C&: SemaRef.Context, Bits: SemaRef.Context.getTypeSize(T: UpperTy),
9699 Signed: UpperTy->hasSignedIntegerRepresentation());
9700 Diff = SemaRef.PerformImplicitConversion(From: Lower, ToType: WideTy,
9701 Action: AssignmentAction::Converting);
9702 if (!Diff.isUsable())
9703 return nullptr;
9704 }
9705
9706 if (RoundToStep) {
9707 // Lower - Step
9708 Diff =
9709 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Sub, LHSExpr: Diff.get(), RHSExpr: NewStep.get());
9710 if (!Diff.isUsable())
9711 return nullptr;
9712 }
9713
9714 // Lower - Step [+ 1]
9715 if (TestIsStrictOp)
9716 Diff = SemaRef.BuildBinOp(
9717 S, OpLoc: DefaultLoc, Opc: BO_Add, LHSExpr: Diff.get(),
9718 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get());
9719 if (!Diff.isUsable())
9720 return nullptr;
9721
9722 Diff = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Diff.get());
9723 if (!Diff.isUsable())
9724 return nullptr;
9725
9726 // Upper - (Lower - Step [+ 1]).
9727 Diff = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Sub, LHSExpr: Upper, RHSExpr: Diff.get());
9728 if (!Diff.isUsable())
9729 return nullptr;
9730 } else {
9731 Diff = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Sub, LHSExpr: Upper, RHSExpr: Lower);
9732
9733 if (!Diff.isUsable() && LCTy->getAsCXXRecordDecl()) {
9734 // BuildBinOp already emitted error, this one is to point user to upper
9735 // and lower bound, and to tell what is passed to 'operator-'.
9736 SemaRef.Diag(Loc: Upper->getBeginLoc(), DiagID: diag::err_omp_loop_diff_cxx)
9737 << Upper->getSourceRange() << Lower->getSourceRange();
9738 return nullptr;
9739 }
9740
9741 if (!Diff.isUsable())
9742 return nullptr;
9743
9744 // Upper - Lower [- 1]
9745 if (TestIsStrictOp)
9746 Diff = SemaRef.BuildBinOp(
9747 S, OpLoc: DefaultLoc, Opc: BO_Sub, LHSExpr: Diff.get(),
9748 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get());
9749 if (!Diff.isUsable())
9750 return nullptr;
9751
9752 if (RoundToStep) {
9753 // Upper - Lower [- 1] + Step
9754 Diff =
9755 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Add, LHSExpr: Diff.get(), RHSExpr: NewStep.get());
9756 if (!Diff.isUsable())
9757 return nullptr;
9758 }
9759 }
9760
9761 // Parentheses (for dumping/debugging purposes only).
9762 Diff = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Diff.get());
9763 if (!Diff.isUsable())
9764 return nullptr;
9765
9766 // (Upper - Lower [- 1] + Step) / Step or (Upper - Lower) / Step
9767 Diff = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Div, LHSExpr: Diff.get(), RHSExpr: NewStep.get());
9768 if (!Diff.isUsable())
9769 return nullptr;
9770
9771 return Diff.get();
9772}
9773
9774/// Build the expression to calculate the number of iterations.
9775Expr *OpenMPIterationSpaceChecker::buildNumIterations(
9776 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType,
9777 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
9778 QualType VarType = LCDecl->getType().getNonReferenceType();
9779 if (!VarType->isIntegerType() && !VarType->isPointerType() &&
9780 !SemaRef.getLangOpts().CPlusPlus)
9781 return nullptr;
9782 Expr *LBVal = LB;
9783 Expr *UBVal = UB;
9784 // OuterVar = (LB = TestIsLessOp.getValue() ? min(LB(MinVal), LB(MaxVal)) :
9785 // max(LB(MinVal), LB(MaxVal)))
9786 if (InitDependOnLC) {
9787 const LoopIterationSpace &IS = ResultIterSpaces[*InitDependOnLC - 1];
9788 if (!IS.MinValue || !IS.MaxValue)
9789 return nullptr;
9790 // OuterVar = Min
9791 ExprResult MinValue =
9792 SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: IS.MinValue);
9793 if (!MinValue.isUsable())
9794 return nullptr;
9795
9796 ExprResult LBMinVal = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Assign,
9797 LHSExpr: IS.CounterVar, RHSExpr: MinValue.get());
9798 if (!LBMinVal.isUsable())
9799 return nullptr;
9800 // OuterVar = Min, LBVal
9801 LBMinVal =
9802 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Comma, LHSExpr: LBMinVal.get(), RHSExpr: LBVal);
9803 if (!LBMinVal.isUsable())
9804 return nullptr;
9805 // (OuterVar = Min, LBVal)
9806 LBMinVal = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: LBMinVal.get());
9807 if (!LBMinVal.isUsable())
9808 return nullptr;
9809
9810 // OuterVar = Max
9811 ExprResult MaxValue =
9812 SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: IS.MaxValue);
9813 if (!MaxValue.isUsable())
9814 return nullptr;
9815
9816 ExprResult LBMaxVal = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Assign,
9817 LHSExpr: IS.CounterVar, RHSExpr: MaxValue.get());
9818 if (!LBMaxVal.isUsable())
9819 return nullptr;
9820 // OuterVar = Max, LBVal
9821 LBMaxVal =
9822 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Comma, LHSExpr: LBMaxVal.get(), RHSExpr: LBVal);
9823 if (!LBMaxVal.isUsable())
9824 return nullptr;
9825 // (OuterVar = Max, LBVal)
9826 LBMaxVal = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: LBMaxVal.get());
9827 if (!LBMaxVal.isUsable())
9828 return nullptr;
9829
9830 Expr *LBMin =
9831 tryBuildCapture(SemaRef, Capture: LBMinVal.get(), Captures, Name: ".lb_min").get();
9832 Expr *LBMax =
9833 tryBuildCapture(SemaRef, Capture: LBMaxVal.get(), Captures, Name: ".lb_max").get();
9834 if (!LBMin || !LBMax)
9835 return nullptr;
9836 // LB(MinVal) < LB(MaxVal)
9837 ExprResult MinLessMaxRes =
9838 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_LT, LHSExpr: LBMin, RHSExpr: LBMax);
9839 if (!MinLessMaxRes.isUsable())
9840 return nullptr;
9841 Expr *MinLessMax =
9842 tryBuildCapture(SemaRef, Capture: MinLessMaxRes.get(), Captures, Name: ".min_less_max")
9843 .get();
9844 if (!MinLessMax)
9845 return nullptr;
9846 if (*TestIsLessOp) {
9847 // LB(MinVal) < LB(MaxVal) ? LB(MinVal) : LB(MaxVal) - min(LB(MinVal),
9848 // LB(MaxVal))
9849 ExprResult MinLB = SemaRef.ActOnConditionalOp(QuestionLoc: DefaultLoc, ColonLoc: DefaultLoc,
9850 CondExpr: MinLessMax, LHSExpr: LBMin, RHSExpr: LBMax);
9851 if (!MinLB.isUsable())
9852 return nullptr;
9853 LBVal = MinLB.get();
9854 } else {
9855 // LB(MinVal) < LB(MaxVal) ? LB(MaxVal) : LB(MinVal) - max(LB(MinVal),
9856 // LB(MaxVal))
9857 ExprResult MaxLB = SemaRef.ActOnConditionalOp(QuestionLoc: DefaultLoc, ColonLoc: DefaultLoc,
9858 CondExpr: MinLessMax, LHSExpr: LBMax, RHSExpr: LBMin);
9859 if (!MaxLB.isUsable())
9860 return nullptr;
9861 LBVal = MaxLB.get();
9862 }
9863 // OuterVar = LB
9864 LBMinVal =
9865 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Assign, LHSExpr: IS.CounterVar, RHSExpr: LBVal);
9866 if (!LBMinVal.isUsable())
9867 return nullptr;
9868 LBVal = LBMinVal.get();
9869 }
9870 // UB = TestIsLessOp.getValue() ? max(UB(MinVal), UB(MaxVal)) :
9871 // min(UB(MinVal), UB(MaxVal))
9872 if (CondDependOnLC) {
9873 const LoopIterationSpace &IS = ResultIterSpaces[*CondDependOnLC - 1];
9874 if (!IS.MinValue || !IS.MaxValue)
9875 return nullptr;
9876 // OuterVar = Min
9877 ExprResult MinValue =
9878 SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: IS.MinValue);
9879 if (!MinValue.isUsable())
9880 return nullptr;
9881
9882 ExprResult UBMinVal = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Assign,
9883 LHSExpr: IS.CounterVar, RHSExpr: MinValue.get());
9884 if (!UBMinVal.isUsable())
9885 return nullptr;
9886 // OuterVar = Min, UBVal
9887 UBMinVal =
9888 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Comma, LHSExpr: UBMinVal.get(), RHSExpr: UBVal);
9889 if (!UBMinVal.isUsable())
9890 return nullptr;
9891 // (OuterVar = Min, UBVal)
9892 UBMinVal = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: UBMinVal.get());
9893 if (!UBMinVal.isUsable())
9894 return nullptr;
9895
9896 // OuterVar = Max
9897 ExprResult MaxValue =
9898 SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: IS.MaxValue);
9899 if (!MaxValue.isUsable())
9900 return nullptr;
9901
9902 ExprResult UBMaxVal = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Assign,
9903 LHSExpr: IS.CounterVar, RHSExpr: MaxValue.get());
9904 if (!UBMaxVal.isUsable())
9905 return nullptr;
9906 // OuterVar = Max, UBVal
9907 UBMaxVal =
9908 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Comma, LHSExpr: UBMaxVal.get(), RHSExpr: UBVal);
9909 if (!UBMaxVal.isUsable())
9910 return nullptr;
9911 // (OuterVar = Max, UBVal)
9912 UBMaxVal = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: UBMaxVal.get());
9913 if (!UBMaxVal.isUsable())
9914 return nullptr;
9915
9916 Expr *UBMin =
9917 tryBuildCapture(SemaRef, Capture: UBMinVal.get(), Captures, Name: ".ub_min").get();
9918 Expr *UBMax =
9919 tryBuildCapture(SemaRef, Capture: UBMaxVal.get(), Captures, Name: ".ub_max").get();
9920 if (!UBMin || !UBMax)
9921 return nullptr;
9922 // UB(MinVal) > UB(MaxVal)
9923 ExprResult MinGreaterMaxRes =
9924 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_GT, LHSExpr: UBMin, RHSExpr: UBMax);
9925 if (!MinGreaterMaxRes.isUsable())
9926 return nullptr;
9927 Expr *MinGreaterMax = tryBuildCapture(SemaRef, Capture: MinGreaterMaxRes.get(),
9928 Captures, Name: ".min_greater_max")
9929 .get();
9930 if (!MinGreaterMax)
9931 return nullptr;
9932 if (*TestIsLessOp) {
9933 // UB(MinVal) > UB(MaxVal) ? UB(MinVal) : UB(MaxVal) - max(UB(MinVal),
9934 // UB(MaxVal))
9935 ExprResult MaxUB = SemaRef.ActOnConditionalOp(
9936 QuestionLoc: DefaultLoc, ColonLoc: DefaultLoc, CondExpr: MinGreaterMax, LHSExpr: UBMin, RHSExpr: UBMax);
9937 if (!MaxUB.isUsable())
9938 return nullptr;
9939 UBVal = MaxUB.get();
9940 } else {
9941 // UB(MinVal) > UB(MaxVal) ? UB(MaxVal) : UB(MinVal) - min(UB(MinVal),
9942 // UB(MaxVal))
9943 ExprResult MinUB = SemaRef.ActOnConditionalOp(
9944 QuestionLoc: DefaultLoc, ColonLoc: DefaultLoc, CondExpr: MinGreaterMax, LHSExpr: UBMax, RHSExpr: UBMin);
9945 if (!MinUB.isUsable())
9946 return nullptr;
9947 UBVal = MinUB.get();
9948 }
9949 }
9950 Expr *UBExpr = *TestIsLessOp ? UBVal : LBVal;
9951 Expr *LBExpr = *TestIsLessOp ? LBVal : UBVal;
9952 Expr *Upper = tryBuildCapture(SemaRef, Capture: UBExpr, Captures, Name: ".upper").get();
9953 Expr *Lower = tryBuildCapture(SemaRef, Capture: LBExpr, Captures, Name: ".lower").get();
9954 if (!Upper || !Lower)
9955 return nullptr;
9956
9957 ExprResult Diff = calculateNumIters(
9958 SemaRef, S, DefaultLoc, Lower, Upper, Step, LCTy: VarType, TestIsStrictOp,
9959 /*RoundToStep=*/true, Captures, InitDependOnLC, CondDependOnLC);
9960 if (!Diff.isUsable())
9961 return nullptr;
9962
9963 // OpenMP runtime requires 32-bit or 64-bit loop variables.
9964 QualType Type = Diff.get()->getType();
9965 ASTContext &C = SemaRef.Context;
9966 bool UseVarType = VarType->hasIntegerRepresentation() &&
9967 C.getTypeSize(T: Type) > C.getTypeSize(T: VarType);
9968 if (!Type->isIntegerType() || UseVarType) {
9969 unsigned NewSize =
9970 UseVarType ? C.getTypeSize(T: VarType) : C.getTypeSize(T: Type);
9971 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
9972 : Type->hasSignedIntegerRepresentation();
9973 Type = getIntTypeForBitwidthOrBitInt(C, Bits: NewSize, Signed: IsSigned);
9974 if (!SemaRef.Context.hasSameType(T1: Diff.get()->getType(), T2: Type)) {
9975 Diff = SemaRef.PerformImplicitConversion(From: Diff.get(), ToType: Type,
9976 Action: AssignmentAction::Converting,
9977 /*AllowExplicit=*/true);
9978 if (!Diff.isUsable())
9979 return nullptr;
9980 }
9981 }
9982 if (LimitedType) {
9983 unsigned NewSize = (C.getTypeSize(T: Type) > 32) ? 64 : 32;
9984 if (NewSize != C.getTypeSize(T: Type)) {
9985 if (NewSize < C.getTypeSize(T: Type)) {
9986 assert(NewSize == 64 && "incorrect loop var size");
9987 SemaRef.Diag(Loc: DefaultLoc, DiagID: diag::warn_omp_loop_64_bit_var)
9988 << InitSrcRange << ConditionSrcRange;
9989 }
9990 QualType NewType = C.getIntTypeForBitwidth(
9991 DestWidth: NewSize, Signed: Type->hasSignedIntegerRepresentation() ||
9992 C.getTypeSize(T: Type) < NewSize);
9993 if (!SemaRef.Context.hasSameType(T1: Diff.get()->getType(), T2: NewType)) {
9994 Diff = SemaRef.PerformImplicitConversion(From: Diff.get(), ToType: NewType,
9995 Action: AssignmentAction::Converting,
9996 /*AllowExplicit=*/true);
9997 if (!Diff.isUsable())
9998 return nullptr;
9999 }
10000 }
10001 }
10002
10003 return Diff.get();
10004}
10005
10006std::pair<Expr *, Expr *> OpenMPIterationSpaceChecker::buildMinMaxValues(
10007 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
10008 // Do not build for iterators, they cannot be used in non-rectangular loop
10009 // nests.
10010 if (LCDecl->getType()->isRecordType())
10011 return std::make_pair(x: nullptr, y: nullptr);
10012 // If we subtract, the min is in the condition, otherwise the min is in the
10013 // init value.
10014 Expr *MinExpr = nullptr;
10015 Expr *MaxExpr = nullptr;
10016 Expr *LBExpr = *TestIsLessOp ? LB : UB;
10017 Expr *UBExpr = *TestIsLessOp ? UB : LB;
10018 bool LBNonRect =
10019 *TestIsLessOp ? InitDependOnLC.has_value() : CondDependOnLC.has_value();
10020 bool UBNonRect =
10021 *TestIsLessOp ? CondDependOnLC.has_value() : InitDependOnLC.has_value();
10022 Expr *Lower =
10023 LBNonRect ? LBExpr : tryBuildCapture(SemaRef, Capture: LBExpr, Captures).get();
10024 Expr *Upper =
10025 UBNonRect ? UBExpr : tryBuildCapture(SemaRef, Capture: UBExpr, Captures).get();
10026 if (!Upper || !Lower)
10027 return std::make_pair(x: nullptr, y: nullptr);
10028
10029 if (*TestIsLessOp)
10030 MinExpr = Lower;
10031 else
10032 MaxExpr = Upper;
10033
10034 // Build minimum/maximum value based on number of iterations.
10035 QualType VarType = LCDecl->getType().getNonReferenceType();
10036
10037 ExprResult Diff = calculateNumIters(
10038 SemaRef, S, DefaultLoc, Lower, Upper, Step, LCTy: VarType, TestIsStrictOp,
10039 /*RoundToStep=*/false, Captures, InitDependOnLC, CondDependOnLC);
10040
10041 if (!Diff.isUsable())
10042 return std::make_pair(x: nullptr, y: nullptr);
10043
10044 // ((Upper - Lower [- 1]) / Step) * Step
10045 // Parentheses (for dumping/debugging purposes only).
10046 Diff = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Diff.get());
10047 if (!Diff.isUsable())
10048 return std::make_pair(x: nullptr, y: nullptr);
10049
10050 ExprResult NewStep = tryBuildCapture(SemaRef, Capture: Step, Captures, Name: ".new_step");
10051 if (!NewStep.isUsable())
10052 return std::make_pair(x: nullptr, y: nullptr);
10053 Diff = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Mul, LHSExpr: Diff.get(), RHSExpr: NewStep.get());
10054 if (!Diff.isUsable())
10055 return std::make_pair(x: nullptr, y: nullptr);
10056
10057 // Parentheses (for dumping/debugging purposes only).
10058 Diff = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Diff.get());
10059 if (!Diff.isUsable())
10060 return std::make_pair(x: nullptr, y: nullptr);
10061
10062 // Convert to the ptrdiff_t, if original type is pointer.
10063 if (VarType->isAnyPointerType() &&
10064 !SemaRef.Context.hasSameType(
10065 T1: Diff.get()->getType(),
10066 T2: SemaRef.Context.getUnsignedPointerDiffType())) {
10067 Diff = SemaRef.PerformImplicitConversion(
10068 From: Diff.get(), ToType: SemaRef.Context.getUnsignedPointerDiffType(),
10069 Action: AssignmentAction::Converting, /*AllowExplicit=*/true);
10070 }
10071 if (!Diff.isUsable())
10072 return std::make_pair(x: nullptr, y: nullptr);
10073
10074 if (*TestIsLessOp) {
10075 // MinExpr = Lower;
10076 // MaxExpr = Lower + (((Upper - Lower [- 1]) / Step) * Step)
10077 Diff = SemaRef.BuildBinOp(
10078 S, OpLoc: DefaultLoc, Opc: BO_Add,
10079 LHSExpr: SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Lower).get(),
10080 RHSExpr: Diff.get());
10081 if (!Diff.isUsable())
10082 return std::make_pair(x: nullptr, y: nullptr);
10083 } else {
10084 // MaxExpr = Upper;
10085 // MinExpr = Upper - (((Upper - Lower [- 1]) / Step) * Step)
10086 Diff = SemaRef.BuildBinOp(
10087 S, OpLoc: DefaultLoc, Opc: BO_Sub,
10088 LHSExpr: SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Upper).get(),
10089 RHSExpr: Diff.get());
10090 if (!Diff.isUsable())
10091 return std::make_pair(x: nullptr, y: nullptr);
10092 }
10093
10094 // Convert to the original type.
10095 if (SemaRef.Context.hasSameType(T1: Diff.get()->getType(), T2: VarType))
10096 Diff = SemaRef.PerformImplicitConversion(From: Diff.get(), ToType: VarType,
10097 Action: AssignmentAction::Converting,
10098 /*AllowExplicit=*/true);
10099 if (!Diff.isUsable())
10100 return std::make_pair(x: nullptr, y: nullptr);
10101
10102 Sema::TentativeAnalysisScope Trap(SemaRef);
10103 Diff = SemaRef.ActOnFinishFullExpr(Expr: Diff.get(), /*DiscardedValue=*/false);
10104 if (!Diff.isUsable())
10105 return std::make_pair(x: nullptr, y: nullptr);
10106
10107 if (*TestIsLessOp)
10108 MaxExpr = Diff.get();
10109 else
10110 MinExpr = Diff.get();
10111
10112 return std::make_pair(x&: MinExpr, y&: MaxExpr);
10113}
10114
10115Expr *OpenMPIterationSpaceChecker::buildFinalCondition(Scope *S) const {
10116 if (InitDependOnLC || CondDependOnLC)
10117 return Condition;
10118 return nullptr;
10119}
10120
10121Expr *OpenMPIterationSpaceChecker::buildPreCond(
10122 Scope *S, Expr *Cond,
10123 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
10124 // Do not build a precondition when the condition/initialization is dependent
10125 // to prevent pessimistic early loop exit.
10126 // TODO: this can be improved by calculating min/max values but not sure that
10127 // it will be very effective.
10128 if (CondDependOnLC || InitDependOnLC)
10129 return SemaRef
10130 .PerformImplicitConversion(
10131 From: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get(),
10132 ToType: SemaRef.Context.BoolTy, /*Action=*/AssignmentAction::Casting,
10133 /*AllowExplicit=*/true)
10134 .get();
10135
10136 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
10137 Sema::TentativeAnalysisScope Trap(SemaRef);
10138
10139 ExprResult NewLB = tryBuildCapture(SemaRef, Capture: LB, Captures);
10140 ExprResult NewUB = tryBuildCapture(SemaRef, Capture: UB, Captures);
10141 if (!NewLB.isUsable() || !NewUB.isUsable())
10142 return nullptr;
10143
10144 ExprResult CondExpr =
10145 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc,
10146 Opc: *TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
10147 : (TestIsStrictOp ? BO_GT : BO_GE),
10148 LHSExpr: NewLB.get(), RHSExpr: NewUB.get());
10149 if (CondExpr.isUsable()) {
10150 if (!SemaRef.Context.hasSameUnqualifiedType(T1: CondExpr.get()->getType(),
10151 T2: SemaRef.Context.BoolTy))
10152 CondExpr = SemaRef.PerformImplicitConversion(
10153 From: CondExpr.get(), ToType: SemaRef.Context.BoolTy,
10154 /*Action=*/AssignmentAction::Casting,
10155 /*AllowExplicit=*/true);
10156 }
10157
10158 // Otherwise use original loop condition and evaluate it in runtime.
10159 return CondExpr.isUsable() ? CondExpr.get() : Cond;
10160}
10161
10162/// Build reference expression to the counter be used for codegen.
10163DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
10164 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
10165 DSAStackTy &DSA) const {
10166 auto *VD = dyn_cast<VarDecl>(Val: LCDecl);
10167 if (!VD) {
10168 VD = SemaRef.OpenMP().isOpenMPCapturedDecl(D: LCDecl);
10169 DeclRefExpr *Ref = buildDeclRefExpr(
10170 S&: SemaRef, D: VD, Ty: VD->getType().getNonReferenceType(), Loc: DefaultLoc);
10171 const DSAStackTy::DSAVarData Data =
10172 DSA.getTopDSA(D: LCDecl, /*FromParent=*/false);
10173 // If the loop control decl is explicitly marked as private, do not mark it
10174 // as captured again.
10175 if (!isOpenMPPrivate(Kind: Data.CKind) || !Data.RefExpr)
10176 Captures.insert(KV: std::make_pair(x: LCRef, y&: Ref));
10177 return Ref;
10178 }
10179 return cast<DeclRefExpr>(Val: LCRef);
10180}
10181
10182Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
10183 if (LCDecl && !LCDecl->isInvalidDecl()) {
10184 QualType Type = LCDecl->getType().getNonReferenceType();
10185 VarDecl *PrivateVar = buildVarDecl(
10186 SemaRef, Loc: DefaultLoc, Type, Name: LCDecl->getName(),
10187 Attrs: LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
10188 OrigRef: isa<VarDecl>(Val: LCDecl)
10189 ? buildDeclRefExpr(S&: SemaRef, D: cast<VarDecl>(Val: LCDecl), Ty: Type, Loc: DefaultLoc)
10190 : nullptr);
10191 if (PrivateVar->isInvalidDecl())
10192 return nullptr;
10193 return buildDeclRefExpr(S&: SemaRef, D: PrivateVar, Ty: Type, Loc: DefaultLoc);
10194 }
10195 return nullptr;
10196}
10197
10198/// Build initialization of the counter to be used for codegen.
10199Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
10200
10201/// Build step of the counter be used for codegen.
10202Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
10203
10204Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
10205 Scope *S, Expr *Counter,
10206 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
10207 Expr *Inc, OverloadedOperatorKind OOK) {
10208 Expr *Cnt = SemaRef.DefaultLvalueConversion(E: Counter).get();
10209 if (!Cnt)
10210 return nullptr;
10211 if (Inc) {
10212 assert((OOK == OO_Plus || OOK == OO_Minus) &&
10213 "Expected only + or - operations for depend clauses.");
10214 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
10215 Cnt = SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: BOK, LHSExpr: Cnt, RHSExpr: Inc).get();
10216 if (!Cnt)
10217 return nullptr;
10218 }
10219 QualType VarType = LCDecl->getType().getNonReferenceType();
10220 if (!VarType->isIntegerType() && !VarType->isPointerType() &&
10221 !SemaRef.getLangOpts().CPlusPlus)
10222 return nullptr;
10223 // Upper - Lower
10224 Expr *Upper =
10225 *TestIsLessOp ? Cnt : tryBuildCapture(SemaRef, Capture: LB, Captures).get();
10226 Expr *Lower =
10227 *TestIsLessOp ? tryBuildCapture(SemaRef, Capture: LB, Captures).get() : Cnt;
10228 if (!Upper || !Lower)
10229 return nullptr;
10230
10231 ExprResult Diff =
10232 calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, Step, LCTy: VarType,
10233 /*TestIsStrictOp=*/false, /*RoundToStep=*/false,
10234 Captures, InitDependOnLC, CondDependOnLC);
10235 if (!Diff.isUsable())
10236 return nullptr;
10237
10238 return Diff.get();
10239}
10240} // namespace
10241
10242void SemaOpenMP::ActOnOpenMPLoopInitialization(SourceLocation ForLoc,
10243 Stmt *Init) {
10244 assert(getLangOpts().OpenMP && "OpenMP is not active.");
10245 assert(Init && "Expected loop in canonical form.");
10246 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
10247 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
10248 if (AssociatedLoops == 0 || !isOpenMPLoopDirective(DKind))
10249 return;
10250
10251 DSAStack->loopStart();
10252 llvm::SmallPtrSet<const Decl *, 1> EmptyDeclSet;
10253 OpenMPIterationSpaceChecker ISC(SemaRef, /*SupportsNonRectangular=*/true,
10254 *DSAStack, ForLoc, EmptyDeclSet,
10255 EmptyDeclSet);
10256 if (!ISC.checkAndSetInit(S: Init, /*EmitDiags=*/false)) {
10257 if (ValueDecl *D = ISC.getLoopDecl()) {
10258 auto *VD = dyn_cast<VarDecl>(Val: D);
10259 DeclRefExpr *PrivateRef = nullptr;
10260 if (!VD) {
10261 if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
10262 VD = Private;
10263 } else {
10264 PrivateRef = buildCapture(S&: SemaRef, D, CaptureExpr: ISC.getLoopDeclRefExpr(),
10265 /*WithInit=*/false);
10266 VD = cast<VarDecl>(Val: PrivateRef->getDecl());
10267 }
10268 }
10269 DSAStack->addLoopControlVariable(D, Capture: VD);
10270 const Decl *LD = DSAStack->getPossiblyLoopCounter();
10271 if (LD != D->getCanonicalDecl()) {
10272 DSAStack->resetPossibleLoopCounter();
10273 if (auto *Var = dyn_cast_or_null<VarDecl>(Val: LD))
10274 SemaRef.MarkDeclarationsReferencedInExpr(E: buildDeclRefExpr(
10275 S&: SemaRef, D: const_cast<VarDecl *>(Var),
10276 Ty: Var->getType().getNonLValueExprType(Context: getASTContext()), Loc: ForLoc,
10277 /*RefersToCapture=*/true));
10278 }
10279 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables
10280 // Referenced in a Construct, C/C++]. The loop iteration variable in the
10281 // associated for-loop of a simd construct with just one associated
10282 // for-loop may be listed in a linear clause with a constant-linear-step
10283 // that is the increment of the associated for-loop. The loop iteration
10284 // variable(s) in the associated for-loop(s) of a for or parallel for
10285 // construct may be listed in a private or lastprivate clause.
10286 DSAStackTy::DSAVarData DVar =
10287 DSAStack->getTopDSA(D, /*FromParent=*/false);
10288 // If LoopVarRefExpr is nullptr it means the corresponding loop variable
10289 // is declared in the loop and it is predetermined as a private.
10290 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
10291 OpenMPClauseKind PredeterminedCKind =
10292 isOpenMPSimdDirective(DKind)
10293 ? (DSAStack->hasMutipleLoops() ? OMPC_lastprivate : OMPC_linear)
10294 : OMPC_private;
10295 auto IsOpenMPTaskloopDirective = [](OpenMPDirectiveKind DK) {
10296 return getLeafConstructsOrSelf(D: DK).back() == OMPD_taskloop;
10297 };
10298 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
10299 DVar.CKind != PredeterminedCKind && DVar.RefExpr &&
10300 (getLangOpts().OpenMP <= 45 ||
10301 (DVar.CKind != OMPC_lastprivate && DVar.CKind != OMPC_private))) ||
10302 ((isOpenMPWorksharingDirective(DKind) ||
10303 IsOpenMPTaskloopDirective(DKind) ||
10304 isOpenMPDistributeDirective(DKind)) &&
10305 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
10306 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
10307 (DVar.CKind != OMPC_private || DVar.RefExpr)) {
10308 llvm::omp::Version OMPVersion = getLangOpts().getOpenMPVersion();
10309 Diag(Loc: Init->getBeginLoc(), DiagID: diag::err_omp_loop_var_dsa)
10310 << getOpenMPClauseNameForDiag(C: DVar.CKind)
10311 << getOpenMPDirectiveName(D: DKind, V: OMPVersion)
10312 << getOpenMPClauseNameForDiag(C: PredeterminedCKind);
10313 if (DVar.RefExpr == nullptr)
10314 DVar.CKind = PredeterminedCKind;
10315 reportOriginalDsa(SemaRef, DSAStack, D, DVar, /*IsLoopIterVar=*/true);
10316 } else if (LoopDeclRefExpr) {
10317 // Make the loop iteration variable private (for worksharing
10318 // constructs), linear (for simd directives with the only one
10319 // associated loop) or lastprivate (for simd directives with several
10320 // collapsed or ordered loops).
10321 if (DVar.CKind == OMPC_unknown)
10322 DSAStack->addDSA(D, E: LoopDeclRefExpr, A: PredeterminedCKind, PrivateCopy: PrivateRef);
10323 }
10324 }
10325 }
10326 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
10327}
10328
10329namespace {
10330// Utility for OpenMP doacross clause kind
10331class OMPDoacrossKind {
10332public:
10333 bool isSource(const OMPDoacrossClause *C) {
10334 return C->getDependenceType() == OMPC_DOACROSS_source ||
10335 C->getDependenceType() == OMPC_DOACROSS_source_omp_cur_iteration;
10336 }
10337 bool isSink(const OMPDoacrossClause *C) {
10338 return C->getDependenceType() == OMPC_DOACROSS_sink;
10339 }
10340 bool isSinkIter(const OMPDoacrossClause *C) {
10341 return C->getDependenceType() == OMPC_DOACROSS_sink_omp_cur_iteration;
10342 }
10343};
10344} // namespace
10345/// Called on a for stmt to check and extract its iteration space
10346/// for further processing (such as collapsing).
10347static bool checkOpenMPIterationSpace(
10348 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
10349 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
10350 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
10351 Expr *OrderedLoopCountExpr,
10352 SemaOpenMP::VarsWithInheritedDSAType &VarsWithImplicitDSA,
10353 llvm::MutableArrayRef<LoopIterationSpace> ResultIterSpaces,
10354 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
10355 const llvm::SmallPtrSetImpl<const Decl *> &CollapsedLoopVarDecls,
10356 llvm::SmallPtrSetImpl<const Decl *> &CollapsedLoopInductionVars) {
10357 bool SupportsNonRectangular = !isOpenMPLoopTransformationDirective(DKind);
10358
10359 // See the tile reinterpretation design note in ActOnOpenMPTileDirective.
10360 // If the loop carries the hint, analyze its rectangular form instead.
10361 //
10362 // Only directives that emit the per-iteration body guard (i.e. those going
10363 // through EmitOMPLoopBody's finals-conditions handling) may reinterpret the
10364 // loop; a loop transformation such as an enclosing 'tile' has nowhere to put
10365 // the overshoot guard, so for those the stored min-bounded form is analyzed
10366 // exactly as it is on a build without this hint.
10367 // OpenMP [2.9.1, Canonical Loop Form]
10368 // for (init-expr; test-expr; incr-expr) structured-block
10369 // for (range-decl: range-expr) structured-block
10370 if (auto *CanonLoop = dyn_cast_or_null<OMPCanonicalLoop>(Val: S))
10371 S = CanonLoop->getLoopStmt();
10372 const OMPInvariantPredicateBoundAttr *IntraTileHint =
10373 OMPLoopBasedDirective::getIntraTileHint(S);
10374 if (IntraTileHint)
10375 S = OMPLoopBasedDirective::ignoreIntraTileHint(S);
10376 Expr *TileRectCond = nullptr;
10377 Expr *TileBodyPredicate = nullptr;
10378 Expr *TileTripCount = nullptr;
10379 if (IntraTileHint && !isOpenMPLoopTransformationDirective(DKind)) {
10380 TileRectCond = IntraTileHint->getRectCond();
10381 TileBodyPredicate = IntraTileHint->getPredicate();
10382 TileTripCount = IntraTileHint->getTileSize();
10383 }
10384 auto *For = dyn_cast_or_null<ForStmt>(Val: S);
10385 auto *CXXFor = dyn_cast_or_null<CXXForRangeStmt>(Val: S);
10386 // Ranged for is supported only in OpenMP 5.0.
10387 if (!For && (SemaRef.LangOpts.OpenMP <= 45 || !CXXFor)) {
10388 llvm::omp::Version OMPVersion = SemaRef.getLangOpts().getOpenMPVersion();
10389 SemaRef.Diag(Loc: S->getBeginLoc(), DiagID: diag::err_omp_not_for)
10390 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
10391 << getOpenMPDirectiveName(D: DKind, V: OMPVersion) << TotalNestedLoopCount
10392 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
10393 if (TotalNestedLoopCount > 1) {
10394 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
10395 SemaRef.Diag(Loc: DSA.getConstructLoc(),
10396 DiagID: diag::note_omp_collapse_ordered_expr)
10397 << 2 << CollapseLoopCountExpr->getSourceRange()
10398 << OrderedLoopCountExpr->getSourceRange();
10399 else if (CollapseLoopCountExpr)
10400 SemaRef.Diag(Loc: CollapseLoopCountExpr->getExprLoc(),
10401 DiagID: diag::note_omp_collapse_ordered_expr)
10402 << 0 << CollapseLoopCountExpr->getSourceRange();
10403 else if (OrderedLoopCountExpr)
10404 SemaRef.Diag(Loc: OrderedLoopCountExpr->getExprLoc(),
10405 DiagID: diag::note_omp_collapse_ordered_expr)
10406 << 1 << OrderedLoopCountExpr->getSourceRange();
10407 }
10408 return true;
10409 }
10410 assert(((For && For->getBody()) || (CXXFor && CXXFor->getBody())) &&
10411 "No loop body.");
10412 // Postpone analysis in dependent contexts for ranged for loops.
10413 if (CXXFor && SemaRef.CurContext->isDependentContext())
10414 return false;
10415
10416 OpenMPIterationSpaceChecker ISC(SemaRef, SupportsNonRectangular, DSA,
10417 For ? For->getForLoc() : CXXFor->getForLoc(),
10418 CollapsedLoopVarDecls,
10419 CollapsedLoopInductionVars);
10420
10421 // Check init.
10422 Stmt *Init = For ? For->getInit() : CXXFor->getBeginStmt();
10423 if (ISC.checkAndSetInit(S: Init))
10424 return true;
10425
10426 bool HasErrors = false;
10427
10428 // Condition used for iteration-space analysis: the rectangular hint when
10429 // reinterpreting an intra-tile loop, otherwise the loop's own condition.
10430 Expr *EffectiveCond =
10431 TileRectCond ? TileRectCond : (For ? For->getCond() : CXXFor->getCond());
10432
10433 // Check loop variable's type.
10434 if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
10435 // OpenMP [2.6, Canonical Loop Form]
10436 // Var is one of the following:
10437 // A variable of signed or unsigned integer type.
10438 // For C++, a variable of a random access iterator type.
10439 // For C, a variable of a pointer type.
10440 QualType VarType = LCDecl->getType().getNonReferenceType();
10441 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
10442 !VarType->isPointerType() &&
10443 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
10444 SemaRef.Diag(Loc: Init->getBeginLoc(), DiagID: diag::err_omp_loop_variable_type)
10445 << SemaRef.getLangOpts().CPlusPlus;
10446 HasErrors = true;
10447 }
10448
10449 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
10450 // a Construct
10451 // The loop iteration variable(s) in the associated for-loop(s) of a for or
10452 // parallel for construct is (are) private.
10453 // The loop iteration variable in the associated for-loop of a simd
10454 // construct with just one associated for-loop is linear with a
10455 // constant-linear-step that is the increment of the associated for-loop.
10456 // Exclude loop var from the list of variables with implicitly defined data
10457 // sharing attributes.
10458 VarsWithImplicitDSA.erase(Val: LCDecl);
10459
10460 assert((isOpenMPLoopDirective(DKind) ||
10461 isOpenMPCanonicalLoopSequenceTransformationDirective(DKind)) &&
10462 "DSA for non-loop vars");
10463
10464 // Check test-expr.
10465 HasErrors |= ISC.checkAndSetCond(S: EffectiveCond);
10466
10467 // Check incr-expr.
10468 HasErrors |= ISC.checkAndSetInc(S: For ? For->getInc() : CXXFor->getInc());
10469 }
10470
10471 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
10472 return HasErrors;
10473
10474 // Build the loop's iteration space representation.
10475 //
10476 // A reinterpreted intra-tile loop takes all three of these from the hint
10477 // instead of deriving them: the precondition is trivially true (the floor
10478 // loop's own precondition already covers an empty iteration space), the trip
10479 // count is the constant tile size, and any overshoot on the remainder tile
10480 // is handled by the body guard.
10481 if (TileRectCond) {
10482 ResultIterSpaces[CurrentNestedLoopCount].PreCond =
10483 SemaRef
10484 .PerformImplicitConversion(
10485 From: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get(),
10486 ToType: SemaRef.Context.BoolTy, Action: AssignmentAction::Casting,
10487 /*AllowExplicit=*/true)
10488 .get();
10489 ResultIterSpaces[CurrentNestedLoopCount].NumIterations = TileTripCount;
10490 } else {
10491 ResultIterSpaces[CurrentNestedLoopCount].PreCond =
10492 ISC.buildPreCond(S: DSA.getCurScope(), Cond: EffectiveCond, Captures);
10493 ResultIterSpaces[CurrentNestedLoopCount].NumIterations =
10494 ISC.buildNumIterations(S: DSA.getCurScope(), ResultIterSpaces,
10495 LimitedType: (isOpenMPWorksharingDirective(DKind) ||
10496 isOpenMPGenericLoopDirective(DKind) ||
10497 isOpenMPTaskLoopDirective(DKind) ||
10498 isOpenMPDistributeDirective(DKind) ||
10499 isOpenMPLoopTransformationDirective(DKind)),
10500 Captures);
10501 }
10502 ResultIterSpaces[CurrentNestedLoopCount].CounterVar =
10503 ISC.buildCounterVar(Captures, DSA);
10504 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar =
10505 ISC.buildPrivateCounterVar();
10506 ResultIterSpaces[CurrentNestedLoopCount].CounterInit = ISC.buildCounterInit();
10507 ResultIterSpaces[CurrentNestedLoopCount].CounterStep = ISC.buildCounterStep();
10508 ResultIterSpaces[CurrentNestedLoopCount].InitSrcRange = ISC.getInitSrcRange();
10509 ResultIterSpaces[CurrentNestedLoopCount].CondSrcRange =
10510 ISC.getConditionSrcRange();
10511 ResultIterSpaces[CurrentNestedLoopCount].IncSrcRange =
10512 ISC.getIncrementSrcRange();
10513 ResultIterSpaces[CurrentNestedLoopCount].Subtract = ISC.shouldSubtractStep();
10514 ResultIterSpaces[CurrentNestedLoopCount].IsStrictCompare =
10515 ISC.isStrictTestOp();
10516 std::tie(args&: ResultIterSpaces[CurrentNestedLoopCount].MinValue,
10517 args&: ResultIterSpaces[CurrentNestedLoopCount].MaxValue) =
10518 ISC.buildMinMaxValues(S: DSA.getCurScope(), Captures);
10519 if (TileRectCond) {
10520 // Floor is not a registered loop-control variable, so this helper is null.
10521 // Use the overshoot guard only (null when N % T == 0).
10522 assert(!ISC.buildFinalCondition(DSA.getCurScope()) &&
10523 "intra-tile floor is not a registered loop-control variable");
10524 ResultIterSpaces[CurrentNestedLoopCount].FinalCondition = TileBodyPredicate;
10525
10526 // Re-read .floor.iv each outer trip. Match it to an outer collapsed
10527 // counter.
10528 bool FoundFloor = false;
10529 if (Expr *InitExpr = ResultIterSpaces[CurrentNestedLoopCount].CounterInit) {
10530 if (const auto *LBRef =
10531 dyn_cast<DeclRefExpr>(Val: InitExpr->IgnoreParenImpCasts())) {
10532 const Decl *FloorDecl = LBRef->getDecl()->getCanonicalDecl();
10533 for (unsigned K = 0; K < CurrentNestedLoopCount; ++K) {
10534 const auto *CV =
10535 dyn_cast_or_null<DeclRefExpr>(Val: ResultIterSpaces[K].CounterVar);
10536 if (CV && CV->getDecl()->getCanonicalDecl() == FloorDecl) {
10537 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularLB = true;
10538 ResultIterSpaces[CurrentNestedLoopCount].LoopDependentIdx = K + 1;
10539 FoundFloor = true;
10540 break;
10541 }
10542 }
10543 }
10544 }
10545 if (!FoundFloor) {
10546 // The floor this intra-tile loop starts from is not one of the
10547 // collapsed counters: it is assigned in the body of an enclosing
10548 // transformed loop, so the collapsed nest would read a stale value
10549 // instead of recomputing it per iteration. Diagnose to avoid silently
10550 // producing the wrong iteration space.
10551 SemaRef.Diag(Loc: ISC.getInitSrcRange().getBegin(),
10552 DiagID: diag::err_omp_collapse_stacked_tile)
10553 << /*Collapse=*/0;
10554 return true;
10555 }
10556 } else {
10557 ResultIterSpaces[CurrentNestedLoopCount].FinalCondition =
10558 ISC.buildFinalCondition(S: DSA.getCurScope());
10559 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularLB =
10560 ISC.doesInitDependOnLC();
10561 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularUB =
10562 ISC.doesCondDependOnLC();
10563 ResultIterSpaces[CurrentNestedLoopCount].LoopDependentIdx =
10564 ISC.getLoopDependentIdx();
10565 }
10566
10567 HasErrors |=
10568 (ResultIterSpaces[CurrentNestedLoopCount].PreCond == nullptr ||
10569 ResultIterSpaces[CurrentNestedLoopCount].NumIterations == nullptr ||
10570 ResultIterSpaces[CurrentNestedLoopCount].CounterVar == nullptr ||
10571 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar == nullptr ||
10572 ResultIterSpaces[CurrentNestedLoopCount].CounterInit == nullptr ||
10573 ResultIterSpaces[CurrentNestedLoopCount].CounterStep == nullptr);
10574 if (!HasErrors && DSA.isOrderedRegion()) {
10575 if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
10576 if (CurrentNestedLoopCount <
10577 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
10578 DSA.getOrderedRegionParam().second->setLoopNumIterations(
10579 NumLoop: CurrentNestedLoopCount,
10580 NumIterations: ResultIterSpaces[CurrentNestedLoopCount].NumIterations);
10581 DSA.getOrderedRegionParam().second->setLoopCounter(
10582 NumLoop: CurrentNestedLoopCount,
10583 Counter: ResultIterSpaces[CurrentNestedLoopCount].CounterVar);
10584 }
10585 }
10586 for (auto &Pair : DSA.getDoacrossDependClauses()) {
10587 auto *DependC = dyn_cast<OMPDependClause>(Val: Pair.first);
10588 auto *DoacrossC = dyn_cast<OMPDoacrossClause>(Val: Pair.first);
10589 unsigned NumLoops =
10590 DependC ? DependC->getNumLoops() : DoacrossC->getNumLoops();
10591 if (CurrentNestedLoopCount >= NumLoops) {
10592 // Erroneous case - clause has some problems.
10593 continue;
10594 }
10595 if (DependC && DependC->getDependencyKind() == OMPC_DEPEND_sink &&
10596 Pair.second.size() <= CurrentNestedLoopCount) {
10597 // Erroneous case - clause has some problems.
10598 DependC->setLoopData(NumLoop: CurrentNestedLoopCount, Cnt: nullptr);
10599 continue;
10600 }
10601 OMPDoacrossKind ODK;
10602 if (DoacrossC && ODK.isSink(C: DoacrossC) &&
10603 Pair.second.size() <= CurrentNestedLoopCount) {
10604 // Erroneous case - clause has some problems.
10605 DoacrossC->setLoopData(NumLoop: CurrentNestedLoopCount, Cnt: nullptr);
10606 continue;
10607 }
10608 Expr *CntValue;
10609 SourceLocation DepLoc =
10610 DependC ? DependC->getDependencyLoc() : DoacrossC->getDependenceLoc();
10611 if ((DependC && DependC->getDependencyKind() == OMPC_DEPEND_source) ||
10612 (DoacrossC && ODK.isSource(C: DoacrossC)))
10613 CntValue = ISC.buildOrderedLoopData(
10614 S: DSA.getCurScope(),
10615 Counter: ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
10616 Loc: DepLoc);
10617 else if (DoacrossC && ODK.isSinkIter(C: DoacrossC)) {
10618 Expr *Cnt = SemaRef
10619 .DefaultLvalueConversion(
10620 E: ResultIterSpaces[CurrentNestedLoopCount].CounterVar)
10621 .get();
10622 if (!Cnt)
10623 continue;
10624 // build CounterVar - 1
10625 Expr *Inc =
10626 SemaRef.ActOnIntegerConstant(Loc: DoacrossC->getColonLoc(), /*Val=*/1)
10627 .get();
10628 CntValue = ISC.buildOrderedLoopData(
10629 S: DSA.getCurScope(),
10630 Counter: ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
10631 Loc: DepLoc, Inc, OOK: clang::OO_Minus);
10632 } else
10633 CntValue = ISC.buildOrderedLoopData(
10634 S: DSA.getCurScope(),
10635 Counter: ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
10636 Loc: DepLoc, Inc: Pair.second[CurrentNestedLoopCount].first,
10637 OOK: Pair.second[CurrentNestedLoopCount].second);
10638 if (DependC)
10639 DependC->setLoopData(NumLoop: CurrentNestedLoopCount, Cnt: CntValue);
10640 else
10641 DoacrossC->setLoopData(NumLoop: CurrentNestedLoopCount, Cnt: CntValue);
10642 }
10643 }
10644 // Record the loop induction variable for nested loop reuse checking.
10645 if (CurrentNestedLoopCount < NestedLoopCount && !HasErrors) {
10646 if (const ValueDecl *LCDecl = ISC.getLoopDecl())
10647 CollapsedLoopInductionVars.insert(Ptr: LCDecl->getCanonicalDecl());
10648 }
10649 return HasErrors;
10650}
10651
10652/// Build 'VarRef = Start.
10653static ExprResult
10654buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
10655 ExprResult Start, bool IsNonRectangularLB,
10656 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
10657 // Build 'VarRef = Start.
10658 ExprResult NewStart = IsNonRectangularLB
10659 ? Start.get()
10660 : tryBuildCapture(SemaRef, Capture: Start.get(), Captures);
10661 if (!NewStart.isUsable())
10662 return ExprError();
10663 if (!SemaRef.Context.hasSameType(T1: NewStart.get()->getType(),
10664 T2: VarRef.get()->getType())) {
10665 NewStart = SemaRef.PerformImplicitConversion(
10666 From: NewStart.get(), ToType: VarRef.get()->getType(), Action: AssignmentAction::Converting,
10667 /*AllowExplicit=*/true);
10668 if (!NewStart.isUsable())
10669 return ExprError();
10670 }
10671
10672 ExprResult Init =
10673 SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: BO_Assign, LHSExpr: VarRef.get(), RHSExpr: NewStart.get());
10674 return Init;
10675}
10676
10677/// Build 'VarRef = Start + Iter * Step'.
10678static ExprResult buildCounterUpdate(
10679 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
10680 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
10681 bool IsNonRectangularLB,
10682 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
10683 // Add parentheses (for debugging purposes only).
10684 Iter = SemaRef.ActOnParenExpr(L: Loc, R: Loc, E: Iter.get());
10685 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
10686 !Step.isUsable())
10687 return ExprError();
10688
10689 ExprResult NewStep = Step;
10690 if (Captures)
10691 NewStep = tryBuildCapture(SemaRef, Capture: Step.get(), Captures&: *Captures);
10692 if (NewStep.isInvalid())
10693 return ExprError();
10694 ExprResult Update =
10695 SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: BO_Mul, LHSExpr: Iter.get(), RHSExpr: NewStep.get());
10696 if (!Update.isUsable())
10697 return ExprError();
10698
10699 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
10700 // 'VarRef = Start (+|-) Iter * Step'.
10701 if (!Start.isUsable())
10702 return ExprError();
10703 ExprResult NewStart = SemaRef.ActOnParenExpr(L: Loc, R: Loc, E: Start.get());
10704 if (!NewStart.isUsable())
10705 return ExprError();
10706 if (Captures && !IsNonRectangularLB)
10707 NewStart = tryBuildCapture(SemaRef, Capture: Start.get(), Captures&: *Captures);
10708 if (NewStart.isInvalid())
10709 return ExprError();
10710
10711 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
10712 ExprResult SavedUpdate = Update;
10713 ExprResult UpdateVal;
10714 if (VarRef.get()->getType()->isOverloadableType() ||
10715 NewStart.get()->getType()->isOverloadableType() ||
10716 Update.get()->getType()->isOverloadableType()) {
10717 Sema::TentativeAnalysisScope Trap(SemaRef);
10718
10719 Update =
10720 SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: BO_Assign, LHSExpr: VarRef.get(), RHSExpr: NewStart.get());
10721 if (Update.isUsable()) {
10722 UpdateVal =
10723 SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: Subtract ? BO_SubAssign : BO_AddAssign,
10724 LHSExpr: VarRef.get(), RHSExpr: SavedUpdate.get());
10725 if (UpdateVal.isUsable()) {
10726 Update = SemaRef.CreateBuiltinBinOp(OpLoc: Loc, Opc: BO_Comma, LHSExpr: Update.get(),
10727 RHSExpr: UpdateVal.get());
10728 }
10729 }
10730 }
10731
10732 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
10733 if (!Update.isUsable() || !UpdateVal.isUsable()) {
10734 Update = SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: Subtract ? BO_Sub : BO_Add,
10735 LHSExpr: NewStart.get(), RHSExpr: SavedUpdate.get());
10736 if (!Update.isUsable())
10737 return ExprError();
10738
10739 if (!SemaRef.Context.hasSameType(T1: Update.get()->getType(),
10740 T2: VarRef.get()->getType())) {
10741 Update = SemaRef.PerformImplicitConversion(
10742 From: Update.get(), ToType: VarRef.get()->getType(), Action: AssignmentAction::Converting,
10743 /*AllowExplicit=*/true);
10744 if (!Update.isUsable())
10745 return ExprError();
10746 }
10747
10748 Update = SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: BO_Assign, LHSExpr: VarRef.get(), RHSExpr: Update.get());
10749 }
10750 return Update;
10751}
10752
10753/// Convert integer expression \a E to make it have at least \a Bits
10754/// bits.
10755static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
10756 if (E == nullptr)
10757 return ExprError();
10758 ASTContext &C = SemaRef.Context;
10759 QualType OldType = E->getType();
10760 unsigned HasBits = C.getTypeSize(T: OldType);
10761 if (HasBits >= Bits)
10762 return ExprResult(E);
10763 // OK to convert to signed, because new type has more bits than old.
10764 QualType NewType = C.getIntTypeForBitwidth(DestWidth: Bits, /*Signed=*/true);
10765 return SemaRef.PerformImplicitConversion(
10766 From: E, ToType: NewType, Action: AssignmentAction::Converting, /*AllowExplicit=*/true);
10767}
10768
10769/// Check if the given expression \a E is a constant integer that fits
10770/// into \a Bits bits.
10771static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
10772 if (E == nullptr)
10773 return false;
10774 if (std::optional<llvm::APSInt> Result =
10775 E->getIntegerConstantExpr(Ctx: SemaRef.Context))
10776 return Signed ? Result->isSignedIntN(N: Bits) : Result->isIntN(N: Bits);
10777 return false;
10778}
10779
10780/// Build preinits statement for the given declarations.
10781static Stmt *buildPreInits(ASTContext &Context,
10782 MutableArrayRef<Decl *> PreInits) {
10783 if (!PreInits.empty()) {
10784 return new (Context) DeclStmt(
10785 DeclGroupRef::Create(C&: Context, Decls: PreInits.begin(), NumDecls: PreInits.size()),
10786 SourceLocation(), SourceLocation());
10787 }
10788 return nullptr;
10789}
10790
10791/// Append the \p Item or the content of a CompoundStmt to the list \p
10792/// TargetList.
10793///
10794/// A CompoundStmt is used as container in case multiple statements need to be
10795/// stored in lieu of using an explicit list. Flattening is necessary because
10796/// contained DeclStmts need to be visible after the execution of the list. Used
10797/// for OpenMP pre-init declarations/statements.
10798static void appendFlattenedStmtList(SmallVectorImpl<Stmt *> &TargetList,
10799 Stmt *Item) {
10800 // nullptr represents an empty list.
10801 if (!Item)
10802 return;
10803
10804 if (auto *CS = dyn_cast<CompoundStmt>(Val: Item))
10805 llvm::append_range(C&: TargetList, R: CS->body());
10806 else
10807 TargetList.push_back(Elt: Item);
10808}
10809
10810/// Build preinits statement for the given declarations.
10811static Stmt *
10812buildPreInits(ASTContext &Context,
10813 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
10814 if (!Captures.empty()) {
10815 SmallVector<Decl *, 16> PreInits;
10816 for (const auto &Pair : Captures)
10817 PreInits.push_back(Elt: Pair.second->getDecl());
10818 return buildPreInits(Context, PreInits);
10819 }
10820 return nullptr;
10821}
10822
10823/// Build pre-init statement for the given statements.
10824static Stmt *buildPreInits(ASTContext &Context, ArrayRef<Stmt *> PreInits) {
10825 if (PreInits.empty())
10826 return nullptr;
10827
10828 SmallVector<Stmt *> Stmts;
10829 for (Stmt *S : PreInits)
10830 appendFlattenedStmtList(TargetList&: Stmts, Item: S);
10831 return CompoundStmt::Create(C: Context, Stmts: PreInits, FPFeatures: FPOptionsOverride(), LB: {}, RB: {});
10832}
10833
10834/// Build postupdate expression for the given list of postupdates expressions.
10835static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
10836 Expr *PostUpdate = nullptr;
10837 if (!PostUpdates.empty()) {
10838 for (Expr *E : PostUpdates) {
10839 Expr *ConvE = S.BuildCStyleCastExpr(
10840 LParenLoc: E->getExprLoc(),
10841 Ty: S.Context.getTrivialTypeSourceInfo(T: S.Context.VoidTy),
10842 RParenLoc: E->getExprLoc(), Op: E)
10843 .get();
10844 PostUpdate = PostUpdate
10845 ? S.CreateBuiltinBinOp(OpLoc: ConvE->getExprLoc(), Opc: BO_Comma,
10846 LHSExpr: PostUpdate, RHSExpr: ConvE)
10847 .get()
10848 : ConvE;
10849 }
10850 }
10851 return PostUpdate;
10852}
10853
10854/// Look for variables declared in the body parts of a for-loop nest. Used
10855/// for verifying loop nest structure before performing a loop collapse
10856/// operation.
10857class ForVarDeclFinder : public DynamicRecursiveASTVisitor {
10858 int NestingDepth = 0;
10859 llvm::SmallPtrSetImpl<const Decl *> &VarDecls;
10860
10861public:
10862 explicit ForVarDeclFinder(llvm::SmallPtrSetImpl<const Decl *> &VD)
10863 : VarDecls(VD) {}
10864
10865 bool VisitForStmt(ForStmt *F) override {
10866 ++NestingDepth;
10867 TraverseStmt(S: F->getBody());
10868 --NestingDepth;
10869 return false;
10870 }
10871
10872 bool VisitCXXForRangeStmt(CXXForRangeStmt *RF) override {
10873 ++NestingDepth;
10874 TraverseStmt(S: RF->getBody());
10875 --NestingDepth;
10876 return false;
10877 }
10878
10879 bool VisitVarDecl(VarDecl *D) override {
10880 Decl *C = D->getCanonicalDecl();
10881 if (NestingDepth > 0)
10882 VarDecls.insert(Ptr: C);
10883 return true;
10884 }
10885};
10886
10887/// Called on a for stmt to check itself and nested loops (if any).
10888/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
10889/// number of collapsed loops otherwise.
10890static unsigned
10891checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
10892 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
10893 DSAStackTy &DSA,
10894 SemaOpenMP::VarsWithInheritedDSAType &VarsWithImplicitDSA,
10895 OMPLoopBasedDirective::HelperExprs &Built) {
10896 // If either of the loop expressions exist and contain errors, we bail out
10897 // early because diagnostics have already been emitted and we can't reliably
10898 // check more about the loop.
10899 if ((CollapseLoopCountExpr && CollapseLoopCountExpr->containsErrors()) ||
10900 (OrderedLoopCountExpr && OrderedLoopCountExpr->containsErrors()))
10901 return 0;
10902
10903 unsigned NestedLoopCount = 1;
10904 bool SupportsNonPerfectlyNested = (SemaRef.LangOpts.OpenMP >= 50) &&
10905 !isOpenMPLoopTransformationDirective(DKind);
10906 llvm::SmallPtrSet<const Decl *, 4> CollapsedLoopVarDecls;
10907 llvm::SmallPtrSet<const Decl *, 4> CollapsedLoopInductionVars;
10908
10909 if (CollapseLoopCountExpr) {
10910 // Found 'collapse' clause - calculate collapse number.
10911 Expr::EvalResult Result;
10912 if (!CollapseLoopCountExpr->isInstantiationDependent() &&
10913 CollapseLoopCountExpr->EvaluateAsInt(Result, Ctx: SemaRef.getASTContext())) {
10914 NestedLoopCount = Result.Val.getInt().getLimitedValue();
10915
10916 ForVarDeclFinder FVDF{CollapsedLoopVarDecls};
10917 FVDF.TraverseStmt(S: AStmt);
10918 } else {
10919 Built.clear(/*Size=*/1);
10920 return 1;
10921 }
10922 }
10923 unsigned OrderedLoopCount = 1;
10924 if (OrderedLoopCountExpr) {
10925 // Found 'ordered' clause - calculate collapse number.
10926 Expr::EvalResult EVResult;
10927 if (!OrderedLoopCountExpr->isInstantiationDependent() &&
10928 OrderedLoopCountExpr->EvaluateAsInt(Result&: EVResult,
10929 Ctx: SemaRef.getASTContext())) {
10930 llvm::APSInt Result = EVResult.Val.getInt();
10931 if (Result.getLimitedValue() < NestedLoopCount) {
10932 SemaRef.Diag(Loc: OrderedLoopCountExpr->getExprLoc(),
10933 DiagID: diag::err_omp_wrong_ordered_loop_count)
10934 << OrderedLoopCountExpr->getSourceRange();
10935 SemaRef.Diag(Loc: CollapseLoopCountExpr->getExprLoc(),
10936 DiagID: diag::note_collapse_loop_count)
10937 << CollapseLoopCountExpr->getSourceRange();
10938 }
10939 OrderedLoopCount = Result.getLimitedValue();
10940 } else {
10941 Built.clear(/*Size=*/1);
10942 return 1;
10943 }
10944 }
10945 // This is helper routine for loop directives (e.g., 'for', 'simd',
10946 // 'for simd', etc.).
10947 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
10948 unsigned NumLoops = std::max(a: OrderedLoopCount, b: NestedLoopCount);
10949 SmallVector<LoopIterationSpace, 4> IterSpaces(NumLoops);
10950 if (!OMPLoopBasedDirective::doForAllLoops(
10951 CurStmt: AStmt->IgnoreContainers(
10952 IgnoreCaptured: !isOpenMPCanonicalLoopNestTransformationDirective(DKind)),
10953 TryImperfectlyNestedLoops: SupportsNonPerfectlyNested, NumLoops,
10954 Callback: [DKind, &SemaRef, &DSA, NumLoops, NestedLoopCount,
10955 CollapseLoopCountExpr, OrderedLoopCountExpr, &VarsWithImplicitDSA,
10956 &IterSpaces, &Captures, &CollapsedLoopVarDecls,
10957 &CollapsedLoopInductionVars](unsigned Cnt, Stmt *Loop,
10958 Stmt *HintWrapper) {
10959 Stmt *CurStmt = HintWrapper ? HintWrapper : Loop;
10960 if (checkOpenMPIterationSpace(
10961 DKind, S: CurStmt, SemaRef, DSA, CurrentNestedLoopCount: Cnt, NestedLoopCount,
10962 TotalNestedLoopCount: NumLoops, CollapseLoopCountExpr, OrderedLoopCountExpr,
10963 VarsWithImplicitDSA, ResultIterSpaces: IterSpaces, Captures,
10964 CollapsedLoopVarDecls, CollapsedLoopInductionVars))
10965 return true;
10966 if (Cnt > 0 && Cnt >= NestedLoopCount &&
10967 IterSpaces[Cnt].CounterVar) {
10968 // Handle initialization of captured loop iterator variables.
10969 auto *DRE = cast<DeclRefExpr>(Val: IterSpaces[Cnt].CounterVar);
10970 if (isa<OMPCapturedExprDecl>(Val: DRE->getDecl())) {
10971 Captures[DRE] = DRE;
10972 }
10973 }
10974 return false;
10975 },
10976 OnTransformationCallback: [&SemaRef, &Captures](OMPLoopTransformationDirective *Transform) {
10977 Stmt *DependentPreInits = Transform->getPreInits();
10978 if (!DependentPreInits)
10979 return;
10980
10981 // Search for pre-init declared variables that need to be captured
10982 // to be referenceable inside the directive.
10983 SmallVector<Stmt *> Constituents;
10984 appendFlattenedStmtList(TargetList&: Constituents, Item: DependentPreInits);
10985 for (Stmt *S : Constituents) {
10986 if (auto *DC = dyn_cast<DeclStmt>(Val: S)) {
10987 for (Decl *C : DC->decls()) {
10988 auto *D = cast<VarDecl>(Val: C);
10989 DeclRefExpr *Ref = buildDeclRefExpr(
10990 S&: SemaRef, D, Ty: D->getType().getNonReferenceType(),
10991 Loc: cast<OMPExecutableDirective>(Val: Transform->getDirective())
10992 ->getBeginLoc());
10993 Captures[Ref] = Ref;
10994 }
10995 }
10996 }
10997 }))
10998 return 0;
10999
11000 Built.clear(/*size=*/Size: NestedLoopCount);
11001
11002 if (SemaRef.CurContext->isDependentContext())
11003 return NestedLoopCount;
11004
11005 // An example of what is generated for the following code:
11006 //
11007 // #pragma omp simd collapse(2) ordered(2)
11008 // for (i = 0; i < NI; ++i)
11009 // for (k = 0; k < NK; ++k)
11010 // for (j = J0; j < NJ; j+=2) {
11011 // <loop body>
11012 // }
11013 //
11014 // We generate the code below.
11015 // Note: the loop body may be outlined in CodeGen.
11016 // Note: some counters may be C++ classes, operator- is used to find number of
11017 // iterations and operator+= to calculate counter value.
11018 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
11019 // or i64 is currently supported).
11020 //
11021 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
11022 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
11023 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
11024 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
11025 // // similar updates for vars in clauses (e.g. 'linear')
11026 // <loop body (using local i and j)>
11027 // }
11028 // i = NI; // assign final values of counters
11029 // j = NJ;
11030 //
11031
11032 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
11033 // the iteration counts of the collapsed for loops.
11034 // Precondition tests if there is at least one iteration (all conditions are
11035 // true).
11036 auto PreCond = ExprResult(IterSpaces[0].PreCond);
11037 ASTContext &C = SemaRef.Context;
11038 unsigned FirstCountBits =
11039 C.getTypeSize(T: IterSpaces[0].NumIterations->getType());
11040 bool AllCountsNeedLessThan32Bits = FirstCountBits < 32;
11041
11042 Scope *CurScope = DSA.getCurScope();
11043 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
11044 if (PreCond.isUsable()) {
11045 PreCond =
11046 SemaRef.BuildBinOp(S: CurScope, OpLoc: PreCond.get()->getExprLoc(), Opc: BO_LAnd,
11047 LHSExpr: PreCond.get(), RHSExpr: IterSpaces[Cnt].PreCond);
11048 }
11049 Expr *N = IterSpaces[Cnt].NumIterations;
11050 AllCountsNeedLessThan32Bits &= C.getTypeSize(T: N->getType()) < 32;
11051 }
11052
11053 auto BuildLastIteration = [&](unsigned Bits) -> ExprResult {
11054 ExprResult Result;
11055 for (unsigned Cnt : llvm::seq<unsigned>(Size: NestedLoopCount)) {
11056 Expr *N = IterSpaces[Cnt].NumIterations;
11057 ExprResult Count = widenIterationCount(
11058 Bits,
11059 E: SemaRef
11060 .PerformImplicitConversion(From: N->IgnoreImpCasts(), ToType: N->getType(),
11061 Action: AssignmentAction::Converting,
11062 /*AllowExplicit=*/true)
11063 .get(),
11064 SemaRef);
11065 if (!Count.isUsable())
11066 return ExprError();
11067 if (Cnt == 0)
11068 Result = Count;
11069 else
11070 Result = SemaRef.BuildBinOp(S: CurScope, OpLoc: N->getExprLoc(), Opc: BO_Mul,
11071 LHSExpr: Result.get(), RHSExpr: Count.get());
11072 if (!Result.isUsable())
11073 return ExprError();
11074 }
11075 return Result;
11076 };
11077
11078 // Build the 32-bit tree immediately only when it is always selected.
11079 // Otherwise, build the 64-bit tree first and build the 32-bit tree only when
11080 // the constant product may fit.
11081 ExprResult LastIteration;
11082 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse ||
11083 AllCountsNeedLessThan32Bits ||
11084 (NestedLoopCount == 1 && FirstCountBits == 32)) {
11085 LastIteration = BuildLastIteration(/*Bits=*/32);
11086 } else {
11087 ExprResult LastIteration64 = BuildLastIteration(/*Bits=*/64);
11088 if (!LastIteration64.isUsable())
11089 return NestedLoopCount;
11090 LastIteration = LastIteration64;
11091 if (LastIteration64.get()->isIntegerConstantExpr(Ctx: C)) {
11092 ExprResult LastIteration32 = BuildLastIteration(/*Bits=*/32);
11093 if (LastIteration32.isUsable() &&
11094 C.getTypeSize(T: LastIteration32.get()->getType()) == 32 &&
11095 fitsInto(
11096 /*Bits=*/32,
11097 Signed: LastIteration32.get()
11098 ->getType()
11099 ->hasSignedIntegerRepresentation(),
11100 E: LastIteration64.get(), SemaRef))
11101 LastIteration = LastIteration32;
11102 }
11103 }
11104 if (!LastIteration.isUsable())
11105 return NestedLoopCount;
11106
11107 QualType VType = LastIteration.get()->getType();
11108 QualType RealVType = VType;
11109 QualType StrideVType = VType;
11110 if (isOpenMPTaskLoopDirective(DKind)) {
11111 VType =
11112 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
11113 StrideVType =
11114 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
11115 }
11116
11117 // Save the number of iterations.
11118 ExprResult NumIterations = LastIteration;
11119 {
11120 LastIteration = SemaRef.BuildBinOp(
11121 S: CurScope, OpLoc: LastIteration.get()->getExprLoc(), Opc: BO_Sub,
11122 LHSExpr: LastIteration.get(),
11123 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get());
11124 if (!LastIteration.isUsable())
11125 return 0;
11126 }
11127
11128 // Calculate the last iteration number beforehand instead of doing this on
11129 // each iteration. Do not do this if the number of iterations may be kfold-ed.
11130 bool IsConstant = LastIteration.get()->isIntegerConstantExpr(Ctx: SemaRef.Context);
11131 ExprResult CalcLastIteration;
11132 if (!IsConstant) {
11133 ExprResult SaveRef =
11134 tryBuildCapture(SemaRef, Capture: LastIteration.get(), Captures);
11135 LastIteration = SaveRef;
11136
11137 // Prepare SaveRef + 1.
11138 NumIterations = SemaRef.BuildBinOp(
11139 S: CurScope, OpLoc: SaveRef.get()->getExprLoc(), Opc: BO_Add, LHSExpr: SaveRef.get(),
11140 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get());
11141 if (!NumIterations.isUsable())
11142 return 0;
11143 }
11144
11145 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
11146
11147 // Build variables passed into runtime, necessary for worksharing directives.
11148 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
11149 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
11150 isOpenMPDistributeDirective(DKind) ||
11151 isOpenMPGenericLoopDirective(DKind) ||
11152 isOpenMPLoopTransformationDirective(DKind)) {
11153 // Lower bound variable, initialized with zero.
11154 VarDecl *LBDecl = buildVarDecl(SemaRef, Loc: InitLoc, Type: VType, Name: ".omp.lb");
11155 LB = buildDeclRefExpr(S&: SemaRef, D: LBDecl, Ty: VType, Loc: InitLoc);
11156 SemaRef.AddInitializerToDecl(dcl: LBDecl,
11157 init: SemaRef.ActOnIntegerConstant(Loc: InitLoc, Val: 0).get(),
11158 /*DirectInit=*/false);
11159
11160 // Upper bound variable, initialized with last iteration number.
11161 VarDecl *UBDecl = buildVarDecl(SemaRef, Loc: InitLoc, Type: VType, Name: ".omp.ub");
11162 UB = buildDeclRefExpr(S&: SemaRef, D: UBDecl, Ty: VType, Loc: InitLoc);
11163 SemaRef.AddInitializerToDecl(dcl: UBDecl, init: LastIteration.get(),
11164 /*DirectInit=*/false);
11165
11166 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
11167 // This will be used to implement clause 'lastprivate'.
11168 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(DestWidth: 32, Signed: true);
11169 VarDecl *ILDecl = buildVarDecl(SemaRef, Loc: InitLoc, Type: Int32Ty, Name: ".omp.is_last");
11170 IL = buildDeclRefExpr(S&: SemaRef, D: ILDecl, Ty: Int32Ty, Loc: InitLoc);
11171 SemaRef.AddInitializerToDecl(dcl: ILDecl,
11172 init: SemaRef.ActOnIntegerConstant(Loc: InitLoc, Val: 0).get(),
11173 /*DirectInit=*/false);
11174
11175 // Stride variable returned by runtime (we initialize it to 1 by default).
11176 VarDecl *STDecl =
11177 buildVarDecl(SemaRef, Loc: InitLoc, Type: StrideVType, Name: ".omp.stride");
11178 ST = buildDeclRefExpr(S&: SemaRef, D: STDecl, Ty: StrideVType, Loc: InitLoc);
11179 SemaRef.AddInitializerToDecl(dcl: STDecl,
11180 init: SemaRef.ActOnIntegerConstant(Loc: InitLoc, Val: 1).get(),
11181 /*DirectInit=*/false);
11182
11183 // Build expression: UB = min(UB, LastIteration)
11184 // It is necessary for CodeGen of directives with static scheduling.
11185 ExprResult IsUBGreater = SemaRef.BuildBinOp(S: CurScope, OpLoc: InitLoc, Opc: BO_GT,
11186 LHSExpr: UB.get(), RHSExpr: LastIteration.get());
11187 ExprResult CondOp = SemaRef.ActOnConditionalOp(
11188 QuestionLoc: LastIteration.get()->getExprLoc(), ColonLoc: InitLoc, CondExpr: IsUBGreater.get(),
11189 LHSExpr: LastIteration.get(), RHSExpr: UB.get());
11190 EUB = SemaRef.BuildBinOp(S: CurScope, OpLoc: InitLoc, Opc: BO_Assign, LHSExpr: UB.get(),
11191 RHSExpr: CondOp.get());
11192 EUB = SemaRef.ActOnFinishFullExpr(Expr: EUB.get(), /*DiscardedValue=*/false);
11193
11194 // If we have a combined directive that combines 'distribute', 'for' or
11195 // 'simd' we need to be able to access the bounds of the schedule of the
11196 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
11197 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
11198 if (isOpenMPLoopBoundSharingDirective(Kind: DKind)) {
11199 // Lower bound variable, initialized with zero.
11200 VarDecl *CombLBDecl =
11201 buildVarDecl(SemaRef, Loc: InitLoc, Type: VType, Name: ".omp.comb.lb");
11202 CombLB = buildDeclRefExpr(S&: SemaRef, D: CombLBDecl, Ty: VType, Loc: InitLoc);
11203 SemaRef.AddInitializerToDecl(
11204 dcl: CombLBDecl, init: SemaRef.ActOnIntegerConstant(Loc: InitLoc, Val: 0).get(),
11205 /*DirectInit=*/false);
11206
11207 // Upper bound variable, initialized with last iteration number.
11208 VarDecl *CombUBDecl =
11209 buildVarDecl(SemaRef, Loc: InitLoc, Type: VType, Name: ".omp.comb.ub");
11210 CombUB = buildDeclRefExpr(S&: SemaRef, D: CombUBDecl, Ty: VType, Loc: InitLoc);
11211 SemaRef.AddInitializerToDecl(dcl: CombUBDecl, init: LastIteration.get(),
11212 /*DirectInit=*/false);
11213
11214 ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
11215 S: CurScope, OpLoc: InitLoc, Opc: BO_GT, LHSExpr: CombUB.get(), RHSExpr: LastIteration.get());
11216 ExprResult CombCondOp =
11217 SemaRef.ActOnConditionalOp(QuestionLoc: InitLoc, ColonLoc: InitLoc, CondExpr: CombIsUBGreater.get(),
11218 LHSExpr: LastIteration.get(), RHSExpr: CombUB.get());
11219 CombEUB = SemaRef.BuildBinOp(S: CurScope, OpLoc: InitLoc, Opc: BO_Assign, LHSExpr: CombUB.get(),
11220 RHSExpr: CombCondOp.get());
11221 CombEUB =
11222 SemaRef.ActOnFinishFullExpr(Expr: CombEUB.get(), /*DiscardedValue=*/false);
11223
11224 const CapturedDecl *CD = cast<CapturedStmt>(Val: AStmt)->getCapturedDecl();
11225 // We expect to have at least 2 more parameters than the 'parallel'
11226 // directive does - the lower and upper bounds of the previous schedule.
11227 assert(CD->getNumParams() >= 4 &&
11228 "Unexpected number of parameters in loop combined directive");
11229
11230 // Set the proper type for the bounds given what we learned from the
11231 // enclosed loops.
11232 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/i: 2);
11233 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/i: 3);
11234
11235 // Previous lower and upper bounds are obtained from the region
11236 // parameters.
11237 PrevLB =
11238 buildDeclRefExpr(S&: SemaRef, D: PrevLBDecl, Ty: PrevLBDecl->getType(), Loc: InitLoc);
11239 PrevUB =
11240 buildDeclRefExpr(S&: SemaRef, D: PrevUBDecl, Ty: PrevUBDecl->getType(), Loc: InitLoc);
11241 }
11242 }
11243
11244 // Build the iteration variable and its initialization before loop.
11245 ExprResult IV;
11246 ExprResult Init, CombInit;
11247 {
11248 VarDecl *IVDecl = buildVarDecl(SemaRef, Loc: InitLoc, Type: RealVType, Name: ".omp.iv");
11249 IV = buildDeclRefExpr(S&: SemaRef, D: IVDecl, Ty: RealVType, Loc: InitLoc);
11250 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
11251 isOpenMPGenericLoopDirective(DKind) ||
11252 isOpenMPTaskLoopDirective(DKind) ||
11253 isOpenMPDistributeDirective(DKind) ||
11254 isOpenMPLoopTransformationDirective(DKind))
11255 ? LB.get()
11256 : SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 0).get();
11257 Init = SemaRef.BuildBinOp(S: CurScope, OpLoc: InitLoc, Opc: BO_Assign, LHSExpr: IV.get(), RHSExpr: RHS);
11258 Init = SemaRef.ActOnFinishFullExpr(Expr: Init.get(), /*DiscardedValue=*/false);
11259
11260 if (isOpenMPLoopBoundSharingDirective(Kind: DKind)) {
11261 Expr *CombRHS =
11262 (isOpenMPWorksharingDirective(DKind) ||
11263 isOpenMPGenericLoopDirective(DKind) ||
11264 isOpenMPTaskLoopDirective(DKind) ||
11265 isOpenMPDistributeDirective(DKind))
11266 ? CombLB.get()
11267 : SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 0).get();
11268 CombInit =
11269 SemaRef.BuildBinOp(S: CurScope, OpLoc: InitLoc, Opc: BO_Assign, LHSExpr: IV.get(), RHSExpr: CombRHS);
11270 CombInit =
11271 SemaRef.ActOnFinishFullExpr(Expr: CombInit.get(), /*DiscardedValue=*/false);
11272 }
11273 }
11274
11275 bool UseStrictCompare =
11276 RealVType->hasUnsignedIntegerRepresentation() &&
11277 llvm::all_of(Range&: IterSpaces, P: [](const LoopIterationSpace &LIS) {
11278 return LIS.IsStrictCompare;
11279 });
11280 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for
11281 // unsigned IV)) for worksharing loops.
11282 SourceLocation CondLoc = AStmt->getBeginLoc();
11283 Expr *BoundUB = UB.get();
11284 if (UseStrictCompare) {
11285 BoundUB =
11286 SemaRef
11287 .BuildBinOp(S: CurScope, OpLoc: CondLoc, Opc: BO_Add, LHSExpr: BoundUB,
11288 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get())
11289 .get();
11290 BoundUB =
11291 SemaRef.ActOnFinishFullExpr(Expr: BoundUB, /*DiscardedValue=*/false).get();
11292 }
11293 ExprResult Cond =
11294 (isOpenMPWorksharingDirective(DKind) ||
11295 isOpenMPGenericLoopDirective(DKind) ||
11296 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind) ||
11297 isOpenMPLoopTransformationDirective(DKind))
11298 ? SemaRef.BuildBinOp(S: CurScope, OpLoc: CondLoc,
11299 Opc: UseStrictCompare ? BO_LT : BO_LE, LHSExpr: IV.get(),
11300 RHSExpr: BoundUB)
11301 : SemaRef.BuildBinOp(S: CurScope, OpLoc: CondLoc, Opc: BO_LT, LHSExpr: IV.get(),
11302 RHSExpr: NumIterations.get());
11303 ExprResult CombDistCond;
11304 if (isOpenMPLoopBoundSharingDirective(Kind: DKind)) {
11305 CombDistCond = SemaRef.BuildBinOp(S: CurScope, OpLoc: CondLoc, Opc: BO_LT, LHSExpr: IV.get(),
11306 RHSExpr: NumIterations.get());
11307 }
11308
11309 ExprResult CombCond;
11310 if (isOpenMPLoopBoundSharingDirective(Kind: DKind)) {
11311 Expr *BoundCombUB = CombUB.get();
11312 if (UseStrictCompare) {
11313 BoundCombUB =
11314 SemaRef
11315 .BuildBinOp(
11316 S: CurScope, OpLoc: CondLoc, Opc: BO_Add, LHSExpr: BoundCombUB,
11317 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get())
11318 .get();
11319 BoundCombUB =
11320 SemaRef.ActOnFinishFullExpr(Expr: BoundCombUB, /*DiscardedValue=*/false)
11321 .get();
11322 }
11323 CombCond =
11324 SemaRef.BuildBinOp(S: CurScope, OpLoc: CondLoc, Opc: UseStrictCompare ? BO_LT : BO_LE,
11325 LHSExpr: IV.get(), RHSExpr: BoundCombUB);
11326 }
11327 // Loop increment (IV = IV + 1)
11328 SourceLocation IncLoc = AStmt->getBeginLoc();
11329 ExprResult Inc =
11330 SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Add, LHSExpr: IV.get(),
11331 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: IncLoc, Val: 1).get());
11332 if (!Inc.isUsable())
11333 return 0;
11334 Inc = SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Assign, LHSExpr: IV.get(), RHSExpr: Inc.get());
11335 Inc = SemaRef.ActOnFinishFullExpr(Expr: Inc.get(), /*DiscardedValue=*/false);
11336 if (!Inc.isUsable())
11337 return 0;
11338
11339 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
11340 // Used for directives with static scheduling.
11341 // In combined construct, add combined version that use CombLB and CombUB
11342 // base variables for the update
11343 ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
11344 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
11345 isOpenMPGenericLoopDirective(DKind) ||
11346 isOpenMPDistributeDirective(DKind) ||
11347 isOpenMPLoopTransformationDirective(DKind)) {
11348 // LB + ST
11349 NextLB = SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Add, LHSExpr: LB.get(), RHSExpr: ST.get());
11350 if (!NextLB.isUsable())
11351 return 0;
11352 // LB = LB + ST
11353 NextLB =
11354 SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Assign, LHSExpr: LB.get(), RHSExpr: NextLB.get());
11355 NextLB =
11356 SemaRef.ActOnFinishFullExpr(Expr: NextLB.get(), /*DiscardedValue=*/false);
11357 if (!NextLB.isUsable())
11358 return 0;
11359 // UB + ST
11360 NextUB = SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Add, LHSExpr: UB.get(), RHSExpr: ST.get());
11361 if (!NextUB.isUsable())
11362 return 0;
11363 // UB = UB + ST
11364 NextUB =
11365 SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Assign, LHSExpr: UB.get(), RHSExpr: NextUB.get());
11366 NextUB =
11367 SemaRef.ActOnFinishFullExpr(Expr: NextUB.get(), /*DiscardedValue=*/false);
11368 if (!NextUB.isUsable())
11369 return 0;
11370 if (isOpenMPLoopBoundSharingDirective(Kind: DKind)) {
11371 CombNextLB =
11372 SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Add, LHSExpr: CombLB.get(), RHSExpr: ST.get());
11373 if (!NextLB.isUsable())
11374 return 0;
11375 // LB = LB + ST
11376 CombNextLB = SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Assign, LHSExpr: CombLB.get(),
11377 RHSExpr: CombNextLB.get());
11378 CombNextLB = SemaRef.ActOnFinishFullExpr(Expr: CombNextLB.get(),
11379 /*DiscardedValue=*/false);
11380 if (!CombNextLB.isUsable())
11381 return 0;
11382 // UB + ST
11383 CombNextUB =
11384 SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Add, LHSExpr: CombUB.get(), RHSExpr: ST.get());
11385 if (!CombNextUB.isUsable())
11386 return 0;
11387 // UB = UB + ST
11388 CombNextUB = SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Assign, LHSExpr: CombUB.get(),
11389 RHSExpr: CombNextUB.get());
11390 CombNextUB = SemaRef.ActOnFinishFullExpr(Expr: CombNextUB.get(),
11391 /*DiscardedValue=*/false);
11392 if (!CombNextUB.isUsable())
11393 return 0;
11394 }
11395 }
11396
11397 // Create increment expression for distribute loop when combined in a same
11398 // directive with for as IV = IV + ST; ensure upper bound expression based
11399 // on PrevUB instead of NumIterations - used to implement 'for' when found
11400 // in combination with 'distribute', like in 'distribute parallel for'
11401 SourceLocation DistIncLoc = AStmt->getBeginLoc();
11402 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond;
11403 if (isOpenMPLoopBoundSharingDirective(Kind: DKind)) {
11404 DistCond = SemaRef.BuildBinOp(
11405 S: CurScope, OpLoc: CondLoc, Opc: UseStrictCompare ? BO_LT : BO_LE, LHSExpr: IV.get(), RHSExpr: BoundUB);
11406 assert(DistCond.isUsable() && "distribute cond expr was not built");
11407
11408 DistInc =
11409 SemaRef.BuildBinOp(S: CurScope, OpLoc: DistIncLoc, Opc: BO_Add, LHSExpr: IV.get(), RHSExpr: ST.get());
11410 assert(DistInc.isUsable() && "distribute inc expr was not built");
11411 DistInc = SemaRef.BuildBinOp(S: CurScope, OpLoc: DistIncLoc, Opc: BO_Assign, LHSExpr: IV.get(),
11412 RHSExpr: DistInc.get());
11413 DistInc =
11414 SemaRef.ActOnFinishFullExpr(Expr: DistInc.get(), /*DiscardedValue=*/false);
11415 assert(DistInc.isUsable() && "distribute inc expr was not built");
11416
11417 // Build expression: UB = min(UB, prevUB) for #for in composite or combined
11418 // construct
11419 ExprResult NewPrevUB = PrevUB;
11420 SourceLocation DistEUBLoc = AStmt->getBeginLoc();
11421 if (!SemaRef.Context.hasSameType(T1: UB.get()->getType(),
11422 T2: PrevUB.get()->getType())) {
11423 NewPrevUB = SemaRef.BuildCStyleCastExpr(
11424 LParenLoc: DistEUBLoc,
11425 Ty: SemaRef.Context.getTrivialTypeSourceInfo(T: UB.get()->getType()),
11426 RParenLoc: DistEUBLoc, Op: NewPrevUB.get());
11427 if (!NewPrevUB.isUsable())
11428 return 0;
11429 }
11430 ExprResult IsUBGreater = SemaRef.BuildBinOp(S: CurScope, OpLoc: DistEUBLoc, Opc: BO_GT,
11431 LHSExpr: UB.get(), RHSExpr: NewPrevUB.get());
11432 ExprResult CondOp = SemaRef.ActOnConditionalOp(
11433 QuestionLoc: DistEUBLoc, ColonLoc: DistEUBLoc, CondExpr: IsUBGreater.get(), LHSExpr: NewPrevUB.get(), RHSExpr: UB.get());
11434 PrevEUB = SemaRef.BuildBinOp(S: CurScope, OpLoc: DistIncLoc, Opc: BO_Assign, LHSExpr: UB.get(),
11435 RHSExpr: CondOp.get());
11436 PrevEUB =
11437 SemaRef.ActOnFinishFullExpr(Expr: PrevEUB.get(), /*DiscardedValue=*/false);
11438
11439 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in
11440 // parallel for is in combination with a distribute directive with
11441 // schedule(static, 1)
11442 Expr *BoundPrevUB = PrevUB.get();
11443 if (UseStrictCompare) {
11444 BoundPrevUB =
11445 SemaRef
11446 .BuildBinOp(
11447 S: CurScope, OpLoc: CondLoc, Opc: BO_Add, LHSExpr: BoundPrevUB,
11448 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get())
11449 .get();
11450 BoundPrevUB =
11451 SemaRef.ActOnFinishFullExpr(Expr: BoundPrevUB, /*DiscardedValue=*/false)
11452 .get();
11453 }
11454 ParForInDistCond =
11455 SemaRef.BuildBinOp(S: CurScope, OpLoc: CondLoc, Opc: UseStrictCompare ? BO_LT : BO_LE,
11456 LHSExpr: IV.get(), RHSExpr: BoundPrevUB);
11457 }
11458
11459 // Build updates and final values of the loop counters.
11460 bool HasErrors = false;
11461 Built.Counters.resize(N: NestedLoopCount);
11462 Built.Inits.resize(N: NestedLoopCount);
11463 Built.Updates.resize(N: NestedLoopCount);
11464 Built.Finals.resize(N: NestedLoopCount);
11465 Built.DependentCounters.resize(N: NestedLoopCount);
11466 Built.DependentInits.resize(N: NestedLoopCount);
11467 Built.FinalsConditions.resize(N: NestedLoopCount);
11468 {
11469 // We implement the following algorithm for obtaining the
11470 // original loop iteration variable values based on the
11471 // value of the collapsed loop iteration variable IV.
11472 //
11473 // Let n+1 be the number of collapsed loops in the nest.
11474 // Iteration variables (I0, I1, .... In)
11475 // Iteration counts (N0, N1, ... Nn)
11476 //
11477 // Acc = IV;
11478 //
11479 // To compute Ik for loop k, 0 <= k <= n, generate:
11480 // Prod = N(k+1) * N(k+2) * ... * Nn;
11481 // Ik = Acc / Prod;
11482 // Acc -= Ik * Prod;
11483 //
11484 ExprResult Acc = IV;
11485 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
11486 LoopIterationSpace &IS = IterSpaces[Cnt];
11487 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
11488 ExprResult Iter;
11489
11490 // Compute prod
11491 ExprResult Prod = SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get();
11492 for (unsigned int K = Cnt + 1; K < NestedLoopCount; ++K)
11493 Prod = SemaRef.BuildBinOp(S: CurScope, OpLoc: UpdLoc, Opc: BO_Mul, LHSExpr: Prod.get(),
11494 RHSExpr: IterSpaces[K].NumIterations);
11495
11496 // Iter = Acc / Prod
11497 // If there is at least one more inner loop to avoid
11498 // multiplication by 1.
11499 if (Cnt + 1 < NestedLoopCount)
11500 Iter =
11501 SemaRef.BuildBinOp(S: CurScope, OpLoc: UpdLoc, Opc: BO_Div, LHSExpr: Acc.get(), RHSExpr: Prod.get());
11502 else
11503 Iter = Acc;
11504 if (!Iter.isUsable()) {
11505 HasErrors = true;
11506 break;
11507 }
11508
11509 // Update Acc:
11510 // Acc -= Iter * Prod
11511 // Check if there is at least one more inner loop to avoid
11512 // multiplication by 1.
11513 if (Cnt + 1 < NestedLoopCount)
11514 Prod = SemaRef.BuildBinOp(S: CurScope, OpLoc: UpdLoc, Opc: BO_Mul, LHSExpr: Iter.get(),
11515 RHSExpr: Prod.get());
11516 else
11517 Prod = Iter;
11518 Acc = SemaRef.BuildBinOp(S: CurScope, OpLoc: UpdLoc, Opc: BO_Sub, LHSExpr: Acc.get(), RHSExpr: Prod.get());
11519
11520 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
11521 auto *VD = cast<VarDecl>(Val: cast<DeclRefExpr>(Val: IS.CounterVar)->getDecl());
11522 DeclRefExpr *CounterVar = buildDeclRefExpr(
11523 S&: SemaRef, D: VD, Ty: IS.CounterVar->getType(), Loc: IS.CounterVar->getExprLoc(),
11524 /*RefersToCapture=*/true);
11525 ExprResult Init =
11526 buildCounterInit(SemaRef, S: CurScope, Loc: UpdLoc, VarRef: CounterVar,
11527 Start: IS.CounterInit, IsNonRectangularLB: IS.IsNonRectangularLB, Captures);
11528 if (!Init.isUsable()) {
11529 HasErrors = true;
11530 break;
11531 }
11532 ExprResult Update = buildCounterUpdate(
11533 SemaRef, S: CurScope, Loc: UpdLoc, VarRef: CounterVar, Start: IS.CounterInit, Iter,
11534 Step: IS.CounterStep, Subtract: IS.Subtract, IsNonRectangularLB: IS.IsNonRectangularLB, Captures: &Captures);
11535 if (!Update.isUsable()) {
11536 HasErrors = true;
11537 break;
11538 }
11539
11540 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
11541 ExprResult Final =
11542 buildCounterUpdate(SemaRef, S: CurScope, Loc: UpdLoc, VarRef: CounterVar,
11543 Start: IS.CounterInit, Iter: IS.NumIterations, Step: IS.CounterStep,
11544 Subtract: IS.Subtract, IsNonRectangularLB: IS.IsNonRectangularLB, Captures: &Captures);
11545 if (!Final.isUsable()) {
11546 HasErrors = true;
11547 break;
11548 }
11549
11550 if (!Update.isUsable() || !Final.isUsable()) {
11551 HasErrors = true;
11552 break;
11553 }
11554 // Save results
11555 Built.Counters[Cnt] = IS.CounterVar;
11556 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
11557 Built.Inits[Cnt] = Init.get();
11558 Built.Updates[Cnt] = Update.get();
11559 Built.Finals[Cnt] = Final.get();
11560 Built.DependentCounters[Cnt] = nullptr;
11561 Built.DependentInits[Cnt] = nullptr;
11562 // Transfer the body-guard condition: the loop condition for
11563 // non-rectangular loops, the overshoot predicate for reinterpreted tiles,
11564 // null otherwise.
11565 Built.FinalsConditions[Cnt] = IS.FinalCondition;
11566 if (IS.IsNonRectangularLB || IS.IsNonRectangularUB) {
11567 Built.DependentCounters[Cnt] = Built.Counters[IS.LoopDependentIdx - 1];
11568 Built.DependentInits[Cnt] = Built.Inits[IS.LoopDependentIdx - 1];
11569 }
11570 }
11571 }
11572
11573 if (HasErrors)
11574 return 0;
11575
11576 // Save results
11577 Built.IterationVarRef = IV.get();
11578 Built.LastIteration = LastIteration.get();
11579 Built.NumIterations = NumIterations.get();
11580 Built.CalcLastIteration = SemaRef
11581 .ActOnFinishFullExpr(Expr: CalcLastIteration.get(),
11582 /*DiscardedValue=*/false)
11583 .get();
11584 Built.PreCond = PreCond.get();
11585 Built.PreInits = buildPreInits(Context&: C, Captures);
11586 Built.Cond = Cond.get();
11587 Built.Init = Init.get();
11588 Built.Inc = Inc.get();
11589 Built.LB = LB.get();
11590 Built.UB = UB.get();
11591 Built.IL = IL.get();
11592 Built.ST = ST.get();
11593 Built.EUB = EUB.get();
11594 Built.NLB = NextLB.get();
11595 Built.NUB = NextUB.get();
11596 Built.PrevLB = PrevLB.get();
11597 Built.PrevUB = PrevUB.get();
11598 Built.DistInc = DistInc.get();
11599 Built.PrevEUB = PrevEUB.get();
11600 Built.DistCombinedFields.LB = CombLB.get();
11601 Built.DistCombinedFields.UB = CombUB.get();
11602 Built.DistCombinedFields.EUB = CombEUB.get();
11603 Built.DistCombinedFields.Init = CombInit.get();
11604 Built.DistCombinedFields.Cond = CombCond.get();
11605 Built.DistCombinedFields.NLB = CombNextLB.get();
11606 Built.DistCombinedFields.NUB = CombNextUB.get();
11607 Built.DistCombinedFields.DistCond = CombDistCond.get();
11608 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get();
11609
11610 return NestedLoopCount;
11611}
11612
11613static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
11614 auto CollapseClauses =
11615 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
11616 if (CollapseClauses.begin() != CollapseClauses.end())
11617 return (*CollapseClauses.begin())->getNumForLoops();
11618 return nullptr;
11619}
11620
11621static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
11622 auto OrderedClauses =
11623 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
11624 if (OrderedClauses.begin() != OrderedClauses.end())
11625 return (*OrderedClauses.begin())->getNumForLoops();
11626 return nullptr;
11627}
11628
11629static bool checkSimdlenSafelenSpecified(Sema &S,
11630 const ArrayRef<OMPClause *> Clauses) {
11631 const OMPSafelenClause *Safelen = nullptr;
11632 const OMPSimdlenClause *Simdlen = nullptr;
11633
11634 for (const OMPClause *Clause : Clauses) {
11635 if (Clause->getClauseKind() == OMPC_safelen)
11636 Safelen = cast<OMPSafelenClause>(Val: Clause);
11637 else if (Clause->getClauseKind() == OMPC_simdlen)
11638 Simdlen = cast<OMPSimdlenClause>(Val: Clause);
11639 if (Safelen && Simdlen)
11640 break;
11641 }
11642
11643 if (Simdlen && Safelen) {
11644 const Expr *SimdlenLength = Simdlen->getSimdlen();
11645 const Expr *SafelenLength = Safelen->getSafelen();
11646 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
11647 SimdlenLength->isInstantiationDependent() ||
11648 SimdlenLength->containsUnexpandedParameterPack())
11649 return false;
11650 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
11651 SafelenLength->isInstantiationDependent() ||
11652 SafelenLength->containsUnexpandedParameterPack())
11653 return false;
11654 Expr::EvalResult SimdlenResult, SafelenResult;
11655 SimdlenLength->EvaluateAsInt(Result&: SimdlenResult, Ctx: S.Context);
11656 SafelenLength->EvaluateAsInt(Result&: SafelenResult, Ctx: S.Context);
11657 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt();
11658 llvm::APSInt SafelenRes = SafelenResult.Val.getInt();
11659 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
11660 // If both simdlen and safelen clauses are specified, the value of the
11661 // simdlen parameter must be less than or equal to the value of the safelen
11662 // parameter.
11663 if (llvm::APSInt::compareValues(I1: SimdlenRes, I2: SafelenRes) > 0) {
11664 S.Diag(Loc: SimdlenLength->getExprLoc(),
11665 DiagID: diag::err_omp_wrong_simdlen_safelen_values)
11666 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
11667 return true;
11668 }
11669 }
11670 return false;
11671}
11672
11673StmtResult SemaOpenMP::ActOnOpenMPSimdDirective(
11674 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11675 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11676 if (!AStmt)
11677 return StmtError();
11678
11679 CapturedStmt *CS = setBranchProtectedScope(SemaRef, DKind: OMPD_simd, AStmt);
11680
11681 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11682 OMPLoopBasedDirective::HelperExprs B;
11683 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
11684 // define the nested loops number.
11685 unsigned NestedLoopCount = checkOpenMPLoop(
11686 DKind: OMPD_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses), OrderedLoopCountExpr: getOrderedNumberExpr(Clauses),
11687 AStmt: CS, SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
11688 if (NestedLoopCount == 0)
11689 return StmtError();
11690
11691 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
11692 return StmtError();
11693
11694 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
11695 return StmtError();
11696
11697 auto *SimdDirective = OMPSimdDirective::Create(
11698 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
11699 return SimdDirective;
11700}
11701
11702StmtResult SemaOpenMP::ActOnOpenMPForDirective(
11703 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11704 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11705 if (!AStmt)
11706 return StmtError();
11707
11708 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11709 OMPLoopBasedDirective::HelperExprs B;
11710 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
11711 // define the nested loops number.
11712 unsigned NestedLoopCount = checkOpenMPLoop(
11713 DKind: OMPD_for, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses), OrderedLoopCountExpr: getOrderedNumberExpr(Clauses),
11714 AStmt, SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
11715 if (NestedLoopCount == 0)
11716 return StmtError();
11717
11718 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
11719 return StmtError();
11720
11721 auto *ForDirective = OMPForDirective::Create(
11722 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
11723 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
11724 return ForDirective;
11725}
11726
11727StmtResult SemaOpenMP::ActOnOpenMPForSimdDirective(
11728 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11729 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11730 if (!AStmt)
11731 return StmtError();
11732
11733 CapturedStmt *CS = setBranchProtectedScope(SemaRef, DKind: OMPD_for_simd, AStmt);
11734
11735 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11736 OMPLoopBasedDirective::HelperExprs B;
11737 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
11738 // define the nested loops number.
11739 unsigned NestedLoopCount =
11740 checkOpenMPLoop(DKind: OMPD_for_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
11741 OrderedLoopCountExpr: getOrderedNumberExpr(Clauses), AStmt: CS, SemaRef, DSA&: *DSAStack,
11742 VarsWithImplicitDSA, Built&: B);
11743 if (NestedLoopCount == 0)
11744 return StmtError();
11745
11746 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
11747 return StmtError();
11748
11749 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
11750 return StmtError();
11751
11752 return OMPForSimdDirective::Create(C: getASTContext(), StartLoc, EndLoc,
11753 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
11754}
11755
11756static bool checkSectionsDirective(Sema &SemaRef, OpenMPDirectiveKind DKind,
11757 Stmt *AStmt, DSAStackTy *Stack) {
11758 if (!AStmt)
11759 return true;
11760
11761 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11762 llvm::omp::Version OMPVersion = SemaRef.getLangOpts().getOpenMPVersion();
11763 auto BaseStmt = AStmt;
11764 while (auto *CS = dyn_cast_or_null<CapturedStmt>(Val: BaseStmt))
11765 BaseStmt = CS->getCapturedStmt();
11766 if (auto *C = dyn_cast_or_null<CompoundStmt>(Val: BaseStmt)) {
11767 auto S = C->children();
11768 if (S.begin() == S.end())
11769 return true;
11770 // All associated statements must be '#pragma omp section' except for
11771 // the first one.
11772 for (Stmt *SectionStmt : llvm::drop_begin(RangeOrContainer&: S)) {
11773 if (!SectionStmt || !isa<OMPSectionDirective>(Val: SectionStmt)) {
11774 if (SectionStmt)
11775 SemaRef.Diag(Loc: SectionStmt->getBeginLoc(),
11776 DiagID: diag::err_omp_sections_substmt_not_section)
11777 << getOpenMPDirectiveName(D: DKind, V: OMPVersion);
11778 return true;
11779 }
11780 cast<OMPSectionDirective>(Val: SectionStmt)
11781 ->setHasCancel(Stack->isCancelRegion());
11782 }
11783 } else {
11784 SemaRef.Diag(Loc: AStmt->getBeginLoc(), DiagID: diag::err_omp_sections_not_compound_stmt)
11785 << getOpenMPDirectiveName(D: DKind, V: OMPVersion);
11786 return true;
11787 }
11788 return false;
11789}
11790
11791StmtResult
11792SemaOpenMP::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
11793 Stmt *AStmt, SourceLocation StartLoc,
11794 SourceLocation EndLoc) {
11795 if (checkSectionsDirective(SemaRef, DKind: OMPD_sections, AStmt, DSAStack))
11796 return StmtError();
11797
11798 SemaRef.setFunctionHasBranchProtectedScope();
11799
11800 return OMPSectionsDirective::Create(
11801 C: getASTContext(), StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
11802 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
11803}
11804
11805StmtResult SemaOpenMP::ActOnOpenMPSectionDirective(Stmt *AStmt,
11806 SourceLocation StartLoc,
11807 SourceLocation EndLoc) {
11808 if (!AStmt)
11809 return StmtError();
11810
11811 SemaRef.setFunctionHasBranchProtectedScope();
11812 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
11813
11814 return OMPSectionDirective::Create(C: getASTContext(), StartLoc, EndLoc, AssociatedStmt: AStmt,
11815 DSAStack->isCancelRegion());
11816}
11817
11818static Expr *getDirectCallExpr(Expr *E) {
11819 E = E->IgnoreParenCasts()->IgnoreImplicit();
11820 // A call replaced by a declare variant keeps the original call.
11821 if (auto *POE = dyn_cast<PseudoObjectExpr>(Val: E))
11822 E = POE->getSyntacticForm()->IgnoreImpCasts();
11823 if (auto *CE = dyn_cast<CallExpr>(Val: E))
11824 if (CE->getDirectCallee())
11825 return E;
11826 return nullptr;
11827}
11828
11829StmtResult
11830SemaOpenMP::ActOnOpenMPDispatchDirective(ArrayRef<OMPClause *> Clauses,
11831 Stmt *AStmt, SourceLocation StartLoc,
11832 SourceLocation EndLoc) {
11833 if (!AStmt)
11834 return StmtError();
11835
11836 Stmt *S = cast<CapturedStmt>(Val: AStmt)->getCapturedStmt();
11837
11838 // 5.1 OpenMP
11839 // expression-stmt : an expression statement with one of the following forms:
11840 // expression = target-call ( [expression-list] );
11841 // target-call ( [expression-list] );
11842
11843 SourceLocation TargetCallLoc;
11844
11845 if (!SemaRef.CurContext->isDependentContext()) {
11846 Expr *TargetCall = nullptr;
11847
11848 auto *E = dyn_cast<Expr>(Val: S);
11849 if (!E) {
11850 Diag(Loc: S->getBeginLoc(), DiagID: diag::err_omp_dispatch_statement_call);
11851 return StmtError();
11852 }
11853
11854 E = E->IgnoreParenCasts()->IgnoreImplicit();
11855
11856 if (auto *BO = dyn_cast<BinaryOperator>(Val: E)) {
11857 if (BO->getOpcode() == BO_Assign)
11858 TargetCall = getDirectCallExpr(E: BO->getRHS());
11859 } else {
11860 if (auto *COCE = dyn_cast<CXXOperatorCallExpr>(Val: E))
11861 if (COCE->getOperator() == OO_Equal)
11862 TargetCall = getDirectCallExpr(E: COCE->getArg(Arg: 1));
11863 if (!TargetCall)
11864 TargetCall = getDirectCallExpr(E);
11865 }
11866 if (!TargetCall) {
11867 Diag(Loc: E->getBeginLoc(), DiagID: diag::err_omp_dispatch_statement_call);
11868 return StmtError();
11869 }
11870 TargetCallLoc = TargetCall->getExprLoc();
11871 }
11872
11873 SemaRef.setFunctionHasBranchProtectedScope();
11874
11875 return OMPDispatchDirective::Create(C: getASTContext(), StartLoc, EndLoc,
11876 Clauses, AssociatedStmt: AStmt, TargetCallLoc);
11877}
11878
11879static bool checkGenericLoopLastprivate(Sema &S, ArrayRef<OMPClause *> Clauses,
11880 OpenMPDirectiveKind K,
11881 DSAStackTy *Stack) {
11882 bool ErrorFound = false;
11883 for (OMPClause *C : Clauses) {
11884 if (auto *LPC = dyn_cast<OMPLastprivateClause>(Val: C)) {
11885 for (Expr *RefExpr : LPC->varlist()) {
11886 SourceLocation ELoc;
11887 SourceRange ERange;
11888 Expr *SimpleRefExpr = RefExpr;
11889 auto Res = getPrivateItem(S, RefExpr&: SimpleRefExpr, ELoc, ERange);
11890 if (ValueDecl *D = Res.first) {
11891 auto &&Info = Stack->isLoopControlVariable(D);
11892 if (!Info.first) {
11893 llvm::omp::Version OMPVersion = S.getLangOpts().getOpenMPVersion();
11894 S.Diag(Loc: ELoc, DiagID: diag::err_omp_lastprivate_loop_var_non_loop_iteration)
11895 << getOpenMPDirectiveName(D: K, V: OMPVersion);
11896 ErrorFound = true;
11897 }
11898 }
11899 }
11900 }
11901 }
11902 return ErrorFound;
11903}
11904
11905StmtResult SemaOpenMP::ActOnOpenMPGenericLoopDirective(
11906 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11907 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11908 if (!AStmt)
11909 return StmtError();
11910
11911 // OpenMP 5.1 [2.11.7, loop construct, Restrictions]
11912 // A list item may not appear in a lastprivate clause unless it is the
11913 // loop iteration variable of a loop that is associated with the construct.
11914 if (checkGenericLoopLastprivate(S&: SemaRef, Clauses, K: OMPD_loop, DSAStack))
11915 return StmtError();
11916
11917 setBranchProtectedScope(SemaRef, DKind: OMPD_loop, AStmt);
11918
11919 OMPLoopDirective::HelperExprs B;
11920 // In presence of clause 'collapse', it will define the nested loops number.
11921 unsigned NestedLoopCount = checkOpenMPLoop(
11922 DKind: OMPD_loop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses), OrderedLoopCountExpr: getOrderedNumberExpr(Clauses),
11923 AStmt, SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
11924 if (NestedLoopCount == 0)
11925 return StmtError();
11926
11927 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
11928 "omp loop exprs were not built");
11929
11930 return OMPGenericLoopDirective::Create(C: getASTContext(), StartLoc, EndLoc,
11931 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
11932}
11933
11934/// Check the number of expressions specified in a multidimensional clause and
11935/// return whether an error was encountered.
11936static bool validateMultidimClauseExprs(
11937 SemaBase &SemaRef, OpenMPClauseKind ClauseKind,
11938 SourceLocation ClauseBeginLoc, ArrayRef<const Expr *> ClauseVarList,
11939 const Expr *DimsModifierExpr, const OMPXBareClause *BareClause = nullptr) {
11940 const uint64_t NumVars = ClauseVarList.size();
11941
11942 // The ompx_bare clause allows up to three expressions.
11943 if (BareClause) {
11944 if (NumVars > 3) {
11945 SemaRef.Diag(Loc: ClauseBeginLoc,
11946 DiagID: diag::err_ompx_more_than_three_expr_not_allowed)
11947 << getOpenMPClauseName(C: ClauseKind);
11948 return true;
11949 }
11950 return false;
11951 }
11952
11953 // By default, only one expression accepted.
11954 uint64_t MaxExprs = 1;
11955 if (DimsModifierExpr) {
11956 // Cannot verify the expected size yet.
11957 if (DimsModifierExpr->isInstantiationDependent())
11958 return false;
11959
11960 // The dims modifier determines the exact number of expressions.
11961 MaxExprs = DimsModifierExpr->EvaluateKnownConstInt(Ctx: SemaRef.getASTContext())
11962 .getExtValue();
11963 }
11964
11965 if (NumVars != MaxExprs) {
11966 SemaRef.Diag(Loc: ClauseBeginLoc, DiagID: diag::err_omp_unexpected_num_exprs)
11967 << getOpenMPClauseName(C: ClauseKind) << MaxExprs << NumVars;
11968 return true;
11969 }
11970 if (NumVars > 3) {
11971 SemaRef.Diag(Loc: ClauseBeginLoc, DiagID: diag::err_omp_max_three_exprs)
11972 << getOpenMPClauseName(C: ClauseKind);
11973 return true;
11974 }
11975 return false;
11976}
11977
11978/// Check the number of expressions specified in a multidimensional clause and
11979/// return whether an error was encountered.
11980template <typename ClauseT>
11981static bool validateMultidimClauseExprs(SemaBase &SemaRef,
11982 const ClauseT *Clause,
11983 const OMPXBareClause *BareClause) {
11984 if (!Clause)
11985 return false;
11986 return validateMultidimClauseExprs(
11987 SemaRef, Clause->getClauseKind(), Clause->getBeginLoc(),
11988 Clause->getVarRefs(), Clause->getDimsModifierExpr(), BareClause);
11989}
11990
11991/// Check the number of expressions specified in clauses that can contain
11992/// multidimensional values, e.g., num_teams and thread_limit. The function
11993/// returns true on error.
11994static bool validateMultidimClauses(SemaBase &SemaRef,
11995 ArrayRef<OMPClause *> Clauses,
11996 bool MayHaveBareClause = false) {
11997 auto BareClauseIt =
11998 MayHaveBareClause ? llvm::find_if(Range&: Clauses, P: llvm::IsaPred<OMPXBareClause>)
11999 : Clauses.end();
12000 auto ThreadLimitIt =
12001 llvm::find_if(Range&: Clauses, P: llvm::IsaPred<OMPThreadLimitClause>);
12002 auto NumTeamsIt = llvm::find_if(Range&: Clauses, P: llvm::IsaPred<OMPNumTeamsClause>);
12003
12004 const auto *BareClause = BareClauseIt != Clauses.end()
12005 ? cast<OMPXBareClause>(Val: *BareClauseIt)
12006 : nullptr;
12007 const auto *ThreadLimitClause =
12008 ThreadLimitIt != Clauses.end()
12009 ? cast<OMPThreadLimitClause>(Val: *ThreadLimitIt)
12010 : nullptr;
12011 const auto *NumTeamsClause = NumTeamsIt != Clauses.end()
12012 ? cast<OMPNumTeamsClause>(Val: *NumTeamsIt)
12013 : nullptr;
12014
12015 if (BareClause) {
12016 if (!NumTeamsClause || !ThreadLimitClause) {
12017 SemaRef.Diag(Loc: BareClause->getBeginLoc(), DiagID: diag::err_ompx_bare_no_grid);
12018 return true;
12019 }
12020 if (ThreadLimitClause->getModifier() == OMPC_THREADLIMIT_dims ||
12021 NumTeamsClause->getModifier() == OMPC_NUMTEAMS_dims) {
12022 SemaRef.Diag(Loc: BareClause->getBeginLoc(), DiagID: diag::err_ompx_bare_no_dims);
12023 return true;
12024 }
12025 }
12026 return validateMultidimClauseExprs(SemaRef, Clause: ThreadLimitClause, BareClause) ||
12027 validateMultidimClauseExprs(SemaRef, Clause: NumTeamsClause, BareClause);
12028}
12029
12030StmtResult SemaOpenMP::ActOnOpenMPTeamsGenericLoopDirective(
12031 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12032 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12033 if (!AStmt)
12034 return StmtError();
12035
12036 if (validateMultidimClauses(SemaRef&: *this, Clauses))
12037 return StmtError();
12038
12039 if (checkClausesForDecompositionConflicts(SemaRef, DKind: OMPD_teams_loop, Clauses))
12040 return StmtError();
12041
12042 // OpenMP 5.1 [2.11.7, loop construct, Restrictions]
12043 // A list item may not appear in a lastprivate clause unless it is the
12044 // loop iteration variable of a loop that is associated with the construct.
12045 if (checkGenericLoopLastprivate(S&: SemaRef, Clauses, K: OMPD_teams_loop, DSAStack))
12046 return StmtError();
12047
12048 CapturedStmt *CS = setBranchProtectedScope(SemaRef, DKind: OMPD_teams_loop, AStmt);
12049
12050 OMPLoopDirective::HelperExprs B;
12051 // In presence of clause 'collapse', it will define the nested loops number.
12052 unsigned NestedLoopCount =
12053 checkOpenMPLoop(DKind: OMPD_teams_loop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
12054 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
12055 VarsWithImplicitDSA, Built&: B);
12056 if (NestedLoopCount == 0)
12057 return StmtError();
12058
12059 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
12060 "omp loop exprs were not built");
12061
12062 DSAStack->setParentTeamsRegionLoc(StartLoc);
12063
12064 return OMPTeamsGenericLoopDirective::Create(
12065 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
12066}
12067
12068StmtResult SemaOpenMP::ActOnOpenMPTargetTeamsGenericLoopDirective(
12069 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12070 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12071 if (!AStmt)
12072 return StmtError();
12073
12074 if (validateMultidimClauses(SemaRef&: *this, Clauses))
12075 return StmtError();
12076
12077 // OpenMP 5.1 [2.11.7, loop construct, Restrictions]
12078 // A list item may not appear in a lastprivate clause unless it is the
12079 // loop iteration variable of a loop that is associated with the construct.
12080 if (checkGenericLoopLastprivate(S&: SemaRef, Clauses, K: OMPD_target_teams_loop,
12081 DSAStack))
12082 return StmtError();
12083
12084 if (checkClausesForDecompositionConflicts(SemaRef, DKind: OMPD_target_teams_loop,
12085 Clauses))
12086 return StmtError();
12087
12088 if (checkOriginalVarMappedButOnlyBindingsUsed(SemaRef, Clauses, Body: AStmt))
12089 return StmtError();
12090
12091 CapturedStmt *CS =
12092 setBranchProtectedScope(SemaRef, DKind: OMPD_target_teams_loop, AStmt);
12093
12094 OMPLoopDirective::HelperExprs B;
12095 // In presence of clause 'collapse', it will define the nested loops number.
12096 unsigned NestedLoopCount =
12097 checkOpenMPLoop(DKind: OMPD_target_teams_loop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
12098 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
12099 VarsWithImplicitDSA, Built&: B);
12100 if (NestedLoopCount == 0)
12101 return StmtError();
12102
12103 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
12104 "omp loop exprs were not built");
12105
12106 return OMPTargetTeamsGenericLoopDirective::Create(
12107 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
12108 CanBeParallelFor: teamsLoopCanBeParallelFor(AStmt, SemaRef));
12109}
12110
12111StmtResult SemaOpenMP::ActOnOpenMPParallelGenericLoopDirective(
12112 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12113 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12114 if (!AStmt)
12115 return StmtError();
12116
12117 // OpenMP 5.1 [2.11.7, loop construct, Restrictions]
12118 // A list item may not appear in a lastprivate clause unless it is the
12119 // loop iteration variable of a loop that is associated with the construct.
12120 if (checkGenericLoopLastprivate(S&: SemaRef, Clauses, K: OMPD_parallel_loop,
12121 DSAStack))
12122 return StmtError();
12123
12124 CapturedStmt *CS =
12125 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_loop, AStmt);
12126
12127 OMPLoopDirective::HelperExprs B;
12128 // In presence of clause 'collapse', it will define the nested loops number.
12129 unsigned NestedLoopCount =
12130 checkOpenMPLoop(DKind: OMPD_parallel_loop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
12131 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
12132 VarsWithImplicitDSA, Built&: B);
12133 if (NestedLoopCount == 0)
12134 return StmtError();
12135
12136 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
12137 "omp loop exprs were not built");
12138
12139 return OMPParallelGenericLoopDirective::Create(
12140 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
12141}
12142
12143/// Shared by 'target loop' and 'target parallel loop'.
12144template <typename DirectiveTy>
12145static StmtResult actOnTargetGenericLoopDirective(
12146 SemaOpenMP &S, DSAStackTy *Stack, OpenMPDirectiveKind DKind,
12147 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12148 SourceLocation EndLoc,
12149 SemaOpenMP::VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12150 if (!AStmt)
12151 return StmtError();
12152
12153 if (validateMultidimClauses(SemaRef&: S, Clauses))
12154 return StmtError();
12155
12156 Sema &SemaRef = S.SemaRef;
12157 if (checkClausesForDecompositionConflicts(SemaRef, DKind, Clauses))
12158 return StmtError();
12159
12160 if (checkOriginalVarMappedButOnlyBindingsUsed(SemaRef, Clauses, Body: AStmt))
12161 return StmtError();
12162
12163 // OpenMP 5.1 [2.11.7, loop construct, Restrictions]
12164 // A list item may not appear in a lastprivate clause unless it is the
12165 // loop iteration variable of a loop that is associated with the construct.
12166 if (checkGenericLoopLastprivate(S&: SemaRef, Clauses, K: DKind, Stack))
12167 return StmtError();
12168
12169 CapturedStmt *CS = setBranchProtectedScope(SemaRef, DKind, AStmt);
12170
12171 OMPLoopDirective::HelperExprs B;
12172 // In presence of clause 'collapse', it will define the nested loops number.
12173 unsigned NestedLoopCount = checkOpenMPLoop(
12174 DKind, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses), /*OrderedLoopCountExpr=*/nullptr,
12175 AStmt: CS, SemaRef, DSA&: *Stack, VarsWithImplicitDSA, Built&: B);
12176 if (NestedLoopCount == 0)
12177 return StmtError();
12178
12179 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
12180 "omp loop exprs were not built");
12181
12182 return DirectiveTy::Create(S.getASTContext(), StartLoc, EndLoc,
12183 NestedLoopCount, Clauses, AStmt, B);
12184}
12185
12186StmtResult SemaOpenMP::ActOnOpenMPTargetParallelGenericLoopDirective(
12187 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12188 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12189 return actOnTargetGenericLoopDirective<OMPTargetParallelGenericLoopDirective>(
12190 S&: *this, DSAStack, DKind: OMPD_target_parallel_loop, Clauses, AStmt, StartLoc,
12191 EndLoc, VarsWithImplicitDSA);
12192}
12193
12194StmtResult SemaOpenMP::ActOnOpenMPTargetGenericLoopDirective(
12195 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12196 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12197 // OpenMP 5.1 [2.11.7, loop Construct, Restrictions]
12198 // If the bind clause is present on the loop construct and binding is teams
12199 // then the corresponding loop region must be strictly nested inside a teams
12200 // region.
12201 if (const auto *BC =
12202 OMPExecutableDirective::getSingleClause<OMPBindClause>(Clauses);
12203 BC && BC->getBindKind() == OMPC_BIND_teams) {
12204 llvm::omp::Version OMPVersion = getLangOpts().getOpenMPVersion();
12205 Diag(Loc: StartLoc, DiagID: diag::err_omp_prohibited_region)
12206 << /*IsClose=*/true << getOpenMPDirectiveName(D: OMPD_target, V: OMPVersion)
12207 << /*ShouldBeInTeamsRegion=*/4
12208 << getOpenMPDirectiveName(D: OMPD_loop, V: OMPVersion);
12209 return StmtError();
12210 }
12211 return actOnTargetGenericLoopDirective<OMPTargetGenericLoopDirective>(
12212 S&: *this, DSAStack, DKind: OMPD_target_loop, Clauses, AStmt, StartLoc, EndLoc,
12213 VarsWithImplicitDSA);
12214}
12215
12216StmtResult SemaOpenMP::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
12217 Stmt *AStmt,
12218 SourceLocation StartLoc,
12219 SourceLocation EndLoc) {
12220 if (!AStmt)
12221 return StmtError();
12222
12223 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
12224
12225 SemaRef.setFunctionHasBranchProtectedScope();
12226
12227 // OpenMP [2.7.3, single Construct, Restrictions]
12228 // The copyprivate clause must not be used with the nowait clause.
12229 const OMPClause *Nowait = nullptr;
12230 const OMPClause *Copyprivate = nullptr;
12231 for (const OMPClause *Clause : Clauses) {
12232 if (Clause->getClauseKind() == OMPC_nowait)
12233 Nowait = Clause;
12234 else if (Clause->getClauseKind() == OMPC_copyprivate)
12235 Copyprivate = Clause;
12236 if (Copyprivate && Nowait) {
12237 Diag(Loc: Copyprivate->getBeginLoc(),
12238 DiagID: diag::err_omp_single_copyprivate_with_nowait);
12239 Diag(Loc: Nowait->getBeginLoc(), DiagID: diag::note_omp_nowait_clause_here);
12240 return StmtError();
12241 }
12242 }
12243
12244 return OMPSingleDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
12245 AssociatedStmt: AStmt);
12246}
12247
12248StmtResult SemaOpenMP::ActOnOpenMPMasterDirective(Stmt *AStmt,
12249 SourceLocation StartLoc,
12250 SourceLocation EndLoc) {
12251 if (!AStmt)
12252 return StmtError();
12253
12254 SemaRef.setFunctionHasBranchProtectedScope();
12255
12256 return OMPMasterDirective::Create(C: getASTContext(), StartLoc, EndLoc, AssociatedStmt: AStmt);
12257}
12258
12259StmtResult SemaOpenMP::ActOnOpenMPMaskedDirective(ArrayRef<OMPClause *> Clauses,
12260 Stmt *AStmt,
12261 SourceLocation StartLoc,
12262 SourceLocation EndLoc) {
12263 if (!AStmt)
12264 return StmtError();
12265
12266 SemaRef.setFunctionHasBranchProtectedScope();
12267
12268 return OMPMaskedDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
12269 AssociatedStmt: AStmt);
12270}
12271
12272StmtResult SemaOpenMP::ActOnOpenMPCriticalDirective(
12273 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
12274 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
12275 if (!AStmt)
12276 return StmtError();
12277
12278 bool ErrorFound = false;
12279 llvm::APSInt Hint;
12280 SourceLocation HintLoc;
12281 bool DependentHint = false;
12282 for (const OMPClause *C : Clauses) {
12283 if (C->getClauseKind() == OMPC_hint) {
12284 if (!DirName.getName()) {
12285 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_hint_clause_no_name);
12286 ErrorFound = true;
12287 }
12288 Expr *E = cast<OMPHintClause>(Val: C)->getHint();
12289 if (E->isTypeDependent() || E->isValueDependent() ||
12290 E->isInstantiationDependent()) {
12291 DependentHint = true;
12292 } else {
12293 Hint = E->EvaluateKnownConstInt(Ctx: getASTContext());
12294 HintLoc = C->getBeginLoc();
12295 }
12296 }
12297 }
12298 if (ErrorFound)
12299 return StmtError();
12300 const auto Pair = DSAStack->getCriticalWithHint(Name: DirName);
12301 if (Pair.first && DirName.getName() && !DependentHint) {
12302 if (llvm::APSInt::compareValues(I1: Hint, I2: Pair.second) != 0) {
12303 Diag(Loc: StartLoc, DiagID: diag::err_omp_critical_with_hint);
12304 if (HintLoc.isValid())
12305 Diag(Loc: HintLoc, DiagID: diag::note_omp_critical_hint_here)
12306 << 0 << toString(I: Hint, /*Radix=*/10, /*Signed=*/false);
12307 else
12308 Diag(Loc: StartLoc, DiagID: diag::note_omp_critical_no_hint) << 0;
12309 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
12310 Diag(Loc: C->getBeginLoc(), DiagID: diag::note_omp_critical_hint_here)
12311 << 1
12312 << toString(I: C->getHint()->EvaluateKnownConstInt(Ctx: getASTContext()),
12313 /*Radix=*/10, /*Signed=*/false);
12314 } else {
12315 Diag(Loc: Pair.first->getBeginLoc(), DiagID: diag::note_omp_critical_no_hint) << 1;
12316 }
12317 }
12318 }
12319
12320 SemaRef.setFunctionHasBranchProtectedScope();
12321
12322 auto *Dir = OMPCriticalDirective::Create(C: getASTContext(), Name: DirName, StartLoc,
12323 EndLoc, Clauses, AssociatedStmt: AStmt);
12324 if (!Pair.first && DirName.getName() && !DependentHint)
12325 DSAStack->addCriticalWithHint(D: Dir, Hint);
12326 return Dir;
12327}
12328
12329StmtResult SemaOpenMP::ActOnOpenMPParallelForDirective(
12330 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12331 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12332 if (!AStmt)
12333 return StmtError();
12334
12335 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_for, AStmt);
12336
12337 OMPLoopBasedDirective::HelperExprs B;
12338 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
12339 // define the nested loops number.
12340 unsigned NestedLoopCount =
12341 checkOpenMPLoop(DKind: OMPD_parallel_for, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
12342 OrderedLoopCountExpr: getOrderedNumberExpr(Clauses), AStmt, SemaRef, DSA&: *DSAStack,
12343 VarsWithImplicitDSA, Built&: B);
12344 if (NestedLoopCount == 0)
12345 return StmtError();
12346
12347 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
12348 return StmtError();
12349
12350 return OMPParallelForDirective::Create(
12351 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
12352 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
12353}
12354
12355StmtResult SemaOpenMP::ActOnOpenMPParallelForSimdDirective(
12356 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12357 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
12358 if (!AStmt)
12359 return StmtError();
12360
12361 CapturedStmt *CS =
12362 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_for_simd, AStmt);
12363
12364 OMPLoopBasedDirective::HelperExprs B;
12365 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
12366 // define the nested loops number.
12367 unsigned NestedLoopCount =
12368 checkOpenMPLoop(DKind: OMPD_parallel_for_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
12369 OrderedLoopCountExpr: getOrderedNumberExpr(Clauses), AStmt: CS, SemaRef, DSA&: *DSAStack,
12370 VarsWithImplicitDSA, Built&: B);
12371 if (NestedLoopCount == 0)
12372 return StmtError();
12373
12374 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
12375 return StmtError();
12376
12377 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
12378 return StmtError();
12379
12380 return OMPParallelForSimdDirective::Create(
12381 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
12382}
12383
12384StmtResult SemaOpenMP::ActOnOpenMPParallelMasterDirective(
12385 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12386 SourceLocation EndLoc) {
12387 if (!AStmt)
12388 return StmtError();
12389
12390 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_master, AStmt);
12391
12392 return OMPParallelMasterDirective::Create(
12393 C: getASTContext(), StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
12394 DSAStack->getTaskgroupReductionRef());
12395}
12396
12397StmtResult SemaOpenMP::ActOnOpenMPParallelMaskedDirective(
12398 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12399 SourceLocation EndLoc) {
12400 if (!AStmt)
12401 return StmtError();
12402
12403 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_masked, AStmt);
12404
12405 return OMPParallelMaskedDirective::Create(
12406 C: getASTContext(), StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
12407 DSAStack->getTaskgroupReductionRef());
12408}
12409
12410StmtResult SemaOpenMP::ActOnOpenMPParallelSectionsDirective(
12411 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
12412 SourceLocation EndLoc) {
12413 if (checkSectionsDirective(SemaRef, DKind: OMPD_parallel_sections, AStmt, DSAStack))
12414 return StmtError();
12415
12416 SemaRef.setFunctionHasBranchProtectedScope();
12417
12418 return OMPParallelSectionsDirective::Create(
12419 C: getASTContext(), StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
12420 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
12421}
12422
12423/// Find and diagnose mutually exclusive clause kinds.
12424static bool checkMutuallyExclusiveClauses(
12425 Sema &S, ArrayRef<OMPClause *> Clauses,
12426 ArrayRef<OpenMPClauseKind> MutuallyExclusiveClauses) {
12427 const OMPClause *PrevClause = nullptr;
12428 bool ErrorFound = false;
12429 for (const OMPClause *C : Clauses) {
12430 if (llvm::is_contained(Range&: MutuallyExclusiveClauses, Element: C->getClauseKind())) {
12431 if (!PrevClause) {
12432 PrevClause = C;
12433 } else if (PrevClause->getClauseKind() != C->getClauseKind()) {
12434 S.Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_clauses_mutually_exclusive)
12435 << getOpenMPClauseNameForDiag(C: C->getClauseKind())
12436 << getOpenMPClauseNameForDiag(C: PrevClause->getClauseKind());
12437 S.Diag(Loc: PrevClause->getBeginLoc(), DiagID: diag::note_omp_previous_clause)
12438 << getOpenMPClauseNameForDiag(C: PrevClause->getClauseKind());
12439 ErrorFound = true;
12440 }
12441 }
12442 }
12443 return ErrorFound;
12444}
12445
12446StmtResult SemaOpenMP::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
12447 Stmt *AStmt,
12448 SourceLocation StartLoc,
12449 SourceLocation EndLoc) {
12450 if (!AStmt)
12451 return StmtError();
12452
12453 // OpenMP 5.0, 2.10.1 task Construct
12454 // If a detach clause appears on the directive, then a mergeable clause cannot
12455 // appear on the same directive.
12456 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
12457 MutuallyExclusiveClauses: {OMPC_detach, OMPC_mergeable}))
12458 return StmtError();
12459
12460 // Check for conflicting capture kinds on structured bindings.
12461 if (checkClausesForDecompositionConflicts(SemaRef, DKind: OMPD_task, Clauses, Body: AStmt))
12462 return StmtError();
12463
12464 setBranchProtectedScope(SemaRef, DKind: OMPD_task, AStmt);
12465
12466 return OMPTaskDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
12467 AssociatedStmt: AStmt, DSAStack->isCancelRegion());
12468}
12469
12470StmtResult SemaOpenMP::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
12471 SourceLocation EndLoc) {
12472 return OMPTaskyieldDirective::Create(C: getASTContext(), StartLoc, EndLoc);
12473}
12474
12475StmtResult SemaOpenMP::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
12476 SourceLocation EndLoc) {
12477 return OMPBarrierDirective::Create(C: getASTContext(), StartLoc, EndLoc);
12478}
12479
12480StmtResult SemaOpenMP::ActOnOpenMPErrorDirective(ArrayRef<OMPClause *> Clauses,
12481 SourceLocation StartLoc,
12482 SourceLocation EndLoc,
12483 bool InExContext) {
12484 const OMPAtClause *AtC =
12485 OMPExecutableDirective::getSingleClause<OMPAtClause>(Clauses);
12486
12487 if (AtC && !InExContext && AtC->getAtKind() == OMPC_AT_execution) {
12488 Diag(Loc: AtC->getAtKindKwLoc(), DiagID: diag::err_omp_unexpected_execution_modifier);
12489 return StmtError();
12490 }
12491
12492 if (!AtC || AtC->getAtKind() == OMPC_AT_compilation) {
12493 const OMPSeverityClause *SeverityC =
12494 OMPExecutableDirective::getSingleClause<OMPSeverityClause>(Clauses);
12495 const OMPMessageClause *MessageC =
12496 OMPExecutableDirective::getSingleClause<OMPMessageClause>(Clauses);
12497 std::optional<std::string> SL =
12498 MessageC ? MessageC->tryEvaluateString(Ctx&: getASTContext()) : std::nullopt;
12499
12500 if (MessageC && !SL)
12501 Diag(Loc: MessageC->getMessageString()->getBeginLoc(),
12502 DiagID: diag::warn_clause_expected_string)
12503 << getOpenMPClauseNameForDiag(C: OMPC_message) << 1;
12504 if (SeverityC && SeverityC->getSeverityKind() == OMPC_SEVERITY_warning)
12505 Diag(Loc: SeverityC->getSeverityKindKwLoc(), DiagID: diag::warn_diagnose_if_succeeded)
12506 << SL.value_or(u: "WARNING");
12507 else
12508 Diag(Loc: StartLoc, DiagID: diag::err_diagnose_if_succeeded) << SL.value_or(u: "ERROR");
12509 if (!SeverityC || SeverityC->getSeverityKind() != OMPC_SEVERITY_warning)
12510 return StmtError();
12511 }
12512
12513 return OMPErrorDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses);
12514}
12515
12516StmtResult
12517SemaOpenMP::ActOnOpenMPTaskwaitDirective(ArrayRef<OMPClause *> Clauses,
12518 SourceLocation StartLoc,
12519 SourceLocation EndLoc) {
12520 const OMPNowaitClause *NowaitC =
12521 OMPExecutableDirective::getSingleClause<OMPNowaitClause>(Clauses);
12522 bool HasDependC =
12523 !OMPExecutableDirective::getClausesOfKind<OMPDependClause>(Clauses)
12524 .empty();
12525 if (NowaitC && !HasDependC) {
12526 Diag(Loc: StartLoc, DiagID: diag::err_omp_nowait_clause_without_depend);
12527 return StmtError();
12528 }
12529
12530 return OMPTaskwaitDirective::Create(C: getASTContext(), StartLoc, EndLoc,
12531 Clauses);
12532}
12533
12534StmtResult
12535SemaOpenMP::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
12536 Stmt *AStmt, SourceLocation StartLoc,
12537 SourceLocation EndLoc) {
12538 if (!AStmt)
12539 return StmtError();
12540
12541 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
12542
12543 SemaRef.setFunctionHasBranchProtectedScope();
12544
12545 return OMPTaskgroupDirective::Create(C: getASTContext(), StartLoc, EndLoc,
12546 Clauses, AssociatedStmt: AStmt,
12547 DSAStack->getTaskgroupReductionRef());
12548}
12549
12550StmtResult SemaOpenMP::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
12551 SourceLocation StartLoc,
12552 SourceLocation EndLoc) {
12553 OMPFlushClause *FC = nullptr;
12554 OMPClause *OrderClause = nullptr;
12555 for (OMPClause *C : Clauses) {
12556 if (C->getClauseKind() == OMPC_flush)
12557 FC = cast<OMPFlushClause>(Val: C);
12558 else
12559 OrderClause = C;
12560 }
12561 llvm::omp::Version OMPVersion = getLangOpts().getOpenMPVersion();
12562 OpenMPClauseKind MemOrderKind = OMPC_unknown;
12563 SourceLocation MemOrderLoc;
12564 for (const OMPClause *C : Clauses) {
12565 if (C->getClauseKind() == OMPC_acq_rel ||
12566 C->getClauseKind() == OMPC_acquire ||
12567 C->getClauseKind() == OMPC_release ||
12568 C->getClauseKind() == OMPC_seq_cst /*OpenMP 5.1*/) {
12569 if (MemOrderKind != OMPC_unknown) {
12570 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_several_mem_order_clauses)
12571 << getOpenMPDirectiveName(D: OMPD_flush, V: OMPVersion) << 1
12572 << SourceRange(C->getBeginLoc(), C->getEndLoc());
12573 Diag(Loc: MemOrderLoc, DiagID: diag::note_omp_previous_mem_order_clause)
12574 << getOpenMPClauseNameForDiag(C: MemOrderKind);
12575 } else {
12576 MemOrderKind = C->getClauseKind();
12577 MemOrderLoc = C->getBeginLoc();
12578 }
12579 }
12580 }
12581 if (FC && OrderClause) {
12582 Diag(Loc: FC->getLParenLoc(), DiagID: diag::err_omp_flush_order_clause_and_list)
12583 << getOpenMPClauseNameForDiag(C: OrderClause->getClauseKind());
12584 Diag(Loc: OrderClause->getBeginLoc(), DiagID: diag::note_omp_flush_order_clause_here)
12585 << getOpenMPClauseNameForDiag(C: OrderClause->getClauseKind());
12586 return StmtError();
12587 }
12588 return OMPFlushDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses);
12589}
12590
12591StmtResult SemaOpenMP::ActOnOpenMPDepobjDirective(ArrayRef<OMPClause *> Clauses,
12592 SourceLocation StartLoc,
12593 SourceLocation EndLoc) {
12594 if (Clauses.empty()) {
12595 Diag(Loc: StartLoc, DiagID: diag::err_omp_depobj_expected);
12596 return StmtError();
12597 } else if (Clauses[0]->getClauseKind() != OMPC_depobj) {
12598 Diag(Loc: Clauses[0]->getBeginLoc(), DiagID: diag::err_omp_depobj_expected);
12599 return StmtError();
12600 }
12601 // Only depobj expression and another single clause is allowed.
12602 if (Clauses.size() > 2) {
12603 Diag(Loc: Clauses[2]->getBeginLoc(),
12604 DiagID: diag::err_omp_depobj_single_clause_expected);
12605 return StmtError();
12606 } else if (Clauses.size() < 1) {
12607 Diag(Loc: Clauses[0]->getEndLoc(), DiagID: diag::err_omp_depobj_single_clause_expected);
12608 return StmtError();
12609 }
12610 return OMPDepobjDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses);
12611}
12612
12613StmtResult SemaOpenMP::ActOnOpenMPScanDirective(ArrayRef<OMPClause *> Clauses,
12614 SourceLocation StartLoc,
12615 SourceLocation EndLoc) {
12616 // Check that exactly one clause is specified.
12617 if (Clauses.size() != 1) {
12618 Diag(Loc: Clauses.empty() ? EndLoc : Clauses[1]->getBeginLoc(),
12619 DiagID: diag::err_omp_scan_single_clause_expected);
12620 return StmtError();
12621 }
12622 // Check that scan directive is used in the scope of the OpenMP loop body.
12623 if (Scope *S = DSAStack->getCurScope()) {
12624 Scope *ParentS = S->getParent();
12625 if (!ParentS || ParentS->getParent() != ParentS->getBreakParent() ||
12626 !ParentS->getBreakParent()->isOpenMPLoopScope()) {
12627 llvm::omp::Version OMPVersion = getLangOpts().getOpenMPVersion();
12628 return StmtError(Diag(Loc: StartLoc, DiagID: diag::err_omp_orphaned_device_directive)
12629 << getOpenMPDirectiveName(D: OMPD_scan, V: OMPVersion) << 5);
12630 }
12631 }
12632 // Check that only one instance of scan directives is used in the same outer
12633 // region.
12634 if (DSAStack->doesParentHasScanDirective()) {
12635 Diag(Loc: StartLoc, DiagID: diag::err_omp_several_directives_in_region) << "scan";
12636 Diag(DSAStack->getParentScanDirectiveLoc(),
12637 DiagID: diag::note_omp_previous_directive)
12638 << "scan";
12639 return StmtError();
12640 }
12641 DSAStack->setParentHasScanDirective(StartLoc);
12642 return OMPScanDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses);
12643}
12644
12645StmtResult
12646SemaOpenMP::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
12647 Stmt *AStmt, SourceLocation StartLoc,
12648 SourceLocation EndLoc) {
12649 const OMPClause *DependFound = nullptr;
12650 const OMPClause *DependSourceClause = nullptr;
12651 const OMPClause *DependSinkClause = nullptr;
12652 const OMPClause *DoacrossFound = nullptr;
12653 const OMPClause *DoacrossSourceClause = nullptr;
12654 const OMPClause *DoacrossSinkClause = nullptr;
12655 bool ErrorFound = false;
12656 const OMPThreadsClause *TC = nullptr;
12657 const OMPSIMDClause *SC = nullptr;
12658 for (const OMPClause *C : Clauses) {
12659 auto DOC = dyn_cast<OMPDoacrossClause>(Val: C);
12660 auto DC = dyn_cast<OMPDependClause>(Val: C);
12661 if (DC || DOC) {
12662 DependFound = DC ? C : nullptr;
12663 DoacrossFound = DOC ? C : nullptr;
12664 OMPDoacrossKind ODK;
12665 if ((DC && DC->getDependencyKind() == OMPC_DEPEND_source) ||
12666 (DOC && (ODK.isSource(C: DOC)))) {
12667 if ((DC && DependSourceClause) || (DOC && DoacrossSourceClause)) {
12668 llvm::omp::Version OMPVersion = getLangOpts().getOpenMPVersion();
12669 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_more_one_clause)
12670 << getOpenMPDirectiveName(DSAStack->getCurrentDirective(),
12671 V: OMPVersion)
12672 << getOpenMPClauseNameForDiag(C: DC ? OMPC_depend : OMPC_doacross)
12673 << 2;
12674 ErrorFound = true;
12675 } else {
12676 if (DC)
12677 DependSourceClause = C;
12678 else
12679 DoacrossSourceClause = C;
12680 }
12681 if ((DC && DependSinkClause) || (DOC && DoacrossSinkClause)) {
12682 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_sink_and_source_not_allowed)
12683 << (DC ? "depend" : "doacross") << 0;
12684 ErrorFound = true;
12685 }
12686 } else if ((DC && DC->getDependencyKind() == OMPC_DEPEND_sink) ||
12687 (DOC && (ODK.isSink(C: DOC) || ODK.isSinkIter(C: DOC)))) {
12688 if (DependSourceClause || DoacrossSourceClause) {
12689 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_sink_and_source_not_allowed)
12690 << (DC ? "depend" : "doacross") << 1;
12691 ErrorFound = true;
12692 }
12693 if (DC)
12694 DependSinkClause = C;
12695 else
12696 DoacrossSinkClause = C;
12697 }
12698 } else if (C->getClauseKind() == OMPC_threads) {
12699 TC = cast<OMPThreadsClause>(Val: C);
12700 } else if (C->getClauseKind() == OMPC_simd) {
12701 SC = cast<OMPSIMDClause>(Val: C);
12702 }
12703 }
12704 if (!ErrorFound && !SC &&
12705 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
12706 // OpenMP [2.8.1,simd Construct, Restrictions]
12707 // An ordered construct with the simd clause is the only OpenMP construct
12708 // that can appear in the simd region.
12709 Diag(Loc: StartLoc, DiagID: diag::err_omp_prohibited_region_simd)
12710 << (getLangOpts().OpenMP >= 50 ? 1 : 0);
12711 ErrorFound = true;
12712 } else if ((DependFound || DoacrossFound) && (TC || SC)) {
12713 SourceLocation Loc =
12714 DependFound ? DependFound->getBeginLoc() : DoacrossFound->getBeginLoc();
12715 Diag(Loc, DiagID: diag::err_omp_depend_clause_thread_simd)
12716 << getOpenMPClauseNameForDiag(C: DependFound ? OMPC_depend : OMPC_doacross)
12717 << getOpenMPClauseNameForDiag(C: TC ? TC->getClauseKind()
12718 : SC->getClauseKind());
12719 ErrorFound = true;
12720 } else if ((DependFound || DoacrossFound) &&
12721 !DSAStack->getParentOrderedRegionParam().first) {
12722 SourceLocation Loc =
12723 DependFound ? DependFound->getBeginLoc() : DoacrossFound->getBeginLoc();
12724 Diag(Loc, DiagID: diag::err_omp_ordered_directive_without_param)
12725 << getOpenMPClauseNameForDiag(C: DependFound ? OMPC_depend
12726 : OMPC_doacross);
12727 ErrorFound = true;
12728 } else if (TC || Clauses.empty()) {
12729 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
12730 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
12731 Diag(Loc: ErrLoc, DiagID: diag::err_omp_ordered_directive_with_param)
12732 << (TC != nullptr);
12733 Diag(Loc: Param->getBeginLoc(), DiagID: diag::note_omp_ordered_param) << 1;
12734 ErrorFound = true;
12735 }
12736 }
12737 if ((!AStmt && !DependFound && !DoacrossFound) || ErrorFound)
12738 return StmtError();
12739
12740 // OpenMP 5.0, 2.17.9, ordered Construct, Restrictions.
12741 // During execution of an iteration of a worksharing-loop or a loop nest
12742 // within a worksharing-loop, simd, or worksharing-loop SIMD region, a thread
12743 // must not execute more than one ordered region corresponding to an ordered
12744 // construct without a depend clause.
12745 if (!DependFound && !DoacrossFound) {
12746 if (DSAStack->doesParentHasOrderedDirective()) {
12747 Diag(Loc: StartLoc, DiagID: diag::err_omp_several_directives_in_region) << "ordered";
12748 Diag(DSAStack->getParentOrderedDirectiveLoc(),
12749 DiagID: diag::note_omp_previous_directive)
12750 << "ordered";
12751 return StmtError();
12752 }
12753 DSAStack->setParentHasOrderedDirective(StartLoc);
12754 }
12755
12756 if (AStmt) {
12757 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
12758
12759 SemaRef.setFunctionHasBranchProtectedScope();
12760 }
12761
12762 if (!AStmt)
12763 return OMPOrderedStandaloneDirective::Create(C: getASTContext(), StartLoc,
12764 EndLoc, Clauses);
12765 return OMPOrderedBlockAssocDirective::Create(C: getASTContext(), StartLoc,
12766 EndLoc, Clauses, AssociatedStmt: AStmt);
12767}
12768
12769namespace {
12770/// Helper class for checking expression in 'omp atomic [update]'
12771/// construct.
12772class OpenMPAtomicUpdateChecker {
12773 /// Error results for atomic update expressions.
12774 enum ExprAnalysisErrorCode {
12775 /// A statement is not an expression statement.
12776 NotAnExpression,
12777 /// Expression is not builtin binary or unary operation.
12778 NotABinaryOrUnaryExpression,
12779 /// Unary operation is not post-/pre- increment/decrement operation.
12780 NotAnUnaryIncDecExpression,
12781 /// An expression is not of scalar type.
12782 NotAScalarType,
12783 /// A binary operation is not an assignment operation.
12784 NotAnAssignmentOp,
12785 /// RHS part of the binary operation is not a binary expression.
12786 NotABinaryExpression,
12787 /// RHS part is not additive/multiplicative/shift/bitwise binary
12788 /// expression.
12789 NotABinaryOperator,
12790 /// RHS binary operation does not have reference to the updated LHS
12791 /// part.
12792 NotAnUpdateExpression,
12793 /// An expression contains semantical error not related to
12794 /// 'omp atomic [update]'
12795 NotAValidExpression,
12796 /// No errors is found.
12797 NoError
12798 };
12799 /// Reference to Sema.
12800 Sema &SemaRef;
12801 /// A location for note diagnostics (when error is found).
12802 SourceLocation NoteLoc;
12803 /// 'x' lvalue part of the source atomic expression.
12804 Expr *X;
12805 /// 'expr' rvalue part of the source atomic expression.
12806 Expr *E;
12807 /// Helper expression of the form
12808 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
12809 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
12810 Expr *UpdateExpr;
12811 /// Is 'x' a LHS in a RHS part of full update expression. It is
12812 /// important for non-associative operations.
12813 bool IsXLHSInRHSPart;
12814 BinaryOperatorKind Op;
12815 SourceLocation OpLoc;
12816 /// true if the source expression is a postfix unary operation, false
12817 /// if it is a prefix unary operation.
12818 bool IsPostfixUpdate;
12819
12820public:
12821 OpenMPAtomicUpdateChecker(Sema &SemaRef)
12822 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
12823 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
12824 /// Check specified statement that it is suitable for 'atomic update'
12825 /// constructs and extract 'x', 'expr' and Operation from the original
12826 /// expression. If DiagId and NoteId == 0, then only check is performed
12827 /// without error notification.
12828 /// \param DiagId Diagnostic which should be emitted if error is found.
12829 /// \param NoteId Diagnostic note for the main error message.
12830 /// \return true if statement is not an update expression, false otherwise.
12831 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
12832 /// Return the 'x' lvalue part of the source atomic expression.
12833 Expr *getX() const { return X; }
12834 /// Return the 'expr' rvalue part of the source atomic expression.
12835 Expr *getExpr() const { return E; }
12836 /// Return the update expression used in calculation of the updated
12837 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
12838 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
12839 Expr *getUpdateExpr() const { return UpdateExpr; }
12840 /// Return true if 'x' is LHS in RHS part of full update expression,
12841 /// false otherwise.
12842 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
12843
12844 /// true if the source expression is a postfix unary operation, false
12845 /// if it is a prefix unary operation.
12846 bool isPostfixUpdate() const { return IsPostfixUpdate; }
12847
12848private:
12849 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
12850 unsigned NoteId = 0);
12851};
12852
12853bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
12854 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
12855 ExprAnalysisErrorCode ErrorFound = NoError;
12856 SourceLocation ErrorLoc, NoteLoc;
12857 SourceRange ErrorRange, NoteRange;
12858 // Allowed constructs are:
12859 // x = x binop expr;
12860 // x = expr binop x;
12861 if (AtomicBinOp->getOpcode() == BO_Assign) {
12862 X = AtomicBinOp->getLHS();
12863 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
12864 Val: AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
12865 if (AtomicInnerBinOp->isMultiplicativeOp() ||
12866 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
12867 AtomicInnerBinOp->isBitwiseOp()) {
12868 Op = AtomicInnerBinOp->getOpcode();
12869 OpLoc = AtomicInnerBinOp->getOperatorLoc();
12870 Expr *LHS = AtomicInnerBinOp->getLHS();
12871 Expr *RHS = AtomicInnerBinOp->getRHS();
12872 llvm::FoldingSetNodeID XId, LHSId, RHSId;
12873 X->IgnoreParenImpCasts()->Profile(ID&: XId, Context: SemaRef.getASTContext(),
12874 /*Canonical=*/true);
12875 LHS->IgnoreParenImpCasts()->Profile(ID&: LHSId, Context: SemaRef.getASTContext(),
12876 /*Canonical=*/true);
12877 RHS->IgnoreParenImpCasts()->Profile(ID&: RHSId, Context: SemaRef.getASTContext(),
12878 /*Canonical=*/true);
12879 if (XId == LHSId) {
12880 E = RHS;
12881 IsXLHSInRHSPart = true;
12882 } else if (XId == RHSId) {
12883 E = LHS;
12884 IsXLHSInRHSPart = false;
12885 } else {
12886 ErrorLoc = AtomicInnerBinOp->getExprLoc();
12887 ErrorRange = AtomicInnerBinOp->getSourceRange();
12888 NoteLoc = X->getExprLoc();
12889 NoteRange = X->getSourceRange();
12890 ErrorFound = NotAnUpdateExpression;
12891 }
12892 } else {
12893 ErrorLoc = AtomicInnerBinOp->getExprLoc();
12894 ErrorRange = AtomicInnerBinOp->getSourceRange();
12895 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
12896 NoteRange = SourceRange(NoteLoc, NoteLoc);
12897 ErrorFound = NotABinaryOperator;
12898 }
12899 } else {
12900 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
12901 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
12902 ErrorFound = NotABinaryExpression;
12903 }
12904 } else {
12905 ErrorLoc = AtomicBinOp->getExprLoc();
12906 ErrorRange = AtomicBinOp->getSourceRange();
12907 NoteLoc = AtomicBinOp->getOperatorLoc();
12908 NoteRange = SourceRange(NoteLoc, NoteLoc);
12909 ErrorFound = NotAnAssignmentOp;
12910 }
12911 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
12912 SemaRef.Diag(Loc: ErrorLoc, DiagID: DiagId) << ErrorRange;
12913 SemaRef.Diag(Loc: NoteLoc, DiagID: NoteId) << ErrorFound << NoteRange;
12914 return true;
12915 }
12916 if (SemaRef.CurContext->isDependentContext())
12917 E = X = UpdateExpr = nullptr;
12918 return ErrorFound != NoError;
12919}
12920
12921bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
12922 unsigned NoteId) {
12923 ExprAnalysisErrorCode ErrorFound = NoError;
12924 SourceLocation ErrorLoc, NoteLoc;
12925 SourceRange ErrorRange, NoteRange;
12926 // Allowed constructs are:
12927 // x++;
12928 // x--;
12929 // ++x;
12930 // --x;
12931 // x binop= expr;
12932 // x = x binop expr;
12933 // x = expr binop x;
12934 if (auto *AtomicBody = dyn_cast<Expr>(Val: S)) {
12935 AtomicBody = AtomicBody->IgnoreParenImpCasts();
12936 if (AtomicBody->getType()->isScalarType() ||
12937 AtomicBody->isInstantiationDependent()) {
12938 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
12939 Val: AtomicBody->IgnoreParenImpCasts())) {
12940 // Check for Compound Assignment Operation
12941 Op = BinaryOperator::getOpForCompoundAssignment(
12942 Opc: AtomicCompAssignOp->getOpcode());
12943 OpLoc = AtomicCompAssignOp->getOperatorLoc();
12944 E = AtomicCompAssignOp->getRHS();
12945 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
12946 IsXLHSInRHSPart = true;
12947 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
12948 Val: AtomicBody->IgnoreParenImpCasts())) {
12949 // Check for Binary Operation
12950 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
12951 return true;
12952 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
12953 Val: AtomicBody->IgnoreParenImpCasts())) {
12954 // Check for Unary Operation
12955 if (AtomicUnaryOp->isIncrementDecrementOp()) {
12956 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
12957 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
12958 OpLoc = AtomicUnaryOp->getOperatorLoc();
12959 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
12960 E = SemaRef.ActOnIntegerConstant(Loc: OpLoc, /*uint64_t Val=*/Val: 1).get();
12961 IsXLHSInRHSPart = true;
12962 } else {
12963 ErrorFound = NotAnUnaryIncDecExpression;
12964 ErrorLoc = AtomicUnaryOp->getExprLoc();
12965 ErrorRange = AtomicUnaryOp->getSourceRange();
12966 NoteLoc = AtomicUnaryOp->getOperatorLoc();
12967 NoteRange = SourceRange(NoteLoc, NoteLoc);
12968 }
12969 } else if (!AtomicBody->isInstantiationDependent()) {
12970 ErrorFound = NotABinaryOrUnaryExpression;
12971 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
12972 NoteRange = ErrorRange = AtomicBody->getSourceRange();
12973 } else if (AtomicBody->containsErrors()) {
12974 ErrorFound = NotAValidExpression;
12975 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
12976 NoteRange = ErrorRange = AtomicBody->getSourceRange();
12977 }
12978 } else {
12979 ErrorFound = NotAScalarType;
12980 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
12981 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
12982 }
12983 } else {
12984 ErrorFound = NotAnExpression;
12985 NoteLoc = ErrorLoc = S->getBeginLoc();
12986 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
12987 }
12988 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
12989 SemaRef.Diag(Loc: ErrorLoc, DiagID: DiagId) << ErrorRange;
12990 SemaRef.Diag(Loc: NoteLoc, DiagID: NoteId) << ErrorFound << NoteRange;
12991 return true;
12992 }
12993 if (SemaRef.CurContext->isDependentContext())
12994 E = X = UpdateExpr = nullptr;
12995 if (ErrorFound == NoError && E && X && !E->containsErrors() &&
12996 !X->containsErrors()) {
12997 // Build an update expression of form 'OpaqueValueExpr(x) binop
12998 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
12999 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
13000 auto *OVEX = new (SemaRef.getASTContext())
13001 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_PRValue);
13002 auto *OVEExpr = new (SemaRef.getASTContext())
13003 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_PRValue);
13004 ExprResult Update =
13005 SemaRef.CreateBuiltinBinOp(OpLoc, Opc: Op, LHSExpr: IsXLHSInRHSPart ? OVEX : OVEExpr,
13006 RHSExpr: IsXLHSInRHSPart ? OVEExpr : OVEX);
13007 if (Update.isInvalid())
13008 return true;
13009 Update = SemaRef.PerformImplicitConversion(From: Update.get(), ToType: X->getType(),
13010 Action: AssignmentAction::Casting);
13011 if (Update.isInvalid())
13012 return true;
13013 UpdateExpr = Update.get();
13014 }
13015 return ErrorFound != NoError;
13016}
13017
13018/// Get the node id of the fixed point of an expression \a S.
13019llvm::FoldingSetNodeID getNodeId(ASTContext &Context, const Expr *S) {
13020 llvm::FoldingSetNodeID Id;
13021 S->IgnoreParenImpCasts()->Profile(ID&: Id, Context, Canonical: true);
13022 return Id;
13023}
13024
13025/// Check if two expressions are same.
13026bool checkIfTwoExprsAreSame(ASTContext &Context, const Expr *LHS,
13027 const Expr *RHS) {
13028 return getNodeId(Context, S: LHS) == getNodeId(Context, S: RHS);
13029}
13030
13031class OpenMPAtomicCompareChecker {
13032public:
13033 /// All kinds of errors that can occur in `atomic compare`
13034 enum ErrorTy {
13035 /// Empty compound statement.
13036 NoStmt = 0,
13037 /// More than one statement in a compound statement.
13038 MoreThanOneStmt,
13039 /// Not an assignment binary operator.
13040 NotAnAssignment,
13041 /// Not a conditional operator.
13042 NotCondOp,
13043 /// Wrong false expr. According to the spec, 'x' should be at the false
13044 /// expression of a conditional expression.
13045 WrongFalseExpr,
13046 /// The condition of a conditional expression is not a binary operator.
13047 NotABinaryOp,
13048 /// Invalid binary operator (not <, >, or ==).
13049 InvalidBinaryOp,
13050 /// Invalid comparison (not x == e, e == x, x ordop expr, or expr ordop x).
13051 InvalidComparison,
13052 /// X is not a lvalue.
13053 XNotLValue,
13054 /// Not a scalar.
13055 NotScalar,
13056 /// Not an integer.
13057 NotInteger,
13058 /// 'else' statement is not expected.
13059 UnexpectedElse,
13060 /// Not an equality operator.
13061 NotEQ,
13062 /// Invalid assignment (not v == x).
13063 InvalidAssignment,
13064 /// Not if statement
13065 NotIfStmt,
13066 /// More than two statements in a compound statement.
13067 MoreThanTwoStmts,
13068 /// Not a compound statement.
13069 NotCompoundStmt,
13070 /// No else statement.
13071 NoElse,
13072 /// Not 'if (r)'.
13073 InvalidCondition,
13074 /// No error.
13075 NoError,
13076 };
13077
13078 struct ErrorInfoTy {
13079 ErrorTy Error;
13080 SourceLocation ErrorLoc;
13081 SourceRange ErrorRange;
13082 SourceLocation NoteLoc;
13083 SourceRange NoteRange;
13084 };
13085
13086 OpenMPAtomicCompareChecker(Sema &S) : ContextRef(S.getASTContext()) {}
13087
13088 /// Check if statement \a S is valid for <tt>atomic compare</tt>.
13089 bool checkStmt(Stmt *S, ErrorInfoTy &ErrorInfo);
13090
13091 Expr *getX() const { return X; }
13092 Expr *getE() const { return E; }
13093 Expr *getD() const { return D; }
13094 Expr *getCond() const { return C; }
13095 bool isXBinopExpr() const { return IsXBinopExpr; }
13096
13097protected:
13098 /// Reference to ASTContext
13099 ASTContext &ContextRef;
13100 /// 'x' lvalue part of the source atomic expression.
13101 Expr *X = nullptr;
13102 /// 'expr' or 'e' rvalue part of the source atomic expression.
13103 Expr *E = nullptr;
13104 /// 'd' rvalue part of the source atomic expression.
13105 Expr *D = nullptr;
13106 /// 'cond' part of the source atomic expression. It is in one of the following
13107 /// forms:
13108 /// expr ordop x
13109 /// x ordop expr
13110 /// x == e
13111 /// e == x
13112 Expr *C = nullptr;
13113 /// True if the cond expr is in the form of 'x ordop expr'.
13114 bool IsXBinopExpr = true;
13115
13116 /// Check if it is a valid conditional update statement (cond-update-stmt).
13117 bool checkCondUpdateStmt(IfStmt *S, ErrorInfoTy &ErrorInfo);
13118
13119 /// Check if it is a valid conditional expression statement (cond-expr-stmt).
13120 bool checkCondExprStmt(Stmt *S, ErrorInfoTy &ErrorInfo);
13121
13122 /// Check if all captured values have right type.
13123 bool checkType(ErrorInfoTy &ErrorInfo) const;
13124
13125 static bool CheckValue(const Expr *E, ErrorInfoTy &ErrorInfo,
13126 bool ShouldBeLValue, bool ShouldBeInteger = false) {
13127 if (E->isInstantiationDependent())
13128 return true;
13129
13130 if (ShouldBeLValue && !E->isLValue()) {
13131 ErrorInfo.Error = ErrorTy::XNotLValue;
13132 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = E->getExprLoc();
13133 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = E->getSourceRange();
13134 return false;
13135 }
13136
13137 QualType QTy = E->getType();
13138 if (!QTy->isScalarType()) {
13139 ErrorInfo.Error = ErrorTy::NotScalar;
13140 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = E->getExprLoc();
13141 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = E->getSourceRange();
13142 return false;
13143 }
13144 if (ShouldBeInteger && !QTy->isIntegerType()) {
13145 ErrorInfo.Error = ErrorTy::NotInteger;
13146 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = E->getExprLoc();
13147 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = E->getSourceRange();
13148 return false;
13149 }
13150
13151 return true;
13152 }
13153};
13154
13155bool OpenMPAtomicCompareChecker::checkCondUpdateStmt(IfStmt *S,
13156 ErrorInfoTy &ErrorInfo) {
13157 auto *Then = S->getThen();
13158 if (auto *CS = dyn_cast<CompoundStmt>(Val: Then)) {
13159 if (CS->body_empty()) {
13160 ErrorInfo.Error = ErrorTy::NoStmt;
13161 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
13162 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
13163 return false;
13164 }
13165 if (CS->size() > 1) {
13166 ErrorInfo.Error = ErrorTy::MoreThanOneStmt;
13167 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
13168 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getSourceRange();
13169 return false;
13170 }
13171 Then = CS->body_front();
13172 }
13173
13174 auto *BO = dyn_cast<BinaryOperator>(Val: Then);
13175 if (!BO) {
13176 ErrorInfo.Error = ErrorTy::NotAnAssignment;
13177 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = Then->getBeginLoc();
13178 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Then->getSourceRange();
13179 return false;
13180 }
13181 if (BO->getOpcode() != BO_Assign) {
13182 ErrorInfo.Error = ErrorTy::NotAnAssignment;
13183 ErrorInfo.ErrorLoc = BO->getExprLoc();
13184 ErrorInfo.NoteLoc = BO->getOperatorLoc();
13185 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getSourceRange();
13186 return false;
13187 }
13188
13189 X = BO->getLHS();
13190
13191 auto *Cond = dyn_cast<BinaryOperator>(Val: S->getCond());
13192 auto *Call = dyn_cast<CXXOperatorCallExpr>(Val: S->getCond());
13193 Expr *LHS = nullptr;
13194 Expr *RHS = nullptr;
13195 if (Cond) {
13196 LHS = Cond->getLHS();
13197 RHS = Cond->getRHS();
13198 } else if (Call) {
13199 LHS = Call->getArg(Arg: 0);
13200 RHS = Call->getArg(Arg: 1);
13201 } else {
13202 ErrorInfo.Error = ErrorTy::NotABinaryOp;
13203 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
13204 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getCond()->getSourceRange();
13205 return false;
13206 }
13207
13208 if ((Cond && Cond->getOpcode() == BO_EQ) ||
13209 (Call && Call->getOperator() == OverloadedOperatorKind::OO_EqualEqual)) {
13210 C = S->getCond();
13211 D = BO->getRHS();
13212 if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: LHS)) {
13213 E = RHS;
13214 } else if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS)) {
13215 E = LHS;
13216 } else {
13217 ErrorInfo.Error = ErrorTy::InvalidComparison;
13218 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
13219 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
13220 S->getCond()->getSourceRange();
13221 return false;
13222 }
13223 } else if ((Cond &&
13224 (Cond->getOpcode() == BO_LT || Cond->getOpcode() == BO_GT)) ||
13225 (Call &&
13226 (Call->getOperator() == OverloadedOperatorKind::OO_Less ||
13227 Call->getOperator() == OverloadedOperatorKind::OO_Greater))) {
13228 E = BO->getRHS();
13229 if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: LHS) &&
13230 checkIfTwoExprsAreSame(Context&: ContextRef, LHS: E, RHS)) {
13231 C = S->getCond();
13232 } else if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: E, RHS: LHS) &&
13233 checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS)) {
13234 C = S->getCond();
13235 IsXBinopExpr = false;
13236 } else {
13237 ErrorInfo.Error = ErrorTy::InvalidComparison;
13238 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
13239 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
13240 S->getCond()->getSourceRange();
13241 return false;
13242 }
13243 } else {
13244 ErrorInfo.Error = ErrorTy::InvalidBinaryOp;
13245 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
13246 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getCond()->getSourceRange();
13247 return false;
13248 }
13249
13250 if (S->getElse()) {
13251 ErrorInfo.Error = ErrorTy::UnexpectedElse;
13252 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getElse()->getBeginLoc();
13253 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getElse()->getSourceRange();
13254 return false;
13255 }
13256
13257 return true;
13258}
13259
13260bool OpenMPAtomicCompareChecker::checkCondExprStmt(Stmt *S,
13261 ErrorInfoTy &ErrorInfo) {
13262 auto *BO = dyn_cast<BinaryOperator>(Val: S);
13263 if (!BO) {
13264 ErrorInfo.Error = ErrorTy::NotAnAssignment;
13265 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getBeginLoc();
13266 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getSourceRange();
13267 return false;
13268 }
13269 if (BO->getOpcode() != BO_Assign) {
13270 ErrorInfo.Error = ErrorTy::NotAnAssignment;
13271 ErrorInfo.ErrorLoc = BO->getExprLoc();
13272 ErrorInfo.NoteLoc = BO->getOperatorLoc();
13273 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getSourceRange();
13274 return false;
13275 }
13276
13277 X = BO->getLHS();
13278
13279 auto *CO = dyn_cast<ConditionalOperator>(Val: BO->getRHS()->IgnoreParenImpCasts());
13280 if (!CO) {
13281 ErrorInfo.Error = ErrorTy::NotCondOp;
13282 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = BO->getRHS()->getExprLoc();
13283 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getRHS()->getSourceRange();
13284 return false;
13285 }
13286
13287 if (!checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: CO->getFalseExpr())) {
13288 ErrorInfo.Error = ErrorTy::WrongFalseExpr;
13289 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CO->getFalseExpr()->getExprLoc();
13290 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
13291 CO->getFalseExpr()->getSourceRange();
13292 return false;
13293 }
13294
13295 auto *Cond = dyn_cast<BinaryOperator>(Val: CO->getCond());
13296 auto *Call = dyn_cast<CXXOperatorCallExpr>(Val: CO->getCond());
13297 Expr *LHS = nullptr;
13298 Expr *RHS = nullptr;
13299 if (Cond) {
13300 LHS = Cond->getLHS();
13301 RHS = Cond->getRHS();
13302 } else if (Call) {
13303 LHS = Call->getArg(Arg: 0);
13304 RHS = Call->getArg(Arg: 1);
13305 } else {
13306 ErrorInfo.Error = ErrorTy::NotABinaryOp;
13307 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CO->getCond()->getExprLoc();
13308 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
13309 CO->getCond()->getSourceRange();
13310 return false;
13311 }
13312
13313 if ((Cond && Cond->getOpcode() == BO_EQ) ||
13314 (Call && Call->getOperator() == OverloadedOperatorKind::OO_EqualEqual)) {
13315 C = CO->getCond();
13316 D = CO->getTrueExpr();
13317 if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: LHS)) {
13318 E = RHS;
13319 } else if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS)) {
13320 E = LHS;
13321 } else {
13322 ErrorInfo.Error = ErrorTy::InvalidComparison;
13323 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CO->getCond()->getExprLoc();
13324 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
13325 CO->getCond()->getSourceRange();
13326 return false;
13327 }
13328 } else if ((Cond &&
13329 (Cond->getOpcode() == BO_LT || Cond->getOpcode() == BO_GT)) ||
13330 (Call &&
13331 (Call->getOperator() == OverloadedOperatorKind::OO_Less ||
13332 Call->getOperator() == OverloadedOperatorKind::OO_Greater))) {
13333
13334 E = CO->getTrueExpr();
13335 if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: LHS) &&
13336 checkIfTwoExprsAreSame(Context&: ContextRef, LHS: E, RHS)) {
13337 C = CO->getCond();
13338 } else if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: E, RHS: LHS) &&
13339 checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS)) {
13340 C = CO->getCond();
13341 IsXBinopExpr = false;
13342 } else {
13343 ErrorInfo.Error = ErrorTy::InvalidComparison;
13344 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CO->getCond()->getExprLoc();
13345 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
13346 CO->getCond()->getSourceRange();
13347 return false;
13348 }
13349 } else {
13350 ErrorInfo.Error = ErrorTy::InvalidBinaryOp;
13351 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CO->getCond()->getExprLoc();
13352 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
13353 CO->getCond()->getSourceRange();
13354 return false;
13355 }
13356
13357 return true;
13358}
13359
13360bool OpenMPAtomicCompareChecker::checkType(ErrorInfoTy &ErrorInfo) const {
13361 // 'x' and 'e' cannot be nullptr
13362 assert(X && E && "X and E cannot be nullptr");
13363
13364 if (!CheckValue(E: X, ErrorInfo, ShouldBeLValue: true))
13365 return false;
13366
13367 if (!CheckValue(E, ErrorInfo, ShouldBeLValue: false))
13368 return false;
13369
13370 if (D && !CheckValue(E: D, ErrorInfo, ShouldBeLValue: false))
13371 return false;
13372
13373 return true;
13374}
13375
13376bool OpenMPAtomicCompareChecker::checkStmt(
13377 Stmt *S, OpenMPAtomicCompareChecker::ErrorInfoTy &ErrorInfo) {
13378 auto *CS = dyn_cast<CompoundStmt>(Val: S);
13379 if (CS) {
13380 if (CS->body_empty()) {
13381 ErrorInfo.Error = ErrorTy::NoStmt;
13382 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
13383 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
13384 return false;
13385 }
13386
13387 if (CS->size() != 1) {
13388 ErrorInfo.Error = ErrorTy::MoreThanOneStmt;
13389 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
13390 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
13391 return false;
13392 }
13393 S = CS->body_front();
13394 }
13395
13396 auto Res = false;
13397
13398 if (auto *IS = dyn_cast<IfStmt>(Val: S)) {
13399 // Check if the statement is in one of the following forms
13400 // (cond-update-stmt):
13401 // if (expr ordop x) { x = expr; }
13402 // if (x ordop expr) { x = expr; }
13403 // if (x == e) { x = d; }
13404 Res = checkCondUpdateStmt(S: IS, ErrorInfo);
13405 } else {
13406 // Check if the statement is in one of the following forms (cond-expr-stmt):
13407 // x = expr ordop x ? expr : x;
13408 // x = x ordop expr ? expr : x;
13409 // x = x == e ? d : x;
13410 Res = checkCondExprStmt(S, ErrorInfo);
13411 }
13412
13413 if (!Res)
13414 return false;
13415
13416 return checkType(ErrorInfo);
13417}
13418
13419class OpenMPAtomicCompareCaptureChecker final
13420 : public OpenMPAtomicCompareChecker {
13421public:
13422 OpenMPAtomicCompareCaptureChecker(Sema &S) : OpenMPAtomicCompareChecker(S) {}
13423
13424 Expr *getV() const { return V; }
13425 Expr *getR() const { return R; }
13426 bool isFailOnly() const { return IsFailOnly; }
13427 bool isPostfixUpdate() const { return IsPostfixUpdate; }
13428
13429 /// Check if statement \a S is valid for <tt>atomic compare capture</tt>.
13430 bool checkStmt(Stmt *S, ErrorInfoTy &ErrorInfo);
13431
13432private:
13433 bool checkType(ErrorInfoTy &ErrorInfo);
13434
13435 // NOTE: Form 3, 4, 5 in the following comments mean the 3rd, 4th, and 5th
13436 // form of 'conditional-update-capture-atomic' structured block on the v5.2
13437 // spec p.p. 82:
13438 // (1) { v = x; cond-update-stmt }
13439 // (2) { cond-update-stmt v = x; }
13440 // (3) if(x == e) { x = d; } else { v = x; }
13441 // (4) { r = x == e; if(r) { x = d; } }
13442 // (5) { r = x == e; if(r) { x = d; } else { v = x; } }
13443
13444 /// Check if it is valid 'if(x == e) { x = d; } else { v = x; }' (form 3)
13445 bool checkForm3(IfStmt *S, ErrorInfoTy &ErrorInfo);
13446
13447 /// Check if it is valid '{ r = x == e; if(r) { x = d; } }',
13448 /// or '{ r = x == e; if(r) { x = d; } else { v = x; } }' (form 4 and 5)
13449 bool checkForm45(Stmt *S, ErrorInfoTy &ErrorInfo);
13450
13451 /// 'v' lvalue part of the source atomic expression.
13452 Expr *V = nullptr;
13453 /// 'r' lvalue part of the source atomic expression.
13454 Expr *R = nullptr;
13455 /// If 'v' is only updated when the comparison fails.
13456 bool IsFailOnly = false;
13457 /// If original value of 'x' must be stored in 'v', not an updated one.
13458 bool IsPostfixUpdate = false;
13459};
13460
13461bool OpenMPAtomicCompareCaptureChecker::checkType(ErrorInfoTy &ErrorInfo) {
13462 if (!OpenMPAtomicCompareChecker::checkType(ErrorInfo))
13463 return false;
13464
13465 if (V && !CheckValue(E: V, ErrorInfo, ShouldBeLValue: true))
13466 return false;
13467
13468 if (R && !CheckValue(E: R, ErrorInfo, ShouldBeLValue: true, ShouldBeInteger: true))
13469 return false;
13470
13471 return true;
13472}
13473
13474bool OpenMPAtomicCompareCaptureChecker::checkForm3(IfStmt *S,
13475 ErrorInfoTy &ErrorInfo) {
13476 IsFailOnly = true;
13477
13478 auto *Then = S->getThen();
13479 if (auto *CS = dyn_cast<CompoundStmt>(Val: Then)) {
13480 if (CS->body_empty()) {
13481 ErrorInfo.Error = ErrorTy::NoStmt;
13482 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
13483 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
13484 return false;
13485 }
13486 if (CS->size() > 1) {
13487 ErrorInfo.Error = ErrorTy::MoreThanOneStmt;
13488 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
13489 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
13490 return false;
13491 }
13492 Then = CS->body_front();
13493 }
13494
13495 auto *BO = dyn_cast<BinaryOperator>(Val: Then);
13496 if (!BO) {
13497 ErrorInfo.Error = ErrorTy::NotAnAssignment;
13498 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = Then->getBeginLoc();
13499 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Then->getSourceRange();
13500 return false;
13501 }
13502 if (BO->getOpcode() != BO_Assign) {
13503 ErrorInfo.Error = ErrorTy::NotAnAssignment;
13504 ErrorInfo.ErrorLoc = BO->getExprLoc();
13505 ErrorInfo.NoteLoc = BO->getOperatorLoc();
13506 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getSourceRange();
13507 return false;
13508 }
13509
13510 X = BO->getLHS();
13511 D = BO->getRHS();
13512
13513 auto *Cond = dyn_cast<BinaryOperator>(Val: S->getCond());
13514 auto *Call = dyn_cast<CXXOperatorCallExpr>(Val: S->getCond());
13515 Expr *LHS = nullptr;
13516 Expr *RHS = nullptr;
13517 if (Cond) {
13518 LHS = Cond->getLHS();
13519 RHS = Cond->getRHS();
13520 } else if (Call) {
13521 LHS = Call->getArg(Arg: 0);
13522 RHS = Call->getArg(Arg: 1);
13523 } else {
13524 ErrorInfo.Error = ErrorTy::NotABinaryOp;
13525 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
13526 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getCond()->getSourceRange();
13527 return false;
13528 }
13529 if ((Cond && Cond->getOpcode() != BO_EQ) ||
13530 (Call && Call->getOperator() != OverloadedOperatorKind::OO_EqualEqual)) {
13531 ErrorInfo.Error = ErrorTy::NotEQ;
13532 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
13533 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getCond()->getSourceRange();
13534 return false;
13535 }
13536
13537 if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: LHS)) {
13538 E = RHS;
13539 } else if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS)) {
13540 E = LHS;
13541 } else {
13542 ErrorInfo.Error = ErrorTy::InvalidComparison;
13543 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
13544 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getCond()->getSourceRange();
13545 return false;
13546 }
13547
13548 C = S->getCond();
13549
13550 if (!S->getElse()) {
13551 ErrorInfo.Error = ErrorTy::NoElse;
13552 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getBeginLoc();
13553 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getSourceRange();
13554 return false;
13555 }
13556
13557 auto *Else = S->getElse();
13558 if (auto *CS = dyn_cast<CompoundStmt>(Val: Else)) {
13559 if (CS->body_empty()) {
13560 ErrorInfo.Error = ErrorTy::NoStmt;
13561 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
13562 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
13563 return false;
13564 }
13565 if (CS->size() > 1) {
13566 ErrorInfo.Error = ErrorTy::MoreThanOneStmt;
13567 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
13568 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getSourceRange();
13569 return false;
13570 }
13571 Else = CS->body_front();
13572 }
13573
13574 auto *ElseBO = dyn_cast<BinaryOperator>(Val: Else);
13575 if (!ElseBO) {
13576 ErrorInfo.Error = ErrorTy::NotAnAssignment;
13577 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = Else->getBeginLoc();
13578 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Else->getSourceRange();
13579 return false;
13580 }
13581 if (ElseBO->getOpcode() != BO_Assign) {
13582 ErrorInfo.Error = ErrorTy::NotAnAssignment;
13583 ErrorInfo.ErrorLoc = ElseBO->getExprLoc();
13584 ErrorInfo.NoteLoc = ElseBO->getOperatorLoc();
13585 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ElseBO->getSourceRange();
13586 return false;
13587 }
13588
13589 if (!checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: ElseBO->getRHS())) {
13590 ErrorInfo.Error = ErrorTy::InvalidAssignment;
13591 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = ElseBO->getRHS()->getExprLoc();
13592 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
13593 ElseBO->getRHS()->getSourceRange();
13594 return false;
13595 }
13596
13597 V = ElseBO->getLHS();
13598
13599 return checkType(ErrorInfo);
13600}
13601
13602bool OpenMPAtomicCompareCaptureChecker::checkForm45(Stmt *S,
13603 ErrorInfoTy &ErrorInfo) {
13604 // We don't check here as they should be already done before call this
13605 // function.
13606 auto *CS = cast<CompoundStmt>(Val: S);
13607 assert(CS->size() == 2 && "CompoundStmt size is not expected");
13608 auto *S1 = cast<BinaryOperator>(Val: CS->body_front());
13609 auto *S2 = cast<IfStmt>(Val: CS->body_back());
13610 assert(S1->getOpcode() == BO_Assign && "unexpected binary operator");
13611
13612 if (!checkIfTwoExprsAreSame(Context&: ContextRef, LHS: S1->getLHS(), RHS: S2->getCond())) {
13613 ErrorInfo.Error = ErrorTy::InvalidCondition;
13614 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S2->getCond()->getExprLoc();
13615 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S1->getLHS()->getSourceRange();
13616 return false;
13617 }
13618
13619 R = S1->getLHS();
13620
13621 auto *Then = S2->getThen();
13622 if (auto *ThenCS = dyn_cast<CompoundStmt>(Val: Then)) {
13623 if (ThenCS->body_empty()) {
13624 ErrorInfo.Error = ErrorTy::NoStmt;
13625 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = ThenCS->getBeginLoc();
13626 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ThenCS->getSourceRange();
13627 return false;
13628 }
13629 if (ThenCS->size() > 1) {
13630 ErrorInfo.Error = ErrorTy::MoreThanOneStmt;
13631 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = ThenCS->getBeginLoc();
13632 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ThenCS->getSourceRange();
13633 return false;
13634 }
13635 Then = ThenCS->body_front();
13636 }
13637
13638 auto *ThenBO = dyn_cast<BinaryOperator>(Val: Then);
13639 if (!ThenBO) {
13640 ErrorInfo.Error = ErrorTy::NotAnAssignment;
13641 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S2->getBeginLoc();
13642 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S2->getSourceRange();
13643 return false;
13644 }
13645 if (ThenBO->getOpcode() != BO_Assign) {
13646 ErrorInfo.Error = ErrorTy::NotAnAssignment;
13647 ErrorInfo.ErrorLoc = ThenBO->getExprLoc();
13648 ErrorInfo.NoteLoc = ThenBO->getOperatorLoc();
13649 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ThenBO->getSourceRange();
13650 return false;
13651 }
13652
13653 X = ThenBO->getLHS();
13654 D = ThenBO->getRHS();
13655
13656 auto *BO = cast<BinaryOperator>(Val: S1->getRHS()->IgnoreImpCasts());
13657 if (BO->getOpcode() != BO_EQ) {
13658 ErrorInfo.Error = ErrorTy::NotEQ;
13659 ErrorInfo.ErrorLoc = BO->getExprLoc();
13660 ErrorInfo.NoteLoc = BO->getOperatorLoc();
13661 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getSourceRange();
13662 return false;
13663 }
13664
13665 C = BO;
13666
13667 if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: BO->getLHS())) {
13668 E = BO->getRHS();
13669 } else if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: BO->getRHS())) {
13670 E = BO->getLHS();
13671 } else {
13672 ErrorInfo.Error = ErrorTy::InvalidComparison;
13673 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = BO->getExprLoc();
13674 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getSourceRange();
13675 return false;
13676 }
13677
13678 if (S2->getElse()) {
13679 IsFailOnly = true;
13680
13681 auto *Else = S2->getElse();
13682 if (auto *ElseCS = dyn_cast<CompoundStmt>(Val: Else)) {
13683 if (ElseCS->body_empty()) {
13684 ErrorInfo.Error = ErrorTy::NoStmt;
13685 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = ElseCS->getBeginLoc();
13686 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ElseCS->getSourceRange();
13687 return false;
13688 }
13689 if (ElseCS->size() > 1) {
13690 ErrorInfo.Error = ErrorTy::MoreThanOneStmt;
13691 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = ElseCS->getBeginLoc();
13692 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ElseCS->getSourceRange();
13693 return false;
13694 }
13695 Else = ElseCS->body_front();
13696 }
13697
13698 auto *ElseBO = dyn_cast<BinaryOperator>(Val: Else);
13699 if (!ElseBO) {
13700 ErrorInfo.Error = ErrorTy::NotAnAssignment;
13701 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = Else->getBeginLoc();
13702 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Else->getSourceRange();
13703 return false;
13704 }
13705 if (ElseBO->getOpcode() != BO_Assign) {
13706 ErrorInfo.Error = ErrorTy::NotAnAssignment;
13707 ErrorInfo.ErrorLoc = ElseBO->getExprLoc();
13708 ErrorInfo.NoteLoc = ElseBO->getOperatorLoc();
13709 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ElseBO->getSourceRange();
13710 return false;
13711 }
13712 if (!checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: ElseBO->getRHS())) {
13713 ErrorInfo.Error = ErrorTy::InvalidAssignment;
13714 ErrorInfo.ErrorLoc = ElseBO->getRHS()->getExprLoc();
13715 ErrorInfo.NoteLoc = X->getExprLoc();
13716 ErrorInfo.ErrorRange = ElseBO->getRHS()->getSourceRange();
13717 ErrorInfo.NoteRange = X->getSourceRange();
13718 return false;
13719 }
13720
13721 V = ElseBO->getLHS();
13722 }
13723
13724 return checkType(ErrorInfo);
13725}
13726
13727bool OpenMPAtomicCompareCaptureChecker::checkStmt(Stmt *S,
13728 ErrorInfoTy &ErrorInfo) {
13729 // if(x == e) { x = d; } else { v = x; }
13730 if (auto *IS = dyn_cast<IfStmt>(Val: S))
13731 return checkForm3(S: IS, ErrorInfo);
13732
13733 auto *CS = dyn_cast<CompoundStmt>(Val: S);
13734 if (!CS) {
13735 ErrorInfo.Error = ErrorTy::NotCompoundStmt;
13736 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getBeginLoc();
13737 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getSourceRange();
13738 return false;
13739 }
13740 if (CS->body_empty()) {
13741 ErrorInfo.Error = ErrorTy::NoStmt;
13742 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
13743 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
13744 return false;
13745 }
13746
13747 // { if(x == e) { x = d; } else { v = x; } }
13748 if (CS->size() == 1) {
13749 auto *IS = dyn_cast<IfStmt>(Val: CS->body_front());
13750 if (!IS) {
13751 ErrorInfo.Error = ErrorTy::NotIfStmt;
13752 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->body_front()->getBeginLoc();
13753 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
13754 CS->body_front()->getSourceRange();
13755 return false;
13756 }
13757
13758 return checkForm3(S: IS, ErrorInfo);
13759 } else if (CS->size() == 2) {
13760 auto *S1 = CS->body_front();
13761 auto *S2 = CS->body_back();
13762
13763 Stmt *UpdateStmt = nullptr;
13764 Stmt *CondUpdateStmt = nullptr;
13765 Stmt *CondExprStmt = nullptr;
13766
13767 if (auto *BO = dyn_cast<BinaryOperator>(Val: S1)) {
13768 // It could be one of the following cases:
13769 // { v = x; cond-update-stmt }
13770 // { v = x; cond-expr-stmt }
13771 // { cond-expr-stmt; v = x; }
13772 // form 45
13773 if (isa<BinaryOperator>(Val: BO->getRHS()->IgnoreImpCasts()) ||
13774 isa<ConditionalOperator>(Val: BO->getRHS()->IgnoreImpCasts())) {
13775 // check if form 45
13776 if (isa<IfStmt>(Val: S2))
13777 return checkForm45(S: CS, ErrorInfo);
13778 // { cond-expr-stmt; v = x; }
13779 CondExprStmt = S1;
13780 UpdateStmt = S2;
13781 } else {
13782 IsPostfixUpdate = true;
13783 UpdateStmt = S1;
13784 if (isa<IfStmt>(Val: S2)) {
13785 // { v = x; cond-update-stmt }
13786 CondUpdateStmt = S2;
13787 } else {
13788 // { v = x; cond-expr-stmt }
13789 CondExprStmt = S2;
13790 }
13791 }
13792 } else {
13793 // { cond-update-stmt v = x; }
13794 UpdateStmt = S2;
13795 CondUpdateStmt = S1;
13796 }
13797
13798 auto CheckCondUpdateStmt = [this, &ErrorInfo](Stmt *CUS) {
13799 auto *IS = dyn_cast<IfStmt>(Val: CUS);
13800 if (!IS) {
13801 ErrorInfo.Error = ErrorTy::NotIfStmt;
13802 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CUS->getBeginLoc();
13803 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CUS->getSourceRange();
13804 return false;
13805 }
13806
13807 return checkCondUpdateStmt(S: IS, ErrorInfo);
13808 };
13809
13810 // CheckUpdateStmt has to be called *after* CheckCondUpdateStmt.
13811 auto CheckUpdateStmt = [this, &ErrorInfo](Stmt *US) {
13812 auto *BO = dyn_cast<BinaryOperator>(Val: US);
13813 if (!BO) {
13814 ErrorInfo.Error = ErrorTy::NotAnAssignment;
13815 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = US->getBeginLoc();
13816 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = US->getSourceRange();
13817 return false;
13818 }
13819 if (BO->getOpcode() != BO_Assign) {
13820 ErrorInfo.Error = ErrorTy::NotAnAssignment;
13821 ErrorInfo.ErrorLoc = BO->getExprLoc();
13822 ErrorInfo.NoteLoc = BO->getOperatorLoc();
13823 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getSourceRange();
13824 return false;
13825 }
13826 if (!checkIfTwoExprsAreSame(Context&: ContextRef, LHS: this->X, RHS: BO->getRHS())) {
13827 ErrorInfo.Error = ErrorTy::InvalidAssignment;
13828 ErrorInfo.ErrorLoc = BO->getRHS()->getExprLoc();
13829 ErrorInfo.NoteLoc = this->X->getExprLoc();
13830 ErrorInfo.ErrorRange = BO->getRHS()->getSourceRange();
13831 ErrorInfo.NoteRange = this->X->getSourceRange();
13832 return false;
13833 }
13834
13835 this->V = BO->getLHS();
13836
13837 return true;
13838 };
13839
13840 if (CondUpdateStmt && !CheckCondUpdateStmt(CondUpdateStmt))
13841 return false;
13842 if (CondExprStmt && !checkCondExprStmt(S: CondExprStmt, ErrorInfo))
13843 return false;
13844 if (!CheckUpdateStmt(UpdateStmt))
13845 return false;
13846 } else {
13847 ErrorInfo.Error = ErrorTy::MoreThanTwoStmts;
13848 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
13849 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
13850 return false;
13851 }
13852
13853 return checkType(ErrorInfo);
13854}
13855} // namespace
13856
13857StmtResult SemaOpenMP::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
13858 Stmt *AStmt,
13859 SourceLocation StartLoc,
13860 SourceLocation EndLoc) {
13861 ASTContext &Context = getASTContext();
13862 llvm::omp::Version OMPVersion = getLangOpts().getOpenMPVersion();
13863 // Register location of the first atomic directive.
13864 DSAStack->addAtomicDirectiveLoc(Loc: StartLoc);
13865 if (!AStmt)
13866 return StmtError();
13867
13868 // 1.2.2 OpenMP Language Terminology
13869 // Structured block - An executable statement with a single entry at the
13870 // top and a single exit at the bottom.
13871 // The point of exit cannot be a branch out of the structured block.
13872 // longjmp() and throw() must not violate the entry/exit criteria.
13873 OpenMPClauseKind AtomicKind = OMPC_unknown;
13874 SourceLocation AtomicKindLoc;
13875 OpenMPClauseKind MemOrderKind = OMPC_unknown;
13876 SourceLocation MemOrderLoc;
13877 bool MutexClauseEncountered = false;
13878 llvm::SmallSet<OpenMPClauseKind, 2> EncounteredAtomicKinds;
13879 for (const OMPClause *C : Clauses) {
13880 switch (C->getClauseKind()) {
13881 case OMPC_read:
13882 case OMPC_write:
13883 case OMPC_update:
13884 MutexClauseEncountered = true;
13885 [[fallthrough]];
13886 case OMPC_capture:
13887 case OMPC_compare: {
13888 if (AtomicKind != OMPC_unknown && MutexClauseEncountered) {
13889 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_atomic_several_clauses)
13890 << SourceRange(C->getBeginLoc(), C->getEndLoc());
13891 Diag(Loc: AtomicKindLoc, DiagID: diag::note_omp_previous_mem_order_clause)
13892 << getOpenMPClauseNameForDiag(C: AtomicKind);
13893 } else {
13894 AtomicKind = C->getClauseKind();
13895 AtomicKindLoc = C->getBeginLoc();
13896 if (!EncounteredAtomicKinds.insert(V: C->getClauseKind()).second) {
13897 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_atomic_several_clauses)
13898 << SourceRange(C->getBeginLoc(), C->getEndLoc());
13899 Diag(Loc: AtomicKindLoc, DiagID: diag::note_omp_previous_mem_order_clause)
13900 << getOpenMPClauseNameForDiag(C: AtomicKind);
13901 }
13902 }
13903 break;
13904 }
13905 case OMPC_weak:
13906 case OMPC_fail: {
13907 if (!EncounteredAtomicKinds.contains(V: OMPC_compare)) {
13908 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_atomic_no_compare)
13909 << getOpenMPClauseNameForDiag(C: C->getClauseKind())
13910 << SourceRange(C->getBeginLoc(), C->getEndLoc());
13911 return StmtError();
13912 }
13913 break;
13914 }
13915 case OMPC_seq_cst:
13916 case OMPC_acq_rel:
13917 case OMPC_acquire:
13918 case OMPC_release:
13919 case OMPC_relaxed: {
13920 if (MemOrderKind != OMPC_unknown) {
13921 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_several_mem_order_clauses)
13922 << getOpenMPDirectiveName(D: OMPD_atomic, V: OMPVersion) << 0
13923 << SourceRange(C->getBeginLoc(), C->getEndLoc());
13924 Diag(Loc: MemOrderLoc, DiagID: diag::note_omp_previous_mem_order_clause)
13925 << getOpenMPClauseNameForDiag(C: MemOrderKind);
13926 } else {
13927 MemOrderKind = C->getClauseKind();
13928 MemOrderLoc = C->getBeginLoc();
13929 }
13930 break;
13931 }
13932 // The following clauses are allowed, but we don't need to do anything here.
13933 case OMPC_hint:
13934 break;
13935 default:
13936 llvm_unreachable("unknown clause is encountered");
13937 }
13938 }
13939 bool IsCompareCapture = false;
13940 if (EncounteredAtomicKinds.contains(V: OMPC_compare) &&
13941 EncounteredAtomicKinds.contains(V: OMPC_capture)) {
13942 IsCompareCapture = true;
13943 AtomicKind = OMPC_compare;
13944 }
13945 // OpenMP 5.0, 2.17.7 atomic Construct, Restrictions
13946 // If atomic-clause is read then memory-order-clause must not be acq_rel or
13947 // release.
13948 // If atomic-clause is write then memory-order-clause must not be acq_rel or
13949 // acquire.
13950 // If atomic-clause is update or not present then memory-order-clause must not
13951 // be acq_rel or acquire.
13952 if ((AtomicKind == OMPC_read &&
13953 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_release)) ||
13954 ((AtomicKind == OMPC_write || AtomicKind == OMPC_update ||
13955 AtomicKind == OMPC_unknown) &&
13956 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_acquire))) {
13957 SourceLocation Loc = AtomicKindLoc;
13958 if (AtomicKind == OMPC_unknown)
13959 Loc = StartLoc;
13960 Diag(Loc, DiagID: diag::err_omp_atomic_incompatible_mem_order_clause)
13961 << getOpenMPClauseNameForDiag(C: AtomicKind)
13962 << (AtomicKind == OMPC_unknown ? 1 : 0)
13963 << getOpenMPClauseNameForDiag(C: MemOrderKind);
13964 Diag(Loc: MemOrderLoc, DiagID: diag::note_omp_previous_mem_order_clause)
13965 << getOpenMPClauseNameForDiag(C: MemOrderKind);
13966 }
13967
13968 Stmt *Body = AStmt;
13969 if (auto *EWC = dyn_cast<ExprWithCleanups>(Val: Body))
13970 Body = EWC->getSubExpr();
13971
13972 Expr *X = nullptr;
13973 Expr *V = nullptr;
13974 Expr *E = nullptr;
13975 Expr *UE = nullptr;
13976 Expr *D = nullptr;
13977 Expr *CE = nullptr;
13978 Expr *R = nullptr;
13979 bool IsXLHSInRHSPart = false;
13980 bool IsPostfixUpdate = false;
13981 bool IsFailOnly = false;
13982 // OpenMP [2.12.6, atomic Construct]
13983 // In the next expressions:
13984 // * x and v (as applicable) are both l-value expressions with scalar type.
13985 // * During the execution of an atomic region, multiple syntactic
13986 // occurrences of x must designate the same storage location.
13987 // * Neither of v and expr (as applicable) may access the storage location
13988 // designated by x.
13989 // * Neither of x and expr (as applicable) may access the storage location
13990 // designated by v.
13991 // * expr is an expression with scalar type.
13992 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
13993 // * binop, binop=, ++, and -- are not overloaded operators.
13994 // * The expression x binop expr must be numerically equivalent to x binop
13995 // (expr). This requirement is satisfied if the operators in expr have
13996 // precedence greater than binop, or by using parentheses around expr or
13997 // subexpressions of expr.
13998 // * The expression expr binop x must be numerically equivalent to (expr)
13999 // binop x. This requirement is satisfied if the operators in expr have
14000 // precedence equal to or greater than binop, or by using parentheses around
14001 // expr or subexpressions of expr.
14002 // * For forms that allow multiple occurrences of x, the number of times
14003 // that x is evaluated is unspecified.
14004 if (AtomicKind == OMPC_read) {
14005 enum {
14006 NotAnExpression,
14007 NotAnAssignmentOp,
14008 NotAScalarType,
14009 NotAnLValue,
14010 NoError
14011 } ErrorFound = NoError;
14012 SourceLocation ErrorLoc, NoteLoc;
14013 SourceRange ErrorRange, NoteRange;
14014 // If clause is read:
14015 // v = x;
14016 if (const auto *AtomicBody = dyn_cast<Expr>(Val: Body)) {
14017 const auto *AtomicBinOp =
14018 dyn_cast<BinaryOperator>(Val: AtomicBody->IgnoreParenImpCasts());
14019 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
14020 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
14021 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
14022 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
14023 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
14024 if (!X->isLValue() || !V->isLValue()) {
14025 const Expr *NotLValueExpr = X->isLValue() ? V : X;
14026 ErrorFound = NotAnLValue;
14027 ErrorLoc = AtomicBinOp->getExprLoc();
14028 ErrorRange = AtomicBinOp->getSourceRange();
14029 NoteLoc = NotLValueExpr->getExprLoc();
14030 NoteRange = NotLValueExpr->getSourceRange();
14031 }
14032 } else if (!X->isInstantiationDependent() ||
14033 !V->isInstantiationDependent()) {
14034 const Expr *NotScalarExpr =
14035 (X->isInstantiationDependent() || X->getType()->isScalarType())
14036 ? V
14037 : X;
14038 ErrorFound = NotAScalarType;
14039 ErrorLoc = AtomicBinOp->getExprLoc();
14040 ErrorRange = AtomicBinOp->getSourceRange();
14041 NoteLoc = NotScalarExpr->getExprLoc();
14042 NoteRange = NotScalarExpr->getSourceRange();
14043 }
14044 } else if (!AtomicBody->isInstantiationDependent()) {
14045 ErrorFound = NotAnAssignmentOp;
14046 ErrorLoc = AtomicBody->getExprLoc();
14047 ErrorRange = AtomicBody->getSourceRange();
14048 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
14049 : AtomicBody->getExprLoc();
14050 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
14051 : AtomicBody->getSourceRange();
14052 }
14053 } else {
14054 ErrorFound = NotAnExpression;
14055 NoteLoc = ErrorLoc = Body->getBeginLoc();
14056 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
14057 }
14058 if (ErrorFound != NoError) {
14059 Diag(Loc: ErrorLoc, DiagID: diag::err_omp_atomic_read_not_expression_statement)
14060 << ErrorRange;
14061 Diag(Loc: NoteLoc, DiagID: diag::note_omp_atomic_read_write)
14062 << ErrorFound << NoteRange;
14063 return StmtError();
14064 }
14065 if (SemaRef.CurContext->isDependentContext())
14066 V = X = nullptr;
14067 } else if (AtomicKind == OMPC_write) {
14068 enum {
14069 NotAnExpression,
14070 NotAnAssignmentOp,
14071 NotAScalarType,
14072 NotAnLValue,
14073 NoError
14074 } ErrorFound = NoError;
14075 SourceLocation ErrorLoc, NoteLoc;
14076 SourceRange ErrorRange, NoteRange;
14077 // If clause is write:
14078 // x = expr;
14079 if (const auto *AtomicBody = dyn_cast<Expr>(Val: Body)) {
14080 const auto *AtomicBinOp =
14081 dyn_cast<BinaryOperator>(Val: AtomicBody->IgnoreParenImpCasts());
14082 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
14083 X = AtomicBinOp->getLHS();
14084 E = AtomicBinOp->getRHS();
14085 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
14086 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
14087 if (!X->isLValue()) {
14088 ErrorFound = NotAnLValue;
14089 ErrorLoc = AtomicBinOp->getExprLoc();
14090 ErrorRange = AtomicBinOp->getSourceRange();
14091 NoteLoc = X->getExprLoc();
14092 NoteRange = X->getSourceRange();
14093 }
14094 } else if (!X->isInstantiationDependent() ||
14095 !E->isInstantiationDependent()) {
14096 const Expr *NotScalarExpr =
14097 (X->isInstantiationDependent() || X->getType()->isScalarType())
14098 ? E
14099 : X;
14100 ErrorFound = NotAScalarType;
14101 ErrorLoc = AtomicBinOp->getExprLoc();
14102 ErrorRange = AtomicBinOp->getSourceRange();
14103 NoteLoc = NotScalarExpr->getExprLoc();
14104 NoteRange = NotScalarExpr->getSourceRange();
14105 }
14106 } else if (!AtomicBody->isInstantiationDependent()) {
14107 ErrorFound = NotAnAssignmentOp;
14108 ErrorLoc = AtomicBody->getExprLoc();
14109 ErrorRange = AtomicBody->getSourceRange();
14110 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
14111 : AtomicBody->getExprLoc();
14112 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
14113 : AtomicBody->getSourceRange();
14114 }
14115 } else {
14116 ErrorFound = NotAnExpression;
14117 NoteLoc = ErrorLoc = Body->getBeginLoc();
14118 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
14119 }
14120 if (ErrorFound != NoError) {
14121 Diag(Loc: ErrorLoc, DiagID: diag::err_omp_atomic_write_not_expression_statement)
14122 << ErrorRange;
14123 Diag(Loc: NoteLoc, DiagID: diag::note_omp_atomic_read_write)
14124 << ErrorFound << NoteRange;
14125 return StmtError();
14126 }
14127 if (SemaRef.CurContext->isDependentContext())
14128 E = X = nullptr;
14129 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
14130 // If clause is update:
14131 // x++;
14132 // x--;
14133 // ++x;
14134 // --x;
14135 // x binop= expr;
14136 // x = x binop expr;
14137 // x = expr binop x;
14138 OpenMPAtomicUpdateChecker Checker(SemaRef);
14139 if (Checker.checkStatement(
14140 S: Body,
14141 DiagId: (AtomicKind == OMPC_update)
14142 ? diag::err_omp_atomic_update_not_expression_statement
14143 : diag::err_omp_atomic_not_expression_statement,
14144 NoteId: diag::note_omp_atomic_update))
14145 return StmtError();
14146 if (!SemaRef.CurContext->isDependentContext()) {
14147 E = Checker.getExpr();
14148 X = Checker.getX();
14149 UE = Checker.getUpdateExpr();
14150 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
14151 }
14152 } else if (AtomicKind == OMPC_capture) {
14153 enum {
14154 NotAnAssignmentOp,
14155 NotACompoundStatement,
14156 NotTwoSubstatements,
14157 NotASpecificExpression,
14158 NoError
14159 } ErrorFound = NoError;
14160 SourceLocation ErrorLoc, NoteLoc;
14161 SourceRange ErrorRange, NoteRange;
14162 if (const auto *AtomicBody = dyn_cast<Expr>(Val: Body)) {
14163 // If clause is a capture:
14164 // v = x++;
14165 // v = x--;
14166 // v = ++x;
14167 // v = --x;
14168 // v = x binop= expr;
14169 // v = x = x binop expr;
14170 // v = x = expr binop x;
14171 const auto *AtomicBinOp =
14172 dyn_cast<BinaryOperator>(Val: AtomicBody->IgnoreParenImpCasts());
14173 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
14174 V = AtomicBinOp->getLHS();
14175 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
14176 OpenMPAtomicUpdateChecker Checker(SemaRef);
14177 if (Checker.checkStatement(
14178 S: Body, DiagId: diag::err_omp_atomic_capture_not_expression_statement,
14179 NoteId: diag::note_omp_atomic_update))
14180 return StmtError();
14181 E = Checker.getExpr();
14182 X = Checker.getX();
14183 UE = Checker.getUpdateExpr();
14184 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
14185 IsPostfixUpdate = Checker.isPostfixUpdate();
14186 } else if (!AtomicBody->isInstantiationDependent()) {
14187 ErrorLoc = AtomicBody->getExprLoc();
14188 ErrorRange = AtomicBody->getSourceRange();
14189 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
14190 : AtomicBody->getExprLoc();
14191 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
14192 : AtomicBody->getSourceRange();
14193 ErrorFound = NotAnAssignmentOp;
14194 }
14195 if (ErrorFound != NoError) {
14196 Diag(Loc: ErrorLoc, DiagID: diag::err_omp_atomic_capture_not_expression_statement)
14197 << ErrorRange;
14198 Diag(Loc: NoteLoc, DiagID: diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
14199 return StmtError();
14200 }
14201 if (SemaRef.CurContext->isDependentContext())
14202 UE = V = E = X = nullptr;
14203 } else {
14204 // If clause is a capture:
14205 // { v = x; x = expr; }
14206 // { v = x; x++; }
14207 // { v = x; x--; }
14208 // { v = x; ++x; }
14209 // { v = x; --x; }
14210 // { v = x; x binop= expr; }
14211 // { v = x; x = x binop expr; }
14212 // { v = x; x = expr binop x; }
14213 // { x++; v = x; }
14214 // { x--; v = x; }
14215 // { ++x; v = x; }
14216 // { --x; v = x; }
14217 // { x binop= expr; v = x; }
14218 // { x = x binop expr; v = x; }
14219 // { x = expr binop x; v = x; }
14220 if (auto *CS = dyn_cast<CompoundStmt>(Val: Body)) {
14221 // Check that this is { expr1; expr2; }
14222 if (CS->size() == 2) {
14223 Stmt *First = CS->body_front();
14224 Stmt *Second = CS->body_back();
14225 if (auto *EWC = dyn_cast<ExprWithCleanups>(Val: First))
14226 First = EWC->getSubExpr()->IgnoreParenImpCasts();
14227 if (auto *EWC = dyn_cast<ExprWithCleanups>(Val: Second))
14228 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
14229 // Need to find what subexpression is 'v' and what is 'x'.
14230 OpenMPAtomicUpdateChecker Checker(SemaRef);
14231 bool IsUpdateExprFound = !Checker.checkStatement(S: Second);
14232 BinaryOperator *BinOp = nullptr;
14233 if (IsUpdateExprFound) {
14234 BinOp = dyn_cast<BinaryOperator>(Val: First);
14235 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
14236 }
14237 if (IsUpdateExprFound && !SemaRef.CurContext->isDependentContext()) {
14238 // { v = x; x++; }
14239 // { v = x; x--; }
14240 // { v = x; ++x; }
14241 // { v = x; --x; }
14242 // { v = x; x binop= expr; }
14243 // { v = x; x = x binop expr; }
14244 // { v = x; x = expr binop x; }
14245 // Check that the first expression has form v = x.
14246 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
14247 llvm::FoldingSetNodeID XId, PossibleXId;
14248 Checker.getX()->Profile(ID&: XId, Context, /*Canonical=*/true);
14249 PossibleX->Profile(ID&: PossibleXId, Context, /*Canonical=*/true);
14250 IsUpdateExprFound = XId == PossibleXId;
14251 if (IsUpdateExprFound) {
14252 V = BinOp->getLHS();
14253 X = Checker.getX();
14254 E = Checker.getExpr();
14255 UE = Checker.getUpdateExpr();
14256 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
14257 IsPostfixUpdate = true;
14258 }
14259 }
14260 if (!IsUpdateExprFound) {
14261 IsUpdateExprFound = !Checker.checkStatement(S: First);
14262 BinOp = nullptr;
14263 if (IsUpdateExprFound) {
14264 BinOp = dyn_cast<BinaryOperator>(Val: Second);
14265 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
14266 }
14267 if (IsUpdateExprFound &&
14268 !SemaRef.CurContext->isDependentContext()) {
14269 // { x++; v = x; }
14270 // { x--; v = x; }
14271 // { ++x; v = x; }
14272 // { --x; v = x; }
14273 // { x binop= expr; v = x; }
14274 // { x = x binop expr; v = x; }
14275 // { x = expr binop x; v = x; }
14276 // Check that the second expression has form v = x.
14277 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
14278 llvm::FoldingSetNodeID XId, PossibleXId;
14279 Checker.getX()->Profile(ID&: XId, Context, /*Canonical=*/true);
14280 PossibleX->Profile(ID&: PossibleXId, Context, /*Canonical=*/true);
14281 IsUpdateExprFound = XId == PossibleXId;
14282 if (IsUpdateExprFound) {
14283 V = BinOp->getLHS();
14284 X = Checker.getX();
14285 E = Checker.getExpr();
14286 UE = Checker.getUpdateExpr();
14287 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
14288 IsPostfixUpdate = false;
14289 }
14290 }
14291 }
14292 if (!IsUpdateExprFound) {
14293 // { v = x; x = expr; }
14294 auto *FirstExpr = dyn_cast<Expr>(Val: First);
14295 auto *SecondExpr = dyn_cast<Expr>(Val: Second);
14296 if (!FirstExpr || !SecondExpr ||
14297 !(FirstExpr->isInstantiationDependent() ||
14298 SecondExpr->isInstantiationDependent())) {
14299 auto *FirstBinOp = dyn_cast<BinaryOperator>(Val: First);
14300 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
14301 ErrorFound = NotAnAssignmentOp;
14302 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
14303 : First->getBeginLoc();
14304 NoteRange = ErrorRange = FirstBinOp
14305 ? FirstBinOp->getSourceRange()
14306 : SourceRange(ErrorLoc, ErrorLoc);
14307 } else {
14308 auto *SecondBinOp = dyn_cast<BinaryOperator>(Val: Second);
14309 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
14310 ErrorFound = NotAnAssignmentOp;
14311 NoteLoc = ErrorLoc = SecondBinOp
14312 ? SecondBinOp->getOperatorLoc()
14313 : Second->getBeginLoc();
14314 NoteRange = ErrorRange =
14315 SecondBinOp ? SecondBinOp->getSourceRange()
14316 : SourceRange(ErrorLoc, ErrorLoc);
14317 } else {
14318 Expr *PossibleXRHSInFirst =
14319 FirstBinOp->getRHS()->IgnoreParenImpCasts();
14320 Expr *PossibleXLHSInSecond =
14321 SecondBinOp->getLHS()->IgnoreParenImpCasts();
14322 llvm::FoldingSetNodeID X1Id, X2Id;
14323 PossibleXRHSInFirst->Profile(ID&: X1Id, Context,
14324 /*Canonical=*/true);
14325 PossibleXLHSInSecond->Profile(ID&: X2Id, Context,
14326 /*Canonical=*/true);
14327 IsUpdateExprFound = X1Id == X2Id;
14328 if (IsUpdateExprFound) {
14329 V = FirstBinOp->getLHS();
14330 X = SecondBinOp->getLHS();
14331 E = SecondBinOp->getRHS();
14332 UE = nullptr;
14333 IsXLHSInRHSPart = false;
14334 IsPostfixUpdate = true;
14335 } else {
14336 ErrorFound = NotASpecificExpression;
14337 ErrorLoc = FirstBinOp->getExprLoc();
14338 ErrorRange = FirstBinOp->getSourceRange();
14339 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
14340 NoteRange = SecondBinOp->getRHS()->getSourceRange();
14341 }
14342 }
14343 }
14344 }
14345 }
14346 } else {
14347 NoteLoc = ErrorLoc = Body->getBeginLoc();
14348 NoteRange = ErrorRange =
14349 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
14350 ErrorFound = NotTwoSubstatements;
14351 }
14352 } else {
14353 NoteLoc = ErrorLoc = Body->getBeginLoc();
14354 NoteRange = ErrorRange =
14355 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
14356 ErrorFound = NotACompoundStatement;
14357 }
14358 }
14359 if (ErrorFound != NoError) {
14360 Diag(Loc: ErrorLoc, DiagID: diag::err_omp_atomic_capture_not_compound_statement)
14361 << ErrorRange;
14362 Diag(Loc: NoteLoc, DiagID: diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
14363 return StmtError();
14364 }
14365 if (SemaRef.CurContext->isDependentContext())
14366 UE = V = E = X = nullptr;
14367 } else if (AtomicKind == OMPC_compare) {
14368 if (IsCompareCapture) {
14369 OpenMPAtomicCompareCaptureChecker::ErrorInfoTy ErrorInfo;
14370 OpenMPAtomicCompareCaptureChecker Checker(SemaRef);
14371 if (!Checker.checkStmt(S: Body, ErrorInfo)) {
14372 Diag(Loc: ErrorInfo.ErrorLoc, DiagID: diag::err_omp_atomic_compare_capture)
14373 << ErrorInfo.ErrorRange;
14374 Diag(Loc: ErrorInfo.NoteLoc, DiagID: diag::note_omp_atomic_compare)
14375 << ErrorInfo.Error << ErrorInfo.NoteRange;
14376 return StmtError();
14377 }
14378 X = Checker.getX();
14379 E = Checker.getE();
14380 D = Checker.getD();
14381 CE = Checker.getCond();
14382 V = Checker.getV();
14383 R = Checker.getR();
14384 // We reuse IsXLHSInRHSPart to tell if it is in the form 'x ordop expr'.
14385 IsXLHSInRHSPart = Checker.isXBinopExpr();
14386 IsFailOnly = Checker.isFailOnly();
14387 IsPostfixUpdate = Checker.isPostfixUpdate();
14388 } else {
14389 OpenMPAtomicCompareChecker::ErrorInfoTy ErrorInfo;
14390 OpenMPAtomicCompareChecker Checker(SemaRef);
14391 if (!Checker.checkStmt(S: Body, ErrorInfo)) {
14392 Diag(Loc: ErrorInfo.ErrorLoc, DiagID: diag::err_omp_atomic_compare)
14393 << ErrorInfo.ErrorRange;
14394 Diag(Loc: ErrorInfo.NoteLoc, DiagID: diag::note_omp_atomic_compare)
14395 << ErrorInfo.Error << ErrorInfo.NoteRange;
14396 return StmtError();
14397 }
14398 X = Checker.getX();
14399 E = Checker.getE();
14400 D = Checker.getD();
14401 CE = Checker.getCond();
14402 // The weak clause may only appear if the resulting atomic operation is
14403 // an atomic conditional update for which the comparison tests for
14404 // equality. It was not possible to do this check in
14405 // OpenMPAtomicCompareChecker::checkStmt() as the check for OMPC_weak
14406 // could not be performed (Clauses are not available).
14407 auto *It = find_if(Range&: Clauses, P: [](OMPClause *C) {
14408 return C->getClauseKind() == llvm::omp::Clause::OMPC_weak;
14409 });
14410 if (It != Clauses.end()) {
14411 auto *Cond = dyn_cast<BinaryOperator>(Val: CE);
14412 if (Cond->getOpcode() != BO_EQ) {
14413 ErrorInfo.Error = Checker.ErrorTy::NotAnAssignment;
14414 ErrorInfo.ErrorLoc = Cond->getExprLoc();
14415 ErrorInfo.NoteLoc = Cond->getOperatorLoc();
14416 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Cond->getSourceRange();
14417
14418 Diag(Loc: ErrorInfo.ErrorLoc, DiagID: diag::err_omp_atomic_weak_no_equality)
14419 << ErrorInfo.ErrorRange;
14420 return StmtError();
14421 }
14422 }
14423 // We reuse IsXLHSInRHSPart to tell if it is in the form 'x ordop expr'.
14424 IsXLHSInRHSPart = Checker.isXBinopExpr();
14425 }
14426 }
14427
14428 SemaRef.setFunctionHasBranchProtectedScope();
14429
14430 return OMPAtomicDirective::Create(
14431 C: Context, StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
14432 Exprs: {.X: X, .V: V, .R: R, .E: E, .UE: UE, .D: D, .Cond: CE, .IsXLHSInRHSPart: IsXLHSInRHSPart, .IsPostfixUpdate: IsPostfixUpdate, .IsFailOnly: IsFailOnly});
14433}
14434
14435StmtResult SemaOpenMP::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
14436 Stmt *AStmt,
14437 SourceLocation StartLoc,
14438 SourceLocation EndLoc) {
14439 if (!AStmt)
14440 return StmtError();
14441
14442 if (validateMultidimClauses(SemaRef&: *this, Clauses))
14443 return StmtError();
14444
14445 // Check for conflicting capture kinds on structured bindings.
14446 if (checkClausesForDecompositionConflicts(SemaRef, DKind: OMPD_target, Clauses))
14447 return StmtError();
14448
14449 if (checkOriginalVarMappedButOnlyBindingsUsed(SemaRef, Clauses, Body: AStmt))
14450 return StmtError();
14451
14452 CapturedStmt *CS = setBranchProtectedScope(SemaRef, DKind: OMPD_target, AStmt);
14453
14454 // OpenMP [2.16, Nesting of Regions]
14455 // If specified, a teams construct must be contained within a target
14456 // construct. That target construct must contain no statements or directives
14457 // outside of the teams construct.
14458 if (DSAStack->hasInnerTeamsRegion()) {
14459 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
14460 bool OMPTeamsFound = true;
14461 if (const auto *CS = dyn_cast<CompoundStmt>(Val: S)) {
14462 auto I = CS->body_begin();
14463 while (I != CS->body_end()) {
14464 const auto *OED = dyn_cast<OMPExecutableDirective>(Val: *I);
14465 bool IsTeams = OED && isOpenMPTeamsDirective(DKind: OED->getDirectiveKind());
14466 if (!IsTeams || I != CS->body_begin()) {
14467 OMPTeamsFound = false;
14468 if (IsTeams && I != CS->body_begin()) {
14469 // This is the two teams case. Since the InnerTeamsRegionLoc will
14470 // point to this second one reset the iterator to the other teams.
14471 --I;
14472 }
14473 break;
14474 }
14475 ++I;
14476 }
14477 assert(I != CS->body_end() && "Not found statement");
14478 S = *I;
14479 } else {
14480 const auto *OED = dyn_cast<OMPExecutableDirective>(Val: S);
14481 OMPTeamsFound = OED && isOpenMPTeamsDirective(DKind: OED->getDirectiveKind());
14482 }
14483 if (!OMPTeamsFound) {
14484 Diag(Loc: StartLoc, DiagID: diag::err_omp_target_contains_not_only_teams);
14485 Diag(DSAStack->getInnerTeamsRegionLoc(),
14486 DiagID: diag::note_omp_nested_teams_construct_here);
14487 Diag(Loc: S->getBeginLoc(), DiagID: diag::note_omp_nested_statement_here)
14488 << isa<OMPExecutableDirective>(Val: S);
14489 return StmtError();
14490 }
14491 }
14492
14493 return OMPTargetDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
14494 AssociatedStmt: AStmt);
14495}
14496
14497StmtResult SemaOpenMP::ActOnOpenMPTargetParallelDirective(
14498 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14499 SourceLocation EndLoc) {
14500 if (!AStmt)
14501 return StmtError();
14502
14503 if (validateMultidimClauses(SemaRef&: *this, Clauses))
14504 return StmtError();
14505
14506 // Check for conflicting capture kinds on structured bindings.
14507 if (checkClausesForDecompositionConflicts(SemaRef, DKind: OMPD_target_parallel,
14508 Clauses))
14509 return StmtError();
14510
14511 if (checkOriginalVarMappedButOnlyBindingsUsed(SemaRef, Clauses, Body: AStmt))
14512 return StmtError();
14513
14514 setBranchProtectedScope(SemaRef, DKind: OMPD_target_parallel, AStmt);
14515
14516 return OMPTargetParallelDirective::Create(
14517 C: getASTContext(), StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
14518 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
14519}
14520
14521StmtResult SemaOpenMP::ActOnOpenMPTargetParallelForDirective(
14522 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14523 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14524 if (!AStmt)
14525 return StmtError();
14526
14527 if (validateMultidimClauses(SemaRef&: *this, Clauses))
14528 return StmtError();
14529
14530 if (checkClausesForDecompositionConflicts(SemaRef, DKind: OMPD_target_parallel_for,
14531 Clauses))
14532 return StmtError();
14533
14534 if (checkOriginalVarMappedButOnlyBindingsUsed(SemaRef, Clauses, Body: AStmt))
14535 return StmtError();
14536
14537 CapturedStmt *CS =
14538 setBranchProtectedScope(SemaRef, DKind: OMPD_target_parallel_for, AStmt);
14539
14540 OMPLoopBasedDirective::HelperExprs B;
14541 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
14542 // define the nested loops number.
14543 unsigned NestedLoopCount =
14544 checkOpenMPLoop(DKind: OMPD_target_parallel_for, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14545 OrderedLoopCountExpr: getOrderedNumberExpr(Clauses), AStmt: CS, SemaRef, DSA&: *DSAStack,
14546 VarsWithImplicitDSA, Built&: B);
14547 if (NestedLoopCount == 0)
14548 return StmtError();
14549
14550 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14551 return StmtError();
14552
14553 return OMPTargetParallelForDirective::Create(
14554 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
14555 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
14556}
14557
14558/// Check for existence of a map clause in the list of clauses.
14559static bool hasClauses(ArrayRef<OMPClause *> Clauses,
14560 const OpenMPClauseKind K) {
14561 return llvm::any_of(
14562 Range&: Clauses, P: [K](const OMPClause *C) { return C->getClauseKind() == K; });
14563}
14564
14565template <typename... Params>
14566static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
14567 const Params... ClauseTypes) {
14568 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
14569}
14570
14571/// Check if the variables in the mapping clause are externally visible.
14572static bool isClauseMappable(ArrayRef<OMPClause *> Clauses) {
14573 for (const OMPClause *C : Clauses) {
14574 if (auto *TC = dyn_cast<OMPToClause>(Val: C))
14575 return llvm::all_of(Range: TC->all_decls(), P: [](ValueDecl *VD) {
14576 return !VD || !VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
14577 (VD->isExternallyVisible() &&
14578 VD->getVisibility() != HiddenVisibility);
14579 });
14580 else if (auto *FC = dyn_cast<OMPFromClause>(Val: C))
14581 return llvm::all_of(Range: FC->all_decls(), P: [](ValueDecl *VD) {
14582 return !VD || !VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
14583 (VD->isExternallyVisible() &&
14584 VD->getVisibility() != HiddenVisibility);
14585 });
14586 }
14587
14588 return true;
14589}
14590
14591StmtResult
14592SemaOpenMP::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
14593 Stmt *AStmt, SourceLocation StartLoc,
14594 SourceLocation EndLoc) {
14595 if (!AStmt)
14596 return StmtError();
14597
14598 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
14599
14600 // OpenMP [2.12.2, target data Construct, Restrictions]
14601 // At least one map, use_device_addr or use_device_ptr clause must appear on
14602 // the directive.
14603 if (!hasClauses(Clauses, K: OMPC_map, ClauseTypes: OMPC_use_device_ptr) &&
14604 (getLangOpts().OpenMP < 50 ||
14605 !hasClauses(Clauses, K: OMPC_use_device_addr))) {
14606 StringRef Expected;
14607 if (getLangOpts().OpenMP < 50)
14608 Expected = "'map' or 'use_device_ptr'";
14609 else
14610 Expected = "'map', 'use_device_ptr', or 'use_device_addr'";
14611 llvm::omp::Version OMPVersion = getLangOpts().getOpenMPVersion();
14612 Diag(Loc: StartLoc, DiagID: diag::err_omp_no_clause_for_directive)
14613 << Expected << getOpenMPDirectiveName(D: OMPD_target_data, V: OMPVersion);
14614 return StmtError();
14615 }
14616
14617 SemaRef.setFunctionHasBranchProtectedScope();
14618
14619 return OMPTargetDataDirective::Create(C: getASTContext(), StartLoc, EndLoc,
14620 Clauses, AssociatedStmt: AStmt);
14621}
14622
14623StmtResult SemaOpenMP::ActOnOpenMPTargetEnterDataDirective(
14624 ArrayRef<OMPClause *> Clauses, SourceLocation StartLoc,
14625 SourceLocation EndLoc, Stmt *AStmt) {
14626 if (!AStmt)
14627 return StmtError();
14628
14629 setBranchProtectedScope(SemaRef, DKind: OMPD_target_enter_data, AStmt);
14630
14631 // OpenMP [2.10.2, Restrictions, p. 99]
14632 // At least one map clause must appear on the directive.
14633 if (!hasClauses(Clauses, K: OMPC_map)) {
14634 llvm::omp::Version OMPVersion = getLangOpts().getOpenMPVersion();
14635 Diag(Loc: StartLoc, DiagID: diag::err_omp_no_clause_for_directive)
14636 << "'map'"
14637 << getOpenMPDirectiveName(D: OMPD_target_enter_data, V: OMPVersion);
14638 return StmtError();
14639 }
14640
14641 return OMPTargetEnterDataDirective::Create(C: getASTContext(), StartLoc, EndLoc,
14642 Clauses, AssociatedStmt: AStmt);
14643}
14644
14645StmtResult SemaOpenMP::ActOnOpenMPTargetExitDataDirective(
14646 ArrayRef<OMPClause *> Clauses, SourceLocation StartLoc,
14647 SourceLocation EndLoc, Stmt *AStmt) {
14648 if (!AStmt)
14649 return StmtError();
14650
14651 setBranchProtectedScope(SemaRef, DKind: OMPD_target_exit_data, AStmt);
14652
14653 // OpenMP [2.10.3, Restrictions, p. 102]
14654 // At least one map clause must appear on the directive.
14655 if (!hasClauses(Clauses, K: OMPC_map)) {
14656 llvm::omp::Version OMPVersion = getLangOpts().getOpenMPVersion();
14657 Diag(Loc: StartLoc, DiagID: diag::err_omp_no_clause_for_directive)
14658 << "'map'" << getOpenMPDirectiveName(D: OMPD_target_exit_data, V: OMPVersion);
14659 return StmtError();
14660 }
14661
14662 return OMPTargetExitDataDirective::Create(C: getASTContext(), StartLoc, EndLoc,
14663 Clauses, AssociatedStmt: AStmt);
14664}
14665
14666StmtResult SemaOpenMP::ActOnOpenMPTargetUpdateDirective(
14667 ArrayRef<OMPClause *> Clauses, SourceLocation StartLoc,
14668 SourceLocation EndLoc, Stmt *AStmt) {
14669 if (!AStmt)
14670 return StmtError();
14671
14672 setBranchProtectedScope(SemaRef, DKind: OMPD_target_update, AStmt);
14673
14674 if (!hasClauses(Clauses, K: OMPC_to, ClauseTypes: OMPC_from)) {
14675 Diag(Loc: StartLoc, DiagID: diag::err_omp_at_least_one_motion_clause_required);
14676 return StmtError();
14677 }
14678
14679 if (!isClauseMappable(Clauses)) {
14680 Diag(Loc: StartLoc, DiagID: diag::err_omp_cannot_update_with_internal_linkage);
14681 return StmtError();
14682 }
14683
14684 return OMPTargetUpdateDirective::Create(C: getASTContext(), StartLoc, EndLoc,
14685 Clauses, AssociatedStmt: AStmt);
14686}
14687
14688StmtResult SemaOpenMP::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
14689 Stmt *AStmt,
14690 SourceLocation StartLoc,
14691 SourceLocation EndLoc) {
14692 if (!AStmt)
14693 return StmtError();
14694
14695 if (validateMultidimClauses(SemaRef&: *this, Clauses))
14696 return StmtError();
14697
14698 if (checkClausesForDecompositionConflicts(SemaRef, DKind: OMPD_teams, Clauses))
14699 return StmtError();
14700
14701 // Report affected OpenMP target offloading behavior when in HIP lang-mode.
14702 if (getLangOpts().HIP && (DSAStack->getParentDirective() == OMPD_target))
14703 Diag(Loc: StartLoc, DiagID: diag::warn_hip_omp_target_directives);
14704
14705 setBranchProtectedScope(SemaRef, DKind: OMPD_teams, AStmt);
14706
14707 DSAStack->setParentTeamsRegionLoc(StartLoc);
14708
14709 return OMPTeamsDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
14710 AssociatedStmt: AStmt);
14711}
14712
14713StmtResult SemaOpenMP::ActOnOpenMPCancellationPointDirective(
14714 SourceLocation StartLoc, SourceLocation EndLoc,
14715 OpenMPDirectiveKind CancelRegion) {
14716 if (DSAStack->isParentNowaitRegion()) {
14717 Diag(Loc: StartLoc, DiagID: diag::err_omp_parent_cancel_region_nowait) << 0;
14718 return StmtError();
14719 }
14720 if (DSAStack->isParentOrderedRegion()) {
14721 Diag(Loc: StartLoc, DiagID: diag::err_omp_parent_cancel_region_ordered) << 0;
14722 return StmtError();
14723 }
14724 return OMPCancellationPointDirective::Create(C: getASTContext(), StartLoc,
14725 EndLoc, CancelRegion);
14726}
14727
14728StmtResult SemaOpenMP::ActOnOpenMPCancelDirective(
14729 ArrayRef<OMPClause *> Clauses, SourceLocation StartLoc,
14730 SourceLocation EndLoc, OpenMPDirectiveKind CancelRegion) {
14731 if (DSAStack->isParentNowaitRegion()) {
14732 Diag(Loc: StartLoc, DiagID: diag::err_omp_parent_cancel_region_nowait) << 1;
14733 return StmtError();
14734 }
14735 if (DSAStack->isParentOrderedRegion()) {
14736 Diag(Loc: StartLoc, DiagID: diag::err_omp_parent_cancel_region_ordered) << 1;
14737 return StmtError();
14738 }
14739 DSAStack->setParentCancelRegion(/*Cancel=*/true);
14740 return OMPCancelDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
14741 CancelRegion);
14742}
14743
14744static bool checkReductionClauseWithNogroup(Sema &S,
14745 ArrayRef<OMPClause *> Clauses) {
14746 const OMPClause *ReductionClause = nullptr;
14747 const OMPClause *NogroupClause = nullptr;
14748 for (const OMPClause *C : Clauses) {
14749 if (C->getClauseKind() == OMPC_reduction) {
14750 ReductionClause = C;
14751 if (NogroupClause)
14752 break;
14753 continue;
14754 }
14755 if (C->getClauseKind() == OMPC_nogroup) {
14756 NogroupClause = C;
14757 if (ReductionClause)
14758 break;
14759 continue;
14760 }
14761 }
14762 if (ReductionClause && NogroupClause) {
14763 S.Diag(Loc: ReductionClause->getBeginLoc(), DiagID: diag::err_omp_reduction_with_nogroup)
14764 << SourceRange(NogroupClause->getBeginLoc(),
14765 NogroupClause->getEndLoc());
14766 return true;
14767 }
14768 return false;
14769}
14770
14771StmtResult SemaOpenMP::ActOnOpenMPTaskLoopDirective(
14772 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14773 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14774 if (!AStmt)
14775 return StmtError();
14776
14777 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
14778 OMPLoopBasedDirective::HelperExprs B;
14779 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
14780 // define the nested loops number.
14781 unsigned NestedLoopCount =
14782 checkOpenMPLoop(DKind: OMPD_taskloop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14783 /*OrderedLoopCountExpr=*/nullptr, AStmt, SemaRef,
14784 DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
14785 if (NestedLoopCount == 0)
14786 return StmtError();
14787
14788 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
14789 "omp for loop exprs were not built");
14790
14791 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
14792 // The grainsize clause and num_tasks clause are mutually exclusive and may
14793 // not appear on the same taskloop directive.
14794 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
14795 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
14796 return StmtError();
14797 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
14798 // If a reduction clause is present on the taskloop directive, the nogroup
14799 // clause must not be specified.
14800 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
14801 return StmtError();
14802
14803 SemaRef.setFunctionHasBranchProtectedScope();
14804 return OMPTaskLoopDirective::Create(C: getASTContext(), StartLoc, EndLoc,
14805 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
14806 DSAStack->isCancelRegion());
14807}
14808
14809StmtResult SemaOpenMP::ActOnOpenMPTaskLoopSimdDirective(
14810 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14811 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14812 if (!AStmt)
14813 return StmtError();
14814
14815 CapturedStmt *CS =
14816 setBranchProtectedScope(SemaRef, DKind: OMPD_taskloop_simd, AStmt);
14817
14818 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
14819 OMPLoopBasedDirective::HelperExprs B;
14820 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
14821 // define the nested loops number.
14822 unsigned NestedLoopCount =
14823 checkOpenMPLoop(DKind: OMPD_taskloop_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14824 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
14825 VarsWithImplicitDSA, Built&: B);
14826 if (NestedLoopCount == 0)
14827 return StmtError();
14828
14829 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14830 return StmtError();
14831
14832 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
14833 // The grainsize clause and num_tasks clause are mutually exclusive and may
14834 // not appear on the same taskloop directive.
14835 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
14836 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
14837 return StmtError();
14838 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
14839 // If a reduction clause is present on the taskloop directive, the nogroup
14840 // clause must not be specified.
14841 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
14842 return StmtError();
14843 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14844 return StmtError();
14845
14846 return OMPTaskLoopSimdDirective::Create(C: getASTContext(), StartLoc, EndLoc,
14847 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14848}
14849
14850StmtResult SemaOpenMP::ActOnOpenMPMasterTaskLoopDirective(
14851 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14852 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14853 if (!AStmt)
14854 return StmtError();
14855
14856 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
14857 OMPLoopBasedDirective::HelperExprs B;
14858 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
14859 // define the nested loops number.
14860 unsigned NestedLoopCount =
14861 checkOpenMPLoop(DKind: OMPD_master_taskloop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14862 /*OrderedLoopCountExpr=*/nullptr, AStmt, SemaRef,
14863 DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
14864 if (NestedLoopCount == 0)
14865 return StmtError();
14866
14867 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
14868 "omp for loop exprs were not built");
14869
14870 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
14871 // The grainsize clause and num_tasks clause are mutually exclusive and may
14872 // not appear on the same taskloop directive.
14873 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
14874 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
14875 return StmtError();
14876 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
14877 // If a reduction clause is present on the taskloop directive, the nogroup
14878 // clause must not be specified.
14879 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
14880 return StmtError();
14881
14882 SemaRef.setFunctionHasBranchProtectedScope();
14883 return OMPMasterTaskLoopDirective::Create(C: getASTContext(), StartLoc, EndLoc,
14884 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
14885 DSAStack->isCancelRegion());
14886}
14887
14888StmtResult SemaOpenMP::ActOnOpenMPMaskedTaskLoopDirective(
14889 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14890 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14891 if (!AStmt)
14892 return StmtError();
14893
14894 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
14895 OMPLoopBasedDirective::HelperExprs B;
14896 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
14897 // define the nested loops number.
14898 unsigned NestedLoopCount =
14899 checkOpenMPLoop(DKind: OMPD_masked_taskloop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14900 /*OrderedLoopCountExpr=*/nullptr, AStmt, SemaRef,
14901 DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
14902 if (NestedLoopCount == 0)
14903 return StmtError();
14904
14905 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
14906 "omp for loop exprs were not built");
14907
14908 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
14909 // The grainsize clause and num_tasks clause are mutually exclusive and may
14910 // not appear on the same taskloop directive.
14911 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
14912 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
14913 return StmtError();
14914 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
14915 // If a reduction clause is present on the taskloop directive, the nogroup
14916 // clause must not be specified.
14917 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
14918 return StmtError();
14919
14920 SemaRef.setFunctionHasBranchProtectedScope();
14921 return OMPMaskedTaskLoopDirective::Create(C: getASTContext(), StartLoc, EndLoc,
14922 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
14923 DSAStack->isCancelRegion());
14924}
14925
14926StmtResult SemaOpenMP::ActOnOpenMPMasterTaskLoopSimdDirective(
14927 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14928 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14929 if (!AStmt)
14930 return StmtError();
14931
14932 CapturedStmt *CS =
14933 setBranchProtectedScope(SemaRef, DKind: OMPD_master_taskloop_simd, AStmt);
14934
14935 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
14936 OMPLoopBasedDirective::HelperExprs B;
14937 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
14938 // define the nested loops number.
14939 unsigned NestedLoopCount =
14940 checkOpenMPLoop(DKind: OMPD_master_taskloop_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14941 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
14942 VarsWithImplicitDSA, Built&: B);
14943 if (NestedLoopCount == 0)
14944 return StmtError();
14945
14946 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14947 return StmtError();
14948
14949 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
14950 // The grainsize clause and num_tasks clause are mutually exclusive and may
14951 // not appear on the same taskloop directive.
14952 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
14953 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
14954 return StmtError();
14955 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
14956 // If a reduction clause is present on the taskloop directive, the nogroup
14957 // clause must not be specified.
14958 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
14959 return StmtError();
14960 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14961 return StmtError();
14962
14963 return OMPMasterTaskLoopSimdDirective::Create(
14964 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14965}
14966
14967StmtResult SemaOpenMP::ActOnOpenMPMaskedTaskLoopSimdDirective(
14968 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14969 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14970 if (!AStmt)
14971 return StmtError();
14972
14973 CapturedStmt *CS =
14974 setBranchProtectedScope(SemaRef, DKind: OMPD_masked_taskloop_simd, AStmt);
14975
14976 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
14977 OMPLoopBasedDirective::HelperExprs B;
14978 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
14979 // define the nested loops number.
14980 unsigned NestedLoopCount =
14981 checkOpenMPLoop(DKind: OMPD_masked_taskloop_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14982 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
14983 VarsWithImplicitDSA, Built&: B);
14984 if (NestedLoopCount == 0)
14985 return StmtError();
14986
14987 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14988 return StmtError();
14989
14990 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
14991 // The grainsize clause and num_tasks clause are mutually exclusive and may
14992 // not appear on the same taskloop directive.
14993 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
14994 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
14995 return StmtError();
14996 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
14997 // If a reduction clause is present on the taskloop directive, the nogroup
14998 // clause must not be specified.
14999 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
15000 return StmtError();
15001 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
15002 return StmtError();
15003
15004 return OMPMaskedTaskLoopSimdDirective::Create(
15005 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
15006}
15007
15008StmtResult SemaOpenMP::ActOnOpenMPParallelMasterTaskLoopDirective(
15009 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
15010 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
15011 if (!AStmt)
15012 return StmtError();
15013
15014 CapturedStmt *CS =
15015 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_master_taskloop, AStmt);
15016
15017 OMPLoopBasedDirective::HelperExprs B;
15018 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
15019 // define the nested loops number.
15020 unsigned NestedLoopCount = checkOpenMPLoop(
15021 DKind: OMPD_parallel_master_taskloop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
15022 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
15023 VarsWithImplicitDSA, Built&: B);
15024 if (NestedLoopCount == 0)
15025 return StmtError();
15026
15027 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
15028 "omp for loop exprs were not built");
15029
15030 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
15031 // The grainsize clause and num_tasks clause are mutually exclusive and may
15032 // not appear on the same taskloop directive.
15033 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
15034 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
15035 return StmtError();
15036 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
15037 // If a reduction clause is present on the taskloop directive, the nogroup
15038 // clause must not be specified.
15039 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
15040 return StmtError();
15041
15042 return OMPParallelMasterTaskLoopDirective::Create(
15043 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
15044 DSAStack->isCancelRegion());
15045}
15046
15047StmtResult SemaOpenMP::ActOnOpenMPParallelMaskedTaskLoopDirective(
15048 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
15049 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
15050 if (!AStmt)
15051 return StmtError();
15052
15053 CapturedStmt *CS =
15054 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_masked_taskloop, AStmt);
15055
15056 OMPLoopBasedDirective::HelperExprs B;
15057 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
15058 // define the nested loops number.
15059 unsigned NestedLoopCount = checkOpenMPLoop(
15060 DKind: OMPD_parallel_masked_taskloop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
15061 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
15062 VarsWithImplicitDSA, Built&: B);
15063 if (NestedLoopCount == 0)
15064 return StmtError();
15065
15066 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
15067 "omp for loop exprs were not built");
15068
15069 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
15070 // The grainsize clause and num_tasks clause are mutually exclusive and may
15071 // not appear on the same taskloop directive.
15072 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
15073 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
15074 return StmtError();
15075 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
15076 // If a reduction clause is present on the taskloop directive, the nogroup
15077 // clause must not be specified.
15078 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
15079 return StmtError();
15080
15081 return OMPParallelMaskedTaskLoopDirective::Create(
15082 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
15083 DSAStack->isCancelRegion());
15084}
15085
15086StmtResult SemaOpenMP::ActOnOpenMPParallelMasterTaskLoopSimdDirective(
15087 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
15088 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
15089 if (!AStmt)
15090 return StmtError();
15091
15092 CapturedStmt *CS = setBranchProtectedScope(
15093 SemaRef, DKind: OMPD_parallel_master_taskloop_simd, AStmt);
15094
15095 OMPLoopBasedDirective::HelperExprs B;
15096 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
15097 // define the nested loops number.
15098 unsigned NestedLoopCount = checkOpenMPLoop(
15099 DKind: OMPD_parallel_master_taskloop_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
15100 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
15101 VarsWithImplicitDSA, Built&: B);
15102 if (NestedLoopCount == 0)
15103 return StmtError();
15104
15105 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
15106 return StmtError();
15107
15108 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
15109 // The grainsize clause and num_tasks clause are mutually exclusive and may
15110 // not appear on the same taskloop directive.
15111 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
15112 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
15113 return StmtError();
15114 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
15115 // If a reduction clause is present on the taskloop directive, the nogroup
15116 // clause must not be specified.
15117 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
15118 return StmtError();
15119 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
15120 return StmtError();
15121
15122 return OMPParallelMasterTaskLoopSimdDirective::Create(
15123 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
15124}
15125
15126StmtResult SemaOpenMP::ActOnOpenMPParallelMaskedTaskLoopSimdDirective(
15127 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
15128 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
15129 if (!AStmt)
15130 return StmtError();
15131
15132 CapturedStmt *CS = setBranchProtectedScope(
15133 SemaRef, DKind: OMPD_parallel_masked_taskloop_simd, AStmt);
15134
15135 OMPLoopBasedDirective::HelperExprs B;
15136 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
15137 // define the nested loops number.
15138 unsigned NestedLoopCount = checkOpenMPLoop(
15139 DKind: OMPD_parallel_masked_taskloop_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
15140 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
15141 VarsWithImplicitDSA, Built&: B);
15142 if (NestedLoopCount == 0)
15143 return StmtError();
15144
15145 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
15146 return StmtError();
15147
15148 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
15149 // The grainsize clause and num_tasks clause are mutually exclusive and may
15150 // not appear on the same taskloop directive.
15151 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
15152 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
15153 return StmtError();
15154 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
15155 // If a reduction clause is present on the taskloop directive, the nogroup
15156 // clause must not be specified.
15157 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
15158 return StmtError();
15159 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
15160 return StmtError();
15161
15162 return OMPParallelMaskedTaskLoopSimdDirective::Create(
15163 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
15164}
15165
15166StmtResult SemaOpenMP::ActOnOpenMPDistributeDirective(
15167 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
15168 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
15169 if (!AStmt)
15170 return StmtError();
15171
15172 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
15173 OMPLoopBasedDirective::HelperExprs B;
15174 // In presence of clause 'collapse' with number of loops, it will
15175 // define the nested loops number.
15176 unsigned NestedLoopCount =
15177 checkOpenMPLoop(DKind: OMPD_distribute, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
15178 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt,
15179 SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
15180 if (NestedLoopCount == 0)
15181 return StmtError();
15182
15183 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
15184 "omp for loop exprs were not built");
15185
15186 SemaRef.setFunctionHasBranchProtectedScope();
15187 auto *DistributeDirective = OMPDistributeDirective::Create(
15188 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
15189 return DistributeDirective;
15190}
15191
15192StmtResult SemaOpenMP::ActOnOpenMPDistributeParallelForDirective(
15193 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
15194 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
15195 if (!AStmt)
15196 return StmtError();
15197
15198 CapturedStmt *CS =
15199 setBranchProtectedScope(SemaRef, DKind: OMPD_distribute_parallel_for, AStmt);
15200
15201 OMPLoopBasedDirective::HelperExprs B;
15202 // In presence of clause 'collapse' with number of loops, it will
15203 // define the nested loops number.
15204 unsigned NestedLoopCount = checkOpenMPLoop(
15205 DKind: OMPD_distribute_parallel_for, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
15206 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
15207 VarsWithImplicitDSA, Built&: B);
15208 if (NestedLoopCount == 0)
15209 return StmtError();
15210
15211 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
15212 "omp for loop exprs were not built");
15213
15214 return OMPDistributeParallelForDirective::Create(
15215 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
15216 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
15217}
15218
15219StmtResult SemaOpenMP::ActOnOpenMPDistributeParallelForSimdDirective(
15220 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
15221 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
15222 if (!AStmt)
15223 return StmtError();
15224
15225 CapturedStmt *CS = setBranchProtectedScope(
15226 SemaRef, DKind: OMPD_distribute_parallel_for_simd, AStmt);
15227
15228 OMPLoopBasedDirective::HelperExprs B;
15229 // In presence of clause 'collapse' with number of loops, it will
15230 // define the nested loops number.
15231 unsigned NestedLoopCount = checkOpenMPLoop(
15232 DKind: OMPD_distribute_parallel_for_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
15233 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
15234 VarsWithImplicitDSA, Built&: B);
15235 if (NestedLoopCount == 0)
15236 return StmtError();
15237
15238 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
15239 return StmtError();
15240
15241 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
15242 return StmtError();
15243
15244 return OMPDistributeParallelForSimdDirective::Create(
15245 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
15246}
15247
15248StmtResult SemaOpenMP::ActOnOpenMPDistributeSimdDirective(
15249 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
15250 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
15251 if (!AStmt)
15252 return StmtError();
15253
15254 CapturedStmt *CS =
15255 setBranchProtectedScope(SemaRef, DKind: OMPD_distribute_simd, AStmt);
15256
15257 OMPLoopBasedDirective::HelperExprs B;
15258 // In presence of clause 'collapse' with number of loops, it will
15259 // define the nested loops number.
15260 unsigned NestedLoopCount =
15261 checkOpenMPLoop(DKind: OMPD_distribute_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
15262 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS,
15263 SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
15264 if (NestedLoopCount == 0)
15265 return StmtError();
15266
15267 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
15268 return StmtError();
15269
15270 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
15271 return StmtError();
15272
15273 return OMPDistributeSimdDirective::Create(C: getASTContext(), StartLoc, EndLoc,
15274 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
15275}
15276
15277StmtResult SemaOpenMP::ActOnOpenMPTargetParallelForSimdDirective(
15278 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
15279 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
15280 if (!AStmt)
15281 return StmtError();
15282
15283 if (validateMultidimClauses(SemaRef&: *this, Clauses))
15284 return StmtError();
15285
15286 if (checkClausesForDecompositionConflicts(
15287 SemaRef, DKind: OMPD_target_parallel_for_simd, Clauses))
15288 return StmtError();
15289
15290 if (checkOriginalVarMappedButOnlyBindingsUsed(SemaRef, Clauses, Body: AStmt))
15291 return StmtError();
15292
15293 CapturedStmt *CS =
15294 setBranchProtectedScope(SemaRef, DKind: OMPD_target_parallel_for_simd, AStmt);
15295
15296 OMPLoopBasedDirective::HelperExprs B;
15297 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
15298 // define the nested loops number.
15299 unsigned NestedLoopCount = checkOpenMPLoop(
15300 DKind: OMPD_target_parallel_for_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
15301 OrderedLoopCountExpr: getOrderedNumberExpr(Clauses), AStmt: CS, SemaRef, DSA&: *DSAStack,
15302 VarsWithImplicitDSA, Built&: B);
15303 if (NestedLoopCount == 0)
15304 return StmtError();
15305
15306 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
15307 return StmtError();
15308
15309 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
15310 return StmtError();
15311
15312 return OMPTargetParallelForSimdDirective::Create(
15313 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
15314}
15315
15316StmtResult SemaOpenMP::ActOnOpenMPTargetSimdDirective(
15317 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
15318 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
15319 if (!AStmt)
15320 return StmtError();
15321
15322 if (validateMultidimClauses(SemaRef&: *this, Clauses))
15323 return StmtError();
15324
15325 if (checkClausesForDecompositionConflicts(SemaRef, DKind: OMPD_target_simd, Clauses))
15326 return StmtError();
15327
15328 if (checkOriginalVarMappedButOnlyBindingsUsed(SemaRef, Clauses, Body: AStmt))
15329 return StmtError();
15330
15331 CapturedStmt *CS = setBranchProtectedScope(SemaRef, DKind: OMPD_target_simd, AStmt);
15332
15333 OMPLoopBasedDirective::HelperExprs B;
15334 // In presence of clause 'collapse' with number of loops, it will define the
15335 // nested loops number.
15336 unsigned NestedLoopCount =
15337 checkOpenMPLoop(DKind: OMPD_target_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
15338 OrderedLoopCountExpr: getOrderedNumberExpr(Clauses), AStmt: CS, SemaRef, DSA&: *DSAStack,
15339 VarsWithImplicitDSA, Built&: B);
15340 if (NestedLoopCount == 0)
15341 return StmtError();
15342
15343 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
15344 return StmtError();
15345
15346 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
15347 return StmtError();
15348
15349 return OMPTargetSimdDirective::Create(C: getASTContext(), StartLoc, EndLoc,
15350 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
15351}
15352
15353StmtResult SemaOpenMP::ActOnOpenMPTeamsDistributeDirective(
15354 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
15355 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
15356 if (!AStmt)
15357 return StmtError();
15358
15359 if (validateMultidimClauses(SemaRef&: *this, Clauses))
15360 return StmtError();
15361
15362 if (checkClausesForDecompositionConflicts(SemaRef, DKind: OMPD_teams_distribute,
15363 Clauses))
15364 return StmtError();
15365
15366 CapturedStmt *CS =
15367 setBranchProtectedScope(SemaRef, DKind: OMPD_teams_distribute, AStmt);
15368
15369 OMPLoopBasedDirective::HelperExprs B;
15370 // In presence of clause 'collapse' with number of loops, it will
15371 // define the nested loops number.
15372 unsigned NestedLoopCount =
15373 checkOpenMPLoop(DKind: OMPD_teams_distribute, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
15374 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS,
15375 SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
15376 if (NestedLoopCount == 0)
15377 return StmtError();
15378
15379 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
15380 "omp teams distribute loop exprs were not built");
15381
15382 DSAStack->setParentTeamsRegionLoc(StartLoc);
15383
15384 return OMPTeamsDistributeDirective::Create(
15385 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
15386}
15387
15388StmtResult SemaOpenMP::ActOnOpenMPTeamsDistributeSimdDirective(
15389 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
15390 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
15391 if (!AStmt)
15392 return StmtError();
15393
15394 if (validateMultidimClauses(SemaRef&: *this, Clauses))
15395 return StmtError();
15396 if (checkClausesForDecompositionConflicts(SemaRef, DKind: OMPD_teams_distribute_simd,
15397 Clauses))
15398 return StmtError();
15399
15400 CapturedStmt *CS =
15401 setBranchProtectedScope(SemaRef, DKind: OMPD_teams_distribute_simd, AStmt);
15402
15403 OMPLoopBasedDirective::HelperExprs B;
15404 // In presence of clause 'collapse' with number of loops, it will
15405 // define the nested loops number.
15406 unsigned NestedLoopCount = checkOpenMPLoop(
15407 DKind: OMPD_teams_distribute_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
15408 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
15409 VarsWithImplicitDSA, Built&: B);
15410 if (NestedLoopCount == 0)
15411 return StmtError();
15412
15413 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
15414 return StmtError();
15415
15416 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
15417 return StmtError();
15418
15419 DSAStack->setParentTeamsRegionLoc(StartLoc);
15420
15421 return OMPTeamsDistributeSimdDirective::Create(
15422 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
15423}
15424
15425StmtResult SemaOpenMP::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
15426 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
15427 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
15428 if (!AStmt)
15429 return StmtError();
15430
15431 if (validateMultidimClauses(SemaRef&: *this, Clauses))
15432 return StmtError();
15433
15434 if (checkClausesForDecompositionConflicts(
15435 SemaRef, DKind: OMPD_teams_distribute_parallel_for_simd, Clauses))
15436 return StmtError();
15437
15438 CapturedStmt *CS = setBranchProtectedScope(
15439 SemaRef, DKind: OMPD_teams_distribute_parallel_for_simd, AStmt);
15440
15441 OMPLoopBasedDirective::HelperExprs B;
15442 // In presence of clause 'collapse' with number of loops, it will
15443 // define the nested loops number.
15444 unsigned NestedLoopCount = checkOpenMPLoop(
15445 DKind: OMPD_teams_distribute_parallel_for_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
15446 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
15447 VarsWithImplicitDSA, Built&: B);
15448 if (NestedLoopCount == 0)
15449 return StmtError();
15450
15451 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
15452 return StmtError();
15453
15454 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
15455 return StmtError();
15456
15457 DSAStack->setParentTeamsRegionLoc(StartLoc);
15458
15459 return OMPTeamsDistributeParallelForSimdDirective::Create(
15460 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
15461}
15462
15463StmtResult SemaOpenMP::ActOnOpenMPTeamsDistributeParallelForDirective(
15464 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
15465 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
15466 if (!AStmt)
15467 return StmtError();
15468
15469 if (validateMultidimClauses(SemaRef&: *this, Clauses))
15470 return StmtError();
15471
15472 if (checkClausesForDecompositionConflicts(
15473 SemaRef, DKind: OMPD_teams_distribute_parallel_for, Clauses))
15474 return StmtError();
15475
15476 CapturedStmt *CS = setBranchProtectedScope(
15477 SemaRef, DKind: OMPD_teams_distribute_parallel_for, AStmt);
15478
15479 OMPLoopBasedDirective::HelperExprs B;
15480 // In presence of clause 'collapse' with number of loops, it will
15481 // define the nested loops number.
15482 unsigned NestedLoopCount = checkOpenMPLoop(
15483 DKind: OMPD_teams_distribute_parallel_for, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
15484 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
15485 VarsWithImplicitDSA, Built&: B);
15486
15487 if (NestedLoopCount == 0)
15488 return StmtError();
15489
15490 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
15491 "omp for loop exprs were not built");
15492
15493 DSAStack->setParentTeamsRegionLoc(StartLoc);
15494
15495 return OMPTeamsDistributeParallelForDirective::Create(
15496 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
15497 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
15498}
15499
15500StmtResult SemaOpenMP::ActOnOpenMPTargetTeamsDirective(
15501 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
15502 SourceLocation EndLoc) {
15503 if (!AStmt)
15504 return StmtError();
15505
15506 if (checkClausesForDecompositionConflicts(SemaRef, DKind: OMPD_target_teams,
15507 Clauses))
15508 return StmtError();
15509
15510 if (checkOriginalVarMappedButOnlyBindingsUsed(SemaRef, Clauses, Body: AStmt))
15511 return StmtError();
15512
15513 setBranchProtectedScope(SemaRef, DKind: OMPD_target_teams, AStmt);
15514
15515 if (validateMultidimClauses(SemaRef&: *this, Clauses, /*MayHaveBareClause=*/true))
15516 return StmtError();
15517
15518 return OMPTargetTeamsDirective::Create(C: getASTContext(), StartLoc, EndLoc,
15519 Clauses, AssociatedStmt: AStmt);
15520}
15521
15522StmtResult SemaOpenMP::ActOnOpenMPTargetTeamsDistributeDirective(
15523 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
15524 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
15525 if (!AStmt)
15526 return StmtError();
15527
15528 if (validateMultidimClauses(SemaRef&: *this, Clauses))
15529 return StmtError();
15530
15531 if (checkClausesForDecompositionConflicts(
15532 SemaRef, DKind: OMPD_target_teams_distribute, Clauses))
15533 return StmtError();
15534
15535 if (checkOriginalVarMappedButOnlyBindingsUsed(SemaRef, Clauses, Body: AStmt))
15536 return StmtError();
15537
15538 CapturedStmt *CS =
15539 setBranchProtectedScope(SemaRef, DKind: OMPD_target_teams_distribute, AStmt);
15540
15541 OMPLoopBasedDirective::HelperExprs B;
15542 // In presence of clause 'collapse' with number of loops, it will
15543 // define the nested loops number.
15544 unsigned NestedLoopCount = checkOpenMPLoop(
15545 DKind: OMPD_target_teams_distribute, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
15546 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
15547 VarsWithImplicitDSA, Built&: B);
15548 if (NestedLoopCount == 0)
15549 return StmtError();
15550
15551 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
15552 "omp target teams distribute loop exprs were not built");
15553
15554 return OMPTargetTeamsDistributeDirective::Create(
15555 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
15556}
15557
15558StmtResult SemaOpenMP::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
15559 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
15560 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
15561 if (!AStmt)
15562 return StmtError();
15563
15564 if (validateMultidimClauses(SemaRef&: *this, Clauses))
15565 return StmtError();
15566
15567 if (checkClausesForDecompositionConflicts(
15568 SemaRef, DKind: OMPD_target_teams_distribute_parallel_for, Clauses))
15569 return StmtError();
15570
15571 if (checkOriginalVarMappedButOnlyBindingsUsed(SemaRef, Clauses, Body: AStmt))
15572 return StmtError();
15573
15574 CapturedStmt *CS = setBranchProtectedScope(
15575 SemaRef, DKind: OMPD_target_teams_distribute_parallel_for, AStmt);
15576
15577 OMPLoopBasedDirective::HelperExprs B;
15578 // In presence of clause 'collapse' with number of loops, it will
15579 // define the nested loops number.
15580 unsigned NestedLoopCount = checkOpenMPLoop(
15581 DKind: OMPD_target_teams_distribute_parallel_for, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
15582 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
15583 VarsWithImplicitDSA, Built&: B);
15584 if (NestedLoopCount == 0)
15585 return StmtError();
15586
15587 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
15588 return StmtError();
15589
15590 return OMPTargetTeamsDistributeParallelForDirective::Create(
15591 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
15592 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
15593}
15594
15595StmtResult SemaOpenMP::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
15596 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
15597 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
15598 if (!AStmt)
15599 return StmtError();
15600
15601 if (validateMultidimClauses(SemaRef&: *this, Clauses))
15602 return StmtError();
15603
15604 if (checkClausesForDecompositionConflicts(
15605 SemaRef, DKind: OMPD_target_teams_distribute_parallel_for_simd, Clauses))
15606 return StmtError();
15607
15608 if (checkOriginalVarMappedButOnlyBindingsUsed(SemaRef, Clauses, Body: AStmt))
15609 return StmtError();
15610
15611 CapturedStmt *CS = setBranchProtectedScope(
15612 SemaRef, DKind: OMPD_target_teams_distribute_parallel_for_simd, AStmt);
15613
15614 OMPLoopBasedDirective::HelperExprs B;
15615 // In presence of clause 'collapse' with number of loops, it will
15616 // define the nested loops number.
15617 unsigned NestedLoopCount =
15618 checkOpenMPLoop(DKind: OMPD_target_teams_distribute_parallel_for_simd,
15619 CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
15620 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS,
15621 SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
15622 if (NestedLoopCount == 0)
15623 return StmtError();
15624
15625 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
15626 return StmtError();
15627
15628 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
15629 return StmtError();
15630
15631 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
15632 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
15633}
15634
15635StmtResult SemaOpenMP::ActOnOpenMPTargetTeamsDistributeSimdDirective(
15636 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
15637 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
15638 if (!AStmt)
15639 return StmtError();
15640
15641 if (validateMultidimClauses(SemaRef&: *this, Clauses))
15642 return StmtError();
15643
15644 if (checkClausesForDecompositionConflicts(
15645 SemaRef, DKind: OMPD_target_teams_distribute_simd, Clauses))
15646 return StmtError();
15647
15648 if (checkOriginalVarMappedButOnlyBindingsUsed(SemaRef, Clauses, Body: AStmt))
15649 return StmtError();
15650
15651 CapturedStmt *CS = setBranchProtectedScope(
15652 SemaRef, DKind: OMPD_target_teams_distribute_simd, AStmt);
15653
15654 OMPLoopBasedDirective::HelperExprs B;
15655 // In presence of clause 'collapse' with number of loops, it will
15656 // define the nested loops number.
15657 unsigned NestedLoopCount = checkOpenMPLoop(
15658 DKind: OMPD_target_teams_distribute_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
15659 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
15660 VarsWithImplicitDSA, Built&: B);
15661 if (NestedLoopCount == 0)
15662 return StmtError();
15663
15664 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
15665 return StmtError();
15666
15667 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
15668 return StmtError();
15669
15670 return OMPTargetTeamsDistributeSimdDirective::Create(
15671 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
15672}
15673
15674/// Updates OriginalInits by checking Transform against loop transformation
15675/// directives and appending their pre-inits if a match is found.
15676static void updatePreInits(OMPLoopTransformationDirective *Transform,
15677 SmallVectorImpl<Stmt *> &PreInits) {
15678 Stmt *Dir = Transform->getDirective();
15679 switch (Dir->getStmtClass()) {
15680#define STMT(CLASS, PARENT)
15681#define ABSTRACT_STMT(CLASS)
15682#define COMMON_OMP_LOOP_TRANSFORMATION(CLASS, PARENT) \
15683 case Stmt::CLASS##Class: \
15684 appendFlattenedStmtList(PreInits, \
15685 static_cast<const CLASS *>(Dir)->getPreInits()); \
15686 break;
15687#define OMPCANONICALLOOPNESTTRANSFORMATIONDIRECTIVE(CLASS, PARENT) \
15688 COMMON_OMP_LOOP_TRANSFORMATION(CLASS, PARENT)
15689#define OMPCANONICALLOOPSEQUENCETRANSFORMATIONDIRECTIVE(CLASS, PARENT) \
15690 COMMON_OMP_LOOP_TRANSFORMATION(CLASS, PARENT)
15691#include "clang/AST/StmtNodes.inc"
15692#undef COMMON_OMP_LOOP_TRANSFORMATION
15693 default:
15694 llvm_unreachable("Not a loop transformation");
15695 }
15696}
15697
15698bool SemaOpenMP::checkTransformableLoopNest(
15699 OpenMPDirectiveKind Kind, Stmt *AStmt, int NumLoops,
15700 SmallVectorImpl<OMPLoopBasedDirective::HelperExprs> &LoopHelpers,
15701 Stmt *&Body, SmallVectorImpl<SmallVector<Stmt *>> &OriginalInits,
15702 SmallVectorImpl<Stmt *> *NestedFinals) {
15703 if (NestedFinals)
15704 NestedFinals->assign(NumElts: NumLoops, Elt: nullptr);
15705 OriginalInits.emplace_back();
15706 // Only tile and stripe re-emit the statements before the nested loop in a
15707 // nested transformation's generated loop (see appendStmtsBeforeNestedLoop);
15708 // other constructs would silently drop them.
15709 bool RelaxNestForPeeledTransformation =
15710 Kind == OMPD_tile || Kind == OMPD_stripe;
15711 bool Result = OMPLoopBasedDirective::doForAllLoops(
15712 CurStmt: AStmt->IgnoreContainers(), /*TryImperfectlyNestedLoops=*/false, NumLoops,
15713 Callback: [this, &LoopHelpers, &Body, &OriginalInits,
15714 Kind](unsigned Cnt, Stmt *CurStmt, Stmt *HintWrapper) {
15715 // The start of this loop is a floor set in the enclosing tile's body.
15716 // Loop analysis reads a start only once, so the nest would repeat the
15717 // first tile. Reject instead of emitting wrong loops.
15718 if (HintWrapper) {
15719 SemaRef.Diag(Loc: CurStmt->getBeginLoc(),
15720 DiagID: diag::err_omp_collapse_stacked_tile)
15721 << /*LoopTransform=*/1;
15722 return true;
15723 }
15724 VarsWithInheritedDSAType TmpDSA;
15725 unsigned SingleNumLoops =
15726 checkOpenMPLoop(DKind: Kind, CollapseLoopCountExpr: nullptr, OrderedLoopCountExpr: nullptr, AStmt: CurStmt, SemaRef, DSA&: *DSAStack,
15727 VarsWithImplicitDSA&: TmpDSA, Built&: LoopHelpers[Cnt]);
15728 if (SingleNumLoops == 0)
15729 return true;
15730 assert(SingleNumLoops == 1 && "Expect single loop iteration space");
15731 if (auto *For = dyn_cast<ForStmt>(Val: CurStmt)) {
15732 OriginalInits.back().push_back(Elt: For->getInit());
15733 Body = For->getBody();
15734 } else {
15735 assert(isa<CXXForRangeStmt>(CurStmt) &&
15736 "Expected canonical for or range-based for loops.");
15737 auto *CXXFor = cast<CXXForRangeStmt>(Val: CurStmt);
15738 OriginalInits.back().push_back(Elt: CXXFor->getBeginStmt());
15739 Body = CXXFor->getBody();
15740 }
15741 OriginalInits.emplace_back();
15742 return false;
15743 },
15744 OnTransformationCallback: [&OriginalInits,
15745 NestedFinals](OMPLoopTransformationDirective *Transform) {
15746 updatePreInits(Transform, PreInits&: OriginalInits.back());
15747 if (NestedFinals)
15748 if (Stmt *Finals = Transform->getFinals()) {
15749 assert(!(*NestedFinals)[OriginalInits.size() - 1] &&
15750 "Expected one nested transformation per loop");
15751 (*NestedFinals)[OriginalInits.size() - 1] = Finals;
15752 }
15753 },
15754 RelaxNestForPeeledTransformation);
15755 assert(OriginalInits.back().empty() && "No preinit after innermost loop");
15756 OriginalInits.pop_back();
15757 return Result;
15758}
15759
15760/// Counts the total number of OpenMP canonical nested loops, including the
15761/// outermost loop (the original loop). PRECONDITION of this visitor is that it
15762/// must be invoked from the original loop to be analyzed. The traversal stops
15763/// for Decl's and Expr's given that they may contain inner loops that must not
15764/// be counted.
15765///
15766/// Example AST structure for the code:
15767///
15768/// int main() {
15769/// #pragma omp fuse
15770/// {
15771/// for (int i = 0; i < 100; i++) { <-- Outer loop
15772/// []() {
15773/// for(int j = 0; j < 100; j++) {} <-- NOT A LOOP (1)
15774/// };
15775/// for(int j = 0; j < 5; ++j) {} <-- Inner loop
15776/// }
15777/// for (int r = 0; i < 100; i++) { <-- Outer loop
15778/// struct LocalClass {
15779/// void bar() {
15780/// for(int j = 0; j < 100; j++) {} <-- NOT A LOOP (2)
15781/// }
15782/// };
15783/// for(int k = 0; k < 10; ++k) {} <-- Inner loop
15784/// {x = 5; for(k = 0; k < 10; ++k) x += k; x}; <-- NOT A LOOP (3)
15785/// }
15786/// }
15787/// }
15788/// (1) because in a different function (here: a lambda)
15789/// (2) because in a different function (here: class method)
15790/// (3) because considered to be intervening-code of non-perfectly nested loop
15791/// Result: Loop 'i' contains 2 loops, Loop 'r' also contains 2 loops.
15792class NestedLoopCounterVisitor final : public DynamicRecursiveASTVisitor {
15793private:
15794 unsigned NestedLoopCount = 0;
15795
15796public:
15797 explicit NestedLoopCounterVisitor() = default;
15798
15799 unsigned getNestedLoopCount() const { return NestedLoopCount; }
15800
15801 bool VisitForStmt(ForStmt *FS) override {
15802 ++NestedLoopCount;
15803 return true;
15804 }
15805
15806 bool VisitCXXForRangeStmt(CXXForRangeStmt *FRS) override {
15807 ++NestedLoopCount;
15808 return true;
15809 }
15810
15811 bool TraverseStmt(Stmt *S) override {
15812 if (!S)
15813 return true;
15814
15815 // Skip traversal of all expressions, including special cases like
15816 // LambdaExpr, StmtExpr, BlockExpr, and RequiresExpr. These expressions
15817 // may contain inner statements (and even loops), but they are not part
15818 // of the syntactic body of the surrounding loop structure.
15819 // Therefore must not be counted.
15820 if (isa<Expr>(Val: S))
15821 return true;
15822
15823 // Only recurse into CompoundStmt (block {}) and loop bodies.
15824 if (isa<CompoundStmt, ForStmt, CXXForRangeStmt>(Val: S)) {
15825 return DynamicRecursiveASTVisitor::TraverseStmt(S);
15826 }
15827
15828 // Stop traversal of the rest of statements, that break perfect
15829 // loop nesting, such as control flow (IfStmt, SwitchStmt...).
15830 return true;
15831 }
15832
15833 bool TraverseDecl(Decl *D) override {
15834 // Stop in the case of finding a declaration, it is not important
15835 // in order to find nested loops (Possible CXXRecordDecl, RecordDecl,
15836 // FunctionDecl...).
15837 return true;
15838 }
15839};
15840
15841bool SemaOpenMP::analyzeLoopSequence(Stmt *LoopSeqStmt,
15842 LoopSequenceAnalysis &SeqAnalysis,
15843 ASTContext &Context,
15844 OpenMPDirectiveKind Kind,
15845 bool IsGeneratedSequence) {
15846 VarsWithInheritedDSAType TmpDSA;
15847 // Helper Lambda to handle storing initialization and body statements for
15848 // both ForStmt and CXXForRangeStmt.
15849 auto StoreLoopStatements = [](LoopAnalysis &Analysis, Stmt *LoopStmt) {
15850 if (auto *For = dyn_cast<ForStmt>(Val: LoopStmt)) {
15851 Analysis.OriginalInits.push_back(Elt: For->getInit());
15852 Analysis.TheForStmt = For;
15853 } else {
15854 auto *CXXFor = cast<CXXForRangeStmt>(Val: LoopStmt);
15855 Analysis.OriginalInits.push_back(Elt: CXXFor->getBeginStmt());
15856 Analysis.TheForStmt = CXXFor;
15857 }
15858 };
15859
15860 // Helper lambda functions to encapsulate the processing of different
15861 // derivations of the canonical loop sequence grammar
15862 // Modularized code for handling loop generation and transformations.
15863 auto AnalyzeLoopGeneration = [&](Stmt *Child) {
15864 auto *LoopTransform = cast<OMPLoopTransformationDirective>(Val: Child);
15865 Stmt *TransformedStmt = LoopTransform->getTransformedStmt();
15866 unsigned NumGeneratedTopLevelLoops =
15867 LoopTransform->getNumGeneratedTopLevelLoops();
15868 // Handle the case where transformed statement is not available due to
15869 // dependent contexts
15870 if (!TransformedStmt) {
15871 if (NumGeneratedTopLevelLoops > 0) {
15872 SeqAnalysis.LoopSeqSize += NumGeneratedTopLevelLoops;
15873 return true;
15874 }
15875 // Unroll full (0 loops produced)
15876 Diag(Loc: Child->getBeginLoc(), DiagID: diag::err_omp_not_for)
15877 << 0 << getOpenMPDirectiveName(D: Kind);
15878 return false;
15879 }
15880 // Handle loop transformations with multiple loop nests
15881 // Unroll full
15882 if (!NumGeneratedTopLevelLoops) {
15883 Diag(Loc: Child->getBeginLoc(), DiagID: diag::err_omp_not_for)
15884 << 0 << getOpenMPDirectiveName(D: Kind);
15885 return false;
15886 }
15887 // Loop transformatons such as split or loopranged fuse
15888 if (NumGeneratedTopLevelLoops > 1) {
15889 // Get the preinits related to this loop sequence generating
15890 // loop transformation (i.e loopranged fuse, split...)
15891 // These preinits differ slightly from regular inits/pre-inits related
15892 // to single loop generating loop transformations (interchange, unroll)
15893 // given that they are not bounded to a particular loop nest
15894 // so they need to be treated independently
15895 updatePreInits(Transform: LoopTransform, PreInits&: SeqAnalysis.LoopSequencePreInits);
15896 if (!analyzeLoopSequence(LoopSeqStmt: TransformedStmt, SeqAnalysis, Context, Kind,
15897 /*IsGeneratedSequence=*/true))
15898 return false;
15899 // Finals follow the last generated loop; looprange fuse inlines its own.
15900 if (Stmt *Finals = LoopTransform->getFinals()) {
15901 assert(!SeqAnalysis.Loops.back().TransformFinals &&
15902 "Generated loop already has Finals");
15903 SeqAnalysis.Loops.back().TransformFinals = Finals;
15904 }
15905 return true;
15906 }
15907 // Vast majority: (Tile, Unroll, Stripe, Reverse, Interchange, Fuse all)
15908 // Process the transformed loop statement
15909 LoopAnalysis &NewTransformedSingleLoop =
15910 SeqAnalysis.Loops.emplace_back(Args&: Child);
15911 unsigned IsCanonical = checkOpenMPLoop(
15912 DKind: Kind, CollapseLoopCountExpr: nullptr, OrderedLoopCountExpr: nullptr, AStmt: TransformedStmt, SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA&: TmpDSA,
15913 Built&: NewTransformedSingleLoop.HelperExprs);
15914
15915 if (!IsCanonical)
15916 return false;
15917
15918 StoreLoopStatements(NewTransformedSingleLoop, TransformedStmt);
15919 updatePreInits(Transform: LoopTransform, PreInits&: NewTransformedSingleLoop.TransformsPreInits);
15920 NewTransformedSingleLoop.TransformFinals = LoopTransform->getFinals();
15921
15922 SeqAnalysis.LoopSeqSize++;
15923 return true;
15924 };
15925
15926 // Modularized code for handling regular canonical loops.
15927 auto AnalyzeRegularLoop = [&](Stmt *Child) {
15928 LoopAnalysis &NewRegularLoop = SeqAnalysis.Loops.emplace_back(Args&: Child);
15929 unsigned IsCanonical =
15930 checkOpenMPLoop(DKind: Kind, CollapseLoopCountExpr: nullptr, OrderedLoopCountExpr: nullptr, AStmt: Child, SemaRef, DSA&: *DSAStack,
15931 VarsWithImplicitDSA&: TmpDSA, Built&: NewRegularLoop.HelperExprs);
15932
15933 if (!IsCanonical)
15934 return false;
15935
15936 StoreLoopStatements(NewRegularLoop, Child);
15937 NestedLoopCounterVisitor NLCV;
15938 NLCV.TraverseStmt(S: Child);
15939 return true;
15940 };
15941
15942 // High level grammar validation.
15943 for (Stmt *Child : LoopSeqStmt->children()) {
15944 if (!Child)
15945 continue;
15946 // A sequence-generating transformation (e.g. looprange fuse) emits each
15947 // out-of-range loop transformation as `{ Loop; Finals }` so its loop
15948 // variables are finalized right after the loop. Analyze the loop and carry
15949 // its Finals along so the enclosing directive can place them.
15950 if (IsGeneratedSequence) {
15951 auto *CS = dyn_cast<CompoundStmt>(Val: Child);
15952 if (CS && CS->size() == 2 &&
15953 LoopAnalysis::isRegularLoop(S: CS->body_front()) &&
15954 !LoopAnalysis::isRegularLoop(S: CS->body_back())) {
15955 if (!AnalyzeRegularLoop(CS->body_front()))
15956 return false;
15957 SeqAnalysis.Loops.back().TransformFinals = CS->body_back();
15958 SeqAnalysis.LoopSeqSize++;
15959 continue;
15960 }
15961 }
15962 // Skip over non-loop-sequence statements.
15963 if (!LoopSequenceAnalysis::isLoopSequenceDerivation(S: Child)) {
15964 Child = Child->IgnoreContainers();
15965 // Ignore empty compound statement.
15966 if (!Child)
15967 continue;
15968 // In the case of a nested loop sequence ignoring containers would not
15969 // be enough, a recurisve transversal of the loop sequence is required.
15970 if (isa<CompoundStmt>(Val: Child)) {
15971 if (!analyzeLoopSequence(LoopSeqStmt: Child, SeqAnalysis, Context, Kind,
15972 IsGeneratedSequence))
15973 return false;
15974 // Already been treated, skip this children
15975 continue;
15976 }
15977 }
15978 // Regular loop sequence handling.
15979 if (LoopSequenceAnalysis::isLoopSequenceDerivation(S: Child)) {
15980 if (LoopAnalysis::isLoopTransformation(S: Child)) {
15981 if (!AnalyzeLoopGeneration(Child))
15982 return false;
15983 // AnalyzeLoopGeneration updates SeqAnalysis.LoopSeqSize accordingly.
15984 } else {
15985 if (!AnalyzeRegularLoop(Child))
15986 return false;
15987 SeqAnalysis.LoopSeqSize++;
15988 }
15989 } else {
15990 // Report error for invalid statement inside canonical loop sequence.
15991 Diag(Loc: Child->getBeginLoc(), DiagID: diag::err_omp_not_for)
15992 << 0 << getOpenMPDirectiveName(D: Kind);
15993 return false;
15994 }
15995 }
15996 return true;
15997}
15998
15999bool SemaOpenMP::checkTransformableLoopSequence(
16000 OpenMPDirectiveKind Kind, Stmt *AStmt, LoopSequenceAnalysis &SeqAnalysis,
16001 ASTContext &Context) {
16002 // Following OpenMP 6.0 API Specification, a Canonical Loop Sequence follows
16003 // the grammar:
16004 //
16005 // canonical-loop-sequence:
16006 // {
16007 // loop-sequence+
16008 // }
16009 // where loop-sequence can be any of the following:
16010 // 1. canonical-loop-sequence
16011 // 2. loop-nest
16012 // 3. loop-sequence-generating-construct (i.e OMPLoopTransformationDirective)
16013 //
16014 // To recognise and traverse this structure the helper function
16015 // analyzeLoopSequence serves as the recurisve entry point
16016 // and tries to match the input AST to the canonical loop sequence grammar
16017 // structure. This function will perform both a semantic and syntactical
16018 // analysis of the given statement according to OpenMP 6.0 definition of
16019 // the aforementioned canonical loop sequence.
16020
16021 // We expect an outer compound statement.
16022 if (!isa<CompoundStmt>(Val: AStmt)) {
16023 Diag(Loc: AStmt->getBeginLoc(), DiagID: diag::err_omp_not_a_loop_sequence)
16024 << getOpenMPDirectiveName(D: Kind);
16025 return false;
16026 }
16027
16028 // Recursive entry point to process the main loop sequence
16029 if (!analyzeLoopSequence(LoopSeqStmt: AStmt, SeqAnalysis, Context, Kind))
16030 return false;
16031
16032 // Diagnose an empty loop sequence.
16033 if (!SeqAnalysis.LoopSeqSize) {
16034 Diag(Loc: AStmt->getBeginLoc(), DiagID: diag::err_omp_empty_loop_sequence)
16035 << getOpenMPDirectiveName(D: Kind);
16036 return false;
16037 }
16038 return true;
16039}
16040
16041/// Add preinit statements that need to be propagated from the selected loop.
16042static void addLoopPreInits(ASTContext &Context,
16043 OMPLoopBasedDirective::HelperExprs &LoopHelper,
16044 Stmt *LoopStmt, ArrayRef<Stmt *> OriginalInit,
16045 SmallVectorImpl<Stmt *> &PreInits) {
16046
16047 // For range-based for-statements, ensure that their syntactic sugar is
16048 // executed by adding them as pre-init statements.
16049 if (auto *CXXRangeFor = dyn_cast<CXXForRangeStmt>(Val: LoopStmt)) {
16050 Stmt *RangeInit = CXXRangeFor->getInit();
16051 if (RangeInit)
16052 PreInits.push_back(Elt: RangeInit);
16053
16054 DeclStmt *RangeStmt = CXXRangeFor->getRangeStmt();
16055 PreInits.push_back(Elt: new (Context) DeclStmt(RangeStmt->getDeclGroup(),
16056 RangeStmt->getBeginLoc(),
16057 RangeStmt->getEndLoc()));
16058
16059 DeclStmt *RangeEnd = CXXRangeFor->getEndStmt();
16060 PreInits.push_back(Elt: new (Context) DeclStmt(RangeEnd->getDeclGroup(),
16061 RangeEnd->getBeginLoc(),
16062 RangeEnd->getEndLoc()));
16063 }
16064
16065 llvm::append_range(C&: PreInits, R&: OriginalInit);
16066
16067 // List of OMPCapturedExprDecl, for __begin, __end, and NumIterations
16068 if (auto *PI = cast_or_null<DeclStmt>(Val: LoopHelper.PreInits)) {
16069 PreInits.push_back(Elt: new (Context) DeclStmt(
16070 PI->getDeclGroup(), PI->getBeginLoc(), PI->getEndLoc()));
16071 }
16072
16073 // Gather declarations for the data members used as counters.
16074 for (Expr *CounterRef : LoopHelper.Counters) {
16075 auto *CounterDecl = cast<DeclRefExpr>(Val: CounterRef)->getDecl();
16076 if (isa<OMPCapturedExprDecl>(Val: CounterDecl))
16077 PreInits.push_back(Elt: new (Context) DeclStmt(
16078 DeclGroupRef(CounterDecl), SourceLocation(), SourceLocation()));
16079 }
16080}
16081
16082/// Collect the loop statements (ForStmt or CXXRangeForStmt) of the affected
16083/// loop of a construct.
16084static void collectLoopStmts(Stmt *AStmt, MutableArrayRef<Stmt *> LoopStmts,
16085 bool RelaxNestForPeeledTransformation = false) {
16086 size_t NumLoops = LoopStmts.size();
16087 OMPLoopBasedDirective::doForAllLoops(
16088 CurStmt: AStmt, /*TryImperfectlyNestedLoops=*/false, NumLoops,
16089 Callback: [LoopStmts](unsigned Cnt, Stmt *CurStmt) {
16090 assert(!LoopStmts[Cnt] && "Loop statement must not yet be assigned");
16091 LoopStmts[Cnt] = CurStmt;
16092 return false;
16093 },
16094 RelaxNestForPeeledTransformation);
16095 assert(!is_contained(LoopStmts, nullptr) &&
16096 "Expecting a loop statement for each affected loop");
16097}
16098
16099/// A non-innermost affected loop generated by a nested loop transformation
16100/// (e.g. `omp reverse`) computes the user counter in statements ahead of the
16101/// next loop. A construct that rebuilds the nest only keeps the innermost
16102/// body, so it must re-emit these statements in the matching loop.
16103static void appendStmtsBeforeNestedLoop(Stmt *LoopStmt,
16104 SmallVectorImpl<Stmt *> &BodyParts) {
16105 Stmt *Body = isa<ForStmt>(Val: LoopStmt)
16106 ? cast<ForStmt>(Val: LoopStmt)->getBody()
16107 : cast<CXXForRangeStmt>(Val: LoopStmt)->getBody();
16108 llvm::append_range(C&: BodyParts,
16109 R: OMPLoopBasedDirective::getStmtsBeforeNestedLoop(Body));
16110}
16111
16112/// Build and return a DeclRefExpr for the floor induction variable using the
16113/// SemaRef and the provided parameters.
16114static Expr *makeFloorIVRef(Sema &SemaRef, ArrayRef<VarDecl *> FloorIndVars,
16115 int I, QualType IVTy, DeclRefExpr *OrigCntVar) {
16116 return buildDeclRefExpr(S&: SemaRef, D: FloorIndVars[I], Ty: IVTy,
16117 Loc: OrigCntVar->getExprLoc());
16118}
16119
16120/// Whether \p Counter is dead after \p LoopStmt: compiler generated (e.g. a
16121/// fuse index) or declared by the loop's own init statement.
16122static bool isDeadAfterLoop(const Expr *Counter, const Stmt *LoopStmt) {
16123 const auto *DRE = dyn_cast<DeclRefExpr>(Val: Counter);
16124 const auto *VD = DRE ? dyn_cast<VarDecl>(Val: DRE->getDecl()) : nullptr;
16125 if (!VD)
16126 return false;
16127 if (VD->isImplicit())
16128 return !isa<OMPCapturedExprDecl>(Val: VD);
16129 const auto *For = dyn_cast<ForStmt>(Val: LoopStmt);
16130 const auto *Init = For ? dyn_cast_or_null<DeclStmt>(Val: For->getInit()) : nullptr;
16131 return Init && llvm::is_contained(Range: Init->decls(), Element: VD);
16132}
16133
16134/// Wrap \p S so it runs only if every loop in \p Loops has iterations.
16135static Stmt *
16136guardByLoopPreConds(ASTContext &Context,
16137 ArrayRef<OMPLoopBasedDirective::HelperExprs> Loops,
16138 Stmt *S) {
16139 for (const auto &Loop : llvm::reverse(C&: Loops))
16140 S = IfStmt::Create(Ctx: Context, IL: SourceLocation(), Kind: IfStatementKind::Ordinary,
16141 Init: nullptr, Var: nullptr, Cond: Loop.PreCond, LPL: SourceLocation(),
16142 RPL: SourceLocation(), Then: S, EL: SourceLocation(), Else: nullptr);
16143 return S;
16144}
16145
16146/// Run loop \p I's inits only if all enclosing loops have iterations.
16147/// Declarations are left as is.
16148static void
16149guardNestedLoopInit(ASTContext &Context,
16150 ArrayRef<OMPLoopBasedDirective::HelperExprs> LoopHelpers,
16151 unsigned I, SmallVectorImpl<Stmt *> &OriginalInit) {
16152 if (I == 0)
16153 return;
16154 for (Stmt *&Init : OriginalInit)
16155 if (Init && !isa<DeclStmt>(Val: Init))
16156 Init = guardByLoopPreConds(Context, Loops: LoopHelpers.take_front(N: I), S: Init);
16157}
16158
16159/// Build loop variable finalization statement from HelperExprs.Finals.
16160/// Each Final runs only if its loop and all enclosing loops have iterations;
16161/// \p NestedFinals[I] is emitted with loop I's.
16162/// Returns nullptr if there is nothing to finalize.
16163/// Counters that are dead after their loop and class-type iterators are
16164/// skipped.
16165static Stmt *
16166buildLoopFinalization(ASTContext &Context,
16167 ArrayRef<OMPLoopBasedDirective::HelperExprs> LoopHelpers,
16168 ArrayRef<Stmt *> LoopStmts,
16169 ArrayRef<Stmt *> NestedFinals = {}) {
16170 assert(LoopStmts.size() == LoopHelpers.size() &&
16171 "Expected one loop statement per loop");
16172 auto GetFinals = [](const OMPLoopBasedDirective::HelperExprs &Helper,
16173 const Stmt *LoopStmt) {
16174 SmallVector<Stmt *, 2> Finals;
16175 for (auto [Counter, Final] :
16176 llvm::zip_equal(t: Helper.Counters, u: Helper.Finals)) {
16177 if (isDeadAfterLoop(Counter, LoopStmt))
16178 continue;
16179 // FIXME: finalize class-type iterators too (random-access arithmetic
16180 // is available per the canonical loop form).
16181 QualType CounterTy = Counter->getType();
16182 if (Final &&
16183 (CounterTy->isArithmeticType() || CounterTy->isPointerType()))
16184 Finals.push_back(Elt: Final);
16185 }
16186 return Finals;
16187 };
16188 auto MakeCompound = [&Context](ArrayRef<Stmt *> Stmts) -> Stmt * {
16189 return CompoundStmt::Create(C: Context, Stmts, FPFeatures: FPOptionsOverride(),
16190 LB: SourceLocation(), RB: SourceLocation());
16191 };
16192
16193 auto GetNestedFinals = [&](size_t I) -> Stmt * {
16194 return I < NestedFinals.size() ? NestedFinals[I] : nullptr;
16195 };
16196
16197 // if (P0) { F0; if (P1) { F1; ... } }, built from the innermost loop out.
16198 Stmt *Nested = nullptr;
16199 for (size_t I = LoopHelpers.size(); I-- > 0;) {
16200 SmallVector<Stmt *, 2> Body = GetFinals(LoopHelpers[I], LoopStmts[I]);
16201 if (Stmt *F = GetNestedFinals(I))
16202 Body.push_back(Elt: F);
16203 if (Nested)
16204 Body.push_back(Elt: Nested);
16205 if (Body.empty())
16206 continue;
16207 Nested = guardByLoopPreConds(Context, Loops: LoopHelpers[I],
16208 S: Body.size() == 1 ? Body.front()
16209 : MakeCompound(Body));
16210 }
16211 return Nested;
16212}
16213
16214StmtResult SemaOpenMP::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses,
16215 Stmt *AStmt,
16216 SourceLocation StartLoc,
16217 SourceLocation EndLoc) {
16218 ASTContext &Context = getASTContext();
16219 Scope *CurScope = SemaRef.getCurScope();
16220
16221 const auto *SizesClause =
16222 OMPExecutableDirective::getSingleClause<OMPSizesClause>(Clauses);
16223 if (!SizesClause ||
16224 llvm::any_of(Range: SizesClause->getSizesRefs(), P: [](Expr *E) { return !E; }))
16225 return StmtError();
16226 unsigned NumLoops = SizesClause->getNumSizes();
16227
16228 // Empty statement should only be possible if there already was an error.
16229 if (!AStmt)
16230 return StmtError();
16231
16232 // Verify and diagnose loop nest.
16233 SmallVector<OMPLoopBasedDirective::HelperExprs, 4> LoopHelpers(NumLoops);
16234 Stmt *Body = nullptr;
16235 SmallVector<SmallVector<Stmt *>, 4> OriginalInits;
16236 SmallVector<Stmt *, 4> NestedFinals;
16237 if (!checkTransformableLoopNest(Kind: OMPD_tile, AStmt, NumLoops, LoopHelpers, Body,
16238 OriginalInits, NestedFinals: &NestedFinals))
16239 return StmtError();
16240
16241 // Delay tiling to when template is completely instantiated.
16242 if (SemaRef.CurContext->isDependentContext())
16243 return OMPTileDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
16244 NumLoops, AssociatedStmt: AStmt, TransformedStmt: nullptr, PreInits: nullptr, Finals: nullptr);
16245
16246 assert(LoopHelpers.size() == NumLoops &&
16247 "Expecting loop iteration space dimensionality to match number of "
16248 "affected loops");
16249 assert(OriginalInits.size() == NumLoops &&
16250 "Expecting loop iteration space dimensionality to match number of "
16251 "affected loops");
16252
16253 // Collect all affected loop statements.
16254 SmallVector<Stmt *> LoopStmts(NumLoops, nullptr);
16255 collectLoopStmts(AStmt, LoopStmts, /*RelaxNestForPeeledTransformation=*/true);
16256
16257 SmallVector<Stmt *, 4> PreInits;
16258 CaptureVars CopyTransformer(SemaRef);
16259
16260 // Create iteration variables for the generated loops.
16261 SmallVector<VarDecl *, 4> FloorIndVars;
16262 SmallVector<VarDecl *, 4> TileIndVars;
16263 FloorIndVars.resize(N: NumLoops);
16264 TileIndVars.resize(N: NumLoops);
16265 for (unsigned I = 0; I < NumLoops; ++I) {
16266 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
16267
16268 assert(LoopHelper.Counters.size() == 1 &&
16269 "Expect single-dimensional loop iteration space");
16270 auto *OrigCntVar = cast<DeclRefExpr>(Val: LoopHelper.Counters.front());
16271 std::string OrigVarName = OrigCntVar->getNameInfo().getAsString();
16272 DeclRefExpr *IterVarRef = cast<DeclRefExpr>(Val: LoopHelper.IterationVarRef);
16273 QualType CntTy = IterVarRef->getType();
16274
16275 // Iteration variable for the floor (i.e. outer) loop.
16276 {
16277 std::string FloorCntName =
16278 (Twine(".floor_") + llvm::utostr(X: I) + ".iv." + OrigVarName).str();
16279 VarDecl *FloorCntDecl =
16280 buildVarDecl(SemaRef, Loc: {}, Type: CntTy, Name: FloorCntName, Attrs: nullptr, OrigRef: OrigCntVar);
16281 FloorIndVars[I] = FloorCntDecl;
16282 }
16283
16284 // Iteration variable for the tile (i.e. inner) loop.
16285 {
16286 std::string TileCntName =
16287 (Twine(".tile_") + llvm::utostr(X: I) + ".iv." + OrigVarName).str();
16288
16289 // Reuse the iteration variable created by checkOpenMPLoop. It is also
16290 // used by the expressions to derive the original iteration variable's
16291 // value from the logical iteration number.
16292 auto *TileCntDecl = cast<VarDecl>(Val: IterVarRef->getDecl());
16293 TileCntDecl->setDeclName(
16294 &SemaRef.PP.getIdentifierTable().get(Name: TileCntName));
16295 TileIndVars[I] = TileCntDecl;
16296 }
16297
16298 guardNestedLoopInit(Context, LoopHelpers, I, OriginalInit&: OriginalInits[I]);
16299 addLoopPreInits(Context, LoopHelper, LoopStmt: LoopStmts[I], OriginalInit: OriginalInits[I],
16300 PreInits);
16301 }
16302
16303 // Once the original iteration values are set, append the innermost body.
16304 Stmt *Inner = Body;
16305
16306 auto MakeDimTileSize = [&SemaRef = this->SemaRef, &CopyTransformer, &Context,
16307 SizesClause, CurScope](int I) -> Expr * {
16308 Expr *DimTileSizeExpr = SizesClause->getSizesRefs()[I];
16309
16310 if (DimTileSizeExpr->containsErrors())
16311 return nullptr;
16312
16313 if (isa<ConstantExpr>(Val: DimTileSizeExpr))
16314 return AssertSuccess(R: CopyTransformer.TransformExpr(E: DimTileSizeExpr));
16315
16316 // When the tile size is not a constant but a variable, it is possible to
16317 // pass non-positive numbers. For instance:
16318 // \code{c}
16319 // int a = 0;
16320 // #pragma omp tile sizes(a)
16321 // for (int i = 0; i < 42; ++i)
16322 // body(i);
16323 // \endcode
16324 // Although there is no meaningful interpretation of the tile size, the body
16325 // should still be executed 42 times to avoid surprises. To preserve the
16326 // invariant that every loop iteration is executed exactly once and not
16327 // cause an infinite loop, apply a minimum tile size of one.
16328 // Build expr:
16329 // \code{c}
16330 // (TS <= 0) ? 1 : TS
16331 // \endcode
16332 QualType DimTy = DimTileSizeExpr->getType();
16333 unsigned DimWidth = Context.getIntWidth(T: DimTy);
16334 IntegerLiteral *Zero = IntegerLiteral::Create(
16335 C: Context, V: llvm::APInt::getZero(numBits: DimWidth), type: DimTy, l: {});
16336 IntegerLiteral *One =
16337 IntegerLiteral::Create(C: Context, V: llvm::APInt(DimWidth, 1), type: DimTy, l: {});
16338 Expr *Cond = AssertSuccess(R: SemaRef.BuildBinOp(
16339 S: CurScope, OpLoc: {}, Opc: BO_LE,
16340 LHSExpr: AssertSuccess(R: CopyTransformer.TransformExpr(E: DimTileSizeExpr)), RHSExpr: Zero));
16341 Expr *MinOne = new (Context) ConditionalOperator(
16342 Cond, {}, One, {},
16343 AssertSuccess(R: CopyTransformer.TransformExpr(E: DimTileSizeExpr)), DimTy,
16344 VK_PRValue, OK_Ordinary);
16345 return MinOne;
16346 };
16347
16348 // Create tile loops from the inside to the outside.
16349 //
16350 // Each intra-tile loop is emitted in its natural min-bounded form, which has
16351 // no per-iteration body predicate and vectorizes well when run directly:
16352 // for (.tile.iv = .floor.iv; .tile.iv < min(.floor.iv + T, N); ++.tile.iv)
16353 //
16354 // A loop-associated directive that *consumes* this loop (e.g. `collapse`)
16355 // instead needs a constant, floor-independent trip count to linearize the
16356 // nest, which the min() bound cannot give it. Rather than change the
16357 // emitted loop, we attach a droppable OMPInvariantPredicateBoundAttr hint
16358 // below carrying an equivalent rectangular reinterpretation:
16359 // RectCond : .tile.iv < .floor.iv + T -- rectangular bound, analyzed by
16360 // checkOpenMPIterationSpace in place of the stored condition
16361 // TileSize : T -- constant per-tile trip count
16362 // Predicate : .tile.iv < N -- remainder-tile overshoot,
16363 // applied as a body guard instead of shortening the trip count;
16364 // omitted when N is known to be a multiple of T, as there is
16365 // then no partial tile to guard against
16366 // Because the reinterpreted lower bound is still `.floor.iv`,
16367 // checkOpenMPIterationSpace sets IsNonRectangularLB on the matching floor
16368 // counter, so a collapsed `.tile.iv` re-reads the floor's current value
16369 // instead of a stale preheader snapshot. Collapsing through stacked tiles
16370 // (tile-of-tile) is not supported: the inner floor is not itself a collapsed
16371 // counter.
16372 for (int I = NumLoops - 1; I >= 0; --I) {
16373 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
16374 Expr *NumIterations = LoopHelper.NumIterations;
16375 auto *OrigCntVar = cast<DeclRefExpr>(Val: LoopHelper.Counters[0]);
16376 QualType IVTy = NumIterations->getType();
16377 Stmt *LoopStmt = LoopStmts[I];
16378
16379 // Commonly used variables. One of the constraints of an AST is that every
16380 // node object must appear at most once, hence we define a lambda that
16381 // creates a new AST node at every use.
16382 auto MakeTileIVRef = [&SemaRef = this->SemaRef, &TileIndVars, I, IVTy,
16383 OrigCntVar]() {
16384 return buildDeclRefExpr(S&: SemaRef, D: TileIndVars[I], Ty: IVTy,
16385 Loc: OrigCntVar->getExprLoc());
16386 };
16387
16388 // For init-statement: auto .tile.iv = .floor.iv
16389 SemaRef.AddInitializerToDecl(
16390 dcl: TileIndVars[I],
16391 init: SemaRef
16392 .DefaultLvalueConversion(
16393 E: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar))
16394 .get(),
16395 /*DirectInit=*/false);
16396 Decl *CounterDecl = TileIndVars[I];
16397 StmtResult InitStmt = new (Context)
16398 DeclStmt(DeclGroupRef::Create(C&: Context, Decls: &CounterDecl, NumDecls: 1),
16399 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc());
16400 if (!InitStmt.isUsable())
16401 return StmtError();
16402
16403 // For cond-expression:
16404 // .tile.iv < min(.floor.iv + DimTileSize, NumIterations)
16405 Expr *DimTileSize = MakeDimTileSize(I);
16406 if (!DimTileSize)
16407 return StmtError();
16408 ExprResult EndOfTile = SemaRef.BuildBinOp(
16409 S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_Add,
16410 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
16411 RHSExpr: DimTileSize);
16412 if (!EndOfTile.isUsable())
16413 return StmtError();
16414 ExprResult IsPartialTile =
16415 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
16416 LHSExpr: NumIterations, RHSExpr: EndOfTile.get());
16417 if (!IsPartialTile.isUsable())
16418 return StmtError();
16419 ExprResult MinTileAndIterSpace = SemaRef.ActOnConditionalOp(
16420 QuestionLoc: LoopHelper.Cond->getBeginLoc(), ColonLoc: LoopHelper.Cond->getEndLoc(),
16421 CondExpr: IsPartialTile.get(), LHSExpr: NumIterations, RHSExpr: EndOfTile.get());
16422 if (!MinTileAndIterSpace.isUsable())
16423 return StmtError();
16424 ExprResult CondExpr =
16425 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
16426 LHSExpr: MakeTileIVRef(), RHSExpr: MinTileAndIterSpace.get());
16427 if (!CondExpr.isUsable())
16428 return StmtError();
16429
16430 // For incr-statement: ++.tile.iv
16431 ExprResult IncrStmt = SemaRef.BuildUnaryOp(
16432 S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(), Opc: UO_PreInc, Input: MakeTileIVRef());
16433 if (!IncrStmt.isUsable())
16434 return StmtError();
16435
16436 // Build OMPInvariantPredicateBoundAttr hint (RectCond, TileSize,
16437 // Predicate).
16438 Expr *RectDimTileSize = MakeDimTileSize(I);
16439 if (!RectDimTileSize)
16440 return StmtError();
16441 ExprResult RectEndOfTile = SemaRef.BuildBinOp(
16442 S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_Add,
16443 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
16444 RHSExpr: RectDimTileSize);
16445 if (!RectEndOfTile.isUsable())
16446 return StmtError();
16447 ExprResult RectCond =
16448 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
16449 LHSExpr: MakeTileIVRef(), RHSExpr: RectEndOfTile.get());
16450 Expr *TileSizeExpr = MakeDimTileSize(I);
16451 if (!TileSizeExpr)
16452 return StmtError();
16453 ExprResult TileSize = SemaRef.PerformImplicitConversion(
16454 From: TileSizeExpr, ToType: IVTy, Action: AssignmentAction::Converting,
16455 /*AllowExplicit=*/true);
16456 if (!RectCond.isUsable() || !TileSize.isUsable())
16457 return StmtError();
16458
16459 // The overshoot guard is only needed if the last tile can be partial. When
16460 // both the trip count and the tile size are known at compile time and the
16461 // former is a multiple of the latter, every tile is full, so leave the
16462 // predicate out. A dependent or run-time value makes
16463 // the evaluation fail, which keeps the guard.
16464 bool NoPartialTile = false;
16465 Expr::EvalResult TileSizeVal, NumIterationsVal;
16466 if (TileSizeExpr->EvaluateAsInt(Result&: TileSizeVal, Ctx: Context) &&
16467 NumIterations->EvaluateAsInt(Result&: NumIterationsVal, Ctx: Context)) {
16468 llvm::APSInt TS = TileSizeVal.Val.getInt();
16469 llvm::APSInt N = NumIterationsVal.Val.getInt();
16470 unsigned Width = std::max(a: TS.getBitWidth(), b: N.getBitWidth());
16471 TS = TS.extend(width: Width);
16472 N = N.extend(width: Width);
16473 NoPartialTile =
16474 TS.isStrictlyPositive() && N.isStrictlyPositive() && N.urem(RHS: TS) == 0;
16475 }
16476
16477 ExprResult Predicate;
16478 if (!NoPartialTile) {
16479 Predicate = SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(),
16480 Opc: BO_LT, LHSExpr: MakeTileIVRef(), RHSExpr: NumIterations);
16481 if (!Predicate.isUsable())
16482 return StmtError();
16483 }
16484
16485 // Statements to set the original iteration variable's value from the
16486 // logical iteration number.
16487 // Generated for loop is:
16488 // \code
16489 // Original_for_init;
16490 // for (auto .tile.iv = .floor.iv;
16491 // .tile.iv < min(.floor.iv + DimTileSize, NumIterations);
16492 // ++.tile.iv) {
16493 // Original_Body;
16494 // Original_counter_update;
16495 // }
16496 // \endcode
16497 // FIXME: If the innermost body is an loop itself, inserting these
16498 // statements stops it being recognized as a perfectly nested loop (e.g.
16499 // for applying tiling again). If this is the case, sink the expressions
16500 // further into the inner loop.
16501 SmallVector<Stmt *, 4> BodyParts;
16502 BodyParts.append(in_start: LoopHelper.Updates.begin(), in_end: LoopHelper.Updates.end());
16503 if (auto *SourceCXXFor = dyn_cast<CXXForRangeStmt>(Val: LoopStmt))
16504 BodyParts.push_back(Elt: SourceCXXFor->getLoopVarStmt());
16505 if (static_cast<unsigned>(I) + 1 < NumLoops)
16506 appendStmtsBeforeNestedLoop(LoopStmt, BodyParts);
16507 BodyParts.push_back(Elt: Inner);
16508 Inner = CompoundStmt::Create(C: Context, Stmts: BodyParts, FPFeatures: FPOptionsOverride(),
16509 LB: Inner->getBeginLoc(), RB: Inner->getEndLoc());
16510 Inner = new (Context)
16511 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr,
16512 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(),
16513 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
16514
16515 // Attach the droppable reinterpretation attribute to the intra-tile loop.
16516 auto *Hint = OMPInvariantPredicateBoundAttr::CreateImplicit(
16517 Ctx&: Context, RectCond: RectCond.get(), TileSize: TileSize.get(),
16518 Predicate: Predicate.isUsable() ? Predicate.get() : nullptr,
16519 Range: Inner->getSourceRange());
16520 Inner =
16521 AttributedStmt::Create(C: Context, Loc: Inner->getBeginLoc(), Attrs: {Hint}, SubStmt: Inner);
16522 }
16523
16524 // Create floor loops from the inside to the outside.
16525 for (int I = NumLoops - 1; I >= 0; --I) {
16526 auto &LoopHelper = LoopHelpers[I];
16527 Expr *NumIterations = LoopHelper.NumIterations;
16528 DeclRefExpr *OrigCntVar = cast<DeclRefExpr>(Val: LoopHelper.Counters[0]);
16529 QualType IVTy = NumIterations->getType();
16530
16531 // For init-statement: auto .floor.iv = 0
16532 SemaRef.AddInitializerToDecl(
16533 dcl: FloorIndVars[I],
16534 init: SemaRef.ActOnIntegerConstant(Loc: LoopHelper.Init->getExprLoc(), Val: 0).get(),
16535 /*DirectInit=*/false);
16536 Decl *CounterDecl = FloorIndVars[I];
16537 StmtResult InitStmt = new (Context)
16538 DeclStmt(DeclGroupRef::Create(C&: Context, Decls: &CounterDecl, NumDecls: 1),
16539 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc());
16540 if (!InitStmt.isUsable())
16541 return StmtError();
16542
16543 // For cond-expression: .floor.iv < NumIterations
16544 ExprResult CondExpr = SemaRef.BuildBinOp(
16545 S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
16546 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
16547 RHSExpr: NumIterations);
16548 if (!CondExpr.isUsable())
16549 return StmtError();
16550
16551 // For incr-statement: .floor.iv += DimTileSize
16552 Expr *DimTileSize = MakeDimTileSize(I);
16553 if (!DimTileSize)
16554 return StmtError();
16555 ExprResult IncrStmt = SemaRef.BuildBinOp(
16556 S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(), Opc: BO_AddAssign,
16557 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
16558 RHSExpr: DimTileSize);
16559 if (!IncrStmt.isUsable())
16560 return StmtError();
16561
16562 Inner = new (Context)
16563 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr,
16564 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(),
16565 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
16566 }
16567
16568 return OMPTileDirective::Create(
16569 C: Context, StartLoc, EndLoc, Clauses, NumLoops, AssociatedStmt: AStmt, TransformedStmt: Inner,
16570 PreInits: buildPreInits(Context, PreInits),
16571 Finals: buildLoopFinalization(Context, LoopHelpers, LoopStmts, NestedFinals));
16572}
16573
16574StmtResult SemaOpenMP::ActOnOpenMPStripeDirective(ArrayRef<OMPClause *> Clauses,
16575 Stmt *AStmt,
16576 SourceLocation StartLoc,
16577 SourceLocation EndLoc) {
16578 ASTContext &Context = getASTContext();
16579 Scope *CurScope = SemaRef.getCurScope();
16580
16581 const auto *SizesClause =
16582 OMPExecutableDirective::getSingleClause<OMPSizesClause>(Clauses);
16583 if (!SizesClause ||
16584 llvm::any_of(Range: SizesClause->getSizesRefs(), P: [](const Expr *SizeExpr) {
16585 return !SizeExpr || SizeExpr->containsErrors();
16586 }))
16587 return StmtError();
16588 unsigned NumLoops = SizesClause->getNumSizes();
16589
16590 // Empty statement should only be possible if there already was an error.
16591 if (!AStmt)
16592 return StmtError();
16593
16594 // Verify and diagnose loop nest.
16595 SmallVector<OMPLoopBasedDirective::HelperExprs, 4> LoopHelpers(NumLoops);
16596 Stmt *Body = nullptr;
16597 SmallVector<SmallVector<Stmt *>, 4> OriginalInits;
16598 SmallVector<Stmt *, 4> NestedFinals;
16599 if (!checkTransformableLoopNest(Kind: OMPD_stripe, AStmt, NumLoops, LoopHelpers,
16600 Body, OriginalInits, NestedFinals: &NestedFinals))
16601 return StmtError();
16602
16603 // Delay striping to when template is completely instantiated.
16604 if (SemaRef.CurContext->isDependentContext())
16605 return OMPStripeDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
16606 NumLoops, AssociatedStmt: AStmt, TransformedStmt: nullptr, PreInits: nullptr,
16607 Finals: nullptr);
16608
16609 assert(LoopHelpers.size() == NumLoops &&
16610 "Expecting loop iteration space dimensionality to match number of "
16611 "affected loops");
16612 assert(OriginalInits.size() == NumLoops &&
16613 "Expecting loop iteration space dimensionality to match number of "
16614 "affected loops");
16615
16616 // Collect all affected loop statements.
16617 SmallVector<Stmt *> LoopStmts(NumLoops, nullptr);
16618 collectLoopStmts(AStmt, LoopStmts, /*RelaxNestForPeeledTransformation=*/true);
16619
16620 SmallVector<Stmt *, 4> PreInits;
16621 CaptureVars CopyTransformer(SemaRef);
16622
16623 // Create iteration variables for the generated loops.
16624 SmallVector<VarDecl *, 4> FloorIndVars;
16625 SmallVector<VarDecl *, 4> StripeIndVars;
16626 FloorIndVars.resize(N: NumLoops);
16627 StripeIndVars.resize(N: NumLoops);
16628 for (unsigned I : llvm::seq<unsigned>(Size: NumLoops)) {
16629 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
16630
16631 assert(LoopHelper.Counters.size() == 1 &&
16632 "Expect single-dimensional loop iteration space");
16633 auto *OrigCntVar = cast<DeclRefExpr>(Val: LoopHelper.Counters.front());
16634 std::string OrigVarName = OrigCntVar->getNameInfo().getAsString();
16635 DeclRefExpr *IterVarRef = cast<DeclRefExpr>(Val: LoopHelper.IterationVarRef);
16636 QualType CntTy = IterVarRef->getType();
16637
16638 // Iteration variable for the stripe (i.e. outer) loop.
16639 {
16640 std::string FloorCntName =
16641 (Twine(".floor_") + llvm::utostr(X: I) + ".iv." + OrigVarName).str();
16642 VarDecl *FloorCntDecl =
16643 buildVarDecl(SemaRef, Loc: {}, Type: CntTy, Name: FloorCntName, Attrs: nullptr, OrigRef: OrigCntVar);
16644 FloorIndVars[I] = FloorCntDecl;
16645 }
16646
16647 // Iteration variable for the stripe (i.e. inner) loop.
16648 {
16649 std::string StripeCntName =
16650 (Twine(".stripe_") + llvm::utostr(X: I) + ".iv." + OrigVarName).str();
16651
16652 // Reuse the iteration variable created by checkOpenMPLoop. It is also
16653 // used by the expressions to derive the original iteration variable's
16654 // value from the logical iteration number.
16655 auto *StripeCntDecl = cast<VarDecl>(Val: IterVarRef->getDecl());
16656 StripeCntDecl->setDeclName(
16657 &SemaRef.PP.getIdentifierTable().get(Name: StripeCntName));
16658 StripeIndVars[I] = StripeCntDecl;
16659 }
16660
16661 guardNestedLoopInit(Context, LoopHelpers, I, OriginalInit&: OriginalInits[I]);
16662 addLoopPreInits(Context, LoopHelper, LoopStmt: LoopStmts[I], OriginalInit: OriginalInits[I],
16663 PreInits);
16664 }
16665
16666 // Once the original iteration values are set, append the innermost body.
16667 Stmt *Inner = Body;
16668
16669 auto MakeDimStripeSize = [&](int I) -> Expr * {
16670 Expr *DimStripeSizeExpr = SizesClause->getSizesRefs()[I];
16671 if (isa<ConstantExpr>(Val: DimStripeSizeExpr))
16672 return AssertSuccess(R: CopyTransformer.TransformExpr(E: DimStripeSizeExpr));
16673
16674 // When the stripe size is not a constant but a variable, it is possible to
16675 // pass non-positive numbers. For instance:
16676 // \code{c}
16677 // int a = 0;
16678 // #pragma omp stripe sizes(a)
16679 // for (int i = 0; i < 42; ++i)
16680 // body(i);
16681 // \endcode
16682 // Although there is no meaningful interpretation of the stripe size, the
16683 // body should still be executed 42 times to avoid surprises. To preserve
16684 // the invariant that every loop iteration is executed exactly once and not
16685 // cause an infinite loop, apply a minimum stripe size of one.
16686 // Build expr:
16687 // \code{c}
16688 // (TS <= 0) ? 1 : TS
16689 // \endcode
16690 QualType DimTy = DimStripeSizeExpr->getType();
16691 unsigned DimWidth = Context.getIntWidth(T: DimTy);
16692 IntegerLiteral *Zero = IntegerLiteral::Create(
16693 C: Context, V: llvm::APInt::getZero(numBits: DimWidth), type: DimTy, l: {});
16694 IntegerLiteral *One =
16695 IntegerLiteral::Create(C: Context, V: llvm::APInt(DimWidth, 1), type: DimTy, l: {});
16696 Expr *Cond = AssertSuccess(R: SemaRef.BuildBinOp(
16697 S: CurScope, OpLoc: {}, Opc: BO_LE,
16698 LHSExpr: AssertSuccess(R: CopyTransformer.TransformExpr(E: DimStripeSizeExpr)), RHSExpr: Zero));
16699 Expr *MinOne = new (Context) ConditionalOperator(
16700 Cond, {}, One, {},
16701 AssertSuccess(R: CopyTransformer.TransformExpr(E: DimStripeSizeExpr)), DimTy,
16702 VK_PRValue, OK_Ordinary);
16703 return MinOne;
16704 };
16705
16706 // Create stripe loops from the inside to the outside.
16707 for (int I = NumLoops - 1; I >= 0; --I) {
16708 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
16709 Expr *NumIterations = LoopHelper.NumIterations;
16710 auto *OrigCntVar = cast<DeclRefExpr>(Val: LoopHelper.Counters[0]);
16711 QualType IVTy = NumIterations->getType();
16712 Stmt *LoopStmt = LoopStmts[I];
16713
16714 // For init-statement: auto .stripe.iv = .floor.iv
16715 SemaRef.AddInitializerToDecl(
16716 dcl: StripeIndVars[I],
16717 init: SemaRef
16718 .DefaultLvalueConversion(
16719 E: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar))
16720 .get(),
16721 /*DirectInit=*/false);
16722 Decl *CounterDecl = StripeIndVars[I];
16723 StmtResult InitStmt = new (Context)
16724 DeclStmt(DeclGroupRef::Create(C&: Context, Decls: &CounterDecl, NumDecls: 1),
16725 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc());
16726 if (!InitStmt.isUsable())
16727 return StmtError();
16728
16729 // For cond-expression:
16730 // .stripe.iv < min(.floor.iv + DimStripeSize, NumIterations)
16731 ExprResult EndOfStripe = SemaRef.BuildBinOp(
16732 S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_Add,
16733 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
16734 RHSExpr: MakeDimStripeSize(I));
16735 if (!EndOfStripe.isUsable())
16736 return StmtError();
16737 ExprResult IsPartialStripe =
16738 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
16739 LHSExpr: NumIterations, RHSExpr: EndOfStripe.get());
16740 if (!IsPartialStripe.isUsable())
16741 return StmtError();
16742 ExprResult MinStripeAndIterSpace = SemaRef.ActOnConditionalOp(
16743 QuestionLoc: LoopHelper.Cond->getBeginLoc(), ColonLoc: LoopHelper.Cond->getEndLoc(),
16744 CondExpr: IsPartialStripe.get(), LHSExpr: NumIterations, RHSExpr: EndOfStripe.get());
16745 if (!MinStripeAndIterSpace.isUsable())
16746 return StmtError();
16747 ExprResult CondExpr = SemaRef.BuildBinOp(
16748 S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
16749 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars: StripeIndVars, I, IVTy, OrigCntVar),
16750 RHSExpr: MinStripeAndIterSpace.get());
16751 if (!CondExpr.isUsable())
16752 return StmtError();
16753
16754 // For incr-statement: ++.stripe.iv
16755 ExprResult IncrStmt = SemaRef.BuildUnaryOp(
16756 S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(), Opc: UO_PreInc,
16757 Input: makeFloorIVRef(SemaRef, FloorIndVars: StripeIndVars, I, IVTy, OrigCntVar));
16758 if (!IncrStmt.isUsable())
16759 return StmtError();
16760
16761 // Statements to set the original iteration variable's value from the
16762 // logical iteration number.
16763 // Generated for loop is:
16764 // \code
16765 // Original_for_init;
16766 // for (auto .stripe.iv = .floor.iv;
16767 // .stripe.iv < min(.floor.iv + DimStripeSize, NumIterations);
16768 // ++.stripe.iv) {
16769 // Original_Body;
16770 // Original_counter_update;
16771 // }
16772 // \endcode
16773 // FIXME: If the innermost body is a loop itself, inserting these
16774 // statements stops it being recognized as a perfectly nested loop (e.g.
16775 // for applying another loop transformation). If this is the case, sink the
16776 // expressions further into the inner loop.
16777 SmallVector<Stmt *, 4> BodyParts;
16778 BodyParts.append(in_start: LoopHelper.Updates.begin(), in_end: LoopHelper.Updates.end());
16779 if (auto *SourceCXXFor = dyn_cast<CXXForRangeStmt>(Val: LoopStmt))
16780 BodyParts.push_back(Elt: SourceCXXFor->getLoopVarStmt());
16781 if (static_cast<unsigned>(I) + 1 < NumLoops)
16782 appendStmtsBeforeNestedLoop(LoopStmt, BodyParts);
16783 BodyParts.push_back(Elt: Inner);
16784 Inner = CompoundStmt::Create(C: Context, Stmts: BodyParts, FPFeatures: FPOptionsOverride(),
16785 LB: Inner->getBeginLoc(), RB: Inner->getEndLoc());
16786 Inner = new (Context)
16787 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr,
16788 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(),
16789 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
16790 }
16791
16792 // Create grid loops from the inside to the outside.
16793 for (int I = NumLoops - 1; I >= 0; --I) {
16794 auto &LoopHelper = LoopHelpers[I];
16795 Expr *NumIterations = LoopHelper.NumIterations;
16796 DeclRefExpr *OrigCntVar = cast<DeclRefExpr>(Val: LoopHelper.Counters[0]);
16797 QualType IVTy = NumIterations->getType();
16798
16799 // For init-statement: auto .grid.iv = 0
16800 SemaRef.AddInitializerToDecl(
16801 dcl: FloorIndVars[I],
16802 init: SemaRef.ActOnIntegerConstant(Loc: LoopHelper.Init->getExprLoc(), Val: 0).get(),
16803 /*DirectInit=*/false);
16804 Decl *CounterDecl = FloorIndVars[I];
16805 StmtResult InitStmt = new (Context)
16806 DeclStmt(DeclGroupRef::Create(C&: Context, Decls: &CounterDecl, NumDecls: 1),
16807 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc());
16808 if (!InitStmt.isUsable())
16809 return StmtError();
16810
16811 // For cond-expression: .floor.iv < NumIterations
16812 ExprResult CondExpr = SemaRef.BuildBinOp(
16813 S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
16814 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
16815 RHSExpr: NumIterations);
16816 if (!CondExpr.isUsable())
16817 return StmtError();
16818
16819 // For incr-statement: .floor.iv += DimStripeSize
16820 ExprResult IncrStmt = SemaRef.BuildBinOp(
16821 S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(), Opc: BO_AddAssign,
16822 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
16823 RHSExpr: MakeDimStripeSize(I));
16824 if (!IncrStmt.isUsable())
16825 return StmtError();
16826
16827 Inner = new (Context)
16828 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr,
16829 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(),
16830 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
16831 }
16832
16833 return OMPStripeDirective::Create(
16834 C: Context, StartLoc, EndLoc, Clauses, NumLoops, AssociatedStmt: AStmt, TransformedStmt: Inner,
16835 PreInits: buildPreInits(Context, PreInits),
16836 Finals: buildLoopFinalization(Context, LoopHelpers, LoopStmts, NestedFinals));
16837}
16838
16839StmtResult SemaOpenMP::ActOnOpenMPUnrollDirective(ArrayRef<OMPClause *> Clauses,
16840 Stmt *AStmt,
16841 SourceLocation StartLoc,
16842 SourceLocation EndLoc) {
16843 ASTContext &Context = getASTContext();
16844 Scope *CurScope = SemaRef.getCurScope();
16845 // Empty statement should only be possible if there already was an error.
16846 if (!AStmt)
16847 return StmtError();
16848
16849 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
16850 MutuallyExclusiveClauses: {OMPC_partial, OMPC_full}))
16851 return StmtError();
16852
16853 const OMPFullClause *FullClause =
16854 OMPExecutableDirective::getSingleClause<OMPFullClause>(Clauses);
16855 const OMPPartialClause *PartialClause =
16856 OMPExecutableDirective::getSingleClause<OMPPartialClause>(Clauses);
16857 assert(!(FullClause && PartialClause) &&
16858 "mutual exclusivity must have been checked before");
16859
16860 constexpr unsigned NumLoops = 1;
16861 Stmt *Body = nullptr;
16862 SmallVector<OMPLoopBasedDirective::HelperExprs, NumLoops> LoopHelpers(
16863 NumLoops);
16864 SmallVector<SmallVector<Stmt *>, NumLoops + 1> OriginalInits;
16865 SmallVector<Stmt *, 4> NestedFinals;
16866 if (!checkTransformableLoopNest(Kind: OMPD_unroll, AStmt, NumLoops, LoopHelpers,
16867 Body, OriginalInits, NestedFinals: &NestedFinals))
16868 return StmtError();
16869
16870 unsigned NumGeneratedTopLevelLoops = PartialClause ? 1 : 0;
16871
16872 // Delay unrolling to when template is completely instantiated.
16873 if (SemaRef.CurContext->isDependentContext())
16874 return OMPUnrollDirective::Create(C: Context, StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
16875 NumGeneratedTopLevelLoops, TransformedStmt: nullptr,
16876 PreInits: nullptr, Finals: nullptr);
16877
16878 assert(LoopHelpers.size() == NumLoops &&
16879 "Expecting a single-dimensional loop iteration space");
16880 assert(OriginalInits.size() == NumLoops &&
16881 "Expecting a single-dimensional loop iteration space");
16882 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers.front();
16883
16884 if (FullClause) {
16885 if (!VerifyPositiveIntegerConstantInClause(
16886 Op: LoopHelper.NumIterations, CKind: OMPC_full, /*StrictlyPositive=*/false,
16887 /*SuppressExprDiags=*/true)
16888 .isUsable()) {
16889 Diag(Loc: AStmt->getBeginLoc(), DiagID: diag::err_omp_unroll_full_variable_trip_count);
16890 Diag(Loc: FullClause->getBeginLoc(), DiagID: diag::note_omp_directive_here)
16891 << "#pragma omp unroll full";
16892 return StmtError();
16893 }
16894 }
16895
16896 // The generated loop may only be passed to other loop-associated directive
16897 // when a partial clause is specified. Without the requirement it is
16898 // sufficient to generate loop unroll metadata at code-generation.
16899 if (NumGeneratedTopLevelLoops == 0)
16900 return OMPUnrollDirective::Create(C: Context, StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
16901 NumGeneratedTopLevelLoops, TransformedStmt: nullptr,
16902 PreInits: nullptr, Finals: nullptr);
16903
16904 // Otherwise, we need to provide a de-sugared/transformed AST that can be
16905 // associated with another loop directive.
16906 //
16907 // The canonical loop analysis return by checkTransformableLoopNest assumes
16908 // the following structure to be the same loop without transformations or
16909 // directives applied: \code OriginalInits; LoopHelper.PreInits;
16910 // LoopHelper.Counters;
16911 // for (; IV < LoopHelper.NumIterations; ++IV) {
16912 // LoopHelper.Updates;
16913 // Body;
16914 // }
16915 // \endcode
16916 // where IV is a variable declared and initialized to 0 in LoopHelper.PreInits
16917 // and referenced by LoopHelper.IterationVarRef.
16918 //
16919 // The unrolling directive transforms this into the following loop:
16920 // \code
16921 // OriginalInits; \
16922 // LoopHelper.PreInits; > NewPreInits
16923 // LoopHelper.Counters; /
16924 // for (auto UIV = 0; UIV < LoopHelper.NumIterations; UIV+=Factor) {
16925 // #pragma clang loop unroll_count(Factor)
16926 // for (IV = UIV; IV < UIV + Factor && UIV < LoopHelper.NumIterations; ++IV)
16927 // {
16928 // LoopHelper.Updates;
16929 // Body;
16930 // }
16931 // }
16932 // \endcode
16933 // where UIV is a new logical iteration counter. IV must be the same VarDecl
16934 // as the original LoopHelper.IterationVarRef because LoopHelper.Updates
16935 // references it. If the partially unrolled loop is associated with another
16936 // loop directive (like an OMPForDirective), it will use checkOpenMPLoop to
16937 // analyze this loop, i.e. the outer loop must fulfill the constraints of an
16938 // OpenMP canonical loop. The inner loop is not an associable canonical loop
16939 // and only exists to defer its unrolling to LLVM's LoopUnroll instead of
16940 // doing it in the frontend (by adding loop metadata). NewPreInits becomes a
16941 // property of the OMPLoopBasedDirective instead of statements in
16942 // CompoundStatement. This is to allow the loop to become a non-outermost loop
16943 // of a canonical loop nest where these PreInits are emitted before the
16944 // outermost directive.
16945
16946 // Find the loop statement.
16947 Stmt *LoopStmt = nullptr;
16948 collectLoopStmts(AStmt, LoopStmts: {LoopStmt});
16949
16950 // Determine the PreInit declarations.
16951 SmallVector<Stmt *, 4> PreInits;
16952 addLoopPreInits(Context, LoopHelper, LoopStmt, OriginalInit: OriginalInits[0], PreInits);
16953
16954 auto *IterationVarRef = cast<DeclRefExpr>(Val: LoopHelper.IterationVarRef);
16955 QualType IVTy = IterationVarRef->getType();
16956 assert(LoopHelper.Counters.size() == 1 &&
16957 "Expecting a single-dimensional loop iteration space");
16958 auto *OrigVar = cast<DeclRefExpr>(Val: LoopHelper.Counters.front());
16959
16960 // Determine the unroll factor.
16961 uint64_t Factor;
16962 SourceLocation FactorLoc;
16963 if (Expr *FactorVal = PartialClause->getFactor();
16964 FactorVal && !FactorVal->containsErrors()) {
16965 Factor = FactorVal->getIntegerConstantExpr(Ctx: Context)->getLimitedValue();
16966 FactorLoc = FactorVal->getExprLoc();
16967 } else {
16968 // TODO: Use a better profitability model.
16969 Factor = llvm::omp::DefaultPartialUnrollFactor;
16970 }
16971 assert(Factor > 0 && "Expected positive unroll factor");
16972 auto MakeFactorExpr = [this, Factor, IVTy, FactorLoc]() {
16973 return IntegerLiteral::Create(
16974 C: getASTContext(), V: llvm::APInt(getASTContext().getIntWidth(T: IVTy), Factor),
16975 type: IVTy, l: FactorLoc);
16976 };
16977
16978 // Iteration variable SourceLocations.
16979 SourceLocation OrigVarLoc = OrigVar->getExprLoc();
16980 SourceLocation OrigVarLocBegin = OrigVar->getBeginLoc();
16981 SourceLocation OrigVarLocEnd = OrigVar->getEndLoc();
16982
16983 // Internal variable names.
16984 std::string OrigVarName = OrigVar->getNameInfo().getAsString();
16985 std::string OuterIVName = (Twine(".unrolled.iv.") + OrigVarName).str();
16986 std::string InnerIVName = (Twine(".unroll_inner.iv.") + OrigVarName).str();
16987
16988 // Create the iteration variable for the unrolled loop.
16989 VarDecl *OuterIVDecl =
16990 buildVarDecl(SemaRef, Loc: {}, Type: IVTy, Name: OuterIVName, Attrs: nullptr, OrigRef: OrigVar);
16991 auto MakeOuterRef = [this, OuterIVDecl, IVTy, OrigVarLoc]() {
16992 return buildDeclRefExpr(S&: SemaRef, D: OuterIVDecl, Ty: IVTy, Loc: OrigVarLoc);
16993 };
16994
16995 // Iteration variable for the inner loop: Reuse the iteration variable created
16996 // by checkOpenMPLoop.
16997 auto *InnerIVDecl = cast<VarDecl>(Val: IterationVarRef->getDecl());
16998 InnerIVDecl->setDeclName(&SemaRef.PP.getIdentifierTable().get(Name: InnerIVName));
16999 auto MakeInnerRef = [this, InnerIVDecl, IVTy, OrigVarLoc]() {
17000 return buildDeclRefExpr(S&: SemaRef, D: InnerIVDecl, Ty: IVTy, Loc: OrigVarLoc);
17001 };
17002
17003 // Make a copy of the NumIterations expression for each use: By the AST
17004 // constraints, every expression object in a DeclContext must be unique.
17005 CaptureVars CopyTransformer(SemaRef);
17006 auto MakeNumIterations = [&CopyTransformer, &LoopHelper]() -> Expr * {
17007 return AssertSuccess(
17008 R: CopyTransformer.TransformExpr(E: LoopHelper.NumIterations));
17009 };
17010
17011 // Inner For init-statement: auto .unroll_inner.iv = .unrolled.iv
17012 ExprResult LValueConv = SemaRef.DefaultLvalueConversion(E: MakeOuterRef());
17013 SemaRef.AddInitializerToDecl(dcl: InnerIVDecl, init: LValueConv.get(),
17014 /*DirectInit=*/false);
17015 StmtResult InnerInit = new (Context)
17016 DeclStmt(DeclGroupRef(InnerIVDecl), OrigVarLocBegin, OrigVarLocEnd);
17017 if (!InnerInit.isUsable())
17018 return StmtError();
17019
17020 // Inner For cond-expression:
17021 // \code
17022 // .unroll_inner.iv < .unrolled.iv + Factor &&
17023 // .unroll_inner.iv < NumIterations
17024 // \endcode
17025 // This conjunction of two conditions allows ScalarEvolution to derive the
17026 // maximum trip count of the inner loop.
17027 ExprResult EndOfTile =
17028 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_Add,
17029 LHSExpr: MakeOuterRef(), RHSExpr: MakeFactorExpr());
17030 if (!EndOfTile.isUsable())
17031 return StmtError();
17032 ExprResult InnerCond1 =
17033 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
17034 LHSExpr: MakeInnerRef(), RHSExpr: EndOfTile.get());
17035 if (!InnerCond1.isUsable())
17036 return StmtError();
17037 ExprResult InnerCond2 =
17038 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
17039 LHSExpr: MakeInnerRef(), RHSExpr: MakeNumIterations());
17040 if (!InnerCond2.isUsable())
17041 return StmtError();
17042 ExprResult InnerCond =
17043 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LAnd,
17044 LHSExpr: InnerCond1.get(), RHSExpr: InnerCond2.get());
17045 if (!InnerCond.isUsable())
17046 return StmtError();
17047
17048 // Inner For incr-statement: ++.unroll_inner.iv
17049 ExprResult InnerIncr = SemaRef.BuildUnaryOp(
17050 S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(), Opc: UO_PreInc, Input: MakeInnerRef());
17051 if (!InnerIncr.isUsable())
17052 return StmtError();
17053
17054 // Inner For statement.
17055 SmallVector<Stmt *> InnerBodyStmts;
17056 InnerBodyStmts.append(in_start: LoopHelper.Updates.begin(), in_end: LoopHelper.Updates.end());
17057 if (auto *CXXRangeFor = dyn_cast<CXXForRangeStmt>(Val: LoopStmt))
17058 InnerBodyStmts.push_back(Elt: CXXRangeFor->getLoopVarStmt());
17059 InnerBodyStmts.push_back(Elt: Body);
17060 CompoundStmt *InnerBody =
17061 CompoundStmt::Create(C: getASTContext(), Stmts: InnerBodyStmts, FPFeatures: FPOptionsOverride(),
17062 LB: Body->getBeginLoc(), RB: Body->getEndLoc());
17063 ForStmt *InnerFor = new (Context)
17064 ForStmt(Context, InnerInit.get(), InnerCond.get(), nullptr,
17065 InnerIncr.get(), InnerBody, LoopHelper.Init->getBeginLoc(),
17066 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
17067
17068 // Unroll metadata for the inner loop.
17069 // This needs to take into account the remainder portion of the unrolled loop,
17070 // hence `unroll(full)` does not apply here, even though the LoopUnroll pass
17071 // supports multiple loop exits. Instead, unroll using a factor equivalent to
17072 // the maximum trip count, which will also generate a remainder loop. Just
17073 // `unroll(enable)` (which could have been useful if the user has not
17074 // specified a concrete factor; even though the outer loop cannot be
17075 // influenced anymore, would avoid more code bloat than necessary) will refuse
17076 // the loop because "Won't unroll; remainder loop could not be generated when
17077 // assuming runtime trip count". Even if it did work, it must not choose a
17078 // larger unroll factor than the maximum loop length, or it would always just
17079 // execute the remainder loop.
17080 LoopHintAttr *UnrollHintAttr =
17081 LoopHintAttr::CreateImplicit(Ctx&: Context, Option: LoopHintAttr::UnrollCount,
17082 State: LoopHintAttr::Numeric, Value: MakeFactorExpr());
17083 AttributedStmt *InnerUnrolled = AttributedStmt::Create(
17084 C: getASTContext(), Loc: StartLoc, Attrs: {UnrollHintAttr}, SubStmt: InnerFor);
17085
17086 // Outer For init-statement: auto .unrolled.iv = 0
17087 SemaRef.AddInitializerToDecl(
17088 dcl: OuterIVDecl,
17089 init: SemaRef.ActOnIntegerConstant(Loc: LoopHelper.Init->getExprLoc(), Val: 0).get(),
17090 /*DirectInit=*/false);
17091 StmtResult OuterInit = new (Context)
17092 DeclStmt(DeclGroupRef(OuterIVDecl), OrigVarLocBegin, OrigVarLocEnd);
17093 if (!OuterInit.isUsable())
17094 return StmtError();
17095
17096 // Outer For cond-expression: .unrolled.iv < NumIterations
17097 ExprResult OuterConde =
17098 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
17099 LHSExpr: MakeOuterRef(), RHSExpr: MakeNumIterations());
17100 if (!OuterConde.isUsable())
17101 return StmtError();
17102
17103 // Outer For incr-statement: .unrolled.iv += Factor
17104 ExprResult OuterIncr =
17105 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(), Opc: BO_AddAssign,
17106 LHSExpr: MakeOuterRef(), RHSExpr: MakeFactorExpr());
17107 if (!OuterIncr.isUsable())
17108 return StmtError();
17109
17110 // Outer For statement.
17111 ForStmt *OuterFor = new (Context)
17112 ForStmt(Context, OuterInit.get(), OuterConde.get(), nullptr,
17113 OuterIncr.get(), InnerUnrolled, LoopHelper.Init->getBeginLoc(),
17114 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
17115
17116 return OMPUnrollDirective::Create(
17117 C: Context, StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt, NumGeneratedTopLevelLoops,
17118 TransformedStmt: OuterFor, PreInits: buildPreInits(Context, PreInits),
17119 Finals: buildLoopFinalization(Context, LoopHelpers, LoopStmts: LoopStmt, NestedFinals));
17120}
17121
17122StmtResult SemaOpenMP::ActOnOpenMPReverseDirective(Stmt *AStmt,
17123 SourceLocation StartLoc,
17124 SourceLocation EndLoc) {
17125 ASTContext &Context = getASTContext();
17126 Scope *CurScope = SemaRef.getCurScope();
17127
17128 // Empty statement should only be possible if there already was an error.
17129 if (!AStmt)
17130 return StmtError();
17131
17132 constexpr unsigned NumLoops = 1;
17133 Stmt *Body = nullptr;
17134 SmallVector<OMPLoopBasedDirective::HelperExprs, NumLoops> LoopHelpers(
17135 NumLoops);
17136 SmallVector<SmallVector<Stmt *>, NumLoops + 1> OriginalInits;
17137 SmallVector<Stmt *, 4> NestedFinals;
17138 if (!checkTransformableLoopNest(Kind: OMPD_reverse, AStmt, NumLoops, LoopHelpers,
17139 Body, OriginalInits, NestedFinals: &NestedFinals))
17140 return StmtError();
17141
17142 // Delay applying the transformation to when template is completely
17143 // instantiated.
17144 if (SemaRef.CurContext->isDependentContext())
17145 return OMPReverseDirective::Create(C: Context, StartLoc, EndLoc, AssociatedStmt: AStmt,
17146 NumLoops, TransformedStmt: nullptr, PreInits: nullptr, Finals: nullptr);
17147
17148 assert(LoopHelpers.size() == NumLoops &&
17149 "Expecting a single-dimensional loop iteration space");
17150 assert(OriginalInits.size() == NumLoops &&
17151 "Expecting a single-dimensional loop iteration space");
17152 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers.front();
17153
17154 // Find the loop statement.
17155 Stmt *LoopStmt = nullptr;
17156 collectLoopStmts(AStmt, LoopStmts: {LoopStmt});
17157
17158 // Determine the PreInit declarations.
17159 SmallVector<Stmt *> PreInits;
17160 addLoopPreInits(Context, LoopHelper, LoopStmt, OriginalInit: OriginalInits[0], PreInits);
17161
17162 auto *IterationVarRef = cast<DeclRefExpr>(Val: LoopHelper.IterationVarRef);
17163 QualType IVTy = IterationVarRef->getType();
17164 uint64_t IVWidth = Context.getTypeSize(T: IVTy);
17165 auto *OrigVar = cast<DeclRefExpr>(Val: LoopHelper.Counters.front());
17166
17167 // Iteration variable SourceLocations.
17168 SourceLocation OrigVarLoc = OrigVar->getExprLoc();
17169 SourceLocation OrigVarLocBegin = OrigVar->getBeginLoc();
17170 SourceLocation OrigVarLocEnd = OrigVar->getEndLoc();
17171
17172 // Locations pointing to the transformation.
17173 SourceLocation TransformLoc = StartLoc;
17174 SourceLocation TransformLocBegin = StartLoc;
17175 SourceLocation TransformLocEnd = EndLoc;
17176
17177 // Internal variable names.
17178 std::string OrigVarName = OrigVar->getNameInfo().getAsString();
17179 SmallString<64> ForwardIVName(".forward.iv.");
17180 ForwardIVName += OrigVarName;
17181 SmallString<64> ReversedIVName(".reversed.iv.");
17182 ReversedIVName += OrigVarName;
17183
17184 // LoopHelper.Updates will read the logical iteration number from
17185 // LoopHelper.IterationVarRef, compute the value of the user loop counter of
17186 // that logical iteration from it, then assign it to the user loop counter
17187 // variable. We cannot directly use LoopHelper.IterationVarRef as the
17188 // induction variable of the generated loop because it may cause an underflow:
17189 // \code{.c}
17190 // for (unsigned i = 0; i < n; ++i)
17191 // body(i);
17192 // \endcode
17193 //
17194 // Naive reversal:
17195 // \code{.c}
17196 // for (unsigned i = n-1; i >= 0; --i)
17197 // body(i);
17198 // \endcode
17199 //
17200 // Instead, we introduce a new iteration variable representing the logical
17201 // iteration counter of the original loop, convert it to the logical iteration
17202 // number of the reversed loop, then let LoopHelper.Updates compute the user's
17203 // loop iteration variable from it.
17204 // \code{.cpp}
17205 // for (auto .forward.iv = 0; .forward.iv < n; ++.forward.iv) {
17206 // auto .reversed.iv = n - .forward.iv - 1;
17207 // i = (.reversed.iv + 0) * 1; // LoopHelper.Updates
17208 // body(i); // Body
17209 // }
17210 // \endcode
17211
17212 // Subexpressions with more than one use. One of the constraints of an AST is
17213 // that every node object must appear at most once, hence we define a lambda
17214 // that creates a new AST node at every use.
17215 CaptureVars CopyTransformer(SemaRef);
17216 auto MakeNumIterations = [&CopyTransformer, &LoopHelper]() -> Expr * {
17217 return AssertSuccess(
17218 R: CopyTransformer.TransformExpr(E: LoopHelper.NumIterations));
17219 };
17220
17221 // Create the iteration variable for the forward loop (from 0 to n-1).
17222 VarDecl *ForwardIVDecl =
17223 buildVarDecl(SemaRef, Loc: {}, Type: IVTy, Name: ForwardIVName, Attrs: nullptr, OrigRef: OrigVar);
17224 auto MakeForwardRef = [&SemaRef = this->SemaRef, ForwardIVDecl, IVTy,
17225 OrigVarLoc]() {
17226 return buildDeclRefExpr(S&: SemaRef, D: ForwardIVDecl, Ty: IVTy, Loc: OrigVarLoc);
17227 };
17228
17229 // Iteration variable for the reversed induction variable (from n-1 downto 0):
17230 // Reuse the iteration variable created by checkOpenMPLoop.
17231 auto *ReversedIVDecl = cast<VarDecl>(Val: IterationVarRef->getDecl());
17232 ReversedIVDecl->setDeclName(
17233 &SemaRef.PP.getIdentifierTable().get(Name: ReversedIVName));
17234
17235 // For init-statement:
17236 // \code{.cpp}
17237 // auto .forward.iv = 0;
17238 // \endcode
17239 auto *Zero = IntegerLiteral::Create(C: Context, V: llvm::APInt::getZero(numBits: IVWidth),
17240 type: ForwardIVDecl->getType(), l: OrigVarLoc);
17241 SemaRef.AddInitializerToDecl(dcl: ForwardIVDecl, init: Zero, /*DirectInit=*/false);
17242 StmtResult Init = new (Context)
17243 DeclStmt(DeclGroupRef(ForwardIVDecl), OrigVarLocBegin, OrigVarLocEnd);
17244 if (!Init.isUsable())
17245 return StmtError();
17246
17247 // Forward iv cond-expression:
17248 // \code{.cpp}
17249 // .forward.iv < MakeNumIterations()
17250 // \endcode
17251 ExprResult Cond =
17252 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
17253 LHSExpr: MakeForwardRef(), RHSExpr: MakeNumIterations());
17254 if (!Cond.isUsable())
17255 return StmtError();
17256
17257 // Forward incr-statement:
17258 // \code{.c}
17259 // ++.forward.iv
17260 // \endcode
17261 ExprResult Incr = SemaRef.BuildUnaryOp(S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(),
17262 Opc: UO_PreInc, Input: MakeForwardRef());
17263 if (!Incr.isUsable())
17264 return StmtError();
17265
17266 // Reverse the forward-iv:
17267 // \code{.cpp}
17268 // auto .reversed.iv = MakeNumIterations() - 1 - .forward.iv
17269 // \endcode
17270 auto *One = IntegerLiteral::Create(C: Context, V: llvm::APInt(IVWidth, 1), type: IVTy,
17271 l: TransformLoc);
17272 ExprResult Minus = SemaRef.BuildBinOp(S: CurScope, OpLoc: TransformLoc, Opc: BO_Sub,
17273 LHSExpr: MakeNumIterations(), RHSExpr: One);
17274 if (!Minus.isUsable())
17275 return StmtError();
17276 Minus = SemaRef.BuildBinOp(S: CurScope, OpLoc: TransformLoc, Opc: BO_Sub, LHSExpr: Minus.get(),
17277 RHSExpr: MakeForwardRef());
17278 if (!Minus.isUsable())
17279 return StmtError();
17280 StmtResult InitReversed = new (Context) DeclStmt(
17281 DeclGroupRef(ReversedIVDecl), TransformLocBegin, TransformLocEnd);
17282 if (!InitReversed.isUsable())
17283 return StmtError();
17284 SemaRef.AddInitializerToDecl(dcl: ReversedIVDecl, init: Minus.get(),
17285 /*DirectInit=*/false);
17286
17287 // The new loop body.
17288 SmallVector<Stmt *, 4> BodyStmts;
17289 BodyStmts.reserve(N: LoopHelper.Updates.size() + 2 +
17290 (isa<CXXForRangeStmt>(Val: LoopStmt) ? 1 : 0));
17291 BodyStmts.push_back(Elt: InitReversed.get());
17292 llvm::append_range(C&: BodyStmts, R&: LoopHelper.Updates);
17293 if (auto *CXXRangeFor = dyn_cast<CXXForRangeStmt>(Val: LoopStmt))
17294 BodyStmts.push_back(Elt: CXXRangeFor->getLoopVarStmt());
17295 BodyStmts.push_back(Elt: Body);
17296 auto *ReversedBody =
17297 CompoundStmt::Create(C: Context, Stmts: BodyStmts, FPFeatures: FPOptionsOverride(),
17298 LB: Body->getBeginLoc(), RB: Body->getEndLoc());
17299
17300 // Finally create the reversed For-statement.
17301 auto *ReversedFor = new (Context)
17302 ForStmt(Context, Init.get(), Cond.get(), nullptr, Incr.get(),
17303 ReversedBody, LoopHelper.Init->getBeginLoc(),
17304 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
17305 return OMPReverseDirective::Create(
17306 C: Context, StartLoc, EndLoc, AssociatedStmt: AStmt, NumLoops, TransformedStmt: ReversedFor,
17307 PreInits: buildPreInits(Context, PreInits),
17308 Finals: buildLoopFinalization(Context, LoopHelpers, LoopStmts: LoopStmt, NestedFinals));
17309}
17310
17311/// Build the AST for \#pragma omp split counts(c1, c2, ...).
17312///
17313/// Splits the single associated loop into N consecutive loops, where N is the
17314/// number of count expressions.
17315StmtResult SemaOpenMP::ActOnOpenMPSplitDirective(ArrayRef<OMPClause *> Clauses,
17316 Stmt *AStmt,
17317 SourceLocation StartLoc,
17318 SourceLocation EndLoc) {
17319 ASTContext &Context = getASTContext();
17320 Scope *CurScope = SemaRef.getCurScope();
17321
17322 // Empty statement should only be possible if there already was an error.
17323 if (!AStmt)
17324 return StmtError();
17325
17326 const auto *CountsClause =
17327 OMPExecutableDirective::getSingleClause<OMPCountsClause>(Clauses);
17328 if (!CountsClause)
17329 return StmtError();
17330
17331 // Split applies to a single loop; check it is transformable and get helpers.
17332 constexpr unsigned NumLoops = 1;
17333 Stmt *Body = nullptr;
17334 SmallVector<OMPLoopBasedDirective::HelperExprs, NumLoops> LoopHelpers(
17335 NumLoops);
17336 SmallVector<SmallVector<Stmt *>, NumLoops + 1> OriginalInits;
17337 SmallVector<Stmt *, 4> NestedFinals;
17338 if (!checkTransformableLoopNest(Kind: OMPD_split, AStmt, NumLoops, LoopHelpers,
17339 Body, OriginalInits, NestedFinals: &NestedFinals))
17340 return StmtError();
17341
17342 // Delay applying the transformation to when template is completely
17343 // instantiated.
17344 if (SemaRef.CurContext->isDependentContext())
17345 return OMPSplitDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
17346 NumLoops, AssociatedStmt: AStmt, TransformedStmt: nullptr, PreInits: nullptr,
17347 Finals: nullptr);
17348
17349 assert(LoopHelpers.size() == NumLoops &&
17350 "Expecting a single-dimensional loop iteration space");
17351 assert(OriginalInits.size() == NumLoops &&
17352 "Expecting a single-dimensional loop iteration space");
17353 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers.front();
17354
17355 // Find the loop statement.
17356 Stmt *LoopStmt = nullptr;
17357 collectLoopStmts(AStmt, LoopStmts: {LoopStmt});
17358
17359 // Determine the PreInit declarations.
17360 SmallVector<Stmt *> PreInits;
17361 addLoopPreInits(Context, LoopHelper, LoopStmt, OriginalInit: OriginalInits[0], PreInits);
17362
17363 // Type and name of the original loop variable; we create one IV per segment
17364 // and assign it to the original var so the body sees the same name.
17365 auto *IterationVarRef = cast<DeclRefExpr>(Val: LoopHelper.IterationVarRef);
17366 QualType IVTy = IterationVarRef->getType();
17367 uint64_t IVWidth = Context.getTypeSize(T: IVTy);
17368 auto *OrigVar = cast<DeclRefExpr>(Val: LoopHelper.Counters.front());
17369
17370 // Iteration variable SourceLocations.
17371 SourceLocation OrigVarLoc = OrigVar->getExprLoc();
17372 SourceLocation OrigVarLocBegin = OrigVar->getBeginLoc();
17373 SourceLocation OrigVarLocEnd = OrigVar->getEndLoc();
17374 // Internal variable names.
17375 std::string OrigVarName = OrigVar->getNameInfo().getAsString();
17376
17377 if (!CountsClause->hasOmpFill())
17378 return StmtError();
17379 unsigned FillIdx = *CountsClause->getOmpFillIndex();
17380
17381 unsigned NumItems = CountsClause->getNumCounts();
17382 SmallVector<uint64_t, 4> CountValues(NumItems, 0);
17383 ArrayRef<Expr *> Refs = CountsClause->getCountsRefs();
17384 for (unsigned I = 0; I < NumItems; ++I) {
17385 if (I == FillIdx)
17386 continue;
17387 Expr *CountExpr = Refs[I];
17388 if (!CountExpr)
17389 return OMPSplitDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
17390 NumLoops, AssociatedStmt: AStmt, TransformedStmt: nullptr, PreInits: nullptr,
17391 Finals: nullptr);
17392 std::optional<llvm::APSInt> OptVal =
17393 CountExpr->getIntegerConstantExpr(Ctx: Context);
17394 if (!OptVal || OptVal->isNegative())
17395 return OMPSplitDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
17396 NumLoops, AssociatedStmt: AStmt, TransformedStmt: nullptr, PreInits: nullptr,
17397 Finals: nullptr);
17398 CountValues[I] = OptVal->getLimitedValue();
17399 }
17400
17401 Expr *NumIterExpr = LoopHelper.NumIterations;
17402
17403 uint64_t RightSum = 0;
17404 for (unsigned I = FillIdx + 1; I < NumItems; ++I)
17405 RightSum += CountValues[I];
17406
17407 auto MakeIntLit = [&](uint64_t Val) {
17408 return IntegerLiteral::Create(C: Context, V: llvm::APInt(IVWidth, Val), type: IVTy,
17409 l: OrigVarLoc);
17410 };
17411
17412 size_t NumSegments = NumItems;
17413 SmallVector<Stmt *, 4> SplitLoops;
17414
17415 auto *IterVarDecl = cast<VarDecl>(Val: IterationVarRef->getDecl());
17416 SplitLoops.push_back(Elt: new (Context) DeclStmt(DeclGroupRef(IterVarDecl),
17417 IterationVarRef->getBeginLoc(),
17418 IterationVarRef->getEndLoc()));
17419
17420 uint64_t LeftAccum = 0;
17421 uint64_t RightRemaining = RightSum;
17422
17423 for (size_t Seg = 0; Seg < NumSegments; ++Seg) {
17424 Expr *StartExpr = nullptr;
17425 Expr *EndExpr = nullptr;
17426
17427 if (Seg < FillIdx) {
17428 StartExpr = MakeIntLit(LeftAccum);
17429 LeftAccum += CountValues[Seg];
17430 EndExpr = MakeIntLit(LeftAccum);
17431 } else if (Seg == FillIdx) {
17432 StartExpr = MakeIntLit(LeftAccum);
17433 if (RightRemaining == 0) {
17434 EndExpr = NumIterExpr;
17435 } else {
17436 ExprResult Sub =
17437 SemaRef.BuildBinOp(S: CurScope, OpLoc: OrigVarLoc, Opc: BO_Sub, LHSExpr: NumIterExpr,
17438 RHSExpr: MakeIntLit(RightRemaining));
17439 if (!Sub.isUsable())
17440 return StmtError();
17441 EndExpr = Sub.get();
17442 }
17443 } else {
17444 if (RightRemaining == RightSum) {
17445 if (RightSum == 0)
17446 StartExpr = NumIterExpr;
17447 else {
17448 ExprResult Sub =
17449 SemaRef.BuildBinOp(S: CurScope, OpLoc: OrigVarLoc, Opc: BO_Sub, LHSExpr: NumIterExpr,
17450 RHSExpr: MakeIntLit(RightRemaining));
17451 if (!Sub.isUsable())
17452 return StmtError();
17453 StartExpr = Sub.get();
17454 }
17455 } else {
17456 ExprResult Sub =
17457 SemaRef.BuildBinOp(S: CurScope, OpLoc: OrigVarLoc, Opc: BO_Sub, LHSExpr: NumIterExpr,
17458 RHSExpr: MakeIntLit(RightRemaining));
17459 if (!Sub.isUsable())
17460 return StmtError();
17461 StartExpr = Sub.get();
17462 }
17463 RightRemaining -= CountValues[Seg];
17464 if (RightRemaining == 0)
17465 EndExpr = NumIterExpr;
17466 else {
17467 ExprResult Sub =
17468 SemaRef.BuildBinOp(S: CurScope, OpLoc: OrigVarLoc, Opc: BO_Sub, LHSExpr: NumIterExpr,
17469 RHSExpr: MakeIntLit(RightRemaining));
17470 if (!Sub.isUsable())
17471 return StmtError();
17472 EndExpr = Sub.get();
17473 }
17474 }
17475
17476 SmallString<64> IVName(".split.iv.");
17477 IVName += (Twine(Seg) + "." + OrigVarName).str();
17478 VarDecl *IVDecl = buildVarDecl(SemaRef, Loc: {}, Type: IVTy, Name: IVName, Attrs: nullptr, OrigRef: OrigVar);
17479 auto MakeIVRef = [&SemaRef = this->SemaRef, IVDecl, IVTy, OrigVarLoc]() {
17480 return buildDeclRefExpr(S&: SemaRef, D: IVDecl, Ty: IVTy, Loc: OrigVarLoc);
17481 };
17482
17483 SemaRef.AddInitializerToDecl(dcl: IVDecl, init: StartExpr, /*DirectInit=*/false);
17484 StmtResult InitStmt = new (Context)
17485 DeclStmt(DeclGroupRef(IVDecl), OrigVarLocBegin, OrigVarLocEnd);
17486 if (!InitStmt.isUsable())
17487 return StmtError();
17488
17489 ExprResult CondExpr = SemaRef.BuildBinOp(
17490 S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT, LHSExpr: MakeIVRef(), RHSExpr: EndExpr);
17491 if (!CondExpr.isUsable())
17492 return StmtError();
17493
17494 ExprResult IncrExpr = SemaRef.BuildUnaryOp(
17495 S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(), Opc: UO_PreInc, Input: MakeIVRef());
17496 if (!IncrExpr.isUsable())
17497 return StmtError();
17498
17499 ExprResult IVAssign = SemaRef.BuildBinOp(S: CurScope, OpLoc: OrigVarLoc, Opc: BO_Assign,
17500 LHSExpr: IterationVarRef, RHSExpr: MakeIVRef());
17501 if (!IVAssign.isUsable())
17502 return StmtError();
17503
17504 SmallVector<Stmt *, 4> BodyStmts;
17505 BodyStmts.push_back(Elt: IVAssign.get());
17506 BodyStmts.append(in_start: LoopHelper.Updates.begin(), in_end: LoopHelper.Updates.end());
17507 if (auto *CXXRangeFor = dyn_cast<CXXForRangeStmt>(Val: LoopStmt)) {
17508 if (Seg == 0) {
17509 BodyStmts.push_back(Elt: CXXRangeFor->getLoopVarStmt());
17510 } else {
17511 VarDecl *LoopVar = CXXRangeFor->getLoopVariable();
17512 DeclRefExpr *LVRef = buildDeclRefExpr(
17513 S&: SemaRef, D: LoopVar, Ty: LoopVar->getType().getNonReferenceType(),
17514 Loc: OrigVarLoc);
17515 ExprResult LVAssign = SemaRef.BuildBinOp(
17516 S: CurScope, OpLoc: OrigVarLoc, Opc: BO_Assign, LHSExpr: LVRef, RHSExpr: LoopVar->getInit());
17517 if (!LVAssign.isUsable())
17518 return StmtError();
17519 BodyStmts.push_back(Elt: LVAssign.get());
17520 }
17521 }
17522 BodyStmts.push_back(Elt: Body);
17523
17524 auto *LoopBody =
17525 CompoundStmt::Create(C: Context, Stmts: BodyStmts, FPFeatures: FPOptionsOverride(),
17526 LB: Body->getBeginLoc(), RB: Body->getEndLoc());
17527
17528 auto *For = new (Context)
17529 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr,
17530 IncrExpr.get(), LoopBody, LoopHelper.Init->getBeginLoc(),
17531 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
17532 SplitLoops.push_back(Elt: For);
17533 }
17534
17535 auto *SplitStmt = CompoundStmt::Create(
17536 C: Context, Stmts: SplitLoops, FPFeatures: FPOptionsOverride(),
17537 LB: SplitLoops.front()->getBeginLoc(), RB: SplitLoops.back()->getEndLoc());
17538
17539 return OMPSplitDirective::Create(
17540 C: Context, StartLoc, EndLoc, Clauses, NumLoops, AssociatedStmt: AStmt, TransformedStmt: SplitStmt,
17541 PreInits: buildPreInits(Context, PreInits),
17542 Finals: buildLoopFinalization(Context, LoopHelpers, LoopStmts: LoopStmt, NestedFinals));
17543}
17544
17545StmtResult SemaOpenMP::ActOnOpenMPInterchangeDirective(
17546 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
17547 SourceLocation EndLoc) {
17548 ASTContext &Context = getASTContext();
17549 DeclContext *CurContext = SemaRef.CurContext;
17550 Scope *CurScope = SemaRef.getCurScope();
17551
17552 // Empty statement should only be possible if there already was an error.
17553 if (!AStmt)
17554 return StmtError();
17555
17556 // interchange without permutation clause swaps two loops.
17557 const OMPPermutationClause *PermutationClause =
17558 OMPExecutableDirective::getSingleClause<OMPPermutationClause>(Clauses);
17559 size_t NumLoops = PermutationClause ? PermutationClause->getNumLoops() : 2;
17560
17561 // Verify and diagnose loop nest.
17562 SmallVector<OMPLoopBasedDirective::HelperExprs, 4> LoopHelpers(NumLoops);
17563 Stmt *Body = nullptr;
17564 SmallVector<SmallVector<Stmt *>, 2> OriginalInits;
17565 SmallVector<Stmt *, 4> NestedFinals;
17566 if (!checkTransformableLoopNest(Kind: OMPD_interchange, AStmt, NumLoops,
17567 LoopHelpers, Body, OriginalInits,
17568 NestedFinals: &NestedFinals))
17569 return StmtError();
17570
17571 // Delay interchange to when template is completely instantiated.
17572 if (CurContext->isDependentContext())
17573 return OMPInterchangeDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
17574 NumLoops, AssociatedStmt: AStmt, TransformedStmt: nullptr, PreInits: nullptr,
17575 Finals: nullptr);
17576
17577 // An invalid expression in the permutation clause is set to nullptr in
17578 // ActOnOpenMPPermutationClause.
17579 if (PermutationClause &&
17580 llvm::is_contained(Range: PermutationClause->getArgsRefs(), Element: nullptr))
17581 return StmtError();
17582
17583 assert(LoopHelpers.size() == NumLoops &&
17584 "Expecting loop iteration space dimensionaly to match number of "
17585 "affected loops");
17586 assert(OriginalInits.size() == NumLoops &&
17587 "Expecting loop iteration space dimensionaly to match number of "
17588 "affected loops");
17589
17590 // Decode the permutation clause.
17591 SmallVector<uint64_t, 2> Permutation;
17592 if (!PermutationClause) {
17593 Permutation = {1, 0};
17594 } else {
17595 ArrayRef<Expr *> PermArgs = PermutationClause->getArgsRefs();
17596 llvm::BitVector Flags(PermArgs.size());
17597 for (Expr *PermArg : PermArgs) {
17598 std::optional<llvm::APSInt> PermCstExpr =
17599 PermArg->getIntegerConstantExpr(Ctx: Context);
17600 if (!PermCstExpr)
17601 continue;
17602 uint64_t PermInt = PermCstExpr->getLimitedValue();
17603 assert(1 <= PermInt && PermInt <= NumLoops &&
17604 "Must be a permutation; diagnostic emitted in "
17605 "ActOnOpenMPPermutationClause");
17606 if (Flags[PermInt - 1]) {
17607 SourceRange ExprRange(PermArg->getBeginLoc(), PermArg->getEndLoc());
17608 Diag(Loc: PermArg->getExprLoc(),
17609 DiagID: diag::err_omp_interchange_permutation_value_repeated)
17610 << PermInt << ExprRange;
17611 continue;
17612 }
17613 Flags[PermInt - 1] = true;
17614
17615 Permutation.push_back(Elt: PermInt - 1);
17616 }
17617
17618 if (Permutation.size() != NumLoops)
17619 return StmtError();
17620 }
17621
17622 // Nothing to transform with trivial permutation.
17623 if (NumLoops <= 1 || llvm::all_of(Range: llvm::enumerate(First&: Permutation), P: [](auto P) {
17624 auto [Idx, Arg] = P;
17625 return Idx == Arg;
17626 }))
17627 return OMPInterchangeDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
17628 NumLoops, AssociatedStmt: AStmt, TransformedStmt: AStmt, PreInits: nullptr,
17629 Finals: nullptr);
17630
17631 // Find the affected loops.
17632 SmallVector<Stmt *> LoopStmts(NumLoops, nullptr);
17633 collectLoopStmts(AStmt, LoopStmts);
17634
17635 // Collect pre-init statements on the order before the permuation.
17636 SmallVector<Stmt *> PreInits;
17637 for (auto I : llvm::seq<int>(Size: NumLoops)) {
17638 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
17639
17640 assert(LoopHelper.Counters.size() == 1 &&
17641 "Single-dimensional loop iteration space expected");
17642
17643 guardNestedLoopInit(Context, LoopHelpers, I, OriginalInit&: OriginalInits[I]);
17644 addLoopPreInits(Context, LoopHelper, LoopStmt: LoopStmts[I], OriginalInit: OriginalInits[I],
17645 PreInits);
17646 }
17647
17648 SmallVector<VarDecl *> PermutedIndVars(NumLoops);
17649 CaptureVars CopyTransformer(SemaRef);
17650
17651 // True if no loop of the nest has a zero trip count.
17652 auto MakeNotEmpty = [&]() -> Expr * {
17653 Expr *NotEmpty = nullptr;
17654 for (const OMPLoopBasedDirective::HelperExprs &Helper : LoopHelpers) {
17655 Expr *NumIters =
17656 AssertSuccess(R: CopyTransformer.TransformExpr(E: Helper.NumIterations));
17657 Expr *HasIters = AssertSuccess(R: SemaRef.BuildBinOp(
17658 S: CurScope, OpLoc: NumIters->getExprLoc(), Opc: BO_NE, LHSExpr: NumIters,
17659 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: NumIters->getExprLoc(), Val: 0).get()));
17660 NotEmpty = NotEmpty ? AssertSuccess(R: SemaRef.BuildBinOp(
17661 S: CurScope, OpLoc: HasIters->getExprLoc(), Opc: BO_LAnd,
17662 LHSExpr: NotEmpty, RHSExpr: HasIters))
17663 : HasIters;
17664 }
17665 return NotEmpty;
17666 };
17667
17668 // Create the permuted loops from the inside to the outside of the
17669 // interchanged loop nest. Body of the innermost new loop is the original
17670 // innermost body.
17671 Stmt *Inner = Body;
17672 for (auto TargetIdx : llvm::reverse(C: llvm::seq<int>(Size: NumLoops))) {
17673 // Get the original loop that belongs to this new position.
17674 uint64_t SourceIdx = Permutation[TargetIdx];
17675 OMPLoopBasedDirective::HelperExprs &SourceHelper = LoopHelpers[SourceIdx];
17676 Stmt *SourceLoopStmt = LoopStmts[SourceIdx];
17677 assert(SourceHelper.Counters.size() == 1 &&
17678 "Single-dimensional loop iteration space expected");
17679 auto *OrigCntVar = cast<DeclRefExpr>(Val: SourceHelper.Counters.front());
17680
17681 // Normalized loop counter variable: From 0 to n-1, always an integer type.
17682 DeclRefExpr *IterVarRef = cast<DeclRefExpr>(Val: SourceHelper.IterationVarRef);
17683 QualType IVTy = IterVarRef->getType();
17684 assert(IVTy->isIntegerType() &&
17685 "Expected the logical iteration counter to be an integer");
17686
17687 std::string OrigVarName = OrigCntVar->getNameInfo().getAsString();
17688 SourceLocation OrigVarLoc = IterVarRef->getExprLoc();
17689
17690 // Make a copy of the NumIterations expression for each use: By the AST
17691 // constraints, every expression object in a DeclContext must be unique.
17692 auto MakeNumIterations = [&CopyTransformer, &SourceHelper]() -> Expr * {
17693 return AssertSuccess(
17694 R: CopyTransformer.TransformExpr(E: SourceHelper.NumIterations));
17695 };
17696
17697 // Iteration variable for the permuted loop. Reuse the one from
17698 // checkOpenMPLoop which will also be used to update the original loop
17699 // variable.
17700 SmallString<64> PermutedCntName(".permuted_");
17701 PermutedCntName.append(Refs: {llvm::utostr(X: TargetIdx), ".iv.", OrigVarName});
17702 auto *PermutedCntDecl = cast<VarDecl>(Val: IterVarRef->getDecl());
17703 PermutedCntDecl->setDeclName(
17704 &SemaRef.PP.getIdentifierTable().get(Name: PermutedCntName));
17705 PermutedIndVars[TargetIdx] = PermutedCntDecl;
17706 auto MakePermutedRef = [this, PermutedCntDecl, IVTy, OrigVarLoc]() {
17707 return buildDeclRefExpr(S&: SemaRef, D: PermutedCntDecl, Ty: IVTy, Loc: OrigVarLoc);
17708 };
17709
17710 // For init-statement:
17711 // \code
17712 // auto .permuted_{target}.iv = 0
17713 // \endcode
17714 ExprResult Zero = SemaRef.ActOnIntegerConstant(Loc: OrigVarLoc, Val: 0);
17715 if (!Zero.isUsable())
17716 return StmtError();
17717 SemaRef.AddInitializerToDecl(dcl: PermutedCntDecl, init: Zero.get(),
17718 /*DirectInit=*/false);
17719 StmtResult InitStmt = new (Context)
17720 DeclStmt(DeclGroupRef(PermutedCntDecl), OrigCntVar->getBeginLoc(),
17721 OrigCntVar->getEndLoc());
17722 if (!InitStmt.isUsable())
17723 return StmtError();
17724
17725 // For cond-expression:
17726 // \code
17727 // .permuted_{target}.iv < MakeNumIterations()
17728 // \endcode
17729 Expr *UpperBound = MakeNumIterations();
17730 // The outermost loop runs only if the iteration space is not empty, so
17731 // that no loop variable is assigned when an original loop has none.
17732 if (TargetIdx == 0) {
17733 ExprResult NoIterations = SemaRef.ActOnIntegerConstant(Loc: OrigVarLoc, Val: 0);
17734 ExprResult Guarded =
17735 SemaRef.ActOnConditionalOp(QuestionLoc: OrigVarLoc, ColonLoc: OrigVarLoc, CondExpr: MakeNotEmpty(),
17736 LHSExpr: UpperBound, RHSExpr: NoIterations.get());
17737 if (!Guarded.isUsable())
17738 return StmtError();
17739 UpperBound = Guarded.get();
17740 }
17741 ExprResult CondExpr =
17742 SemaRef.BuildBinOp(S: CurScope, OpLoc: SourceHelper.Cond->getExprLoc(), Opc: BO_LT,
17743 LHSExpr: MakePermutedRef(), RHSExpr: UpperBound);
17744 if (!CondExpr.isUsable())
17745 return StmtError();
17746
17747 // For incr-statement:
17748 // \code
17749 // ++.tile.iv
17750 // \endcode
17751 ExprResult IncrStmt = SemaRef.BuildUnaryOp(
17752 S: CurScope, OpLoc: SourceHelper.Inc->getExprLoc(), Opc: UO_PreInc, Input: MakePermutedRef());
17753 if (!IncrStmt.isUsable())
17754 return StmtError();
17755
17756 SmallVector<Stmt *, 4> BodyParts(SourceHelper.Updates.begin(),
17757 SourceHelper.Updates.end());
17758 if (auto *SourceCXXFor = dyn_cast<CXXForRangeStmt>(Val: SourceLoopStmt))
17759 BodyParts.push_back(Elt: SourceCXXFor->getLoopVarStmt());
17760 BodyParts.push_back(Elt: Inner);
17761 Inner = CompoundStmt::Create(C: Context, Stmts: BodyParts, FPFeatures: FPOptionsOverride(),
17762 LB: Inner->getBeginLoc(), RB: Inner->getEndLoc());
17763 Inner = new (Context) ForStmt(
17764 Context, InitStmt.get(), CondExpr.get(), nullptr, IncrStmt.get(), Inner,
17765 SourceHelper.Init->getBeginLoc(), SourceHelper.Init->getBeginLoc(),
17766 SourceHelper.Inc->getEndLoc());
17767 }
17768
17769 return OMPInterchangeDirective::Create(
17770 C: Context, StartLoc, EndLoc, Clauses, NumLoops, AssociatedStmt: AStmt, TransformedStmt: Inner,
17771 PreInits: buildPreInits(Context, PreInits),
17772 Finals: buildLoopFinalization(Context, LoopHelpers, LoopStmts, NestedFinals));
17773}
17774
17775StmtResult
17776SemaOpenMP::ActOnOpenMPFlattenDirective(ArrayRef<OMPClause *> Clauses,
17777 Stmt *AStmt, SourceLocation StartLoc,
17778 SourceLocation EndLoc) {
17779 ASTContext &Context = getASTContext();
17780 DeclContext *CurContext = SemaRef.CurContext;
17781 Scope *CurScope = SemaRef.getCurScope();
17782
17783 // Empty statement should only be possible if there already was an error.
17784 if (!AStmt)
17785 return StmtError();
17786
17787 // flatten without 'depth' clause combines two loops; 'depth(k)' selects k.
17788 unsigned NumLoops = 2;
17789 bool DepthIsDependent = false;
17790 const auto *DepthClause =
17791 OMPExecutableDirective::getSingleClause<OMPDepthClause>(Clauses);
17792 if (DepthClause) {
17793 Expr *DepthExpr = DepthClause->getDepth();
17794 if (DepthExpr && DepthExpr->isInstantiationDependent()) {
17795 DepthIsDependent = true;
17796 } else if (DepthExpr) {
17797 Expr::EvalResult EvalResult;
17798 if (DepthExpr->EvaluateAsInt(Result&: EvalResult, Ctx: Context))
17799 NumLoops = EvalResult.Val.getInt().getLimitedValue(
17800 Limit: std::numeric_limits<unsigned>::max());
17801 }
17802 }
17803
17804 // Count perfectly nested loops with doForAllLoops. When 'depth' is present,
17805 // walk NumLoops iterations to diagnose an insufficient nest. When it is
17806 // omitted, walk one extra loop (3 total) so we can warn that default
17807 // flatten only combines 2 of a deeper nest.
17808 if (!DepthIsDependent) {
17809 unsigned WalkLimit = DepthClause ? NumLoops : 3;
17810 unsigned Found = 0;
17811 bool Enough = OMPLoopBasedDirective::doForAllLoops(
17812 CurStmt: AStmt->IgnoreContainers(), /*TryImperfectlyNestedLoops=*/false,
17813 NumLoops: WalkLimit, Callback: [&](unsigned Cnt, Stmt *S) {
17814 if (!isa<ForStmt>(Val: S) && !isa<CXXForRangeStmt>(Val: S))
17815 return true;
17816 Found = Cnt + 1;
17817 return false;
17818 });
17819 if (DepthClause && !Enough) {
17820 Diag(Loc: AStmt->getBeginLoc(), DiagID: diag::err_omp_not_for)
17821 << /*expected N for loops form=*/1
17822 << getOpenMPDirectiveName(D: OMPD_flatten) << NumLoops << (Found > 0)
17823 << Found;
17824 return StmtError();
17825 }
17826 if (!DepthClause && Found >= 3) {
17827 Diag(Loc: StartLoc, DiagID: diag::warn_omp_flatten_omitted_depth);
17828 if (SemaRef.getLangOpts().OpenMP >= 61)
17829 Diag(Loc: StartLoc, DiagID: diag::note_omp_flatten_insert_depth)
17830 << FixItHint::CreateInsertion(InsertionLoc: EndLoc, Code: " depth(2)");
17831 }
17832 }
17833
17834 // Defer when 'depth' is instantiation-dependent (concrete k unknown until
17835 // instantiation).
17836 if (DepthIsDependent)
17837 return OMPFlattenDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
17838 NumLoops, AssociatedStmt: AStmt, TransformedStmt: nullptr, PreInits: nullptr,
17839 Finals: nullptr);
17840
17841 // Verify and diagnose loop nest.
17842 SmallVector<OMPLoopBasedDirective::HelperExprs, 4> LoopHelpers(NumLoops);
17843 Stmt *Body = nullptr;
17844 SmallVector<SmallVector<Stmt *>, 4> OriginalInits;
17845 SmallVector<Stmt *, 4> NestedFinals;
17846 if (!checkTransformableLoopNest(Kind: OMPD_flatten, AStmt, NumLoops, LoopHelpers,
17847 Body, OriginalInits, NestedFinals: &NestedFinals))
17848 return StmtError();
17849
17850 // Delay flattening to when template is completely instantiated.
17851 if (CurContext->isDependentContext())
17852 return OMPFlattenDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
17853 NumLoops, AssociatedStmt: AStmt, TransformedStmt: nullptr, PreInits: nullptr,
17854 Finals: nullptr);
17855
17856 assert(LoopHelpers.size() == NumLoops &&
17857 "Expecting loop iteration space dimensionality to match number of "
17858 "affected loops");
17859 assert(OriginalInits.size() == NumLoops &&
17860 "Expecting loop iteration space dimensionality to match number of "
17861 "affected loops");
17862
17863 // Find the affected loops.
17864 SmallVector<Stmt *> LoopStmts(NumLoops, nullptr);
17865 collectLoopStmts(AStmt, LoopStmts);
17866
17867 // Collect pre-init statements in outer-to-inner order.
17868 SmallVector<Stmt *> PreInits;
17869 for (auto I : llvm::seq<unsigned>(Size: NumLoops)) {
17870 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
17871 assert(LoopHelper.Counters.size() == 1 &&
17872 "Single-dimensional loop iteration space expected");
17873 guardNestedLoopInit(Context, LoopHelpers, I, OriginalInit&: OriginalInits[I]);
17874 addLoopPreInits(Context, LoopHelper, LoopStmt: LoopStmts[I], OriginalInit: OriginalInits[I],
17875 PreInits);
17876 }
17877
17878 CaptureVars CopyTransformer(SemaRef);
17879 // Convert a non-integer trip count. Width matches buildNumIterations.
17880 // Signedness follows an integer loop variable so a float count stays signed.
17881 auto MakeNumIterations = [&CopyTransformer, &LoopHelpers, &Context,
17882 &SemaRef =
17883 this->SemaRef](unsigned I) -> ExprResult {
17884 ExprResult N = CopyTransformer.TransformExpr(E: LoopHelpers[I].NumIterations);
17885 if (!N.isUsable())
17886 return ExprError();
17887 QualType Type = N.get()->getType();
17888 if (Type->isIntegerType())
17889 return N;
17890 const auto *Counter = cast<DeclRefExpr>(Val: LoopHelpers[I].Counters.front());
17891 QualType VarType = Counter->getType().getNonReferenceType();
17892 bool VarIsInt = VarType->hasIntegerRepresentation();
17893 bool UseVarWidth =
17894 VarIsInt && Context.getTypeSize(T: Type) > Context.getTypeSize(T: VarType);
17895 unsigned NewSize =
17896 UseVarWidth ? Context.getTypeSize(T: VarType) : Context.getTypeSize(T: Type);
17897 bool IsSigned = VarIsInt ? VarType->hasSignedIntegerRepresentation()
17898 : Type->hasFloatingRepresentation();
17899 QualType IntTy = getIntTypeForBitwidthOrBitInt(C&: Context, Bits: NewSize, Signed: IsSigned);
17900 N = SemaRef.PerformImplicitConversion(
17901 From: N.get(), ToType: IntTy, Action: AssignmentAction::Converting, /*AllowExplicit=*/true);
17902 if (!N.isUsable())
17903 return ExprError();
17904 return N;
17905 };
17906
17907 OMPLoopBasedDirective::HelperExprs &OutermostHelper = LoopHelpers[0];
17908 auto *OutermostCntVar = cast<DeclRefExpr>(Val: OutermostHelper.Counters.front());
17909 SourceLocation OrigVarLoc = OutermostCntVar->getExprLoc();
17910 SourceLocation OrigVarLocBegin = OutermostCntVar->getBeginLoc();
17911 SourceLocation OrigVarLocEnd = OutermostCntVar->getEndLoc();
17912 SourceLocation CondLoc = OutermostHelper.Cond->getExprLoc();
17913
17914 // Product of trip counts; mirror 'collapse' IV-width selection to avoid
17915 // overflow when several counts are multiplied.
17916 auto BuildTripCount = [&](unsigned Bits) -> ExprResult {
17917 ExprResult Product;
17918 for (unsigned I = 0; I < NumLoops; ++I) {
17919 ExprResult N = MakeNumIterations(I);
17920 if (!N.isUsable())
17921 return ExprError();
17922 N = widenIterationCount(Bits, E: N.get(), SemaRef);
17923 if (!N.isUsable())
17924 return ExprError();
17925 if (I == 0)
17926 Product = N;
17927 else
17928 Product = SemaRef.BuildBinOp(S: CurScope, OpLoc: CondLoc, Opc: BO_Mul, LHSExpr: Product.get(),
17929 RHSExpr: N.get());
17930 if (!Product.isUsable())
17931 return ExprError();
17932 }
17933 return Product;
17934 };
17935
17936 bool AllCountsLessThan32Bits =
17937 llvm::all_of(Range: llvm::seq<unsigned>(Size: NumLoops), P: [&](unsigned I) {
17938 return Context.getTypeSize(T: LoopHelpers[I].NumIterations->getType()) <
17939 32;
17940 });
17941
17942 ExprResult TripCount;
17943 if (AllCountsLessThan32Bits || NumLoops == 1) {
17944 TripCount = BuildTripCount(/*Bits=*/32);
17945 } else {
17946 ExprResult TripCount64 = BuildTripCount(/*Bits=*/64);
17947 if (!TripCount64.isUsable())
17948 return StmtError();
17949 TripCount = TripCount64;
17950 if (TripCount64.get()->isIntegerConstantExpr(Ctx: Context)) {
17951 ExprResult TripCount32 = BuildTripCount(/*Bits=*/32);
17952 if (TripCount32.isUsable() &&
17953 Context.getTypeSize(T: TripCount32.get()->getType()) == 32 &&
17954 fitsInto(
17955 /*Bits=*/32,
17956 Signed: TripCount32.get()->getType()->hasSignedIntegerRepresentation(),
17957 E: TripCount64.get(), SemaRef))
17958 TripCount = TripCount32;
17959 }
17960 }
17961 if (!TripCount.isUsable())
17962 return StmtError();
17963
17964 QualType IVTy = TripCount.get()->getType();
17965 uint64_t IVWidth = Context.getTypeSize(T: IVTy);
17966
17967 // Build a condition that is true if the outermost \p Count loops all have at
17968 // least one iteration.
17969 auto BuildHasIterations = [&](unsigned Count) -> ExprResult {
17970 ExprResult Result;
17971 for (unsigned I = 0; I < Count; ++I) {
17972 ExprResult LoopPreCond =
17973 CopyTransformer.TransformExpr(E: LoopHelpers[I].PreCond);
17974 if (!LoopPreCond.isUsable())
17975 return ExprError();
17976 if (I == 0)
17977 Result = LoopPreCond;
17978 else
17979 Result = SemaRef.BuildBinOp(S: CurScope, OpLoc: CondLoc, Opc: BO_LAnd, LHSExpr: Result.get(),
17980 RHSExpr: LoopPreCond.get());
17981 if (!Result.isUsable())
17982 return ExprError();
17983 }
17984 return Result;
17985 };
17986
17987 // NumIterations may wrap or overflow when an empty loop has extreme runtime
17988 // bounds. Test whether every original loop has at least one iteration before
17989 // evaluating their product.
17990 ExprResult HasIterations = BuildHasIterations(NumLoops);
17991 if (!HasIterations.isUsable())
17992 return StmtError();
17993 Expr *ZeroTripCount = IntegerLiteral::Create(
17994 C: Context, V: llvm::APInt::getZero(numBits: IVWidth), type: IVTy, l: CondLoc);
17995 TripCount = SemaRef.ActOnConditionalOp(QuestionLoc: CondLoc, ColonLoc: CondLoc, CondExpr: HasIterations.get(),
17996 LHSExpr: TripCount.get(), RHSExpr: ZeroTripCount);
17997 if (!TripCount.isUsable())
17998 return StmtError();
17999
18000 auto MakeNumIterationsInIVTy = [&](unsigned I) -> ExprResult {
18001 ExprResult N = MakeNumIterations(I);
18002 if (!N.isUsable())
18003 return ExprError();
18004 return SemaRef.PerformImplicitConversion(
18005 From: N.get(), ToType: IVTy, Action: AssignmentAction::Converting, /*AllowExplicit=*/true);
18006 };
18007
18008 // Divisors in index recovery use (N == 0 ? 1 : N) so a zero trip count does
18009 // not warn.
18010 auto MakeDivisorInIVTy = [&](unsigned I) -> ExprResult {
18011 ExprResult N = MakeNumIterationsInIVTy(I);
18012 ExprResult NCmp = MakeNumIterationsInIVTy(I);
18013 if (!N.isUsable() || !NCmp.isUsable())
18014 return ExprError();
18015 auto MakeCst = [&](uint64_t V) -> Expr * {
18016 return IntegerLiteral::Create(C: Context, V: llvm::APInt(IVWidth, V), type: IVTy,
18017 l: CondLoc);
18018 };
18019 ExprResult IsZero =
18020 SemaRef.BuildBinOp(S: CurScope, OpLoc: CondLoc, Opc: BO_EQ, LHSExpr: NCmp.get(), RHSExpr: MakeCst(0));
18021 if (!IsZero.isUsable())
18022 return N;
18023 return SemaRef.ActOnConditionalOp(QuestionLoc: CondLoc, ColonLoc: CondLoc, CondExpr: IsZero.get(),
18024 LHSExpr: MakeCst(1), RHSExpr: N.get());
18025 };
18026
18027 // \code{.cpp}
18028 // for (auto .flatten.iv = 0; .flatten.iv < n0 * n1 * ...; ++.flatten.iv) {
18029 // .flatten.iv.0 = .flatten.iv / (n1 * ...);
18030 // i0 = ...; // Updates[0]
18031 // .flatten.iv.1 = (.flatten.iv / ...) % n1;
18032 // i1 = ...; // Updates[1]
18033 // ...
18034 // body(i0, i1, ...);
18035 // }
18036 // \endcode
18037 SmallString<64> FlattenedIVName(".flatten.iv");
18038 VarDecl *FlattenedIVDecl = buildVarDecl(SemaRef, Loc: {}, Type: IVTy, Name: FlattenedIVName,
18039 Attrs: nullptr, OrigRef: OutermostCntVar);
18040 auto MakeFlattenedRef = [&SemaRef = this->SemaRef, FlattenedIVDecl, IVTy,
18041 OrigVarLoc]() {
18042 return buildDeclRefExpr(S&: SemaRef, D: FlattenedIVDecl, Ty: IVTy, Loc: OrigVarLoc);
18043 };
18044
18045 // For init-statement:
18046 // \code{.cpp}
18047 // auto .flatten.iv = 0;
18048 // \endcode
18049 auto *Zero = IntegerLiteral::Create(C: Context, V: llvm::APInt::getZero(numBits: IVWidth),
18050 type: IVTy, l: OrigVarLoc);
18051 SemaRef.AddInitializerToDecl(dcl: FlattenedIVDecl, init: Zero, /*DirectInit=*/false);
18052 StmtResult Init = new (Context)
18053 DeclStmt(DeclGroupRef(FlattenedIVDecl), OrigVarLocBegin, OrigVarLocEnd);
18054 if (!Init.isUsable())
18055 return StmtError();
18056
18057 // For cond-expression:
18058 // \code{.cpp}
18059 // .flatten.iv < n0 * n1 * ... * n(k-1)
18060 // \endcode
18061 ExprResult Cond = SemaRef.BuildBinOp(S: CurScope, OpLoc: CondLoc, Opc: BO_LT,
18062 LHSExpr: MakeFlattenedRef(), RHSExpr: TripCount.get());
18063 if (!Cond.isUsable())
18064 return StmtError();
18065
18066 // For incr-statement:
18067 // \code{.cpp}
18068 // ++.flatten.iv
18069 // \endcode
18070 ExprResult Incr =
18071 SemaRef.BuildUnaryOp(S: CurScope, OpLoc: OutermostHelper.Inc->getExprLoc(),
18072 Opc: UO_PreInc, Input: MakeFlattenedRef());
18073 if (!Incr.isUsable())
18074 return StmtError();
18075
18076 // Recover each logical iteration counter via mixed-radix div/mod; reuse the
18077 // iteration variables from checkOpenMPLoop so Updates compute user counters.
18078 SmallVector<Stmt *, 8> BodyStmts;
18079 for (unsigned I = 0; I < NumLoops; ++I) {
18080 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
18081 auto *IVRef = cast<DeclRefExpr>(Val: LoopHelper.IterationVarRef);
18082 auto *IVDecl = cast<VarDecl>(Val: IVRef->getDecl());
18083 std::string IVName = (".flatten.iv." + llvm::Twine(I)).str();
18084 IVDecl->setDeclName(&SemaRef.PP.getIdentifierTable().get(Name: IVName));
18085
18086 ExprResult Value = MakeFlattenedRef();
18087 if (I + 1 < NumLoops) {
18088 ExprResult Divisor = MakeDivisorInIVTy(I + 1);
18089 if (!Divisor.isUsable())
18090 return StmtError();
18091 for (unsigned J = I + 2; J < NumLoops; ++J) {
18092 ExprResult Next = MakeDivisorInIVTy(J);
18093 if (!Next.isUsable())
18094 return StmtError();
18095 Divisor = SemaRef.BuildBinOp(S: CurScope, OpLoc: OrigVarLoc, Opc: BO_Mul,
18096 LHSExpr: Divisor.get(), RHSExpr: Next.get());
18097 if (!Divisor.isUsable())
18098 return StmtError();
18099 }
18100 Value = SemaRef.BuildBinOp(S: CurScope, OpLoc: OrigVarLoc, Opc: BO_Div, LHSExpr: Value.get(),
18101 RHSExpr: Divisor.get());
18102 if (!Value.isUsable())
18103 return StmtError();
18104 }
18105 if (I > 0) {
18106 ExprResult RemDivisor = MakeDivisorInIVTy(I);
18107 if (!RemDivisor.isUsable())
18108 return StmtError();
18109 Value = SemaRef.BuildBinOp(S: CurScope, OpLoc: OrigVarLoc, Opc: BO_Rem, LHSExpr: Value.get(),
18110 RHSExpr: RemDivisor.get());
18111 if (!Value.isUsable())
18112 return StmtError();
18113 }
18114
18115 SemaRef.AddInitializerToDecl(dcl: IVDecl, init: Value.get(), /*DirectInit=*/false);
18116 StmtResult IVStmt = new (Context)
18117 DeclStmt(DeclGroupRef(IVDecl), OrigVarLocBegin, OrigVarLocEnd);
18118 if (!IVStmt.isUsable())
18119 return StmtError();
18120
18121 BodyStmts.push_back(Elt: IVStmt.get());
18122 llvm::append_range(C&: BodyStmts, R&: LoopHelper.Updates);
18123 if (auto *CXXFor = dyn_cast<CXXForRangeStmt>(Val: LoopStmts[I]))
18124 BodyStmts.push_back(Elt: CXXFor->getLoopVarStmt());
18125 }
18126 BodyStmts.push_back(Elt: Body);
18127 auto *FlattenedBody =
18128 CompoundStmt::Create(C: Context, Stmts: BodyStmts, FPFeatures: FPOptionsOverride(),
18129 LB: Body->getBeginLoc(), RB: Body->getEndLoc());
18130
18131 auto *FlattenedFor = new (Context) ForStmt(
18132 Context, Init.get(), Cond.get(), nullptr, Incr.get(), FlattenedBody,
18133 OutermostHelper.Init->getBeginLoc(), OutermostHelper.Init->getBeginLoc(),
18134 OutermostHelper.Inc->getEndLoc());
18135
18136 // A counter only reaches its final value if its own loop and all enclosing
18137 // loops execute at least one iteration; otherwise it keeps the value assigned
18138 // by the pre-inits. Guarding also avoids evaluating 'start + n * step' for an
18139 // empty loop, whose trip count may have wrapped.
18140 SmallVector<Stmt *, 4> Finals;
18141 for (unsigned I = 0; I < NumLoops; ++I) {
18142 assert(LoopHelpers[I].Finals.size() == 1 &&
18143 "Single-dimensional loop iteration space expected");
18144 ExprResult Final =
18145 CopyTransformer.TransformExpr(E: LoopHelpers[I].Finals.front());
18146 if (!Final.isUsable())
18147 return StmtError();
18148 ExprResult FinalCond = BuildHasIterations(/*Count=*/I + 1);
18149 if (!FinalCond.isUsable())
18150 return StmtError();
18151 // If the loop has its own directive (e.g. reverse), restore its variable.
18152 Stmt *Then = Final.get();
18153 if (Stmt *Nested = NestedFinals[I])
18154 Then = CompoundStmt::Create(C: Context, Stmts: {Then, Nested}, FPFeatures: FPOptionsOverride(),
18155 LB: CondLoc, RB: CondLoc);
18156 Finals.push_back(Elt: IfStmt::Create(Ctx: Context, IL: CondLoc, Kind: IfStatementKind::Ordinary,
18157 Init: nullptr, Var: nullptr, Cond: FinalCond.get(), LPL: CondLoc,
18158 RPL: CondLoc, Then, EL: SourceLocation(), Else: nullptr));
18159 }
18160 Stmt *FinalsStmt = CompoundStmt::Create(C: Context, Stmts: Finals, FPFeatures: FPOptionsOverride(),
18161 LB: FlattenedFor->getBeginLoc(),
18162 RB: FlattenedFor->getEndLoc());
18163
18164 return OMPFlattenDirective::Create(
18165 C: Context, StartLoc, EndLoc, Clauses, NumLoops, AssociatedStmt: AStmt, TransformedStmt: FlattenedFor,
18166 PreInits: buildPreInits(Context, PreInits), Finals: FinalsStmt);
18167}
18168
18169StmtResult SemaOpenMP::ActOnOpenMPFuseDirective(ArrayRef<OMPClause *> Clauses,
18170 Stmt *AStmt,
18171 SourceLocation StartLoc,
18172 SourceLocation EndLoc) {
18173
18174 ASTContext &Context = getASTContext();
18175 DeclContext *CurrContext = SemaRef.CurContext;
18176 Scope *CurScope = SemaRef.getCurScope();
18177 CaptureVars CopyTransformer(SemaRef);
18178
18179 // Ensure the structured block is not empty
18180 if (!AStmt)
18181 return StmtError();
18182
18183 if (const auto *DepthC =
18184 OMPExecutableDirective::getSingleClause<OMPDepthClause>(Clauses)) {
18185 Diag(Loc: DepthC->getBeginLoc(), DiagID: diag::err_omp_clause_not_supported_yet)
18186 << "depth" << getOpenMPDirectiveName(D: OMPD_fuse);
18187 return StmtError();
18188 }
18189
18190 // Defer transformation in dependent contexts
18191 // The NumLoopNests argument is set to a placeholder 1 (even though
18192 // using looprange fuse could yield up to 3 top level loop nests)
18193 // because a dependent context could prevent determining its true value
18194 if (CurrContext->isDependentContext())
18195 return OMPFuseDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
18196 /* NumLoops */ NumGeneratedTopLevelLoops: 1, AssociatedStmt: AStmt, TransformedStmt: nullptr, PreInits: nullptr,
18197 Finals: nullptr);
18198
18199 // Validate that the potential loop sequence is transformable for fusion
18200 // Also collect the HelperExprs, Loop Stmts, Inits, and Number of loops
18201 LoopSequenceAnalysis SeqAnalysis;
18202 if (!checkTransformableLoopSequence(Kind: OMPD_fuse, AStmt, SeqAnalysis, Context))
18203 return StmtError();
18204
18205 // SeqAnalysis.LoopSeqSize exists mostly to handle dependent contexts,
18206 // otherwise it must be the same as SeqAnalysis.Loops.size().
18207 assert(SeqAnalysis.LoopSeqSize == SeqAnalysis.Loops.size() &&
18208 "Inconsistent size of the loop sequence and the number of loops "
18209 "found in the sequence");
18210
18211 // Handle clauses, which can be any of the following: [looprange, apply]
18212 const auto *LRC =
18213 OMPExecutableDirective::getSingleClause<OMPLoopRangeClause>(Clauses);
18214
18215 // The clause arguments are invalidated if any error arises
18216 // such as non-constant or non-positive arguments
18217 if (LRC && (!LRC->getFirst() || !LRC->getCount()))
18218 return StmtError();
18219
18220 // Delayed semantic check of LoopRange constraint
18221 // Evaluates the loop range arguments and returns the first and count values
18222 auto EvaluateLoopRangeArguments = [&Context](Expr *First, Expr *Count,
18223 uint64_t &FirstVal,
18224 uint64_t &CountVal) {
18225 llvm::APSInt FirstInt = First->EvaluateKnownConstInt(Ctx: Context);
18226 llvm::APSInt CountInt = Count->EvaluateKnownConstInt(Ctx: Context);
18227 FirstVal = FirstInt.getLimitedValue();
18228 CountVal = CountInt.getLimitedValue();
18229 };
18230
18231 // OpenMP [6.0, Restrictions]
18232 // first + count - 1 must not evaluate to a value greater than the
18233 // loop sequence length of the associated canonical loop sequence.
18234 auto ValidLoopRange = [](uint64_t FirstVal, uint64_t CountVal,
18235 unsigned NumLoops) -> bool {
18236 return FirstVal + CountVal - 1 <= NumLoops;
18237 };
18238 uint64_t FirstVal = 1, CountVal = 0, LastVal = SeqAnalysis.LoopSeqSize;
18239
18240 // Validates the loop range after evaluating the semantic information
18241 // and ensures that the range is valid for the given loop sequence size.
18242 // Expressions are evaluated at compile time to obtain constant values.
18243 if (LRC) {
18244 EvaluateLoopRangeArguments(LRC->getFirst(), LRC->getCount(), FirstVal,
18245 CountVal);
18246 if (CountVal == 1)
18247 SemaRef.Diag(Loc: LRC->getCountLoc(), DiagID: diag::warn_omp_redundant_fusion)
18248 << getOpenMPDirectiveName(D: OMPD_fuse);
18249
18250 if (!ValidLoopRange(FirstVal, CountVal, SeqAnalysis.LoopSeqSize)) {
18251 SemaRef.Diag(Loc: LRC->getFirstLoc(), DiagID: diag::err_omp_invalid_looprange)
18252 << getOpenMPDirectiveName(D: OMPD_fuse) << FirstVal
18253 << (FirstVal + CountVal - 1) << SeqAnalysis.LoopSeqSize;
18254 return StmtError();
18255 }
18256
18257 LastVal = FirstVal + CountVal - 1;
18258 }
18259
18260 // Complete fusion generates a single canonical loop nest
18261 // However looprange clause may generate several loop nests
18262 unsigned NumGeneratedTopLevelLoops =
18263 LRC ? SeqAnalysis.LoopSeqSize - CountVal + 1 : 1;
18264
18265 // Emit a warning for redundant loop fusion when the sequence contains only
18266 // one loop.
18267 if (SeqAnalysis.LoopSeqSize == 1)
18268 SemaRef.Diag(Loc: AStmt->getBeginLoc(), DiagID: diag::warn_omp_redundant_fusion)
18269 << getOpenMPDirectiveName(D: OMPD_fuse);
18270
18271 // Select the type with the largest bit width among all induction variables
18272 QualType IVType =
18273 SeqAnalysis.Loops[FirstVal - 1].HelperExprs.IterationVarRef->getType();
18274 for (unsigned I : llvm::seq<unsigned>(Begin: FirstVal, End: LastVal)) {
18275 QualType CurrentIVType =
18276 SeqAnalysis.Loops[I].HelperExprs.IterationVarRef->getType();
18277 if (Context.getTypeSize(T: CurrentIVType) > Context.getTypeSize(T: IVType)) {
18278 IVType = CurrentIVType;
18279 }
18280 }
18281 uint64_t IVBitWidth = Context.getIntWidth(T: IVType);
18282
18283 // Create pre-init declarations for all loops lower bounds, upper bounds,
18284 // strides and num-iterations for every top level loop in the fusion
18285 SmallVector<VarDecl *, 4> LBVarDecls;
18286 SmallVector<VarDecl *, 4> STVarDecls;
18287 SmallVector<VarDecl *, 4> NIVarDecls;
18288 SmallVector<VarDecl *, 4> IVVarDecls;
18289
18290 // Helper lambda to create variables for bounds, strides, and other
18291 // expressions. Generates both the variable declaration and the corresponding
18292 // initialization statement.
18293 auto CreateHelperVarAndStmt =
18294 [&, &SemaRef = SemaRef](Expr *ExprToCopy, const std::string &BaseName,
18295 unsigned I, bool NeedsNewVD = false) {
18296 Expr *TransformedExpr =
18297 AssertSuccess(R: CopyTransformer.TransformExpr(E: ExprToCopy));
18298 if (!TransformedExpr)
18299 return std::pair<VarDecl *, StmtResult>(nullptr, StmtError());
18300
18301 auto Name = (Twine(".omp.") + BaseName + std::to_string(val: I)).str();
18302
18303 VarDecl *VD;
18304 if (NeedsNewVD) {
18305 VD = buildVarDecl(SemaRef, Loc: SourceLocation(), Type: IVType, Name);
18306 SemaRef.AddInitializerToDecl(dcl: VD, init: TransformedExpr, DirectInit: false);
18307 } else {
18308 // Create a unique variable name
18309 DeclRefExpr *DRE = cast<DeclRefExpr>(Val: TransformedExpr);
18310 VD = cast<VarDecl>(Val: DRE->getDecl());
18311 VD->setDeclName(&SemaRef.PP.getIdentifierTable().get(Name));
18312 }
18313 // Create the corresponding declaration statement
18314 StmtResult DeclStmt = new (Context) class DeclStmt(
18315 DeclGroupRef(VD), SourceLocation(), SourceLocation());
18316 return std::make_pair(x&: VD, y&: DeclStmt);
18317 };
18318
18319 // PreInits hold a sequence of variable declarations that must be executed
18320 // before the fused loop begins. These include bounds, strides, and other
18321 // helper variables required for the transformation. Other loop transforms
18322 // also contain their own preinits
18323 SmallVector<Stmt *> PreInits;
18324
18325 // Update the general preinits using the preinits generated by loop sequence
18326 // generating loop transformations. These preinits differ slightly from
18327 // single-loop transformation preinits, as they can be detached from a
18328 // specific loop inside multiple generated loop nests. This happens
18329 // because certain helper variables, like '.omp.fuse.max', are introduced to
18330 // handle fused iteration spaces and may not be directly tied to a single
18331 // original loop. The preinit structure must ensure that hidden variables
18332 // like '.omp.fuse.max' are still properly handled.
18333 // Transformations that apply this concept: Loopranged Fuse, Split
18334 llvm::append_range(C&: PreInits, R&: SeqAnalysis.LoopSequencePreInits);
18335
18336 // Process each single loop to generate and collect declarations
18337 // and statements for all helper expressions related to
18338 // particular single loop nests
18339
18340 // Also In the case of the fused loops, we keep track of their original
18341 // inits by appending them to their preinits statement, and in the case of
18342 // transformations, also append their preinits (which contain the original
18343 // loop initialization statement or other statements)
18344
18345 for (unsigned int I = FirstVal - 1, J = 0; I < LastVal; ++I, ++J) {
18346 if (SeqAnalysis.Loops[I].isRegularLoop()) {
18347 addLoopPreInits(Context, LoopHelper&: SeqAnalysis.Loops[I].HelperExprs,
18348 LoopStmt: SeqAnalysis.Loops[I].TheForStmt,
18349 OriginalInit: SeqAnalysis.Loops[I].OriginalInits, PreInits);
18350 } else if (SeqAnalysis.Loops[I].isLoopTransformation()) {
18351 // For transformed loops, insert both pre-inits and original inits.
18352 // Order matters: pre-inits may define variables used in the original
18353 // inits such as upper bounds...
18354 llvm::append_range(C&: PreInits, R&: SeqAnalysis.Loops[I].TransformsPreInits);
18355
18356 addLoopPreInits(Context, LoopHelper&: SeqAnalysis.Loops[I].HelperExprs,
18357 LoopStmt: SeqAnalysis.Loops[I].TheForStmt,
18358 OriginalInit: SeqAnalysis.Loops[I].OriginalInits, PreInits);
18359 }
18360 auto [LBVD, LBDStmt] =
18361 CreateHelperVarAndStmt(SeqAnalysis.Loops[I].HelperExprs.LB, "lb", J);
18362 auto [STVD, STDStmt] =
18363 CreateHelperVarAndStmt(SeqAnalysis.Loops[I].HelperExprs.ST, "st", J);
18364 auto [NIVD, NIDStmt] = CreateHelperVarAndStmt(
18365 SeqAnalysis.Loops[I].HelperExprs.NumIterations, "ni", J, true);
18366 auto [IVVD, IVDStmt] = CreateHelperVarAndStmt(
18367 SeqAnalysis.Loops[I].HelperExprs.IterationVarRef, "iv", J);
18368
18369 assert(LBVD && STVD && NIVD && IVVD &&
18370 "OpenMP Fuse Helper variables creation failed");
18371
18372 LBVarDecls.push_back(Elt: LBVD);
18373 STVarDecls.push_back(Elt: STVD);
18374 NIVarDecls.push_back(Elt: NIVD);
18375 IVVarDecls.push_back(Elt: IVVD);
18376
18377 PreInits.push_back(Elt: LBDStmt.get());
18378 PreInits.push_back(Elt: STDStmt.get());
18379 PreInits.push_back(Elt: NIDStmt.get());
18380 PreInits.push_back(Elt: IVDStmt.get());
18381 }
18382
18383 auto MakeVarDeclRef = [&SemaRef = this->SemaRef](VarDecl *VD) {
18384 return buildDeclRefExpr(S&: SemaRef, D: VD, Ty: VD->getType(), Loc: VD->getLocation(),
18385 RefersToCapture: false);
18386 };
18387
18388 // Following up the creation of the final fused loop will be performed
18389 // which has the following shape (considering the selected loops):
18390 //
18391 // for (fuse.index = 0; fuse.index < max(ni0, ni1..., nik); ++fuse.index) {
18392 // if (fuse.index < ni0){
18393 // iv0 = lb0 + st0 * fuse.index;
18394 // original.index0 = iv0
18395 // body(0);
18396 // }
18397 // if (fuse.index < ni1){
18398 // iv1 = lb1 + st1 * fuse.index;
18399 // original.index1 = iv1
18400 // body(1);
18401 // }
18402 //
18403 // ...
18404 //
18405 // if (fuse.index < nik){
18406 // ivk = lbk + stk * fuse.index;
18407 // original.indexk = ivk
18408 // body(k); Expr *InitVal = IntegerLiteral::Create(Context,
18409 // llvm::APInt(IVWidth, 0),
18410 // }
18411
18412 // 1. Create the initialized fuse index
18413 StringRef IndexName = ".omp.fuse.index";
18414 Expr *InitVal = IntegerLiteral::Create(C: Context, V: llvm::APInt(IVBitWidth, 0),
18415 type: IVType, l: SourceLocation());
18416 VarDecl *IndexDecl =
18417 buildVarDecl(SemaRef, Loc: {}, Type: IVType, Name: IndexName, Attrs: nullptr, OrigRef: nullptr);
18418 SemaRef.AddInitializerToDecl(dcl: IndexDecl, init: InitVal, DirectInit: false);
18419 StmtResult InitStmt = new (Context)
18420 DeclStmt(DeclGroupRef(IndexDecl), SourceLocation(), SourceLocation());
18421
18422 if (!InitStmt.isUsable())
18423 return StmtError();
18424
18425 auto MakeIVRef = [&SemaRef = this->SemaRef, IndexDecl, IVType,
18426 Loc = InitVal->getExprLoc()]() {
18427 return buildDeclRefExpr(S&: SemaRef, D: IndexDecl, Ty: IVType, Loc, RefersToCapture: false);
18428 };
18429
18430 // 2. Iteratively compute the max number of logical iterations Max(NI_1, NI_2,
18431 // ..., NI_k)
18432 //
18433 // This loop accumulates the maximum value across multiple expressions,
18434 // ensuring each step constructs a unique AST node for correctness. By using
18435 // intermediate temporary variables and conditional operators, we maintain
18436 // distinct nodes and avoid duplicating subtrees, For instance, max(a,b,c):
18437 // omp.temp0 = max(a, b)
18438 // omp.temp1 = max(omp.temp0, c)
18439 // omp.fuse.max = max(omp.temp1, omp.temp0)
18440
18441 ExprResult MaxExpr;
18442 // I is the range of loops in the sequence that we fuse.
18443 for (unsigned I = FirstVal - 1, J = 0; I < LastVal; ++I, ++J) {
18444 DeclRefExpr *NIRef = MakeVarDeclRef(NIVarDecls[J]);
18445 QualType NITy = NIRef->getType();
18446
18447 if (MaxExpr.isUnset()) {
18448 // Initialize MaxExpr with the first NI expression
18449 MaxExpr = NIRef;
18450 } else {
18451 // Create a new acummulator variable t_i = MaxExpr
18452 std::string TempName = (Twine(".omp.temp.") + Twine(J)).str();
18453 VarDecl *TempDecl =
18454 buildVarDecl(SemaRef, Loc: {}, Type: NITy, Name: TempName, Attrs: nullptr, OrigRef: nullptr);
18455 TempDecl->setInit(MaxExpr.get());
18456 DeclRefExpr *TempRef =
18457 buildDeclRefExpr(S&: SemaRef, D: TempDecl, Ty: NITy, Loc: SourceLocation(), RefersToCapture: false);
18458 DeclRefExpr *TempRef2 =
18459 buildDeclRefExpr(S&: SemaRef, D: TempDecl, Ty: NITy, Loc: SourceLocation(), RefersToCapture: false);
18460 // Add a DeclStmt to PreInits to ensure the variable is declared.
18461 StmtResult TempStmt = new (Context)
18462 DeclStmt(DeclGroupRef(TempDecl), SourceLocation(), SourceLocation());
18463
18464 if (!TempStmt.isUsable())
18465 return StmtError();
18466 PreInits.push_back(Elt: TempStmt.get());
18467
18468 // Build MaxExpr <-(MaxExpr > NIRef ? MaxExpr : NIRef)
18469 ExprResult Comparison =
18470 SemaRef.BuildBinOp(S: nullptr, OpLoc: SourceLocation(), Opc: BO_GT, LHSExpr: TempRef, RHSExpr: NIRef);
18471 // Handle any errors in Comparison creation
18472 if (!Comparison.isUsable())
18473 return StmtError();
18474
18475 DeclRefExpr *NIRef2 = MakeVarDeclRef(NIVarDecls[J]);
18476 // Update MaxExpr using a conditional expression to hold the max value
18477 MaxExpr = new (Context) ConditionalOperator(
18478 Comparison.get(), SourceLocation(), TempRef2, SourceLocation(),
18479 NIRef2->getExprStmt(), NITy, VK_LValue, OK_Ordinary);
18480
18481 if (!MaxExpr.isUsable())
18482 return StmtError();
18483 }
18484 }
18485 if (!MaxExpr.isUsable())
18486 return StmtError();
18487
18488 // 3. Declare the max variable
18489 const std::string MaxName = Twine(".omp.fuse.max").str();
18490 VarDecl *MaxDecl =
18491 buildVarDecl(SemaRef, Loc: {}, Type: IVType, Name: MaxName, Attrs: nullptr, OrigRef: nullptr);
18492 MaxDecl->setInit(MaxExpr.get());
18493 DeclRefExpr *MaxRef = buildDeclRefExpr(S&: SemaRef, D: MaxDecl, Ty: IVType, Loc: {}, RefersToCapture: false);
18494 StmtResult MaxStmt = new (Context)
18495 DeclStmt(DeclGroupRef(MaxDecl), SourceLocation(), SourceLocation());
18496
18497 if (MaxStmt.isInvalid())
18498 return StmtError();
18499 PreInits.push_back(Elt: MaxStmt.get());
18500
18501 // 4. Create condition Expr: index < n_max
18502 ExprResult CondExpr = SemaRef.BuildBinOp(S: CurScope, OpLoc: SourceLocation(), Opc: BO_LT,
18503 LHSExpr: MakeIVRef(), RHSExpr: MaxRef);
18504 if (!CondExpr.isUsable())
18505 return StmtError();
18506
18507 // 5. Increment Expr: ++index
18508 ExprResult IncrExpr =
18509 SemaRef.BuildUnaryOp(S: CurScope, OpLoc: SourceLocation(), Opc: UO_PreInc, Input: MakeIVRef());
18510 if (!IncrExpr.isUsable())
18511 return StmtError();
18512
18513 // 6. Build the Fused Loop Body
18514 // The final fused loop iterates over the maximum logical range. Inside the
18515 // loop, each original loop's index is calculated dynamically, and its body
18516 // is executed conditionally.
18517 //
18518 // Each sub-loop's body is guarded by a conditional statement to ensure
18519 // it executes only within its logical iteration range:
18520 //
18521 // if (fuse.index < ni_k){
18522 // iv_k = lb_k + st_k * fuse.index;
18523 // original.index = iv_k
18524 // body(k);
18525 // }
18526
18527 CompoundStmt *FusedBody = nullptr;
18528 SmallVector<Stmt *, 4> FusedBodyStmts;
18529 for (unsigned I = FirstVal - 1, J = 0; I < LastVal; ++I, ++J) {
18530 // Assingment of the original sub-loop index to compute the logical index
18531 // IV_k = LB_k + omp.fuse.index * ST_k
18532 ExprResult IdxExpr =
18533 SemaRef.BuildBinOp(S: CurScope, OpLoc: SourceLocation(), Opc: BO_Mul,
18534 LHSExpr: MakeVarDeclRef(STVarDecls[J]), RHSExpr: MakeIVRef());
18535 if (!IdxExpr.isUsable())
18536 return StmtError();
18537 IdxExpr = SemaRef.BuildBinOp(S: CurScope, OpLoc: SourceLocation(), Opc: BO_Add,
18538 LHSExpr: MakeVarDeclRef(LBVarDecls[J]), RHSExpr: IdxExpr.get());
18539
18540 if (!IdxExpr.isUsable())
18541 return StmtError();
18542 IdxExpr = SemaRef.BuildBinOp(S: CurScope, OpLoc: SourceLocation(), Opc: BO_Assign,
18543 LHSExpr: MakeVarDeclRef(IVVarDecls[J]), RHSExpr: IdxExpr.get());
18544 if (!IdxExpr.isUsable())
18545 return StmtError();
18546
18547 // Update the original i_k = IV_k
18548 SmallVector<Stmt *, 4> BodyStmts;
18549 BodyStmts.push_back(Elt: IdxExpr.get());
18550 llvm::append_range(C&: BodyStmts, R&: SeqAnalysis.Loops[I].HelperExprs.Updates);
18551
18552 // If the loop is a CXXForRangeStmt then the iterator variable is needed
18553 if (auto *SourceCXXFor =
18554 dyn_cast<CXXForRangeStmt>(Val: SeqAnalysis.Loops[I].TheForStmt))
18555 BodyStmts.push_back(Elt: SourceCXXFor->getLoopVarStmt());
18556
18557 Stmt *Body =
18558 (isa<ForStmt>(Val: SeqAnalysis.Loops[I].TheForStmt))
18559 ? cast<ForStmt>(Val: SeqAnalysis.Loops[I].TheForStmt)->getBody()
18560 : cast<CXXForRangeStmt>(Val: SeqAnalysis.Loops[I].TheForStmt)->getBody();
18561 BodyStmts.push_back(Elt: Body);
18562
18563 CompoundStmt *CombinedBody =
18564 CompoundStmt::Create(C: Context, Stmts: BodyStmts, FPFeatures: FPOptionsOverride(),
18565 LB: SourceLocation(), RB: SourceLocation());
18566 ExprResult Condition =
18567 SemaRef.BuildBinOp(S: CurScope, OpLoc: SourceLocation(), Opc: BO_LT, LHSExpr: MakeIVRef(),
18568 RHSExpr: MakeVarDeclRef(NIVarDecls[J]));
18569
18570 if (!Condition.isUsable())
18571 return StmtError();
18572
18573 IfStmt *IfStatement = IfStmt::Create(
18574 Ctx: Context, IL: SourceLocation(), Kind: IfStatementKind::Ordinary, Init: nullptr, Var: nullptr,
18575 Cond: Condition.get(), LPL: SourceLocation(), RPL: SourceLocation(), Then: CombinedBody,
18576 EL: SourceLocation(), Else: nullptr);
18577
18578 FusedBodyStmts.push_back(Elt: IfStatement);
18579 }
18580 FusedBody = CompoundStmt::Create(C: Context, Stmts: FusedBodyStmts, FPFeatures: FPOptionsOverride(),
18581 LB: SourceLocation(), RB: SourceLocation());
18582
18583 // 7. Construct the final fused loop
18584 ForStmt *FusedForStmt = new (Context)
18585 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr, IncrExpr.get(),
18586 FusedBody, InitStmt.get()->getBeginLoc(), SourceLocation(),
18587 IncrExpr.get()->getEndLoc());
18588
18589 // 8. Finals of the fused loops, in sequence order. Out-of-range loops keep
18590 // theirs inline.
18591 SmallVector<Stmt *, 4> AllFinalsStmts;
18592 for (unsigned I : llvm::seq<unsigned>(Begin: FirstVal - 1, End: LastVal)) {
18593 const LoopAnalysis &Loop = SeqAnalysis.Loops[I];
18594 if (Loop.isRegularLoop())
18595 if (Stmt *Own =
18596 buildLoopFinalization(Context, LoopHelpers: Loop.HelperExprs, LoopStmts: Loop.TheForStmt))
18597 AllFinalsStmts.push_back(Elt: Own);
18598 if (Loop.TransformFinals)
18599 AllFinalsStmts.push_back(Elt: Loop.TransformFinals);
18600 }
18601 Stmt *CombinedFinals = nullptr;
18602 if (AllFinalsStmts.size() == 1)
18603 CombinedFinals = AllFinalsStmts.front();
18604 else if (!AllFinalsStmts.empty())
18605 CombinedFinals =
18606 CompoundStmt::Create(C: Context, Stmts: AllFinalsStmts, FPFeatures: FPOptionsOverride(),
18607 LB: SourceLocation(), RB: SourceLocation());
18608
18609 // In the case of looprange, the result of fuse won't simply
18610 // be a single loop (ForStmt), but rather a loop sequence
18611 // (CompoundStmt) of 3 parts: the pre-fusion loops, the fused loop
18612 // and the post-fusion loops, preserving its original order.
18613 //
18614 // Note: If looprange clause produces a single fused loop nest then
18615 // this compound statement wrapper is unnecessary (Therefore this
18616 // treatment is skipped)
18617
18618 auto WithFinals = [&](Stmt *Loop, Stmt *Finals) -> Stmt * {
18619 return Finals ? CompoundStmt::Create(C: Context, Stmts: {Loop, Finals},
18620 FPFeatures: FPOptionsOverride(), LB: SourceLocation(),
18621 RB: SourceLocation())
18622 : Loop;
18623 };
18624 Stmt *FusionStmt = FusedForStmt;
18625 if (LRC && CountVal != SeqAnalysis.LoopSeqSize) {
18626 SmallVector<Stmt *, 4> FinalLoops;
18627
18628 // Collect all non-fused loops before and after the fused region.
18629 // Pre-fusion and post-fusion loops are inserted in order exploiting their
18630 // symmetry, along with their corresponding transformation pre-inits if
18631 // needed. The fused loop is added between the two regions.
18632 for (unsigned I : llvm::seq<unsigned>(Size: SeqAnalysis.LoopSeqSize)) {
18633 if (I >= FirstVal - 1 && I < FirstVal + CountVal - 1)
18634 continue;
18635
18636 // Out-of-range transformations still need their pre-inits.
18637 if (SeqAnalysis.Loops[I].isLoopTransformation())
18638 llvm::append_range(C&: PreInits, R&: SeqAnalysis.Loops[I].TransformsPreInits);
18639
18640 // For out-of-range loop-transformations, emit their finalization inline
18641 // right after the transformed loop so that IVs are restored as if the
18642 // transformation had not been present (OpenMP 6.0 finalization rule).
18643 FinalLoops.push_back(Elt: WithFinals(SeqAnalysis.Loops[I].TheForStmt,
18644 SeqAnalysis.Loops[I].TransformFinals));
18645 }
18646
18647 // Insert the fused loop with its Finals between the pre- and post-fusion
18648 // loops.
18649 FinalLoops.insert(I: FinalLoops.begin() + (FirstVal - 1),
18650 Elt: WithFinals(FusedForStmt, CombinedFinals));
18651 CombinedFinals = nullptr; // emitted inline
18652 FusionStmt = CompoundStmt::Create(C: Context, Stmts: FinalLoops, FPFeatures: FPOptionsOverride(),
18653 LB: SourceLocation(), RB: SourceLocation());
18654 }
18655 return OMPFuseDirective::Create(
18656 C: Context, StartLoc, EndLoc, Clauses, NumGeneratedTopLevelLoops, AssociatedStmt: AStmt,
18657 TransformedStmt: FusionStmt, PreInits: buildPreInits(Context, PreInits), Finals: CombinedFinals);
18658}
18659
18660OMPClause *SemaOpenMP::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind,
18661 Expr *Expr,
18662 SourceLocation StartLoc,
18663 SourceLocation LParenLoc,
18664 SourceLocation EndLoc) {
18665 OMPClause *Res = nullptr;
18666 switch (Kind) {
18667 case OMPC_final:
18668 Res = ActOnOpenMPFinalClause(Condition: Expr, StartLoc, LParenLoc, EndLoc);
18669 break;
18670 case OMPC_safelen:
18671 Res = ActOnOpenMPSafelenClause(Length: Expr, StartLoc, LParenLoc, EndLoc);
18672 break;
18673 case OMPC_simdlen:
18674 Res = ActOnOpenMPSimdlenClause(Length: Expr, StartLoc, LParenLoc, EndLoc);
18675 break;
18676 case OMPC_allocator:
18677 Res = ActOnOpenMPAllocatorClause(Allocator: Expr, StartLoc, LParenLoc, EndLoc);
18678 break;
18679 case OMPC_collapse:
18680 Res = ActOnOpenMPCollapseClause(NumForLoops: Expr, StartLoc, LParenLoc, EndLoc);
18681 break;
18682 case OMPC_ordered:
18683 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, NumForLoops: Expr);
18684 break;
18685 case OMPC_nowait:
18686 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc, LParenLoc, Condition: Expr);
18687 break;
18688 case OMPC_priority:
18689 Res = ActOnOpenMPPriorityClause(Priority: Expr, StartLoc, LParenLoc, EndLoc);
18690 break;
18691 case OMPC_hint:
18692 Res = ActOnOpenMPHintClause(Hint: Expr, StartLoc, LParenLoc, EndLoc);
18693 break;
18694 case OMPC_depobj:
18695 Res = ActOnOpenMPDepobjClause(Depobj: Expr, StartLoc, LParenLoc, EndLoc);
18696 break;
18697 case OMPC_detach:
18698 Res = ActOnOpenMPDetachClause(Evt: Expr, StartLoc, LParenLoc, EndLoc);
18699 break;
18700 case OMPC_novariants:
18701 Res = ActOnOpenMPNovariantsClause(Condition: Expr, StartLoc, LParenLoc, EndLoc);
18702 break;
18703 case OMPC_nocontext:
18704 Res = ActOnOpenMPNocontextClause(Condition: Expr, StartLoc, LParenLoc, EndLoc);
18705 break;
18706 case OMPC_filter:
18707 Res = ActOnOpenMPFilterClause(ThreadID: Expr, StartLoc, LParenLoc, EndLoc);
18708 break;
18709 case OMPC_partial:
18710 Res = ActOnOpenMPPartialClause(FactorExpr: Expr, StartLoc, LParenLoc, EndLoc);
18711 break;
18712 case OMPC_depth:
18713 Res = ActOnOpenMPDepthClause(DepthExpr: Expr, StartLoc, LParenLoc, EndLoc);
18714 break;
18715 case OMPC_message:
18716 Res = ActOnOpenMPMessageClause(MS: Expr, StartLoc, LParenLoc, EndLoc);
18717 break;
18718 case OMPC_align:
18719 Res = ActOnOpenMPAlignClause(Alignment: Expr, StartLoc, LParenLoc, EndLoc);
18720 break;
18721 case OMPC_ompx_dyn_cgroup_mem:
18722 Res = ActOnOpenMPXDynCGroupMemClause(Size: Expr, StartLoc, LParenLoc, EndLoc);
18723 break;
18724 case OMPC_holds:
18725 Res = ActOnOpenMPHoldsClause(E: Expr, StartLoc, LParenLoc, EndLoc);
18726 break;
18727 case OMPC_transparent:
18728 Res = ActOnOpenMPTransparentClause(Transparent: Expr, StartLoc, LParenLoc, EndLoc);
18729 break;
18730 case OMPC_dyn_groupprivate:
18731 case OMPC_grainsize:
18732 case OMPC_num_tasks:
18733 case OMPC_num_threads:
18734 case OMPC_device:
18735 case OMPC_if:
18736 case OMPC_default:
18737 case OMPC_proc_bind:
18738 case OMPC_schedule:
18739 case OMPC_private:
18740 case OMPC_firstprivate:
18741 case OMPC_lastprivate:
18742 case OMPC_shared:
18743 case OMPC_reduction:
18744 case OMPC_task_reduction:
18745 case OMPC_in_reduction:
18746 case OMPC_linear:
18747 case OMPC_aligned:
18748 case OMPC_copyin:
18749 case OMPC_copyprivate:
18750 case OMPC_untied:
18751 case OMPC_mergeable:
18752 case OMPC_threadprivate:
18753 case OMPC_groupprivate:
18754 case OMPC_sizes:
18755 case OMPC_allocate:
18756 case OMPC_flush:
18757 case OMPC_read:
18758 case OMPC_write:
18759 case OMPC_update:
18760 case OMPC_capture:
18761 case OMPC_compare:
18762 case OMPC_seq_cst:
18763 case OMPC_acq_rel:
18764 case OMPC_acquire:
18765 case OMPC_release:
18766 case OMPC_relaxed:
18767 case OMPC_depend:
18768 case OMPC_threads:
18769 case OMPC_simd:
18770 case OMPC_map:
18771 case OMPC_nogroup:
18772 case OMPC_dist_schedule:
18773 case OMPC_defaultmap:
18774 case OMPC_unknown:
18775 case OMPC_uniform:
18776 case OMPC_to:
18777 case OMPC_from:
18778 case OMPC_use_device_ptr:
18779 case OMPC_use_device_addr:
18780 case OMPC_is_device_ptr:
18781 case OMPC_unified_address:
18782 case OMPC_unified_shared_memory:
18783 case OMPC_reverse_offload:
18784 case OMPC_dynamic_allocators:
18785 case OMPC_atomic_default_mem_order:
18786 case OMPC_self_maps:
18787 case OMPC_device_type:
18788 case OMPC_match:
18789 case OMPC_nontemporal:
18790 case OMPC_order:
18791 case OMPC_at:
18792 case OMPC_severity:
18793 case OMPC_destroy:
18794 case OMPC_inclusive:
18795 case OMPC_exclusive:
18796 case OMPC_uses_allocators:
18797 case OMPC_affinity:
18798 case OMPC_when:
18799 case OMPC_bind:
18800 case OMPC_num_teams:
18801 case OMPC_thread_limit:
18802 default:
18803 llvm_unreachable("Clause is not allowed.");
18804 }
18805 return Res;
18806}
18807
18808// An OpenMP directive such as 'target parallel' has two captured regions:
18809// for the 'target' and 'parallel' respectively. This function returns
18810// the region in which to capture expressions associated with a clause.
18811// A return value of OMPD_unknown signifies that the expression should not
18812// be captured.
18813static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
18814 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
18815 llvm::omp::Version OMPVersion,
18816 OpenMPDirectiveKind NameModifier = OMPD_unknown) {
18817 assert(isAllowedClauseForDirective(DKind, CKind, OMPVersion) &&
18818 "Invalid directive with CKind-clause");
18819
18820 // Invalid modifier will be diagnosed separately, just return OMPD_unknown.
18821 if (NameModifier != OMPD_unknown &&
18822 !isAllowedClauseForDirective(D: NameModifier, C: CKind, V: OMPVersion))
18823 return OMPD_unknown;
18824
18825 ArrayRef<OpenMPDirectiveKind> Leafs = getLeafConstructsOrSelf(D: DKind);
18826
18827 // [5.2:341:24-30]
18828 // If the clauses have expressions on them, such as for various clauses where
18829 // the argument of the clause is an expression, or lower-bound, length, or
18830 // stride expressions inside array sections (or subscript and stride
18831 // expressions in subscript-triplet for Fortran), or linear-step or alignment
18832 // expressions, the expressions are evaluated immediately before the construct
18833 // to which the clause has been split or duplicated per the above rules
18834 // (therefore inside of the outer leaf constructs). However, the expressions
18835 // inside the num_teams and thread_limit clauses are always evaluated before
18836 // the outermost leaf construct.
18837
18838 // Process special cases first.
18839 switch (CKind) {
18840 case OMPC_if:
18841 switch (DKind) {
18842 case OMPD_teams_loop:
18843 case OMPD_target_teams_loop:
18844 // For [target] teams loop, assume capture region is 'teams' so it's
18845 // available for codegen later to use if/when necessary.
18846 return OMPD_teams;
18847 case OMPD_target_update:
18848 case OMPD_target_enter_data:
18849 case OMPD_target_exit_data:
18850 return OMPD_task;
18851 default:
18852 break;
18853 }
18854 break;
18855 case OMPC_num_teams:
18856 case OMPC_thread_limit:
18857 case OMPC_ompx_dyn_cgroup_mem:
18858 case OMPC_dyn_groupprivate:
18859 // TODO: This may need to consider teams too.
18860 if (Leafs[0] == OMPD_target)
18861 return OMPD_target;
18862 break;
18863 case OMPC_device:
18864 if (Leafs[0] == OMPD_target ||
18865 llvm::is_contained(Set: {OMPD_dispatch, OMPD_target_update,
18866 OMPD_target_enter_data, OMPD_target_exit_data},
18867 Element: DKind))
18868 return OMPD_task;
18869 break;
18870 case OMPC_novariants:
18871 case OMPC_nocontext:
18872 if (DKind == OMPD_dispatch)
18873 return OMPD_task;
18874 break;
18875 case OMPC_when:
18876 if (DKind == OMPD_metadirective)
18877 return OMPD_metadirective;
18878 break;
18879 case OMPC_filter:
18880 return OMPD_unknown;
18881 default:
18882 break;
18883 }
18884
18885 // If none of the special cases above applied, and DKind is a capturing
18886 // directive, find the innermost enclosing leaf construct that allows the
18887 // clause, and returns the corresponding capture region.
18888
18889 auto GetEnclosingRegion = [&](int EndIdx, OpenMPClauseKind Clause) {
18890 // Find the index in "Leafs" of the last leaf that allows the given
18891 // clause. The search will only include indexes [0, EndIdx).
18892 // EndIdx may be set to the index of the NameModifier, if present.
18893 int InnermostIdx = [&]() {
18894 for (int I = EndIdx - 1; I >= 0; --I) {
18895 if (isAllowedClauseForDirective(D: Leafs[I], C: Clause, V: OMPVersion))
18896 return I;
18897 }
18898 return -1;
18899 }();
18900
18901 // Find the nearest enclosing capture region.
18902 SmallVector<OpenMPDirectiveKind, 2> Regions;
18903 for (int I = InnermostIdx - 1; I >= 0; --I) {
18904 if (!isOpenMPCapturingDirective(DKind: Leafs[I]))
18905 continue;
18906 Regions.clear();
18907 getOpenMPCaptureRegions(CaptureRegions&: Regions, DKind: Leafs[I]);
18908 if (Regions[0] != OMPD_unknown)
18909 return Regions.back();
18910 }
18911 return OMPD_unknown;
18912 };
18913
18914 if (isOpenMPCapturingDirective(DKind)) {
18915 auto GetLeafIndex = [&](OpenMPDirectiveKind Dir) {
18916 for (int I = 0, E = Leafs.size(); I != E; ++I) {
18917 if (Leafs[I] == Dir)
18918 return I + 1;
18919 }
18920 return 0;
18921 };
18922
18923 int End = NameModifier == OMPD_unknown ? Leafs.size()
18924 : GetLeafIndex(NameModifier);
18925 return GetEnclosingRegion(End, CKind);
18926 }
18927
18928 return OMPD_unknown;
18929}
18930
18931OMPClause *SemaOpenMP::ActOnOpenMPIfClause(
18932 OpenMPDirectiveKind NameModifier, Expr *Condition, SourceLocation StartLoc,
18933 SourceLocation LParenLoc, SourceLocation NameModifierLoc,
18934 SourceLocation ColonLoc, SourceLocation EndLoc) {
18935 Expr *ValExpr = Condition;
18936 Stmt *HelperValStmt = nullptr;
18937 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
18938 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
18939 !Condition->isInstantiationDependent() &&
18940 !Condition->containsUnexpandedParameterPack()) {
18941 ExprResult Val = SemaRef.CheckBooleanCondition(Loc: StartLoc, E: Condition);
18942 if (Val.isInvalid())
18943 return nullptr;
18944
18945 ValExpr = Val.get();
18946
18947 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
18948 CaptureRegion = getOpenMPCaptureRegionForClause(
18949 DKind, CKind: OMPC_if, OMPVersion: getLangOpts().getOpenMPVersion(), NameModifier);
18950 if (CaptureRegion != OMPD_unknown &&
18951 !SemaRef.CurContext->isDependentContext()) {
18952 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
18953 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
18954 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
18955 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
18956 }
18957 }
18958
18959 return new (getASTContext())
18960 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
18961 LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
18962}
18963
18964OMPClause *SemaOpenMP::ActOnOpenMPFinalClause(Expr *Condition,
18965 SourceLocation StartLoc,
18966 SourceLocation LParenLoc,
18967 SourceLocation EndLoc) {
18968 Expr *ValExpr = Condition;
18969 Stmt *HelperValStmt = nullptr;
18970 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
18971 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
18972 !Condition->isInstantiationDependent() &&
18973 !Condition->containsUnexpandedParameterPack()) {
18974 ExprResult Val = SemaRef.CheckBooleanCondition(Loc: StartLoc, E: Condition);
18975 if (Val.isInvalid())
18976 return nullptr;
18977
18978 ValExpr = SemaRef.MakeFullExpr(Arg: Val.get()).get();
18979
18980 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
18981 CaptureRegion = getOpenMPCaptureRegionForClause(
18982 DKind, CKind: OMPC_final, OMPVersion: getLangOpts().getOpenMPVersion());
18983 if (CaptureRegion != OMPD_unknown &&
18984 !SemaRef.CurContext->isDependentContext()) {
18985 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
18986 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
18987 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
18988 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
18989 }
18990 }
18991
18992 return new (getASTContext()) OMPFinalClause(
18993 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
18994}
18995
18996ExprResult
18997SemaOpenMP::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
18998 Expr *Op) {
18999 if (!Op)
19000 return ExprError();
19001
19002 class IntConvertDiagnoser : public Sema::ICEConvertDiagnoser {
19003 public:
19004 IntConvertDiagnoser()
19005 : ICEConvertDiagnoser(/*AllowScopedEnumerations=*/false, false, true) {}
19006 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
19007 QualType T) override {
19008 return S.Diag(Loc, DiagID: diag::err_omp_not_integral) << T;
19009 }
19010 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
19011 QualType T) override {
19012 return S.Diag(Loc, DiagID: diag::err_omp_incomplete_type) << T;
19013 }
19014 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
19015 QualType T,
19016 QualType ConvTy) override {
19017 return S.Diag(Loc, DiagID: diag::err_omp_explicit_conversion) << T << ConvTy;
19018 }
19019 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
19020 QualType ConvTy) override {
19021 return S.Diag(Loc: Conv->getLocation(), DiagID: diag::note_omp_conversion_here)
19022 << ConvTy->isEnumeralType() << ConvTy;
19023 }
19024 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
19025 QualType T) override {
19026 return S.Diag(Loc, DiagID: diag::err_omp_ambiguous_conversion) << T;
19027 }
19028 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
19029 QualType ConvTy) override {
19030 return S.Diag(Loc: Conv->getLocation(), DiagID: diag::note_omp_conversion_here)
19031 << ConvTy->isEnumeralType() << ConvTy;
19032 }
19033 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
19034 QualType) override {
19035 llvm_unreachable("conversion functions are permitted");
19036 }
19037 } ConvertDiagnoser;
19038 return SemaRef.PerformContextualImplicitConversion(Loc, FromE: Op, Converter&: ConvertDiagnoser);
19039}
19040
19041static bool
19042isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, OpenMPClauseKind CKind,
19043 bool StrictlyPositive, bool BuildCapture = false,
19044 OpenMPDirectiveKind DKind = OMPD_unknown,
19045 OpenMPDirectiveKind *CaptureRegion = nullptr,
19046 Stmt **HelperValStmt = nullptr) {
19047 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
19048 !ValExpr->isInstantiationDependent()) {
19049 SourceLocation Loc = ValExpr->getExprLoc();
19050 ExprResult Value =
19051 SemaRef.OpenMP().PerformOpenMPImplicitIntegerConversion(Loc, Op: ValExpr);
19052 if (Value.isInvalid())
19053 return false;
19054
19055 ValExpr = Value.get();
19056 // The expression must evaluate to a non-negative integer value.
19057 if (std::optional<llvm::APSInt> Result =
19058 ValExpr->getIntegerConstantExpr(Ctx: SemaRef.Context)) {
19059 if (Result->isSigned() &&
19060 !((!StrictlyPositive && Result->isNonNegative()) ||
19061 (StrictlyPositive && Result->isStrictlyPositive()))) {
19062 SemaRef.Diag(Loc, DiagID: diag::err_omp_negative_expression_in_clause)
19063 << getOpenMPClauseNameForDiag(C: CKind) << (StrictlyPositive ? 1 : 0)
19064 << ValExpr->getSourceRange();
19065 return false;
19066 }
19067 }
19068 if (!BuildCapture)
19069 return true;
19070 *CaptureRegion = getOpenMPCaptureRegionForClause(
19071 DKind, CKind, OMPVersion: SemaRef.getLangOpts().getOpenMPVersion());
19072 if (*CaptureRegion != OMPD_unknown &&
19073 !SemaRef.CurContext->isDependentContext()) {
19074 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
19075 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
19076 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
19077 *HelperValStmt = buildPreInits(Context&: SemaRef.Context, Captures);
19078 }
19079 }
19080 return true;
19081}
19082
19083static std::string getListOfPossibleValues(OpenMPClauseKind K, unsigned First,
19084 unsigned Last,
19085 ArrayRef<unsigned> Exclude = {}) {
19086 SmallString<256> Buffer;
19087 llvm::raw_svector_ostream Out(Buffer);
19088 unsigned Skipped = Exclude.size();
19089 for (unsigned I = First; I < Last; ++I) {
19090 if (llvm::is_contained(Range&: Exclude, Element: I)) {
19091 --Skipped;
19092 continue;
19093 }
19094 Out << "'" << getOpenMPSimpleClauseTypeName(Kind: K, Type: I) << "'";
19095 if (I + Skipped + 2 == Last)
19096 Out << " or ";
19097 else if (I + Skipped + 1 != Last)
19098 Out << ", ";
19099 }
19100 return std::string(Out.str());
19101}
19102
19103OMPClause *SemaOpenMP::ActOnOpenMPNumThreadsClause(
19104 ArrayRef<Expr *> VarList, OpenMPNumThreadsClauseModifier SimpleModifier,
19105 SourceLocation SimpleModifierLoc,
19106 OpenMPNumThreadsClauseModifier ComplexModifier, Expr *ComplexModifierExpr,
19107 SourceLocation ComplexModifierLoc, SourceLocation StartLoc,
19108 SourceLocation LParenLoc, SourceLocation EndLoc) {
19109 // Check that modifiers were correctly specified.
19110 if (ComplexModifierLoc.isValid() &&
19111 (ComplexModifier != OMPC_NUMTHREADS_dims || !ComplexModifierExpr)) {
19112 Diag(Loc: ComplexModifierLoc, DiagID: diag::err_omp_malformed_complex_modifier)
19113 << getOpenMPSimpleClauseTypeName(Kind: OMPC_num_threads, Type: OMPC_NUMTHREADS_dims)
19114 << getOpenMPClauseName(C: OMPC_num_threads);
19115 return nullptr;
19116 }
19117 if (SimpleModifierLoc.isValid() && SimpleModifier == OMPC_NUMTHREADS_dims) {
19118 Diag(Loc: SimpleModifierLoc, DiagID: diag::err_omp_malformed_complex_modifier)
19119 << getOpenMPSimpleClauseTypeName(Kind: OMPC_num_threads, Type: OMPC_NUMTHREADS_dims)
19120 << getOpenMPClauseName(C: OMPC_num_threads);
19121 return nullptr;
19122 }
19123 if (SimpleModifierLoc.isValid() && SimpleModifier != OMPC_NUMTHREADS_strict) {
19124 Diag(Loc: SimpleModifierLoc, DiagID: diag::err_omp_unexpected_clause_value)
19125 << getOpenMPSimpleClauseTypeName(Kind: OMPC_num_threads,
19126 Type: OMPC_NUMTHREADS_strict)
19127 << getOpenMPClauseName(C: OMPC_num_threads);
19128 return nullptr;
19129 }
19130
19131 if (VarList.empty())
19132 return nullptr;
19133
19134 SmallVector<Expr *, 3> Vars(VarList.begin(), VarList.end());
19135 for (Expr *&ValExpr : Vars) {
19136 // OpenMP [2.5, Restrictions]
19137 // The num_threads expression must evaluate to a positive integer value.
19138 if (!isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_num_threads,
19139 /*StrictlyPositive=*/true))
19140 return nullptr;
19141 }
19142
19143 if (ComplexModifier == OMPC_NUMTHREADS_dims) {
19144 ExprResult Res = ActOnOpenMPDimsModifier(Kind: OMPC_num_threads, Modifier: ComplexModifier,
19145 ModifierExpr: ComplexModifierExpr,
19146 ModifierLoc: ComplexModifierLoc, VarList: Vars, VarListEndLoc: EndLoc);
19147 if (Res.isInvalid())
19148 return nullptr;
19149 ComplexModifierExpr = Res.get();
19150
19151 if (validateMultidimClauseExprs(SemaRef&: *this, ClauseKind: OMPC_num_threads, ClauseBeginLoc: StartLoc, ClauseVarList: Vars,
19152 DimsModifierExpr: ComplexModifierExpr))
19153 return nullptr;
19154 }
19155 if (SimpleModifier == OMPC_NUMTHREADS_strict && getLangOpts().OpenMP < 60) {
19156 Diag(Loc: SimpleModifierLoc, DiagID: diag::err_omp_modifier_requires_version)
19157 << getOpenMPSimpleClauseTypeName(Kind: OMPC_num_threads, Type: SimpleModifier)
19158 << getOpenMPClauseName(C: OMPC_num_threads) << "6.0";
19159 return nullptr;
19160 }
19161
19162 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
19163 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
19164 DKind, CKind: OMPC_num_threads, OMPVersion: getLangOpts().getOpenMPVersion());
19165 if (CaptureRegion == OMPD_unknown || SemaRef.CurContext->isDependentContext())
19166 return OMPNumThreadsClause::Create(
19167 C: getASTContext(), CaptureRegion, StartLoc, LParenLoc, EndLoc, VL: Vars,
19168 PrescriptivenessModifier: SimpleModifier, DimsModifier: ComplexModifier, PrescriptivenessModifierLoc: SimpleModifierLoc, DimsModifierLoc: ComplexModifierLoc,
19169 DimsModifierExpr: ComplexModifierExpr, /*PreInit=*/nullptr);
19170
19171 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
19172 for (Expr *&ValExpr : Vars) {
19173 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
19174 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
19175 }
19176 if (ComplexModifierExpr) {
19177 ComplexModifierExpr = SemaRef.MakeFullExpr(Arg: ComplexModifierExpr).get();
19178 ComplexModifierExpr =
19179 tryBuildCapture(SemaRef, Capture: ComplexModifierExpr, Captures).get();
19180 }
19181 Stmt *PreInit = buildPreInits(Context&: getASTContext(), Captures);
19182
19183 return OMPNumThreadsClause::Create(
19184 C: getASTContext(), CaptureRegion, StartLoc, LParenLoc, EndLoc, VL: Vars,
19185 PrescriptivenessModifier: SimpleModifier, DimsModifier: ComplexModifier, PrescriptivenessModifierLoc: SimpleModifierLoc, DimsModifierLoc: ComplexModifierLoc,
19186 DimsModifierExpr: ComplexModifierExpr, PreInit);
19187}
19188
19189ExprResult SemaOpenMP::VerifyPositiveIntegerConstantInClause(
19190 Expr *E, OpenMPClauseKind CKind, bool StrictlyPositive,
19191 bool SuppressExprDiags) {
19192 if (!E)
19193 return ExprError();
19194 if (E->isValueDependent() || E->isTypeDependent() ||
19195 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
19196 return E;
19197
19198 llvm::APSInt Result;
19199 ExprResult ICE;
19200 if (SuppressExprDiags) {
19201 // Use a custom diagnoser that suppresses 'note' diagnostics about the
19202 // expression.
19203 struct SuppressedDiagnoser : public Sema::VerifyICEDiagnoser {
19204 SuppressedDiagnoser() : VerifyICEDiagnoser(/*Suppress=*/true) {}
19205 SemaBase::SemaDiagnosticBuilder
19206 diagnoseNotICE(Sema &S, SourceLocation Loc) override {
19207 llvm_unreachable("Diagnostic suppressed");
19208 }
19209 } Diagnoser;
19210 ICE = SemaRef.VerifyIntegerConstantExpression(E, Result: &Result, Diagnoser,
19211 CanFold: AllowFoldKind::Allow);
19212 } else {
19213 ICE =
19214 SemaRef.VerifyIntegerConstantExpression(E, Result: &Result,
19215 /*FIXME*/ CanFold: AllowFoldKind::Allow);
19216 }
19217 if (ICE.isInvalid())
19218 return ExprError();
19219
19220 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
19221 (!StrictlyPositive && !Result.isNonNegative())) {
19222 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_negative_expression_in_clause)
19223 << getOpenMPClauseNameForDiag(C: CKind) << (StrictlyPositive ? 1 : 0)
19224 << E->getSourceRange();
19225 return ExprError();
19226 }
19227 if ((CKind == OMPC_aligned || CKind == OMPC_align ||
19228 CKind == OMPC_allocate) &&
19229 !Result.isPowerOf2()) {
19230 Diag(Loc: E->getExprLoc(), DiagID: diag::warn_omp_alignment_not_power_of_two)
19231 << E->getSourceRange();
19232 return ExprError();
19233 }
19234
19235 if (!Result.isRepresentableByInt64()) {
19236 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_large_expression_in_clause)
19237 << getOpenMPClauseNameForDiag(C: CKind) << E->getSourceRange();
19238 return ExprError();
19239 }
19240
19241 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
19242 DSAStack->setAssociatedLoops(Result.getExtValue());
19243 else if (CKind == OMPC_ordered)
19244 DSAStack->setAssociatedLoops(Result.getExtValue());
19245 return ICE;
19246}
19247
19248void SemaOpenMP::setOpenMPDeviceNum(int Num) { DeviceNum = Num; }
19249
19250void SemaOpenMP::setOpenMPDeviceNumID(StringRef ID) { DeviceNumID = ID; }
19251
19252int SemaOpenMP::getOpenMPDeviceNum() const { return DeviceNum; }
19253
19254void SemaOpenMP::ActOnOpenMPDeviceNum(Expr *DeviceNumExpr) {
19255 llvm::APSInt Result;
19256 Expr::EvalResult EvalResult;
19257 // Evaluate the expression to an integer value
19258 if (!DeviceNumExpr->isValueDependent() &&
19259 DeviceNumExpr->EvaluateAsInt(Result&: EvalResult, Ctx: SemaRef.Context)) {
19260 // The device expression must evaluate to a non-negative integer value.
19261 Result = EvalResult.Val.getInt();
19262 if (Result.isNonNegative()) {
19263 setOpenMPDeviceNum(Result.getZExtValue());
19264 } else {
19265 Diag(Loc: DeviceNumExpr->getExprLoc(),
19266 DiagID: diag::err_omp_negative_expression_in_clause)
19267 << "device_num" << 0 << DeviceNumExpr->getSourceRange();
19268 }
19269 } else if (auto *DeclRef = dyn_cast<DeclRefExpr>(Val: DeviceNumExpr)) {
19270 // Check if the expression is an identifier
19271 IdentifierInfo *IdInfo = DeclRef->getDecl()->getIdentifier();
19272 if (IdInfo) {
19273 setOpenMPDeviceNumID(IdInfo->getName());
19274 }
19275 } else {
19276 Diag(Loc: DeviceNumExpr->getExprLoc(), DiagID: diag::err_expected_expression);
19277 }
19278}
19279
19280OMPClause *SemaOpenMP::ActOnOpenMPSafelenClause(Expr *Len,
19281 SourceLocation StartLoc,
19282 SourceLocation LParenLoc,
19283 SourceLocation EndLoc) {
19284 // OpenMP [2.8.1, simd construct, Description]
19285 // The parameter of the safelen clause must be a constant
19286 // positive integer expression.
19287 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(E: Len, CKind: OMPC_safelen);
19288 if (Safelen.isInvalid())
19289 return nullptr;
19290 return new (getASTContext())
19291 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
19292}
19293
19294OMPClause *SemaOpenMP::ActOnOpenMPSimdlenClause(Expr *Len,
19295 SourceLocation StartLoc,
19296 SourceLocation LParenLoc,
19297 SourceLocation EndLoc) {
19298 // OpenMP [2.8.1, simd construct, Description]
19299 // The parameter of the simdlen clause must be a constant
19300 // positive integer expression.
19301 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(E: Len, CKind: OMPC_simdlen);
19302 if (Simdlen.isInvalid())
19303 return nullptr;
19304 return new (getASTContext())
19305 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
19306}
19307
19308/// Tries to find omp_allocator_handle_t type.
19309static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc,
19310 DSAStackTy *Stack) {
19311 if (!Stack->getOMPAllocatorHandleT().isNull())
19312 return true;
19313
19314 // Set the allocator handle type.
19315 IdentifierInfo *II = &S.PP.getIdentifierTable().get(Name: "omp_allocator_handle_t");
19316 ParsedType PT = S.getTypeName(II: *II, NameLoc: Loc, S: S.getCurScope());
19317 if (!PT.getAsOpaquePtr() || PT.get().isNull()) {
19318 S.Diag(Loc, DiagID: diag::err_omp_implied_type_not_found)
19319 << "omp_allocator_handle_t";
19320 return false;
19321 }
19322 QualType AllocatorHandleEnumTy = PT.get();
19323 AllocatorHandleEnumTy.addConst();
19324
19325 // Fill the predefined allocator map.
19326 bool ErrorFound = false;
19327 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
19328 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
19329 StringRef Allocator =
19330 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(Val: AllocatorKind);
19331 DeclarationName AllocatorName = &S.getASTContext().Idents.get(Name: Allocator);
19332 auto *VD = dyn_cast_or_null<ValueDecl>(
19333 Val: S.LookupSingleName(S: S.TUScope, Name: AllocatorName, Loc, NameKind: Sema::LookupAnyName));
19334 if (!VD) {
19335 ErrorFound = true;
19336 break;
19337 }
19338 QualType AllocatorType =
19339 VD->getType().getNonLValueExprType(Context: S.getASTContext());
19340 ExprResult Res = S.BuildDeclRefExpr(D: VD, Ty: AllocatorType, VK: VK_LValue, Loc);
19341 if (!Res.isUsable()) {
19342 ErrorFound = true;
19343 break;
19344 }
19345 Res = S.PerformImplicitConversion(From: Res.get(), ToType: AllocatorHandleEnumTy,
19346 Action: AssignmentAction::Initializing,
19347 /*AllowExplicit=*/true);
19348 if (!Res.isUsable()) {
19349 ErrorFound = true;
19350 break;
19351 }
19352 Stack->setAllocator(AllocatorKind, Allocator: Res.get());
19353 }
19354 if (ErrorFound) {
19355 S.Diag(Loc, DiagID: diag::err_omp_implied_type_not_found)
19356 << "omp_allocator_handle_t";
19357 return false;
19358 }
19359
19360 // Record the type only now. It is what tells a later call that the map above
19361 // is ready to be read, so setting it before the map is filled would let that
19362 // call proceed on a map this one gave up on halfway through.
19363 Stack->setOMPAllocatorHandleT(AllocatorHandleEnumTy);
19364
19365 return true;
19366}
19367
19368OMPClause *SemaOpenMP::ActOnOpenMPAllocatorClause(Expr *A,
19369 SourceLocation StartLoc,
19370 SourceLocation LParenLoc,
19371 SourceLocation EndLoc) {
19372 // OpenMP [2.11.3, allocate Directive, Description]
19373 // allocator is an expression of omp_allocator_handle_t type.
19374 if (!findOMPAllocatorHandleT(S&: SemaRef, Loc: A->getExprLoc(), DSAStack))
19375 return nullptr;
19376
19377 ExprResult Allocator = SemaRef.DefaultLvalueConversion(E: A);
19378 if (Allocator.isInvalid())
19379 return nullptr;
19380 Allocator = SemaRef.PerformImplicitConversion(
19381 From: Allocator.get(), DSAStack->getOMPAllocatorHandleT(),
19382 Action: AssignmentAction::Initializing,
19383 /*AllowExplicit=*/true);
19384 if (Allocator.isInvalid())
19385 return nullptr;
19386 return new (getASTContext())
19387 OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc);
19388}
19389
19390OMPClause *SemaOpenMP::ActOnOpenMPCollapseClause(Expr *NumForLoops,
19391 SourceLocation StartLoc,
19392 SourceLocation LParenLoc,
19393 SourceLocation EndLoc) {
19394 // OpenMP [2.7.1, loop construct, Description]
19395 // OpenMP [2.8.1, simd construct, Description]
19396 // OpenMP [2.9.6, distribute construct, Description]
19397 // The parameter of the collapse clause must be a constant
19398 // positive integer expression.
19399 ExprResult NumForLoopsResult =
19400 VerifyPositiveIntegerConstantInClause(E: NumForLoops, CKind: OMPC_collapse);
19401 if (NumForLoopsResult.isInvalid())
19402 return nullptr;
19403 return new (getASTContext())
19404 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
19405}
19406
19407OMPClause *SemaOpenMP::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
19408 SourceLocation EndLoc,
19409 SourceLocation LParenLoc,
19410 Expr *NumForLoops) {
19411 // OpenMP [2.7.1, loop construct, Description]
19412 // OpenMP [2.8.1, simd construct, Description]
19413 // OpenMP [2.9.6, distribute construct, Description]
19414 // The parameter of the ordered clause must be a constant
19415 // positive integer expression if any.
19416 if (NumForLoops && LParenLoc.isValid()) {
19417 ExprResult NumForLoopsResult =
19418 VerifyPositiveIntegerConstantInClause(E: NumForLoops, CKind: OMPC_ordered);
19419 if (NumForLoopsResult.isInvalid())
19420 return nullptr;
19421 NumForLoops = NumForLoopsResult.get();
19422 } else {
19423 NumForLoops = nullptr;
19424 }
19425 auto *Clause =
19426 OMPOrderedClause::Create(C: getASTContext(), Num: NumForLoops,
19427 NumLoops: NumForLoops ? DSAStack->getAssociatedLoops() : 0,
19428 StartLoc, LParenLoc, EndLoc);
19429 DSAStack->setOrderedRegion(/*IsOrdered=*/true, Param: NumForLoops, Clause);
19430 return Clause;
19431}
19432
19433OMPClause *SemaOpenMP::ActOnOpenMPSimpleClause(
19434 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
19435 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
19436 OMPClause *Res = nullptr;
19437 switch (Kind) {
19438 case OMPC_proc_bind:
19439 Res = ActOnOpenMPProcBindClause(Kind: static_cast<ProcBindKind>(Argument),
19440 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
19441 break;
19442 case OMPC_atomic_default_mem_order:
19443 Res = ActOnOpenMPAtomicDefaultMemOrderClause(
19444 Kind: static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument),
19445 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
19446 break;
19447 case OMPC_fail:
19448 Res = ActOnOpenMPFailClause(Kind: static_cast<OpenMPClauseKind>(Argument),
19449 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
19450 break;
19451 case OMPC_update_depend_objects:
19452 Res = ActOnOpenMPUpdateDependObjectsClause(
19453 Kind: static_cast<OpenMPDependClauseKind>(Argument), KindLoc: ArgumentLoc, StartLoc,
19454 LParenLoc, EndLoc);
19455 break;
19456 case OMPC_bind:
19457 Res = ActOnOpenMPBindClause(Kind: static_cast<OpenMPBindClauseKind>(Argument),
19458 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
19459 break;
19460 case OMPC_at:
19461 Res = ActOnOpenMPAtClause(Kind: static_cast<OpenMPAtClauseKind>(Argument),
19462 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
19463 break;
19464 case OMPC_severity:
19465 Res = ActOnOpenMPSeverityClause(
19466 Kind: static_cast<OpenMPSeverityClauseKind>(Argument), KindLoc: ArgumentLoc, StartLoc,
19467 LParenLoc, EndLoc);
19468 break;
19469 case OMPC_threadset:
19470 Res = ActOnOpenMPThreadsetClause(Kind: static_cast<OpenMPThreadsetKind>(Argument),
19471 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
19472 break;
19473 case OMPC_if:
19474 case OMPC_final:
19475 case OMPC_num_threads:
19476 case OMPC_safelen:
19477 case OMPC_simdlen:
19478 case OMPC_sizes:
19479 case OMPC_depth:
19480 case OMPC_allocator:
19481 case OMPC_collapse:
19482 case OMPC_schedule:
19483 case OMPC_private:
19484 case OMPC_firstprivate:
19485 case OMPC_lastprivate:
19486 case OMPC_shared:
19487 case OMPC_reduction:
19488 case OMPC_task_reduction:
19489 case OMPC_in_reduction:
19490 case OMPC_linear:
19491 case OMPC_aligned:
19492 case OMPC_copyin:
19493 case OMPC_copyprivate:
19494 case OMPC_ordered:
19495 case OMPC_nowait:
19496 case OMPC_untied:
19497 case OMPC_mergeable:
19498 case OMPC_threadprivate:
19499 case OMPC_groupprivate:
19500 case OMPC_allocate:
19501 case OMPC_flush:
19502 case OMPC_depobj:
19503 case OMPC_read:
19504 case OMPC_write:
19505 case OMPC_capture:
19506 case OMPC_compare:
19507 case OMPC_update:
19508 case OMPC_seq_cst:
19509 case OMPC_acq_rel:
19510 case OMPC_acquire:
19511 case OMPC_release:
19512 case OMPC_relaxed:
19513 case OMPC_depend:
19514 case OMPC_device:
19515 case OMPC_threads:
19516 case OMPC_simd:
19517 case OMPC_map:
19518 case OMPC_num_teams:
19519 case OMPC_thread_limit:
19520 case OMPC_priority:
19521 case OMPC_grainsize:
19522 case OMPC_nogroup:
19523 case OMPC_num_tasks:
19524 case OMPC_hint:
19525 case OMPC_dist_schedule:
19526 case OMPC_default:
19527 case OMPC_defaultmap:
19528 case OMPC_unknown:
19529 case OMPC_uniform:
19530 case OMPC_to:
19531 case OMPC_from:
19532 case OMPC_use_device_ptr:
19533 case OMPC_use_device_addr:
19534 case OMPC_is_device_ptr:
19535 case OMPC_has_device_addr:
19536 case OMPC_unified_address:
19537 case OMPC_unified_shared_memory:
19538 case OMPC_reverse_offload:
19539 case OMPC_dynamic_allocators:
19540 case OMPC_self_maps:
19541 case OMPC_device_type:
19542 case OMPC_match:
19543 case OMPC_nontemporal:
19544 case OMPC_destroy:
19545 case OMPC_novariants:
19546 case OMPC_nocontext:
19547 case OMPC_detach:
19548 case OMPC_inclusive:
19549 case OMPC_exclusive:
19550 case OMPC_uses_allocators:
19551 case OMPC_affinity:
19552 case OMPC_when:
19553 case OMPC_message:
19554 default:
19555 llvm_unreachable("Clause is not allowed.");
19556 }
19557 return Res;
19558}
19559
19560OMPClause *SemaOpenMP::ActOnOpenMPDefaultClause(
19561 llvm::omp::DefaultKind M, SourceLocation MLoc,
19562 OpenMPDefaultClauseVariableCategory VCKind, SourceLocation VCKindLoc,
19563 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
19564 if (M == OMP_DEFAULT_unknown) {
19565 Diag(Loc: MLoc, DiagID: diag::err_omp_unexpected_clause_value)
19566 << getListOfPossibleValues(K: OMPC_default, /*First=*/0,
19567 /*Last=*/unsigned(OMP_DEFAULT_unknown))
19568 << getOpenMPClauseNameForDiag(C: OMPC_default);
19569 return nullptr;
19570 }
19571 if (VCKind == OMPC_DEFAULT_VC_unknown) {
19572 Diag(Loc: VCKindLoc, DiagID: diag::err_omp_default_vc)
19573 << getOpenMPSimpleClauseTypeName(Kind: OMPC_default, Type: unsigned(M));
19574 return nullptr;
19575 }
19576
19577 bool IsTargetDefault =
19578 getLangOpts().OpenMP >= 60 &&
19579 isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective());
19580
19581 // OpenMP 6.0, page 224, lines 3-4 default Clause, Semantics
19582 // If data-sharing-attribute is shared then the clause has no effect
19583 // on a target construct;
19584 if (IsTargetDefault && M == OMP_DEFAULT_shared)
19585 return nullptr;
19586
19587 auto SetDefaultClauseAttrs = [&](llvm::omp::DefaultKind M,
19588 OpenMPDefaultClauseVariableCategory VCKind) {
19589 OpenMPDefaultmapClauseModifier DefMapMod;
19590 OpenMPDefaultmapClauseKind DefMapKind;
19591 // default data-sharing-attribute
19592 switch (M) {
19593 case OMP_DEFAULT_none:
19594 if (IsTargetDefault)
19595 DefMapMod = OMPC_DEFAULTMAP_MODIFIER_none;
19596 else
19597 DSAStack->setDefaultDSANone(MLoc);
19598 break;
19599 case OMP_DEFAULT_firstprivate:
19600 if (IsTargetDefault)
19601 DefMapMod = OMPC_DEFAULTMAP_MODIFIER_firstprivate;
19602 else
19603 DSAStack->setDefaultDSAFirstPrivate(MLoc);
19604 break;
19605 case OMP_DEFAULT_private:
19606 if (IsTargetDefault)
19607 DefMapMod = OMPC_DEFAULTMAP_MODIFIER_private;
19608 else
19609 DSAStack->setDefaultDSAPrivate(MLoc);
19610 break;
19611 case OMP_DEFAULT_shared:
19612 assert(!IsTargetDefault && "DSA shared invalid with target directive");
19613 DSAStack->setDefaultDSAShared(MLoc);
19614 break;
19615 default:
19616 llvm_unreachable("unexpected DSA in OpenMP default clause");
19617 }
19618 // default variable-category
19619 switch (VCKind) {
19620 case OMPC_DEFAULT_VC_aggregate:
19621 if (IsTargetDefault)
19622 DefMapKind = OMPC_DEFAULTMAP_aggregate;
19623 else
19624 DSAStack->setDefaultDSAVCAggregate(VCKindLoc);
19625 break;
19626 case OMPC_DEFAULT_VC_pointer:
19627 if (IsTargetDefault)
19628 DefMapKind = OMPC_DEFAULTMAP_pointer;
19629 else
19630 DSAStack->setDefaultDSAVCPointer(VCKindLoc);
19631 break;
19632 case OMPC_DEFAULT_VC_scalar:
19633 if (IsTargetDefault)
19634 DefMapKind = OMPC_DEFAULTMAP_scalar;
19635 else
19636 DSAStack->setDefaultDSAVCScalar(VCKindLoc);
19637 break;
19638 case OMPC_DEFAULT_VC_all:
19639 if (IsTargetDefault)
19640 DefMapKind = OMPC_DEFAULTMAP_all;
19641 else
19642 DSAStack->setDefaultDSAVCAll(VCKindLoc);
19643 break;
19644 default:
19645 llvm_unreachable("unexpected variable category in OpenMP default clause");
19646 }
19647 // OpenMP 6.0, page 224, lines 4-5 default Clause, Semantics
19648 // otherwise, its effect on a target construct is equivalent to
19649 // specifying the defaultmap clause with the same data-sharing-attribute
19650 // and variable-category.
19651 //
19652 // If earlier than OpenMP 6.0, or not a target directive, the default DSA
19653 // is/was set as before.
19654 if (IsTargetDefault) {
19655 if (DefMapKind == OMPC_DEFAULTMAP_all) {
19656 DSAStack->setDefaultDMAAttr(M: DefMapMod, Kind: OMPC_DEFAULTMAP_aggregate, Loc: MLoc);
19657 DSAStack->setDefaultDMAAttr(M: DefMapMod, Kind: OMPC_DEFAULTMAP_scalar, Loc: MLoc);
19658 DSAStack->setDefaultDMAAttr(M: DefMapMod, Kind: OMPC_DEFAULTMAP_pointer, Loc: MLoc);
19659 } else {
19660 DSAStack->setDefaultDMAAttr(M: DefMapMod, Kind: DefMapKind, Loc: MLoc);
19661 }
19662 }
19663 };
19664
19665 SetDefaultClauseAttrs(M, VCKind);
19666 return new (getASTContext())
19667 OMPDefaultClause(M, MLoc, VCKind, VCKindLoc, StartLoc, LParenLoc, EndLoc);
19668}
19669
19670OMPClause *SemaOpenMP::ActOnOpenMPThreadsetClause(OpenMPThreadsetKind Kind,
19671 SourceLocation KindLoc,
19672 SourceLocation StartLoc,
19673 SourceLocation LParenLoc,
19674 SourceLocation EndLoc) {
19675 if (Kind == OMPC_THREADSET_unknown) {
19676 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
19677 << getListOfPossibleValues(K: OMPC_threadset, /*First=*/0,
19678 /*Last=*/unsigned(OMPC_THREADSET_unknown))
19679 << getOpenMPClauseName(C: OMPC_threadset);
19680 return nullptr;
19681 }
19682
19683 return new (getASTContext())
19684 OMPThreadsetClause(Kind, KindLoc, StartLoc, LParenLoc, EndLoc);
19685}
19686
19687static OMPClause *
19688createTransparentClause(Sema &SemaRef, ASTContext &Ctx, Expr *ImpexTypeArg,
19689 Stmt *HelperValStmt, OpenMPDirectiveKind CaptureRegion,
19690 SourceLocation StartLoc, SourceLocation LParenLoc,
19691 SourceLocation EndLoc) {
19692 ExprResult ER = SemaRef.DefaultLvalueConversion(E: ImpexTypeArg);
19693 if (ER.isInvalid())
19694 return nullptr;
19695
19696 return new (Ctx) OMPTransparentClause(ER.get(), HelperValStmt, CaptureRegion,
19697 StartLoc, LParenLoc, EndLoc);
19698}
19699
19700OMPClause *SemaOpenMP::ActOnOpenMPTransparentClause(Expr *ImpexTypeArg,
19701 SourceLocation StartLoc,
19702 SourceLocation LParenLoc,
19703 SourceLocation EndLoc) {
19704 Stmt *HelperValStmt = nullptr;
19705 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
19706 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
19707 DKind, CKind: OMPC_transparent, OMPVersion: getLangOpts().getOpenMPVersion());
19708 if (CaptureRegion != OMPD_unknown &&
19709 !SemaRef.CurContext->isDependentContext()) {
19710 Expr *ValExpr = SemaRef.MakeFullExpr(Arg: ImpexTypeArg).get();
19711 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
19712 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
19713 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
19714 }
19715 if (!ImpexTypeArg) {
19716 return new (getASTContext())
19717 OMPTransparentClause(ImpexTypeArg, HelperValStmt, CaptureRegion,
19718 StartLoc, LParenLoc, EndLoc);
19719 }
19720 QualType Ty = ImpexTypeArg->getType();
19721
19722 if (const auto *TT = Ty->getAs<TypedefType>()) {
19723 const TypedefNameDecl *TypedefDecl = TT->getDecl();
19724 llvm::StringRef TypedefName = TypedefDecl->getName();
19725 IdentifierInfo &II = SemaRef.PP.getIdentifierTable().get(Name: TypedefName);
19726 ParsedType ImpexTy =
19727 SemaRef.getTypeName(II, NameLoc: StartLoc, S: SemaRef.getCurScope());
19728 if (!ImpexTy.getAsOpaquePtr() || ImpexTy.get().isNull()) {
19729 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_implied_type_not_found)
19730 << TypedefName;
19731 return nullptr;
19732 }
19733 return new (getASTContext())
19734 OMPTransparentClause(ImpexTypeArg, HelperValStmt, CaptureRegion,
19735 StartLoc, LParenLoc, EndLoc);
19736 }
19737
19738 if (Ty->isEnumeralType())
19739 return createTransparentClause(SemaRef, Ctx&: getASTContext(), ImpexTypeArg,
19740 HelperValStmt, CaptureRegion, StartLoc,
19741 LParenLoc, EndLoc);
19742 if (Ty->isIntegerType()) {
19743 if (isNonNegativeIntegerValue(ValExpr&: ImpexTypeArg, SemaRef, CKind: OMPC_transparent,
19744 /*StrictlyPositive=*/false)) {
19745 ExprResult Value =
19746 SemaRef.OpenMP().PerformOpenMPImplicitIntegerConversion(Loc: StartLoc,
19747 Op: ImpexTypeArg);
19748 if (std::optional<llvm::APSInt> Result =
19749 Value.get()->getIntegerConstantExpr(Ctx: SemaRef.Context)) {
19750 if (Result->isNegative() ||
19751 Result >
19752 static_cast<int64_t>(SemaOpenMP::OpenMPImpexType::OMP_Export))
19753 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_transparent_invalid_value);
19754 }
19755 return new (getASTContext())
19756 OMPTransparentClause(ImpexTypeArg, HelperValStmt, CaptureRegion,
19757 StartLoc, LParenLoc, EndLoc);
19758 }
19759 }
19760 if (!isNonNegativeIntegerValue(ValExpr&: ImpexTypeArg, SemaRef, CKind: OMPC_transparent,
19761 /*StrictlyPositive=*/true))
19762 return nullptr;
19763 return new (getASTContext()) OMPTransparentClause(
19764 ImpexTypeArg, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
19765}
19766
19767OMPClause *SemaOpenMP::ActOnOpenMPProcBindClause(ProcBindKind Kind,
19768 SourceLocation KindKwLoc,
19769 SourceLocation StartLoc,
19770 SourceLocation LParenLoc,
19771 SourceLocation EndLoc) {
19772 if (Kind == OMP_PROC_BIND_unknown) {
19773 Diag(Loc: KindKwLoc, DiagID: diag::err_omp_unexpected_clause_value)
19774 << getListOfPossibleValues(K: OMPC_proc_bind,
19775 /*First=*/unsigned(OMP_PROC_BIND_master),
19776 /*Last=*/
19777 unsigned(getLangOpts().OpenMP > 50
19778 ? OMP_PROC_BIND_primary
19779 : OMP_PROC_BIND_spread) +
19780 1)
19781 << getOpenMPClauseNameForDiag(C: OMPC_proc_bind);
19782 return nullptr;
19783 }
19784 if (Kind == OMP_PROC_BIND_primary && getLangOpts().OpenMP < 51)
19785 Diag(Loc: KindKwLoc, DiagID: diag::err_omp_unexpected_clause_value)
19786 << getListOfPossibleValues(K: OMPC_proc_bind,
19787 /*First=*/unsigned(OMP_PROC_BIND_master),
19788 /*Last=*/
19789 unsigned(OMP_PROC_BIND_spread) + 1)
19790 << getOpenMPClauseNameForDiag(C: OMPC_proc_bind);
19791 return new (getASTContext())
19792 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
19793}
19794
19795OMPClause *SemaOpenMP::ActOnOpenMPAtomicDefaultMemOrderClause(
19796 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc,
19797 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
19798 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) {
19799 Diag(Loc: KindKwLoc, DiagID: diag::err_omp_unexpected_clause_value)
19800 << getListOfPossibleValues(
19801 K: OMPC_atomic_default_mem_order, /*First=*/0,
19802 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown)
19803 << getOpenMPClauseNameForDiag(C: OMPC_atomic_default_mem_order);
19804 return nullptr;
19805 }
19806 return new (getASTContext()) OMPAtomicDefaultMemOrderClause(
19807 Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
19808}
19809
19810OMPClause *SemaOpenMP::ActOnOpenMPAtClause(OpenMPAtClauseKind Kind,
19811 SourceLocation KindKwLoc,
19812 SourceLocation StartLoc,
19813 SourceLocation LParenLoc,
19814 SourceLocation EndLoc) {
19815 if (Kind == OMPC_AT_unknown) {
19816 Diag(Loc: KindKwLoc, DiagID: diag::err_omp_unexpected_clause_value)
19817 << getListOfPossibleValues(K: OMPC_at, /*First=*/0,
19818 /*Last=*/OMPC_AT_unknown)
19819 << getOpenMPClauseNameForDiag(C: OMPC_at);
19820 return nullptr;
19821 }
19822 return new (getASTContext())
19823 OMPAtClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
19824}
19825
19826OMPClause *SemaOpenMP::ActOnOpenMPSeverityClause(OpenMPSeverityClauseKind Kind,
19827 SourceLocation KindKwLoc,
19828 SourceLocation StartLoc,
19829 SourceLocation LParenLoc,
19830 SourceLocation EndLoc) {
19831 if (Kind == OMPC_SEVERITY_unknown) {
19832 Diag(Loc: KindKwLoc, DiagID: diag::err_omp_unexpected_clause_value)
19833 << getListOfPossibleValues(K: OMPC_severity, /*First=*/0,
19834 /*Last=*/OMPC_SEVERITY_unknown)
19835 << getOpenMPClauseNameForDiag(C: OMPC_severity);
19836 return nullptr;
19837 }
19838 return new (getASTContext())
19839 OMPSeverityClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
19840}
19841
19842OMPClause *SemaOpenMP::ActOnOpenMPMessageClause(Expr *ME,
19843 SourceLocation StartLoc,
19844 SourceLocation LParenLoc,
19845 SourceLocation EndLoc) {
19846 assert(ME && "NULL expr in Message clause");
19847 QualType Type = ME->getType();
19848 if ((!Type->isPointerType() && !Type->isArrayType()) ||
19849 !Type->getPointeeOrArrayElementType()->isAnyCharacterType()) {
19850 Diag(Loc: ME->getBeginLoc(), DiagID: diag::warn_clause_expected_string)
19851 << getOpenMPClauseNameForDiag(C: OMPC_message) << 0;
19852 return nullptr;
19853 }
19854
19855 Stmt *HelperValStmt = nullptr;
19856
19857 // Depending on whether this clause appears in an executable context or not,
19858 // we may or may not build a capture.
19859 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
19860 OpenMPDirectiveKind CaptureRegion =
19861 DKind == OMPD_unknown
19862 ? OMPD_unknown
19863 : getOpenMPCaptureRegionForClause(DKind, CKind: OMPC_message,
19864 OMPVersion: getLangOpts().getOpenMPVersion());
19865 if (CaptureRegion != OMPD_unknown &&
19866 !SemaRef.CurContext->isDependentContext()) {
19867 ME = SemaRef.MakeFullExpr(Arg: ME).get();
19868 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
19869 ME = tryBuildCapture(SemaRef, Capture: ME, Captures).get();
19870 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
19871 }
19872
19873 // Convert array type to pointer type if needed.
19874 ME = SemaRef.DefaultFunctionArrayLvalueConversion(E: ME).get();
19875
19876 return new (getASTContext()) OMPMessageClause(
19877 ME, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
19878}
19879
19880OMPClause *SemaOpenMP::ActOnOpenMPOrderClause(
19881 OpenMPOrderClauseModifier Modifier, OpenMPOrderClauseKind Kind,
19882 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
19883 SourceLocation KindLoc, SourceLocation EndLoc) {
19884 if (Kind != OMPC_ORDER_concurrent ||
19885 (getLangOpts().OpenMP < 51 && MLoc.isValid())) {
19886 // Kind should be concurrent,
19887 // Modifiers introduced in OpenMP 5.1
19888 static_assert(OMPC_ORDER_unknown > 0,
19889 "OMPC_ORDER_unknown not greater than 0");
19890
19891 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
19892 << getListOfPossibleValues(K: OMPC_order,
19893 /*First=*/0,
19894 /*Last=*/OMPC_ORDER_unknown)
19895 << getOpenMPClauseNameForDiag(C: OMPC_order);
19896 return nullptr;
19897 }
19898 if (getLangOpts().OpenMP >= 51 && Modifier == OMPC_ORDER_MODIFIER_unknown &&
19899 MLoc.isValid()) {
19900 Diag(Loc: MLoc, DiagID: diag::err_omp_unexpected_clause_value)
19901 << getListOfPossibleValues(K: OMPC_order,
19902 /*First=*/OMPC_ORDER_MODIFIER_unknown + 1,
19903 /*Last=*/OMPC_ORDER_MODIFIER_last)
19904 << getOpenMPClauseNameForDiag(C: OMPC_order);
19905 } else if (getLangOpts().OpenMP >= 50) {
19906 DSAStack->setRegionHasOrderConcurrent(/*HasOrderConcurrent=*/true);
19907 if (DSAStack->getCurScope()) {
19908 // mark the current scope with 'order' flag
19909 unsigned existingFlags = DSAStack->getCurScope()->getFlags();
19910 DSAStack->getCurScope()->setFlags(existingFlags |
19911 Scope::OpenMPOrderClauseScope);
19912 }
19913 }
19914 return new (getASTContext()) OMPOrderClause(
19915 Kind, KindLoc, StartLoc, LParenLoc, EndLoc, Modifier, MLoc);
19916}
19917
19918OMPClause *SemaOpenMP::ActOnOpenMPUpdateDependObjectsClause(
19919 OpenMPDependClauseKind Kind, SourceLocation KindKwLoc,
19920 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
19921 if (Kind == OMPC_DEPEND_unknown || Kind == OMPC_DEPEND_source ||
19922 Kind == OMPC_DEPEND_sink || Kind == OMPC_DEPEND_depobj) {
19923 SmallVector<unsigned> Except = {
19924 OMPC_DEPEND_source, OMPC_DEPEND_sink, OMPC_DEPEND_depobj,
19925 OMPC_DEPEND_outallmemory, OMPC_DEPEND_inoutallmemory};
19926 if (getLangOpts().OpenMP < 51)
19927 Except.push_back(Elt: OMPC_DEPEND_inoutset);
19928 Diag(Loc: KindKwLoc, DiagID: diag::err_omp_unexpected_clause_value)
19929 << getListOfPossibleValues(K: OMPC_depend, /*First=*/0,
19930 /*Last=*/OMPC_DEPEND_unknown, Exclude: Except)
19931 << getOpenMPClauseNameForDiag(C: OMPC_update_depend_objects);
19932 return nullptr;
19933 }
19934 return OMPUpdateDependObjectsClause::Create(
19935 C: getASTContext(), StartLoc, LParenLoc, ArgumentLoc: KindKwLoc, DK: Kind, EndLoc);
19936}
19937
19938OMPClause *SemaOpenMP::ActOnOpenMPSizesClause(ArrayRef<Expr *> SizeExprs,
19939 SourceLocation StartLoc,
19940 SourceLocation LParenLoc,
19941 SourceLocation EndLoc) {
19942 SmallVector<Expr *> SanitizedSizeExprs(SizeExprs);
19943
19944 for (Expr *&SizeExpr : SanitizedSizeExprs) {
19945 // Skip if already sanitized, e.g. during a partial template instantiation.
19946 if (!SizeExpr)
19947 continue;
19948
19949 bool IsValid = isNonNegativeIntegerValue(ValExpr&: SizeExpr, SemaRef, CKind: OMPC_sizes,
19950 /*StrictlyPositive=*/true);
19951
19952 // isNonNegativeIntegerValue returns true for non-integral types (but still
19953 // emits error diagnostic), so check for the expected type explicitly.
19954 QualType SizeTy = SizeExpr->getType();
19955 if (!SizeTy->isIntegerType())
19956 IsValid = false;
19957
19958 // Handling in templates is tricky. There are four possibilities to
19959 // consider:
19960 //
19961 // 1a. The expression is valid and we are in a instantiated template or not
19962 // in a template:
19963 // Pass valid expression to be further analysed later in Sema.
19964 // 1b. The expression is valid and we are in a template (including partial
19965 // instantiation):
19966 // isNonNegativeIntegerValue skipped any checks so there is no
19967 // guarantee it will be correct after instantiation.
19968 // ActOnOpenMPSizesClause will be called again at instantiation when
19969 // it is not in a dependent context anymore. This may cause warnings
19970 // to be emitted multiple times.
19971 // 2a. The expression is invalid and we are in an instantiated template or
19972 // not in a template:
19973 // Invalidate the expression with a clearly wrong value (nullptr) so
19974 // later in Sema we do not have to do the same validity analysis again
19975 // or crash from unexpected data. Error diagnostics have already been
19976 // emitted.
19977 // 2b. The expression is invalid and we are in a template (including partial
19978 // instantiation):
19979 // Pass the invalid expression as-is, template instantiation may
19980 // replace unexpected types/values with valid ones. The directives
19981 // with this clause must not try to use these expressions in dependent
19982 // contexts, but delay analysis until full instantiation.
19983 if (!SizeExpr->isInstantiationDependent() && !IsValid)
19984 SizeExpr = nullptr;
19985 }
19986
19987 return OMPSizesClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
19988 Sizes: SanitizedSizeExprs);
19989}
19990
19991OMPClause *SemaOpenMP::ActOnOpenMPCountsClause(ArrayRef<Expr *> CountExprs,
19992 SourceLocation StartLoc,
19993 SourceLocation LParenLoc,
19994 SourceLocation EndLoc,
19995 std::optional<unsigned> FillIdx,
19996 SourceLocation FillLoc,
19997 unsigned FillCount) {
19998 SmallVector<Expr *> SanitizedCountExprs(CountExprs);
19999
20000 // OpenMP 6.0: each list item in counts(...) is either the omp_fill keyword
20001 // or an integral constant expression (non-negative). Runtime variables are
20002 // not permitted; this matches split codegen, which needs segment sizes at
20003 // compile time.
20004 for (unsigned I = 0; I < SanitizedCountExprs.size(); ++I) {
20005 Expr *&CountExpr = SanitizedCountExprs[I];
20006 if (FillIdx && I == *FillIdx)
20007 continue;
20008 if (!CountExpr)
20009 continue;
20010
20011 ExprResult Verified = VerifyPositiveIntegerConstantInClause(
20012 E: CountExpr, CKind: OMPC_counts, /*StrictlyPositive=*/false);
20013 if (Verified.isInvalid())
20014 CountExpr = nullptr;
20015 else
20016 CountExpr = Verified.get();
20017 }
20018
20019 if (FillCount != 1) {
20020 Diag(Loc: FillCount == 0 ? StartLoc : FillLoc,
20021 DiagID: diag::err_omp_split_counts_not_one_omp_fill);
20022 }
20023
20024 return OMPCountsClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
20025 Counts: SanitizedCountExprs, FillIdx, FillLoc);
20026}
20027
20028OMPClause *SemaOpenMP::ActOnOpenMPPermutationClause(ArrayRef<Expr *> PermExprs,
20029 SourceLocation StartLoc,
20030 SourceLocation LParenLoc,
20031 SourceLocation EndLoc) {
20032 size_t NumLoops = PermExprs.size();
20033 SmallVector<Expr *> SanitizedPermExprs;
20034 llvm::append_range(C&: SanitizedPermExprs, R&: PermExprs);
20035
20036 for (Expr *&PermExpr : SanitizedPermExprs) {
20037 // Skip if template-dependent or already sanitized, e.g. during a partial
20038 // template instantiation.
20039 if (!PermExpr || PermExpr->isInstantiationDependent())
20040 continue;
20041
20042 llvm::APSInt PermVal;
20043 ExprResult PermEvalExpr = SemaRef.VerifyIntegerConstantExpression(
20044 E: PermExpr, Result: &PermVal, CanFold: AllowFoldKind::Allow);
20045 bool IsValid = PermEvalExpr.isUsable();
20046 if (IsValid)
20047 PermExpr = PermEvalExpr.get();
20048
20049 if (IsValid && (PermVal < 1 || NumLoops < PermVal)) {
20050 SourceRange ExprRange(PermEvalExpr.get()->getBeginLoc(),
20051 PermEvalExpr.get()->getEndLoc());
20052 Diag(Loc: PermEvalExpr.get()->getExprLoc(),
20053 DiagID: diag::err_omp_interchange_permutation_value_range)
20054 << NumLoops << ExprRange;
20055 IsValid = false;
20056 }
20057
20058 if (!PermExpr->isInstantiationDependent() && !IsValid)
20059 PermExpr = nullptr;
20060 }
20061
20062 return OMPPermutationClause::Create(C: getASTContext(), StartLoc, LParenLoc,
20063 EndLoc, Args: SanitizedPermExprs);
20064}
20065
20066OMPClause *SemaOpenMP::ActOnOpenMPFullClause(SourceLocation StartLoc,
20067 SourceLocation EndLoc) {
20068 return new (getASTContext()) OMPFullClause(StartLoc, EndLoc);
20069}
20070
20071OMPClause *SemaOpenMP::ActOnOpenMPPartialClause(Expr *FactorExpr,
20072 SourceLocation StartLoc,
20073 SourceLocation LParenLoc,
20074 SourceLocation EndLoc) {
20075 if (FactorExpr) {
20076 // If an argument is specified, it must be a constant (or an unevaluated
20077 // template expression).
20078 ExprResult FactorResult = VerifyPositiveIntegerConstantInClause(
20079 E: FactorExpr, CKind: OMPC_partial, /*StrictlyPositive=*/true);
20080 if (FactorResult.isInvalid())
20081 return nullptr;
20082 FactorExpr = FactorResult.get();
20083 }
20084
20085 return new (getASTContext())
20086 OMPPartialClause(StartLoc, LParenLoc, EndLoc, FactorExpr);
20087}
20088
20089OMPClause *SemaOpenMP::ActOnOpenMPDepthClause(Expr *DepthExpr,
20090 SourceLocation StartLoc,
20091 SourceLocation LParenLoc,
20092 SourceLocation EndLoc) {
20093 // The depth-expr must be a positive integer constant expression and
20094 // not greater than the number of loops in the associated loop nest.
20095 ExprResult DepthResult = VerifyPositiveIntegerConstantInClause(
20096 E: DepthExpr, CKind: OMPC_depth, /*StrictlyPositive=*/true);
20097 if (DepthResult.isInvalid())
20098 return nullptr;
20099 DepthExpr = DepthResult.get();
20100
20101 return new (getASTContext())
20102 OMPDepthClause(StartLoc, LParenLoc, EndLoc, DepthExpr);
20103}
20104
20105OMPClause *SemaOpenMP::ActOnOpenMPLoopRangeClause(
20106 Expr *First, Expr *Count, SourceLocation StartLoc, SourceLocation LParenLoc,
20107 SourceLocation FirstLoc, SourceLocation CountLoc, SourceLocation EndLoc) {
20108
20109 // OpenMP [6.0, Restrictions]
20110 // First and Count must be integer expressions with positive value
20111 ExprResult FirstVal =
20112 VerifyPositiveIntegerConstantInClause(E: First, CKind: OMPC_looprange);
20113 if (FirstVal.isInvalid())
20114 First = nullptr;
20115
20116 ExprResult CountVal =
20117 VerifyPositiveIntegerConstantInClause(E: Count, CKind: OMPC_looprange);
20118 if (CountVal.isInvalid())
20119 Count = nullptr;
20120
20121 // OpenMP [6.0, Restrictions]
20122 // first + count - 1 must not evaluate to a value greater than the
20123 // loop sequence length of the associated canonical loop sequence.
20124 // This check must be performed afterwards due to the delayed
20125 // parsing and computation of the associated loop sequence
20126 return new (getASTContext()) OMPLoopRangeClause(
20127 StartLoc, LParenLoc, FirstLoc, CountLoc, EndLoc, First, Count);
20128}
20129
20130OMPClause *SemaOpenMP::ActOnOpenMPAlignClause(Expr *A, SourceLocation StartLoc,
20131 SourceLocation LParenLoc,
20132 SourceLocation EndLoc) {
20133 ExprResult AlignVal;
20134 AlignVal = VerifyPositiveIntegerConstantInClause(E: A, CKind: OMPC_align);
20135 if (AlignVal.isInvalid())
20136 return nullptr;
20137 return new (getASTContext())
20138 OMPAlignClause(AlignVal.get(), StartLoc, LParenLoc, EndLoc);
20139}
20140
20141OMPClause *SemaOpenMP::ActOnOpenMPSingleExprWithArgClause(
20142 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
20143 SourceLocation StartLoc, SourceLocation LParenLoc,
20144 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
20145 SourceLocation EndLoc) {
20146 OMPClause *Res = nullptr;
20147 switch (Kind) {
20148 case OMPC_schedule: {
20149 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
20150 assert(Argument.size() == NumberOfElements &&
20151 ArgumentLoc.size() == NumberOfElements);
20152 Res = ActOnOpenMPScheduleClause(
20153 M1: static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
20154 M2: static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
20155 Kind: static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), ChunkSize: Expr,
20156 StartLoc, LParenLoc, M1Loc: ArgumentLoc[Modifier1], M2Loc: ArgumentLoc[Modifier2],
20157 KindLoc: ArgumentLoc[ScheduleKind], CommaLoc: DelimLoc, EndLoc);
20158 break;
20159 }
20160 case OMPC_if:
20161 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
20162 Res = ActOnOpenMPIfClause(NameModifier: static_cast<OpenMPDirectiveKind>(Argument.back()),
20163 Condition: Expr, StartLoc, LParenLoc, NameModifierLoc: ArgumentLoc.back(),
20164 ColonLoc: DelimLoc, EndLoc);
20165 break;
20166 case OMPC_dist_schedule:
20167 Res = ActOnOpenMPDistScheduleClause(
20168 Kind: static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), ChunkSize: Expr,
20169 StartLoc, LParenLoc, KindLoc: ArgumentLoc.back(), CommaLoc: DelimLoc, EndLoc);
20170 break;
20171 case OMPC_default:
20172 enum { DefaultModifier, DefaultVarCategory };
20173 Res = ActOnOpenMPDefaultClause(
20174 M: static_cast<llvm::omp::DefaultKind>(Argument[DefaultModifier]),
20175 MLoc: ArgumentLoc[DefaultModifier],
20176 VCKind: static_cast<OpenMPDefaultClauseVariableCategory>(
20177 Argument[DefaultVarCategory]),
20178 VCKindLoc: ArgumentLoc[DefaultVarCategory], StartLoc, LParenLoc, EndLoc);
20179 break;
20180 case OMPC_defaultmap:
20181 enum { Modifier, DefaultmapKind };
20182 Res = ActOnOpenMPDefaultmapClause(
20183 M: static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
20184 Kind: static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
20185 StartLoc, LParenLoc, MLoc: ArgumentLoc[Modifier], KindLoc: ArgumentLoc[DefaultmapKind],
20186 EndLoc);
20187 break;
20188 case OMPC_order:
20189 enum { OrderModifier, OrderKind };
20190 Res = ActOnOpenMPOrderClause(
20191 Modifier: static_cast<OpenMPOrderClauseModifier>(Argument[OrderModifier]),
20192 Kind: static_cast<OpenMPOrderClauseKind>(Argument[OrderKind]), StartLoc,
20193 LParenLoc, MLoc: ArgumentLoc[OrderModifier], KindLoc: ArgumentLoc[OrderKind], EndLoc);
20194 break;
20195 case OMPC_device:
20196 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
20197 Res = ActOnOpenMPDeviceClause(
20198 Modifier: static_cast<OpenMPDeviceClauseModifier>(Argument.back()), Device: Expr,
20199 StartLoc, LParenLoc, ModifierLoc: ArgumentLoc.back(), EndLoc);
20200 break;
20201 case OMPC_grainsize:
20202 assert(Argument.size() == 1 && ArgumentLoc.size() == 1 &&
20203 "Modifier for grainsize clause and its location are expected.");
20204 Res = ActOnOpenMPGrainsizeClause(
20205 Modifier: static_cast<OpenMPGrainsizeClauseModifier>(Argument.back()), Size: Expr,
20206 StartLoc, LParenLoc, ModifierLoc: ArgumentLoc.back(), EndLoc);
20207 break;
20208 case OMPC_num_tasks:
20209 assert(Argument.size() == 1 && ArgumentLoc.size() == 1 &&
20210 "Modifier for num_tasks clause and its location are expected.");
20211 Res = ActOnOpenMPNumTasksClause(
20212 Modifier: static_cast<OpenMPNumTasksClauseModifier>(Argument.back()), NumTasks: Expr,
20213 StartLoc, LParenLoc, ModifierLoc: ArgumentLoc.back(), EndLoc);
20214 break;
20215 case OMPC_dyn_groupprivate: {
20216 enum { Modifier1, Modifier2, NumberOfElements };
20217 assert(Argument.size() == NumberOfElements &&
20218 ArgumentLoc.size() == NumberOfElements &&
20219 "Modifiers for dyn_groupprivate clause and their locations are "
20220 "expected.");
20221 Res = ActOnOpenMPDynGroupprivateClause(
20222 M1: static_cast<OpenMPDynGroupprivateClauseModifier>(Argument[Modifier1]),
20223 M2: static_cast<OpenMPDynGroupprivateClauseFallbackModifier>(
20224 Argument[Modifier2]),
20225 Size: Expr, StartLoc, LParenLoc, M1Loc: ArgumentLoc[Modifier1],
20226 M2Loc: ArgumentLoc[Modifier2], EndLoc);
20227 break;
20228 }
20229 case OMPC_final:
20230 case OMPC_safelen:
20231 case OMPC_simdlen:
20232 case OMPC_sizes:
20233 case OMPC_depth:
20234 case OMPC_allocator:
20235 case OMPC_collapse:
20236 case OMPC_proc_bind:
20237 case OMPC_private:
20238 case OMPC_firstprivate:
20239 case OMPC_lastprivate:
20240 case OMPC_shared:
20241 case OMPC_reduction:
20242 case OMPC_task_reduction:
20243 case OMPC_in_reduction:
20244 case OMPC_linear:
20245 case OMPC_aligned:
20246 case OMPC_copyin:
20247 case OMPC_copyprivate:
20248 case OMPC_ordered:
20249 case OMPC_nowait:
20250 case OMPC_untied:
20251 case OMPC_mergeable:
20252 case OMPC_threadprivate:
20253 case OMPC_groupprivate:
20254 case OMPC_allocate:
20255 case OMPC_flush:
20256 case OMPC_depobj:
20257 case OMPC_read:
20258 case OMPC_write:
20259 case OMPC_update:
20260 case OMPC_capture:
20261 case OMPC_compare:
20262 case OMPC_seq_cst:
20263 case OMPC_acq_rel:
20264 case OMPC_acquire:
20265 case OMPC_release:
20266 case OMPC_relaxed:
20267 case OMPC_depend:
20268 case OMPC_threads:
20269 case OMPC_simd:
20270 case OMPC_map:
20271 case OMPC_num_teams:
20272 case OMPC_thread_limit:
20273 case OMPC_priority:
20274 case OMPC_nogroup:
20275 case OMPC_hint:
20276 case OMPC_unknown:
20277 case OMPC_uniform:
20278 case OMPC_to:
20279 case OMPC_from:
20280 case OMPC_use_device_ptr:
20281 case OMPC_use_device_addr:
20282 case OMPC_is_device_ptr:
20283 case OMPC_has_device_addr:
20284 case OMPC_unified_address:
20285 case OMPC_unified_shared_memory:
20286 case OMPC_reverse_offload:
20287 case OMPC_dynamic_allocators:
20288 case OMPC_atomic_default_mem_order:
20289 case OMPC_self_maps:
20290 case OMPC_device_type:
20291 case OMPC_match:
20292 case OMPC_nontemporal:
20293 case OMPC_at:
20294 case OMPC_severity:
20295 case OMPC_message:
20296 case OMPC_destroy:
20297 case OMPC_novariants:
20298 case OMPC_nocontext:
20299 case OMPC_detach:
20300 case OMPC_inclusive:
20301 case OMPC_exclusive:
20302 case OMPC_uses_allocators:
20303 case OMPC_affinity:
20304 case OMPC_when:
20305 case OMPC_bind:
20306 default:
20307 llvm_unreachable("Clause is not allowed.");
20308 }
20309 return Res;
20310}
20311
20312static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
20313 OpenMPScheduleClauseModifier M2,
20314 SourceLocation M1Loc, SourceLocation M2Loc) {
20315 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
20316 SmallVector<unsigned, 2> Excluded;
20317 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
20318 Excluded.push_back(Elt: M2);
20319 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
20320 Excluded.push_back(Elt: OMPC_SCHEDULE_MODIFIER_monotonic);
20321 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
20322 Excluded.push_back(Elt: OMPC_SCHEDULE_MODIFIER_nonmonotonic);
20323 S.Diag(Loc: M1Loc, DiagID: diag::err_omp_unexpected_clause_value)
20324 << getListOfPossibleValues(K: OMPC_schedule,
20325 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
20326 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
20327 Exclude: Excluded)
20328 << getOpenMPClauseNameForDiag(C: OMPC_schedule);
20329 return true;
20330 }
20331 return false;
20332}
20333
20334OMPClause *SemaOpenMP::ActOnOpenMPScheduleClause(
20335 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
20336 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
20337 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
20338 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
20339 if (checkScheduleModifiers(S&: SemaRef, M1, M2, M1Loc, M2Loc) ||
20340 checkScheduleModifiers(S&: SemaRef, M1: M2, M2: M1, M1Loc: M2Loc, M2Loc: M1Loc))
20341 return nullptr;
20342 // OpenMP, 2.7.1, Loop Construct, Restrictions
20343 // Either the monotonic modifier or the nonmonotonic modifier can be specified
20344 // but not both.
20345 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
20346 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
20347 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
20348 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
20349 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
20350 Diag(Loc: M2Loc, DiagID: diag::err_omp_unexpected_schedule_modifier)
20351 << getOpenMPSimpleClauseTypeName(Kind: OMPC_schedule, Type: M2)
20352 << getOpenMPSimpleClauseTypeName(Kind: OMPC_schedule, Type: M1);
20353 return nullptr;
20354 }
20355 if (Kind == OMPC_SCHEDULE_unknown) {
20356 std::string Values;
20357 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
20358 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
20359 Values = getListOfPossibleValues(K: OMPC_schedule, /*First=*/0,
20360 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
20361 Exclude);
20362 } else {
20363 Values = getListOfPossibleValues(K: OMPC_schedule, /*First=*/0,
20364 /*Last=*/OMPC_SCHEDULE_unknown);
20365 }
20366 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
20367 << Values << getOpenMPClauseNameForDiag(C: OMPC_schedule);
20368 return nullptr;
20369 }
20370 // OpenMP, 2.7.1, Loop Construct, Restrictions
20371 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
20372 // schedule(guided).
20373 // OpenMP 5.0 does not have this restriction.
20374 if (getLangOpts().OpenMP < 50 &&
20375 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
20376 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
20377 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
20378 Diag(Loc: M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
20379 DiagID: diag::err_omp_schedule_nonmonotonic_static);
20380 return nullptr;
20381 }
20382 Expr *ValExpr = ChunkSize;
20383 Stmt *HelperValStmt = nullptr;
20384 if (ChunkSize) {
20385 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
20386 !ChunkSize->isInstantiationDependent() &&
20387 !ChunkSize->containsUnexpandedParameterPack()) {
20388 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
20389 ExprResult Val =
20390 PerformOpenMPImplicitIntegerConversion(Loc: ChunkSizeLoc, Op: ChunkSize);
20391 if (Val.isInvalid())
20392 return nullptr;
20393
20394 ValExpr = Val.get();
20395
20396 // OpenMP [2.7.1, Restrictions]
20397 // chunk_size must be a loop invariant integer expression with a positive
20398 // value.
20399 if (std::optional<llvm::APSInt> Result =
20400 ValExpr->getIntegerConstantExpr(Ctx: getASTContext())) {
20401 if (Result->isSigned() && !Result->isStrictlyPositive()) {
20402 Diag(Loc: ChunkSizeLoc, DiagID: diag::err_omp_negative_expression_in_clause)
20403 << "schedule" << 1 << ChunkSize->getSourceRange();
20404 return nullptr;
20405 }
20406 } else if (getOpenMPCaptureRegionForClause(
20407 DSAStack->getCurrentDirective(), CKind: OMPC_schedule,
20408 OMPVersion: getLangOpts().getOpenMPVersion()) != OMPD_unknown &&
20409 !SemaRef.CurContext->isDependentContext()) {
20410 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
20411 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
20412 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
20413 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
20414 }
20415 }
20416 }
20417
20418 return new (getASTContext())
20419 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
20420 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
20421}
20422
20423OMPClause *SemaOpenMP::ActOnOpenMPClause(OpenMPClauseKind Kind,
20424 SourceLocation StartLoc,
20425 SourceLocation EndLoc) {
20426 OMPClause *Res = nullptr;
20427 switch (Kind) {
20428 case OMPC_ordered:
20429 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
20430 break;
20431 case OMPC_nowait:
20432 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc,
20433 /*LParenLoc=*/SourceLocation(),
20434 /*Condition=*/nullptr);
20435 break;
20436 case OMPC_untied:
20437 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
20438 break;
20439 case OMPC_mergeable:
20440 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
20441 break;
20442 case OMPC_read:
20443 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
20444 break;
20445 case OMPC_write:
20446 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
20447 break;
20448 case OMPC_update:
20449 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
20450 break;
20451 case OMPC_capture:
20452 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
20453 break;
20454 case OMPC_compare:
20455 Res = ActOnOpenMPCompareClause(StartLoc, EndLoc);
20456 break;
20457 case OMPC_fail:
20458 Res = ActOnOpenMPFailClause(StartLoc, EndLoc);
20459 break;
20460 case OMPC_seq_cst:
20461 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
20462 break;
20463 case OMPC_acq_rel:
20464 Res = ActOnOpenMPAcqRelClause(StartLoc, EndLoc);
20465 break;
20466 case OMPC_acquire:
20467 Res = ActOnOpenMPAcquireClause(StartLoc, EndLoc);
20468 break;
20469 case OMPC_release:
20470 Res = ActOnOpenMPReleaseClause(StartLoc, EndLoc);
20471 break;
20472 case OMPC_relaxed:
20473 Res = ActOnOpenMPRelaxedClause(StartLoc, EndLoc);
20474 break;
20475 case OMPC_weak:
20476 Res = ActOnOpenMPWeakClause(StartLoc, EndLoc);
20477 break;
20478 case OMPC_threads:
20479 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
20480 break;
20481 case OMPC_simd:
20482 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
20483 break;
20484 case OMPC_nogroup:
20485 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
20486 break;
20487 case OMPC_unified_address:
20488 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
20489 break;
20490 case OMPC_unified_shared_memory:
20491 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
20492 break;
20493 case OMPC_reverse_offload:
20494 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc);
20495 break;
20496 case OMPC_dynamic_allocators:
20497 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
20498 break;
20499 case OMPC_self_maps:
20500 Res = ActOnOpenMPSelfMapsClause(StartLoc, EndLoc);
20501 break;
20502 case OMPC_destroy:
20503 Res = ActOnOpenMPDestroyClause(/*InteropVar=*/nullptr, StartLoc,
20504 /*LParenLoc=*/SourceLocation(),
20505 /*VarLoc=*/SourceLocation(), EndLoc);
20506 break;
20507 case OMPC_full:
20508 Res = ActOnOpenMPFullClause(StartLoc, EndLoc);
20509 break;
20510 case OMPC_partial:
20511 Res = ActOnOpenMPPartialClause(FactorExpr: nullptr, StartLoc, /*LParenLoc=*/{}, EndLoc);
20512 break;
20513 case OMPC_ompx_bare:
20514 Res = ActOnOpenMPXBareClause(StartLoc, EndLoc);
20515 break;
20516 case OMPC_if:
20517 case OMPC_final:
20518 case OMPC_num_threads:
20519 case OMPC_safelen:
20520 case OMPC_simdlen:
20521 case OMPC_sizes:
20522 case OMPC_depth:
20523 case OMPC_allocator:
20524 case OMPC_collapse:
20525 case OMPC_schedule:
20526 case OMPC_private:
20527 case OMPC_firstprivate:
20528 case OMPC_lastprivate:
20529 case OMPC_shared:
20530 case OMPC_reduction:
20531 case OMPC_task_reduction:
20532 case OMPC_in_reduction:
20533 case OMPC_linear:
20534 case OMPC_aligned:
20535 case OMPC_copyin:
20536 case OMPC_copyprivate:
20537 case OMPC_default:
20538 case OMPC_proc_bind:
20539 case OMPC_threadprivate:
20540 case OMPC_groupprivate:
20541 case OMPC_allocate:
20542 case OMPC_flush:
20543 case OMPC_depobj:
20544 case OMPC_depend:
20545 case OMPC_device:
20546 case OMPC_map:
20547 case OMPC_num_teams:
20548 case OMPC_thread_limit:
20549 case OMPC_priority:
20550 case OMPC_grainsize:
20551 case OMPC_num_tasks:
20552 case OMPC_hint:
20553 case OMPC_dist_schedule:
20554 case OMPC_defaultmap:
20555 case OMPC_unknown:
20556 case OMPC_uniform:
20557 case OMPC_to:
20558 case OMPC_from:
20559 case OMPC_use_device_ptr:
20560 case OMPC_use_device_addr:
20561 case OMPC_is_device_ptr:
20562 case OMPC_has_device_addr:
20563 case OMPC_atomic_default_mem_order:
20564 case OMPC_device_type:
20565 case OMPC_match:
20566 case OMPC_nontemporal:
20567 case OMPC_order:
20568 case OMPC_at:
20569 case OMPC_severity:
20570 case OMPC_message:
20571 case OMPC_novariants:
20572 case OMPC_nocontext:
20573 case OMPC_detach:
20574 case OMPC_inclusive:
20575 case OMPC_exclusive:
20576 case OMPC_uses_allocators:
20577 case OMPC_affinity:
20578 case OMPC_when:
20579 case OMPC_ompx_dyn_cgroup_mem:
20580 case OMPC_dyn_groupprivate:
20581 default:
20582 llvm_unreachable("Clause is not allowed.");
20583 }
20584 return Res;
20585}
20586
20587OMPClause *SemaOpenMP::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
20588 SourceLocation EndLoc,
20589 SourceLocation LParenLoc,
20590 Expr *Condition) {
20591 Expr *ValExpr = Condition;
20592 if (Condition && LParenLoc.isValid()) {
20593 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
20594 !Condition->isInstantiationDependent() &&
20595 !Condition->containsUnexpandedParameterPack()) {
20596 ExprResult Val = SemaRef.CheckBooleanCondition(Loc: StartLoc, E: Condition);
20597 if (Val.isInvalid())
20598 return nullptr;
20599
20600 ValExpr = Val.get();
20601 }
20602 }
20603 DSAStack->setNowaitRegion();
20604 return new (getASTContext())
20605 OMPNowaitClause(ValExpr, StartLoc, LParenLoc, EndLoc);
20606}
20607
20608OMPClause *SemaOpenMP::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
20609 SourceLocation EndLoc) {
20610 DSAStack->setUntiedRegion();
20611 return new (getASTContext()) OMPUntiedClause(StartLoc, EndLoc);
20612}
20613
20614OMPClause *SemaOpenMP::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
20615 SourceLocation EndLoc) {
20616 return new (getASTContext()) OMPMergeableClause(StartLoc, EndLoc);
20617}
20618
20619OMPClause *SemaOpenMP::ActOnOpenMPReadClause(SourceLocation StartLoc,
20620 SourceLocation EndLoc) {
20621 return new (getASTContext()) OMPReadClause(StartLoc, EndLoc);
20622}
20623
20624OMPClause *SemaOpenMP::ActOnOpenMPWriteClause(SourceLocation StartLoc,
20625 SourceLocation EndLoc) {
20626 return new (getASTContext()) OMPWriteClause(StartLoc, EndLoc);
20627}
20628
20629OMPClause *SemaOpenMP::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
20630 SourceLocation EndLoc) {
20631 return new (getASTContext()) OMPUpdateClause(StartLoc, EndLoc);
20632}
20633
20634OMPClause *SemaOpenMP::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
20635 SourceLocation EndLoc) {
20636 return new (getASTContext()) OMPCaptureClause(StartLoc, EndLoc);
20637}
20638
20639OMPClause *SemaOpenMP::ActOnOpenMPCompareClause(SourceLocation StartLoc,
20640 SourceLocation EndLoc) {
20641 return new (getASTContext()) OMPCompareClause(StartLoc, EndLoc);
20642}
20643
20644OMPClause *SemaOpenMP::ActOnOpenMPFailClause(SourceLocation StartLoc,
20645 SourceLocation EndLoc) {
20646 return new (getASTContext()) OMPFailClause(StartLoc, EndLoc);
20647}
20648
20649OMPClause *SemaOpenMP::ActOnOpenMPFailClause(OpenMPClauseKind Parameter,
20650 SourceLocation KindLoc,
20651 SourceLocation StartLoc,
20652 SourceLocation LParenLoc,
20653 SourceLocation EndLoc) {
20654
20655 if (!checkFailClauseParameter(FailClauseParameter: Parameter)) {
20656 Diag(Loc: KindLoc, DiagID: diag::err_omp_atomic_fail_wrong_or_no_clauses);
20657 return nullptr;
20658 }
20659 return new (getASTContext())
20660 OMPFailClause(Parameter, KindLoc, StartLoc, LParenLoc, EndLoc);
20661}
20662
20663OMPClause *SemaOpenMP::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
20664 SourceLocation EndLoc) {
20665 return new (getASTContext()) OMPSeqCstClause(StartLoc, EndLoc);
20666}
20667
20668OMPClause *SemaOpenMP::ActOnOpenMPAcqRelClause(SourceLocation StartLoc,
20669 SourceLocation EndLoc) {
20670 return new (getASTContext()) OMPAcqRelClause(StartLoc, EndLoc);
20671}
20672
20673OMPClause *SemaOpenMP::ActOnOpenMPAcquireClause(SourceLocation StartLoc,
20674 SourceLocation EndLoc) {
20675 return new (getASTContext()) OMPAcquireClause(StartLoc, EndLoc);
20676}
20677
20678OMPClause *SemaOpenMP::ActOnOpenMPReleaseClause(SourceLocation StartLoc,
20679 SourceLocation EndLoc) {
20680 return new (getASTContext()) OMPReleaseClause(StartLoc, EndLoc);
20681}
20682
20683OMPClause *SemaOpenMP::ActOnOpenMPRelaxedClause(SourceLocation StartLoc,
20684 SourceLocation EndLoc) {
20685 return new (getASTContext()) OMPRelaxedClause(StartLoc, EndLoc);
20686}
20687
20688OMPClause *SemaOpenMP::ActOnOpenMPWeakClause(SourceLocation StartLoc,
20689 SourceLocation EndLoc) {
20690 return new (getASTContext()) OMPWeakClause(StartLoc, EndLoc);
20691}
20692
20693OMPClause *SemaOpenMP::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
20694 SourceLocation EndLoc) {
20695 return new (getASTContext()) OMPThreadsClause(StartLoc, EndLoc);
20696}
20697
20698OMPClause *SemaOpenMP::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
20699 SourceLocation EndLoc) {
20700 return new (getASTContext()) OMPSIMDClause(StartLoc, EndLoc);
20701}
20702
20703OMPClause *SemaOpenMP::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
20704 SourceLocation EndLoc) {
20705 return new (getASTContext()) OMPNogroupClause(StartLoc, EndLoc);
20706}
20707
20708OMPClause *SemaOpenMP::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
20709 SourceLocation EndLoc) {
20710 return new (getASTContext()) OMPUnifiedAddressClause(StartLoc, EndLoc);
20711}
20712
20713OMPClause *
20714SemaOpenMP::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
20715 SourceLocation EndLoc) {
20716 return new (getASTContext()) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
20717}
20718
20719OMPClause *SemaOpenMP::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc,
20720 SourceLocation EndLoc) {
20721 return new (getASTContext()) OMPReverseOffloadClause(StartLoc, EndLoc);
20722}
20723
20724OMPClause *
20725SemaOpenMP::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
20726 SourceLocation EndLoc) {
20727 return new (getASTContext()) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
20728}
20729
20730OMPClause *SemaOpenMP::ActOnOpenMPSelfMapsClause(SourceLocation StartLoc,
20731 SourceLocation EndLoc) {
20732 return new (getASTContext()) OMPSelfMapsClause(StartLoc, EndLoc);
20733}
20734
20735StmtResult
20736SemaOpenMP::ActOnOpenMPInteropDirective(ArrayRef<OMPClause *> Clauses,
20737 SourceLocation StartLoc,
20738 SourceLocation EndLoc) {
20739
20740 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions]
20741 // At least one action-clause must appear on a directive.
20742 if (!hasClauses(Clauses, K: OMPC_init, ClauseTypes: OMPC_use, ClauseTypes: OMPC_destroy, ClauseTypes: OMPC_nowait)) {
20743 llvm::omp::Version OMPVersion = getLangOpts().getOpenMPVersion();
20744 StringRef Expected = "'init', 'use', 'destroy', or 'nowait'";
20745 Diag(Loc: StartLoc, DiagID: diag::err_omp_no_clause_for_directive)
20746 << Expected << getOpenMPDirectiveName(D: OMPD_interop, V: OMPVersion);
20747 return StmtError();
20748 }
20749
20750 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions]
20751 // A depend clause can only appear on the directive if a targetsync
20752 // interop-type is present or the interop-var was initialized with
20753 // the targetsync interop-type.
20754
20755 // If there is any 'init' clause diagnose if there is no 'init' clause with
20756 // interop-type of 'targetsync'. Cases involving other directives cannot be
20757 // diagnosed.
20758 const OMPDependClause *DependClause = nullptr;
20759 bool HasInitClause = false;
20760 bool IsTargetSync = false;
20761 for (const OMPClause *C : Clauses) {
20762 if (IsTargetSync)
20763 break;
20764 if (const auto *InitClause = dyn_cast<OMPInitClause>(Val: C)) {
20765 HasInitClause = true;
20766 if (InitClause->getIsTargetSync())
20767 IsTargetSync = true;
20768 } else if (const auto *DC = dyn_cast<OMPDependClause>(Val: C)) {
20769 DependClause = DC;
20770 }
20771 }
20772 if (DependClause && HasInitClause && !IsTargetSync) {
20773 Diag(Loc: DependClause->getBeginLoc(), DiagID: diag::err_omp_interop_bad_depend_clause);
20774 return StmtError();
20775 }
20776
20777 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions]
20778 // Each interop-var may be specified for at most one action-clause of each
20779 // interop construct.
20780 llvm::SmallPtrSet<const ValueDecl *, 4> InteropVars;
20781 for (OMPClause *C : Clauses) {
20782 OpenMPClauseKind ClauseKind = C->getClauseKind();
20783 std::pair<ValueDecl *, bool> DeclResult;
20784 SourceLocation ELoc;
20785 SourceRange ERange;
20786
20787 if (ClauseKind == OMPC_init) {
20788 auto *E = cast<OMPInitClause>(Val: C)->getInteropVar();
20789 DeclResult = getPrivateItem(S&: SemaRef, RefExpr&: E, ELoc, ERange);
20790 } else if (ClauseKind == OMPC_use) {
20791 auto *E = cast<OMPUseClause>(Val: C)->getInteropVar();
20792 DeclResult = getPrivateItem(S&: SemaRef, RefExpr&: E, ELoc, ERange);
20793 } else if (ClauseKind == OMPC_destroy) {
20794 auto *E = cast<OMPDestroyClause>(Val: C)->getInteropVar();
20795 DeclResult = getPrivateItem(S&: SemaRef, RefExpr&: E, ELoc, ERange);
20796 }
20797
20798 if (DeclResult.first) {
20799 if (!InteropVars.insert(Ptr: DeclResult.first).second) {
20800 Diag(Loc: ELoc, DiagID: diag::err_omp_interop_var_multiple_actions)
20801 << DeclResult.first;
20802 return StmtError();
20803 }
20804 }
20805 }
20806
20807 return OMPInteropDirective::Create(C: getASTContext(), StartLoc, EndLoc,
20808 Clauses);
20809}
20810
20811static bool isValidInteropVariable(Sema &SemaRef, Expr *InteropVarExpr,
20812 SourceLocation VarLoc,
20813 OpenMPClauseKind Kind) {
20814 SourceLocation ELoc;
20815 SourceRange ERange;
20816 Expr *RefExpr = InteropVarExpr;
20817 auto Res = getPrivateItem(S&: SemaRef, RefExpr, ELoc, ERange,
20818 /*AllowArraySection=*/false,
20819 /*AllowAssumedSizeArray=*/false,
20820 /*DiagType=*/"omp_interop_t");
20821
20822 if (Res.second) {
20823 // It will be analyzed later.
20824 return true;
20825 }
20826
20827 if (!Res.first)
20828 return false;
20829
20830 // Interop variable should be of type omp_interop_t.
20831 bool HasError = false;
20832 QualType InteropType;
20833 LookupResult Result(SemaRef, &SemaRef.Context.Idents.get(Name: "omp_interop_t"),
20834 VarLoc, Sema::LookupOrdinaryName);
20835 if (SemaRef.LookupName(R&: Result, S: SemaRef.getCurScope())) {
20836 NamedDecl *ND = Result.getFoundDecl();
20837 if (const auto *TD = dyn_cast<TypeDecl>(Val: ND)) {
20838 InteropType = QualType(TD->getTypeForDecl(), 0);
20839 } else {
20840 HasError = true;
20841 }
20842 } else {
20843 HasError = true;
20844 }
20845
20846 if (HasError) {
20847 SemaRef.Diag(Loc: VarLoc, DiagID: diag::err_omp_implied_type_not_found)
20848 << "omp_interop_t";
20849 return false;
20850 }
20851
20852 QualType VarType = InteropVarExpr->getType().getUnqualifiedType();
20853 if (!SemaRef.Context.hasSameType(T1: InteropType, T2: VarType)) {
20854 SemaRef.Diag(Loc: VarLoc, DiagID: diag::err_omp_interop_variable_wrong_type);
20855 return false;
20856 }
20857
20858 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions]
20859 // The interop-var passed to init or destroy must be non-const.
20860 if ((Kind == OMPC_init || Kind == OMPC_destroy) &&
20861 isConstNotMutableType(SemaRef, Type: InteropVarExpr->getType())) {
20862 SemaRef.Diag(Loc: VarLoc, DiagID: diag::err_omp_interop_variable_expected)
20863 << /*non-const*/ 1;
20864 return false;
20865 }
20866 return true;
20867}
20868
20869OMPClause *SemaOpenMP::ActOnOpenMPInitClause(
20870 Expr *InteropVar, OMPInteropInfo &InteropInfo, SourceLocation StartLoc,
20871 SourceLocation LParenLoc, SourceLocation VarLoc, SourceLocation EndLoc) {
20872
20873 if (!isValidInteropVariable(SemaRef, InteropVarExpr: InteropVar, VarLoc, Kind: OMPC_init))
20874 return nullptr;
20875
20876 if (!checkPreferTypeArgs(S&: *this, Info: InteropInfo))
20877 return nullptr;
20878
20879 return OMPInitClause::Create(C: getASTContext(), InteropVar, InteropInfo,
20880 StartLoc, LParenLoc, VarLoc, EndLoc);
20881}
20882
20883OMPClause *SemaOpenMP::ActOnOpenMPUseClause(Expr *InteropVar,
20884 SourceLocation StartLoc,
20885 SourceLocation LParenLoc,
20886 SourceLocation VarLoc,
20887 SourceLocation EndLoc) {
20888
20889 if (!isValidInteropVariable(SemaRef, InteropVarExpr: InteropVar, VarLoc, Kind: OMPC_use))
20890 return nullptr;
20891
20892 return new (getASTContext())
20893 OMPUseClause(InteropVar, StartLoc, LParenLoc, VarLoc, EndLoc);
20894}
20895
20896OMPClause *SemaOpenMP::ActOnOpenMPDestroyClause(Expr *InteropVar,
20897 SourceLocation StartLoc,
20898 SourceLocation LParenLoc,
20899 SourceLocation VarLoc,
20900 SourceLocation EndLoc) {
20901 if (!InteropVar && getLangOpts().OpenMP >= 52 &&
20902 DSAStack->getCurrentDirective() == OMPD_depobj) {
20903 llvm::omp::Version OMPVersion = getLangOpts().getOpenMPVersion();
20904 Diag(Loc: StartLoc, DiagID: diag::err_omp_expected_clause_argument)
20905 << getOpenMPClauseNameForDiag(C: OMPC_destroy)
20906 << getOpenMPDirectiveName(D: OMPD_depobj, V: OMPVersion);
20907 return nullptr;
20908 }
20909 if (InteropVar &&
20910 !isValidInteropVariable(SemaRef, InteropVarExpr: InteropVar, VarLoc, Kind: OMPC_destroy))
20911 return nullptr;
20912
20913 return new (getASTContext())
20914 OMPDestroyClause(InteropVar, StartLoc, LParenLoc, VarLoc, EndLoc);
20915}
20916
20917OMPClause *SemaOpenMP::ActOnOpenMPNovariantsClause(Expr *Condition,
20918 SourceLocation StartLoc,
20919 SourceLocation LParenLoc,
20920 SourceLocation EndLoc) {
20921 Expr *ValExpr = Condition;
20922 Stmt *HelperValStmt = nullptr;
20923 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
20924 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
20925 !Condition->isInstantiationDependent() &&
20926 !Condition->containsUnexpandedParameterPack()) {
20927 ExprResult Val = SemaRef.CheckBooleanCondition(Loc: StartLoc, E: Condition);
20928 if (Val.isInvalid())
20929 return nullptr;
20930
20931 ValExpr = SemaRef.MakeFullExpr(Arg: Val.get()).get();
20932
20933 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
20934 CaptureRegion = getOpenMPCaptureRegionForClause(
20935 DKind, CKind: OMPC_novariants, OMPVersion: getLangOpts().getOpenMPVersion());
20936 if (CaptureRegion != OMPD_unknown &&
20937 !SemaRef.CurContext->isDependentContext()) {
20938 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
20939 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
20940 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
20941 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
20942 }
20943 }
20944
20945 return new (getASTContext()) OMPNovariantsClause(
20946 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
20947}
20948
20949OMPClause *SemaOpenMP::ActOnOpenMPNocontextClause(Expr *Condition,
20950 SourceLocation StartLoc,
20951 SourceLocation LParenLoc,
20952 SourceLocation EndLoc) {
20953 Expr *ValExpr = Condition;
20954 Stmt *HelperValStmt = nullptr;
20955 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
20956 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
20957 !Condition->isInstantiationDependent() &&
20958 !Condition->containsUnexpandedParameterPack()) {
20959 ExprResult Val = SemaRef.CheckBooleanCondition(Loc: StartLoc, E: Condition);
20960 if (Val.isInvalid())
20961 return nullptr;
20962
20963 ValExpr = SemaRef.MakeFullExpr(Arg: Val.get()).get();
20964
20965 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
20966 CaptureRegion = getOpenMPCaptureRegionForClause(
20967 DKind, CKind: OMPC_nocontext, OMPVersion: getLangOpts().getOpenMPVersion());
20968 if (CaptureRegion != OMPD_unknown &&
20969 !SemaRef.CurContext->isDependentContext()) {
20970 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
20971 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
20972 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
20973 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
20974 }
20975 }
20976
20977 return new (getASTContext()) OMPNocontextClause(
20978 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
20979}
20980
20981OMPClause *SemaOpenMP::ActOnOpenMPFilterClause(Expr *ThreadID,
20982 SourceLocation StartLoc,
20983 SourceLocation LParenLoc,
20984 SourceLocation EndLoc) {
20985 Expr *ValExpr = ThreadID;
20986 Stmt *HelperValStmt = nullptr;
20987
20988 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
20989 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
20990 DKind, CKind: OMPC_filter, OMPVersion: getLangOpts().getOpenMPVersion());
20991 if (CaptureRegion != OMPD_unknown &&
20992 !SemaRef.CurContext->isDependentContext()) {
20993 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
20994 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
20995 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
20996 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
20997 }
20998
20999 return new (getASTContext()) OMPFilterClause(
21000 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
21001}
21002
21003OMPClause *SemaOpenMP::ActOnOpenMPVarListClause(OpenMPClauseKind Kind,
21004 ArrayRef<Expr *> VarList,
21005 const OMPVarListLocTy &Locs,
21006 OpenMPVarListDataTy &Data) {
21007 SourceLocation StartLoc = Locs.StartLoc;
21008 SourceLocation LParenLoc = Locs.LParenLoc;
21009 SourceLocation EndLoc = Locs.EndLoc;
21010 OMPClause *Res = nullptr;
21011 int ExtraModifier = Data.ExtraModifier;
21012 int OriginalSharingModifier = Data.OriginalSharingModifier;
21013 Expr *ExtraModifierExpr = Data.ExtraModifierExpr;
21014 SourceLocation ExtraModifierLoc = Data.ExtraModifierLoc;
21015 SourceLocation ColonLoc = Data.ColonLoc;
21016 switch (Kind) {
21017 case OMPC_private:
21018 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
21019 break;
21020 case OMPC_firstprivate:
21021 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
21022 break;
21023 case OMPC_lastprivate:
21024 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LASTPRIVATE_unknown &&
21025 "Unexpected lastprivate modifier.");
21026 Res = ActOnOpenMPLastprivateClause(
21027 VarList, LPKind: static_cast<OpenMPLastprivateModifier>(ExtraModifier),
21028 LPKindLoc: ExtraModifierLoc, ColonLoc, StartLoc, LParenLoc, EndLoc);
21029 break;
21030 case OMPC_shared:
21031 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
21032 break;
21033 case OMPC_reduction:
21034 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_REDUCTION_unknown &&
21035 "Unexpected lastprivate modifier.");
21036 Res = ActOnOpenMPReductionClause(
21037 VarList,
21038 Modifiers: OpenMPVarListDataTy::OpenMPReductionClauseModifiers(
21039 ExtraModifier, OriginalSharingModifier),
21040 StartLoc, LParenLoc, ModifierLoc: ExtraModifierLoc, ColonLoc, EndLoc,
21041 ReductionIdScopeSpec&: Data.ReductionOrMapperIdScopeSpec, ReductionId: Data.ReductionOrMapperId);
21042 break;
21043 case OMPC_task_reduction:
21044 Res = ActOnOpenMPTaskReductionClause(
21045 VarList, StartLoc, LParenLoc, ColonLoc, EndLoc,
21046 ReductionIdScopeSpec&: Data.ReductionOrMapperIdScopeSpec, ReductionId: Data.ReductionOrMapperId);
21047 break;
21048 case OMPC_in_reduction:
21049 Res = ActOnOpenMPInReductionClause(
21050 VarList, StartLoc, LParenLoc, ColonLoc, EndLoc,
21051 ReductionIdScopeSpec&: Data.ReductionOrMapperIdScopeSpec, ReductionId: Data.ReductionOrMapperId);
21052 break;
21053 case OMPC_linear:
21054 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LINEAR_unknown &&
21055 "Unexpected linear modifier.");
21056 Res = ActOnOpenMPLinearClause(
21057 VarList, Step: Data.DepModOrTailExpr, StartLoc, LParenLoc,
21058 LinKind: static_cast<OpenMPLinearClauseKind>(ExtraModifier), LinLoc: ExtraModifierLoc,
21059 ColonLoc, StepModifierLoc: Data.StepModifierLoc, EndLoc);
21060 break;
21061 case OMPC_aligned:
21062 Res = ActOnOpenMPAlignedClause(VarList, Alignment: Data.DepModOrTailExpr, StartLoc,
21063 LParenLoc, ColonLoc, EndLoc);
21064 break;
21065 case OMPC_copyin:
21066 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
21067 break;
21068 case OMPC_copyprivate:
21069 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
21070 break;
21071 case OMPC_flush:
21072 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
21073 break;
21074 case OMPC_depend:
21075 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_DEPEND_unknown &&
21076 "Unexpected depend modifier.");
21077 Res = ActOnOpenMPDependClause(
21078 Data: {.DepKind: static_cast<OpenMPDependClauseKind>(ExtraModifier), .DepLoc: ExtraModifierLoc,
21079 .ColonLoc: ColonLoc, .OmpAllMemoryLoc: Data.OmpAllMemoryLoc},
21080 DepModifier: Data.DepModOrTailExpr, VarList, StartLoc, LParenLoc, EndLoc);
21081 break;
21082 case OMPC_map:
21083 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_MAP_unknown &&
21084 "Unexpected map modifier.");
21085 Res = ActOnOpenMPMapClause(
21086 IteratorModifier: Data.IteratorExpr, MapTypeModifiers: Data.MapTypeModifiers, MapTypeModifiersLoc: Data.MapTypeModifiersLoc,
21087 MapperIdScopeSpec&: Data.ReductionOrMapperIdScopeSpec, MapperId&: Data.ReductionOrMapperId,
21088 MapType: static_cast<OpenMPMapClauseKind>(ExtraModifier), IsMapTypeImplicit: Data.IsMapTypeImplicit,
21089 MapLoc: ExtraModifierLoc, ColonLoc, VarList, Locs);
21090 break;
21091 case OMPC_to:
21092 Res = ActOnOpenMPToClause(
21093 MotionModifiers: Data.MotionModifiers, MotionModifiersLoc: Data.MotionModifiersLoc, IteratorModifier: Data.IteratorExpr,
21094 MapperIdScopeSpec&: Data.ReductionOrMapperIdScopeSpec, MapperId&: Data.ReductionOrMapperId, ColonLoc,
21095 VarList, Locs);
21096 break;
21097 case OMPC_from:
21098 Res = ActOnOpenMPFromClause(
21099 MotionModifiers: Data.MotionModifiers, MotionModifiersLoc: Data.MotionModifiersLoc, IteratorModifier: Data.IteratorExpr,
21100 MapperIdScopeSpec&: Data.ReductionOrMapperIdScopeSpec, MapperId&: Data.ReductionOrMapperId, ColonLoc,
21101 VarList, Locs);
21102 break;
21103 case OMPC_use_device_ptr:
21104 assert(0 <= Data.ExtraModifier &&
21105 Data.ExtraModifier <= OMPC_USE_DEVICE_PTR_FALLBACK_unknown &&
21106 "Unexpected use_device_ptr fallback modifier.");
21107 Res = ActOnOpenMPUseDevicePtrClause(
21108 VarList, Locs,
21109 FallbackModifier: static_cast<OpenMPUseDevicePtrFallbackModifier>(Data.ExtraModifier),
21110 FallbackModifierLoc: Data.ExtraModifierLoc);
21111 break;
21112 case OMPC_use_device_addr:
21113 Res = ActOnOpenMPUseDeviceAddrClause(VarList, Locs);
21114 break;
21115 case OMPC_is_device_ptr:
21116 Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs);
21117 break;
21118 case OMPC_has_device_addr:
21119 Res = ActOnOpenMPHasDeviceAddrClause(VarList, Locs);
21120 break;
21121 case OMPC_allocate: {
21122 OpenMPAllocateClauseModifier Modifier1 = OMPC_ALLOCATE_unknown;
21123 OpenMPAllocateClauseModifier Modifier2 = OMPC_ALLOCATE_unknown;
21124 SourceLocation Modifier1Loc, Modifier2Loc;
21125 if (!Data.AllocClauseModifiers.empty()) {
21126 assert(Data.AllocClauseModifiers.size() <= 2 &&
21127 "More allocate modifiers than expected");
21128 Modifier1 = Data.AllocClauseModifiers[0];
21129 Modifier1Loc = Data.AllocClauseModifiersLoc[0];
21130 if (Data.AllocClauseModifiers.size() == 2) {
21131 Modifier2 = Data.AllocClauseModifiers[1];
21132 Modifier2Loc = Data.AllocClauseModifiersLoc[1];
21133 }
21134 }
21135 Res = ActOnOpenMPAllocateClause(
21136 Allocator: Data.DepModOrTailExpr, Alignment: Data.AllocateAlignment, FirstModifier: Modifier1, FirstModifierLoc: Modifier1Loc,
21137 SecondModifier: Modifier2, SecondModifierLoc: Modifier2Loc, VarList, StartLoc, ColonLoc: LParenLoc, LParenLoc: ColonLoc,
21138 EndLoc);
21139 break;
21140 }
21141 case OMPC_nontemporal:
21142 Res = ActOnOpenMPNontemporalClause(VarList, StartLoc, LParenLoc, EndLoc);
21143 break;
21144 case OMPC_inclusive:
21145 Res = ActOnOpenMPInclusiveClause(VarList, StartLoc, LParenLoc, EndLoc);
21146 break;
21147 case OMPC_exclusive:
21148 Res = ActOnOpenMPExclusiveClause(VarList, StartLoc, LParenLoc, EndLoc);
21149 break;
21150 case OMPC_affinity:
21151 Res = ActOnOpenMPAffinityClause(StartLoc, LParenLoc, ColonLoc, EndLoc,
21152 Modifier: Data.DepModOrTailExpr, Locators: VarList);
21153 break;
21154 case OMPC_doacross:
21155 Res = ActOnOpenMPDoacrossClause(
21156 DepType: static_cast<OpenMPDoacrossClauseModifier>(ExtraModifier),
21157 DepLoc: ExtraModifierLoc, ColonLoc, VarList, StartLoc, LParenLoc, EndLoc);
21158 break;
21159 case OMPC_num_teams:
21160 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_NUMTEAMS_unknown &&
21161 "Unexpected num_teams modifier.");
21162 Res = ActOnOpenMPNumTeamsClause(
21163 VarList,
21164 Modifier: static_cast<OpenMPNumTeamsClauseModifier>(Data.ExtraModifierArray[0]),
21165 ModifierExpr: Data.ExtraModifierExprArray[0], ModifierLoc: Data.ExtraModifierLocArray[0],
21166 ModifierExtra: static_cast<OpenMPNumTeamsClauseModifier>(Data.ExtraModifierArray[1]),
21167 ModifierExtraExpr: Data.ExtraModifierExprArray[1], ModifierExtraLoc: Data.ExtraModifierLocArray[1], StartLoc,
21168 LParenLoc, EndLoc);
21169 break;
21170 case OMPC_thread_limit:
21171 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_THREADLIMIT_unknown &&
21172 "Unexpected thread_limit modifier.");
21173 Res = ActOnOpenMPThreadLimitClause(
21174 VarList, Modifier: static_cast<OpenMPThreadLimitClauseModifier>(ExtraModifier),
21175 ModifierExpr: ExtraModifierExpr, ModifierLoc: ExtraModifierLoc, StartLoc, LParenLoc, EndLoc);
21176 break;
21177 case OMPC_num_threads:
21178 assert(0 <= Data.ExtraModifierArray[0] &&
21179 Data.ExtraModifierArray[0] <= OMPC_NUMTHREADS_unknown &&
21180 0 <= Data.ExtraModifierArray[1] &&
21181 Data.ExtraModifierArray[1] <= OMPC_NUMTHREADS_unknown &&
21182 "Unexpected num_threads modifier.");
21183 Res = ActOnOpenMPNumThreadsClause(
21184 VarList,
21185 SimpleModifier: static_cast<OpenMPNumThreadsClauseModifier>(Data.ExtraModifierArray[0]),
21186 SimpleModifierLoc: Data.ExtraModifierLocArray[0],
21187 ComplexModifier: static_cast<OpenMPNumThreadsClauseModifier>(Data.ExtraModifierArray[1]),
21188 ComplexModifierExpr: Data.ExtraModifierExprArray[1], ComplexModifierLoc: Data.ExtraModifierLocArray[1], StartLoc,
21189 LParenLoc, EndLoc);
21190 break;
21191 case OMPC_if:
21192 case OMPC_depobj:
21193 case OMPC_final:
21194 case OMPC_safelen:
21195 case OMPC_simdlen:
21196 case OMPC_sizes:
21197 case OMPC_depth:
21198 case OMPC_allocator:
21199 case OMPC_collapse:
21200 case OMPC_default:
21201 case OMPC_proc_bind:
21202 case OMPC_schedule:
21203 case OMPC_ordered:
21204 case OMPC_nowait:
21205 case OMPC_untied:
21206 case OMPC_mergeable:
21207 case OMPC_threadprivate:
21208 case OMPC_groupprivate:
21209 case OMPC_read:
21210 case OMPC_write:
21211 case OMPC_update:
21212 case OMPC_capture:
21213 case OMPC_compare:
21214 case OMPC_seq_cst:
21215 case OMPC_acq_rel:
21216 case OMPC_acquire:
21217 case OMPC_release:
21218 case OMPC_relaxed:
21219 case OMPC_device:
21220 case OMPC_threads:
21221 case OMPC_simd:
21222 case OMPC_priority:
21223 case OMPC_grainsize:
21224 case OMPC_nogroup:
21225 case OMPC_num_tasks:
21226 case OMPC_hint:
21227 case OMPC_dist_schedule:
21228 case OMPC_defaultmap:
21229 case OMPC_unknown:
21230 case OMPC_uniform:
21231 case OMPC_unified_address:
21232 case OMPC_unified_shared_memory:
21233 case OMPC_reverse_offload:
21234 case OMPC_dynamic_allocators:
21235 case OMPC_atomic_default_mem_order:
21236 case OMPC_self_maps:
21237 case OMPC_device_type:
21238 case OMPC_match:
21239 case OMPC_order:
21240 case OMPC_at:
21241 case OMPC_severity:
21242 case OMPC_message:
21243 case OMPC_destroy:
21244 case OMPC_novariants:
21245 case OMPC_nocontext:
21246 case OMPC_detach:
21247 case OMPC_uses_allocators:
21248 case OMPC_when:
21249 case OMPC_bind:
21250 default:
21251 llvm_unreachable("Clause is not allowed.");
21252 }
21253 return Res;
21254}
21255
21256ExprResult SemaOpenMP::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
21257 ExprObjectKind OK,
21258 SourceLocation Loc) {
21259 ExprResult Res = SemaRef.BuildDeclRefExpr(
21260 D: Capture, Ty: Capture->getType().getNonReferenceType(), VK: VK_LValue, Loc);
21261 if (!Res.isUsable())
21262 return ExprError();
21263 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
21264 Res = SemaRef.CreateBuiltinUnaryOp(OpLoc: Loc, Opc: UO_Deref, InputExpr: Res.get());
21265 if (!Res.isUsable())
21266 return ExprError();
21267 }
21268 if (VK != VK_LValue && Res.get()->isGLValue()) {
21269 Res = SemaRef.DefaultLvalueConversion(E: Res.get());
21270 if (!Res.isUsable())
21271 return ExprError();
21272 }
21273 return Res;
21274}
21275
21276OMPClause *SemaOpenMP::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
21277 SourceLocation StartLoc,
21278 SourceLocation LParenLoc,
21279 SourceLocation EndLoc) {
21280 SmallVector<Expr *, 8> Vars;
21281 SmallVector<Expr *, 8> PrivateCopies;
21282 llvm::omp::Version OMPVersion = getLangOpts().getOpenMPVersion();
21283 bool IsImplicitClause =
21284 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
21285 for (Expr *RefExpr : VarList) {
21286 assert(RefExpr && "NULL expr in OpenMP private clause.");
21287 SourceLocation ELoc;
21288 SourceRange ERange;
21289 Expr *SimpleRefExpr = RefExpr;
21290 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
21291 if (Res.second) {
21292 // It will be analyzed later.
21293 Vars.push_back(Elt: RefExpr);
21294 PrivateCopies.push_back(Elt: nullptr);
21295 }
21296 ValueDecl *D = Res.first;
21297 if (!D)
21298 continue;
21299
21300 QualType Type = D->getType();
21301 auto *VD = dyn_cast<VarDecl>(Val: D);
21302
21303 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
21304 // A variable that appears in a private clause must not have an incomplete
21305 // type or a reference type.
21306 if (SemaRef.RequireCompleteType(Loc: ELoc, T: Type,
21307 DiagID: diag::err_omp_private_incomplete_type))
21308 continue;
21309 Type = Type.getNonReferenceType();
21310
21311 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
21312 // A variable that is privatized must not have a const-qualified type
21313 // unless it is of class type with a mutable member. This restriction does
21314 // not apply to the firstprivate clause.
21315 //
21316 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions]
21317 // A variable that appears in a private clause must not have a
21318 // const-qualified type unless it is of class type with a mutable member.
21319 if (rejectConstNotMutableType(SemaRef, D, Type, CKind: OMPC_private, ELoc))
21320 continue;
21321
21322 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
21323 // in a Construct]
21324 // Variables with the predetermined data-sharing attributes may not be
21325 // listed in data-sharing attributes clauses, except for the cases
21326 // listed below. For these exceptions only, listing a predetermined
21327 // variable in a data-sharing attribute clause is allowed and overrides
21328 // the variable's predetermined data-sharing attributes.
21329 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
21330 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
21331 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
21332 << getOpenMPClauseNameForDiag(C: DVar.CKind)
21333 << getOpenMPClauseNameForDiag(C: OMPC_private);
21334 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
21335 continue;
21336 }
21337
21338 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
21339 // Variably modified types are not supported for tasks.
21340 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
21341 isOpenMPTaskingDirective(Kind: CurrDir)) {
21342 Diag(Loc: ELoc, DiagID: diag::err_omp_variably_modified_type_not_supported)
21343 << getOpenMPClauseNameForDiag(C: OMPC_private) << Type
21344 << getOpenMPDirectiveName(D: CurrDir, V: OMPVersion);
21345 bool IsDecl = !VD || VD->isThisDeclarationADefinition(getASTContext()) ==
21346 VarDecl::DeclarationOnly;
21347 Diag(Loc: D->getLocation(),
21348 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
21349 << D;
21350 continue;
21351 }
21352
21353 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
21354 // A list item cannot appear in both a map clause and a data-sharing
21355 // attribute clause on the same construct
21356 //
21357 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
21358 // A list item cannot appear in both a map clause and a data-sharing
21359 // attribute clause on the same construct unless the construct is a
21360 // combined construct.
21361 if ((getLangOpts().OpenMP <= 45 &&
21362 isOpenMPTargetExecutionDirective(DKind: CurrDir)) ||
21363 CurrDir == OMPD_target) {
21364 OpenMPClauseKind ConflictKind;
21365 if (DSAStack->checkMappableExprComponentListsForDecl(
21366 VD, /*CurrentRegionOnly=*/true,
21367 Check: [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
21368 OpenMPClauseKind WhereFoundClauseKind) -> bool {
21369 ConflictKind = WhereFoundClauseKind;
21370 return true;
21371 })) {
21372 Diag(Loc: ELoc, DiagID: diag::err_omp_variable_in_given_clause_and_dsa)
21373 << getOpenMPClauseNameForDiag(C: OMPC_private)
21374 << getOpenMPClauseNameForDiag(C: ConflictKind)
21375 << getOpenMPDirectiveName(D: CurrDir, V: OMPVersion);
21376 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
21377 continue;
21378 }
21379 }
21380
21381 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
21382 // A variable of class type (or array thereof) that appears in a private
21383 // clause requires an accessible, unambiguous default constructor for the
21384 // class type.
21385 // Generate helper private variable and initialize it with the default
21386 // value. The address of the original variable is replaced by the address of
21387 // the new private variable in CodeGen. This new variable is not added to
21388 // IdResolver, so the code in the OpenMP region uses original variable for
21389 // proper diagnostics.
21390 Type = Type.getUnqualifiedType();
21391 VarDecl *VDPrivate =
21392 buildVarDecl(SemaRef, Loc: ELoc, Type, Name: D->getName(),
21393 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr,
21394 OrigRef: VD ? cast<DeclRefExpr>(Val: SimpleRefExpr) : nullptr);
21395 SemaRef.ActOnUninitializedDecl(dcl: VDPrivate);
21396 if (VDPrivate->isInvalidDecl())
21397 continue;
21398 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
21399 S&: SemaRef, D: VDPrivate, Ty: RefExpr->getType().getUnqualifiedType(), Loc: ELoc);
21400
21401 DeclRefExpr *Ref = nullptr;
21402 bool IsBindingDecl = isa<BindingDecl>(Val: D);
21403 if (!VD && !IsBindingDecl && !SemaRef.CurContext->isDependentContext()) {
21404 auto *FD = dyn_cast<FieldDecl>(Val: D);
21405 VarDecl *VD = FD ? DSAStack->getImplicitFDCapExprDecl(FD) : nullptr;
21406 if (VD)
21407 Ref = buildDeclRefExpr(S&: SemaRef, D: VD, Ty: VD->getType().getNonReferenceType(),
21408 Loc: RefExpr->getExprLoc());
21409 else
21410 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/false);
21411 }
21412 if (!IsImplicitClause) {
21413 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_private, PrivateCopy: Ref);
21414 }
21415 Vars.push_back(
21416 Elt: (VD || IsBindingDecl || SemaRef.CurContext->isDependentContext())
21417 ? RefExpr->IgnoreParens()
21418 : Ref);
21419 PrivateCopies.push_back(Elt: VDPrivateRefExpr);
21420 }
21421
21422 if (Vars.empty())
21423 return nullptr;
21424
21425 return OMPPrivateClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
21426 VL: Vars, PrivateVL: PrivateCopies);
21427}
21428
21429OMPClause *SemaOpenMP::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
21430 SourceLocation StartLoc,
21431 SourceLocation LParenLoc,
21432 SourceLocation EndLoc) {
21433 SmallVector<Expr *, 8> Vars;
21434 SmallVector<Expr *, 8> PrivateCopies;
21435 SmallVector<Expr *, 8> Inits;
21436 SmallVector<Decl *, 4> ExprCaptures;
21437 bool IsImplicitClause =
21438 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
21439 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
21440 llvm::omp::Version OMPVersion = getLangOpts().getOpenMPVersion();
21441
21442 for (Expr *RefExpr : VarList) {
21443 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
21444 SourceLocation ELoc;
21445 SourceRange ERange;
21446 Expr *SimpleRefExpr = RefExpr;
21447 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
21448 if (Res.second) {
21449 // It will be analyzed later.
21450 Vars.push_back(Elt: RefExpr);
21451 PrivateCopies.push_back(Elt: nullptr);
21452 Inits.push_back(Elt: nullptr);
21453 }
21454 ValueDecl *D = Res.first;
21455 if (!D)
21456 continue;
21457
21458 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
21459 QualType Type = D->getType();
21460 auto *VD = dyn_cast<VarDecl>(Val: D);
21461
21462 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
21463 // A variable that appears in a private clause must not have an incomplete
21464 // type or a reference type.
21465 if (SemaRef.RequireCompleteType(Loc: ELoc, T: Type,
21466 DiagID: diag::err_omp_firstprivate_incomplete_type))
21467 continue;
21468 Type = Type.getNonReferenceType();
21469
21470 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
21471 // A variable of class type (or array thereof) that appears in a private
21472 // clause requires an accessible, unambiguous copy constructor for the
21473 // class type.
21474 QualType ElemType =
21475 getASTContext().getBaseElementType(QT: Type).getNonReferenceType();
21476
21477 // If an implicit firstprivate variable found it was checked already.
21478 DSAStackTy::DSAVarData TopDVar;
21479 if (!IsImplicitClause) {
21480 DSAStackTy::DSAVarData DVar =
21481 DSAStack->getTopDSA(D, /*FromParent=*/false);
21482 TopDVar = DVar;
21483 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
21484 bool IsConstant = ElemType.isConstant(Ctx: getASTContext());
21485 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
21486 // A list item that specifies a given variable may not appear in more
21487 // than one clause on the same directive, except that a variable may be
21488 // specified in both firstprivate and lastprivate clauses.
21489 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
21490 // A list item may appear in a firstprivate or lastprivate clause but not
21491 // both.
21492 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
21493 (isOpenMPDistributeDirective(DKind: CurrDir) ||
21494 DVar.CKind != OMPC_lastprivate) &&
21495 DVar.RefExpr) {
21496 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
21497 << getOpenMPClauseNameForDiag(C: DVar.CKind)
21498 << getOpenMPClauseNameForDiag(C: OMPC_firstprivate);
21499 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
21500 continue;
21501 }
21502
21503 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
21504 // in a Construct]
21505 // Variables with the predetermined data-sharing attributes may not be
21506 // listed in data-sharing attributes clauses, except for the cases
21507 // listed below. For these exceptions only, listing a predetermined
21508 // variable in a data-sharing attribute clause is allowed and overrides
21509 // the variable's predetermined data-sharing attributes.
21510 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
21511 // in a Construct, C/C++, p.2]
21512 // Variables with const-qualified type having no mutable member may be
21513 // listed in a firstprivate clause, even if they are static data members.
21514 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
21515 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
21516 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
21517 << getOpenMPClauseNameForDiag(C: DVar.CKind)
21518 << getOpenMPClauseNameForDiag(C: OMPC_firstprivate);
21519 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
21520 continue;
21521 }
21522
21523 // OpenMP [2.9.3.4, Restrictions, p.2]
21524 // A list item that is private within a parallel region must not appear
21525 // in a firstprivate clause on a worksharing construct if any of the
21526 // worksharing regions arising from the worksharing construct ever bind
21527 // to any of the parallel regions arising from the parallel construct.
21528 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
21529 // A list item that is private within a teams region must not appear in a
21530 // firstprivate clause on a distribute construct if any of the distribute
21531 // regions arising from the distribute construct ever bind to any of the
21532 // teams regions arising from the teams construct.
21533 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
21534 // A list item that appears in a reduction clause of a teams construct
21535 // must not appear in a firstprivate clause on a distribute construct if
21536 // any of the distribute regions arising from the distribute construct
21537 // ever bind to any of the teams regions arising from the teams construct.
21538 if ((isOpenMPWorksharingDirective(DKind: CurrDir) ||
21539 isOpenMPDistributeDirective(DKind: CurrDir)) &&
21540 !isOpenMPParallelDirective(DKind: CurrDir) &&
21541 !isOpenMPTeamsDirective(DKind: CurrDir)) {
21542 DVar = DSAStack->getImplicitDSA(D, FromParent: true);
21543 if (DVar.CKind != OMPC_shared &&
21544 (isOpenMPParallelDirective(DKind: DVar.DKind) ||
21545 isOpenMPTeamsDirective(DKind: DVar.DKind) ||
21546 DVar.DKind == OMPD_unknown)) {
21547 Diag(Loc: ELoc, DiagID: diag::err_omp_required_access)
21548 << getOpenMPClauseNameForDiag(C: OMPC_firstprivate)
21549 << getOpenMPClauseNameForDiag(C: OMPC_shared);
21550 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
21551 continue;
21552 }
21553 }
21554 // OpenMP [2.9.3.4, Restrictions, p.3]
21555 // A list item that appears in a reduction clause of a parallel construct
21556 // must not appear in a firstprivate clause on a worksharing or task
21557 // construct if any of the worksharing or task regions arising from the
21558 // worksharing or task construct ever bind to any of the parallel regions
21559 // arising from the parallel construct.
21560 // OpenMP [2.9.3.4, Restrictions, p.4]
21561 // A list item that appears in a reduction clause in worksharing
21562 // construct must not appear in a firstprivate clause in a task construct
21563 // encountered during execution of any of the worksharing regions arising
21564 // from the worksharing construct.
21565 if (isOpenMPTaskingDirective(Kind: CurrDir)) {
21566 DVar = DSAStack->hasInnermostDSA(
21567 D,
21568 CPred: [](OpenMPClauseKind C, bool AppliedToPointee) {
21569 return C == OMPC_reduction && !AppliedToPointee;
21570 },
21571 DPred: [](OpenMPDirectiveKind K) {
21572 return isOpenMPParallelDirective(DKind: K) ||
21573 isOpenMPWorksharingDirective(DKind: K) ||
21574 isOpenMPTeamsDirective(DKind: K);
21575 },
21576 /*FromParent=*/true);
21577 if (DVar.CKind == OMPC_reduction &&
21578 (isOpenMPParallelDirective(DKind: DVar.DKind) ||
21579 isOpenMPWorksharingDirective(DKind: DVar.DKind) ||
21580 isOpenMPTeamsDirective(DKind: DVar.DKind))) {
21581 Diag(Loc: ELoc, DiagID: diag::err_omp_parallel_reduction_in_task_firstprivate)
21582 << getOpenMPDirectiveName(D: DVar.DKind, V: OMPVersion);
21583 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
21584 continue;
21585 }
21586 }
21587
21588 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
21589 // A list item cannot appear in both a map clause and a data-sharing
21590 // attribute clause on the same construct
21591 //
21592 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
21593 // A list item cannot appear in both a map clause and a data-sharing
21594 // attribute clause on the same construct unless the construct is a
21595 // combined construct.
21596 if ((getLangOpts().OpenMP <= 45 &&
21597 isOpenMPTargetExecutionDirective(DKind: CurrDir)) ||
21598 CurrDir == OMPD_target) {
21599 OpenMPClauseKind ConflictKind;
21600 if (DSAStack->checkMappableExprComponentListsForDecl(
21601 VD, /*CurrentRegionOnly=*/true,
21602 Check: [&ConflictKind](
21603 OMPClauseMappableExprCommon::MappableExprComponentListRef,
21604 OpenMPClauseKind WhereFoundClauseKind) {
21605 ConflictKind = WhereFoundClauseKind;
21606 return true;
21607 })) {
21608 Diag(Loc: ELoc, DiagID: diag::err_omp_variable_in_given_clause_and_dsa)
21609 << getOpenMPClauseNameForDiag(C: OMPC_firstprivate)
21610 << getOpenMPClauseNameForDiag(C: ConflictKind)
21611 << getOpenMPDirectiveName(DSAStack->getCurrentDirective(),
21612 V: OMPVersion);
21613 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
21614 continue;
21615 }
21616 }
21617 }
21618
21619 // Variably modified types are not supported for tasks.
21620 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
21621 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
21622 Diag(Loc: ELoc, DiagID: diag::err_omp_variably_modified_type_not_supported)
21623 << getOpenMPClauseNameForDiag(C: OMPC_firstprivate) << Type
21624 << getOpenMPDirectiveName(DSAStack->getCurrentDirective(),
21625 V: OMPVersion);
21626 bool IsDecl = !VD || VD->isThisDeclarationADefinition(getASTContext()) ==
21627 VarDecl::DeclarationOnly;
21628 Diag(Loc: D->getLocation(),
21629 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
21630 << D;
21631 continue;
21632 }
21633
21634 Type = Type.getUnqualifiedType();
21635
21636 // For BindingDecls, use the DecompositionDecl's type so all bindings from
21637 // the same decomposition are accessible in the privatized region. We do
21638 // this after conflict checking to preserve diagnostic errors. Don't modify
21639 // RefExpr/SimpleRefExpr to keep diagnostics working.
21640 bool IsBindingDecl = isa<BindingDecl>(Val: D);
21641 VarDecl *PrivateVD = VD;
21642 QualType PrivateType = Type;
21643 if (IsBindingDecl) {
21644 const auto *BD = cast<BindingDecl>(Val: D);
21645 PrivateVD = cast<VarDecl>(Val: BD->getDecomposedDecl());
21646 // PrivateType stays as the BindingDecl's type (Type), not the
21647 // DecompositionDecl's type.
21648 }
21649
21650 // For regular arrays, pass the original var ref. For BindingDecls, don't
21651 // pass the DecompositionDecl ref since VDPrivate has the binding's type,
21652 // not the decomposition type.
21653 VarDecl *VDPrivate = buildVarDecl(SemaRef, Loc: ELoc, Type: PrivateType, Name: D->getName(),
21654 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr,
21655 OrigRef: (PrivateVD && !IsBindingDecl)
21656 ? cast<DeclRefExpr>(Val: SimpleRefExpr)
21657 : nullptr);
21658
21659 // Generate helper private variable and initialize it with the value of the
21660 // original variable. The address of the original variable is replaced by
21661 // the address of the new private variable in the CodeGen. This new variable
21662 // is not added to IdResolver, so the code in the OpenMP region uses
21663 // original variable for proper diagnostics and variable capturing.
21664 Expr *VDInitRefExpr = nullptr;
21665
21666 // For BindingDecls, VDPrivate should have the binding's type (not the
21667 // DecompositionDecl's type), and will be initialized from the binding's
21668 // field in the original DecompositionDecl.
21669 if (IsBindingDecl && !PrivateType->isArrayType()) {
21670 // For non-array bindings, create a simple copy initialization.
21671 VarDecl *VDInit = buildVarDecl(SemaRef, Loc: RefExpr->getExprLoc(),
21672 Type: PrivateType, Name: ".firstprivate.temp");
21673 VDInitRefExpr =
21674 buildDeclRefExpr(S&: SemaRef, D: VDInit, Ty: PrivateType, Loc: RefExpr->getExprLoc());
21675
21676 // Initialize VDPrivate from VDInit (which will point to the field).
21677 SemaRef.AddInitializerToDecl(
21678 dcl: VDPrivate, init: SemaRef.DefaultLvalueConversion(E: VDInitRefExpr).get(),
21679 /*DirectInit=*/false);
21680 } else if (Type->isArrayType()) {
21681 // For arrays generate initializer for single element and replace it by
21682 // the original array element in CodeGen.
21683 VarDecl *VDInit =
21684 buildVarDecl(SemaRef, Loc: RefExpr->getExprLoc(), Type: ElemType, Name: D->getName());
21685 VDInitRefExpr = buildDeclRefExpr(S&: SemaRef, D: VDInit, Ty: ElemType, Loc: ELoc);
21686 Expr *Init = SemaRef.DefaultLvalueConversion(E: VDInitRefExpr).get();
21687 ElemType = ElemType.getUnqualifiedType();
21688 VarDecl *VDInitTemp = buildVarDecl(SemaRef, Loc: RefExpr->getExprLoc(),
21689 Type: ElemType, Name: ".firstprivate.temp");
21690 InitializedEntity Entity =
21691 InitializedEntity::InitializeVariable(Var: VDInitTemp);
21692 InitializationKind Kind = InitializationKind::CreateCopy(InitLoc: ELoc, EqualLoc: ELoc);
21693
21694 InitializationSequence InitSeq(SemaRef, Entity, Kind, Init);
21695 ExprResult Result = InitSeq.Perform(S&: SemaRef, Entity, Kind, Args: Init);
21696 if (Result.isInvalid())
21697 VDPrivate->setInvalidDecl();
21698 else
21699 VDPrivate->setInit(Result.getAs<Expr>());
21700 // Remove temp variable declaration.
21701 getASTContext().Deallocate(Ptr: VDInitTemp);
21702 } else {
21703 VarDecl *VDInit = buildVarDecl(SemaRef, Loc: RefExpr->getExprLoc(), Type,
21704 Name: ".firstprivate.temp");
21705 VDInitRefExpr = buildDeclRefExpr(S&: SemaRef, D: VDInit, Ty: RefExpr->getType(),
21706 Loc: RefExpr->getExprLoc());
21707 SemaRef.AddInitializerToDecl(
21708 dcl: VDPrivate, init: SemaRef.DefaultLvalueConversion(E: VDInitRefExpr).get(),
21709 /*DirectInit=*/false);
21710 }
21711 if (VDPrivate->isInvalidDecl()) {
21712 if (IsImplicitClause) {
21713 Diag(Loc: RefExpr->getExprLoc(),
21714 DiagID: diag::note_omp_task_predetermined_firstprivate_here);
21715 }
21716 continue;
21717 }
21718 SemaRef.CurContext->addDecl(D: VDPrivate);
21719 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
21720 S&: SemaRef, D: VDPrivate, Ty: RefExpr->getType().getUnqualifiedType(),
21721 Loc: RefExpr->getExprLoc());
21722 DeclRefExpr *Ref = nullptr;
21723 if (!VD && !SemaRef.CurContext->isDependentContext()) {
21724 if (TopDVar.CKind == OMPC_lastprivate) {
21725 Ref = TopDVar.PrivateCopy;
21726 } else if (!IsBindingDecl) {
21727 auto *FD = dyn_cast<FieldDecl>(Val: D);
21728 VarDecl *VD = FD ? DSAStack->getImplicitFDCapExprDecl(FD) : nullptr;
21729 if (VD)
21730 Ref =
21731 buildDeclRefExpr(S&: SemaRef, D: VD, Ty: VD->getType().getNonReferenceType(),
21732 Loc: RefExpr->getExprLoc());
21733 else
21734 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/true);
21735 if (VD || !isOpenMPCapturedDecl(D))
21736 ExprCaptures.push_back(Elt: Ref->getDecl());
21737 }
21738 }
21739 if (!IsImplicitClause)
21740 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_firstprivate, PrivateCopy: Ref);
21741 Vars.push_back(
21742 Elt: (VD || IsBindingDecl || SemaRef.CurContext->isDependentContext())
21743 ? RefExpr->IgnoreParens()
21744 : Ref);
21745 PrivateCopies.push_back(Elt: VDPrivateRefExpr);
21746 Inits.push_back(Elt: VDInitRefExpr);
21747 }
21748
21749 if (Vars.empty())
21750 return nullptr;
21751
21752 return OMPFirstprivateClause::Create(
21753 C: getASTContext(), StartLoc, LParenLoc, EndLoc, VL: Vars, PrivateVL: PrivateCopies, InitVL: Inits,
21754 PreInit: buildPreInits(Context&: getASTContext(), PreInits: ExprCaptures));
21755}
21756
21757OMPClause *SemaOpenMP::ActOnOpenMPLastprivateClause(
21758 ArrayRef<Expr *> VarList, OpenMPLastprivateModifier LPKind,
21759 SourceLocation LPKindLoc, SourceLocation ColonLoc, SourceLocation StartLoc,
21760 SourceLocation LParenLoc, SourceLocation EndLoc) {
21761 if (LPKind == OMPC_LASTPRIVATE_unknown && LPKindLoc.isValid()) {
21762 assert(ColonLoc.isValid() && "Colon location must be valid.");
21763 Diag(Loc: LPKindLoc, DiagID: diag::err_omp_unexpected_clause_value)
21764 << getListOfPossibleValues(K: OMPC_lastprivate, /*First=*/0,
21765 /*Last=*/OMPC_LASTPRIVATE_unknown)
21766 << getOpenMPClauseNameForDiag(C: OMPC_lastprivate);
21767 return nullptr;
21768 }
21769
21770 SmallVector<Expr *, 8> Vars;
21771 SmallVector<Expr *, 8> SrcExprs;
21772 SmallVector<Expr *, 8> DstExprs;
21773 SmallVector<Expr *, 8> AssignmentOps;
21774 SmallVector<Decl *, 4> ExprCaptures;
21775 SmallVector<Expr *, 4> ExprPostUpdates;
21776 for (Expr *RefExpr : VarList) {
21777 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
21778 SourceLocation ELoc;
21779 SourceRange ERange;
21780 Expr *SimpleRefExpr = RefExpr;
21781 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
21782 if (Res.second) {
21783 // It will be analyzed later.
21784 Vars.push_back(Elt: RefExpr);
21785 SrcExprs.push_back(Elt: nullptr);
21786 DstExprs.push_back(Elt: nullptr);
21787 AssignmentOps.push_back(Elt: nullptr);
21788 }
21789 ValueDecl *D = Res.first;
21790 if (!D)
21791 continue;
21792
21793 QualType Type = D->getType();
21794 auto *VD = dyn_cast<VarDecl>(Val: D);
21795
21796 // Structured bindings with conditional modifier are currently not
21797 // supported.
21798 if (LPKind == OMPC_LASTPRIVATE_conditional && isa<BindingDecl>(Val: D)) {
21799 Diag(Loc: ELoc, DiagID: diag::err_omp_unsupported_on_binding) << 1;
21800 Diag(Loc: D->getLocation(), DiagID: diag::note_defined_here) << D;
21801 continue;
21802 }
21803
21804 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
21805 // A variable that appears in a lastprivate clause must not have an
21806 // incomplete type or a reference type.
21807 if (SemaRef.RequireCompleteType(Loc: ELoc, T: Type,
21808 DiagID: diag::err_omp_lastprivate_incomplete_type))
21809 continue;
21810 Type = Type.getNonReferenceType();
21811
21812 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
21813 // A variable that is privatized must not have a const-qualified type
21814 // unless it is of class type with a mutable member. This restriction does
21815 // not apply to the firstprivate clause.
21816 //
21817 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions]
21818 // A variable that appears in a lastprivate clause must not have a
21819 // const-qualified type unless it is of class type with a mutable member.
21820 if (rejectConstNotMutableType(SemaRef, D, Type, CKind: OMPC_lastprivate, ELoc))
21821 continue;
21822
21823 // OpenMP 5.0 [2.19.4.5 lastprivate Clause, Restrictions]
21824 // A list item that appears in a lastprivate clause with the conditional
21825 // modifier must be a scalar variable.
21826 if (LPKind == OMPC_LASTPRIVATE_conditional && !Type->isScalarType()) {
21827 Diag(Loc: ELoc, DiagID: diag::err_omp_lastprivate_conditional_non_scalar);
21828 bool IsDecl = !VD || VD->isThisDeclarationADefinition(getASTContext()) ==
21829 VarDecl::DeclarationOnly;
21830 Diag(Loc: D->getLocation(),
21831 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
21832 << D;
21833 continue;
21834 }
21835
21836 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
21837 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
21838 // in a Construct]
21839 // Variables with the predetermined data-sharing attributes may not be
21840 // listed in data-sharing attributes clauses, except for the cases
21841 // listed below.
21842 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
21843 // A list item may appear in a firstprivate or lastprivate clause but not
21844 // both.
21845 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
21846 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
21847 (isOpenMPDistributeDirective(DKind: CurrDir) ||
21848 DVar.CKind != OMPC_firstprivate) &&
21849 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
21850 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
21851 << getOpenMPClauseNameForDiag(C: DVar.CKind)
21852 << getOpenMPClauseNameForDiag(C: OMPC_lastprivate);
21853 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
21854 continue;
21855 }
21856
21857 // OpenMP [2.14.3.5, Restrictions, p.2]
21858 // A list item that is private within a parallel region, or that appears in
21859 // the reduction clause of a parallel construct, must not appear in a
21860 // lastprivate clause on a worksharing construct if any of the corresponding
21861 // worksharing regions ever binds to any of the corresponding parallel
21862 // regions.
21863 DSAStackTy::DSAVarData TopDVar = DVar;
21864 if (isOpenMPWorksharingDirective(DKind: CurrDir) &&
21865 !isOpenMPParallelDirective(DKind: CurrDir) &&
21866 !isOpenMPTeamsDirective(DKind: CurrDir)) {
21867 DVar = DSAStack->getImplicitDSA(D, FromParent: true);
21868 if (DVar.CKind != OMPC_shared) {
21869 Diag(Loc: ELoc, DiagID: diag::err_omp_required_access)
21870 << getOpenMPClauseNameForDiag(C: OMPC_lastprivate)
21871 << getOpenMPClauseNameForDiag(C: OMPC_shared);
21872 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
21873 continue;
21874 }
21875 }
21876
21877 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
21878 // A variable of class type (or array thereof) that appears in a
21879 // lastprivate clause requires an accessible, unambiguous default
21880 // constructor for the class type, unless the list item is also specified
21881 // in a firstprivate clause.
21882 // A variable of class type (or array thereof) that appears in a
21883 // lastprivate clause requires an accessible, unambiguous copy assignment
21884 // operator for the class type.
21885 Type = getASTContext().getBaseElementType(QT: Type).getNonReferenceType();
21886 VarDecl *SrcVD = buildVarDecl(SemaRef, Loc: ERange.getBegin(),
21887 Type: Type.getUnqualifiedType(), Name: ".lastprivate.src",
21888 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
21889 DeclRefExpr *PseudoSrcExpr =
21890 buildDeclRefExpr(S&: SemaRef, D: SrcVD, Ty: Type.getUnqualifiedType(), Loc: ELoc);
21891 VarDecl *DstVD =
21892 buildVarDecl(SemaRef, Loc: ERange.getBegin(), Type, Name: ".lastprivate.dst",
21893 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
21894 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(S&: SemaRef, D: DstVD, Ty: Type, Loc: ELoc);
21895 // For arrays generate assignment operation for single element and replace
21896 // it by the original array element in CodeGen.
21897 ExprResult AssignmentOp = SemaRef.BuildBinOp(/*S=*/nullptr, OpLoc: ELoc, Opc: BO_Assign,
21898 LHSExpr: PseudoDstExpr, RHSExpr: PseudoSrcExpr);
21899 if (AssignmentOp.isInvalid())
21900 continue;
21901 AssignmentOp = SemaRef.ActOnFinishFullExpr(Expr: AssignmentOp.get(), CC: ELoc,
21902 /*DiscardedValue=*/false);
21903 if (AssignmentOp.isInvalid())
21904 continue;
21905
21906 DeclRefExpr *Ref = nullptr;
21907 if (!VD && !SemaRef.CurContext->isDependentContext()) {
21908 if (TopDVar.CKind == OMPC_firstprivate) {
21909 Ref = TopDVar.PrivateCopy;
21910 } else {
21911 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/false);
21912 if (!isOpenMPCapturedDecl(D))
21913 ExprCaptures.push_back(Elt: Ref->getDecl());
21914 }
21915 if (Ref &&
21916 ((TopDVar.CKind == OMPC_firstprivate && !TopDVar.PrivateCopy) ||
21917 (!isOpenMPCapturedDecl(D) &&
21918 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()))) {
21919 ExprResult RefRes = SemaRef.DefaultLvalueConversion(E: Ref);
21920 if (!RefRes.isUsable())
21921 continue;
21922 ExprResult PostUpdateRes =
21923 SemaRef.BuildBinOp(DSAStack->getCurScope(), OpLoc: ELoc, Opc: BO_Assign,
21924 LHSExpr: SimpleRefExpr, RHSExpr: RefRes.get());
21925 if (!PostUpdateRes.isUsable())
21926 continue;
21927 ExprPostUpdates.push_back(
21928 Elt: SemaRef.IgnoredValueConversions(E: PostUpdateRes.get()).get());
21929 }
21930 }
21931 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_lastprivate, PrivateCopy: Ref);
21932 bool IsBindingDecl = isa<BindingDecl>(Val: D);
21933 Vars.push_back(
21934 Elt: (VD || IsBindingDecl || SemaRef.CurContext->isDependentContext())
21935 ? RefExpr->IgnoreParens()
21936 : Ref);
21937 SrcExprs.push_back(Elt: PseudoSrcExpr);
21938 DstExprs.push_back(Elt: PseudoDstExpr);
21939 AssignmentOps.push_back(Elt: AssignmentOp.get());
21940 }
21941
21942 if (Vars.empty())
21943 return nullptr;
21944
21945 return OMPLastprivateClause::Create(
21946 C: getASTContext(), StartLoc, LParenLoc, EndLoc, VL: Vars, SrcExprs, DstExprs,
21947 AssignmentOps, LPKind, LPKindLoc, ColonLoc,
21948 PreInit: buildPreInits(Context&: getASTContext(), PreInits: ExprCaptures),
21949 PostUpdate: buildPostUpdate(S&: SemaRef, PostUpdates: ExprPostUpdates));
21950}
21951
21952OMPClause *SemaOpenMP::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
21953 SourceLocation StartLoc,
21954 SourceLocation LParenLoc,
21955 SourceLocation EndLoc) {
21956 SmallVector<Expr *, 8> Vars;
21957 for (Expr *RefExpr : VarList) {
21958 assert(RefExpr && "NULL expr in OpenMP shared clause.");
21959 SourceLocation ELoc;
21960 SourceRange ERange;
21961 Expr *SimpleRefExpr = RefExpr;
21962 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
21963 if (Res.second) {
21964 // It will be analyzed later.
21965 Vars.push_back(Elt: RefExpr);
21966 }
21967 ValueDecl *D = Res.first;
21968 if (!D)
21969 continue;
21970
21971 auto *VD = dyn_cast<VarDecl>(Val: D);
21972 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
21973 // in a Construct]
21974 // Variables with the predetermined data-sharing attributes may not be
21975 // listed in data-sharing attributes clauses, except for the cases
21976 // listed below. For these exceptions only, listing a predetermined
21977 // variable in a data-sharing attribute clause is allowed and overrides
21978 // the variable's predetermined data-sharing attributes.
21979 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
21980 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
21981 DVar.RefExpr) {
21982 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
21983 << getOpenMPClauseNameForDiag(C: DVar.CKind)
21984 << getOpenMPClauseNameForDiag(C: OMPC_shared);
21985 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
21986 continue;
21987 }
21988
21989 DeclRefExpr *Ref = nullptr;
21990 if (!VD && isOpenMPCapturedDecl(D) &&
21991 !SemaRef.CurContext->isDependentContext())
21992 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/true);
21993 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_shared, PrivateCopy: Ref);
21994 Vars.push_back(Elt: (VD || !Ref || SemaRef.CurContext->isDependentContext())
21995 ? RefExpr->IgnoreParens()
21996 : Ref);
21997 }
21998
21999 if (Vars.empty())
22000 return nullptr;
22001
22002 return OMPSharedClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
22003 VL: Vars);
22004}
22005
22006namespace {
22007class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
22008 DSAStackTy *Stack;
22009
22010public:
22011 bool VisitDeclRefExpr(DeclRefExpr *E) {
22012 if (auto *VD = dyn_cast<VarDecl>(Val: E->getDecl())) {
22013 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D: VD, /*FromParent=*/false);
22014 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
22015 return false;
22016 if (DVar.CKind != OMPC_unknown)
22017 return true;
22018 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
22019 D: VD,
22020 CPred: [](OpenMPClauseKind C, bool AppliedToPointee, bool) {
22021 return isOpenMPPrivate(Kind: C) && !AppliedToPointee;
22022 },
22023 DPred: [](OpenMPDirectiveKind) { return true; },
22024 /*FromParent=*/true);
22025 return DVarPrivate.CKind != OMPC_unknown;
22026 }
22027 return false;
22028 }
22029 bool VisitStmt(Stmt *S) {
22030 for (Stmt *Child : S->children()) {
22031 if (Child && Visit(S: Child))
22032 return true;
22033 }
22034 return false;
22035 }
22036 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
22037};
22038} // namespace
22039
22040namespace {
22041// Transform MemberExpression for specified FieldDecl of current class to
22042// DeclRefExpr to specified OMPCapturedExprDecl.
22043class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
22044 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
22045 ValueDecl *Field = nullptr;
22046 DeclRefExpr *CapturedExpr = nullptr;
22047
22048public:
22049 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
22050 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
22051
22052 ExprResult TransformMemberExpr(MemberExpr *E) {
22053 if (isa<CXXThisExpr>(Val: E->getBase()->IgnoreParenImpCasts()) &&
22054 E->getMemberDecl() == Field) {
22055 CapturedExpr = buildCapture(S&: SemaRef, D: Field, CaptureExpr: E, /*WithInit=*/false);
22056 return CapturedExpr;
22057 }
22058 return BaseTransform::TransformMemberExpr(E);
22059 }
22060 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
22061};
22062} // namespace
22063
22064template <typename T, typename U>
22065static T filterLookupForUDReductionAndMapper(
22066 SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) {
22067 for (U &Set : Lookups) {
22068 for (auto *D : Set) {
22069 if (T Res = Gen(cast<ValueDecl>(D)))
22070 return Res;
22071 }
22072 }
22073 return T();
22074}
22075
22076static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
22077 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
22078
22079 for (auto *RD : D->redecls()) {
22080 // Don't bother with extra checks if we already know this one isn't visible.
22081 if (RD == D)
22082 continue;
22083
22084 auto ND = cast<NamedDecl>(Val: RD);
22085 if (LookupResult::isVisible(SemaRef, D: ND))
22086 return ND;
22087 }
22088
22089 return nullptr;
22090}
22091
22092static void
22093argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id,
22094 SourceLocation Loc, QualType Ty,
22095 SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
22096 // Find all of the associated namespaces and classes based on the
22097 // arguments we have.
22098 Sema::AssociatedNamespaceSet AssociatedNamespaces;
22099 Sema::AssociatedClassSet AssociatedClasses;
22100 OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
22101 SemaRef.FindAssociatedClassesAndNamespaces(InstantiationLoc: Loc, Args: &OVE, AssociatedNamespaces,
22102 AssociatedClasses);
22103
22104 // C++ [basic.lookup.argdep]p3:
22105 // Let X be the lookup set produced by unqualified lookup (3.4.1)
22106 // and let Y be the lookup set produced by argument dependent
22107 // lookup (defined as follows). If X contains [...] then Y is
22108 // empty. Otherwise Y is the set of declarations found in the
22109 // namespaces associated with the argument types as described
22110 // below. The set of declarations found by the lookup of the name
22111 // is the union of X and Y.
22112 //
22113 // Here, we compute Y and add its members to the overloaded
22114 // candidate set.
22115 for (auto *NS : AssociatedNamespaces) {
22116 // When considering an associated namespace, the lookup is the
22117 // same as the lookup performed when the associated namespace is
22118 // used as a qualifier (3.4.3.2) except that:
22119 //
22120 // -- Any using-directives in the associated namespace are
22121 // ignored.
22122 //
22123 // -- Any namespace-scope friend functions declared in
22124 // associated classes are visible within their respective
22125 // namespaces even if they are not visible during an ordinary
22126 // lookup (11.4).
22127 DeclContext::lookup_result R = NS->lookup(Name: Id.getName());
22128 for (auto *D : R) {
22129 auto *Underlying = D;
22130 if (auto *USD = dyn_cast<UsingShadowDecl>(Val: D))
22131 Underlying = USD->getTargetDecl();
22132
22133 if (!isa<OMPDeclareReductionDecl>(Val: Underlying) &&
22134 !isa<OMPDeclareMapperDecl>(Val: Underlying))
22135 continue;
22136
22137 if (!SemaRef.isVisible(D)) {
22138 D = findAcceptableDecl(SemaRef, D);
22139 if (!D)
22140 continue;
22141 if (auto *USD = dyn_cast<UsingShadowDecl>(Val: D))
22142 Underlying = USD->getTargetDecl();
22143 }
22144 Lookups.emplace_back();
22145 Lookups.back().addDecl(D: Underlying);
22146 }
22147 }
22148}
22149
22150static ExprResult
22151buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
22152 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
22153 const DeclarationNameInfo &ReductionId, QualType Ty,
22154 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
22155 if (ReductionIdScopeSpec.isInvalid())
22156 return ExprError();
22157 SmallVector<UnresolvedSet<8>, 4> Lookups;
22158 if (S) {
22159 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
22160 Lookup.suppressDiagnostics();
22161 while (S && SemaRef.LookupParsedName(R&: Lookup, S, SS: &ReductionIdScopeSpec,
22162 /*ObjectType=*/QualType())) {
22163 NamedDecl *D = Lookup.getRepresentativeDecl();
22164 do {
22165 S = S->getParent();
22166 } while (S && !S->isDeclScope(D));
22167 if (S)
22168 S = S->getParent();
22169 Lookups.emplace_back();
22170 Lookups.back().append(I: Lookup.begin(), E: Lookup.end());
22171 Lookup.clear();
22172 }
22173 } else if (auto *ULE =
22174 cast_or_null<UnresolvedLookupExpr>(Val: UnresolvedReduction)) {
22175 Lookups.push_back(Elt: UnresolvedSet<8>());
22176 Decl *PrevD = nullptr;
22177 for (NamedDecl *D : ULE->decls()) {
22178 if (D == PrevD)
22179 Lookups.push_back(Elt: UnresolvedSet<8>());
22180 else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Val: D))
22181 Lookups.back().addDecl(D: DRD);
22182 PrevD = D;
22183 }
22184 }
22185 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
22186 Ty->isInstantiationDependentType() ||
22187 Ty->containsUnexpandedParameterPack() ||
22188 filterLookupForUDReductionAndMapper<bool>(Lookups, Gen: [](ValueDecl *D) {
22189 return !D->isInvalidDecl() &&
22190 (D->getType()->isDependentType() ||
22191 D->getType()->isInstantiationDependentType() ||
22192 D->getType()->containsUnexpandedParameterPack());
22193 })) {
22194 UnresolvedSet<8> ResSet;
22195 for (const UnresolvedSet<8> &Set : Lookups) {
22196 if (Set.empty())
22197 continue;
22198 ResSet.append(I: Set.begin(), E: Set.end());
22199 // The last item marks the end of all declarations at the specified scope.
22200 ResSet.addDecl(D: Set[Set.size() - 1]);
22201 }
22202 return UnresolvedLookupExpr::Create(
22203 Context: SemaRef.Context, /*NamingClass=*/nullptr,
22204 QualifierLoc: ReductionIdScopeSpec.getWithLocInContext(Context&: SemaRef.Context), NameInfo: ReductionId,
22205 /*ADL=*/RequiresADL: true, Begin: ResSet.begin(), End: ResSet.end(), /*KnownDependent=*/false,
22206 /*KnownInstantiationDependent=*/false);
22207 }
22208 // Lookup inside the classes.
22209 // C++ [over.match.oper]p3:
22210 // For a unary operator @ with an operand of a type whose
22211 // cv-unqualified version is T1, and for a binary operator @ with
22212 // a left operand of a type whose cv-unqualified version is T1 and
22213 // a right operand of a type whose cv-unqualified version is T2,
22214 // three sets of candidate functions, designated member
22215 // candidates, non-member candidates and built-in candidates, are
22216 // constructed as follows:
22217 // -- If T1 is a complete class type or a class currently being
22218 // defined, the set of member candidates is the result of the
22219 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
22220 // the set of member candidates is empty.
22221 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
22222 Lookup.suppressDiagnostics();
22223 if (Ty->isRecordType()) {
22224 // Complete the type if it can be completed.
22225 // If the type is neither complete nor being defined, bail out now.
22226 bool IsComplete = SemaRef.isCompleteType(Loc, T: Ty);
22227 auto *RD = Ty->castAsRecordDecl();
22228 if (IsComplete || RD->isBeingDefined()) {
22229 Lookup.clear();
22230 SemaRef.LookupQualifiedName(R&: Lookup, LookupCtx: RD);
22231 if (Lookup.empty()) {
22232 Lookups.emplace_back();
22233 Lookups.back().append(I: Lookup.begin(), E: Lookup.end());
22234 }
22235 }
22236 }
22237 // Perform ADL.
22238 if (SemaRef.getLangOpts().CPlusPlus)
22239 argumentDependentLookup(SemaRef, Id: ReductionId, Loc, Ty, Lookups);
22240 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
22241 Lookups, Gen: [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
22242 if (!D->isInvalidDecl() &&
22243 SemaRef.Context.hasSameType(T1: D->getType(), T2: Ty))
22244 return D;
22245 return nullptr;
22246 }))
22247 return SemaRef.BuildDeclRefExpr(D: VD, Ty: VD->getType().getNonReferenceType(),
22248 VK: VK_LValue, Loc);
22249 if (SemaRef.getLangOpts().CPlusPlus) {
22250 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
22251 Lookups, Gen: [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
22252 if (!D->isInvalidDecl() &&
22253 SemaRef.IsDerivedFrom(Loc, Derived: Ty, Base: D->getType()) &&
22254 !Ty.isMoreQualifiedThan(other: D->getType(),
22255 Ctx: SemaRef.getASTContext()))
22256 return D;
22257 return nullptr;
22258 })) {
22259 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
22260 /*DetectVirtual=*/false);
22261 if (SemaRef.IsDerivedFrom(Loc, Derived: Ty, Base: VD->getType(), Paths)) {
22262 if (!Paths.isAmbiguous(BaseType: SemaRef.Context.getCanonicalType(
22263 T: VD->getType().getUnqualifiedType()))) {
22264 if (SemaRef.CheckBaseClassAccess(
22265 AccessLoc: Loc, Base: VD->getType(), Derived: Ty, Path: Paths.front(),
22266 /*DiagID=*/0) != Sema::AR_inaccessible) {
22267 SemaRef.BuildBasePathArray(Paths, BasePath);
22268 return SemaRef.BuildDeclRefExpr(
22269 D: VD, Ty: VD->getType().getNonReferenceType(), VK: VK_LValue, Loc);
22270 }
22271 }
22272 }
22273 }
22274 }
22275 if (ReductionIdScopeSpec.isSet()) {
22276 SemaRef.Diag(Loc, DiagID: diag::err_omp_not_resolved_reduction_identifier)
22277 << Ty << Range;
22278 return ExprError();
22279 }
22280 return ExprEmpty();
22281}
22282
22283namespace {
22284/// Data for the reduction-based clauses.
22285struct ReductionData {
22286 /// List of original reduction items.
22287 SmallVector<Expr *, 8> Vars;
22288 /// List of private copies of the reduction items.
22289 SmallVector<Expr *, 8> Privates;
22290 /// LHS expressions for the reduction_op expressions.
22291 SmallVector<Expr *, 8> LHSs;
22292 /// RHS expressions for the reduction_op expressions.
22293 SmallVector<Expr *, 8> RHSs;
22294 /// Reduction operation expression.
22295 SmallVector<Expr *, 8> ReductionOps;
22296 /// inscan copy operation expressions.
22297 SmallVector<Expr *, 8> InscanCopyOps;
22298 /// inscan copy temp array expressions for prefix sums.
22299 SmallVector<Expr *, 8> InscanCopyArrayTemps;
22300 /// inscan copy temp array element expressions for prefix sums.
22301 SmallVector<Expr *, 8> InscanCopyArrayElems;
22302 /// Taskgroup descriptors for the corresponding reduction items in
22303 /// in_reduction clauses.
22304 SmallVector<Expr *, 8> TaskgroupDescriptors;
22305 /// List of captures for clause.
22306 SmallVector<Decl *, 4> ExprCaptures;
22307 /// List of postupdate expressions.
22308 SmallVector<Expr *, 4> ExprPostUpdates;
22309 /// Reduction modifier.
22310 unsigned RedModifier = 0;
22311 /// Original modifier.
22312 unsigned OrigSharingModifier = 0;
22313 /// Private Variable Reduction
22314 SmallVector<bool, 8> IsPrivateVarReduction;
22315 ReductionData() = delete;
22316 /// Reserves required memory for the reduction data.
22317 ReductionData(unsigned Size, unsigned Modifier = 0, unsigned OrgModifier = 0)
22318 : RedModifier(Modifier), OrigSharingModifier(OrgModifier) {
22319 Vars.reserve(N: Size);
22320 Privates.reserve(N: Size);
22321 LHSs.reserve(N: Size);
22322 RHSs.reserve(N: Size);
22323 ReductionOps.reserve(N: Size);
22324 IsPrivateVarReduction.reserve(N: Size);
22325 if (RedModifier == OMPC_REDUCTION_inscan) {
22326 InscanCopyOps.reserve(N: Size);
22327 InscanCopyArrayTemps.reserve(N: Size);
22328 InscanCopyArrayElems.reserve(N: Size);
22329 }
22330 TaskgroupDescriptors.reserve(N: Size);
22331 ExprCaptures.reserve(N: Size);
22332 ExprPostUpdates.reserve(N: Size);
22333 }
22334 /// Stores reduction item and reduction operation only (required for dependent
22335 /// reduction item).
22336 void push(Expr *Item, Expr *ReductionOp) {
22337 Vars.emplace_back(Args&: Item);
22338 Privates.emplace_back(Args: nullptr);
22339 LHSs.emplace_back(Args: nullptr);
22340 RHSs.emplace_back(Args: nullptr);
22341 ReductionOps.emplace_back(Args&: ReductionOp);
22342 IsPrivateVarReduction.emplace_back(Args: false);
22343 TaskgroupDescriptors.emplace_back(Args: nullptr);
22344 if (RedModifier == OMPC_REDUCTION_inscan) {
22345 InscanCopyOps.push_back(Elt: nullptr);
22346 InscanCopyArrayTemps.push_back(Elt: nullptr);
22347 InscanCopyArrayElems.push_back(Elt: nullptr);
22348 }
22349 }
22350 /// Stores reduction data.
22351 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
22352 Expr *TaskgroupDescriptor, Expr *CopyOp, Expr *CopyArrayTemp,
22353 Expr *CopyArrayElem, bool IsPrivate) {
22354 Vars.emplace_back(Args&: Item);
22355 Privates.emplace_back(Args&: Private);
22356 LHSs.emplace_back(Args&: LHS);
22357 RHSs.emplace_back(Args&: RHS);
22358 ReductionOps.emplace_back(Args&: ReductionOp);
22359 TaskgroupDescriptors.emplace_back(Args&: TaskgroupDescriptor);
22360 if (RedModifier == OMPC_REDUCTION_inscan) {
22361 InscanCopyOps.push_back(Elt: CopyOp);
22362 InscanCopyArrayTemps.push_back(Elt: CopyArrayTemp);
22363 InscanCopyArrayElems.push_back(Elt: CopyArrayElem);
22364 } else {
22365 assert(CopyOp == nullptr && CopyArrayTemp == nullptr &&
22366 CopyArrayElem == nullptr &&
22367 "Copy operation must be used for inscan reductions only.");
22368 }
22369 IsPrivateVarReduction.emplace_back(Args&: IsPrivate);
22370 }
22371};
22372} // namespace
22373
22374static bool checkOMPArraySectionConstantForReduction(
22375 ASTContext &Context, const ArraySectionExpr *OASE, bool &SingleElement,
22376 SmallVectorImpl<llvm::APSInt> &ArraySizes) {
22377 const Expr *Length = OASE->getLength();
22378 if (Length == nullptr) {
22379 // For array sections of the form [1:] or [:], we would need to analyze
22380 // the lower bound...
22381 if (OASE->getColonLocFirst().isValid())
22382 return false;
22383
22384 // This is an array subscript which has implicit length 1!
22385 SingleElement = true;
22386 ArraySizes.push_back(Elt: llvm::APSInt::get(X: 1));
22387 } else {
22388 Expr::EvalResult Result;
22389 if (!Length->EvaluateAsInt(Result, Ctx: Context))
22390 return false;
22391
22392 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
22393 SingleElement = (ConstantLengthValue.getSExtValue() == 1);
22394 ArraySizes.push_back(Elt: ConstantLengthValue);
22395 }
22396
22397 // Get the base of this array section and walk up from there.
22398 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
22399
22400 // We require length = 1 for all array sections except the right-most to
22401 // guarantee that the memory region is contiguous and has no holes in it.
22402 while (const auto *TempOASE = dyn_cast<ArraySectionExpr>(Val: Base)) {
22403 Length = TempOASE->getLength();
22404 if (Length == nullptr) {
22405 // For array sections of the form [1:] or [:], we would need to analyze
22406 // the lower bound...
22407 if (OASE->getColonLocFirst().isValid())
22408 return false;
22409
22410 // This is an array subscript which has implicit length 1!
22411 llvm::APSInt ConstantOne = llvm::APSInt::get(X: 1);
22412 ArraySizes.push_back(Elt: ConstantOne);
22413 } else {
22414 Expr::EvalResult Result;
22415 if (!Length->EvaluateAsInt(Result, Ctx: Context))
22416 return false;
22417
22418 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
22419 if (ConstantLengthValue.getSExtValue() != 1)
22420 return false;
22421
22422 ArraySizes.push_back(Elt: ConstantLengthValue);
22423 }
22424 Base = TempOASE->getBase()->IgnoreParenImpCasts();
22425 }
22426
22427 // If we have a single element, we don't need to add the implicit lengths.
22428 if (!SingleElement) {
22429 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Val: Base)) {
22430 // Has implicit length 1!
22431 llvm::APSInt ConstantOne = llvm::APSInt::get(X: 1);
22432 ArraySizes.push_back(Elt: ConstantOne);
22433 Base = TempASE->getBase()->IgnoreParenImpCasts();
22434 }
22435 }
22436
22437 // This array section can be privatized as a single value or as a constant
22438 // sized array.
22439 return true;
22440}
22441
22442static BinaryOperatorKind
22443getRelatedCompoundReductionOp(BinaryOperatorKind BOK) {
22444 if (BOK == BO_Add)
22445 return BO_AddAssign;
22446 if (BOK == BO_Mul)
22447 return BO_MulAssign;
22448 if (BOK == BO_And)
22449 return BO_AndAssign;
22450 if (BOK == BO_Or)
22451 return BO_OrAssign;
22452 if (BOK == BO_Xor)
22453 return BO_XorAssign;
22454 return BOK;
22455}
22456
22457static bool actOnOMPReductionKindClause(
22458 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
22459 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
22460 SourceLocation ColonLoc, SourceLocation EndLoc,
22461 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
22462 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
22463 DeclarationName DN = ReductionId.getName();
22464 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
22465 BinaryOperatorKind BOK = BO_Comma;
22466
22467 ASTContext &Context = S.Context;
22468 // OpenMP [2.14.3.6, reduction clause]
22469 // C
22470 // reduction-identifier is either an identifier or one of the following
22471 // operators: +, -, *, &, |, ^, && and ||
22472 // C++
22473 // reduction-identifier is either an id-expression or one of the following
22474 // operators: +, -, *, &, |, ^, && and ||
22475 switch (OOK) {
22476 case OO_Plus:
22477 BOK = BO_Add;
22478 break;
22479 case OO_Minus:
22480 // Minus(-) operator is not supported in TR11 (OpenMP 6.0). Setting BOK to
22481 // BO_Comma will automatically diagnose it for OpenMP > 52 as not allowed
22482 // reduction identifier.
22483 if (S.LangOpts.OpenMP > 52)
22484 BOK = BO_Comma;
22485 else
22486 BOK = BO_Add;
22487 break;
22488 case OO_Star:
22489 BOK = BO_Mul;
22490 break;
22491 case OO_Amp:
22492 BOK = BO_And;
22493 break;
22494 case OO_Pipe:
22495 BOK = BO_Or;
22496 break;
22497 case OO_Caret:
22498 BOK = BO_Xor;
22499 break;
22500 case OO_AmpAmp:
22501 BOK = BO_LAnd;
22502 break;
22503 case OO_PipePipe:
22504 BOK = BO_LOr;
22505 break;
22506 case OO_New:
22507 case OO_Delete:
22508 case OO_Array_New:
22509 case OO_Array_Delete:
22510 case OO_Slash:
22511 case OO_Percent:
22512 case OO_Tilde:
22513 case OO_Exclaim:
22514 case OO_Equal:
22515 case OO_Less:
22516 case OO_Greater:
22517 case OO_LessEqual:
22518 case OO_GreaterEqual:
22519 case OO_PlusEqual:
22520 case OO_MinusEqual:
22521 case OO_StarEqual:
22522 case OO_SlashEqual:
22523 case OO_PercentEqual:
22524 case OO_CaretEqual:
22525 case OO_AmpEqual:
22526 case OO_PipeEqual:
22527 case OO_LessLess:
22528 case OO_GreaterGreater:
22529 case OO_LessLessEqual:
22530 case OO_GreaterGreaterEqual:
22531 case OO_EqualEqual:
22532 case OO_ExclaimEqual:
22533 case OO_Spaceship:
22534 case OO_PlusPlus:
22535 case OO_MinusMinus:
22536 case OO_Comma:
22537 case OO_ArrowStar:
22538 case OO_Arrow:
22539 case OO_Call:
22540 case OO_Subscript:
22541 case OO_Conditional:
22542 case OO_Coawait:
22543 case NUM_OVERLOADED_OPERATORS:
22544 llvm_unreachable("Unexpected reduction identifier");
22545 case OO_None:
22546 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
22547 if (II->isStr(Str: "max"))
22548 BOK = BO_GT;
22549 else if (II->isStr(Str: "min"))
22550 BOK = BO_LT;
22551 }
22552 break;
22553 }
22554
22555 // OpenMP 5.2, 5.5.5 (see page 627, line 18) reduction Clause, Restrictions
22556 // A reduction clause with the minus (-) operator was deprecated
22557 if (OOK == OO_Minus && S.LangOpts.OpenMP == 52)
22558 S.Diag(Loc: ReductionId.getLoc(), DiagID: diag::warn_omp_minus_in_reduction_deprecated);
22559
22560 SourceRange ReductionIdRange;
22561 if (ReductionIdScopeSpec.isValid())
22562 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
22563 else
22564 ReductionIdRange.setBegin(ReductionId.getBeginLoc());
22565 ReductionIdRange.setEnd(ReductionId.getEndLoc());
22566
22567 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
22568 bool FirstIter = true;
22569 for (Expr *RefExpr : VarList) {
22570 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
22571 // OpenMP [2.1, C/C++]
22572 // A list item is a variable or array section, subject to the restrictions
22573 // specified in Section 2.4 on page 42 and in each of the sections
22574 // describing clauses and directives for which a list appears.
22575 // OpenMP [2.14.3.3, Restrictions, p.1]
22576 // A variable that is part of another variable (as an array or
22577 // structure element) cannot appear in a private clause.
22578 if (!FirstIter && IR != ER)
22579 ++IR;
22580 FirstIter = false;
22581 SourceLocation ELoc;
22582 SourceRange ERange;
22583 bool IsPrivate = false;
22584 Expr *SimpleRefExpr = RefExpr;
22585 auto Res = getPrivateItem(S, RefExpr&: SimpleRefExpr, ELoc, ERange,
22586 /*AllowArraySection=*/true);
22587 if (Res.second) {
22588 // Try to find 'declare reduction' corresponding construct before using
22589 // builtin/overloaded operators.
22590 QualType Type = Context.DependentTy;
22591 CXXCastPath BasePath;
22592 ExprResult DeclareReductionRef = buildDeclareReductionRef(
22593 SemaRef&: S, Loc: ELoc, Range: ERange, S: Stack->getCurScope(), ReductionIdScopeSpec,
22594 ReductionId, Ty: Type, BasePath, UnresolvedReduction: IR == ER ? nullptr : *IR);
22595 Expr *ReductionOp = nullptr;
22596 if (S.CurContext->isDependentContext() &&
22597 (DeclareReductionRef.isUnset() ||
22598 isa<UnresolvedLookupExpr>(Val: DeclareReductionRef.get())))
22599 ReductionOp = DeclareReductionRef.get();
22600 // It will be analyzed later.
22601 RD.push(Item: RefExpr, ReductionOp);
22602 }
22603 ValueDecl *D = Res.first;
22604 if (!D)
22605 continue;
22606
22607 Expr *TaskgroupDescriptor = nullptr;
22608 QualType Type;
22609 auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: RefExpr->IgnoreParens());
22610 auto *OASE = dyn_cast<ArraySectionExpr>(Val: RefExpr->IgnoreParens());
22611 if (ASE) {
22612 Type = ASE->getType().getNonReferenceType();
22613 } else if (OASE) {
22614 QualType BaseType =
22615 ArraySectionExpr::getBaseOriginalType(Base: OASE->getBase());
22616 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
22617 Type = ATy->getElementType();
22618 else
22619 Type = BaseType->getPointeeType();
22620 Type = Type.getNonReferenceType();
22621 } else {
22622 Type = Context.getBaseElementType(QT: D->getType().getNonReferenceType());
22623 }
22624 auto *VD = dyn_cast<VarDecl>(Val: D);
22625
22626 auto *BD = dyn_cast<BindingDecl>(Val: D);
22627 if (BD) {
22628 // FIXME: reductions on bindings are rejected.
22629 S.Diag(Loc: ELoc, DiagID: diag::err_omp_unsupported_on_binding) << 0;
22630 continue;
22631 }
22632
22633 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
22634 // A variable that appears in a private clause must not have an incomplete
22635 // type or a reference type.
22636 if (S.RequireCompleteType(Loc: ELoc, T: D->getType(),
22637 DiagID: diag::err_omp_reduction_incomplete_type))
22638 continue;
22639 // OpenMP [2.14.3.6, reduction clause, Restrictions]
22640 // A list item that appears in a reduction clause must not be
22641 // const-qualified.
22642 if (rejectConstNotMutableType(SemaRef&: S, D, Type, CKind: ClauseKind, ELoc,
22643 /*AcceptIfMutable=*/false, ListItemNotVar: ASE || OASE))
22644 continue;
22645
22646 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
22647 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
22648 // If a list-item is a reference type then it must bind to the same object
22649 // for all threads of the team.
22650 if (!ASE && !OASE) {
22651 if (VD) {
22652 VarDecl *VDDef = VD->getDefinition();
22653 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
22654 DSARefChecker Check(Stack);
22655 if (Check.Visit(S: VDDef->getInit())) {
22656 S.Diag(Loc: ELoc, DiagID: diag::err_omp_reduction_ref_type_arg)
22657 << getOpenMPClauseNameForDiag(C: ClauseKind) << ERange;
22658 S.Diag(Loc: VDDef->getLocation(), DiagID: diag::note_defined_here) << VDDef;
22659 continue;
22660 }
22661 }
22662 }
22663
22664 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
22665 // in a Construct]
22666 // Variables with the predetermined data-sharing attributes may not be
22667 // listed in data-sharing attributes clauses, except for the cases
22668 // listed below. For these exceptions only, listing a predetermined
22669 // variable in a data-sharing attribute clause is allowed and overrides
22670 // the variable's predetermined data-sharing attributes.
22671 // OpenMP [2.14.3.6, Restrictions, p.3]
22672 // Any number of reduction clauses can be specified on the directive,
22673 // but a list item can appear only once in the reduction clauses for that
22674 // directive.
22675 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
22676 if (DVar.CKind == OMPC_reduction) {
22677 S.Diag(Loc: ELoc, DiagID: diag::err_omp_once_referenced)
22678 << getOpenMPClauseNameForDiag(C: ClauseKind);
22679 if (DVar.RefExpr)
22680 S.Diag(Loc: DVar.RefExpr->getExprLoc(), DiagID: diag::note_omp_referenced);
22681 continue;
22682 }
22683 if (DVar.CKind != OMPC_unknown) {
22684 S.Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
22685 << getOpenMPClauseNameForDiag(C: DVar.CKind)
22686 << getOpenMPClauseNameForDiag(C: OMPC_reduction);
22687 reportOriginalDsa(SemaRef&: S, Stack, D, DVar);
22688 continue;
22689 }
22690
22691 // OpenMP [2.14.3.6, Restrictions, p.1]
22692 // A list item that appears in a reduction clause of a worksharing
22693 // construct must be shared in the parallel regions to which any of the
22694 // worksharing regions arising from the worksharing construct bind.
22695
22696 if (S.getLangOpts().OpenMP <= 52 &&
22697 isOpenMPWorksharingDirective(DKind: CurrDir) &&
22698 !isOpenMPParallelDirective(DKind: CurrDir) &&
22699 !isOpenMPTeamsDirective(DKind: CurrDir)) {
22700 DVar = Stack->getImplicitDSA(D, FromParent: true);
22701 if (DVar.CKind != OMPC_shared) {
22702 S.Diag(Loc: ELoc, DiagID: diag::err_omp_required_access)
22703 << getOpenMPClauseNameForDiag(C: OMPC_reduction)
22704 << getOpenMPClauseNameForDiag(C: OMPC_shared);
22705 reportOriginalDsa(SemaRef&: S, Stack, D, DVar);
22706 continue;
22707 }
22708 } else if (isOpenMPWorksharingDirective(DKind: CurrDir) &&
22709 !isOpenMPParallelDirective(DKind: CurrDir) &&
22710 !isOpenMPTeamsDirective(DKind: CurrDir)) {
22711 // OpenMP 6.0 [ 7.6.10 ]
22712 // Support Reduction over private variables with reduction clause.
22713 // A list item in a reduction clause can now be private in the enclosing
22714 // context. For orphaned constructs it is assumed to be shared unless
22715 // the original(private) modifier appears in the clause.
22716 DVar = Stack->getImplicitDSA(D, FromParent: true);
22717 // Determine if the variable should be considered private
22718 IsPrivate = DVar.CKind != OMPC_shared;
22719 bool IsOrphaned = false;
22720 OpenMPDirectiveKind ParentDir = Stack->getParentDirective();
22721 IsOrphaned = ParentDir == OMPD_unknown;
22722 if ((IsOrphaned &&
22723 RD.OrigSharingModifier == OMPC_ORIGINAL_SHARING_private))
22724 IsPrivate = true;
22725 }
22726 } else {
22727 // Threadprivates cannot be shared between threads, so dignose if the base
22728 // is a threadprivate variable.
22729 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
22730 if (DVar.CKind == OMPC_threadprivate) {
22731 S.Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
22732 << getOpenMPClauseNameForDiag(C: DVar.CKind)
22733 << getOpenMPClauseNameForDiag(C: OMPC_reduction);
22734 reportOriginalDsa(SemaRef&: S, Stack, D, DVar);
22735 continue;
22736 }
22737 }
22738
22739 // Try to find 'declare reduction' corresponding construct before using
22740 // builtin/overloaded operators.
22741 CXXCastPath BasePath;
22742 ExprResult DeclareReductionRef = buildDeclareReductionRef(
22743 SemaRef&: S, Loc: ELoc, Range: ERange, S: Stack->getCurScope(), ReductionIdScopeSpec,
22744 ReductionId, Ty: Type, BasePath, UnresolvedReduction: IR == ER ? nullptr : *IR);
22745 if (DeclareReductionRef.isInvalid())
22746 continue;
22747 if (S.CurContext->isDependentContext() &&
22748 (DeclareReductionRef.isUnset() ||
22749 isa<UnresolvedLookupExpr>(Val: DeclareReductionRef.get()))) {
22750 RD.push(Item: RefExpr, ReductionOp: DeclareReductionRef.get());
22751 // Handle non-dependent inscan reduction variables in dependent contexts.
22752 if (RD.RedModifier == OMPC_REDUCTION_inscan)
22753 Stack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_reduction, PrivateCopy: nullptr,
22754 Modifier: RD.RedModifier, AppliedToPointee: ASE || OASE);
22755 continue;
22756 }
22757 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
22758 // Not allowed reduction identifier is found.
22759 if (S.LangOpts.OpenMP > 52)
22760 S.Diag(Loc: ReductionId.getBeginLoc(),
22761 DiagID: diag::err_omp_unknown_reduction_identifier_since_omp_6_0)
22762 << Type << ReductionIdRange;
22763 else
22764 S.Diag(Loc: ReductionId.getBeginLoc(),
22765 DiagID: diag::err_omp_unknown_reduction_identifier_prior_omp_6_0)
22766 << Type << ReductionIdRange;
22767 continue;
22768 }
22769
22770 // OpenMP [2.14.3.6, reduction clause, Restrictions]
22771 // The type of a list item that appears in a reduction clause must be valid
22772 // for the reduction-identifier. For a max or min reduction in C, the type
22773 // of the list item must be an allowed arithmetic data type: char, int,
22774 // float, double, or _Bool, possibly modified with long, short, signed, or
22775 // unsigned. For a max or min reduction in C++, the type of the list item
22776 // must be an allowed arithmetic data type: char, wchar_t, int, float,
22777 // double, or bool, possibly modified with long, short, signed, or unsigned.
22778 if (DeclareReductionRef.isUnset()) {
22779 if ((BOK == BO_GT || BOK == BO_LT) &&
22780 !(Type->isScalarType() ||
22781 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
22782 S.Diag(Loc: ELoc, DiagID: diag::err_omp_clause_not_arithmetic_type_arg)
22783 << getOpenMPClauseNameForDiag(C: ClauseKind)
22784 << S.getLangOpts().CPlusPlus;
22785 if (!ASE && !OASE) {
22786 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
22787 VarDecl::DeclarationOnly;
22788 S.Diag(Loc: D->getLocation(),
22789 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
22790 << D;
22791 }
22792 continue;
22793 }
22794 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
22795 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
22796 S.Diag(Loc: ELoc, DiagID: diag::err_omp_clause_floating_type_arg)
22797 << getOpenMPClauseNameForDiag(C: ClauseKind);
22798 if (!ASE && !OASE) {
22799 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
22800 VarDecl::DeclarationOnly;
22801 S.Diag(Loc: D->getLocation(),
22802 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
22803 << D;
22804 }
22805 continue;
22806 }
22807 }
22808
22809 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
22810 VarDecl *LHSVD = buildVarDecl(SemaRef&: S, Loc: ELoc, Type, Name: ".reduction.lhs",
22811 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
22812 VarDecl *RHSVD = buildVarDecl(SemaRef&: S, Loc: ELoc, Type, Name: D->getName(),
22813 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
22814 QualType PrivateTy = Type;
22815
22816 // Try if we can determine constant lengths for all array sections and avoid
22817 // the VLA.
22818 bool ConstantLengthOASE = false;
22819 if (OASE) {
22820 bool SingleElement;
22821 llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
22822 ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
22823 Context, OASE, SingleElement, ArraySizes);
22824
22825 // If we don't have a single element, we must emit a constant array type.
22826 if (ConstantLengthOASE && !SingleElement) {
22827 for (llvm::APSInt &Size : ArraySizes)
22828 PrivateTy = Context.getConstantArrayType(EltTy: PrivateTy, ArySize: Size, SizeExpr: nullptr,
22829 ASM: ArraySizeModifier::Normal,
22830 /*IndexTypeQuals=*/0);
22831 }
22832 }
22833
22834 if ((OASE && !ConstantLengthOASE) ||
22835 (!OASE && !ASE &&
22836 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
22837 if (!Context.getTargetInfo().isVLASupported()) {
22838 if (isOpenMPTargetExecutionDirective(DKind: Stack->getCurrentDirective())) {
22839 S.Diag(Loc: ELoc, DiagID: diag::err_omp_reduction_vla_unsupported) << !!OASE;
22840 S.Diag(Loc: ELoc, DiagID: diag::note_vla_unsupported);
22841 continue;
22842 } else {
22843 S.targetDiag(Loc: ELoc, DiagID: diag::err_omp_reduction_vla_unsupported) << !!OASE;
22844 S.targetDiag(Loc: ELoc, DiagID: diag::note_vla_unsupported);
22845 }
22846 }
22847 // For arrays/array sections only:
22848 // Create pseudo array type for private copy. The size for this array will
22849 // be generated during codegen.
22850 // For array subscripts or single variables Private Ty is the same as Type
22851 // (type of the variable or single array element).
22852 PrivateTy = Context.getVariableArrayType(
22853 EltTy: Type,
22854 NumElts: new (Context)
22855 OpaqueValueExpr(ELoc, Context.getSizeType(), VK_PRValue),
22856 ASM: ArraySizeModifier::Normal, /*IndexTypeQuals=*/0);
22857 } else if (!ASE && !OASE &&
22858 Context.getAsArrayType(T: D->getType().getNonReferenceType())) {
22859 PrivateTy = D->getType().getNonReferenceType();
22860 }
22861 // Private copy.
22862 VarDecl *PrivateVD =
22863 buildVarDecl(SemaRef&: S, Loc: ELoc, Type: PrivateTy, Name: D->getName(),
22864 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr,
22865 OrigRef: VD ? cast<DeclRefExpr>(Val: SimpleRefExpr) : nullptr);
22866 // Add initializer for private variable.
22867 Expr *Init = nullptr;
22868 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, D: LHSVD, Ty: Type, Loc: ELoc);
22869 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, D: RHSVD, Ty: Type, Loc: ELoc);
22870 if (DeclareReductionRef.isUsable()) {
22871 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
22872 auto *DRD = cast<OMPDeclareReductionDecl>(Val: DRDRef->getDecl());
22873 if (DRD->getInitializer()) {
22874 Init = DRDRef;
22875 RHSVD->setInit(DRDRef);
22876 RHSVD->setInitStyle(VarDecl::CallInit);
22877 }
22878 } else {
22879 switch (BOK) {
22880 case BO_Add:
22881 case BO_Xor:
22882 case BO_Or:
22883 case BO_LOr:
22884 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
22885 if (Type->isScalarType() || Type->isAnyComplexType())
22886 Init = S.ActOnIntegerConstant(Loc: ELoc, /*Val=*/0).get();
22887 break;
22888 case BO_Mul:
22889 // '*' reduction op - initializer is '1'.
22890 // For C++ class types (e.g. std::complex) the OpenMP built-in
22891 // reduction identifiers are an extension: the standard only defines
22892 // identities for arithmetic (and, in Clang, _Complex) types. Without
22893 // an explicit initializer the private copy would be value-initialized,
22894 // which yields the *additive* identity (e.g. std::complex(0,0)) and is
22895 // wrong for multiplication. Initialize from the integer literal '1'
22896 // instead and let the converting constructor build the multiplicative
22897 // identity (e.g. std::complex(1) == (1,0)).
22898 if (Type->isScalarType() || Type->isAnyComplexType()) {
22899 Init = S.ActOnIntegerConstant(Loc: ELoc, /*Val=*/1).get();
22900 } else if (S.getLangOpts().CPlusPlus && Type->isRecordType()) {
22901 // Only use '1' when the type is actually copy-initializable from it.
22902 // Otherwise fall back to value-initialization (the previous behavior)
22903 // rather than rejecting the reduction, so a class that used to
22904 // compile keeps compiling. Such a class keeps its (possibly
22905 // incorrect) value-initialized identity, matching the pre-existing
22906 // behavior; BO_Add likewise relies on value-initialization for class
22907 // types.
22908 Expr *One = S.ActOnIntegerConstant(Loc: ELoc, /*Val=*/1).get();
22909 InitializedEntity Entity =
22910 InitializedEntity::InitializeTemporary(Type);
22911 InitializationKind Kind = InitializationKind::CreateCopy(InitLoc: ELoc, EqualLoc: ELoc);
22912 InitializationSequence Seq(S, Entity, Kind, One);
22913 if (Seq)
22914 Init = One;
22915 }
22916 break;
22917 case BO_LAnd:
22918 if (Type->isScalarType() || Type->isAnyComplexType()) {
22919 // '&&' reduction ops - initializer is '1'.
22920 Init = S.ActOnIntegerConstant(Loc: ELoc, /*Val=*/1).get();
22921 }
22922 break;
22923 case BO_And: {
22924 // '&' reduction op - initializer is '~0'.
22925 QualType OrigType = Type;
22926 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
22927 Type = ComplexTy->getElementType();
22928 if (Type->isRealFloatingType()) {
22929 llvm::APFloat InitValue = llvm::APFloat::getAllOnesValue(
22930 Semantics: Context.getFloatTypeSemantics(T: Type));
22931 Init = FloatingLiteral::Create(C: Context, V: InitValue, /*isexact=*/true,
22932 Type, L: ELoc);
22933 } else if (Type->isScalarType()) {
22934 uint64_t Size = Context.getTypeSize(T: Type);
22935 QualType IntTy = Context.getIntTypeForBitwidth(DestWidth: Size, /*Signed=*/0);
22936 llvm::APInt InitValue = llvm::APInt::getAllOnes(numBits: Size);
22937 Init = IntegerLiteral::Create(C: Context, V: InitValue, type: IntTy, l: ELoc);
22938 }
22939 if (Init && OrigType->isAnyComplexType()) {
22940 // Init = 0xFFFF + 0xFFFFi;
22941 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
22942 Init = S.CreateBuiltinBinOp(OpLoc: ELoc, Opc: BO_Add, LHSExpr: Init, RHSExpr: Im).get();
22943 }
22944 Type = OrigType;
22945 break;
22946 }
22947 case BO_LT:
22948 case BO_GT: {
22949 // 'min' reduction op - initializer is 'Largest representable number in
22950 // the reduction list item type'.
22951 // 'max' reduction op - initializer is 'Least representable number in
22952 // the reduction list item type'.
22953 if (Type->isIntegerType() || Type->isPointerType()) {
22954 bool IsSigned = Type->hasSignedIntegerRepresentation();
22955 uint64_t Size = Context.getTypeSize(T: Type);
22956 QualType IntTy =
22957 Context.getIntTypeForBitwidth(DestWidth: Size, /*Signed=*/IsSigned);
22958 llvm::APInt InitValue =
22959 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(numBits: Size)
22960 : llvm::APInt::getMinValue(numBits: Size)
22961 : IsSigned ? llvm::APInt::getSignedMaxValue(numBits: Size)
22962 : llvm::APInt::getMaxValue(numBits: Size);
22963 Init = IntegerLiteral::Create(C: Context, V: InitValue, type: IntTy, l: ELoc);
22964 if (Type->isPointerType()) {
22965 // Cast to pointer type.
22966 ExprResult CastExpr = S.BuildCStyleCastExpr(
22967 LParenLoc: ELoc, Ty: Context.getTrivialTypeSourceInfo(T: Type, Loc: ELoc), RParenLoc: ELoc, Op: Init);
22968 if (CastExpr.isInvalid())
22969 continue;
22970 Init = CastExpr.get();
22971 }
22972 } else if (Type->isRealFloatingType()) {
22973 llvm::APFloat InitValue = llvm::APFloat::getLargest(
22974 Sem: Context.getFloatTypeSemantics(T: Type), Negative: BOK != BO_LT);
22975 Init = FloatingLiteral::Create(C: Context, V: InitValue, /*isexact=*/true,
22976 Type, L: ELoc);
22977 }
22978 break;
22979 }
22980 case BO_PtrMemD:
22981 case BO_PtrMemI:
22982 case BO_MulAssign:
22983 case BO_Div:
22984 case BO_Rem:
22985 case BO_Sub:
22986 case BO_Shl:
22987 case BO_Shr:
22988 case BO_LE:
22989 case BO_GE:
22990 case BO_EQ:
22991 case BO_NE:
22992 case BO_Cmp:
22993 case BO_AndAssign:
22994 case BO_XorAssign:
22995 case BO_OrAssign:
22996 case BO_Assign:
22997 case BO_AddAssign:
22998 case BO_SubAssign:
22999 case BO_DivAssign:
23000 case BO_RemAssign:
23001 case BO_ShlAssign:
23002 case BO_ShrAssign:
23003 case BO_Comma:
23004 llvm_unreachable("Unexpected reduction operation");
23005 }
23006 }
23007 if (Init && DeclareReductionRef.isUnset()) {
23008 S.AddInitializerToDecl(dcl: RHSVD, init: Init, /*DirectInit=*/false);
23009 // Store initializer for single element in private copy. Will be used
23010 // during codegen.
23011 PrivateVD->setInit(RHSVD->getInit());
23012 PrivateVD->setInitStyle(RHSVD->getInitStyle());
23013 } else if (!Init) {
23014 S.ActOnUninitializedDecl(dcl: RHSVD);
23015 // Store initializer for single element in private copy. Will be used
23016 // during codegen.
23017 PrivateVD->setInit(RHSVD->getInit());
23018 PrivateVD->setInitStyle(RHSVD->getInitStyle());
23019 }
23020 if (RHSVD->isInvalidDecl())
23021 continue;
23022 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
23023 S.Diag(Loc: ELoc, DiagID: diag::err_omp_reduction_id_not_compatible)
23024 << Type << ReductionIdRange;
23025 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
23026 VarDecl::DeclarationOnly;
23027 S.Diag(Loc: D->getLocation(),
23028 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
23029 << D;
23030 continue;
23031 }
23032 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, D: PrivateVD, Ty: PrivateTy, Loc: ELoc);
23033 ExprResult ReductionOp;
23034 if (DeclareReductionRef.isUsable()) {
23035 QualType RedTy = DeclareReductionRef.get()->getType();
23036 QualType PtrRedTy = Context.getPointerType(T: RedTy);
23037 ExprResult LHS = S.CreateBuiltinUnaryOp(OpLoc: ELoc, Opc: UO_AddrOf, InputExpr: LHSDRE);
23038 ExprResult RHS = S.CreateBuiltinUnaryOp(OpLoc: ELoc, Opc: UO_AddrOf, InputExpr: RHSDRE);
23039 if (!BasePath.empty()) {
23040 LHS = S.DefaultLvalueConversion(E: LHS.get());
23041 RHS = S.DefaultLvalueConversion(E: RHS.get());
23042 LHS = ImplicitCastExpr::Create(
23043 Context, T: PtrRedTy, Kind: CK_UncheckedDerivedToBase, Operand: LHS.get(), BasePath: &BasePath,
23044 Cat: LHS.get()->getValueKind(), FPO: FPOptionsOverride());
23045 RHS = ImplicitCastExpr::Create(
23046 Context, T: PtrRedTy, Kind: CK_UncheckedDerivedToBase, Operand: RHS.get(), BasePath: &BasePath,
23047 Cat: RHS.get()->getValueKind(), FPO: FPOptionsOverride());
23048 }
23049 FunctionProtoType::ExtProtoInfo EPI;
23050 QualType Params[] = {PtrRedTy, PtrRedTy};
23051 QualType FnTy = Context.getFunctionType(ResultTy: Context.VoidTy, Args: Params, EPI);
23052 auto *OVE = new (Context) OpaqueValueExpr(
23053 ELoc, Context.getPointerType(T: FnTy), VK_PRValue, OK_Ordinary,
23054 S.DefaultLvalueConversion(E: DeclareReductionRef.get()).get());
23055 Expr *Args[] = {LHS.get(), RHS.get()};
23056 ReductionOp =
23057 CallExpr::Create(Ctx: Context, Fn: OVE, Args, Ty: Context.VoidTy, VK: VK_PRValue, RParenLoc: ELoc,
23058 FPFeatures: S.CurFPFeatureOverrides());
23059 } else {
23060 BinaryOperatorKind CombBOK = getRelatedCompoundReductionOp(BOK);
23061 if (Type->isRecordType() && CombBOK != BOK) {
23062 Sema::TentativeAnalysisScope Trap(S);
23063 ReductionOp =
23064 S.BuildBinOp(S: Stack->getCurScope(), OpLoc: ReductionId.getBeginLoc(),
23065 Opc: CombBOK, LHSExpr: LHSDRE, RHSExpr: RHSDRE);
23066 }
23067 if (!ReductionOp.isUsable()) {
23068 ReductionOp =
23069 S.BuildBinOp(S: Stack->getCurScope(), OpLoc: ReductionId.getBeginLoc(), Opc: BOK,
23070 LHSExpr: LHSDRE, RHSExpr: RHSDRE);
23071 if (ReductionOp.isUsable()) {
23072 if (BOK != BO_LT && BOK != BO_GT) {
23073 ReductionOp =
23074 S.BuildBinOp(S: Stack->getCurScope(), OpLoc: ReductionId.getBeginLoc(),
23075 Opc: BO_Assign, LHSExpr: LHSDRE, RHSExpr: ReductionOp.get());
23076 } else {
23077 auto *ConditionalOp = new (Context)
23078 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc,
23079 RHSDRE, Type, VK_LValue, OK_Ordinary);
23080 ReductionOp =
23081 S.BuildBinOp(S: Stack->getCurScope(), OpLoc: ReductionId.getBeginLoc(),
23082 Opc: BO_Assign, LHSExpr: LHSDRE, RHSExpr: ConditionalOp);
23083 }
23084 }
23085 }
23086 if (ReductionOp.isUsable())
23087 ReductionOp = S.ActOnFinishFullExpr(Expr: ReductionOp.get(),
23088 /*DiscardedValue=*/false);
23089 if (!ReductionOp.isUsable())
23090 continue;
23091 }
23092
23093 // Add copy operations for inscan reductions.
23094 // LHS = RHS;
23095 ExprResult CopyOpRes, TempArrayRes, TempArrayElem;
23096 if (ClauseKind == OMPC_reduction &&
23097 RD.RedModifier == OMPC_REDUCTION_inscan) {
23098 ExprResult RHS = S.DefaultLvalueConversion(E: RHSDRE);
23099 CopyOpRes = S.BuildBinOp(S: Stack->getCurScope(), OpLoc: ELoc, Opc: BO_Assign, LHSExpr: LHSDRE,
23100 RHSExpr: RHS.get());
23101 if (!CopyOpRes.isUsable())
23102 continue;
23103 CopyOpRes =
23104 S.ActOnFinishFullExpr(Expr: CopyOpRes.get(), /*DiscardedValue=*/true);
23105 if (!CopyOpRes.isUsable())
23106 continue;
23107 // For simd directive and simd-based directives in simd mode no need to
23108 // construct temp array, need just a single temp element.
23109 if (Stack->getCurrentDirective() == OMPD_simd ||
23110 (S.getLangOpts().OpenMPSimd &&
23111 isOpenMPSimdDirective(DKind: Stack->getCurrentDirective()))) {
23112 VarDecl *TempArrayVD =
23113 buildVarDecl(SemaRef&: S, Loc: ELoc, Type: PrivateTy, Name: D->getName(),
23114 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
23115 // Add a constructor to the temp decl.
23116 S.ActOnUninitializedDecl(dcl: TempArrayVD);
23117 TempArrayRes = buildDeclRefExpr(S, D: TempArrayVD, Ty: PrivateTy, Loc: ELoc);
23118 } else {
23119 // Build temp array for prefix sum.
23120 auto *Dim = new (S.Context)
23121 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_PRValue);
23122 QualType ArrayTy = S.Context.getVariableArrayType(
23123 EltTy: PrivateTy, NumElts: Dim, ASM: ArraySizeModifier::Normal,
23124 /*IndexTypeQuals=*/0);
23125 VarDecl *TempArrayVD =
23126 buildVarDecl(SemaRef&: S, Loc: ELoc, Type: ArrayTy, Name: D->getName(),
23127 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
23128 // Add a constructor to the temp decl.
23129 S.ActOnUninitializedDecl(dcl: TempArrayVD);
23130 TempArrayRes = buildDeclRefExpr(S, D: TempArrayVD, Ty: ArrayTy, Loc: ELoc);
23131 TempArrayElem =
23132 S.DefaultFunctionArrayLvalueConversion(E: TempArrayRes.get());
23133 auto *Idx = new (S.Context)
23134 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_PRValue);
23135 TempArrayElem = S.CreateBuiltinArraySubscriptExpr(Base: TempArrayElem.get(),
23136 LLoc: ELoc, Idx, RLoc: ELoc);
23137 }
23138 }
23139
23140 // OpenMP [2.15.4.6, Restrictions, p.2]
23141 // A list item that appears in an in_reduction clause of a task construct
23142 // must appear in a task_reduction clause of a construct associated with a
23143 // taskgroup region that includes the participating task in its taskgroup
23144 // set. The construct associated with the innermost region that meets this
23145 // condition must specify the same reduction-identifier as the in_reduction
23146 // clause.
23147 if (ClauseKind == OMPC_in_reduction) {
23148 SourceRange ParentSR;
23149 BinaryOperatorKind ParentBOK;
23150 const Expr *ParentReductionOp = nullptr;
23151 Expr *ParentBOKTD = nullptr, *ParentReductionOpTD = nullptr;
23152 DSAStackTy::DSAVarData ParentBOKDSA =
23153 Stack->getTopMostTaskgroupReductionData(D, SR&: ParentSR, BOK&: ParentBOK,
23154 TaskgroupDescriptor&: ParentBOKTD);
23155 DSAStackTy::DSAVarData ParentReductionOpDSA =
23156 Stack->getTopMostTaskgroupReductionData(
23157 D, SR&: ParentSR, ReductionRef&: ParentReductionOp, TaskgroupDescriptor&: ParentReductionOpTD);
23158 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
23159 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
23160 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
23161 (DeclareReductionRef.isUsable() && IsParentBOK) ||
23162 (IsParentBOK && BOK != ParentBOK) || IsParentReductionOp) {
23163 bool EmitError = true;
23164 if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
23165 llvm::FoldingSetNodeID RedId, ParentRedId;
23166 ParentReductionOp->Profile(ID&: ParentRedId, Context, /*Canonical=*/true);
23167 DeclareReductionRef.get()->Profile(ID&: RedId, Context,
23168 /*Canonical=*/true);
23169 EmitError = RedId != ParentRedId;
23170 }
23171 if (EmitError) {
23172 S.Diag(Loc: ReductionId.getBeginLoc(),
23173 DiagID: diag::err_omp_reduction_identifier_mismatch)
23174 << ReductionIdRange << RefExpr->getSourceRange();
23175 S.Diag(Loc: ParentSR.getBegin(),
23176 DiagID: diag::note_omp_previous_reduction_identifier)
23177 << ParentSR
23178 << (IsParentBOK ? ParentBOKDSA.RefExpr
23179 : ParentReductionOpDSA.RefExpr)
23180 ->getSourceRange();
23181 continue;
23182 }
23183 }
23184 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
23185 }
23186
23187 DeclRefExpr *Ref = nullptr;
23188 Expr *VarsExpr = RefExpr->IgnoreParens();
23189 bool IsBindingDecl = isa<BindingDecl>(Val: D);
23190 if (!VD && !IsBindingDecl && !S.CurContext->isDependentContext()) {
23191 if (ASE || OASE) {
23192 TransformExprToCaptures RebuildToCapture(S, D);
23193 VarsExpr =
23194 RebuildToCapture.TransformExpr(E: RefExpr->IgnoreParens()).get();
23195 Ref = RebuildToCapture.getCapturedExpr();
23196 } else {
23197 VarsExpr = Ref = buildCapture(S, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/false);
23198 }
23199 if (!S.OpenMP().isOpenMPCapturedDecl(D)) {
23200 RD.ExprCaptures.emplace_back(Args: Ref->getDecl());
23201 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
23202 ExprResult RefRes = S.DefaultLvalueConversion(E: Ref);
23203 if (!RefRes.isUsable())
23204 continue;
23205 ExprResult PostUpdateRes =
23206 S.BuildBinOp(S: Stack->getCurScope(), OpLoc: ELoc, Opc: BO_Assign, LHSExpr: SimpleRefExpr,
23207 RHSExpr: RefRes.get());
23208 if (!PostUpdateRes.isUsable())
23209 continue;
23210 if (isOpenMPTaskingDirective(Kind: Stack->getCurrentDirective()) ||
23211 Stack->getCurrentDirective() == OMPD_taskgroup) {
23212 S.Diag(Loc: RefExpr->getExprLoc(),
23213 DiagID: diag::err_omp_reduction_non_addressable_expression)
23214 << RefExpr->getSourceRange();
23215 continue;
23216 }
23217 RD.ExprPostUpdates.emplace_back(
23218 Args: S.IgnoredValueConversions(E: PostUpdateRes.get()).get());
23219 }
23220 }
23221 }
23222 // All reduction items are still marked as reduction (to do not increase
23223 // code base size).
23224 unsigned Modifier = RD.RedModifier;
23225 // Consider task_reductions as reductions with task modifier. Required for
23226 // correct analysis of in_reduction clauses.
23227 if (CurrDir == OMPD_taskgroup && ClauseKind == OMPC_task_reduction)
23228 Modifier = OMPC_REDUCTION_task;
23229 Stack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_reduction, PrivateCopy: Ref, Modifier,
23230 AppliedToPointee: ASE || OASE);
23231 if (Modifier == OMPC_REDUCTION_task &&
23232 (CurrDir == OMPD_taskgroup ||
23233 ((isOpenMPParallelDirective(DKind: CurrDir) ||
23234 isOpenMPWorksharingDirective(DKind: CurrDir)) &&
23235 !isOpenMPSimdDirective(DKind: CurrDir)))) {
23236 if (DeclareReductionRef.isUsable())
23237 Stack->addTaskgroupReductionData(D, SR: ReductionIdRange,
23238 ReductionRef: DeclareReductionRef.get());
23239 else
23240 Stack->addTaskgroupReductionData(D, SR: ReductionIdRange, BOK);
23241 }
23242 RD.push(Item: VarsExpr, Private: PrivateDRE, LHS: LHSDRE, RHS: RHSDRE, ReductionOp: ReductionOp.get(),
23243 TaskgroupDescriptor, CopyOp: CopyOpRes.get(), CopyArrayTemp: TempArrayRes.get(),
23244 CopyArrayElem: TempArrayElem.get(), IsPrivate);
23245 }
23246 return RD.Vars.empty();
23247}
23248
23249OMPClause *SemaOpenMP::ActOnOpenMPReductionClause(
23250 ArrayRef<Expr *> VarList,
23251 OpenMPVarListDataTy::OpenMPReductionClauseModifiers Modifiers,
23252 SourceLocation StartLoc, SourceLocation LParenLoc,
23253 SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc,
23254 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
23255 ArrayRef<Expr *> UnresolvedReductions) {
23256 OpenMPReductionClauseModifier Modifier =
23257 static_cast<OpenMPReductionClauseModifier>(Modifiers.ExtraModifier);
23258 OpenMPOriginalSharingModifier OriginalSharingModifier =
23259 static_cast<OpenMPOriginalSharingModifier>(
23260 Modifiers.OriginalSharingModifier);
23261 if (ModifierLoc.isValid() && Modifier == OMPC_REDUCTION_unknown) {
23262 Diag(Loc: LParenLoc, DiagID: diag::err_omp_unexpected_clause_value)
23263 << getListOfPossibleValues(K: OMPC_reduction, /*First=*/0,
23264 /*Last=*/OMPC_REDUCTION_unknown)
23265 << getOpenMPClauseNameForDiag(C: OMPC_reduction);
23266 return nullptr;
23267 }
23268 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions
23269 // A reduction clause with the inscan reduction-modifier may only appear on a
23270 // worksharing-loop construct, a worksharing-loop SIMD construct, a simd
23271 // construct, a parallel worksharing-loop construct or a parallel
23272 // worksharing-loop SIMD construct.
23273 if (Modifier == OMPC_REDUCTION_inscan &&
23274 (DSAStack->getCurrentDirective() != OMPD_for &&
23275 DSAStack->getCurrentDirective() != OMPD_for_simd &&
23276 DSAStack->getCurrentDirective() != OMPD_simd &&
23277 DSAStack->getCurrentDirective() != OMPD_parallel_for &&
23278 DSAStack->getCurrentDirective() != OMPD_parallel_for_simd)) {
23279 Diag(Loc: ModifierLoc, DiagID: diag::err_omp_wrong_inscan_reduction);
23280 return nullptr;
23281 }
23282 ReductionData RD(VarList.size(), Modifier, OriginalSharingModifier);
23283 if (actOnOMPReductionKindClause(S&: SemaRef, DSAStack, ClauseKind: OMPC_reduction, VarList,
23284 StartLoc, LParenLoc, ColonLoc, EndLoc,
23285 ReductionIdScopeSpec, ReductionId,
23286 UnresolvedReductions, RD))
23287 return nullptr;
23288
23289 return OMPReductionClause::Create(
23290 C: getASTContext(), StartLoc, LParenLoc, ModifierLoc, ColonLoc, EndLoc,
23291 Modifier, VL: RD.Vars,
23292 QualifierLoc: ReductionIdScopeSpec.getWithLocInContext(Context&: getASTContext()), NameInfo: ReductionId,
23293 Privates: RD.Privates, LHSExprs: RD.LHSs, RHSExprs: RD.RHSs, ReductionOps: RD.ReductionOps, CopyOps: RD.InscanCopyOps,
23294 CopyArrayTemps: RD.InscanCopyArrayTemps, CopyArrayElems: RD.InscanCopyArrayElems,
23295 PreInit: buildPreInits(Context&: getASTContext(), PreInits: RD.ExprCaptures),
23296 PostUpdate: buildPostUpdate(S&: SemaRef, PostUpdates: RD.ExprPostUpdates), IsPrivateVarReduction: RD.IsPrivateVarReduction,
23297 OriginalSharingModifier);
23298}
23299
23300OMPClause *SemaOpenMP::ActOnOpenMPTaskReductionClause(
23301 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
23302 SourceLocation ColonLoc, SourceLocation EndLoc,
23303 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
23304 ArrayRef<Expr *> UnresolvedReductions) {
23305 ReductionData RD(VarList.size());
23306 if (actOnOMPReductionKindClause(S&: SemaRef, DSAStack, ClauseKind: OMPC_task_reduction,
23307 VarList, StartLoc, LParenLoc, ColonLoc,
23308 EndLoc, ReductionIdScopeSpec, ReductionId,
23309 UnresolvedReductions, RD))
23310 return nullptr;
23311
23312 return OMPTaskReductionClause::Create(
23313 C: getASTContext(), StartLoc, LParenLoc, ColonLoc, EndLoc, VL: RD.Vars,
23314 QualifierLoc: ReductionIdScopeSpec.getWithLocInContext(Context&: getASTContext()), NameInfo: ReductionId,
23315 Privates: RD.Privates, LHSExprs: RD.LHSs, RHSExprs: RD.RHSs, ReductionOps: RD.ReductionOps,
23316 PreInit: buildPreInits(Context&: getASTContext(), PreInits: RD.ExprCaptures),
23317 PostUpdate: buildPostUpdate(S&: SemaRef, PostUpdates: RD.ExprPostUpdates));
23318}
23319
23320OMPClause *SemaOpenMP::ActOnOpenMPInReductionClause(
23321 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
23322 SourceLocation ColonLoc, SourceLocation EndLoc,
23323 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
23324 ArrayRef<Expr *> UnresolvedReductions) {
23325 ReductionData RD(VarList.size());
23326 if (actOnOMPReductionKindClause(S&: SemaRef, DSAStack, ClauseKind: OMPC_in_reduction, VarList,
23327 StartLoc, LParenLoc, ColonLoc, EndLoc,
23328 ReductionIdScopeSpec, ReductionId,
23329 UnresolvedReductions, RD))
23330 return nullptr;
23331
23332 return OMPInReductionClause::Create(
23333 C: getASTContext(), StartLoc, LParenLoc, ColonLoc, EndLoc, VL: RD.Vars,
23334 QualifierLoc: ReductionIdScopeSpec.getWithLocInContext(Context&: getASTContext()), NameInfo: ReductionId,
23335 Privates: RD.Privates, LHSExprs: RD.LHSs, RHSExprs: RD.RHSs, ReductionOps: RD.ReductionOps, TaskgroupDescriptors: RD.TaskgroupDescriptors,
23336 PreInit: buildPreInits(Context&: getASTContext(), PreInits: RD.ExprCaptures),
23337 PostUpdate: buildPostUpdate(S&: SemaRef, PostUpdates: RD.ExprPostUpdates));
23338}
23339
23340bool SemaOpenMP::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
23341 SourceLocation LinLoc) {
23342 if ((!getLangOpts().CPlusPlus && LinKind != OMPC_LINEAR_val) ||
23343 LinKind == OMPC_LINEAR_unknown || LinKind == OMPC_LINEAR_step) {
23344 Diag(Loc: LinLoc, DiagID: diag::err_omp_wrong_linear_modifier)
23345 << getLangOpts().CPlusPlus;
23346 return true;
23347 }
23348 return false;
23349}
23350
23351bool SemaOpenMP::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
23352 OpenMPLinearClauseKind LinKind,
23353 QualType Type, bool IsDeclareSimd) {
23354 const auto *VD = dyn_cast_or_null<VarDecl>(Val: D);
23355 // A variable must not have an incomplete type or a reference type.
23356 if (SemaRef.RequireCompleteType(Loc: ELoc, T: Type,
23357 DiagID: diag::err_omp_linear_incomplete_type))
23358 return true;
23359 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
23360 !Type->isReferenceType()) {
23361 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_linear_modifier_non_reference)
23362 << Type << getOpenMPSimpleClauseTypeName(Kind: OMPC_linear, Type: LinKind);
23363 return true;
23364 }
23365 Type = Type.getNonReferenceType();
23366
23367 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
23368 // A variable that is privatized must not have a const-qualified type
23369 // unless it is of class type with a mutable member. This restriction does
23370 // not apply to the firstprivate clause, nor to the linear clause on
23371 // declarative directives (like declare simd).
23372 if (!IsDeclareSimd &&
23373 rejectConstNotMutableType(SemaRef, D, Type, CKind: OMPC_linear, ELoc))
23374 return true;
23375
23376 // A list item must be of integral or pointer type.
23377 Type = Type.getUnqualifiedType().getCanonicalType();
23378 const auto *Ty = Type.getTypePtrOrNull();
23379 if (!Ty || (LinKind != OMPC_LINEAR_ref && !Ty->isDependentType() &&
23380 !Ty->isIntegralType(Ctx: getASTContext()) && !Ty->isPointerType())) {
23381 Diag(Loc: ELoc, DiagID: diag::err_omp_linear_expected_int_or_ptr) << Type;
23382 if (D) {
23383 bool IsDecl = !VD || VD->isThisDeclarationADefinition(getASTContext()) ==
23384 VarDecl::DeclarationOnly;
23385 Diag(Loc: D->getLocation(),
23386 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
23387 << D;
23388 }
23389 return true;
23390 }
23391 return false;
23392}
23393
23394OMPClause *SemaOpenMP::ActOnOpenMPLinearClause(
23395 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
23396 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
23397 SourceLocation LinLoc, SourceLocation ColonLoc,
23398 SourceLocation StepModifierLoc, SourceLocation EndLoc) {
23399 SmallVector<Expr *, 8> Vars;
23400 SmallVector<Expr *, 8> Privates;
23401 SmallVector<Expr *, 8> Inits;
23402 SmallVector<Decl *, 4> ExprCaptures;
23403 SmallVector<Expr *, 4> ExprPostUpdates;
23404 // OpenMP 5.2 [Section 5.4.6, linear clause]
23405 // step-simple-modifier is exclusive, can't be used with 'val', 'uval', or
23406 // 'ref'
23407 if (LinLoc.isValid() && StepModifierLoc.isInvalid() && Step &&
23408 getLangOpts().OpenMP >= 52)
23409 Diag(Loc: Step->getBeginLoc(), DiagID: diag::err_omp_step_simple_modifier_exclusive);
23410 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
23411 LinKind = OMPC_LINEAR_val;
23412 for (Expr *RefExpr : VarList) {
23413 assert(RefExpr && "NULL expr in OpenMP linear clause.");
23414 SourceLocation ELoc;
23415 SourceRange ERange;
23416 Expr *SimpleRefExpr = RefExpr;
23417 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
23418 if (Res.second) {
23419 // It will be analyzed later.
23420 Vars.push_back(Elt: RefExpr);
23421 Privates.push_back(Elt: nullptr);
23422 Inits.push_back(Elt: nullptr);
23423 }
23424 ValueDecl *D = Res.first;
23425 if (!D)
23426 continue;
23427
23428 // Linear on bindings only works for simple simd, not parallel constructs.
23429 if (isa<BindingDecl>(Val: D)) {
23430 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
23431 if (isOpenMPParallelDirective(DKind)) {
23432 Diag(Loc: ELoc, DiagID: diag::err_omp_unsupported_on_binding) << 2;
23433 continue;
23434 }
23435 }
23436
23437 QualType Type = D->getType();
23438 auto *VD = dyn_cast<VarDecl>(Val: D);
23439
23440 // OpenMP [2.14.3.7, linear clause]
23441 // A list-item cannot appear in more than one linear clause.
23442 // A list-item that appears in a linear clause cannot appear in any
23443 // other data-sharing attribute clause.
23444 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
23445 if (DVar.RefExpr) {
23446 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
23447 << getOpenMPClauseNameForDiag(C: DVar.CKind)
23448 << getOpenMPClauseNameForDiag(C: OMPC_linear);
23449 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
23450 continue;
23451 }
23452
23453 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
23454 continue;
23455 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
23456
23457 // Build private copy of original var.
23458 VarDecl *Private =
23459 buildVarDecl(SemaRef, Loc: ELoc, Type, Name: D->getName(),
23460 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr,
23461 OrigRef: VD ? cast<DeclRefExpr>(Val: SimpleRefExpr) : nullptr);
23462 DeclRefExpr *PrivateRef = buildDeclRefExpr(S&: SemaRef, D: Private, Ty: Type, Loc: ELoc);
23463 // Build var to save initial value.
23464 VarDecl *Init = buildVarDecl(SemaRef, Loc: ELoc, Type, Name: ".linear.start");
23465 DeclRefExpr *Ref = nullptr;
23466 bool IsBindingDecl = isa<BindingDecl>(Val: D);
23467 if (!VD && !IsBindingDecl && !SemaRef.CurContext->isDependentContext()) {
23468 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/false);
23469 if (!isOpenMPCapturedDecl(D)) {
23470 ExprCaptures.push_back(Elt: Ref->getDecl());
23471 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
23472 ExprResult RefRes = SemaRef.DefaultLvalueConversion(E: Ref);
23473 if (!RefRes.isUsable())
23474 continue;
23475 ExprResult PostUpdateRes =
23476 SemaRef.BuildBinOp(DSAStack->getCurScope(), OpLoc: ELoc, Opc: BO_Assign,
23477 LHSExpr: SimpleRefExpr, RHSExpr: RefRes.get());
23478 if (!PostUpdateRes.isUsable())
23479 continue;
23480 ExprPostUpdates.push_back(
23481 Elt: SemaRef.IgnoredValueConversions(E: PostUpdateRes.get()).get());
23482 }
23483 }
23484 }
23485 // Read the initial value through the list item itself for every modifier;
23486 // a 'uval' item is a reference whose referenced value is linear, exactly as
23487 // for 'val', and need not have an initializer (e.g. a reference parameter).
23488 Expr *InitExpr = Ref ? Ref : SimpleRefExpr;
23489 SemaRef.AddInitializerToDecl(
23490 dcl: Init, init: SemaRef.DefaultLvalueConversion(E: InitExpr).get(),
23491 /*DirectInit=*/false);
23492 DeclRefExpr *InitRef = buildDeclRefExpr(S&: SemaRef, D: Init, Ty: Type, Loc: ELoc);
23493
23494 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_linear, PrivateCopy: Ref);
23495 Vars.push_back(
23496 Elt: (VD || IsBindingDecl || SemaRef.CurContext->isDependentContext())
23497 ? RefExpr->IgnoreParens()
23498 : Ref);
23499 Privates.push_back(Elt: PrivateRef);
23500 Inits.push_back(Elt: InitRef);
23501 }
23502
23503 if (Vars.empty())
23504 return nullptr;
23505
23506 Expr *StepExpr = Step;
23507 Expr *CalcStepExpr = nullptr;
23508 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
23509 !Step->isInstantiationDependent() &&
23510 !Step->containsUnexpandedParameterPack()) {
23511 SourceLocation StepLoc = Step->getBeginLoc();
23512 ExprResult Val = PerformOpenMPImplicitIntegerConversion(Loc: StepLoc, Op: Step);
23513 if (Val.isInvalid())
23514 return nullptr;
23515 StepExpr = Val.get();
23516
23517 // Build var to save the step value.
23518 VarDecl *SaveVar =
23519 buildVarDecl(SemaRef, Loc: StepLoc, Type: StepExpr->getType(), Name: ".linear.step");
23520 ExprResult SaveRef =
23521 buildDeclRefExpr(S&: SemaRef, D: SaveVar, Ty: StepExpr->getType(), Loc: StepLoc);
23522 ExprResult CalcStep = SemaRef.BuildBinOp(
23523 S: SemaRef.getCurScope(), OpLoc: StepLoc, Opc: BO_Assign, LHSExpr: SaveRef.get(), RHSExpr: StepExpr);
23524 CalcStep =
23525 SemaRef.ActOnFinishFullExpr(Expr: CalcStep.get(), /*DiscardedValue=*/false);
23526
23527 // Warn about zero linear step (it would be probably better specified as
23528 // making corresponding variables 'const').
23529 if (std::optional<llvm::APSInt> Result =
23530 StepExpr->getIntegerConstantExpr(Ctx: getASTContext())) {
23531 if (!Result->isNegative() && !Result->isStrictlyPositive())
23532 Diag(Loc: StepLoc, DiagID: diag::warn_omp_linear_step_zero)
23533 << Vars[0] << (Vars.size() > 1);
23534 } else if (CalcStep.isUsable()) {
23535 // Calculate the step beforehand instead of doing this on each iteration.
23536 // (This is not used if the number of iterations may be kfold-ed).
23537 CalcStepExpr = CalcStep.get();
23538 }
23539 }
23540
23541 return OMPLinearClause::Create(C: getASTContext(), StartLoc, LParenLoc, Modifier: LinKind,
23542 ModifierLoc: LinLoc, ColonLoc, StepModifierLoc, EndLoc,
23543 VL: Vars, PL: Privates, IL: Inits, Step: StepExpr, CalcStep: CalcStepExpr,
23544 PreInit: buildPreInits(Context&: getASTContext(), PreInits: ExprCaptures),
23545 PostUpdate: buildPostUpdate(S&: SemaRef, PostUpdates: ExprPostUpdates));
23546}
23547
23548static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
23549 Expr *NumIterations, Sema &SemaRef,
23550 Scope *S, DSAStackTy *Stack) {
23551 // Walk the vars and build update/final expressions for the CodeGen.
23552 SmallVector<Expr *, 8> Updates;
23553 SmallVector<Expr *, 8> Finals;
23554 SmallVector<Expr *, 8> UsedExprs;
23555 Expr *Step = Clause.getStep();
23556 Expr *CalcStep = Clause.getCalcStep();
23557 // OpenMP [2.14.3.7, linear clause]
23558 // If linear-step is not specified it is assumed to be 1.
23559 if (!Step)
23560 Step = SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get();
23561 else if (CalcStep)
23562 Step = cast<BinaryOperator>(Val: CalcStep)->getLHS();
23563 bool HasErrors = false;
23564 auto CurInit = Clause.inits().begin();
23565 auto CurPrivate = Clause.privates().begin();
23566 for (Expr *RefExpr : Clause.varlist()) {
23567 SourceLocation ELoc;
23568 SourceRange ERange;
23569 Expr *SimpleRefExpr = RefExpr;
23570 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
23571 ValueDecl *D = Res.first;
23572 if (Res.second || !D) {
23573 Updates.push_back(Elt: nullptr);
23574 Finals.push_back(Elt: nullptr);
23575 HasErrors = true;
23576 continue;
23577 }
23578 auto &&Info = Stack->isLoopControlVariable(D);
23579 // OpenMP [2.15.11, distribute simd Construct]
23580 // A list item may not appear in a linear clause, unless it is the loop
23581 // iteration variable.
23582 if (isOpenMPDistributeDirective(DKind: Stack->getCurrentDirective()) &&
23583 isOpenMPSimdDirective(DKind: Stack->getCurrentDirective()) && !Info.first) {
23584 SemaRef.Diag(Loc: ELoc,
23585 DiagID: diag::err_omp_linear_distribute_var_non_loop_iteration);
23586 Updates.push_back(Elt: nullptr);
23587 Finals.push_back(Elt: nullptr);
23588 HasErrors = true;
23589 continue;
23590 }
23591 Expr *InitExpr = *CurInit;
23592
23593 // Build privatized reference to the current linear var. The final value is
23594 // written back through the variable itself, also for 'uval' references.
23595 auto *DE = cast<DeclRefExpr>(Val: SimpleRefExpr);
23596 Expr *CapturedRef;
23597 if (isa<BindingDecl>(Val: DE->getDecl())) {
23598 CapturedRef = SimpleRefExpr;
23599 } else {
23600 CapturedRef =
23601 buildDeclRefExpr(S&: SemaRef, D: cast<VarDecl>(Val: DE->getDecl()),
23602 Ty: DE->getType().getUnqualifiedType(), Loc: DE->getExprLoc(),
23603 /*RefersToCapture=*/true);
23604 }
23605
23606 // Build update: Var = InitExpr + IV * Step
23607 ExprResult Update;
23608 if (!Info.first)
23609 Update = buildCounterUpdate(
23610 SemaRef, S, Loc: RefExpr->getExprLoc(), VarRef: *CurPrivate, Start: InitExpr, Iter: IV, Step,
23611 /*Subtract=*/false, /*IsNonRectangularLB=*/false);
23612 else
23613 Update = *CurPrivate;
23614 Update = SemaRef.ActOnFinishFullExpr(Expr: Update.get(), CC: DE->getBeginLoc(),
23615 /*DiscardedValue=*/false);
23616
23617 // Build final: Var = PrivCopy;
23618 ExprResult Final;
23619 if (!Info.first)
23620 Final = SemaRef.BuildBinOp(
23621 S, OpLoc: RefExpr->getExprLoc(), Opc: BO_Assign, LHSExpr: CapturedRef,
23622 RHSExpr: SemaRef.DefaultLvalueConversion(E: *CurPrivate).get());
23623 else
23624 Final = *CurPrivate;
23625 Final = SemaRef.ActOnFinishFullExpr(Expr: Final.get(), CC: DE->getBeginLoc(),
23626 /*DiscardedValue=*/false);
23627
23628 if (!Update.isUsable() || !Final.isUsable()) {
23629 Updates.push_back(Elt: nullptr);
23630 Finals.push_back(Elt: nullptr);
23631 UsedExprs.push_back(Elt: nullptr);
23632 HasErrors = true;
23633 } else {
23634 Updates.push_back(Elt: Update.get());
23635 Finals.push_back(Elt: Final.get());
23636 if (!Info.first)
23637 UsedExprs.push_back(Elt: SimpleRefExpr);
23638 }
23639 ++CurInit;
23640 ++CurPrivate;
23641 }
23642 if (Expr *S = Clause.getStep())
23643 UsedExprs.push_back(Elt: S);
23644 // Fill the remaining part with the nullptr.
23645 UsedExprs.append(NumInputs: Clause.varlist_size() + 1 - UsedExprs.size(), Elt: nullptr);
23646 Clause.setUpdates(Updates);
23647 Clause.setFinals(Finals);
23648 Clause.setUsedExprs(UsedExprs);
23649 return HasErrors;
23650}
23651
23652OMPClause *SemaOpenMP::ActOnOpenMPAlignedClause(
23653 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
23654 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
23655 SmallVector<Expr *, 8> Vars;
23656 for (Expr *RefExpr : VarList) {
23657 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
23658 SourceLocation ELoc;
23659 SourceRange ERange;
23660 Expr *SimpleRefExpr = RefExpr;
23661 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
23662 if (Res.second) {
23663 // It will be analyzed later.
23664 Vars.push_back(Elt: RefExpr);
23665 }
23666 ValueDecl *D = Res.first;
23667 if (!D)
23668 continue;
23669
23670 QualType QType = D->getType();
23671 auto *VD = dyn_cast<VarDecl>(Val: D);
23672
23673 // OpenMP [2.8.1, simd construct, Restrictions]
23674 // The type of list items appearing in the aligned clause must be
23675 // array, pointer, reference to array, or reference to pointer.
23676 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
23677 const Type *Ty = QType.getTypePtrOrNull();
23678 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
23679 Diag(Loc: ELoc, DiagID: diag::err_omp_aligned_expected_array_or_ptr)
23680 << QType << getLangOpts().CPlusPlus << ERange;
23681 bool IsDecl = !VD || VD->isThisDeclarationADefinition(getASTContext()) ==
23682 VarDecl::DeclarationOnly;
23683 Diag(Loc: D->getLocation(),
23684 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
23685 << D;
23686 continue;
23687 }
23688
23689 // OpenMP [2.8.1, simd construct, Restrictions]
23690 // A list-item cannot appear in more than one aligned clause.
23691 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, NewDE: SimpleRefExpr)) {
23692 Diag(Loc: ELoc, DiagID: diag::err_omp_used_in_clause_twice)
23693 << 0 << getOpenMPClauseNameForDiag(C: OMPC_aligned) << ERange;
23694 Diag(Loc: PrevRef->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
23695 << getOpenMPClauseNameForDiag(C: OMPC_aligned);
23696 continue;
23697 }
23698
23699 DeclRefExpr *Ref = nullptr;
23700 if (!VD && isOpenMPCapturedDecl(D))
23701 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/true);
23702 Vars.push_back(Elt: SemaRef
23703 .DefaultFunctionArrayConversion(
23704 E: (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
23705 .get());
23706 }
23707
23708 // OpenMP [2.8.1, simd construct, Description]
23709 // The parameter of the aligned clause, alignment, must be a constant
23710 // positive integer expression.
23711 // If no optional parameter is specified, implementation-defined default
23712 // alignments for SIMD instructions on the target platforms are assumed.
23713 if (Alignment != nullptr) {
23714 ExprResult AlignResult =
23715 VerifyPositiveIntegerConstantInClause(E: Alignment, CKind: OMPC_aligned);
23716 if (AlignResult.isInvalid())
23717 return nullptr;
23718 Alignment = AlignResult.get();
23719 }
23720 if (Vars.empty())
23721 return nullptr;
23722
23723 return OMPAlignedClause::Create(C: getASTContext(), StartLoc, LParenLoc,
23724 ColonLoc, EndLoc, VL: Vars, A: Alignment);
23725}
23726
23727OMPClause *SemaOpenMP::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
23728 SourceLocation StartLoc,
23729 SourceLocation LParenLoc,
23730 SourceLocation EndLoc) {
23731 SmallVector<Expr *, 8> Vars;
23732 SmallVector<Expr *, 8> SrcExprs;
23733 SmallVector<Expr *, 8> DstExprs;
23734 SmallVector<Expr *, 8> AssignmentOps;
23735 for (Expr *RefExpr : VarList) {
23736 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
23737 if (isa<DependentScopeDeclRefExpr>(Val: RefExpr)) {
23738 // It will be analyzed later.
23739 Vars.push_back(Elt: RefExpr);
23740 SrcExprs.push_back(Elt: nullptr);
23741 DstExprs.push_back(Elt: nullptr);
23742 AssignmentOps.push_back(Elt: nullptr);
23743 continue;
23744 }
23745
23746 SourceLocation ELoc = RefExpr->getExprLoc();
23747 // OpenMP [2.1, C/C++]
23748 // A list item is a variable name.
23749 // OpenMP [2.14.4.1, Restrictions, p.1]
23750 // A list item that appears in a copyin clause must be threadprivate.
23751 auto *DE = dyn_cast<DeclRefExpr>(Val: RefExpr);
23752 if (!DE || !isa<VarDecl>(Val: DE->getDecl())) {
23753 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_var_name_member_expr)
23754 << 0 << RefExpr->getSourceRange();
23755 continue;
23756 }
23757
23758 Decl *D = DE->getDecl();
23759 auto *VD = cast<VarDecl>(Val: D);
23760
23761 QualType Type = VD->getType();
23762 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
23763 // It will be analyzed later.
23764 Vars.push_back(Elt: DE);
23765 SrcExprs.push_back(Elt: nullptr);
23766 DstExprs.push_back(Elt: nullptr);
23767 AssignmentOps.push_back(Elt: nullptr);
23768 continue;
23769 }
23770
23771 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
23772 // A list item that appears in a copyin clause must be threadprivate.
23773 if (!DSAStack->isThreadPrivate(D: VD)) {
23774 llvm::omp::Version OMPVersion = getLangOpts().getOpenMPVersion();
23775 Diag(Loc: ELoc, DiagID: diag::err_omp_required_access)
23776 << getOpenMPClauseNameForDiag(C: OMPC_copyin)
23777 << getOpenMPDirectiveName(D: OMPD_threadprivate, V: OMPVersion);
23778 continue;
23779 }
23780
23781 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
23782 // A variable of class type (or array thereof) that appears in a
23783 // copyin clause requires an accessible, unambiguous copy assignment
23784 // operator for the class type.
23785 QualType ElemType =
23786 getASTContext().getBaseElementType(QT: Type).getNonReferenceType();
23787 VarDecl *SrcVD =
23788 buildVarDecl(SemaRef, Loc: DE->getBeginLoc(), Type: ElemType.getUnqualifiedType(),
23789 Name: ".copyin.src", Attrs: VD->hasAttrs() ? &VD->getAttrs() : nullptr);
23790 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
23791 S&: SemaRef, D: SrcVD, Ty: ElemType.getUnqualifiedType(), Loc: DE->getExprLoc());
23792 VarDecl *DstVD =
23793 buildVarDecl(SemaRef, Loc: DE->getBeginLoc(), Type: ElemType, Name: ".copyin.dst",
23794 Attrs: VD->hasAttrs() ? &VD->getAttrs() : nullptr);
23795 DeclRefExpr *PseudoDstExpr =
23796 buildDeclRefExpr(S&: SemaRef, D: DstVD, Ty: ElemType, Loc: DE->getExprLoc());
23797 // For arrays generate assignment operation for single element and replace
23798 // it by the original array element in CodeGen.
23799 ExprResult AssignmentOp =
23800 SemaRef.BuildBinOp(/*S=*/nullptr, OpLoc: DE->getExprLoc(), Opc: BO_Assign,
23801 LHSExpr: PseudoDstExpr, RHSExpr: PseudoSrcExpr);
23802 if (AssignmentOp.isInvalid())
23803 continue;
23804 AssignmentOp =
23805 SemaRef.ActOnFinishFullExpr(Expr: AssignmentOp.get(), CC: DE->getExprLoc(),
23806 /*DiscardedValue=*/false);
23807 if (AssignmentOp.isInvalid())
23808 continue;
23809
23810 DSAStack->addDSA(D: VD, E: DE, A: OMPC_copyin);
23811 Vars.push_back(Elt: DE);
23812 SrcExprs.push_back(Elt: PseudoSrcExpr);
23813 DstExprs.push_back(Elt: PseudoDstExpr);
23814 AssignmentOps.push_back(Elt: AssignmentOp.get());
23815 }
23816
23817 if (Vars.empty())
23818 return nullptr;
23819
23820 return OMPCopyinClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
23821 VL: Vars, SrcExprs, DstExprs, AssignmentOps);
23822}
23823
23824OMPClause *SemaOpenMP::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
23825 SourceLocation StartLoc,
23826 SourceLocation LParenLoc,
23827 SourceLocation EndLoc) {
23828 SmallVector<Expr *, 8> Vars;
23829 SmallVector<Expr *, 8> SrcExprs;
23830 SmallVector<Expr *, 8> DstExprs;
23831 SmallVector<Expr *, 8> AssignmentOps;
23832 for (Expr *RefExpr : VarList) {
23833 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
23834 SourceLocation ELoc;
23835 SourceRange ERange;
23836 Expr *SimpleRefExpr = RefExpr;
23837 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
23838 if (Res.second) {
23839 // It will be analyzed later.
23840 Vars.push_back(Elt: RefExpr);
23841 SrcExprs.push_back(Elt: nullptr);
23842 DstExprs.push_back(Elt: nullptr);
23843 AssignmentOps.push_back(Elt: nullptr);
23844 }
23845 ValueDecl *D = Res.first;
23846 if (!D)
23847 continue;
23848
23849 QualType Type = D->getType();
23850 auto *VD = dyn_cast<VarDecl>(Val: D);
23851
23852 // OpenMP [2.14.4.2, Restrictions, p.2]
23853 // A list item that appears in a copyprivate clause may not appear in a
23854 // private or firstprivate clause on the single construct.
23855 if (!VD || !DSAStack->isThreadPrivate(D: VD)) {
23856 DSAStackTy::DSAVarData DVar =
23857 DSAStack->getTopDSA(D, /*FromParent=*/false);
23858 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
23859 DVar.RefExpr) {
23860 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
23861 << getOpenMPClauseNameForDiag(C: DVar.CKind)
23862 << getOpenMPClauseNameForDiag(C: OMPC_copyprivate);
23863 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
23864 continue;
23865 }
23866
23867 // OpenMP [2.11.4.2, Restrictions, p.1]
23868 // All list items that appear in a copyprivate clause must be either
23869 // threadprivate or private in the enclosing context.
23870 if (DVar.CKind == OMPC_unknown) {
23871 DVar = DSAStack->getImplicitDSA(D, FromParent: false);
23872 // A data member is private only if an enclosing construct captured it.
23873 const bool IsShared = DVar.CKind == OMPC_shared;
23874 if (IsShared ||
23875 (isa<FieldDecl>(Val: D) && !SemaRef.CurContext->isDependentContext() &&
23876 !isOpenMPCapturedDecl(D))) {
23877 Diag(Loc: ELoc, DiagID: diag::err_omp_required_access)
23878 << getOpenMPClauseNameForDiag(C: OMPC_copyprivate)
23879 << "threadprivate or private in the enclosing context";
23880 if (IsShared)
23881 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
23882 continue;
23883 }
23884 }
23885 }
23886
23887 // Variably modified types are not supported.
23888 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
23889 llvm::omp::Version OMPVersion = getLangOpts().getOpenMPVersion();
23890 Diag(Loc: ELoc, DiagID: diag::err_omp_variably_modified_type_not_supported)
23891 << getOpenMPClauseNameForDiag(C: OMPC_copyprivate) << Type
23892 << getOpenMPDirectiveName(DSAStack->getCurrentDirective(),
23893 V: OMPVersion);
23894 bool IsDecl = !VD || VD->isThisDeclarationADefinition(getASTContext()) ==
23895 VarDecl::DeclarationOnly;
23896 Diag(Loc: D->getLocation(),
23897 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
23898 << D;
23899 continue;
23900 }
23901
23902 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
23903 // A variable of class type (or array thereof) that appears in a
23904 // copyin clause requires an accessible, unambiguous copy assignment
23905 // operator for the class type.
23906 Type = getASTContext()
23907 .getBaseElementType(QT: Type.getNonReferenceType())
23908 .getUnqualifiedType();
23909 VarDecl *SrcVD =
23910 buildVarDecl(SemaRef, Loc: RefExpr->getBeginLoc(), Type, Name: ".copyprivate.src",
23911 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
23912 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(S&: SemaRef, D: SrcVD, Ty: Type, Loc: ELoc);
23913 VarDecl *DstVD =
23914 buildVarDecl(SemaRef, Loc: RefExpr->getBeginLoc(), Type, Name: ".copyprivate.dst",
23915 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
23916 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(S&: SemaRef, D: DstVD, Ty: Type, Loc: ELoc);
23917 ExprResult AssignmentOp = SemaRef.BuildBinOp(
23918 DSAStack->getCurScope(), OpLoc: ELoc, Opc: BO_Assign, LHSExpr: PseudoDstExpr, RHSExpr: PseudoSrcExpr);
23919 if (AssignmentOp.isInvalid())
23920 continue;
23921 AssignmentOp = SemaRef.ActOnFinishFullExpr(Expr: AssignmentOp.get(), CC: ELoc,
23922 /*DiscardedValue=*/false);
23923 if (AssignmentOp.isInvalid())
23924 continue;
23925
23926 // No need to mark vars as copyprivate, they are already threadprivate or
23927 // implicitly private.
23928 const bool IsBindingDecl = isa<BindingDecl>(Val: D);
23929 assert(VD || IsBindingDecl || SemaRef.CurContext->isDependentContext() ||
23930 isOpenMPCapturedDecl(D));
23931 Vars.push_back(
23932 Elt: (VD || IsBindingDecl || SemaRef.CurContext->isDependentContext())
23933 ? RefExpr->IgnoreParens()
23934 : buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/false));
23935 SrcExprs.push_back(Elt: PseudoSrcExpr);
23936 DstExprs.push_back(Elt: PseudoDstExpr);
23937 AssignmentOps.push_back(Elt: AssignmentOp.get());
23938 }
23939
23940 if (Vars.empty())
23941 return nullptr;
23942
23943 return OMPCopyprivateClause::Create(C: getASTContext(), StartLoc, LParenLoc,
23944 EndLoc, VL: Vars, SrcExprs, DstExprs,
23945 AssignmentOps);
23946}
23947
23948OMPClause *SemaOpenMP::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
23949 SourceLocation StartLoc,
23950 SourceLocation LParenLoc,
23951 SourceLocation EndLoc) {
23952 if (VarList.empty())
23953 return nullptr;
23954
23955 return OMPFlushClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
23956 VL: VarList);
23957}
23958
23959/// Tries to find omp_depend_t. type.
23960static bool findOMPDependT(Sema &S, SourceLocation Loc, DSAStackTy *Stack,
23961 bool Diagnose = true) {
23962 QualType OMPDependT = Stack->getOMPDependT();
23963 if (!OMPDependT.isNull())
23964 return true;
23965 IdentifierInfo *II = &S.PP.getIdentifierTable().get(Name: "omp_depend_t");
23966 ParsedType PT = S.getTypeName(II: *II, NameLoc: Loc, S: S.getCurScope());
23967 if (!PT.getAsOpaquePtr() || PT.get().isNull()) {
23968 if (Diagnose)
23969 S.Diag(Loc, DiagID: diag::err_omp_implied_type_not_found) << "omp_depend_t";
23970 return false;
23971 }
23972 Stack->setOMPDependT(PT.get());
23973 return true;
23974}
23975
23976OMPClause *SemaOpenMP::ActOnOpenMPDepobjClause(Expr *Depobj,
23977 SourceLocation StartLoc,
23978 SourceLocation LParenLoc,
23979 SourceLocation EndLoc) {
23980 if (!Depobj)
23981 return nullptr;
23982
23983 bool OMPDependTFound = findOMPDependT(S&: SemaRef, Loc: StartLoc, DSAStack);
23984
23985 // OpenMP 5.0, 2.17.10.1 depobj Construct
23986 // depobj is an lvalue expression of type omp_depend_t.
23987 if (!Depobj->isTypeDependent() && !Depobj->isValueDependent() &&
23988 !Depobj->isInstantiationDependent() &&
23989 !Depobj->containsUnexpandedParameterPack() &&
23990 (OMPDependTFound && !getASTContext().typesAreCompatible(
23991 DSAStack->getOMPDependT(), T2: Depobj->getType(),
23992 /*CompareUnqualified=*/true))) {
23993 Diag(Loc: Depobj->getExprLoc(), DiagID: diag::err_omp_expected_omp_depend_t_lvalue)
23994 << 0 << Depobj->getType() << Depobj->getSourceRange();
23995 }
23996
23997 if (!Depobj->isLValue()) {
23998 Diag(Loc: Depobj->getExprLoc(), DiagID: diag::err_omp_expected_omp_depend_t_lvalue)
23999 << 1 << Depobj->getSourceRange();
24000 }
24001
24002 return new (getASTContext())
24003 OMPDepobjClause(StartLoc, LParenLoc, EndLoc, Depobj);
24004}
24005
24006namespace {
24007// Utility struct that gathers the related info for doacross clause.
24008struct DoacrossDataInfoTy {
24009 // The list of expressions.
24010 SmallVector<Expr *, 8> Vars;
24011 // The OperatorOffset for doacross loop.
24012 DSAStackTy::OperatorOffsetTy OpsOffs;
24013 // The depended loop count.
24014 llvm::APSInt TotalDepCount;
24015};
24016} // namespace
24017static DoacrossDataInfoTy
24018ProcessOpenMPDoacrossClauseCommon(Sema &SemaRef, bool IsSource,
24019 ArrayRef<Expr *> VarList, DSAStackTy *Stack,
24020 SourceLocation EndLoc) {
24021
24022 SmallVector<Expr *, 8> Vars;
24023 DSAStackTy::OperatorOffsetTy OpsOffs;
24024 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
24025
24026 if (const Expr *OrderedCountExpr =
24027 Stack->getParentOrderedRegionParam().first) {
24028 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Ctx: SemaRef.Context);
24029 TotalDepCount.setIsUnsigned(/*Val=*/true);
24030 }
24031 llvm::APSInt DepCounter(TotalDepCount.getBitWidth(),
24032 TotalDepCount.isUnsigned());
24033
24034 for (Expr *RefExpr : VarList) {
24035 assert(RefExpr && "NULL expr in OpenMP doacross clause.");
24036 if (isa<DependentScopeDeclRefExpr>(Val: RefExpr)) {
24037 // It will be analyzed later.
24038 Vars.push_back(Elt: RefExpr);
24039 continue;
24040 }
24041
24042 SourceLocation ELoc = RefExpr->getExprLoc();
24043 Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
24044 if (!IsSource) {
24045 if (Stack->getParentOrderedRegionParam().first &&
24046 DepCounter >= TotalDepCount) {
24047 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_depend_sink_unexpected_expr);
24048 continue;
24049 }
24050 ++DepCounter;
24051 // OpenMP [2.13.9, Summary]
24052 // depend(dependence-type : vec), where dependence-type is:
24053 // 'sink' and where vec is the iteration vector, which has the form:
24054 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
24055 // where n is the value specified by the ordered clause in the loop
24056 // directive, xi denotes the loop iteration variable of the i-th nested
24057 // loop associated with the loop directive, and di is a constant
24058 // non-negative integer.
24059 if (SemaRef.CurContext->isDependentContext()) {
24060 // It will be analyzed later.
24061 Vars.push_back(Elt: RefExpr);
24062 continue;
24063 }
24064 SimpleExpr = SimpleExpr->IgnoreImplicit();
24065 OverloadedOperatorKind OOK = OO_None;
24066 SourceLocation OOLoc;
24067 Expr *LHS = SimpleExpr;
24068 Expr *RHS = nullptr;
24069 if (auto *BO = dyn_cast<BinaryOperator>(Val: SimpleExpr)) {
24070 OOK = BinaryOperator::getOverloadedOperator(Opc: BO->getOpcode());
24071 OOLoc = BO->getOperatorLoc();
24072 LHS = BO->getLHS()->IgnoreParenImpCasts();
24073 RHS = BO->getRHS()->IgnoreParenImpCasts();
24074 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(Val: SimpleExpr)) {
24075 OOK = OCE->getOperator();
24076 OOLoc = OCE->getOperatorLoc();
24077 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
24078 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
24079 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(Val: SimpleExpr)) {
24080 OOK = MCE->getMethodDecl()
24081 ->getNameInfo()
24082 .getName()
24083 .getCXXOverloadedOperator();
24084 OOLoc = MCE->getCallee()->getExprLoc();
24085 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
24086 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
24087 }
24088 SourceLocation ELoc;
24089 SourceRange ERange;
24090 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: LHS, ELoc, ERange);
24091 if (Res.second) {
24092 // It will be analyzed later.
24093 Vars.push_back(Elt: RefExpr);
24094 }
24095 ValueDecl *D = Res.first;
24096 if (!D)
24097 continue;
24098
24099 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
24100 SemaRef.Diag(Loc: OOLoc, DiagID: diag::err_omp_depend_sink_expected_plus_minus);
24101 continue;
24102 }
24103 if (RHS) {
24104 ExprResult RHSRes =
24105 SemaRef.OpenMP().VerifyPositiveIntegerConstantInClause(
24106 E: RHS, CKind: OMPC_depend, /*StrictlyPositive=*/false);
24107 if (RHSRes.isInvalid())
24108 continue;
24109 }
24110 if (!SemaRef.CurContext->isDependentContext() &&
24111 Stack->getParentOrderedRegionParam().first &&
24112 DepCounter != Stack->isParentLoopControlVariable(D).first) {
24113 const ValueDecl *VD =
24114 Stack->getParentLoopControlVariable(I: DepCounter.getZExtValue());
24115 if (VD)
24116 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_depend_sink_expected_loop_iteration)
24117 << 1 << VD;
24118 else
24119 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_depend_sink_expected_loop_iteration)
24120 << 0;
24121 continue;
24122 }
24123 OpsOffs.emplace_back(Args&: RHS, Args&: OOK);
24124 }
24125 Vars.push_back(Elt: RefExpr->IgnoreParenImpCasts());
24126 }
24127 if (!SemaRef.CurContext->isDependentContext() && !IsSource &&
24128 TotalDepCount > VarList.size() &&
24129 Stack->getParentOrderedRegionParam().first &&
24130 Stack->getParentLoopControlVariable(I: VarList.size() + 1)) {
24131 SemaRef.Diag(Loc: EndLoc, DiagID: diag::err_omp_depend_sink_expected_loop_iteration)
24132 << 1 << Stack->getParentLoopControlVariable(I: VarList.size() + 1);
24133 }
24134 return {.Vars: Vars, .OpsOffs: OpsOffs, .TotalDepCount: TotalDepCount};
24135}
24136
24137OMPClause *SemaOpenMP::ActOnOpenMPDependClause(
24138 const OMPDependClause::DependDataTy &Data, Expr *DepModifier,
24139 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
24140 SourceLocation EndLoc) {
24141 OpenMPDependClauseKind DepKind = Data.DepKind;
24142 SourceLocation DepLoc = Data.DepLoc;
24143 if (DSAStack->getCurrentDirective() == OMPD_ordered_standalone &&
24144 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
24145 Diag(Loc: DepLoc, DiagID: diag::err_omp_unexpected_clause_value)
24146 << "'source' or 'sink'" << getOpenMPClauseNameForDiag(C: OMPC_depend);
24147 return nullptr;
24148 }
24149 if (DSAStack->getCurrentDirective() == OMPD_taskwait &&
24150 DepKind == OMPC_DEPEND_mutexinoutset) {
24151 Diag(Loc: DepLoc, DiagID: diag::err_omp_taskwait_depend_mutexinoutset_not_allowed);
24152 return nullptr;
24153 }
24154 if ((DSAStack->getCurrentDirective() != OMPD_ordered_standalone ||
24155 DSAStack->getCurrentDirective() == OMPD_depobj) &&
24156 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
24157 DepKind == OMPC_DEPEND_sink ||
24158 ((getLangOpts().OpenMP < 50 ||
24159 DSAStack->getCurrentDirective() == OMPD_depobj) &&
24160 DepKind == OMPC_DEPEND_depobj))) {
24161 SmallVector<unsigned, 6> Except = {OMPC_DEPEND_source, OMPC_DEPEND_sink,
24162 OMPC_DEPEND_outallmemory,
24163 OMPC_DEPEND_inoutallmemory};
24164 if (getLangOpts().OpenMP < 50 ||
24165 DSAStack->getCurrentDirective() == OMPD_depobj)
24166 Except.push_back(Elt: OMPC_DEPEND_depobj);
24167 if (getLangOpts().OpenMP < 51)
24168 Except.push_back(Elt: OMPC_DEPEND_inoutset);
24169 std::string Expected = (getLangOpts().OpenMP >= 50 && !DepModifier)
24170 ? "depend modifier(iterator) or "
24171 : "";
24172 Diag(Loc: DepLoc, DiagID: diag::err_omp_unexpected_clause_value)
24173 << Expected + getListOfPossibleValues(K: OMPC_depend, /*First=*/0,
24174 /*Last=*/OMPC_DEPEND_unknown,
24175 Exclude: Except)
24176 << getOpenMPClauseNameForDiag(C: OMPC_depend);
24177 return nullptr;
24178 }
24179 if (DepModifier &&
24180 (DepKind == OMPC_DEPEND_source || DepKind == OMPC_DEPEND_sink)) {
24181 Diag(Loc: DepModifier->getExprLoc(),
24182 DiagID: diag::err_omp_depend_sink_source_with_modifier);
24183 return nullptr;
24184 }
24185 if (DepModifier &&
24186 !DepModifier->getType()->isSpecificBuiltinType(K: BuiltinType::OMPIterator))
24187 Diag(Loc: DepModifier->getExprLoc(), DiagID: diag::err_omp_depend_modifier_not_iterator);
24188
24189 SmallVector<Expr *, 8> Vars;
24190 DSAStackTy::OperatorOffsetTy OpsOffs;
24191 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
24192
24193 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
24194 DoacrossDataInfoTy VarOffset = ProcessOpenMPDoacrossClauseCommon(
24195 SemaRef, IsSource: DepKind == OMPC_DEPEND_source, VarList, DSAStack, EndLoc);
24196 Vars = VarOffset.Vars;
24197 OpsOffs = VarOffset.OpsOffs;
24198 TotalDepCount = VarOffset.TotalDepCount;
24199 } else {
24200 for (Expr *RefExpr : VarList) {
24201 assert(RefExpr && "NULL expr in OpenMP depend clause.");
24202 if (isa<DependentScopeDeclRefExpr>(Val: RefExpr)) {
24203 // It will be analyzed later.
24204 Vars.push_back(Elt: RefExpr);
24205 continue;
24206 }
24207
24208 SourceLocation ELoc = RefExpr->getExprLoc();
24209 Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
24210 if (DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) {
24211 bool OMPDependTFound = getLangOpts().OpenMP >= 50;
24212 if (OMPDependTFound)
24213 OMPDependTFound = findOMPDependT(S&: SemaRef, Loc: StartLoc, DSAStack,
24214 Diagnose: DepKind == OMPC_DEPEND_depobj);
24215 if (DepKind == OMPC_DEPEND_depobj) {
24216 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++
24217 // List items used in depend clauses with the depobj dependence type
24218 // must be expressions of the omp_depend_t type.
24219 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() &&
24220 !RefExpr->isInstantiationDependent() &&
24221 !RefExpr->containsUnexpandedParameterPack() &&
24222 (OMPDependTFound &&
24223 !getASTContext().hasSameUnqualifiedType(
24224 DSAStack->getOMPDependT(), T2: RefExpr->getType()))) {
24225 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_omp_depend_t_lvalue)
24226 << 0 << RefExpr->getType() << RefExpr->getSourceRange();
24227 continue;
24228 }
24229 if (!RefExpr->isLValue()) {
24230 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_omp_depend_t_lvalue)
24231 << 1 << RefExpr->getType() << RefExpr->getSourceRange();
24232 continue;
24233 }
24234 } else {
24235 // OpenMP 5.0 [2.17.11, Restrictions]
24236 // List items used in depend clauses cannot be zero-length array
24237 // sections.
24238 QualType ExprTy = RefExpr->getType().getNonReferenceType();
24239 const auto *OASE = dyn_cast<ArraySectionExpr>(Val: SimpleExpr);
24240 if (OASE) {
24241 QualType BaseType =
24242 ArraySectionExpr::getBaseOriginalType(Base: OASE->getBase());
24243 if (BaseType.isNull())
24244 return nullptr;
24245 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
24246 ExprTy = ATy->getElementType();
24247 else
24248 ExprTy = BaseType->getPointeeType();
24249 if (BaseType.isNull() || ExprTy.isNull())
24250 return nullptr;
24251 ExprTy = ExprTy.getNonReferenceType();
24252 const Expr *Length = OASE->getLength();
24253 Expr::EvalResult Result;
24254 if (Length && !Length->isValueDependent() &&
24255 Length->EvaluateAsInt(Result, Ctx: getASTContext()) &&
24256 Result.Val.getInt().isZero()) {
24257 Diag(Loc: ELoc,
24258 DiagID: diag::err_omp_depend_zero_length_array_section_not_allowed)
24259 << SimpleExpr->getSourceRange();
24260 continue;
24261 }
24262 }
24263
24264 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++
24265 // List items used in depend clauses with the in, out, inout,
24266 // inoutset, or mutexinoutset dependence types cannot be
24267 // expressions of the omp_depend_t type.
24268 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() &&
24269 !RefExpr->isInstantiationDependent() &&
24270 !RefExpr->containsUnexpandedParameterPack() &&
24271 (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
24272 (OMPDependTFound && DSAStack->getOMPDependT().getTypePtr() ==
24273 ExprTy.getTypePtr()))) {
24274 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_addressable_lvalue_or_array_item)
24275 << (getLangOpts().OpenMP >= 50 ? 1 : 0)
24276 << (getLangOpts().OpenMP >= 50 ? 1 : 0)
24277 << RefExpr->getSourceRange();
24278 continue;
24279 }
24280
24281 auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: SimpleExpr);
24282 if (ASE && !ASE->getBase()->isTypeDependent() &&
24283 !ASE->getBase()
24284 ->getType()
24285 .getNonReferenceType()
24286 ->isPointerType() &&
24287 !ASE->getBase()->getType().getNonReferenceType()->isArrayType()) {
24288 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_addressable_lvalue_or_array_item)
24289 << (getLangOpts().OpenMP >= 50 ? 1 : 0)
24290 << (getLangOpts().OpenMP >= 50 ? 1 : 0)
24291 << RefExpr->getSourceRange();
24292 continue;
24293 }
24294
24295 ExprResult Res;
24296 {
24297 Sema::TentativeAnalysisScope Trap(SemaRef);
24298 Res = SemaRef.CreateBuiltinUnaryOp(OpLoc: ELoc, Opc: UO_AddrOf,
24299 InputExpr: RefExpr->IgnoreParenImpCasts());
24300 }
24301 if (!Res.isUsable() && !isa<ArraySectionExpr>(Val: SimpleExpr) &&
24302 !isa<OMPArrayShapingExpr>(Val: SimpleExpr)) {
24303 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_addressable_lvalue_or_array_item)
24304 << (getLangOpts().OpenMP >= 50 ? 1 : 0)
24305 << (getLangOpts().OpenMP >= 50 ? 1 : 0)
24306 << RefExpr->getSourceRange();
24307 continue;
24308 }
24309 }
24310 }
24311 Vars.push_back(Elt: RefExpr->IgnoreParenImpCasts());
24312 }
24313 }
24314
24315 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
24316 DepKind != OMPC_DEPEND_outallmemory &&
24317 DepKind != OMPC_DEPEND_inoutallmemory && Vars.empty())
24318 return nullptr;
24319
24320 auto *C = OMPDependClause::Create(
24321 C: getASTContext(), StartLoc, LParenLoc, EndLoc,
24322 Data: {.DepKind: DepKind, .DepLoc: DepLoc, .ColonLoc: Data.ColonLoc, .OmpAllMemoryLoc: Data.OmpAllMemoryLoc}, DepModifier, VL: Vars,
24323 NumLoops: TotalDepCount.getZExtValue());
24324 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
24325 DSAStack->isParentOrderedRegion())
24326 DSAStack->addDoacrossDependClause(C, OpsOffs);
24327 return C;
24328}
24329
24330OMPClause *SemaOpenMP::ActOnOpenMPDeviceClause(
24331 OpenMPDeviceClauseModifier Modifier, Expr *Device, SourceLocation StartLoc,
24332 SourceLocation LParenLoc, SourceLocation ModifierLoc,
24333 SourceLocation EndLoc) {
24334 assert((ModifierLoc.isInvalid() || getLangOpts().OpenMP >= 50) &&
24335 "Unexpected device modifier in OpenMP < 50.");
24336
24337 bool ErrorFound = false;
24338 if (ModifierLoc.isValid() && Modifier == OMPC_DEVICE_unknown) {
24339 std::string Values =
24340 getListOfPossibleValues(K: OMPC_device, /*First=*/0, Last: OMPC_DEVICE_unknown);
24341 Diag(Loc: ModifierLoc, DiagID: diag::err_omp_unexpected_clause_value)
24342 << Values << getOpenMPClauseNameForDiag(C: OMPC_device);
24343 ErrorFound = true;
24344 }
24345
24346 Expr *ValExpr = Device;
24347 Stmt *HelperValStmt = nullptr;
24348
24349 // OpenMP 5.2 [1.3, Execution Model]: a conforming device number is either
24350 // a non-negative integer that is less than or equal to omp_get_num_devices()
24351 // or equal to omp_initial_device or omp_invalid_device. The predefined
24352 // identifiers were introduced in OpenMP 5.2; earlier versions require a
24353 // non-negative integer.
24354 if (getLangOpts().OpenMP >= 52) {
24355 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
24356 !ValExpr->isInstantiationDependent()) {
24357 SourceLocation Loc = ValExpr->getExprLoc();
24358 ExprResult Value = PerformOpenMPImplicitIntegerConversion(Loc, Op: ValExpr);
24359 if (Value.isInvalid()) {
24360 ErrorFound = true;
24361 } else {
24362 ValExpr = Value.get();
24363 if (std::optional<llvm::APSInt> Result =
24364 ValExpr->getIntegerConstantExpr(Ctx: getASTContext())) {
24365 if (Result->isSigned() && Result->slt(RHS: -2)) {
24366 Diag(Loc, DiagID: diag::err_omp_device_expression_invalid)
24367 << ValExpr->getSourceRange();
24368 ErrorFound = true;
24369 }
24370 }
24371 }
24372 }
24373 } else {
24374 ErrorFound = !isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_device,
24375 /*StrictlyPositive=*/false) ||
24376 ErrorFound;
24377 }
24378 if (ErrorFound)
24379 return nullptr;
24380
24381 // OpenMP 5.0 [2.12.5, Restrictions]
24382 // In case of ancestor device-modifier, a requires directive with
24383 // the reverse_offload clause must be specified.
24384 if (Modifier == OMPC_DEVICE_ancestor) {
24385 if (!DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>()) {
24386 SemaRef.targetDiag(
24387 Loc: StartLoc,
24388 DiagID: diag::err_omp_device_ancestor_without_requires_reverse_offload);
24389 ErrorFound = true;
24390 }
24391 }
24392
24393 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
24394 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
24395 DKind, CKind: OMPC_device, OMPVersion: getLangOpts().getOpenMPVersion());
24396 if (CaptureRegion != OMPD_unknown &&
24397 !SemaRef.CurContext->isDependentContext()) {
24398 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
24399 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
24400 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
24401 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
24402 }
24403
24404 return new (getASTContext())
24405 OMPDeviceClause(Modifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
24406 LParenLoc, ModifierLoc, EndLoc);
24407}
24408
24409static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
24410 DSAStackTy *Stack, QualType QTy,
24411 bool FullCheck = true) {
24412 if (SemaRef.RequireCompleteType(Loc: SL, T: QTy, DiagID: diag::err_incomplete_type))
24413 return false;
24414 if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
24415 !QTy.isTriviallyCopyableType(Context: SemaRef.Context))
24416 SemaRef.Diag(Loc: SL, DiagID: diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
24417 return true;
24418}
24419
24420/// Return true if it can be proven that the provided array expression
24421/// (array section or array subscript) does NOT specify the whole size of the
24422/// array whose base type is \a BaseQTy.
24423static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
24424 const Expr *E,
24425 QualType BaseQTy) {
24426 const auto *OASE = dyn_cast<ArraySectionExpr>(Val: E);
24427
24428 // If this is an array subscript, it refers to the whole size if the size of
24429 // the dimension is constant and equals 1. Also, an array section assumes the
24430 // format of an array subscript if no colon is used.
24431 if (isa<ArraySubscriptExpr>(Val: E) ||
24432 (OASE && OASE->getColonLocFirst().isInvalid())) {
24433 if (const auto *ATy = dyn_cast<ConstantArrayType>(Val: BaseQTy.getTypePtr()))
24434 return ATy->getSExtSize() != 1;
24435 // Size can't be evaluated statically.
24436 return false;
24437 }
24438
24439 assert(OASE && "Expecting array section if not an array subscript.");
24440 const Expr *LowerBound = OASE->getLowerBound();
24441 const Expr *Length = OASE->getLength();
24442
24443 // If there is a lower bound that does not evaluates to zero, we are not
24444 // covering the whole dimension.
24445 if (LowerBound) {
24446 Expr::EvalResult Result;
24447 if (!LowerBound->EvaluateAsInt(Result, Ctx: SemaRef.getASTContext()))
24448 return false; // Can't get the integer value as a constant.
24449
24450 llvm::APSInt ConstLowerBound = Result.Val.getInt();
24451 if (ConstLowerBound.getSExtValue())
24452 return true;
24453 }
24454
24455 // If we don't have a length we covering the whole dimension.
24456 if (!Length)
24457 return false;
24458
24459 // If the base is a pointer, we don't have a way to get the size of the
24460 // pointee.
24461 if (BaseQTy->isPointerType())
24462 return false;
24463
24464 // We can only check if the length is the same as the size of the dimension
24465 // if we have a constant array.
24466 const auto *CATy = dyn_cast<ConstantArrayType>(Val: BaseQTy.getTypePtr());
24467 if (!CATy)
24468 return false;
24469
24470 Expr::EvalResult Result;
24471 if (!Length->EvaluateAsInt(Result, Ctx: SemaRef.getASTContext()))
24472 return false; // Can't get the integer value as a constant.
24473
24474 llvm::APSInt ConstLength = Result.Val.getInt();
24475 return CATy->getSExtSize() != ConstLength.getSExtValue();
24476}
24477
24478// Return true if it can be proven that the provided array expression (array
24479// section or array subscript) does NOT specify a single element of the array
24480// whose base type is \a BaseQTy.
24481static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
24482 const Expr *E,
24483 QualType BaseQTy) {
24484 const auto *OASE = dyn_cast<ArraySectionExpr>(Val: E);
24485
24486 // An array subscript always refer to a single element. Also, an array section
24487 // assumes the format of an array subscript if no colon is used.
24488 if (isa<ArraySubscriptExpr>(Val: E) ||
24489 (OASE && OASE->getColonLocFirst().isInvalid()))
24490 return false;
24491
24492 assert(OASE && "Expecting array section if not an array subscript.");
24493 const Expr *Length = OASE->getLength();
24494
24495 // If we don't have a length we have to check if the array has unitary size
24496 // for this dimension. Also, we should always expect a length if the base type
24497 // is pointer.
24498 if (!Length) {
24499 if (const auto *ATy = dyn_cast<ConstantArrayType>(Val: BaseQTy.getTypePtr()))
24500 return ATy->getSExtSize() != 1;
24501 // We cannot assume anything.
24502 return false;
24503 }
24504
24505 // Check if the length evaluates to 1.
24506 Expr::EvalResult Result;
24507 if (!Length->EvaluateAsInt(Result, Ctx: SemaRef.getASTContext()))
24508 return false; // Can't get the integer value as a constant.
24509
24510 llvm::APSInt ConstLength = Result.Val.getInt();
24511 return ConstLength.getSExtValue() != 1;
24512}
24513
24514// The base of elements of list in a map clause have to be either:
24515// - a reference to variable or field.
24516// - a member expression.
24517// - an array expression.
24518//
24519// E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
24520// reference to 'r'.
24521//
24522// If we have:
24523//
24524// struct SS {
24525// Bla S;
24526// foo() {
24527// #pragma omp target map (S.Arr[:12]);
24528// }
24529// }
24530//
24531// We want to retrieve the member expression 'this->S';
24532
24533// OpenMP 5.0 [2.19.7.1, map Clause, Restrictions, p.2]
24534// If a list item is an array section, it must specify contiguous storage.
24535//
24536// For this restriction it is sufficient that we make sure only references
24537// to variables or fields and array expressions, and that no array sections
24538// exist except in the rightmost expression (unless they cover the whole
24539// dimension of the array). E.g. these would be invalid:
24540//
24541// r.ArrS[3:5].Arr[6:7]
24542//
24543// r.ArrS[3:5].x
24544//
24545// but these would be valid:
24546// r.ArrS[3].Arr[6:7]
24547//
24548// r.ArrS[3].x
24549namespace {
24550class MapBaseChecker final : public StmtVisitor<MapBaseChecker, bool> {
24551 Sema &SemaRef;
24552 OpenMPClauseKind CKind = OMPC_unknown;
24553 OpenMPDirectiveKind DKind = OMPD_unknown;
24554 OMPClauseMappableExprCommon::MappableExprComponentList &Components;
24555 bool IsNonContiguous = false;
24556 bool NoDiagnose = false;
24557 const Expr *RelevantExpr = nullptr;
24558 bool AllowUnitySizeArraySection = true;
24559 bool AllowWholeSizeArraySection = true;
24560 bool AllowAnotherPtr = true;
24561 SourceLocation ELoc;
24562 SourceRange ERange;
24563
24564 void emitErrorMsg() {
24565 // If nothing else worked, this is not a valid map clause expression.
24566 if (SemaRef.getLangOpts().OpenMP < 50) {
24567 SemaRef.Diag(Loc: ELoc,
24568 DiagID: diag::err_omp_expected_named_var_member_or_array_expression)
24569 << ERange;
24570 } else {
24571 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_non_lvalue_in_map_or_motion_clauses)
24572 << getOpenMPClauseNameForDiag(C: CKind) << ERange;
24573 }
24574 }
24575
24576public:
24577 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
24578 ValueDecl *D = DRE->getDecl();
24579 Expr *E = DRE;
24580
24581 // Handle BindingDecls by mapping them as member accesses.
24582 // When the user writes:
24583 // auto [a, b] = p;
24584 // #pragma omp target map(tofrom:a) map(to:b)
24585 // we transform it to:
24586 // #pragma omp target map(tofrom:p.x) map(to:p.y)
24587 // This avoids conflicts when different bindings have different map types.
24588 if (auto *BD = dyn_cast<BindingDecl>(Val: D)) {
24589 auto *DD = cast<DecompositionDecl>(Val: BD->getDecomposedDecl());
24590 Expr *BindingExpr = BD->getBinding();
24591
24592 // Check if the binding is a member expression (struct/class
24593 // decomposition).
24594 if (auto *ME = dyn_cast_or_null<MemberExpr>(Val: BindingExpr)) {
24595
24596 // Use the DecompositionDecl as the base for the member expression.
24597 // The structured binding creates a copy (if initialized from a
24598 // variable) or holds the only storage (if initialized from a prvalue).
24599 // Using DD ensures map clauses reference the correct storage.
24600 Expr *BaseExpr =
24601 SemaRef
24602 .BuildDeclarationNameExpr(
24603 SS: CXXScopeSpec(),
24604 NameInfo: DeclarationNameInfo(DD->getDeclName(), DRE->getLocation()),
24605 D: DD)
24606 .get();
24607
24608 // Create member expression: base.member.
24609 E = MemberExpr::Create(
24610 C: SemaRef.Context, Base: BaseExpr, /*IsArrow=*/false, OperatorLoc: ME->getOperatorLoc(),
24611 QualifierLoc: ME->getQualifierLoc(), TemplateKWLoc: ME->getTemplateKeywordLoc(),
24612 MemberDecl: ME->getMemberDecl(), FoundDecl: ME->getFoundDecl(), MemberNameInfo: ME->getMemberNameInfo(),
24613 /*TemplateArgs=*/nullptr, T: ME->getType(), VK: ME->getValueKind(),
24614 OK: ME->getObjectKind(), NOUR: ME->isNonOdrUse());
24615
24616 // Now process this as a member expression, which will properly
24617 // handle the field-level mapping.
24618 return Visit(S: E);
24619 }
24620 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(Val: BindingExpr)) {
24621 Expr *BaseExpr =
24622 SemaRef
24623 .BuildDeclarationNameExpr(
24624 SS: CXXScopeSpec(),
24625 NameInfo: DeclarationNameInfo(DD->getDeclName(), DRE->getLocation()),
24626 D: DD)
24627 .get();
24628 E = new (SemaRef.Context) ArraySubscriptExpr(
24629 BaseExpr, ASE->getIdx(), ASE->getType(), ASE->getValueKind(),
24630 ASE->getObjectKind(), ASE->getRBracketLoc());
24631 return Visit(S: E);
24632 }
24633 // Tuple-like should already be rejected; do not map DD as a silent
24634 // fallback.
24635 SemaRef.Diag(Loc: DRE->getExprLoc(),
24636 DiagID: diag::err_omp_unsupported_structured_binding_init)
24637 << 4;
24638 return false;
24639 }
24640 // Handle DecompositionDecl directly (implicit captures).
24641 else if (auto *DD = dyn_cast<DecompositionDecl>(Val: D)) {
24642 if (const VarDecl *OrigVar =
24643 getOriginalVarOrDiagnose(S&: SemaRef, DD, Loc: DRE->getExprLoc())) {
24644 D = const_cast<VarDecl *>(OrigVar);
24645 DeclarationNameInfo NameInfo(D->getDeclName(), DRE->getLocation());
24646 E = DeclRefExpr::Create(Context: SemaRef.Context, QualifierLoc: DRE->getQualifierLoc(),
24647 TemplateKWLoc: DRE->getTemplateKeywordLoc(), D,
24648 /*RefersToEnclosingVariableOrCapture=*/false,
24649 NameInfo, T: D->getType(), VK: DRE->getValueKind(),
24650 FoundD: DRE->getFoundDecl(),
24651 /*TemplateArgs=*/nullptr, NOUR: DRE->isNonOdrUse());
24652 } else {
24653 return false;
24654 }
24655 } else if (!isa<VarDecl>(Val: D)) {
24656 emitErrorMsg();
24657 return false;
24658 }
24659 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
24660 RelevantExpr = E;
24661 // Record the component.
24662 Components.emplace_back(Args&: E, Args&: D, Args&: IsNonContiguous);
24663 return true;
24664 }
24665
24666 bool VisitMemberExpr(MemberExpr *ME) {
24667 Expr *E = ME;
24668 Expr *BaseE = ME->getBase()->IgnoreParenCasts();
24669
24670 if (isa<CXXThisExpr>(Val: BaseE)) {
24671 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
24672 // We found a base expression: this->Val.
24673 RelevantExpr = ME;
24674 } else {
24675 E = BaseE;
24676 }
24677
24678 if (!isa<FieldDecl>(Val: ME->getMemberDecl())) {
24679 if (!NoDiagnose) {
24680 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_expected_access_to_data_field)
24681 << ME->getSourceRange();
24682 return false;
24683 }
24684 if (RelevantExpr)
24685 return false;
24686 return Visit(S: E);
24687 }
24688
24689 auto *FD = cast<FieldDecl>(Val: ME->getMemberDecl());
24690
24691 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
24692 // A bit-field cannot appear in a map clause.
24693 //
24694 if (FD->isBitField()) {
24695 if (!NoDiagnose) {
24696 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_bit_fields_forbidden_in_clause)
24697 << ME->getSourceRange() << getOpenMPClauseNameForDiag(C: CKind);
24698 return false;
24699 }
24700 if (RelevantExpr)
24701 return false;
24702 return Visit(S: E);
24703 }
24704
24705 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
24706 // If the type of a list item is a reference to a type T then the type
24707 // will be considered to be T for all purposes of this clause.
24708 QualType CurType = BaseE->getType().getNonReferenceType();
24709
24710 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
24711 // A list item cannot be a variable that is a member of a structure with
24712 // a union type.
24713 //
24714 if (CurType->isUnionType()) {
24715 if (!NoDiagnose) {
24716 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_union_type_not_allowed)
24717 << ME->getSourceRange();
24718 return false;
24719 }
24720 return RelevantExpr || Visit(S: E);
24721 }
24722
24723 // If we got a member expression, we should not expect any array section
24724 // before that:
24725 //
24726 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
24727 // If a list item is an element of a structure, only the rightmost symbol
24728 // of the variable reference can be an array section.
24729 //
24730 AllowUnitySizeArraySection = false;
24731 AllowWholeSizeArraySection = false;
24732
24733 // Record the component.
24734 Components.emplace_back(Args&: ME, Args&: FD, Args&: IsNonContiguous);
24735 return RelevantExpr || Visit(S: E);
24736 }
24737
24738 bool VisitArraySubscriptExpr(ArraySubscriptExpr *AE) {
24739 Expr *E = AE->getBase()->IgnoreParenImpCasts();
24740
24741 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
24742 if (!NoDiagnose) {
24743 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_expected_base_var_name)
24744 << 0 << AE->getSourceRange();
24745 return false;
24746 }
24747 return RelevantExpr || Visit(S: E);
24748 }
24749
24750 // If we got an array subscript that express the whole dimension we
24751 // can have any array expressions before. If it only expressing part of
24752 // the dimension, we can only have unitary-size array expressions.
24753 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, E: AE, BaseQTy: E->getType()))
24754 AllowWholeSizeArraySection = false;
24755
24756 if (const auto *TE = dyn_cast<CXXThisExpr>(Val: E->IgnoreParenCasts())) {
24757 Expr::EvalResult Result;
24758 if (!AE->getIdx()->isValueDependent() &&
24759 AE->getIdx()->EvaluateAsInt(Result, Ctx: SemaRef.getASTContext()) &&
24760 !Result.Val.getInt().isZero()) {
24761 SemaRef.Diag(Loc: AE->getIdx()->getExprLoc(),
24762 DiagID: diag::err_omp_invalid_map_this_expr);
24763 SemaRef.Diag(Loc: AE->getIdx()->getExprLoc(),
24764 DiagID: diag::note_omp_invalid_subscript_on_this_ptr_map);
24765 }
24766 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
24767 RelevantExpr = TE;
24768 }
24769
24770 // Record the component - we don't have any declaration associated.
24771 Components.emplace_back(Args&: AE, Args: nullptr, Args&: IsNonContiguous);
24772
24773 return RelevantExpr || Visit(S: E);
24774 }
24775
24776 bool VisitArraySectionExpr(ArraySectionExpr *OASE) {
24777 // After OMP 5.0 Array section in reduction clause will be implicitly
24778 // mapped
24779 assert(!(SemaRef.getLangOpts().OpenMP < 50 && NoDiagnose) &&
24780 "Array sections cannot be implicitly mapped.");
24781 Expr *E = OASE->getBase()->IgnoreParenImpCasts();
24782 QualType CurType =
24783 ArraySectionExpr::getBaseOriginalType(Base: E).getCanonicalType();
24784
24785 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
24786 // If the type of a list item is a reference to a type T then the type
24787 // will be considered to be T for all purposes of this clause.
24788 if (CurType->isReferenceType())
24789 CurType = CurType->getPointeeType();
24790
24791 bool IsPointer = CurType->isAnyPointerType();
24792
24793 if (!IsPointer && !CurType->isArrayType()) {
24794 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_expected_base_var_name)
24795 << 0 << OASE->getSourceRange();
24796 return false;
24797 }
24798
24799 bool NotWhole =
24800 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, E: OASE, BaseQTy: CurType);
24801 bool NotUnity =
24802 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, E: OASE, BaseQTy: CurType);
24803
24804 if (AllowWholeSizeArraySection) {
24805 // Any array section is currently allowed. Allowing a whole size array
24806 // section implies allowing a unity array section as well.
24807 //
24808 // If this array section refers to the whole dimension we can still
24809 // accept other array sections before this one, except if the base is a
24810 // pointer. Otherwise, only unitary sections are accepted.
24811 if (NotWhole || IsPointer)
24812 AllowWholeSizeArraySection = false;
24813 } else if (DKind == OMPD_target_update &&
24814 SemaRef.getLangOpts().OpenMP >= 50) {
24815 if (IsPointer && !AllowAnotherPtr)
24816 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_section_length_undefined)
24817 << /*array of unknown bound */ 1;
24818 else
24819 IsNonContiguous = true;
24820 } else if (AllowUnitySizeArraySection && NotUnity) {
24821 // A unity or whole array section is not allowed and that is not
24822 // compatible with the properties of the current array section.
24823 if (NoDiagnose)
24824 return false;
24825 SemaRef.Diag(Loc: ELoc,
24826 DiagID: diag::err_array_section_does_not_specify_contiguous_storage)
24827 << OASE->getSourceRange();
24828 return false;
24829 }
24830
24831 if (IsPointer)
24832 AllowAnotherPtr = false;
24833
24834 if (const auto *TE = dyn_cast<CXXThisExpr>(Val: E)) {
24835 Expr::EvalResult ResultR;
24836 Expr::EvalResult ResultL;
24837 if (!OASE->getLength()->isValueDependent() &&
24838 OASE->getLength()->EvaluateAsInt(Result&: ResultR, Ctx: SemaRef.getASTContext()) &&
24839 !ResultR.Val.getInt().isOne()) {
24840 SemaRef.Diag(Loc: OASE->getLength()->getExprLoc(),
24841 DiagID: diag::err_omp_invalid_map_this_expr);
24842 SemaRef.Diag(Loc: OASE->getLength()->getExprLoc(),
24843 DiagID: diag::note_omp_invalid_length_on_this_ptr_mapping);
24844 }
24845 if (OASE->getLowerBound() && !OASE->getLowerBound()->isValueDependent() &&
24846 OASE->getLowerBound()->EvaluateAsInt(Result&: ResultL,
24847 Ctx: SemaRef.getASTContext()) &&
24848 !ResultL.Val.getInt().isZero()) {
24849 SemaRef.Diag(Loc: OASE->getLowerBound()->getExprLoc(),
24850 DiagID: diag::err_omp_invalid_map_this_expr);
24851 SemaRef.Diag(Loc: OASE->getLowerBound()->getExprLoc(),
24852 DiagID: diag::note_omp_invalid_lower_bound_on_this_ptr_mapping);
24853 }
24854 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
24855 RelevantExpr = TE;
24856 }
24857
24858 // Record the component - we don't have any declaration associated.
24859 Components.emplace_back(Args&: OASE, Args: nullptr, /*IsNonContiguous=*/Args: false);
24860 return RelevantExpr || Visit(S: E);
24861 }
24862 bool VisitOMPArrayShapingExpr(OMPArrayShapingExpr *E) {
24863 Expr *Base = E->getBase();
24864
24865 // Record the component - we don't have any declaration associated.
24866 Components.emplace_back(Args&: E, Args: nullptr, Args&: IsNonContiguous);
24867
24868 return Visit(S: Base->IgnoreParenImpCasts());
24869 }
24870
24871 bool VisitUnaryOperator(UnaryOperator *UO) {
24872 if (SemaRef.getLangOpts().OpenMP < 50 || !UO->isLValue() ||
24873 UO->getOpcode() != UO_Deref) {
24874 emitErrorMsg();
24875 return false;
24876 }
24877 if (!RelevantExpr) {
24878 // Record the component if haven't found base decl.
24879 Components.emplace_back(Args&: UO, Args: nullptr, /*IsNonContiguous=*/Args: false);
24880 }
24881 return RelevantExpr || Visit(S: UO->getSubExpr()->IgnoreParenImpCasts());
24882 }
24883 bool VisitBinaryOperator(BinaryOperator *BO) {
24884 // Since OpenMP 5.0 only pointer arithmetic (pointer +/- integer offset)
24885 // may lead to the base, which is in the pointer operand.
24886 Expr *PtrE = nullptr;
24887 if (SemaRef.getLangOpts().OpenMP >= 50 && BO->isAdditiveOp() &&
24888 BO->getType()->isPointerType()) {
24889 Expr *LE = BO->getLHS()->IgnoreParenImpCasts();
24890 Expr *RE = BO->getRHS()->IgnoreParenImpCasts();
24891 if (LE->getType()->isPointerType())
24892 PtrE = LE;
24893 else if (BO->getOpcode() == BO_Add && RE->getType()->isPointerType())
24894 PtrE = RE;
24895 }
24896 if (!PtrE) {
24897 emitErrorMsg();
24898 return false;
24899 }
24900 Components.emplace_back(Args&: BO, Args: nullptr, /*IsNonContiguous=*/Args: false);
24901 return RelevantExpr || Visit(S: PtrE);
24902 }
24903 bool VisitCXXThisExpr(CXXThisExpr *CTE) {
24904 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
24905 RelevantExpr = CTE;
24906 Components.emplace_back(Args&: CTE, Args: nullptr, Args&: IsNonContiguous);
24907 return true;
24908 }
24909 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *COCE) {
24910 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
24911 Components.emplace_back(Args&: COCE, Args: nullptr, Args&: IsNonContiguous);
24912 return true;
24913 }
24914 bool VisitOpaqueValueExpr(OpaqueValueExpr *E) {
24915 Expr *Source = E->getSourceExpr();
24916 if (!Source) {
24917 emitErrorMsg();
24918 return false;
24919 }
24920 return Visit(S: Source);
24921 }
24922 bool VisitStmt(Stmt *) {
24923 emitErrorMsg();
24924 return false;
24925 }
24926 const Expr *getFoundBase() const { return RelevantExpr; }
24927 explicit MapBaseChecker(
24928 Sema &SemaRef, OpenMPClauseKind CKind, OpenMPDirectiveKind DKind,
24929 OMPClauseMappableExprCommon::MappableExprComponentList &Components,
24930 bool NoDiagnose, SourceLocation &ELoc, SourceRange &ERange)
24931 : SemaRef(SemaRef), CKind(CKind), DKind(DKind), Components(Components),
24932 NoDiagnose(NoDiagnose), ELoc(ELoc), ERange(ERange) {}
24933};
24934} // namespace
24935
24936/// Return the expression of the base of the mappable expression or null if it
24937/// cannot be determined and do all the necessary checks to see if the
24938/// expression is valid as a standalone mappable expression. In the process,
24939/// record all the components of the expression.
24940static const Expr *checkMapClauseExpressionBase(
24941 Sema &SemaRef, Expr *E,
24942 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
24943 OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, bool NoDiagnose) {
24944 SourceLocation ELoc = E->getExprLoc();
24945 SourceRange ERange = E->getSourceRange();
24946 MapBaseChecker Checker(SemaRef, CKind, DKind, CurComponents, NoDiagnose, ELoc,
24947 ERange);
24948 if (Checker.Visit(S: E->IgnoreParens())) {
24949 // Check if the highest dimension array section has length specified
24950 if (SemaRef.getLangOpts().OpenMP >= 50 && !CurComponents.empty() &&
24951 (CKind == OMPC_to || CKind == OMPC_from)) {
24952 auto CI = CurComponents.rbegin();
24953 auto CE = CurComponents.rend();
24954 for (; CI != CE; ++CI) {
24955 const auto *OASE =
24956 dyn_cast<ArraySectionExpr>(Val: CI->getAssociatedExpression());
24957 if (!OASE)
24958 continue;
24959 if (OASE && OASE->getLength())
24960 break;
24961 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_array_section_does_not_specify_length)
24962 << ERange;
24963 }
24964 }
24965 return Checker.getFoundBase();
24966 }
24967 return nullptr;
24968}
24969
24970// Return true if expression E associated with value VD has conflicts with other
24971// map information.
24972static bool checkMapConflicts(
24973 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
24974 bool CurrentRegionOnly,
24975 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
24976 OpenMPClauseKind CKind) {
24977 assert(VD && E);
24978 SourceLocation ELoc = E->getExprLoc();
24979 SourceRange ERange = E->getSourceRange();
24980
24981 // In order to easily check the conflicts we need to match each component of
24982 // the expression under test with the components of the expressions that are
24983 // already in the stack.
24984
24985 assert(!CurComponents.empty() && "Map clause expression with no components!");
24986 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
24987 "Map clause expression with unexpected base!");
24988
24989 // Variables to help detecting enclosing problems in data environment nests.
24990 bool IsEnclosedByDataEnvironmentExpr = false;
24991 const Expr *EnclosingExpr = nullptr;
24992
24993 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
24994 VD, CurrentRegionOnly,
24995 Check: [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
24996 ERange, CKind, &EnclosingExpr,
24997 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
24998 StackComponents,
24999 OpenMPClauseKind Kind) {
25000 if (CKind == Kind && SemaRef.LangOpts.OpenMP >= 50)
25001 return false;
25002 assert(!StackComponents.empty() &&
25003 "Map clause expression with no components!");
25004 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
25005 "Map clause expression with unexpected base!");
25006 (void)VD;
25007
25008 // The whole expression in the stack.
25009 const Expr *RE = StackComponents.front().getAssociatedExpression();
25010
25011 // Expressions must start from the same base. Here we detect at which
25012 // point both expressions diverge from each other and see if we can
25013 // detect if the memory referred to both expressions is contiguous and
25014 // do not overlap.
25015 auto CI = CurComponents.rbegin();
25016 auto CE = CurComponents.rend();
25017 auto SI = StackComponents.rbegin();
25018 auto SE = StackComponents.rend();
25019 for (; CI != CE && SI != SE; ++CI, ++SI) {
25020
25021 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
25022 // At most one list item can be an array item derived from a given
25023 // variable in map clauses of the same construct.
25024 if (CurrentRegionOnly &&
25025 (isa<ArraySubscriptExpr>(Val: CI->getAssociatedExpression()) ||
25026 isa<ArraySectionExpr>(Val: CI->getAssociatedExpression()) ||
25027 isa<OMPArrayShapingExpr>(Val: CI->getAssociatedExpression())) &&
25028 (isa<ArraySubscriptExpr>(Val: SI->getAssociatedExpression()) ||
25029 isa<ArraySectionExpr>(Val: SI->getAssociatedExpression()) ||
25030 isa<OMPArrayShapingExpr>(Val: SI->getAssociatedExpression()))) {
25031 SemaRef.Diag(Loc: CI->getAssociatedExpression()->getExprLoc(),
25032 DiagID: diag::err_omp_multiple_array_items_in_map_clause)
25033 << CI->getAssociatedExpression()->getSourceRange();
25034 SemaRef.Diag(Loc: SI->getAssociatedExpression()->getExprLoc(),
25035 DiagID: diag::note_used_here)
25036 << SI->getAssociatedExpression()->getSourceRange();
25037 return true;
25038 }
25039
25040 // Do both expressions have the same kind?
25041 if (CI->getAssociatedExpression()->getStmtClass() !=
25042 SI->getAssociatedExpression()->getStmtClass())
25043 break;
25044
25045 // Are we dealing with different variables/fields?
25046 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
25047 break;
25048 }
25049 // Check if the extra components of the expressions in the enclosing
25050 // data environment are redundant for the current base declaration.
25051 // If they are, the maps completely overlap, which is legal.
25052 for (; SI != SE; ++SI) {
25053 QualType Type;
25054 if (const auto *ASE =
25055 dyn_cast<ArraySubscriptExpr>(Val: SI->getAssociatedExpression())) {
25056 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
25057 } else if (const auto *OASE = dyn_cast<ArraySectionExpr>(
25058 Val: SI->getAssociatedExpression())) {
25059 const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
25060 Type = ArraySectionExpr::getBaseOriginalType(Base: E).getCanonicalType();
25061 } else if (const auto *OASE = dyn_cast<OMPArrayShapingExpr>(
25062 Val: SI->getAssociatedExpression())) {
25063 Type = OASE->getBase()->getType()->getPointeeType();
25064 }
25065 if (Type.isNull() || Type->isAnyPointerType() ||
25066 checkArrayExpressionDoesNotReferToWholeSize(
25067 SemaRef, E: SI->getAssociatedExpression(), BaseQTy: Type))
25068 break;
25069 }
25070
25071 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
25072 // List items of map clauses in the same construct must not share
25073 // original storage.
25074 //
25075 // If the expressions are exactly the same or one is a subset of the
25076 // other, it means they are sharing storage.
25077 if (CI == CE && SI == SE) {
25078 if (CurrentRegionOnly) {
25079 if (CKind == OMPC_map) {
25080 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_map_shared_storage) << ERange;
25081 } else {
25082 assert(CKind == OMPC_to || CKind == OMPC_from);
25083 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_once_referenced_in_target_update)
25084 << ERange;
25085 }
25086 SemaRef.Diag(Loc: RE->getExprLoc(), DiagID: diag::note_used_here)
25087 << RE->getSourceRange();
25088 return true;
25089 }
25090 // If we find the same expression in the enclosing data environment,
25091 // that is legal.
25092 IsEnclosedByDataEnvironmentExpr = true;
25093 return false;
25094 }
25095
25096 QualType DerivedType =
25097 std::prev(x: CI)->getAssociatedDeclaration()->getType();
25098 SourceLocation DerivedLoc =
25099 std::prev(x: CI)->getAssociatedExpression()->getExprLoc();
25100
25101 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
25102 // If the type of a list item is a reference to a type T then the type
25103 // will be considered to be T for all purposes of this clause.
25104 DerivedType = DerivedType.getNonReferenceType();
25105
25106 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
25107 // A variable for which the type is pointer and an array section
25108 // derived from that variable must not appear as list items of map
25109 // clauses of the same construct.
25110 //
25111 // Also, cover one of the cases in:
25112 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
25113 // If any part of the original storage of a list item has corresponding
25114 // storage in the device data environment, all of the original storage
25115 // must have corresponding storage in the device data environment.
25116 //
25117 if (DerivedType->isAnyPointerType()) {
25118 if (CI == CE || SI == SE) {
25119 SemaRef.Diag(
25120 Loc: DerivedLoc,
25121 DiagID: diag::err_omp_pointer_mapped_along_with_derived_section)
25122 << DerivedLoc;
25123 SemaRef.Diag(Loc: RE->getExprLoc(), DiagID: diag::note_used_here)
25124 << RE->getSourceRange();
25125 return true;
25126 }
25127 if (CI->getAssociatedExpression()->getStmtClass() !=
25128 SI->getAssociatedExpression()->getStmtClass() ||
25129 CI->getAssociatedDeclaration()->getCanonicalDecl() ==
25130 SI->getAssociatedDeclaration()->getCanonicalDecl()) {
25131 assert(CI != CE && SI != SE);
25132 SemaRef.Diag(Loc: DerivedLoc, DiagID: diag::err_omp_same_pointer_dereferenced)
25133 << DerivedLoc;
25134 SemaRef.Diag(Loc: RE->getExprLoc(), DiagID: diag::note_used_here)
25135 << RE->getSourceRange();
25136 return true;
25137 }
25138 }
25139
25140 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
25141 // List items of map clauses in the same construct must not share
25142 // original storage.
25143 //
25144 // An expression is a subset of the other.
25145 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
25146 if (CKind == OMPC_map) {
25147 if (CI != CE || SI != SE) {
25148 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
25149 // a pointer.
25150 auto Begin =
25151 CI != CE ? CurComponents.begin() : StackComponents.begin();
25152 auto End = CI != CE ? CurComponents.end() : StackComponents.end();
25153 auto It = Begin;
25154 while (It != End && !It->getAssociatedDeclaration())
25155 std::advance(i&: It, n: 1);
25156 assert(It != End &&
25157 "Expected at least one component with the declaration.");
25158 if (It != Begin && It->getAssociatedDeclaration()
25159 ->getType()
25160 .getCanonicalType()
25161 ->isAnyPointerType()) {
25162 IsEnclosedByDataEnvironmentExpr = false;
25163 EnclosingExpr = nullptr;
25164 return false;
25165 }
25166 }
25167 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_map_shared_storage) << ERange;
25168 } else {
25169 assert(CKind == OMPC_to || CKind == OMPC_from);
25170 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_once_referenced_in_target_update)
25171 << ERange;
25172 }
25173 SemaRef.Diag(Loc: RE->getExprLoc(), DiagID: diag::note_used_here)
25174 << RE->getSourceRange();
25175 return true;
25176 }
25177
25178 // The current expression uses the same base as other expression in the
25179 // data environment but does not contain it completely.
25180 if (!CurrentRegionOnly && SI != SE)
25181 EnclosingExpr = RE;
25182
25183 // The current expression is a subset of the expression in the data
25184 // environment.
25185 IsEnclosedByDataEnvironmentExpr |=
25186 (!CurrentRegionOnly && CI != CE && SI == SE);
25187
25188 return false;
25189 });
25190
25191 if (CurrentRegionOnly)
25192 return FoundError;
25193
25194 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
25195 // If any part of the original storage of a list item has corresponding
25196 // storage in the device data environment, all of the original storage must
25197 // have corresponding storage in the device data environment.
25198 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
25199 // If a list item is an element of a structure, and a different element of
25200 // the structure has a corresponding list item in the device data environment
25201 // prior to a task encountering the construct associated with the map clause,
25202 // then the list item must also have a corresponding list item in the device
25203 // data environment prior to the task encountering the construct.
25204 //
25205 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
25206 SemaRef.Diag(Loc: ELoc,
25207 DiagID: diag::err_omp_original_storage_is_shared_and_does_not_contain)
25208 << ERange;
25209 SemaRef.Diag(Loc: EnclosingExpr->getExprLoc(), DiagID: diag::note_used_here)
25210 << EnclosingExpr->getSourceRange();
25211 return true;
25212 }
25213
25214 return FoundError;
25215}
25216
25217// Look up the user-defined mapper given the mapper name and mapped type, and
25218// build a reference to it. \a ItemLoc is the location of the mapped list item;
25219// it is used as the point of instantiation since \a MapperId has no location
25220// for implicit map clauses.
25221static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S,
25222 CXXScopeSpec &MapperIdScopeSpec,
25223 const DeclarationNameInfo &MapperId,
25224 QualType Type,
25225 Expr *UnresolvedMapper,
25226 SourceLocation ItemLoc) {
25227 if (MapperIdScopeSpec.isInvalid())
25228 return ExprError();
25229 // Get the actual type for the array type.
25230 if (Type->isArrayType()) {
25231 assert(Type->getAsArrayTypeUnsafe() && "Expect to get a valid array type");
25232 Type = Type->getAsArrayTypeUnsafe()->getElementType().getCanonicalType();
25233 }
25234 // Find all user-defined mappers with the given MapperId.
25235 SmallVector<UnresolvedSet<8>, 4> Lookups;
25236 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName);
25237 Lookup.suppressDiagnostics();
25238 if (S) {
25239 while (S && SemaRef.LookupParsedName(R&: Lookup, S, SS: &MapperIdScopeSpec,
25240 /*ObjectType=*/QualType())) {
25241 NamedDecl *D = Lookup.getRepresentativeDecl();
25242 while (S && !S->isDeclScope(D))
25243 S = S->getParent();
25244 if (S)
25245 S = S->getParent();
25246 Lookups.emplace_back();
25247 Lookups.back().append(I: Lookup.begin(), E: Lookup.end());
25248 Lookup.clear();
25249 }
25250 } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(Val: UnresolvedMapper)) {
25251 // Extract the user-defined mappers with the given MapperId.
25252 Lookups.push_back(Elt: UnresolvedSet<8>());
25253 for (NamedDecl *D : ULE->decls()) {
25254 auto *DMD = cast<OMPDeclareMapperDecl>(Val: D);
25255 assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation.");
25256 Lookups.back().addDecl(D: DMD);
25257 }
25258 }
25259 // Defer the lookup for dependent types. The results will be passed through
25260 // UnresolvedMapper on instantiation.
25261 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() ||
25262 Type->isInstantiationDependentType() ||
25263 Type->containsUnexpandedParameterPack() ||
25264 filterLookupForUDReductionAndMapper<bool>(Lookups, Gen: [](ValueDecl *D) {
25265 return !D->isInvalidDecl() &&
25266 (D->getType()->isDependentType() ||
25267 D->getType()->isInstantiationDependentType() ||
25268 D->getType()->containsUnexpandedParameterPack());
25269 })) {
25270 UnresolvedSet<8> URS;
25271 for (const UnresolvedSet<8> &Set : Lookups) {
25272 if (Set.empty())
25273 continue;
25274 URS.append(I: Set.begin(), E: Set.end());
25275 }
25276 return UnresolvedLookupExpr::Create(
25277 Context: SemaRef.Context, /*NamingClass=*/nullptr,
25278 QualifierLoc: MapperIdScopeSpec.getWithLocInContext(Context&: SemaRef.Context), NameInfo: MapperId,
25279 /*ADL=*/RequiresADL: false, Begin: URS.begin(), End: URS.end(), /*KnownDependent=*/false,
25280 /*KnownInstantiationDependent=*/false);
25281 }
25282 SourceLocation Loc = MapperId.getLoc();
25283 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
25284 // The type must be of struct, union or class type in C and C++
25285 if (!Type->isStructureOrClassType() && !Type->isUnionType() &&
25286 (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default")) {
25287 SemaRef.Diag(Loc, DiagID: diag::err_omp_mapper_wrong_type);
25288 return ExprError();
25289 }
25290 // Perform argument dependent lookup.
25291 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet())
25292 argumentDependentLookup(SemaRef, Id: MapperId, Loc: ItemLoc, Ty: Type, Lookups);
25293 // Return the first user-defined mapper with the desired type.
25294 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
25295 Lookups, Gen: [&SemaRef, Type](ValueDecl *D) -> ValueDecl * {
25296 if (!D->isInvalidDecl() &&
25297 SemaRef.Context.hasSameType(T1: D->getType(), T2: Type))
25298 return D;
25299 return nullptr;
25300 }))
25301 return SemaRef.BuildDeclRefExpr(D: VD, Ty: Type, VK: VK_LValue, Loc);
25302 // Find the first user-defined mapper with a type derived from the desired
25303 // type.
25304 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
25305 Lookups, Gen: [&SemaRef, Type, ItemLoc](ValueDecl *D) -> ValueDecl * {
25306 if (!D->isInvalidDecl() &&
25307 SemaRef.IsDerivedFrom(Loc: ItemLoc, Derived: Type, Base: D->getType()) &&
25308 !Type.isMoreQualifiedThan(other: D->getType(),
25309 Ctx: SemaRef.getASTContext()))
25310 return D;
25311 return nullptr;
25312 })) {
25313 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
25314 /*DetectVirtual=*/false);
25315 if (SemaRef.IsDerivedFrom(Loc: ItemLoc, Derived: Type, Base: VD->getType(), Paths)) {
25316 if (!Paths.isAmbiguous(BaseType: SemaRef.Context.getCanonicalType(
25317 T: VD->getType().getUnqualifiedType()))) {
25318 if (SemaRef.CheckBaseClassAccess(
25319 AccessLoc: ItemLoc, Base: VD->getType(), Derived: Type, Path: Paths.front(),
25320 /*DiagID=*/0) != Sema::AR_inaccessible) {
25321 return SemaRef.BuildDeclRefExpr(D: VD, Ty: Type, VK: VK_LValue, Loc);
25322 }
25323 }
25324 }
25325 }
25326 // Report error if a mapper is specified, but cannot be found.
25327 if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") {
25328 SemaRef.Diag(Loc, DiagID: diag::err_omp_invalid_mapper)
25329 << Type << MapperId.getName();
25330 return ExprError();
25331 }
25332 return ExprEmpty();
25333}
25334
25335namespace {
25336// Utility struct that gathers all the related lists associated with a mappable
25337// expression.
25338struct MappableVarListInfo {
25339 // The list of expressions.
25340 ArrayRef<Expr *> VarList;
25341 // The list of processed expressions.
25342 SmallVector<Expr *, 16> ProcessedVarList;
25343 // The mappble components for each expression.
25344 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
25345 // The base declaration of the variable.
25346 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
25347 // The reference to the user-defined mapper associated with every expression.
25348 SmallVector<Expr *, 16> UDMapperList;
25349
25350 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
25351 // We have a list of components and base declarations for each entry in the
25352 // variable list.
25353 VarComponents.reserve(N: VarList.size());
25354 VarBaseDeclarations.reserve(N: VarList.size());
25355 }
25356};
25357} // namespace
25358
25359static DeclRefExpr *buildImplicitMap(Sema &S, QualType BaseType,
25360 DSAStackTy *Stack,
25361 SmallVectorImpl<OMPClause *> &Maps) {
25362
25363 const RecordDecl *RD = BaseType->getAsRecordDecl();
25364 SourceRange Range = RD->getSourceRange();
25365 DeclarationNameInfo ImplicitName;
25366 // Dummy variable _s for Mapper.
25367 VarDecl *VD = buildVarDecl(SemaRef&: S, Loc: Range.getEnd(), Type: BaseType, Name: "_s");
25368 DeclRefExpr *MapperVarRef =
25369 buildDeclRefExpr(S, D: VD, Ty: BaseType, Loc: SourceLocation());
25370
25371 // Create implicit map clause for mapper.
25372 SmallVector<Expr *, 4> SExprs;
25373 for (auto *FD : RD->fields()) {
25374 Expr *BE = S.BuildMemberExpr(
25375 Base: MapperVarRef, /*IsArrow=*/false, OpLoc: Range.getBegin(),
25376 NNS: NestedNameSpecifierLoc(), TemplateKWLoc: Range.getBegin(), Member: FD,
25377 FoundDecl: DeclAccessPair::make(D: FD, AS: FD->getAccess()),
25378 /*HadMultipleCandidates=*/false,
25379 MemberNameInfo: DeclarationNameInfo(FD->getDeclName(), FD->getSourceRange().getBegin()),
25380 Ty: FD->getType(), VK: VK_LValue, OK: OK_Ordinary);
25381 SExprs.push_back(Elt: BE);
25382 }
25383 CXXScopeSpec MapperIdScopeSpec;
25384 DeclarationNameInfo MapperId;
25385 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
25386
25387 OMPClause *MapClause = S.OpenMP().ActOnOpenMPMapClause(
25388 IteratorModifier: nullptr, MapTypeModifiers: OMPC_MAP_MODIFIER_unknown, MapTypeModifiersLoc: SourceLocation(), MapperIdScopeSpec,
25389 MapperId, MapType: DKind == OMPD_target_enter_data ? OMPC_MAP_to : OMPC_MAP_tofrom,
25390 /*IsMapTypeImplicit=*/true, MapLoc: SourceLocation(), ColonLoc: SourceLocation(), VarList: SExprs,
25391 Locs: OMPVarListLocTy());
25392 Maps.push_back(Elt: MapClause);
25393 return MapperVarRef;
25394}
25395
25396static ExprResult buildImplicitMapper(Sema &S, QualType BaseType,
25397 DSAStackTy *Stack) {
25398
25399 // Build impilicit map for mapper
25400 SmallVector<OMPClause *, 4> Maps;
25401 DeclRefExpr *MapperVarRef = buildImplicitMap(S, BaseType, Stack, Maps);
25402
25403 const RecordDecl *RD = BaseType->getAsRecordDecl();
25404 // AST context is RD's ParentASTContext().
25405 ASTContext &Ctx = RD->getParentASTContext();
25406 // DeclContext is RD's DeclContext.
25407 DeclContext *DCT = const_cast<DeclContext *>(RD->getDeclContext());
25408
25409 // Create implicit default mapper for "RD".
25410 DeclarationName MapperId;
25411 auto &DeclNames = Ctx.DeclarationNames;
25412 MapperId = DeclNames.getIdentifier(ID: &Ctx.Idents.get(Name: "default"));
25413 auto *DMD = OMPDeclareMapperDecl::Create(C&: Ctx, DC: DCT, L: SourceLocation(), Name: MapperId,
25414 T: BaseType, VarName: MapperId, Clauses: Maps, PrevDeclInScope: nullptr);
25415 Scope *Scope = S.getScopeForContext(Ctx: DCT);
25416 if (Scope)
25417 S.PushOnScopeChains(D: DMD, S: Scope, /*AddToContext=*/false);
25418 DCT->addDecl(D: DMD);
25419 DMD->setAccess(clang::AS_none);
25420 auto *VD = cast<DeclRefExpr>(Val: MapperVarRef)->getDecl();
25421 VD->setDeclContext(DMD);
25422 VD->setLexicalDeclContext(DMD);
25423 DMD->addDecl(D: VD);
25424 DMD->setMapperVarRef(MapperVarRef);
25425 FieldDecl *FD = *RD->field_begin();
25426 // create mapper refence.
25427 return DeclRefExpr::Create(Context: Ctx, QualifierLoc: NestedNameSpecifierLoc{}, TemplateKWLoc: FD->getLocation(),
25428 D: DMD, RefersToEnclosingVariableOrCapture: false, NameLoc: SourceLocation(), T: BaseType, VK: VK_LValue);
25429}
25430
25431// Look up the user-defined mapper given the mapper name and mapper type,
25432// return true if found one.
25433static bool hasUserDefinedMapper(Sema &SemaRef, Scope *S,
25434 CXXScopeSpec &MapperIdScopeSpec,
25435 const DeclarationNameInfo &MapperId,
25436 QualType Type) {
25437 // Find all user-defined mappers with the given MapperId.
25438 SmallVector<UnresolvedSet<8>, 4> Lookups;
25439 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName);
25440 Lookup.suppressDiagnostics();
25441 while (S && SemaRef.LookupParsedName(R&: Lookup, S, SS: &MapperIdScopeSpec,
25442 /*ObjectType=*/QualType())) {
25443 NamedDecl *D = Lookup.getRepresentativeDecl();
25444 while (S && !S->isDeclScope(D))
25445 S = S->getParent();
25446 if (S)
25447 S = S->getParent();
25448 Lookups.emplace_back();
25449 Lookups.back().append(I: Lookup.begin(), E: Lookup.end());
25450 Lookup.clear();
25451 }
25452 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() ||
25453 Type->isInstantiationDependentType() ||
25454 Type->containsUnexpandedParameterPack() ||
25455 filterLookupForUDReductionAndMapper<bool>(Lookups, Gen: [](ValueDecl *D) {
25456 return !D->isInvalidDecl() &&
25457 (D->getType()->isDependentType() ||
25458 D->getType()->isInstantiationDependentType() ||
25459 D->getType()->containsUnexpandedParameterPack());
25460 }))
25461 return false;
25462 // Perform argument dependent lookup.
25463 SourceLocation Loc = MapperId.getLoc();
25464 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet())
25465 argumentDependentLookup(SemaRef, Id: MapperId, Loc, Ty: Type, Lookups);
25466 if (filterLookupForUDReductionAndMapper<ValueDecl *>(
25467 Lookups, Gen: [&SemaRef, Type](ValueDecl *D) -> ValueDecl * {
25468 if (!D->isInvalidDecl() &&
25469 SemaRef.Context.hasSameType(T1: D->getType(), T2: Type))
25470 return D;
25471 return nullptr;
25472 }))
25473 return true;
25474 // Find the first user-defined mapper with a type derived from the desired
25475 // type.
25476 auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
25477 Lookups, Gen: [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * {
25478 if (!D->isInvalidDecl() &&
25479 SemaRef.IsDerivedFrom(Loc, Derived: Type, Base: D->getType()) &&
25480 !Type.isMoreQualifiedThan(other: D->getType(), Ctx: SemaRef.getASTContext()))
25481 return D;
25482 return nullptr;
25483 });
25484 if (!VD)
25485 return false;
25486 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
25487 /*DetectVirtual=*/false);
25488 if (SemaRef.IsDerivedFrom(Loc, Derived: Type, Base: VD->getType(), Paths)) {
25489 bool IsAmbiguous = !Paths.isAmbiguous(
25490 BaseType: SemaRef.Context.getCanonicalType(T: VD->getType().getUnqualifiedType()));
25491 if (IsAmbiguous)
25492 return false;
25493 if (SemaRef.CheckBaseClassAccess(AccessLoc: Loc, Base: VD->getType(), Derived: Type, Path: Paths.front(),
25494 /*DiagID=*/0) != Sema::AR_inaccessible)
25495 return true;
25496 }
25497 return false;
25498}
25499
25500static bool isImplicitMapperNeeded(Sema &S, DSAStackTy *Stack,
25501 QualType CanonType, const Expr *E) {
25502
25503 // DFS over data members in structures/classes.
25504 SmallVector<std::pair<QualType, FieldDecl *>, 4> Types(1,
25505 {CanonType, nullptr});
25506 llvm::DenseMap<const Type *, bool> Visited;
25507 SmallVector<std::pair<FieldDecl *, unsigned>, 4> ParentChain(1, {nullptr, 1});
25508 while (!Types.empty()) {
25509 auto [BaseType, CurFD] = Types.pop_back_val();
25510 while (ParentChain.back().second == 0)
25511 ParentChain.pop_back();
25512 --ParentChain.back().second;
25513 if (BaseType.isNull())
25514 continue;
25515 // Only structs/classes are allowed to have mappers.
25516 const RecordDecl *RD = BaseType.getCanonicalType()->getAsRecordDecl();
25517 if (!RD)
25518 continue;
25519 auto It = Visited.find(Val: BaseType.getTypePtr());
25520 if (It == Visited.end()) {
25521 // Try to find the associated user-defined mapper.
25522 CXXScopeSpec MapperIdScopeSpec;
25523 DeclarationNameInfo DefaultMapperId;
25524 DefaultMapperId.setName(S.Context.DeclarationNames.getIdentifier(
25525 ID: &S.Context.Idents.get(Name: "default")));
25526 DefaultMapperId.setLoc(E->getExprLoc());
25527 bool HasUDMapper =
25528 hasUserDefinedMapper(SemaRef&: S, S: Stack->getCurScope(), MapperIdScopeSpec,
25529 MapperId: DefaultMapperId, Type: BaseType);
25530 It = Visited.try_emplace(Key: BaseType.getTypePtr(), Args&: HasUDMapper).first;
25531 }
25532 // Found default mapper.
25533 if (It->second)
25534 return true;
25535 // Check for the "default" mapper for data members.
25536 bool FirstIter = true;
25537 for (FieldDecl *FD : RD->fields()) {
25538 if (!FD)
25539 continue;
25540 QualType FieldTy = FD->getType();
25541 if (FieldTy.isNull() ||
25542 !(FieldTy->isStructureOrClassType() || FieldTy->isUnionType()))
25543 continue;
25544 if (FirstIter) {
25545 FirstIter = false;
25546 ParentChain.emplace_back(Args&: CurFD, Args: 1);
25547 } else {
25548 ++ParentChain.back().second;
25549 }
25550 Types.emplace_back(Args&: FieldTy, Args&: FD);
25551 }
25552 }
25553 return false;
25554}
25555
25556// Check the validity of the provided variable list for the provided clause kind
25557// \a CKind. In the check process the valid expressions, mappable expression
25558// components, variables, and user-defined mappers are extracted and used to
25559// fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a
25560// UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec,
25561// and \a MapperId are expected to be valid if the clause kind is 'map'.
25562static void checkMappableExpressionList(
25563 Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind,
25564 MappableVarListInfo &MVLI, SourceLocation StartLoc,
25565 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId,
25566 ArrayRef<Expr *> UnresolvedMappers,
25567 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
25568 ArrayRef<OpenMPMapModifierKind> Modifiers = {},
25569 bool IsMapTypeImplicit = false, bool NoDiagnose = false) {
25570 // We only expect mappable expressions in 'to', 'from', 'map', and
25571 // 'use_device_addr' clauses.
25572 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from ||
25573 CKind == OMPC_use_device_addr) &&
25574 "Unexpected clause kind with mappable expressions!");
25575 llvm::omp::Version OMPVersion = SemaRef.getLangOpts().getOpenMPVersion();
25576
25577 // If the identifier of user-defined mapper is not specified, it is "default".
25578 // We do not change the actual name in this clause to distinguish whether a
25579 // mapper is specified explicitly, i.e., it is not explicitly specified when
25580 // MapperId.getName() is empty.
25581 if (!MapperId.getName() || MapperId.getName().isEmpty()) {
25582 auto &DeclNames = SemaRef.getASTContext().DeclarationNames;
25583 MapperId.setName(DeclNames.getIdentifier(
25584 ID: &SemaRef.getASTContext().Idents.get(Name: "default")));
25585 MapperId.setLoc(StartLoc);
25586 }
25587
25588 // Iterators to find the current unresolved mapper expression.
25589 auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end();
25590 bool UpdateUMIt = false;
25591 Expr *UnresolvedMapper = nullptr;
25592
25593 bool HasHoldModifier =
25594 llvm::is_contained(Range&: Modifiers, Element: OMPC_MAP_MODIFIER_ompx_hold);
25595
25596 // Keep track of the mappable components and base declarations in this clause.
25597 // Each entry in the list is going to have a list of components associated. We
25598 // record each set of the components so that we can build the clause later on.
25599 // In the end we should have the same amount of declarations and component
25600 // lists.
25601
25602 for (Expr *RE : MVLI.VarList) {
25603 assert(RE && "Null expr in omp to/from/map clause");
25604 SourceLocation ELoc = RE->getExprLoc();
25605
25606 // Find the current unresolved mapper expression.
25607 if (UpdateUMIt && UMIt != UMEnd) {
25608 UMIt++;
25609 assert(
25610 UMIt != UMEnd &&
25611 "Expect the size of UnresolvedMappers to match with that of VarList");
25612 }
25613 UpdateUMIt = true;
25614 if (UMIt != UMEnd)
25615 UnresolvedMapper = *UMIt;
25616
25617 const Expr *VE = RE->IgnoreParenLValueCasts();
25618
25619 if (VE->isValueDependent() || VE->isTypeDependent() ||
25620 VE->isInstantiationDependent() ||
25621 VE->containsUnexpandedParameterPack()) {
25622 // Try to find the associated user-defined mapper.
25623 ExprResult ER = buildUserDefinedMapperRef(
25624 SemaRef, S: DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
25625 Type: VE->getType().getCanonicalType(), UnresolvedMapper, ItemLoc: ELoc);
25626 if (ER.isInvalid())
25627 continue;
25628 MVLI.UDMapperList.push_back(Elt: ER.get());
25629 // We can only analyze this information once the missing information is
25630 // resolved.
25631 MVLI.ProcessedVarList.push_back(Elt: RE);
25632 continue;
25633 }
25634
25635 Expr *SimpleExpr = RE->IgnoreParenCasts();
25636 if (!RE->isLValue()) {
25637 if (SemaRef.getLangOpts().OpenMP < 50) {
25638 SemaRef.Diag(
25639 Loc: ELoc, DiagID: diag::err_omp_expected_named_var_member_or_array_expression)
25640 << RE->getSourceRange();
25641 } else {
25642 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_non_lvalue_in_map_or_motion_clauses)
25643 << getOpenMPClauseNameForDiag(C: CKind) << RE->getSourceRange();
25644 }
25645 continue;
25646 }
25647
25648 // Check for unsupported structured bindings early.
25649 if (!NoDiagnose) {
25650 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: SimpleExpr)) {
25651 const DecompositionDecl *DD = nullptr;
25652 const BindingDecl *BD = nullptr;
25653 if (const auto *B = dyn_cast<BindingDecl>(Val: DRE->getDecl())) {
25654 BD = B;
25655 DD = cast<DecompositionDecl>(Val: B->getDecomposedDecl());
25656 } else if (const auto *D =
25657 dyn_cast<DecompositionDecl>(Val: DRE->getDecl())) {
25658 DD = D;
25659 }
25660 if (DD) {
25661 if (BD && BD->getHoldingVar()) {
25662 SemaRef.Diag(Loc: ELoc,
25663 DiagID: diag::err_omp_unsupported_structured_binding_init)
25664 << 4;
25665 continue;
25666 }
25667 if (!getOriginalVarOrDiagnose(S&: SemaRef, DD, Loc: ELoc))
25668 continue;
25669 }
25670 }
25671 }
25672 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
25673 ValueDecl *CurDeclaration = nullptr;
25674
25675 // Obtain the array or member expression bases if required. Also, fill the
25676 // components array with all the components identified in the process.
25677 const Expr *BE =
25678 checkMapClauseExpressionBase(SemaRef, E: SimpleExpr, CurComponents, CKind,
25679 DKind: DSAS->getCurrentDirective(), NoDiagnose);
25680 if (!BE)
25681 continue;
25682
25683 assert(!CurComponents.empty() &&
25684 "Invalid mappable expression information.");
25685
25686 if (const auto *TE = dyn_cast<CXXThisExpr>(Val: BE)) {
25687 // Add store "this" pointer to class in DSAStackTy for future checking
25688 DSAS->addMappedClassesQualTypes(QT: TE->getType());
25689 // Try to find the associated user-defined mapper.
25690 ExprResult ER = buildUserDefinedMapperRef(
25691 SemaRef, S: DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
25692 Type: VE->getType().getCanonicalType(), UnresolvedMapper, ItemLoc: ELoc);
25693 if (ER.isInvalid())
25694 continue;
25695 MVLI.UDMapperList.push_back(Elt: ER.get());
25696 // Skip restriction checking for variable or field declarations
25697 MVLI.ProcessedVarList.push_back(Elt: RE);
25698 MVLI.VarComponents.resize(N: MVLI.VarComponents.size() + 1);
25699 MVLI.VarComponents.back().append(in_start: CurComponents.begin(),
25700 in_end: CurComponents.end());
25701 MVLI.VarBaseDeclarations.push_back(Elt: nullptr);
25702 continue;
25703 }
25704
25705 // For the following checks, we rely on the base declaration which is
25706 // expected to be associated with the last component. The declaration is
25707 // expected to be a variable or a field (if 'this' is being mapped).
25708 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
25709 assert(CurDeclaration && "Null decl on map clause.");
25710 assert(
25711 CurDeclaration->isCanonicalDecl() &&
25712 "Expecting components to have associated only canonical declarations.");
25713
25714 auto *VD = dyn_cast<VarDecl>(Val: CurDeclaration);
25715 const auto *FD = dyn_cast<FieldDecl>(Val: CurDeclaration);
25716
25717 assert((VD || FD) && "Only variables or fields are expected here!");
25718 (void)FD;
25719
25720 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
25721 // threadprivate variables cannot appear in a map clause.
25722 // OpenMP 4.5 [2.10.5, target update Construct]
25723 // threadprivate variables cannot appear in a from clause.
25724 if (VD && DSAS->isThreadPrivate(D: VD)) {
25725 if (NoDiagnose)
25726 continue;
25727 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(D: VD, /*FromParent=*/false);
25728 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_threadprivate_in_clause)
25729 << getOpenMPClauseNameForDiag(C: CKind);
25730 reportOriginalDsa(SemaRef, Stack: DSAS, D: VD, DVar);
25731 continue;
25732 }
25733
25734 // OpenMP 6.0 [7.9.6, map Clause, Restrictions, p. 386]
25735 // A device-local variable must not appear as a list item in a map clause.
25736 if (VD && CKind == OMPC_map) {
25737 if (std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
25738 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
25739 if (*Res == OMPDeclareTargetDeclAttr::MT_Local) {
25740 if (NoDiagnose)
25741 continue;
25742 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_device_local_in_clause)
25743 << VD << getOpenMPClauseNameForDiag(C: CKind);
25744 continue;
25745 }
25746 }
25747 }
25748
25749 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
25750 // A list item cannot appear in both a map clause and a data-sharing
25751 // attribute clause on the same construct.
25752
25753 // Check conflicts with other map clause expressions. We check the conflicts
25754 // with the current construct separately from the enclosing data
25755 // environment, because the restrictions are different. We only have to
25756 // check conflicts across regions for the map clauses.
25757 if (checkMapConflicts(SemaRef, DSAS, VD: CurDeclaration, E: SimpleExpr,
25758 /*CurrentRegionOnly=*/true, CurComponents, CKind))
25759 break;
25760 if (CKind == OMPC_map &&
25761 (SemaRef.getLangOpts().OpenMP <= 45 || StartLoc.isValid()) &&
25762 checkMapConflicts(SemaRef, DSAS, VD: CurDeclaration, E: SimpleExpr,
25763 /*CurrentRegionOnly=*/false, CurComponents, CKind))
25764 break;
25765
25766 // OpenMP 4.5 [2.10.5, target update Construct]
25767 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
25768 // If the type of a list item is a reference to a type T then the type will
25769 // be considered to be T for all purposes of this clause.
25770 auto I = llvm::find_if(
25771 Range&: CurComponents,
25772 P: [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
25773 return MC.getAssociatedDeclaration();
25774 });
25775 assert(I != CurComponents.end() && "Null decl on map clause.");
25776 (void)I;
25777 QualType Type;
25778 auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: VE->IgnoreParens());
25779 auto *OASE = dyn_cast<ArraySectionExpr>(Val: VE->IgnoreParens());
25780 auto *OAShE = dyn_cast<OMPArrayShapingExpr>(Val: VE->IgnoreParens());
25781 if (ASE) {
25782 Type = ASE->getType().getNonReferenceType();
25783 } else if (OASE) {
25784 QualType BaseType =
25785 ArraySectionExpr::getBaseOriginalType(Base: OASE->getBase());
25786 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
25787 Type = ATy->getElementType();
25788 else
25789 Type = BaseType->getPointeeType();
25790 Type = Type.getNonReferenceType();
25791 } else if (OAShE) {
25792 Type = OAShE->getBase()->getType()->getPointeeType();
25793 } else {
25794 Type = VE->getType();
25795 }
25796
25797 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
25798 // A list item in a to or from clause must have a mappable type.
25799 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
25800 // A list item must have a mappable type.
25801 if (!checkTypeMappable(SL: VE->getExprLoc(), SR: VE->getSourceRange(), SemaRef,
25802 Stack: DSAS, QTy: Type, /*FullCheck=*/true))
25803 continue;
25804
25805 if (CKind == OMPC_map) {
25806 // target enter data
25807 // OpenMP [2.10.2, Restrictions, p. 99]
25808 // A map-type must be specified in all map clauses and must be either
25809 // to or alloc. Starting with OpenMP 5.2 the default map type is `to` if
25810 // no map type is present.
25811 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
25812 if (DKind == OMPD_target_enter_data &&
25813 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc ||
25814 SemaRef.getLangOpts().OpenMP >= 52)) {
25815 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_invalid_map_type_for_directive)
25816 << (IsMapTypeImplicit ? 1 : 0)
25817 << getOpenMPSimpleClauseTypeName(Kind: OMPC_map, Type: MapType)
25818 << getOpenMPDirectiveName(D: DKind, V: OMPVersion);
25819 continue;
25820 }
25821
25822 // target exit_data
25823 // OpenMP [2.10.3, Restrictions, p. 102]
25824 // A map-type must be specified in all map clauses and must be either
25825 // from, release, or delete. Starting with OpenMP 5.2 the default map
25826 // type is `from` if no map type is present.
25827 if (DKind == OMPD_target_exit_data &&
25828 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
25829 MapType == OMPC_MAP_delete || SemaRef.getLangOpts().OpenMP >= 52)) {
25830 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_invalid_map_type_for_directive)
25831 << (IsMapTypeImplicit ? 1 : 0)
25832 << getOpenMPSimpleClauseTypeName(Kind: OMPC_map, Type: MapType)
25833 << getOpenMPDirectiveName(D: DKind, V: OMPVersion);
25834 continue;
25835 }
25836
25837 // The 'ompx_hold' modifier is specifically intended to be used on a
25838 // 'target' or 'target data' directive to prevent data from being unmapped
25839 // during the associated statement. It is not permitted on a 'target
25840 // enter data' or 'target exit data' directive, which have no associated
25841 // statement.
25842 if ((DKind == OMPD_target_enter_data || DKind == OMPD_target_exit_data) &&
25843 HasHoldModifier) {
25844 SemaRef.Diag(Loc: StartLoc,
25845 DiagID: diag::err_omp_invalid_map_type_modifier_for_directive)
25846 << getOpenMPSimpleClauseTypeName(Kind: OMPC_map,
25847 Type: OMPC_MAP_MODIFIER_ompx_hold)
25848 << getOpenMPDirectiveName(D: DKind, V: OMPVersion);
25849 continue;
25850 }
25851
25852 // target, target data
25853 // OpenMP 5.0 [2.12.2, Restrictions, p. 163]
25854 // OpenMP 5.0 [2.12.5, Restrictions, p. 174]
25855 // A map-type in a map clause must be to, from, tofrom or alloc
25856 if ((DKind == OMPD_target_data ||
25857 isOpenMPTargetExecutionDirective(DKind)) &&
25858 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_from ||
25859 MapType == OMPC_MAP_tofrom || MapType == OMPC_MAP_alloc)) {
25860 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_invalid_map_type_for_directive)
25861 << (IsMapTypeImplicit ? 1 : 0)
25862 << getOpenMPSimpleClauseTypeName(Kind: OMPC_map, Type: MapType)
25863 << getOpenMPDirectiveName(D: DKind, V: OMPVersion);
25864 continue;
25865 }
25866
25867 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
25868 // A list item cannot appear in both a map clause and a data-sharing
25869 // attribute clause on the same construct
25870 //
25871 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
25872 // A list item cannot appear in both a map clause and a data-sharing
25873 // attribute clause on the same construct unless the construct is a
25874 // combined construct.
25875 if (VD && ((SemaRef.LangOpts.OpenMP <= 45 &&
25876 isOpenMPTargetExecutionDirective(DKind)) ||
25877 DKind == OMPD_target)) {
25878 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(D: VD, /*FromParent=*/false);
25879 if (isOpenMPPrivate(Kind: DVar.CKind)) {
25880 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_variable_in_given_clause_and_dsa)
25881 << getOpenMPClauseNameForDiag(C: DVar.CKind)
25882 << getOpenMPClauseNameForDiag(C: OMPC_map)
25883 << getOpenMPDirectiveName(D: DSAS->getCurrentDirective(),
25884 V: OMPVersion);
25885 reportOriginalDsa(SemaRef, Stack: DSAS, D: CurDeclaration, DVar);
25886 continue;
25887 }
25888 }
25889 }
25890
25891 // Try to find the associated user-defined mapper.
25892 ExprResult ER = buildUserDefinedMapperRef(
25893 SemaRef, S: DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
25894 Type: Type.getCanonicalType(), UnresolvedMapper, ItemLoc: ELoc);
25895 if (ER.isInvalid())
25896 continue;
25897
25898 // If no user-defined mapper is found, we need to create an implicit one for
25899 // arrays/array-sections on structs that have members that have
25900 // user-defined mappers. This is needed to ensure that the mapper for the
25901 // member is invoked when mapping each element of the array/array-section.
25902 if (!ER.get()) {
25903 QualType BaseType;
25904
25905 if (isa<ArraySectionExpr>(Val: VE)) {
25906 BaseType = VE->getType().getCanonicalType();
25907 if (BaseType->isSpecificBuiltinType(K: BuiltinType::ArraySection)) {
25908 const auto *OASE = cast<ArraySectionExpr>(Val: VE->IgnoreParenImpCasts());
25909 QualType BType =
25910 ArraySectionExpr::getBaseOriginalType(Base: OASE->getBase());
25911 QualType ElemType;
25912 if (const auto *ATy = BType->getAsArrayTypeUnsafe())
25913 ElemType = ATy->getElementType();
25914 else
25915 ElemType = BType->getPointeeType();
25916 BaseType = ElemType.getCanonicalType();
25917 }
25918 } else if (VE->getType()->isArrayType()) {
25919 const ArrayType *AT = VE->getType()->getAsArrayTypeUnsafe();
25920 const QualType ElemType = AT->getElementType();
25921 BaseType = ElemType.getCanonicalType();
25922 }
25923
25924 if (!BaseType.isNull() && BaseType->getAsRecordDecl() &&
25925 isImplicitMapperNeeded(S&: SemaRef, Stack: DSAS, CanonType: BaseType, E: VE)) {
25926 ER = buildImplicitMapper(S&: SemaRef, BaseType, Stack: DSAS);
25927 }
25928 }
25929 MVLI.UDMapperList.push_back(Elt: ER.get());
25930
25931 // Save the current expression.
25932 MVLI.ProcessedVarList.push_back(Elt: RE);
25933
25934 // Store the components in the stack so that they can be used to check
25935 // against other clauses later on.
25936 DSAS->addMappableExpressionComponents(VD: CurDeclaration, Components: CurComponents,
25937 /*WhereFoundClauseKind=*/OMPC_map);
25938
25939 // Save the components and declaration to create the clause. For purposes of
25940 // the clause creation, any component list that has base 'this' uses
25941 // null as base declaration.
25942 MVLI.VarComponents.resize(N: MVLI.VarComponents.size() + 1);
25943 MVLI.VarComponents.back().append(in_start: CurComponents.begin(),
25944 in_end: CurComponents.end());
25945 MVLI.VarBaseDeclarations.push_back(Elt: isa<MemberExpr>(Val: BE) ? nullptr
25946 : CurDeclaration);
25947 }
25948}
25949
25950OMPClause *SemaOpenMP::ActOnOpenMPMapClause(
25951 Expr *IteratorModifier, ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
25952 ArrayRef<SourceLocation> MapTypeModifiersLoc,
25953 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
25954 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc,
25955 SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
25956 const OMPVarListLocTy &Locs, bool NoDiagnose,
25957 ArrayRef<Expr *> UnresolvedMappers) {
25958 OpenMPMapModifierKind Modifiers[] = {
25959 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
25960 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
25961 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
25962 OMPC_MAP_MODIFIER_unknown};
25963 SourceLocation ModifiersLoc[NumberOfOMPMapClauseModifiers];
25964
25965 if (IteratorModifier && !IteratorModifier->getType()->isSpecificBuiltinType(
25966 K: BuiltinType::OMPIterator))
25967 Diag(Loc: IteratorModifier->getExprLoc(),
25968 DiagID: diag::err_omp_map_modifier_not_iterator);
25969
25970 // Process map-type-modifiers, flag errors for duplicate modifiers.
25971 unsigned Count = 0;
25972 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) {
25973 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown &&
25974 llvm::is_contained(Range&: Modifiers, Element: MapTypeModifiers[I])) {
25975 Diag(Loc: MapTypeModifiersLoc[I], DiagID: diag::err_omp_duplicate_map_type_modifier);
25976 continue;
25977 }
25978 assert(Count < NumberOfOMPMapClauseModifiers &&
25979 "Modifiers exceed the allowed number of map type modifiers");
25980 Modifiers[Count] = MapTypeModifiers[I];
25981 ModifiersLoc[Count] = MapTypeModifiersLoc[I];
25982 ++Count;
25983 }
25984
25985 MappableVarListInfo MVLI(VarList);
25986 // Per OpenMP 6.0 p299 lines 3-4, a list item with the const specifier and
25987 // no mutable members is ignored for 'from' clauses. A const-qualified
25988 // variable cannot be modified on the device, so copying back to the host
25989 // is unnecessary and potentially unsafe. Strip the FROM component:
25990 // map(tofrom:) -> map(to:), map(from:) -> map(alloc:).
25991 for (auto *E : VarList) {
25992 if ((MapType == OMPC_MAP_from || MapType == OMPC_MAP_tofrom) &&
25993 hasConstQualifiedMappingType(T: E->getType()))
25994 MapType = (MapType == OMPC_MAP_tofrom) ? OMPC_MAP_to : OMPC_MAP_alloc;
25995 }
25996 checkMappableExpressionList(SemaRef, DSAStack, CKind: OMPC_map, MVLI, StartLoc: Locs.StartLoc,
25997 MapperIdScopeSpec, MapperId, UnresolvedMappers,
25998 MapType, Modifiers, IsMapTypeImplicit,
25999 NoDiagnose);
26000
26001 // We need to produce a map clause even if we don't have variables so that
26002 // other diagnostics related with non-existing map clauses are accurate.
26003 return OMPMapClause::Create(
26004 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, Declarations: MVLI.VarBaseDeclarations,
26005 ComponentLists: MVLI.VarComponents, UDMapperRefs: MVLI.UDMapperList, IteratorModifier, MapModifiers: Modifiers,
26006 MapModifiersLoc: ModifiersLoc, UDMQualifierLoc: MapperIdScopeSpec.getWithLocInContext(Context&: getASTContext()),
26007 MapperId, Type: MapType, TypeIsImplicit: IsMapTypeImplicit, TypeLoc: MapLoc);
26008}
26009
26010QualType SemaOpenMP::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
26011 TypeResult ParsedType) {
26012 assert(ParsedType.isUsable());
26013
26014 QualType ReductionType = SemaRef.GetTypeFromParser(Ty: ParsedType.get());
26015 if (ReductionType.isNull())
26016 return QualType();
26017
26018 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
26019 // A type name in a declare reduction directive cannot be a function type, an
26020 // array type, a reference type, or a type qualified with const, volatile or
26021 // restrict.
26022 if (ReductionType.hasQualifiers()) {
26023 Diag(Loc: TyLoc, DiagID: diag::err_omp_reduction_wrong_type) << 0;
26024 return QualType();
26025 }
26026
26027 if (ReductionType->isFunctionType()) {
26028 Diag(Loc: TyLoc, DiagID: diag::err_omp_reduction_wrong_type) << 1;
26029 return QualType();
26030 }
26031 if (ReductionType->isReferenceType()) {
26032 Diag(Loc: TyLoc, DiagID: diag::err_omp_reduction_wrong_type) << 2;
26033 return QualType();
26034 }
26035 if (ReductionType->isArrayType()) {
26036 Diag(Loc: TyLoc, DiagID: diag::err_omp_reduction_wrong_type) << 3;
26037 return QualType();
26038 }
26039 return ReductionType;
26040}
26041
26042SemaOpenMP::DeclGroupPtrTy
26043SemaOpenMP::ActOnOpenMPDeclareReductionDirectiveStart(
26044 Scope *S, DeclContext *DC, DeclarationName Name,
26045 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
26046 AccessSpecifier AS, Decl *PrevDeclInScope) {
26047 SmallVector<Decl *, 8> Decls;
26048 Decls.reserve(N: ReductionTypes.size());
26049
26050 LookupResult Lookup(SemaRef, Name, SourceLocation(),
26051 Sema::LookupOMPReductionName,
26052 SemaRef.forRedeclarationInCurContext());
26053 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
26054 // A reduction-identifier may not be re-declared in the current scope for the
26055 // same type or for a type that is compatible according to the base language
26056 // rules.
26057 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
26058 OMPDeclareReductionDecl *PrevDRD = nullptr;
26059 bool InCompoundScope = true;
26060 if (S != nullptr) {
26061 // Find previous declaration with the same name not referenced in other
26062 // declarations.
26063 FunctionScopeInfo *ParentFn = SemaRef.getEnclosingFunction();
26064 InCompoundScope =
26065 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
26066 SemaRef.LookupName(R&: Lookup, S);
26067 SemaRef.FilterLookupForScope(R&: Lookup, Ctx: DC, S, /*ConsiderLinkage=*/false,
26068 /*AllowInlineNamespace=*/false);
26069 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
26070 LookupResult::Filter Filter = Lookup.makeFilter();
26071 while (Filter.hasNext()) {
26072 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Val: Filter.next());
26073 if (InCompoundScope) {
26074 UsedAsPrevious.try_emplace(Key: PrevDecl, Args: false);
26075 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
26076 UsedAsPrevious[D] = true;
26077 }
26078 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
26079 PrevDecl->getLocation();
26080 }
26081 Filter.done();
26082 if (InCompoundScope) {
26083 for (const auto &PrevData : UsedAsPrevious) {
26084 if (!PrevData.second) {
26085 PrevDRD = PrevData.first;
26086 break;
26087 }
26088 }
26089 }
26090 } else if (PrevDeclInScope != nullptr) {
26091 auto *PrevDRDInScope = PrevDRD =
26092 cast<OMPDeclareReductionDecl>(Val: PrevDeclInScope);
26093 do {
26094 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
26095 PrevDRDInScope->getLocation();
26096 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
26097 } while (PrevDRDInScope != nullptr);
26098 }
26099 for (const auto &TyData : ReductionTypes) {
26100 const auto I = PreviousRedeclTypes.find(Val: TyData.first.getCanonicalType());
26101 bool Invalid = false;
26102 if (I != PreviousRedeclTypes.end()) {
26103 Diag(Loc: TyData.second, DiagID: diag::err_omp_declare_reduction_redefinition)
26104 << TyData.first;
26105 Diag(Loc: I->second, DiagID: diag::note_previous_definition);
26106 Invalid = true;
26107 }
26108 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
26109 auto *DRD = OMPDeclareReductionDecl::Create(
26110 C&: getASTContext(), DC, L: TyData.second, Name, T: TyData.first, PrevDeclInScope: PrevDRD);
26111 DC->addDecl(D: DRD);
26112 DRD->setAccess(AS);
26113 Decls.push_back(Elt: DRD);
26114 if (Invalid)
26115 DRD->setInvalidDecl();
26116 else
26117 PrevDRD = DRD;
26118 }
26119
26120 return DeclGroupPtrTy::make(
26121 P: DeclGroupRef::Create(C&: getASTContext(), Decls: Decls.begin(), NumDecls: Decls.size()));
26122}
26123
26124void SemaOpenMP::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
26125 auto *DRD = cast<OMPDeclareReductionDecl>(Val: D);
26126
26127 // Enter new function scope.
26128 SemaRef.PushFunctionScope();
26129 SemaRef.setFunctionHasBranchProtectedScope();
26130 SemaRef.getCurFunction()->setHasOMPDeclareReductionCombiner();
26131
26132 if (S != nullptr)
26133 SemaRef.PushDeclContext(S, DC: DRD);
26134 else
26135 SemaRef.CurContext = DRD;
26136
26137 SemaRef.PushExpressionEvaluationContext(
26138 NewContext: Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
26139
26140 QualType ReductionType = DRD->getType();
26141 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
26142 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
26143 // uses semantics of argument handles by value, but it should be passed by
26144 // reference. C lang does not support references, so pass all parameters as
26145 // pointers.
26146 // Create 'T omp_in;' variable.
26147 VarDecl *OmpInParm =
26148 buildVarDecl(SemaRef, Loc: D->getLocation(), Type: ReductionType, Name: "omp_in");
26149 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
26150 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
26151 // uses semantics of argument handles by value, but it should be passed by
26152 // reference. C lang does not support references, so pass all parameters as
26153 // pointers.
26154 // Create 'T omp_out;' variable.
26155 VarDecl *OmpOutParm =
26156 buildVarDecl(SemaRef, Loc: D->getLocation(), Type: ReductionType, Name: "omp_out");
26157 if (S != nullptr) {
26158 SemaRef.PushOnScopeChains(D: OmpInParm, S);
26159 SemaRef.PushOnScopeChains(D: OmpOutParm, S);
26160 } else {
26161 DRD->addDecl(D: OmpInParm);
26162 DRD->addDecl(D: OmpOutParm);
26163 }
26164 Expr *InE =
26165 ::buildDeclRefExpr(S&: SemaRef, D: OmpInParm, Ty: ReductionType, Loc: D->getLocation());
26166 Expr *OutE =
26167 ::buildDeclRefExpr(S&: SemaRef, D: OmpOutParm, Ty: ReductionType, Loc: D->getLocation());
26168 DRD->setCombinerData(InE, OutE);
26169}
26170
26171void SemaOpenMP::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D,
26172 Expr *Combiner) {
26173 auto *DRD = cast<OMPDeclareReductionDecl>(Val: D);
26174 SemaRef.DiscardCleanupsInEvaluationContext();
26175 SemaRef.PopExpressionEvaluationContext();
26176
26177 SemaRef.PopDeclContext();
26178 SemaRef.PopFunctionScopeInfo();
26179
26180 if (Combiner != nullptr)
26181 DRD->setCombiner(Combiner);
26182 else
26183 DRD->setInvalidDecl();
26184}
26185
26186VarDecl *SemaOpenMP::ActOnOpenMPDeclareReductionInitializerStart(Scope *S,
26187 Decl *D) {
26188 auto *DRD = cast<OMPDeclareReductionDecl>(Val: D);
26189
26190 // Enter new function scope.
26191 SemaRef.PushFunctionScope();
26192 SemaRef.setFunctionHasBranchProtectedScope();
26193
26194 if (S != nullptr)
26195 SemaRef.PushDeclContext(S, DC: DRD);
26196 else
26197 SemaRef.CurContext = DRD;
26198
26199 SemaRef.PushExpressionEvaluationContext(
26200 NewContext: Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
26201
26202 QualType ReductionType = DRD->getType();
26203 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
26204 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
26205 // uses semantics of argument handles by value, but it should be passed by
26206 // reference. C lang does not support references, so pass all parameters as
26207 // pointers.
26208 // Create 'T omp_priv;' variable.
26209 VarDecl *OmpPrivParm =
26210 buildVarDecl(SemaRef, Loc: D->getLocation(), Type: ReductionType, Name: "omp_priv");
26211 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
26212 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
26213 // uses semantics of argument handles by value, but it should be passed by
26214 // reference. C lang does not support references, so pass all parameters as
26215 // pointers.
26216 // Create 'T omp_orig;' variable.
26217 VarDecl *OmpOrigParm =
26218 buildVarDecl(SemaRef, Loc: D->getLocation(), Type: ReductionType, Name: "omp_orig");
26219 if (S != nullptr) {
26220 SemaRef.PushOnScopeChains(D: OmpPrivParm, S);
26221 SemaRef.PushOnScopeChains(D: OmpOrigParm, S);
26222 } else {
26223 DRD->addDecl(D: OmpPrivParm);
26224 DRD->addDecl(D: OmpOrigParm);
26225 }
26226 Expr *OrigE =
26227 ::buildDeclRefExpr(S&: SemaRef, D: OmpOrigParm, Ty: ReductionType, Loc: D->getLocation());
26228 Expr *PrivE =
26229 ::buildDeclRefExpr(S&: SemaRef, D: OmpPrivParm, Ty: ReductionType, Loc: D->getLocation());
26230 DRD->setInitializerData(OrigE, PrivE);
26231 return OmpPrivParm;
26232}
26233
26234void SemaOpenMP::ActOnOpenMPDeclareReductionInitializerEnd(
26235 Decl *D, Expr *Initializer, VarDecl *OmpPrivParm) {
26236 auto *DRD = cast<OMPDeclareReductionDecl>(Val: D);
26237
26238 // Ensure OmpPrivParm is default-constructed before the user initializer runs
26239 // (required for class types with non-trivial default constructors).
26240 if (Initializer && !DRD->getDeclContext()->isDependentContext()) {
26241 QualType ReductionType = DRD->getType();
26242 if (CXXRecordDecl *RD = ReductionType->getAsCXXRecordDecl()) {
26243 if (!RD->hasTrivialDefaultConstructor())
26244 SemaRef.ActOnUninitializedDecl(dcl: OmpPrivParm);
26245 }
26246 }
26247
26248 SemaRef.DiscardCleanupsInEvaluationContext();
26249 SemaRef.PopExpressionEvaluationContext();
26250
26251 SemaRef.PopDeclContext();
26252 SemaRef.PopFunctionScopeInfo();
26253
26254 if (Initializer != nullptr) {
26255 DRD->setInitializer(E: Initializer, IK: OMPDeclareReductionInitKind::Call);
26256 } else if (OmpPrivParm->hasInit()) {
26257 DRD->setInitializer(E: OmpPrivParm->getInit(),
26258 IK: OmpPrivParm->isDirectInit()
26259 ? OMPDeclareReductionInitKind::Direct
26260 : OMPDeclareReductionInitKind::Copy);
26261 } else {
26262 DRD->setInvalidDecl();
26263 }
26264}
26265
26266SemaOpenMP::DeclGroupPtrTy SemaOpenMP::ActOnOpenMPDeclareReductionDirectiveEnd(
26267 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
26268 for (Decl *D : DeclReductions.get()) {
26269 if (IsValid) {
26270 if (S)
26271 SemaRef.PushOnScopeChains(D: cast<OMPDeclareReductionDecl>(Val: D), S,
26272 /*AddToContext=*/false);
26273 } else {
26274 D->setInvalidDecl();
26275 }
26276 }
26277 return DeclReductions;
26278}
26279
26280TypeResult SemaOpenMP::ActOnOpenMPDeclareMapperVarDecl(Scope *S,
26281 Declarator &D) {
26282 TypeSourceInfo *TInfo = SemaRef.GetTypeForDeclarator(D);
26283 QualType T = TInfo->getType();
26284 if (D.isInvalidType())
26285 return true;
26286
26287 if (getLangOpts().CPlusPlus) {
26288 // Check that there are no default arguments (C++ only).
26289 SemaRef.CheckExtraCXXDefaultArguments(D);
26290 }
26291
26292 return SemaRef.CreateParsedType(T, TInfo);
26293}
26294
26295QualType SemaOpenMP::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc,
26296 TypeResult ParsedType) {
26297 assert(ParsedType.isUsable() && "Expect usable parsed mapper type");
26298
26299 QualType MapperType = SemaRef.GetTypeFromParser(Ty: ParsedType.get());
26300 assert(!MapperType.isNull() && "Expect valid mapper type");
26301
26302 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
26303 // The type must be of struct, union or class type in C and C++
26304 if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) {
26305 Diag(Loc: TyLoc, DiagID: diag::err_omp_mapper_wrong_type);
26306 return QualType();
26307 }
26308 return MapperType;
26309}
26310
26311SemaOpenMP::DeclGroupPtrTy SemaOpenMP::ActOnOpenMPDeclareMapperDirective(
26312 Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType,
26313 SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS,
26314 Expr *MapperVarRef, ArrayRef<OMPClause *> Clauses, Decl *PrevDeclInScope) {
26315 LookupResult Lookup(SemaRef, Name, SourceLocation(),
26316 Sema::LookupOMPMapperName,
26317 SemaRef.forRedeclarationInCurContext());
26318 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
26319 // A mapper-identifier may not be redeclared in the current scope for the
26320 // same type or for a type that is compatible according to the base language
26321 // rules.
26322 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
26323 OMPDeclareMapperDecl *PrevDMD = nullptr;
26324 bool InCompoundScope = true;
26325 if (S != nullptr) {
26326 // Find previous declaration with the same name not referenced in other
26327 // declarations.
26328 FunctionScopeInfo *ParentFn = SemaRef.getEnclosingFunction();
26329 InCompoundScope =
26330 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
26331 SemaRef.LookupName(R&: Lookup, S);
26332 SemaRef.FilterLookupForScope(R&: Lookup, Ctx: DC, S, /*ConsiderLinkage=*/false,
26333 /*AllowInlineNamespace=*/false);
26334 llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious;
26335 LookupResult::Filter Filter = Lookup.makeFilter();
26336 while (Filter.hasNext()) {
26337 auto *PrevDecl = cast<OMPDeclareMapperDecl>(Val: Filter.next());
26338 if (InCompoundScope) {
26339 UsedAsPrevious.try_emplace(Key: PrevDecl, Args: false);
26340 if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope())
26341 UsedAsPrevious[D] = true;
26342 }
26343 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
26344 PrevDecl->getLocation();
26345 }
26346 Filter.done();
26347 if (InCompoundScope) {
26348 for (const auto &PrevData : UsedAsPrevious) {
26349 if (!PrevData.second) {
26350 PrevDMD = PrevData.first;
26351 break;
26352 }
26353 }
26354 }
26355 } else if (PrevDeclInScope) {
26356 auto *PrevDMDInScope = PrevDMD =
26357 cast<OMPDeclareMapperDecl>(Val: PrevDeclInScope);
26358 do {
26359 PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] =
26360 PrevDMDInScope->getLocation();
26361 PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope();
26362 } while (PrevDMDInScope != nullptr);
26363 }
26364 const auto I = PreviousRedeclTypes.find(Val: MapperType.getCanonicalType());
26365 bool Invalid = false;
26366 if (I != PreviousRedeclTypes.end()) {
26367 Diag(Loc: StartLoc, DiagID: diag::err_omp_declare_mapper_redefinition)
26368 << MapperType << Name;
26369 Diag(Loc: I->second, DiagID: diag::note_previous_definition);
26370 Invalid = true;
26371 }
26372 // Build expressions for implicit maps of data members with 'default'
26373 // mappers.
26374 SmallVector<OMPClause *, 4> ClausesWithImplicit(Clauses);
26375 if (getLangOpts().OpenMP >= 50)
26376 processImplicitMapsWithDefaultMappers(S&: SemaRef, DSAStack,
26377 Clauses&: ClausesWithImplicit);
26378 auto *DMD = OMPDeclareMapperDecl::Create(C&: getASTContext(), DC, L: StartLoc, Name,
26379 T: MapperType, VarName: VN, Clauses: ClausesWithImplicit,
26380 PrevDeclInScope: PrevDMD);
26381 if (S)
26382 SemaRef.PushOnScopeChains(D: DMD, S);
26383 else
26384 DC->addDecl(D: DMD);
26385 DMD->setAccess(AS);
26386 if (Invalid)
26387 DMD->setInvalidDecl();
26388
26389 auto *VD = cast<DeclRefExpr>(Val: MapperVarRef)->getDecl();
26390 VD->setDeclContext(DMD);
26391 VD->setLexicalDeclContext(DMD);
26392 DMD->addDecl(D: VD);
26393 DMD->setMapperVarRef(MapperVarRef);
26394
26395 return DeclGroupPtrTy::make(P: DeclGroupRef(DMD));
26396}
26397
26398ExprResult SemaOpenMP::ActOnOpenMPDeclareMapperDirectiveVarDecl(
26399 Scope *S, QualType MapperType, SourceLocation StartLoc,
26400 DeclarationName VN) {
26401 TypeSourceInfo *TInfo =
26402 getASTContext().getTrivialTypeSourceInfo(T: MapperType, Loc: StartLoc);
26403 auto *VD = VarDecl::Create(
26404 C&: getASTContext(), DC: getASTContext().getTranslationUnitDecl(), StartLoc,
26405 IdLoc: StartLoc, Id: VN.getAsIdentifierInfo(), T: MapperType, TInfo, S: SC_None);
26406 if (S)
26407 SemaRef.PushOnScopeChains(D: VD, S, /*AddToContext=*/false);
26408 Expr *E = buildDeclRefExpr(S&: SemaRef, D: VD, Ty: MapperType, Loc: StartLoc);
26409 DSAStack->addDeclareMapperVarRef(Ref: E);
26410 return E;
26411}
26412
26413void SemaOpenMP::ActOnOpenMPIteratorVarDecl(VarDecl *VD) {
26414 bool IsGlobalVar =
26415 !VD->isLocalVarDecl() && VD->getDeclContext()->isTranslationUnit();
26416 if (DSAStack->getDeclareMapperVarRef()) {
26417 if (IsGlobalVar)
26418 SemaRef.Consumer.HandleTopLevelDecl(D: DeclGroupRef(VD));
26419 DSAStack->addIteratorVarDecl(VD);
26420 } else {
26421 // Currently, only declare mapper handles global-scope iterator vars.
26422 assert(!IsGlobalVar && "Only declare mapper handles TU-scope iterators.");
26423 }
26424}
26425
26426bool SemaOpenMP::isOpenMPDeclareMapperVarDeclAllowed(const VarDecl *VD) const {
26427 assert(getLangOpts().OpenMP && "Expected OpenMP mode.");
26428 const Expr *Ref = DSAStack->getDeclareMapperVarRef();
26429 if (const auto *DRE = cast_or_null<DeclRefExpr>(Val: Ref)) {
26430 if (VD->getCanonicalDecl() == DRE->getDecl()->getCanonicalDecl())
26431 return true;
26432 if (VD->isUsableInConstantExpressions(C: getASTContext()))
26433 return true;
26434 if (getLangOpts().OpenMP >= 52 && DSAStack->isIteratorVarDecl(VD))
26435 return true;
26436 return false;
26437 }
26438 return true;
26439}
26440
26441const ValueDecl *SemaOpenMP::getOpenMPDeclareMapperVarName() const {
26442 assert(getLangOpts().OpenMP && "Expected OpenMP mode.");
26443 return cast<DeclRefExpr>(DSAStack->getDeclareMapperVarRef())->getDecl();
26444}
26445
26446ExprResult SemaOpenMP::ActOnOpenMPDimsModifier(OpenMPClauseKind ClauseKind,
26447 int Modifier, Expr *ModifierExpr,
26448 SourceLocation ModifierLoc,
26449 ArrayRef<Expr *> VarList,
26450 SourceLocation VarListEndLoc) {
26451 assert(ModifierExpr && "Unexpected modifier expression.");
26452
26453 if (getLangOpts().OpenMP < 61) {
26454 Diag(Loc: ModifierLoc, DiagID: diag::err_omp_modifier_requires_version)
26455 << getOpenMPSimpleClauseTypeName(Kind: ClauseKind, Type: Modifier)
26456 << getOpenMPClauseName(C: ClauseKind) << "6.1";
26457 return ExprError();
26458 }
26459
26460 ExprResult DimsRes = VerifyPositiveIntegerConstantInClause(
26461 E: ModifierExpr, CKind: ClauseKind, /*StrictlyPositive=*/true,
26462 /*SuppressExprDiags=*/false);
26463 if (DimsRes.isInvalid())
26464 return ExprError();
26465
26466 ModifierExpr = DimsRes.get();
26467 if (ModifierExpr->isInstantiationDependent())
26468 return DimsRes;
26469
26470 uint64_t NumDims =
26471 ModifierExpr->EvaluateKnownConstInt(Ctx: getASTContext()).getExtValue();
26472 if (NumDims == VarList.size())
26473 return DimsRes;
26474
26475 Diag(Loc: VarListEndLoc, DiagID: diag::err_omp_unexpected_num_exprs)
26476 << getOpenMPClauseName(C: ClauseKind) << NumDims << VarList.size();
26477 return ExprError();
26478}
26479
26480OMPClause *SemaOpenMP::ActOnOpenMPNumTeamsClause(
26481 ArrayRef<Expr *> VarList, OpenMPNumTeamsClauseModifier Modifier,
26482 Expr *ModifierExpr, SourceLocation ModifierLoc,
26483 OpenMPNumTeamsClauseModifier ModifierExtra, Expr *,
26484 SourceLocation ModifierExtraLoc, SourceLocation StartLoc,
26485 SourceLocation LParenLoc, SourceLocation EndLoc) {
26486 if (VarList.empty())
26487 return nullptr;
26488
26489 for (Expr *ValExpr : VarList) {
26490 // OpenMP [teams Construct, Restrictions]
26491 // The num_teams expression must evaluate to a positive integer value.
26492 if (!isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_num_teams,
26493 /*StrictlyPositive=*/true))
26494 return nullptr;
26495 }
26496
26497 // OpenMP [teams Construct, Restrictions]
26498 // The lower-bound modifier cannot be specified if the dims modifier is
26499 // specified.
26500 if (Modifier != OMPC_NUMTEAMS_unknown &&
26501 ModifierExtra != OMPC_NUMTEAMS_unknown) {
26502 Diag(Loc: ModifierExtraLoc, DiagID: diag::err_omp_incompatible_modifiers)
26503 << getOpenMPSimpleClauseTypeName(Kind: llvm::omp::OMPC_num_teams,
26504 Type: ModifierExtra)
26505 << getOpenMPSimpleClauseTypeName(Kind: llvm::omp::OMPC_num_teams, Type: Modifier)
26506 << getOpenMPClauseName(C: llvm::omp::OMPC_num_teams);
26507 ModifierExtra = OMPC_NUMTEAMS_unknown;
26508 ModifierExtraLoc = SourceLocation();
26509 }
26510
26511 if (Modifier == OMPC_NUMTEAMS_dims) {
26512 ExprResult Res = ActOnOpenMPDimsModifier(
26513 ClauseKind: OMPC_num_teams, Modifier, ModifierExpr, ModifierLoc, VarList, VarListEndLoc: EndLoc);
26514 if (Res.isInvalid())
26515 return nullptr;
26516 ModifierExpr = Res.get();
26517 } else if (Modifier == OMPC_NUMTEAMS_lower_bound) {
26518 assert(ModifierExpr && "Unexpected modifier expression.");
26519
26520 if (getLangOpts().OpenMP < 51) {
26521 Diag(Loc: ModifierLoc, DiagID: diag::err_omp_modifier_requires_version)
26522 << getOpenMPSimpleClauseTypeName(Kind: llvm::omp::OMPC_num_teams, Type: Modifier)
26523 << getOpenMPClauseName(C: llvm::omp::OMPC_num_teams) << "5.1";
26524 return nullptr;
26525 }
26526
26527 // OpenMP [teams Construct, Restrictions]
26528 // The lower-bound expression in num_teams must evaluate to a positive
26529 // integer value.
26530 if (!isNonNegativeIntegerValue(ValExpr&: ModifierExpr, SemaRef, CKind: OMPC_num_teams,
26531 /*StrictlyPositive=*/true))
26532 return nullptr;
26533
26534 // OpenMP 5.2: Validate lower-bound is less than or equal to upper-bound.
26535 Expr *LowerBound = ModifierExpr;
26536 Expr *UpperBound = VarList[0];
26537
26538 // Check if both are compile-time constants for validation.
26539 if (!LowerBound->isValueDependent() && !UpperBound->isValueDependent() &&
26540 LowerBound->isIntegerConstantExpr(Ctx: getASTContext()) &&
26541 UpperBound->isIntegerConstantExpr(Ctx: getASTContext())) {
26542
26543 // Get the actual constant values.
26544 llvm::APSInt LowerVal =
26545 LowerBound->EvaluateKnownConstInt(Ctx: getASTContext());
26546 llvm::APSInt UpperVal =
26547 UpperBound->EvaluateKnownConstInt(Ctx: getASTContext());
26548
26549 if (LowerVal > UpperVal) {
26550 Diag(Loc: LowerBound->getExprLoc(),
26551 DiagID: diag::err_omp_num_teams_lower_bound_larger)
26552 << LowerBound->getSourceRange() << UpperBound->getSourceRange();
26553 return nullptr;
26554 }
26555 }
26556 }
26557
26558 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
26559 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
26560 DKind, CKind: OMPC_num_teams, OMPVersion: getLangOpts().getOpenMPVersion());
26561 if (CaptureRegion == OMPD_unknown || SemaRef.CurContext->isDependentContext())
26562 return OMPNumTeamsClause::Create(C: getASTContext(), CaptureRegion, StartLoc,
26563 LParenLoc, EndLoc, VL: VarList, Modifier,
26564 ModifierExpr, ModifierLoc,
26565 /*PreInit=*/nullptr);
26566
26567 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
26568 SmallVector<Expr *, 3> Vars;
26569 for (Expr *ValExpr : VarList) {
26570 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
26571 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
26572 Vars.push_back(Elt: ValExpr);
26573 }
26574
26575 if (ModifierExpr) {
26576 ModifierExpr = SemaRef.MakeFullExpr(Arg: ModifierExpr).get();
26577 ModifierExpr = tryBuildCapture(SemaRef, Capture: ModifierExpr, Captures).get();
26578 }
26579
26580 Stmt *PreInit = buildPreInits(Context&: getASTContext(), Captures);
26581 return OMPNumTeamsClause::Create(C: getASTContext(), CaptureRegion, StartLoc,
26582 LParenLoc, EndLoc, VL: Vars, Modifier,
26583 ModifierExpr, ModifierLoc, PreInit);
26584}
26585
26586OMPClause *SemaOpenMP::ActOnOpenMPThreadLimitClause(
26587 ArrayRef<Expr *> VarList, OpenMPThreadLimitClauseModifier Modifier,
26588 Expr *ModifierExpr, SourceLocation ModifierLoc, SourceLocation StartLoc,
26589 SourceLocation LParenLoc, SourceLocation EndLoc) {
26590 if (VarList.empty())
26591 return nullptr;
26592
26593 for (Expr *ValExpr : VarList) {
26594 // OpenMP [teams Constrcut, Restrictions]
26595 // The thread_limit expression must evaluate to a positive integer value.
26596 if (!isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_thread_limit,
26597 /*StrictlyPositive=*/true))
26598 return nullptr;
26599 }
26600
26601 if (Modifier == OMPC_THREADLIMIT_dims) {
26602 ExprResult Res =
26603 ActOnOpenMPDimsModifier(ClauseKind: OMPC_thread_limit, Modifier, ModifierExpr,
26604 ModifierLoc, VarList, VarListEndLoc: EndLoc);
26605 if (Res.isInvalid())
26606 return nullptr;
26607 ModifierExpr = Res.get();
26608 }
26609
26610 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
26611 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
26612 DKind, CKind: OMPC_thread_limit, OMPVersion: getLangOpts().getOpenMPVersion());
26613 if (CaptureRegion == OMPD_unknown || SemaRef.CurContext->isDependentContext())
26614 return OMPThreadLimitClause::Create(C: getASTContext(), CaptureRegion,
26615 StartLoc, LParenLoc, EndLoc, VL: VarList,
26616 Modifier, ModifierExpr, ModifierLoc,
26617 /*PreInit=*/nullptr);
26618
26619 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
26620 SmallVector<Expr *, 3> Vars;
26621 for (Expr *ValExpr : VarList) {
26622 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
26623 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
26624 Vars.push_back(Elt: ValExpr);
26625 }
26626
26627 if (ModifierExpr) {
26628 ModifierExpr = SemaRef.MakeFullExpr(Arg: ModifierExpr).get();
26629 ModifierExpr = tryBuildCapture(SemaRef, Capture: ModifierExpr, Captures).get();
26630 }
26631
26632 Stmt *PreInit = buildPreInits(Context&: getASTContext(), Captures);
26633 return OMPThreadLimitClause::Create(C: getASTContext(), CaptureRegion, StartLoc,
26634 LParenLoc, EndLoc, VL: Vars, Modifier,
26635 ModifierExpr, ModifierLoc, PreInit);
26636}
26637
26638OMPClause *SemaOpenMP::ActOnOpenMPPriorityClause(Expr *Priority,
26639 SourceLocation StartLoc,
26640 SourceLocation LParenLoc,
26641 SourceLocation EndLoc) {
26642 Expr *ValExpr = Priority;
26643 Stmt *HelperValStmt = nullptr;
26644 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
26645
26646 // OpenMP [2.9.1, task Constrcut]
26647 // The priority-value is a non-negative numerical scalar expression.
26648 if (!isNonNegativeIntegerValue(
26649 ValExpr, SemaRef, CKind: OMPC_priority,
26650 /*StrictlyPositive=*/false, /*BuildCapture=*/true,
26651 DSAStack->getCurrentDirective(), CaptureRegion: &CaptureRegion, HelperValStmt: &HelperValStmt))
26652 return nullptr;
26653
26654 return new (getASTContext()) OMPPriorityClause(
26655 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
26656}
26657
26658OMPClause *SemaOpenMP::ActOnOpenMPGrainsizeClause(
26659 OpenMPGrainsizeClauseModifier Modifier, Expr *Grainsize,
26660 SourceLocation StartLoc, SourceLocation LParenLoc,
26661 SourceLocation ModifierLoc, SourceLocation EndLoc) {
26662 assert((ModifierLoc.isInvalid() || getLangOpts().OpenMP >= 51) &&
26663 "Unexpected grainsize modifier in OpenMP < 51.");
26664
26665 if (ModifierLoc.isValid() && Modifier == OMPC_GRAINSIZE_unknown) {
26666 std::string Values = getListOfPossibleValues(K: OMPC_grainsize, /*First=*/0,
26667 Last: OMPC_GRAINSIZE_unknown);
26668 Diag(Loc: ModifierLoc, DiagID: diag::err_omp_unexpected_clause_value)
26669 << Values << getOpenMPClauseNameForDiag(C: OMPC_grainsize);
26670 return nullptr;
26671 }
26672
26673 Expr *ValExpr = Grainsize;
26674 Stmt *HelperValStmt = nullptr;
26675 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
26676
26677 // OpenMP [2.9.2, taskloop Constrcut]
26678 // The parameter of the grainsize clause must be a positive integer
26679 // expression.
26680 if (!isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_grainsize,
26681 /*StrictlyPositive=*/true,
26682 /*BuildCapture=*/true,
26683 DSAStack->getCurrentDirective(),
26684 CaptureRegion: &CaptureRegion, HelperValStmt: &HelperValStmt))
26685 return nullptr;
26686
26687 return new (getASTContext())
26688 OMPGrainsizeClause(Modifier, ValExpr, HelperValStmt, CaptureRegion,
26689 StartLoc, LParenLoc, ModifierLoc, EndLoc);
26690}
26691
26692OMPClause *SemaOpenMP::ActOnOpenMPNumTasksClause(
26693 OpenMPNumTasksClauseModifier Modifier, Expr *NumTasks,
26694 SourceLocation StartLoc, SourceLocation LParenLoc,
26695 SourceLocation ModifierLoc, SourceLocation EndLoc) {
26696 assert((ModifierLoc.isInvalid() || getLangOpts().OpenMP >= 51) &&
26697 "Unexpected num_tasks modifier in OpenMP < 51.");
26698
26699 if (ModifierLoc.isValid() && Modifier == OMPC_NUMTASKS_unknown) {
26700 std::string Values = getListOfPossibleValues(K: OMPC_num_tasks, /*First=*/0,
26701 Last: OMPC_NUMTASKS_unknown);
26702 Diag(Loc: ModifierLoc, DiagID: diag::err_omp_unexpected_clause_value)
26703 << Values << getOpenMPClauseNameForDiag(C: OMPC_num_tasks);
26704 return nullptr;
26705 }
26706
26707 Expr *ValExpr = NumTasks;
26708 Stmt *HelperValStmt = nullptr;
26709 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
26710
26711 // OpenMP [2.9.2, taskloop Constrcut]
26712 // The parameter of the num_tasks clause must be a positive integer
26713 // expression.
26714 if (!isNonNegativeIntegerValue(
26715 ValExpr, SemaRef, CKind: OMPC_num_tasks,
26716 /*StrictlyPositive=*/true, /*BuildCapture=*/true,
26717 DSAStack->getCurrentDirective(), CaptureRegion: &CaptureRegion, HelperValStmt: &HelperValStmt))
26718 return nullptr;
26719
26720 return new (getASTContext())
26721 OMPNumTasksClause(Modifier, ValExpr, HelperValStmt, CaptureRegion,
26722 StartLoc, LParenLoc, ModifierLoc, EndLoc);
26723}
26724
26725OMPClause *SemaOpenMP::ActOnOpenMPHintClause(Expr *Hint,
26726 SourceLocation StartLoc,
26727 SourceLocation LParenLoc,
26728 SourceLocation EndLoc) {
26729 // OpenMP [2.13.2, critical construct, Description]
26730 // ... where hint-expression is an integer constant expression that evaluates
26731 // to a valid lock hint.
26732 ExprResult HintExpr =
26733 VerifyPositiveIntegerConstantInClause(E: Hint, CKind: OMPC_hint, StrictlyPositive: false);
26734 if (HintExpr.isInvalid())
26735 return nullptr;
26736 return new (getASTContext())
26737 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
26738}
26739
26740/// Tries to find omp_event_handle_t type.
26741static bool findOMPEventHandleT(Sema &S, SourceLocation Loc,
26742 DSAStackTy *Stack) {
26743 QualType OMPEventHandleT = Stack->getOMPEventHandleT();
26744 if (!OMPEventHandleT.isNull())
26745 return true;
26746 IdentifierInfo *II = &S.PP.getIdentifierTable().get(Name: "omp_event_handle_t");
26747 ParsedType PT = S.getTypeName(II: *II, NameLoc: Loc, S: S.getCurScope());
26748 if (!PT.getAsOpaquePtr() || PT.get().isNull()) {
26749 S.Diag(Loc, DiagID: diag::err_omp_implied_type_not_found) << "omp_event_handle_t";
26750 return false;
26751 }
26752 Stack->setOMPEventHandleT(PT.get());
26753 return true;
26754}
26755
26756OMPClause *SemaOpenMP::ActOnOpenMPDetachClause(Expr *Evt,
26757 SourceLocation StartLoc,
26758 SourceLocation LParenLoc,
26759 SourceLocation EndLoc) {
26760 if (!Evt->isValueDependent() && !Evt->isTypeDependent() &&
26761 !Evt->isInstantiationDependent() &&
26762 !Evt->containsUnexpandedParameterPack()) {
26763 if (!findOMPEventHandleT(S&: SemaRef, Loc: Evt->getExprLoc(), DSAStack))
26764 return nullptr;
26765 // OpenMP 5.0, 2.10.1 task Construct.
26766 // event-handle is a variable of the omp_event_handle_t type.
26767 auto *Ref = dyn_cast<DeclRefExpr>(Val: Evt->IgnoreParenImpCasts());
26768 if (!Ref) {
26769 Diag(Loc: Evt->getExprLoc(), DiagID: diag::err_omp_var_expected)
26770 << "omp_event_handle_t" << 0 << Evt->getSourceRange();
26771 return nullptr;
26772 }
26773 auto *VD = dyn_cast_or_null<VarDecl>(Val: Ref->getDecl());
26774 if (!VD) {
26775 Diag(Loc: Evt->getExprLoc(), DiagID: diag::err_omp_var_expected)
26776 << "omp_event_handle_t" << 0 << Evt->getSourceRange();
26777 return nullptr;
26778 }
26779 if (!getASTContext().hasSameUnqualifiedType(DSAStack->getOMPEventHandleT(),
26780 T2: VD->getType()) ||
26781 VD->getType().isConstant(Ctx: getASTContext())) {
26782 Diag(Loc: Evt->getExprLoc(), DiagID: diag::err_omp_var_expected)
26783 << "omp_event_handle_t" << 1 << VD->getType()
26784 << Evt->getSourceRange();
26785 return nullptr;
26786 }
26787 // OpenMP 5.0, 2.10.1 task Construct
26788 // [detach clause]... The event-handle will be considered as if it was
26789 // specified on a firstprivate clause.
26790 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D: VD, /*FromParent=*/false);
26791 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
26792 DVar.RefExpr) {
26793 Diag(Loc: Evt->getExprLoc(), DiagID: diag::err_omp_wrong_dsa)
26794 << getOpenMPClauseNameForDiag(C: DVar.CKind)
26795 << getOpenMPClauseNameForDiag(C: OMPC_firstprivate);
26796 reportOriginalDsa(SemaRef, DSAStack, D: VD, DVar);
26797 return nullptr;
26798 }
26799 }
26800
26801 return new (getASTContext())
26802 OMPDetachClause(Evt, StartLoc, LParenLoc, EndLoc);
26803}
26804
26805OMPClause *SemaOpenMP::ActOnOpenMPDistScheduleClause(
26806 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
26807 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
26808 SourceLocation EndLoc) {
26809 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
26810 std::string Values;
26811 Values += "'";
26812 Values += getOpenMPSimpleClauseTypeName(Kind: OMPC_dist_schedule, Type: 0);
26813 Values += "'";
26814 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
26815 << Values << getOpenMPClauseNameForDiag(C: OMPC_dist_schedule);
26816 return nullptr;
26817 }
26818 Expr *ValExpr = ChunkSize;
26819 Stmt *HelperValStmt = nullptr;
26820 if (ChunkSize) {
26821 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
26822 !ChunkSize->isInstantiationDependent() &&
26823 !ChunkSize->containsUnexpandedParameterPack()) {
26824 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
26825 ExprResult Val =
26826 PerformOpenMPImplicitIntegerConversion(Loc: ChunkSizeLoc, Op: ChunkSize);
26827 if (Val.isInvalid())
26828 return nullptr;
26829
26830 ValExpr = Val.get();
26831
26832 // OpenMP [2.7.1, Restrictions]
26833 // chunk_size must be a loop invariant integer expression with a positive
26834 // value.
26835 if (std::optional<llvm::APSInt> Result =
26836 ValExpr->getIntegerConstantExpr(Ctx: getASTContext())) {
26837 if (Result->isSigned() && !Result->isStrictlyPositive()) {
26838 Diag(Loc: ChunkSizeLoc, DiagID: diag::err_omp_negative_expression_in_clause)
26839 << "dist_schedule" << /*strictly positive*/ 1
26840 << ChunkSize->getSourceRange();
26841 return nullptr;
26842 }
26843 } else if (getOpenMPCaptureRegionForClause(
26844 DSAStack->getCurrentDirective(), CKind: OMPC_dist_schedule,
26845 OMPVersion: getLangOpts().getOpenMPVersion()) != OMPD_unknown &&
26846 !SemaRef.CurContext->isDependentContext()) {
26847 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
26848 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
26849 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
26850 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
26851 }
26852 }
26853 }
26854
26855 return new (getASTContext())
26856 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
26857 Kind, ValExpr, HelperValStmt);
26858}
26859
26860OMPClause *SemaOpenMP::ActOnOpenMPDefaultmapClause(
26861 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
26862 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
26863 SourceLocation KindLoc, SourceLocation EndLoc) {
26864 if (getLangOpts().OpenMP < 50) {
26865 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
26866 Kind != OMPC_DEFAULTMAP_scalar) {
26867 std::string Value;
26868 SourceLocation Loc;
26869 Value += "'";
26870 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
26871 Value += getOpenMPSimpleClauseTypeName(Kind: OMPC_defaultmap,
26872 Type: OMPC_DEFAULTMAP_MODIFIER_tofrom);
26873 Loc = MLoc;
26874 } else {
26875 Value += getOpenMPSimpleClauseTypeName(Kind: OMPC_defaultmap,
26876 Type: OMPC_DEFAULTMAP_scalar);
26877 Loc = KindLoc;
26878 }
26879 Value += "'";
26880 Diag(Loc, DiagID: diag::err_omp_unexpected_clause_value)
26881 << Value << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
26882 return nullptr;
26883 }
26884 } else {
26885 bool isDefaultmapModifier = (M != OMPC_DEFAULTMAP_MODIFIER_unknown);
26886 bool isDefaultmapKind = (Kind != OMPC_DEFAULTMAP_unknown) ||
26887 (getLangOpts().OpenMP >= 50 && KindLoc.isInvalid());
26888 if (!isDefaultmapKind || !isDefaultmapModifier) {
26889 StringRef KindValue = getLangOpts().OpenMP < 52
26890 ? "'scalar', 'aggregate', 'pointer'"
26891 : "'scalar', 'aggregate', 'pointer', 'all'";
26892 if (getLangOpts().OpenMP == 50) {
26893 StringRef ModifierValue = "'alloc', 'from', 'to', 'tofrom', "
26894 "'firstprivate', 'none', 'default'";
26895 if (!isDefaultmapKind && isDefaultmapModifier) {
26896 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
26897 << KindValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
26898 } else if (isDefaultmapKind && !isDefaultmapModifier) {
26899 Diag(Loc: MLoc, DiagID: diag::err_omp_unexpected_clause_value)
26900 << ModifierValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
26901 } else {
26902 Diag(Loc: MLoc, DiagID: diag::err_omp_unexpected_clause_value)
26903 << ModifierValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
26904 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
26905 << KindValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
26906 }
26907 } else {
26908 StringRef ModifierValue =
26909 getLangOpts().OpenMP < 60
26910 ? "'alloc', 'from', 'to', 'tofrom', "
26911 "'firstprivate', 'none', 'default', 'present'"
26912 : "'storage', 'from', 'to', 'tofrom', "
26913 "'firstprivate', 'private', 'none', 'default', 'present'";
26914 if (!isDefaultmapKind && isDefaultmapModifier) {
26915 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
26916 << KindValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
26917 } else if (isDefaultmapKind && !isDefaultmapModifier) {
26918 Diag(Loc: MLoc, DiagID: diag::err_omp_unexpected_clause_value)
26919 << ModifierValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
26920 } else {
26921 Diag(Loc: MLoc, DiagID: diag::err_omp_unexpected_clause_value)
26922 << ModifierValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
26923 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
26924 << KindValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
26925 }
26926 }
26927 return nullptr;
26928 }
26929
26930 // OpenMP [5.0, 2.12.5, Restrictions, p. 174]
26931 // At most one defaultmap clause for each category can appear on the
26932 // directive.
26933 if (DSAStack->checkDefaultmapCategory(VariableCategory: Kind)) {
26934 Diag(Loc: StartLoc, DiagID: diag::err_omp_one_defaultmap_each_category);
26935 return nullptr;
26936 }
26937 }
26938 if (Kind == OMPC_DEFAULTMAP_unknown || Kind == OMPC_DEFAULTMAP_all) {
26939 // Variable category is not specified - mark all categories.
26940 DSAStack->setDefaultDMAAttr(M, Kind: OMPC_DEFAULTMAP_aggregate, Loc: StartLoc);
26941 DSAStack->setDefaultDMAAttr(M, Kind: OMPC_DEFAULTMAP_scalar, Loc: StartLoc);
26942 DSAStack->setDefaultDMAAttr(M, Kind: OMPC_DEFAULTMAP_pointer, Loc: StartLoc);
26943 } else {
26944 DSAStack->setDefaultDMAAttr(M, Kind, Loc: StartLoc);
26945 }
26946
26947 return new (getASTContext())
26948 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
26949}
26950
26951bool SemaOpenMP::ActOnStartOpenMPDeclareTargetContext(
26952 DeclareTargetContextInfo &DTCI) {
26953 DeclContext *CurLexicalContext = SemaRef.getCurLexicalContext();
26954 if (!CurLexicalContext->isFileContext() &&
26955 !CurLexicalContext->isExternCContext() &&
26956 !CurLexicalContext->isExternCXXContext() &&
26957 !isa<CXXRecordDecl>(Val: CurLexicalContext) &&
26958 !isa<ClassTemplateDecl>(Val: CurLexicalContext) &&
26959 !isa<ClassTemplatePartialSpecializationDecl>(Val: CurLexicalContext) &&
26960 !isa<ClassTemplateSpecializationDecl>(Val: CurLexicalContext)) {
26961 Diag(Loc: DTCI.Loc, DiagID: diag::err_omp_region_not_file_context);
26962 return false;
26963 }
26964
26965 // Report affected OpenMP target offloading behavior when in HIP lang-mode.
26966 if (getLangOpts().HIP)
26967 Diag(Loc: DTCI.Loc, DiagID: diag::warn_hip_omp_target_directives);
26968
26969 DeclareTargetNesting.push_back(Elt: DTCI);
26970 return true;
26971}
26972
26973const SemaOpenMP::DeclareTargetContextInfo
26974SemaOpenMP::ActOnOpenMPEndDeclareTargetDirective() {
26975 assert(!DeclareTargetNesting.empty() &&
26976 "check isInOpenMPDeclareTargetContext() first!");
26977 return DeclareTargetNesting.pop_back_val();
26978}
26979
26980void SemaOpenMP::ActOnFinishedOpenMPDeclareTargetContext(
26981 DeclareTargetContextInfo &DTCI) {
26982 for (auto &It : DTCI.ExplicitlyMapped)
26983 ActOnOpenMPDeclareTargetName(ND: It.first, Loc: It.second.Loc, MT: It.second.MT, DTCI);
26984}
26985
26986void SemaOpenMP::DiagnoseUnterminatedOpenMPDeclareTarget() {
26987 if (DeclareTargetNesting.empty())
26988 return;
26989 DeclareTargetContextInfo &DTCI = DeclareTargetNesting.back();
26990 llvm::omp::Version OMPVersion = getLangOpts().getOpenMPVersion();
26991 Diag(Loc: DTCI.Loc, DiagID: diag::warn_omp_unterminated_declare_target)
26992 << getOpenMPDirectiveName(D: DTCI.Kind, V: OMPVersion);
26993}
26994
26995NamedDecl *SemaOpenMP::lookupOpenMPDeclareTargetName(
26996 Scope *CurScope, CXXScopeSpec &ScopeSpec, const DeclarationNameInfo &Id) {
26997 LookupResult Lookup(SemaRef, Id, Sema::LookupOrdinaryName);
26998 SemaRef.LookupParsedName(R&: Lookup, S: CurScope, SS: &ScopeSpec,
26999 /*ObjectType=*/QualType(),
27000 /*AllowBuiltinCreation=*/true);
27001
27002 if (Lookup.isAmbiguous())
27003 return nullptr;
27004 Lookup.suppressDiagnostics();
27005
27006 if (!Lookup.isSingleResult()) {
27007 VarOrFuncDeclFilterCCC CCC(SemaRef);
27008 if (TypoCorrection Corrected =
27009 SemaRef.CorrectTypo(Typo: Id, LookupKind: Sema::LookupOrdinaryName, S: CurScope, SS: nullptr,
27010 CCC, Mode: CorrectTypoKind::ErrorRecovery)) {
27011 SemaRef.diagnoseTypo(Correction: Corrected,
27012 TypoDiag: SemaRef.PDiag(DiagID: diag::err_undeclared_var_use_suggest)
27013 << Id.getName());
27014 checkDeclIsAllowedInOpenMPTarget(E: nullptr, D: Corrected.getCorrectionDecl());
27015 return nullptr;
27016 }
27017
27018 Diag(Loc: Id.getLoc(), DiagID: diag::err_undeclared_var_use) << Id.getName();
27019 return nullptr;
27020 }
27021
27022 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
27023 if (!isa<VarDecl>(Val: ND) && !isa<FunctionDecl>(Val: ND) &&
27024 !isa<FunctionTemplateDecl>(Val: ND)) {
27025 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_invalid_target_decl) << Id.getName();
27026 return nullptr;
27027 }
27028 return ND;
27029}
27030
27031void SemaOpenMP::ActOnOpenMPDeclareTargetName(
27032 NamedDecl *ND, SourceLocation Loc, OMPDeclareTargetDeclAttr::MapTypeTy MT,
27033 DeclareTargetContextInfo &DTCI) {
27034 assert((isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
27035 isa<FunctionTemplateDecl>(ND)) &&
27036 "Expected variable, function or function template.");
27037
27038 if (auto *VD = dyn_cast<VarDecl>(Val: ND)) {
27039 // Only global variables can be marked as declare target.
27040 if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
27041 !VD->isStaticDataMember()) {
27042 Diag(Loc, DiagID: diag::err_omp_declare_target_has_local_vars)
27043 << VD->getNameAsString();
27044 return;
27045 }
27046 }
27047 // Diagnose marking after use as it may lead to incorrect diagnosis and
27048 // codegen.
27049 if (getLangOpts().OpenMP >= 50 &&
27050 (ND->isUsed(/*CheckUsedAttr=*/false) || ND->isReferenced()))
27051 Diag(Loc, DiagID: diag::warn_omp_declare_target_after_first_use);
27052
27053 // Report affected OpenMP target offloading behavior when in HIP lang-mode.
27054 if (getLangOpts().HIP)
27055 Diag(Loc, DiagID: diag::warn_hip_omp_target_directives);
27056
27057 // 'local' is incompatible with 'device_type(host)' because 'local'
27058 // variables exist only on the device.
27059 if (MT == OMPDeclareTargetDeclAttr::MT_Local &&
27060 DTCI.DT == OMPDeclareTargetDeclAttr::DT_Host) {
27061 Diag(Loc, DiagID: diag::err_omp_declare_target_local_host_only);
27062 return;
27063 }
27064
27065 // Explicit declare target lists have precedence.
27066 const unsigned Level = -1;
27067
27068 auto *VD = cast<ValueDecl>(Val: ND);
27069 std::optional<OMPDeclareTargetDeclAttr *> ActiveAttr =
27070 OMPDeclareTargetDeclAttr::getActiveAttr(VD);
27071 if (ActiveAttr && (*ActiveAttr)->getDevType() != DTCI.DT &&
27072 (*ActiveAttr)->getLevel() == Level) {
27073 Diag(Loc, DiagID: diag::err_omp_device_type_mismatch)
27074 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(Val: DTCI.DT)
27075 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(
27076 Val: (*ActiveAttr)->getDevType());
27077 return;
27078 }
27079 if (ActiveAttr && (*ActiveAttr)->getMapType() != MT &&
27080 (*ActiveAttr)->getLevel() == Level) {
27081 Diag(Loc, DiagID: diag::err_omp_declare_target_var_in_both_clauses)
27082 << ND
27083 << OMPDeclareTargetDeclAttr::ConvertMapTypeTyToStr(
27084 Val: (*ActiveAttr)->getMapType())
27085 << OMPDeclareTargetDeclAttr::ConvertMapTypeTyToStr(Val: MT);
27086 return;
27087 }
27088
27089 if (ActiveAttr && (*ActiveAttr)->getLevel() == Level)
27090 return;
27091
27092 Expr *IndirectE = nullptr;
27093 bool IsIndirect = false;
27094 if (DTCI.Indirect) {
27095 IndirectE = *DTCI.Indirect;
27096 if (!IndirectE)
27097 IsIndirect = true;
27098 }
27099 // FIXME: 'local' with 'device_type(nohost)' is not yet fully supported
27100 // in codegen. Treat as 'device_type(any)' for now. The variable will
27101 // exist on both host and device, but the host copy is unused.
27102 auto DT = DTCI.DT;
27103 if (MT == OMPDeclareTargetDeclAttr::MT_Local &&
27104 DT == OMPDeclareTargetDeclAttr::DT_NoHost) {
27105 Diag(Loc, DiagID: diag::warn_omp_declare_target_local_nohost);
27106 DT = OMPDeclareTargetDeclAttr::DT_Any;
27107 }
27108
27109 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
27110 Ctx&: getASTContext(), MapType: MT, DevType: DT, IndirectExpr: IndirectE, Indirect: IsIndirect, Level,
27111 Range: SourceRange(Loc, Loc));
27112 ND->addAttr(A);
27113 if (ASTMutationListener *ML = getASTContext().getASTMutationListener())
27114 ML->DeclarationMarkedOpenMPDeclareTarget(D: ND, Attr: A);
27115 checkDeclIsAllowedInOpenMPTarget(E: nullptr, D: ND, IdLoc: Loc);
27116 if (auto *VD = dyn_cast<VarDecl>(Val: ND);
27117 getLangOpts().OpenMP && VD && VD->hasAttr<OMPDeclareTargetDeclAttr>() &&
27118 VD->hasGlobalStorage())
27119 ActOnOpenMPDeclareTargetInitializer(D: ND);
27120}
27121
27122static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
27123 Sema &SemaRef, Decl *D) {
27124 if (!D || !isa<VarDecl>(Val: D))
27125 return;
27126 auto *VD = cast<VarDecl>(Val: D);
27127 std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy =
27128 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
27129 if (SemaRef.LangOpts.OpenMP >= 50 &&
27130 (SemaRef.getCurLambda(/*IgnoreNonLambdaCapturingScope=*/true) ||
27131 SemaRef.getCurBlock() || SemaRef.getCurCapturedRegion()) &&
27132 VD->hasGlobalStorage()) {
27133 if (!MapTy || (*MapTy != OMPDeclareTargetDeclAttr::MT_To &&
27134 *MapTy != OMPDeclareTargetDeclAttr::MT_Enter &&
27135 *MapTy != OMPDeclareTargetDeclAttr::MT_Local)) {
27136 // OpenMP 5.0, 2.12.7 declare target Directive, Restrictions
27137 // If a lambda declaration and definition appears between a
27138 // declare target directive and the matching end declare target
27139 // directive, all variables that are captured by the lambda
27140 // expression must also appear in a to clause.
27141 SemaRef.Diag(Loc: VD->getLocation(),
27142 DiagID: diag::err_omp_lambda_capture_in_declare_target_not_to);
27143 SemaRef.Diag(Loc: SL, DiagID: diag::note_var_explicitly_captured_here)
27144 << VD << 0 << SR;
27145 return;
27146 }
27147 }
27148 if (MapTy)
27149 return;
27150 SemaRef.Diag(Loc: VD->getLocation(), DiagID: diag::warn_omp_not_in_target_context);
27151 SemaRef.Diag(Loc: SL, DiagID: diag::note_used_here) << SR;
27152}
27153
27154static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
27155 Sema &SemaRef, DSAStackTy *Stack,
27156 ValueDecl *VD) {
27157 return OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) ||
27158 checkTypeMappable(SL, SR, SemaRef, Stack, QTy: VD->getType(),
27159 /*FullCheck=*/false);
27160}
27161
27162void SemaOpenMP::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
27163 SourceLocation IdLoc) {
27164 if (!D || D->isInvalidDecl())
27165 return;
27166 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
27167 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
27168 if (auto *VD = dyn_cast<VarDecl>(Val: D)) {
27169 // Only global variables can be marked as declare target.
27170 if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
27171 !VD->isStaticDataMember())
27172 return;
27173 // 2.10.6: threadprivate variable cannot appear in a declare target
27174 // directive.
27175 if (DSAStack->isThreadPrivate(D: VD)) {
27176 Diag(Loc: SL, DiagID: diag::err_omp_threadprivate_in_target);
27177 reportOriginalDsa(SemaRef, DSAStack, D: VD, DSAStack->getTopDSA(D: VD, FromParent: false));
27178 return;
27179 }
27180 }
27181 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(Val: D))
27182 D = FTD->getTemplatedDecl();
27183 if (auto *FD = dyn_cast<FunctionDecl>(Val: D)) {
27184 std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
27185 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD: FD);
27186 if (IdLoc.isValid() && Res &&
27187 (*Res == OMPDeclareTargetDeclAttr::MT_Link ||
27188 *Res == OMPDeclareTargetDeclAttr::MT_Local)) {
27189 Diag(Loc: IdLoc, DiagID: diag::err_omp_function_in_target_clause_list)
27190 << OMPDeclareTargetDeclAttr::ConvertMapTypeTyToStr(Val: *Res);
27191 Diag(Loc: FD->getLocation(), DiagID: diag::note_defined_here) << FD;
27192 return;
27193 }
27194 }
27195 if (auto *VD = dyn_cast<ValueDecl>(Val: D)) {
27196 // Problem if any with var declared with incomplete type will be reported
27197 // as normal, so no need to check it here.
27198 if ((E || !VD->getType()->isIncompleteType()) &&
27199 !checkValueDeclInTarget(SL, SR, SemaRef, DSAStack, VD))
27200 return;
27201 if (!E && isInOpenMPDeclareTargetContext()) {
27202 // Checking declaration inside declare target region.
27203 if (isa<VarDecl>(Val: D) || isa<FunctionDecl>(Val: D) ||
27204 isa<FunctionTemplateDecl>(Val: D)) {
27205 std::optional<OMPDeclareTargetDeclAttr *> ActiveAttr =
27206 OMPDeclareTargetDeclAttr::getActiveAttr(VD);
27207 unsigned Level = DeclareTargetNesting.size();
27208 if (ActiveAttr && (*ActiveAttr)->getLevel() >= Level)
27209 return;
27210 DeclareTargetContextInfo &DTCI = DeclareTargetNesting.back();
27211 Expr *IndirectE = nullptr;
27212 bool IsIndirect = false;
27213 if (DTCI.Indirect) {
27214 IndirectE = *DTCI.Indirect;
27215 if (!IndirectE)
27216 IsIndirect = true;
27217 }
27218 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
27219 Ctx&: getASTContext(),
27220 MapType: getLangOpts().OpenMP >= 52 ? OMPDeclareTargetDeclAttr::MT_Enter
27221 : OMPDeclareTargetDeclAttr::MT_To,
27222 DevType: DTCI.DT, IndirectExpr: IndirectE, Indirect: IsIndirect, Level,
27223 Range: SourceRange(DTCI.Loc, DTCI.Loc));
27224 D->addAttr(A);
27225 if (ASTMutationListener *ML = getASTContext().getASTMutationListener())
27226 ML->DeclarationMarkedOpenMPDeclareTarget(D, Attr: A);
27227 }
27228 return;
27229 }
27230 }
27231 if (!E)
27232 return;
27233 checkDeclInTargetContext(SL: E->getExprLoc(), SR: E->getSourceRange(), SemaRef, D);
27234}
27235
27236/// This class visits every VarDecl that the initializer references and adds
27237/// OMPDeclareTargetDeclAttr to each of them.
27238class GlobalDeclRefChecker final : public StmtVisitor<GlobalDeclRefChecker> {
27239 SmallVector<VarDecl *> DeclVector;
27240 Attr *A;
27241
27242public:
27243 /// A StmtVisitor class function that visits all DeclRefExpr and adds
27244 /// OMPDeclareTargetDeclAttr to them.
27245 void VisitDeclRefExpr(DeclRefExpr *Node) {
27246 if (auto *VD = dyn_cast<VarDecl>(Val: Node->getDecl())) {
27247 VD->addAttr(A);
27248 DeclVector.push_back(Elt: VD);
27249 }
27250 }
27251 /// A function that iterates across each of the Expr's children.
27252 void VisitExpr(Expr *Ex) {
27253 for (auto *Child : Ex->children()) {
27254 Visit(S: Child);
27255 }
27256 }
27257 /// A function that keeps a record of all the Decls that are variables, has
27258 /// OMPDeclareTargetDeclAttr, and has global storage in the DeclVector. Pop
27259 /// each Decl one at a time and use the inherited 'visit' functions to look
27260 /// for DeclRefExpr.
27261 void declareTargetInitializer(Decl *TD) {
27262 A = TD->getAttr<OMPDeclareTargetDeclAttr>();
27263 DeclVector.push_back(Elt: cast<VarDecl>(Val: TD));
27264 llvm::SmallDenseSet<Decl *> Visited;
27265 while (!DeclVector.empty()) {
27266 VarDecl *TargetVarDecl = DeclVector.pop_back_val();
27267 if (!Visited.insert(V: TargetVarDecl).second)
27268 continue;
27269
27270 if (TargetVarDecl->hasAttr<OMPDeclareTargetDeclAttr>() &&
27271 TargetVarDecl->hasInit() && TargetVarDecl->hasGlobalStorage()) {
27272 if (Expr *Ex = TargetVarDecl->getInit())
27273 Visit(S: Ex);
27274 }
27275 }
27276 }
27277};
27278
27279/// Adding OMPDeclareTargetDeclAttr to variables with static storage
27280/// duration that are referenced in the initializer expression list of
27281/// variables with static storage duration in declare target directive.
27282void SemaOpenMP::ActOnOpenMPDeclareTargetInitializer(Decl *TargetDecl) {
27283 GlobalDeclRefChecker Checker;
27284 if (isa<VarDecl>(Val: TargetDecl))
27285 Checker.declareTargetInitializer(TD: TargetDecl);
27286}
27287
27288OMPClause *SemaOpenMP::ActOnOpenMPToClause(
27289 ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
27290 ArrayRef<SourceLocation> MotionModifiersLoc, Expr *IteratorExpr,
27291 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
27292 SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
27293 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) {
27294 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown,
27295 OMPC_MOTION_MODIFIER_unknown,
27296 OMPC_MOTION_MODIFIER_unknown};
27297 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers];
27298
27299 // Process motion-modifiers, flag errors for duplicate modifiers.
27300 unsigned Count = 0;
27301 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) {
27302 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown &&
27303 llvm::is_contained(Range&: Modifiers, Element: MotionModifiers[I])) {
27304 Diag(Loc: MotionModifiersLoc[I], DiagID: diag::err_omp_duplicate_motion_modifier);
27305 continue;
27306 }
27307 assert(Count < NumberOfOMPMotionModifiers &&
27308 "Modifiers exceed the allowed number of motion modifiers");
27309 Modifiers[Count] = MotionModifiers[I];
27310 ModifiersLoc[Count] = MotionModifiersLoc[I];
27311 ++Count;
27312 }
27313
27314 MappableVarListInfo MVLI(VarList);
27315 checkMappableExpressionList(SemaRef, DSAStack, CKind: OMPC_to, MVLI, StartLoc: Locs.StartLoc,
27316 MapperIdScopeSpec, MapperId, UnresolvedMappers);
27317 if (MVLI.ProcessedVarList.empty())
27318 return nullptr;
27319 if (IteratorExpr)
27320 if (auto *DRE = dyn_cast<DeclRefExpr>(Val: IteratorExpr))
27321 if (auto *VD = dyn_cast<VarDecl>(Val: DRE->getDecl()))
27322 DSAStack->addIteratorVarDecl(VD);
27323 return OMPToClause::Create(
27324 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, Declarations: MVLI.VarBaseDeclarations,
27325 ComponentLists: MVLI.VarComponents, UDMapperRefs: MVLI.UDMapperList, IteratorModifier: IteratorExpr, MotionModifiers: Modifiers,
27326 MotionModifiersLoc: ModifiersLoc, UDMQualifierLoc: MapperIdScopeSpec.getWithLocInContext(Context&: getASTContext()),
27327 MapperId);
27328}
27329
27330OMPClause *SemaOpenMP::ActOnOpenMPFromClause(
27331 ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
27332 ArrayRef<SourceLocation> MotionModifiersLoc, Expr *IteratorExpr,
27333 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
27334 SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
27335 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) {
27336 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown,
27337 OMPC_MOTION_MODIFIER_unknown,
27338 OMPC_MOTION_MODIFIER_unknown};
27339 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers];
27340
27341 // Process motion-modifiers, flag errors for duplicate modifiers.
27342 unsigned Count = 0;
27343 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) {
27344 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown &&
27345 llvm::is_contained(Range&: Modifiers, Element: MotionModifiers[I])) {
27346 Diag(Loc: MotionModifiersLoc[I], DiagID: diag::err_omp_duplicate_motion_modifier);
27347 continue;
27348 }
27349 assert(Count < NumberOfOMPMotionModifiers &&
27350 "Modifiers exceed the allowed number of motion modifiers");
27351 Modifiers[Count] = MotionModifiers[I];
27352 ModifiersLoc[Count] = MotionModifiersLoc[I];
27353 ++Count;
27354 }
27355
27356 MappableVarListInfo MVLI(VarList);
27357 checkMappableExpressionList(SemaRef, DSAStack, CKind: OMPC_from, MVLI, StartLoc: Locs.StartLoc,
27358 MapperIdScopeSpec, MapperId, UnresolvedMappers);
27359 if (MVLI.ProcessedVarList.empty())
27360 return nullptr;
27361 if (IteratorExpr)
27362 if (auto *DRE = dyn_cast<DeclRefExpr>(Val: IteratorExpr))
27363 if (auto *VD = dyn_cast<VarDecl>(Val: DRE->getDecl()))
27364 DSAStack->addIteratorVarDecl(VD);
27365 return OMPFromClause::Create(
27366 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, Declarations: MVLI.VarBaseDeclarations,
27367 ComponentLists: MVLI.VarComponents, UDMapperRefs: MVLI.UDMapperList, IteratorExpr, MotionModifiers: Modifiers,
27368 MotionModifiersLoc: ModifiersLoc, UDMQualifierLoc: MapperIdScopeSpec.getWithLocInContext(Context&: getASTContext()),
27369 MapperId);
27370}
27371
27372OMPClause *SemaOpenMP::ActOnOpenMPUseDevicePtrClause(
27373 ArrayRef<Expr *> VarList, const OMPVarListLocTy &Locs,
27374 OpenMPUseDevicePtrFallbackModifier FallbackModifier,
27375 SourceLocation FallbackModifierLoc) {
27376 MappableVarListInfo MVLI(VarList);
27377 SmallVector<Expr *, 8> PrivateCopies;
27378 SmallVector<Expr *, 8> Inits;
27379
27380 for (Expr *RefExpr : VarList) {
27381 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
27382 SourceLocation ELoc;
27383 SourceRange ERange;
27384 Expr *SimpleRefExpr = RefExpr;
27385 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
27386 if (Res.second) {
27387 // It will be analyzed later.
27388 MVLI.ProcessedVarList.push_back(Elt: RefExpr);
27389 PrivateCopies.push_back(Elt: nullptr);
27390 Inits.push_back(Elt: nullptr);
27391 }
27392 ValueDecl *D = Res.first;
27393 if (!D)
27394 continue;
27395
27396 QualType Type = D->getType();
27397 Type = Type.getNonReferenceType().getUnqualifiedType();
27398
27399 auto *VD = dyn_cast<VarDecl>(Val: D);
27400
27401 // Item should be a pointer or reference to pointer.
27402 if (!Type->isPointerType()) {
27403 Diag(Loc: ELoc, DiagID: diag::err_omp_usedeviceptr_not_a_pointer)
27404 << 0 << RefExpr->getSourceRange();
27405 continue;
27406 }
27407
27408 // Build the private variable and the expression that refers to it.
27409 auto VDPrivate =
27410 buildVarDecl(SemaRef, Loc: ELoc, Type, Name: D->getName(),
27411 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr,
27412 OrigRef: VD ? cast<DeclRefExpr>(Val: SimpleRefExpr) : nullptr);
27413 if (VDPrivate->isInvalidDecl())
27414 continue;
27415
27416 SemaRef.CurContext->addDecl(D: VDPrivate);
27417 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
27418 S&: SemaRef, D: VDPrivate, Ty: RefExpr->getType().getUnqualifiedType(), Loc: ELoc);
27419
27420 // Add temporary variable to initialize the private copy of the pointer.
27421 VarDecl *VDInit =
27422 buildVarDecl(SemaRef, Loc: RefExpr->getExprLoc(), Type, Name: ".devptr.temp");
27423 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
27424 S&: SemaRef, D: VDInit, Ty: RefExpr->getType(), Loc: RefExpr->getExprLoc());
27425 SemaRef.AddInitializerToDecl(
27426 dcl: VDPrivate, init: SemaRef.DefaultLvalueConversion(E: VDInitRefExpr).get(),
27427 /*DirectInit=*/false);
27428
27429 // If required, build a capture to implement the privatization initialized
27430 // with the current list item value.
27431 DeclRefExpr *Ref = nullptr;
27432 if (!VD)
27433 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/true);
27434 MVLI.ProcessedVarList.push_back(Elt: VD ? RefExpr->IgnoreParens() : Ref);
27435 PrivateCopies.push_back(Elt: VDPrivateRefExpr);
27436 Inits.push_back(Elt: VDInitRefExpr);
27437
27438 // We need to add a data sharing attribute for this variable to make sure it
27439 // is correctly captured. A variable that shows up in a use_device_ptr has
27440 // similar properties of a first private variable.
27441 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_firstprivate, PrivateCopy: Ref);
27442
27443 // Create a mappable component for the list item. List items in this clause
27444 // only need a component.
27445 MVLI.VarBaseDeclarations.push_back(Elt: D);
27446 MVLI.VarComponents.resize(N: MVLI.VarComponents.size() + 1);
27447 MVLI.VarComponents.back().emplace_back(Args&: SimpleRefExpr, Args&: D,
27448 /*IsNonContiguous=*/Args: false);
27449 }
27450
27451 if (MVLI.ProcessedVarList.empty())
27452 return nullptr;
27453
27454 return OMPUseDevicePtrClause::Create(
27455 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, PrivateVars: PrivateCopies, Inits,
27456 Declarations: MVLI.VarBaseDeclarations, ComponentLists: MVLI.VarComponents, FallbackModifier,
27457 FallbackModifierLoc);
27458}
27459
27460OMPClause *
27461SemaOpenMP::ActOnOpenMPUseDeviceAddrClause(ArrayRef<Expr *> VarList,
27462 const OMPVarListLocTy &Locs) {
27463 MappableVarListInfo MVLI(VarList);
27464
27465 for (Expr *RefExpr : VarList) {
27466 assert(RefExpr && "NULL expr in OpenMP use_device_addr clause.");
27467 SourceLocation ELoc;
27468 SourceRange ERange;
27469 Expr *SimpleRefExpr = RefExpr;
27470 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange,
27471 /*AllowArraySection=*/true,
27472 /*AllowAssumedSizeArray=*/true);
27473 if (Res.second) {
27474 // It will be analyzed later.
27475 MVLI.ProcessedVarList.push_back(Elt: RefExpr);
27476 }
27477 ValueDecl *D = Res.first;
27478 if (!D)
27479 continue;
27480 auto *VD = dyn_cast<VarDecl>(Val: D);
27481
27482 // If required, build a capture to implement the privatization initialized
27483 // with the current list item value.
27484 DeclRefExpr *Ref = nullptr;
27485 if (!VD)
27486 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/true);
27487 MVLI.ProcessedVarList.push_back(Elt: VD ? RefExpr->IgnoreParens() : Ref);
27488
27489 // We need to add a data sharing attribute for this variable to make sure it
27490 // is correctly captured. A variable that shows up in a use_device_addr has
27491 // similar properties of a first private variable.
27492 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_firstprivate, PrivateCopy: Ref);
27493
27494 // Use the map-like approach to fully populate VarComponents
27495 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
27496
27497 const Expr *BE = checkMapClauseExpressionBase(
27498 SemaRef, E: RefExpr, CurComponents, CKind: OMPC_use_device_addr,
27499 DSAStack->getCurrentDirective(),
27500 /*NoDiagnose=*/false);
27501
27502 if (!BE)
27503 continue;
27504
27505 assert(!CurComponents.empty() &&
27506 "use_device_addr clause expression with no components!");
27507
27508 // OpenMP use_device_addr: If a list item is an array section, the array
27509 // base must be a base language identifier. We caught the cases where
27510 // the array-section has a base-variable in getPrivateItem. e.g.
27511 // struct S {
27512 // int a[10];
27513 // }; S s1;
27514 // ... use_device_addr(s1.a[0]) // not ok, caught already
27515 //
27516 // But we still neeed to verify that the base-pointer is also a
27517 // base-language identifier, and catch cases like:
27518 // int *pa[10]; *p;
27519 // ... use_device_addr(pa[1][2]) // not ok, base-pointer is pa[1]
27520 // ... use_device_addr(p[1]) // ok
27521 // ... use_device_addr(this->p[1]) // ok
27522 auto AttachPtrResult = OMPClauseMappableExprCommon::findAttachPtrExpr(
27523 Components: CurComponents, DSAStack->getCurrentDirective());
27524 const Expr *AttachPtrExpr = AttachPtrResult.first;
27525
27526 if (AttachPtrExpr) {
27527 const Expr *BaseExpr = AttachPtrExpr->IgnoreParenImpCasts();
27528 bool IsValidBase = false;
27529
27530 if (isa<DeclRefExpr>(Val: BaseExpr))
27531 IsValidBase = true;
27532 else if (const auto *ME = dyn_cast<MemberExpr>(Val: BaseExpr);
27533 ME && isa<CXXThisExpr>(Val: ME->getBase()->IgnoreParenImpCasts()))
27534 IsValidBase = true;
27535
27536 if (!IsValidBase) {
27537 SemaRef.Diag(Loc: ELoc,
27538 DiagID: diag::err_omp_expected_base_pointer_var_name_member_expr)
27539 << (SemaRef.getCurrentThisType().isNull() ? 0 : 1)
27540 << AttachPtrExpr->getSourceRange();
27541 continue;
27542 }
27543 }
27544
27545 // Get the declaration from the components
27546 ValueDecl *CurDeclaration = CurComponents.back().getAssociatedDeclaration();
27547 assert((isa<CXXThisExpr>(BE) || CurDeclaration) &&
27548 "Unexpected null decl for use_device_addr clause.");
27549
27550 MVLI.VarBaseDeclarations.push_back(Elt: CurDeclaration);
27551 MVLI.VarComponents.resize(N: MVLI.VarComponents.size() + 1);
27552 MVLI.VarComponents.back().append(in_start: CurComponents.begin(),
27553 in_end: CurComponents.end());
27554 }
27555
27556 if (MVLI.ProcessedVarList.empty())
27557 return nullptr;
27558
27559 return OMPUseDeviceAddrClause::Create(
27560 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, Declarations: MVLI.VarBaseDeclarations,
27561 ComponentLists: MVLI.VarComponents);
27562}
27563
27564OMPClause *
27565SemaOpenMP::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
27566 const OMPVarListLocTy &Locs) {
27567 MappableVarListInfo MVLI(VarList);
27568 for (Expr *RefExpr : VarList) {
27569 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
27570 SourceLocation ELoc;
27571 SourceRange ERange;
27572 Expr *SimpleRefExpr = RefExpr;
27573 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
27574 if (Res.second) {
27575 // It will be analyzed later.
27576 MVLI.ProcessedVarList.push_back(Elt: RefExpr);
27577 }
27578 ValueDecl *D = Res.first;
27579 if (!D)
27580 continue;
27581
27582 QualType Type = D->getType();
27583 // item should be a pointer or array or reference to pointer or array
27584 if (!Type.getNonReferenceType()->isPointerType() &&
27585 !Type.getNonReferenceType()->isArrayType()) {
27586 Diag(Loc: ELoc, DiagID: diag::err_omp_argument_type_isdeviceptr)
27587 << 0 << RefExpr->getSourceRange();
27588 continue;
27589 }
27590
27591 // Check if the declaration in the clause does not show up in any data
27592 // sharing attribute.
27593 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
27594 if (isOpenMPPrivate(Kind: DVar.CKind)) {
27595 llvm::omp::Version OMPVersion = getLangOpts().getOpenMPVersion();
27596 Diag(Loc: ELoc, DiagID: diag::err_omp_variable_in_given_clause_and_dsa)
27597 << getOpenMPClauseNameForDiag(C: DVar.CKind)
27598 << getOpenMPClauseNameForDiag(C: OMPC_is_device_ptr)
27599 << getOpenMPDirectiveName(DSAStack->getCurrentDirective(),
27600 V: OMPVersion);
27601 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
27602 continue;
27603 }
27604
27605 const Expr *ConflictExpr;
27606 if (DSAStack->checkMappableExprComponentListsForDecl(
27607 VD: D, /*CurrentRegionOnly=*/true,
27608 Check: [&ConflictExpr](
27609 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
27610 OpenMPClauseKind) -> bool {
27611 ConflictExpr = R.front().getAssociatedExpression();
27612 return true;
27613 })) {
27614 Diag(Loc: ELoc, DiagID: diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
27615 Diag(Loc: ConflictExpr->getExprLoc(), DiagID: diag::note_used_here)
27616 << ConflictExpr->getSourceRange();
27617 continue;
27618 }
27619
27620 // Store the components in the stack so that they can be used to check
27621 // against other clauses later on.
27622 OMPClauseMappableExprCommon::MappableComponent MC(
27623 SimpleRefExpr, D, /*IsNonContiguous=*/false);
27624 DSAStack->addMappableExpressionComponents(
27625 VD: D, Components: MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
27626
27627 // Record the expression we've just processed.
27628 MVLI.ProcessedVarList.push_back(Elt: SimpleRefExpr);
27629
27630 // Create a mappable component for the list item. List items in this clause
27631 // only need a component. We use a null declaration to signal fields in
27632 // 'this'.
27633 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
27634 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
27635 "Unexpected device pointer expression!");
27636 MVLI.VarBaseDeclarations.push_back(
27637 Elt: isa<DeclRefExpr>(Val: SimpleRefExpr) ? D : nullptr);
27638 MVLI.VarComponents.resize(N: MVLI.VarComponents.size() + 1);
27639 MVLI.VarComponents.back().push_back(Elt: MC);
27640 }
27641
27642 if (MVLI.ProcessedVarList.empty())
27643 return nullptr;
27644
27645 return OMPIsDevicePtrClause::Create(
27646 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, Declarations: MVLI.VarBaseDeclarations,
27647 ComponentLists: MVLI.VarComponents);
27648}
27649
27650OMPClause *
27651SemaOpenMP::ActOnOpenMPHasDeviceAddrClause(ArrayRef<Expr *> VarList,
27652 const OMPVarListLocTy &Locs) {
27653 MappableVarListInfo MVLI(VarList);
27654 for (Expr *RefExpr : VarList) {
27655 assert(RefExpr && "NULL expr in OpenMP has_device_addr clause.");
27656 SourceLocation ELoc;
27657 SourceRange ERange;
27658 Expr *SimpleRefExpr = RefExpr;
27659 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange,
27660 /*AllowArraySection=*/true);
27661 if (Res.second) {
27662 // It will be analyzed later.
27663 MVLI.ProcessedVarList.push_back(Elt: RefExpr);
27664 }
27665 ValueDecl *D = Res.first;
27666 if (!D)
27667 continue;
27668
27669 // Check if the declaration in the clause does not show up in any data
27670 // sharing attribute.
27671 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
27672 if (isOpenMPPrivate(Kind: DVar.CKind)) {
27673 llvm::omp::Version OMPVersion = getLangOpts().getOpenMPVersion();
27674 Diag(Loc: ELoc, DiagID: diag::err_omp_variable_in_given_clause_and_dsa)
27675 << getOpenMPClauseNameForDiag(C: DVar.CKind)
27676 << getOpenMPClauseNameForDiag(C: OMPC_has_device_addr)
27677 << getOpenMPDirectiveName(DSAStack->getCurrentDirective(),
27678 V: OMPVersion);
27679 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
27680 continue;
27681 }
27682
27683 const Expr *ConflictExpr;
27684 if (DSAStack->checkMappableExprComponentListsForDecl(
27685 VD: D, /*CurrentRegionOnly=*/true,
27686 Check: [&ConflictExpr](
27687 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
27688 OpenMPClauseKind) -> bool {
27689 ConflictExpr = R.front().getAssociatedExpression();
27690 return true;
27691 })) {
27692 Diag(Loc: ELoc, DiagID: diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
27693 Diag(Loc: ConflictExpr->getExprLoc(), DiagID: diag::note_used_here)
27694 << ConflictExpr->getSourceRange();
27695 continue;
27696 }
27697
27698 // Store the components in the stack so that they can be used to check
27699 // against other clauses later on.
27700 Expr *Component = SimpleRefExpr;
27701 auto *VD = dyn_cast<VarDecl>(Val: D);
27702 if (VD && (isa<ArraySectionExpr>(Val: RefExpr->IgnoreParenImpCasts()) ||
27703 isa<ArraySubscriptExpr>(Val: RefExpr->IgnoreParenImpCasts())))
27704 Component =
27705 SemaRef.DefaultFunctionArrayLvalueConversion(E: SimpleRefExpr).get();
27706 OMPClauseMappableExprCommon::MappableComponent MC(
27707 Component, D, /*IsNonContiguous=*/false);
27708 DSAStack->addMappableExpressionComponents(
27709 VD: D, Components: MC, /*WhereFoundClauseKind=*/OMPC_has_device_addr);
27710
27711 // Record the expression we've just processed.
27712 if (!VD && !SemaRef.CurContext->isDependentContext()) {
27713 DeclRefExpr *Ref =
27714 buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/true);
27715 assert(Ref && "has_device_addr capture failed");
27716 MVLI.ProcessedVarList.push_back(Elt: Ref);
27717 } else
27718 MVLI.ProcessedVarList.push_back(Elt: RefExpr->IgnoreParens());
27719
27720 // Create a mappable component for the list item. List items in this clause
27721 // only need a component. We use a null declaration to signal fields in
27722 // 'this'.
27723 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
27724 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
27725 "Unexpected device pointer expression!");
27726 MVLI.VarBaseDeclarations.push_back(
27727 Elt: isa<DeclRefExpr>(Val: SimpleRefExpr) ? D : nullptr);
27728 MVLI.VarComponents.resize(N: MVLI.VarComponents.size() + 1);
27729 MVLI.VarComponents.back().push_back(Elt: MC);
27730 }
27731
27732 if (MVLI.ProcessedVarList.empty())
27733 return nullptr;
27734
27735 return OMPHasDeviceAddrClause::Create(
27736 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, Declarations: MVLI.VarBaseDeclarations,
27737 ComponentLists: MVLI.VarComponents);
27738}
27739
27740OMPClause *SemaOpenMP::ActOnOpenMPAllocateClause(
27741 Expr *Allocator, Expr *Alignment,
27742 OpenMPAllocateClauseModifier FirstAllocateModifier,
27743 SourceLocation FirstAllocateModifierLoc,
27744 OpenMPAllocateClauseModifier SecondAllocateModifier,
27745 SourceLocation SecondAllocateModifierLoc, ArrayRef<Expr *> VarList,
27746 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
27747 SourceLocation EndLoc) {
27748 if (Allocator) {
27749 // Allocator expression is dependent - skip it for now and build the
27750 // allocator when instantiated.
27751 bool AllocDependent =
27752 (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
27753 Allocator->isInstantiationDependent() ||
27754 Allocator->containsUnexpandedParameterPack());
27755 if (!AllocDependent) {
27756 // OpenMP [2.11.4 allocate Clause, Description]
27757 // allocator is an expression of omp_allocator_handle_t type.
27758 if (!findOMPAllocatorHandleT(S&: SemaRef, Loc: Allocator->getExprLoc(), DSAStack))
27759 return nullptr;
27760
27761 ExprResult AllocatorRes = SemaRef.DefaultLvalueConversion(E: Allocator);
27762 if (AllocatorRes.isInvalid())
27763 return nullptr;
27764 AllocatorRes = SemaRef.PerformImplicitConversion(
27765 From: AllocatorRes.get(), DSAStack->getOMPAllocatorHandleT(),
27766 Action: AssignmentAction::Initializing,
27767 /*AllowExplicit=*/true);
27768 if (AllocatorRes.isInvalid())
27769 return nullptr;
27770 Allocator = AllocatorRes.isUsable() ? AllocatorRes.get() : nullptr;
27771 }
27772 } else {
27773 // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions.
27774 // allocate clauses that appear on a target construct or on constructs in a
27775 // target region must specify an allocator expression unless a requires
27776 // directive with the dynamic_allocators clause is present in the same
27777 // compilation unit.
27778 if (getLangOpts().OpenMPIsTargetDevice &&
27779 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
27780 SemaRef.targetDiag(Loc: StartLoc, DiagID: diag::err_expected_allocator_expression);
27781 }
27782 if (Alignment) {
27783 bool AlignmentDependent = Alignment->isTypeDependent() ||
27784 Alignment->isValueDependent() ||
27785 Alignment->isInstantiationDependent() ||
27786 Alignment->containsUnexpandedParameterPack();
27787 if (!AlignmentDependent) {
27788 ExprResult AlignResult =
27789 VerifyPositiveIntegerConstantInClause(E: Alignment, CKind: OMPC_allocate);
27790 Alignment = AlignResult.isUsable() ? AlignResult.get() : nullptr;
27791 }
27792 }
27793 // Analyze and build list of variables.
27794 SmallVector<Expr *, 8> Vars;
27795 for (Expr *RefExpr : VarList) {
27796 assert(RefExpr && "NULL expr in OpenMP allocate clause.");
27797 SourceLocation ELoc;
27798 SourceRange ERange;
27799 Expr *SimpleRefExpr = RefExpr;
27800 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
27801 if (Res.second) {
27802 // It will be analyzed later.
27803 Vars.push_back(Elt: RefExpr);
27804 }
27805 ValueDecl *D = Res.first;
27806 if (!D)
27807 continue;
27808
27809 auto *VD = dyn_cast<VarDecl>(Val: D);
27810 DeclRefExpr *Ref = nullptr;
27811 if (!VD && !SemaRef.CurContext->isDependentContext())
27812 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/false);
27813 Vars.push_back(Elt: (VD || SemaRef.CurContext->isDependentContext())
27814 ? RefExpr->IgnoreParens()
27815 : Ref);
27816 }
27817
27818 if (Vars.empty())
27819 return nullptr;
27820
27821 if (Allocator)
27822 DSAStack->addInnerAllocatorExpr(E: Allocator);
27823
27824 return OMPAllocateClause::Create(
27825 C: getASTContext(), StartLoc, LParenLoc, Allocator, Alignment, ColonLoc,
27826 Modifier1: FirstAllocateModifier, Modifier1Loc: FirstAllocateModifierLoc, Modifier2: SecondAllocateModifier,
27827 Modifier2Loc: SecondAllocateModifierLoc, EndLoc, VL: Vars);
27828}
27829
27830OMPClause *SemaOpenMP::ActOnOpenMPNontemporalClause(ArrayRef<Expr *> VarList,
27831 SourceLocation StartLoc,
27832 SourceLocation LParenLoc,
27833 SourceLocation EndLoc) {
27834 SmallVector<Expr *, 8> Vars;
27835 for (Expr *RefExpr : VarList) {
27836 assert(RefExpr && "NULL expr in OpenMP nontemporal clause.");
27837 SourceLocation ELoc;
27838 SourceRange ERange;
27839 Expr *SimpleRefExpr = RefExpr;
27840 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
27841 if (Res.second)
27842 // It will be analyzed later.
27843 Vars.push_back(Elt: RefExpr);
27844 ValueDecl *D = Res.first;
27845 if (!D)
27846 continue;
27847
27848 // OpenMP 5.0, 2.9.3.1 simd Construct, Restrictions.
27849 // A list-item cannot appear in more than one nontemporal clause.
27850 if (const Expr *PrevRef =
27851 DSAStack->addUniqueNontemporal(D, NewDE: SimpleRefExpr)) {
27852 Diag(Loc: ELoc, DiagID: diag::err_omp_used_in_clause_twice)
27853 << 0 << getOpenMPClauseNameForDiag(C: OMPC_nontemporal) << ERange;
27854 Diag(Loc: PrevRef->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
27855 << getOpenMPClauseNameForDiag(C: OMPC_nontemporal);
27856 continue;
27857 }
27858
27859 Vars.push_back(Elt: RefExpr);
27860 }
27861
27862 if (Vars.empty())
27863 return nullptr;
27864
27865 return OMPNontemporalClause::Create(C: getASTContext(), StartLoc, LParenLoc,
27866 EndLoc, VL: Vars);
27867}
27868
27869StmtResult SemaOpenMP::ActOnOpenMPScopeDirective(ArrayRef<OMPClause *> Clauses,
27870 Stmt *AStmt,
27871 SourceLocation StartLoc,
27872 SourceLocation EndLoc) {
27873 if (!AStmt)
27874 return StmtError();
27875
27876 SemaRef.setFunctionHasBranchProtectedScope();
27877
27878 return OMPScopeDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
27879 AssociatedStmt: AStmt);
27880}
27881
27882OMPClause *SemaOpenMP::ActOnOpenMPInclusiveClause(ArrayRef<Expr *> VarList,
27883 SourceLocation StartLoc,
27884 SourceLocation LParenLoc,
27885 SourceLocation EndLoc) {
27886 SmallVector<Expr *, 8> Vars;
27887 for (Expr *RefExpr : VarList) {
27888 assert(RefExpr && "NULL expr in OpenMP inclusive clause.");
27889 SourceLocation ELoc;
27890 SourceRange ERange;
27891 Expr *SimpleRefExpr = RefExpr;
27892 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange,
27893 /*AllowArraySection=*/true);
27894 if (Res.second)
27895 // It will be analyzed later.
27896 Vars.push_back(Elt: RefExpr);
27897 ValueDecl *D = Res.first;
27898 if (!D)
27899 continue;
27900
27901 const DSAStackTy::DSAVarData DVar =
27902 DSAStack->getTopDSA(D, /*FromParent=*/true);
27903 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions.
27904 // A list item that appears in the inclusive or exclusive clause must appear
27905 // in a reduction clause with the inscan modifier on the enclosing
27906 // worksharing-loop, worksharing-loop SIMD, or simd construct.
27907 if (DVar.CKind != OMPC_reduction || DVar.Modifier != OMPC_REDUCTION_inscan)
27908 Diag(Loc: ELoc, DiagID: diag::err_omp_inclusive_exclusive_not_reduction)
27909 << RefExpr->getSourceRange();
27910
27911 if (DSAStack->getParentDirective() != OMPD_unknown)
27912 DSAStack->markDeclAsUsedInScanDirective(D);
27913 Vars.push_back(Elt: RefExpr);
27914 }
27915
27916 if (Vars.empty())
27917 return nullptr;
27918
27919 return OMPInclusiveClause::Create(C: getASTContext(), StartLoc, LParenLoc,
27920 EndLoc, VL: Vars);
27921}
27922
27923OMPClause *SemaOpenMP::ActOnOpenMPExclusiveClause(ArrayRef<Expr *> VarList,
27924 SourceLocation StartLoc,
27925 SourceLocation LParenLoc,
27926 SourceLocation EndLoc) {
27927 SmallVector<Expr *, 8> Vars;
27928 for (Expr *RefExpr : VarList) {
27929 assert(RefExpr && "NULL expr in OpenMP exclusive clause.");
27930 SourceLocation ELoc;
27931 SourceRange ERange;
27932 Expr *SimpleRefExpr = RefExpr;
27933 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange,
27934 /*AllowArraySection=*/true);
27935 if (Res.second)
27936 // It will be analyzed later.
27937 Vars.push_back(Elt: RefExpr);
27938 ValueDecl *D = Res.first;
27939 if (!D)
27940 continue;
27941
27942 OpenMPDirectiveKind ParentDirective = DSAStack->getParentDirective();
27943 DSAStackTy::DSAVarData DVar;
27944 if (ParentDirective != OMPD_unknown)
27945 DVar = DSAStack->getTopDSA(D, /*FromParent=*/true);
27946 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions.
27947 // A list item that appears in the inclusive or exclusive clause must appear
27948 // in a reduction clause with the inscan modifier on the enclosing
27949 // worksharing-loop, worksharing-loop SIMD, or simd construct.
27950 if (ParentDirective == OMPD_unknown || DVar.CKind != OMPC_reduction ||
27951 DVar.Modifier != OMPC_REDUCTION_inscan) {
27952 Diag(Loc: ELoc, DiagID: diag::err_omp_inclusive_exclusive_not_reduction)
27953 << RefExpr->getSourceRange();
27954 } else {
27955 DSAStack->markDeclAsUsedInScanDirective(D);
27956 }
27957 Vars.push_back(Elt: RefExpr);
27958 }
27959
27960 if (Vars.empty())
27961 return nullptr;
27962
27963 return OMPExclusiveClause::Create(C: getASTContext(), StartLoc, LParenLoc,
27964 EndLoc, VL: Vars);
27965}
27966
27967/// Tries to find omp_alloctrait_t type.
27968static bool findOMPAlloctraitT(Sema &S, SourceLocation Loc, DSAStackTy *Stack) {
27969 QualType OMPAlloctraitT = Stack->getOMPAlloctraitT();
27970 if (!OMPAlloctraitT.isNull())
27971 return true;
27972 IdentifierInfo &II = S.PP.getIdentifierTable().get(Name: "omp_alloctrait_t");
27973 ParsedType PT = S.getTypeName(II, NameLoc: Loc, S: S.getCurScope());
27974 if (!PT.getAsOpaquePtr() || PT.get().isNull()) {
27975 S.Diag(Loc, DiagID: diag::err_omp_implied_type_not_found) << "omp_alloctrait_t";
27976 return false;
27977 }
27978 Stack->setOMPAlloctraitT(PT.get());
27979 return true;
27980}
27981
27982OMPClause *SemaOpenMP::ActOnOpenMPUsesAllocatorClause(
27983 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc,
27984 ArrayRef<UsesAllocatorsData> Data) {
27985 ASTContext &Context = getASTContext();
27986 // OpenMP [2.12.5, target Construct]
27987 // allocator is an identifier of omp_allocator_handle_t type.
27988 if (!findOMPAllocatorHandleT(S&: SemaRef, Loc: StartLoc, DSAStack))
27989 return nullptr;
27990 // OpenMP [2.12.5, target Construct]
27991 // allocator-traits-array is an identifier of const omp_alloctrait_t * type.
27992 if (llvm::any_of(
27993 Range&: Data,
27994 P: [](const UsesAllocatorsData &D) { return D.AllocatorTraits; }) &&
27995 !findOMPAlloctraitT(S&: SemaRef, Loc: StartLoc, DSAStack))
27996 return nullptr;
27997 llvm::SmallPtrSet<CanonicalDeclPtr<Decl>, 4> PredefinedAllocators;
27998 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
27999 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
28000 StringRef Allocator =
28001 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(Val: AllocatorKind);
28002 DeclarationName AllocatorName = &Context.Idents.get(Name: Allocator);
28003 PredefinedAllocators.insert(Ptr: SemaRef.LookupSingleName(
28004 S: SemaRef.TUScope, Name: AllocatorName, Loc: StartLoc, NameKind: Sema::LookupAnyName));
28005 }
28006
28007 SmallVector<OMPUsesAllocatorsClause::Data, 4> NewData;
28008 for (const UsesAllocatorsData &D : Data) {
28009 Expr *AllocatorExpr = nullptr;
28010 // Check allocator expression.
28011 if (D.Allocator->isTypeDependent()) {
28012 AllocatorExpr = D.Allocator;
28013 } else {
28014 // Traits were specified - need to assign new allocator to the specified
28015 // allocator, so it must be an lvalue.
28016 AllocatorExpr = D.Allocator->IgnoreParenImpCasts();
28017 auto *DRE = dyn_cast<DeclRefExpr>(Val: AllocatorExpr);
28018 bool IsPredefinedAllocator = false;
28019 if (DRE) {
28020 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorTy =
28021 getAllocatorKind(S&: SemaRef, DSAStack, Allocator: AllocatorExpr);
28022 IsPredefinedAllocator =
28023 AllocatorTy !=
28024 OMPAllocateDeclAttr::AllocatorTypeTy::OMPUserDefinedMemAlloc;
28025 }
28026 QualType OMPAllocatorHandleT = DSAStack->getOMPAllocatorHandleT();
28027 QualType AllocatorExprType = AllocatorExpr->getType();
28028 bool IsTypeCompatible = IsPredefinedAllocator;
28029 IsTypeCompatible = IsTypeCompatible ||
28030 Context.hasSameUnqualifiedType(T1: AllocatorExprType,
28031 T2: OMPAllocatorHandleT);
28032 IsTypeCompatible =
28033 IsTypeCompatible ||
28034 Context.typesAreCompatible(T1: AllocatorExprType, T2: OMPAllocatorHandleT);
28035 bool IsNonConstantLValue =
28036 !AllocatorExprType.isConstant(Ctx: Context) && AllocatorExpr->isLValue();
28037 if (!DRE || !IsTypeCompatible ||
28038 (!IsPredefinedAllocator && !IsNonConstantLValue)) {
28039 Diag(Loc: D.Allocator->getExprLoc(), DiagID: diag::err_omp_var_expected)
28040 << "omp_allocator_handle_t" << (DRE ? 1 : 0)
28041 << AllocatorExpr->getType() << D.Allocator->getSourceRange();
28042 continue;
28043 }
28044 // OpenMP [2.12.5, target Construct]
28045 // Predefined allocators appearing in a uses_allocators clause cannot have
28046 // traits specified.
28047 if (IsPredefinedAllocator && D.AllocatorTraits) {
28048 Diag(Loc: D.AllocatorTraits->getExprLoc(),
28049 DiagID: diag::err_omp_predefined_allocator_with_traits)
28050 << D.AllocatorTraits->getSourceRange();
28051 Diag(Loc: D.Allocator->getExprLoc(), DiagID: diag::note_omp_predefined_allocator)
28052 << cast<NamedDecl>(Val: DRE->getDecl())->getName()
28053 << D.Allocator->getSourceRange();
28054 continue;
28055 }
28056 // OpenMP [2.12.5, target Construct]
28057 // Non-predefined allocators appearing in a uses_allocators clause must
28058 // have traits specified.
28059 if (getLangOpts().OpenMP < 52) {
28060 if (!IsPredefinedAllocator && !D.AllocatorTraits) {
28061 Diag(Loc: D.Allocator->getExprLoc(),
28062 DiagID: diag::err_omp_nonpredefined_allocator_without_traits);
28063 continue;
28064 }
28065 }
28066 // No allocator traits - just convert it to rvalue.
28067 if (!D.AllocatorTraits)
28068 AllocatorExpr = SemaRef.DefaultLvalueConversion(E: AllocatorExpr).get();
28069 DSAStack->addUsesAllocatorsDecl(
28070 D: DRE->getDecl(),
28071 Kind: IsPredefinedAllocator
28072 ? DSAStackTy::UsesAllocatorsDeclKind::PredefinedAllocator
28073 : DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator);
28074 }
28075 Expr *AllocatorTraitsExpr = nullptr;
28076 if (D.AllocatorTraits) {
28077 if (D.AllocatorTraits->isTypeDependent()) {
28078 AllocatorTraitsExpr = D.AllocatorTraits;
28079 } else {
28080 // OpenMP [2.12.5, target Construct]
28081 // Arrays that contain allocator traits that appear in a uses_allocators
28082 // clause must be constant arrays, have constant values and be defined
28083 // in the same scope as the construct in which the clause appears.
28084 AllocatorTraitsExpr = D.AllocatorTraits->IgnoreParenImpCasts();
28085 // Check that traits expr is a constant array.
28086 QualType TraitTy;
28087 if (const ArrayType *Ty =
28088 AllocatorTraitsExpr->getType()->getAsArrayTypeUnsafe())
28089 if (const auto *ConstArrayTy = dyn_cast<ConstantArrayType>(Val: Ty))
28090 TraitTy = ConstArrayTy->getElementType();
28091 if (TraitTy.isNull() ||
28092 !(Context.hasSameUnqualifiedType(T1: TraitTy,
28093 DSAStack->getOMPAlloctraitT()) ||
28094 Context.typesAreCompatible(T1: TraitTy, DSAStack->getOMPAlloctraitT(),
28095 /*CompareUnqualified=*/true))) {
28096 Diag(Loc: D.AllocatorTraits->getExprLoc(),
28097 DiagID: diag::err_omp_expected_array_alloctraits)
28098 << AllocatorTraitsExpr->getType();
28099 continue;
28100 }
28101 // Do not map by default allocator traits if it is a standalone
28102 // variable.
28103 if (auto *DRE = dyn_cast<DeclRefExpr>(Val: AllocatorTraitsExpr))
28104 DSAStack->addUsesAllocatorsDecl(
28105 D: DRE->getDecl(),
28106 Kind: DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait);
28107 }
28108 }
28109 OMPUsesAllocatorsClause::Data &NewD = NewData.emplace_back();
28110 NewD.Allocator = AllocatorExpr;
28111 NewD.AllocatorTraits = AllocatorTraitsExpr;
28112 NewD.LParenLoc = D.LParenLoc;
28113 NewD.RParenLoc = D.RParenLoc;
28114 }
28115 return OMPUsesAllocatorsClause::Create(C: getASTContext(), StartLoc, LParenLoc,
28116 EndLoc, Data: NewData);
28117}
28118
28119OMPClause *SemaOpenMP::ActOnOpenMPAffinityClause(
28120 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
28121 SourceLocation EndLoc, Expr *Modifier, ArrayRef<Expr *> Locators) {
28122 SmallVector<Expr *, 8> Vars;
28123 for (Expr *RefExpr : Locators) {
28124 assert(RefExpr && "NULL expr in OpenMP affinity clause.");
28125 if (isa<DependentScopeDeclRefExpr>(Val: RefExpr) || RefExpr->isTypeDependent()) {
28126 // It will be analyzed later.
28127 Vars.push_back(Elt: RefExpr);
28128 continue;
28129 }
28130
28131 SourceLocation ELoc = RefExpr->getExprLoc();
28132 Expr *SimpleExpr = RefExpr->IgnoreParenImpCasts();
28133
28134 if (!SimpleExpr->isLValue()) {
28135 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_addressable_lvalue_or_array_item)
28136 << 1 << 0 << RefExpr->getSourceRange();
28137 continue;
28138 }
28139
28140 ExprResult Res;
28141 {
28142 Sema::TentativeAnalysisScope Trap(SemaRef);
28143 Res = SemaRef.CreateBuiltinUnaryOp(OpLoc: ELoc, Opc: UO_AddrOf, InputExpr: SimpleExpr);
28144 }
28145 if (!Res.isUsable() && !isa<ArraySectionExpr>(Val: SimpleExpr) &&
28146 !isa<OMPArrayShapingExpr>(Val: SimpleExpr)) {
28147 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_addressable_lvalue_or_array_item)
28148 << 1 << 0 << RefExpr->getSourceRange();
28149 continue;
28150 }
28151 Vars.push_back(Elt: SimpleExpr);
28152 }
28153
28154 return OMPAffinityClause::Create(C: getASTContext(), StartLoc, LParenLoc,
28155 ColonLoc, EndLoc, Modifier, Locators: Vars);
28156}
28157
28158OMPClause *SemaOpenMP::ActOnOpenMPBindClause(OpenMPBindClauseKind Kind,
28159 SourceLocation KindLoc,
28160 SourceLocation StartLoc,
28161 SourceLocation LParenLoc,
28162 SourceLocation EndLoc) {
28163 if (Kind == OMPC_BIND_unknown) {
28164 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
28165 << getListOfPossibleValues(K: OMPC_bind, /*First=*/0,
28166 /*Last=*/unsigned(OMPC_BIND_unknown))
28167 << getOpenMPClauseNameForDiag(C: OMPC_bind);
28168 return nullptr;
28169 }
28170
28171 return new (getASTContext())
28172 OMPBindClause(Kind, KindLoc, StartLoc, LParenLoc, EndLoc);
28173}
28174
28175OMPClause *SemaOpenMP::ActOnOpenMPXDynCGroupMemClause(Expr *Size,
28176 SourceLocation StartLoc,
28177 SourceLocation LParenLoc,
28178 SourceLocation EndLoc) {
28179 Expr *ValExpr = Size;
28180 Stmt *HelperValStmt = nullptr;
28181
28182 // OpenMP [2.5, Restrictions]
28183 // The ompx_dyn_cgroup_mem expression must evaluate to a positive integer
28184 // value.
28185 if (!isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_ompx_dyn_cgroup_mem,
28186 /*StrictlyPositive=*/false))
28187 return nullptr;
28188
28189 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
28190 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
28191 DKind, CKind: OMPC_ompx_dyn_cgroup_mem, OMPVersion: getLangOpts().getOpenMPVersion());
28192 if (CaptureRegion != OMPD_unknown &&
28193 !SemaRef.CurContext->isDependentContext()) {
28194 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
28195 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
28196 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
28197 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
28198 }
28199
28200 return new (getASTContext()) OMPXDynCGroupMemClause(
28201 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
28202}
28203
28204OMPClause *SemaOpenMP::ActOnOpenMPDynGroupprivateClause(
28205 OpenMPDynGroupprivateClauseModifier M1,
28206 OpenMPDynGroupprivateClauseFallbackModifier M2, Expr *Size,
28207 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation M1Loc,
28208 SourceLocation M2Loc, SourceLocation EndLoc) {
28209
28210 if ((M1Loc.isValid() && M1 == OMPC_DYN_GROUPPRIVATE_unknown) ||
28211 (M2Loc.isValid() && M2 == OMPC_DYN_GROUPPRIVATE_FALLBACK_unknown)) {
28212 std::string Values = getListOfPossibleValues(
28213 K: OMPC_dyn_groupprivate, /*First=*/0, Last: OMPC_DYN_GROUPPRIVATE_unknown);
28214 Diag(Loc: (M1Loc.isValid() && M1 == OMPC_DYN_GROUPPRIVATE_unknown) ? M1Loc
28215 : M2Loc,
28216 DiagID: diag::err_omp_unexpected_clause_value)
28217 << Values << getOpenMPClauseName(C: OMPC_dyn_groupprivate);
28218 return nullptr;
28219 }
28220
28221 Expr *ValExpr = Size;
28222 Stmt *HelperValStmt = nullptr;
28223
28224 // OpenMP [2.5, Restrictions]
28225 // The dyn_groupprivate expression must evaluate to a positive integer
28226 // value.
28227 if (!isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_dyn_groupprivate,
28228 /*StrictlyPositive=*/false))
28229 return nullptr;
28230
28231 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
28232 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
28233 DKind, CKind: OMPC_dyn_groupprivate, OMPVersion: getLangOpts().getOpenMPVersion());
28234 if (CaptureRegion != OMPD_unknown &&
28235 !SemaRef.CurContext->isDependentContext()) {
28236 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
28237 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
28238 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
28239 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
28240 }
28241
28242 return new (getASTContext()) OMPDynGroupprivateClause(
28243 StartLoc, LParenLoc, EndLoc, ValExpr, HelperValStmt, CaptureRegion, M1,
28244 M1Loc, M2, M2Loc);
28245}
28246
28247OMPClause *SemaOpenMP::ActOnOpenMPDoacrossClause(
28248 OpenMPDoacrossClauseModifier DepType, SourceLocation DepLoc,
28249 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, SourceLocation StartLoc,
28250 SourceLocation LParenLoc, SourceLocation EndLoc) {
28251
28252 if (DSAStack->getCurrentDirective() == OMPD_ordered_standalone &&
28253 DepType != OMPC_DOACROSS_source && DepType != OMPC_DOACROSS_sink &&
28254 DepType != OMPC_DOACROSS_sink_omp_cur_iteration &&
28255 DepType != OMPC_DOACROSS_source_omp_cur_iteration) {
28256 Diag(Loc: DepLoc, DiagID: diag::err_omp_unexpected_clause_value)
28257 << "'source' or 'sink'" << getOpenMPClauseNameForDiag(C: OMPC_doacross);
28258 return nullptr;
28259 }
28260
28261 SmallVector<Expr *, 8> Vars;
28262 DSAStackTy::OperatorOffsetTy OpsOffs;
28263 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
28264 DoacrossDataInfoTy VarOffset = ProcessOpenMPDoacrossClauseCommon(
28265 SemaRef,
28266 IsSource: DepType == OMPC_DOACROSS_source ||
28267 DepType == OMPC_DOACROSS_source_omp_cur_iteration ||
28268 DepType == OMPC_DOACROSS_sink_omp_cur_iteration,
28269 VarList, DSAStack, EndLoc);
28270 Vars = VarOffset.Vars;
28271 OpsOffs = VarOffset.OpsOffs;
28272 TotalDepCount = VarOffset.TotalDepCount;
28273 auto *C = OMPDoacrossClause::Create(C: getASTContext(), StartLoc, LParenLoc,
28274 EndLoc, DepType, DepLoc, ColonLoc, VL: Vars,
28275 NumLoops: TotalDepCount.getZExtValue());
28276 if (DSAStack->isParentOrderedRegion())
28277 DSAStack->addDoacrossDependClause(C, OpsOffs);
28278 return C;
28279}
28280
28281OMPClause *SemaOpenMP::ActOnOpenMPXAttributeClause(ArrayRef<const Attr *> Attrs,
28282 SourceLocation StartLoc,
28283 SourceLocation LParenLoc,
28284 SourceLocation EndLoc) {
28285 return new (getASTContext())
28286 OMPXAttributeClause(Attrs, StartLoc, LParenLoc, EndLoc);
28287}
28288
28289OMPClause *SemaOpenMP::ActOnOpenMPXBareClause(SourceLocation StartLoc,
28290 SourceLocation EndLoc) {
28291 return new (getASTContext()) OMPXBareClause(StartLoc, EndLoc);
28292}
28293
28294OMPClause *SemaOpenMP::ActOnOpenMPHoldsClause(Expr *E, SourceLocation StartLoc,
28295 SourceLocation LParenLoc,
28296 SourceLocation EndLoc) {
28297 if (E->HasSideEffects(Ctx: getASTContext()))
28298 Diag(Loc: E->getBeginLoc(), DiagID: diag::warn_assume_side_effects)
28299 << "holds" << E->getSourceRange();
28300 return new (getASTContext()) OMPHoldsClause(E, StartLoc, LParenLoc, EndLoc);
28301}
28302
28303OMPClause *SemaOpenMP::ActOnOpenMPDirectivePresenceClause(
28304 OpenMPClauseKind CK, llvm::ArrayRef<OpenMPDirectiveKind> DKVec,
28305 SourceLocation Loc, SourceLocation LLoc, SourceLocation RLoc) {
28306 switch (CK) {
28307 case OMPC_absent:
28308 return OMPAbsentClause::Create(C: getASTContext(), DKVec, Loc, LLoc, RLoc);
28309 case OMPC_contains:
28310 return OMPContainsClause::Create(C: getASTContext(), DKVec, Loc, LLoc, RLoc);
28311 default:
28312 llvm_unreachable("Unexpected OpenMP clause");
28313 }
28314}
28315
28316OMPClause *SemaOpenMP::ActOnOpenMPNullaryAssumptionClause(OpenMPClauseKind CK,
28317 SourceLocation Loc,
28318 SourceLocation RLoc) {
28319 switch (CK) {
28320 case OMPC_no_openmp:
28321 return new (getASTContext()) OMPNoOpenMPClause(Loc, RLoc);
28322 case OMPC_no_openmp_routines:
28323 return new (getASTContext()) OMPNoOpenMPRoutinesClause(Loc, RLoc);
28324 case OMPC_no_parallelism:
28325 return new (getASTContext()) OMPNoParallelismClause(Loc, RLoc);
28326 case OMPC_no_openmp_constructs:
28327 return new (getASTContext()) OMPNoOpenMPConstructsClause(Loc, RLoc);
28328 default:
28329 llvm_unreachable("Unexpected OpenMP clause");
28330 }
28331}
28332
28333ExprResult SemaOpenMP::ActOnOMPArraySectionExpr(
28334 Expr *Base, SourceLocation LBLoc, Expr *LowerBound,
28335 SourceLocation ColonLocFirst, SourceLocation ColonLocSecond, Expr *Length,
28336 Expr *Stride, SourceLocation RBLoc) {
28337 ASTContext &Context = getASTContext();
28338 if (Base->hasPlaceholderType() &&
28339 !Base->hasPlaceholderType(K: BuiltinType::ArraySection)) {
28340 ExprResult Result = SemaRef.CheckPlaceholderExpr(E: Base);
28341 if (Result.isInvalid())
28342 return ExprError();
28343 Base = Result.get();
28344 }
28345 if (LowerBound && LowerBound->getType()->isNonOverloadPlaceholderType()) {
28346 ExprResult Result = SemaRef.CheckPlaceholderExpr(E: LowerBound);
28347 if (Result.isInvalid())
28348 return ExprError();
28349 Result = SemaRef.DefaultLvalueConversion(E: Result.get());
28350 if (Result.isInvalid())
28351 return ExprError();
28352 LowerBound = Result.get();
28353 }
28354 if (Length && Length->getType()->isNonOverloadPlaceholderType()) {
28355 ExprResult Result = SemaRef.CheckPlaceholderExpr(E: Length);
28356 if (Result.isInvalid())
28357 return ExprError();
28358 Result = SemaRef.DefaultLvalueConversion(E: Result.get());
28359 if (Result.isInvalid())
28360 return ExprError();
28361 Length = Result.get();
28362 }
28363 if (Stride && Stride->getType()->isNonOverloadPlaceholderType()) {
28364 ExprResult Result = SemaRef.CheckPlaceholderExpr(E: Stride);
28365 if (Result.isInvalid())
28366 return ExprError();
28367 Result = SemaRef.DefaultLvalueConversion(E: Result.get());
28368 if (Result.isInvalid())
28369 return ExprError();
28370 Stride = Result.get();
28371 }
28372
28373 // Build an unanalyzed expression if either operand is type-dependent.
28374 if (Base->isTypeDependent() ||
28375 (LowerBound &&
28376 (LowerBound->isTypeDependent() || LowerBound->isValueDependent())) ||
28377 (Length && (Length->isTypeDependent() || Length->isValueDependent())) ||
28378 (Stride && (Stride->isTypeDependent() || Stride->isValueDependent()))) {
28379 return new (Context) ArraySectionExpr(
28380 Base, LowerBound, Length, Stride, Context.DependentTy, VK_LValue,
28381 OK_Ordinary, ColonLocFirst, ColonLocSecond, RBLoc);
28382 }
28383
28384 // Perform default conversions.
28385 QualType OriginalTy = ArraySectionExpr::getBaseOriginalType(Base);
28386 QualType ResultTy;
28387 if (OriginalTy->isAnyPointerType()) {
28388 ResultTy = OriginalTy->getPointeeType();
28389 } else if (OriginalTy->isArrayType()) {
28390 ResultTy = OriginalTy->getAsArrayTypeUnsafe()->getElementType();
28391 } else {
28392 return ExprError(
28393 Diag(Loc: Base->getExprLoc(), DiagID: diag::err_omp_typecheck_section_value)
28394 << Base->getSourceRange());
28395 }
28396 // C99 6.5.2.1p1
28397 if (LowerBound) {
28398 auto Res = PerformOpenMPImplicitIntegerConversion(Loc: LowerBound->getExprLoc(),
28399 Op: LowerBound);
28400 if (Res.isInvalid())
28401 return ExprError(Diag(Loc: LowerBound->getExprLoc(),
28402 DiagID: diag::err_omp_typecheck_section_not_integer)
28403 << 0 << LowerBound->getSourceRange());
28404 LowerBound = Res.get();
28405
28406 if (LowerBound->getType()->isSpecificBuiltinType(K: BuiltinType::Char_S) ||
28407 LowerBound->getType()->isSpecificBuiltinType(K: BuiltinType::Char_U))
28408 Diag(Loc: LowerBound->getExprLoc(), DiagID: diag::warn_omp_section_is_char)
28409 << 0 << LowerBound->getSourceRange();
28410 }
28411 if (Length) {
28412 auto Res =
28413 PerformOpenMPImplicitIntegerConversion(Loc: Length->getExprLoc(), Op: Length);
28414 if (Res.isInvalid())
28415 return ExprError(Diag(Loc: Length->getExprLoc(),
28416 DiagID: diag::err_omp_typecheck_section_not_integer)
28417 << 1 << Length->getSourceRange());
28418 Length = Res.get();
28419
28420 if (Length->getType()->isSpecificBuiltinType(K: BuiltinType::Char_S) ||
28421 Length->getType()->isSpecificBuiltinType(K: BuiltinType::Char_U))
28422 Diag(Loc: Length->getExprLoc(), DiagID: diag::warn_omp_section_is_char)
28423 << 1 << Length->getSourceRange();
28424 }
28425 if (Stride) {
28426 ExprResult Res =
28427 PerformOpenMPImplicitIntegerConversion(Loc: Stride->getExprLoc(), Op: Stride);
28428 if (Res.isInvalid())
28429 return ExprError(Diag(Loc: Stride->getExprLoc(),
28430 DiagID: diag::err_omp_typecheck_section_not_integer)
28431 << 1 << Stride->getSourceRange());
28432 Stride = Res.get();
28433
28434 if (Stride->getType()->isSpecificBuiltinType(K: BuiltinType::Char_S) ||
28435 Stride->getType()->isSpecificBuiltinType(K: BuiltinType::Char_U))
28436 Diag(Loc: Stride->getExprLoc(), DiagID: diag::warn_omp_section_is_char)
28437 << 1 << Stride->getSourceRange();
28438 }
28439
28440 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly,
28441 // C++ [expr.sub]p1: The type "T" shall be a completely-defined object
28442 // type. Note that functions are not objects, and that (in C99 parlance)
28443 // incomplete types are not object types.
28444 if (ResultTy->isFunctionType()) {
28445 Diag(Loc: Base->getExprLoc(), DiagID: diag::err_omp_section_function_type)
28446 << ResultTy << Base->getSourceRange();
28447 return ExprError();
28448 }
28449
28450 if (SemaRef.RequireCompleteType(Loc: Base->getExprLoc(), T: ResultTy,
28451 DiagID: diag::err_omp_section_incomplete_type, Args: Base))
28452 return ExprError();
28453
28454 if (LowerBound && !OriginalTy->isAnyPointerType()) {
28455 Expr::EvalResult Result;
28456 if (LowerBound->EvaluateAsInt(Result, Ctx: Context)) {
28457 // OpenMP 5.0, [2.1.5 Array Sections]
28458 // The array section must be a subset of the original array.
28459 llvm::APSInt LowerBoundValue = Result.Val.getInt();
28460 if (LowerBoundValue.isNegative()) {
28461 Diag(Loc: LowerBound->getExprLoc(),
28462 DiagID: diag::err_omp_section_not_subset_of_array)
28463 << LowerBound->getSourceRange();
28464 return ExprError();
28465 }
28466 }
28467 }
28468
28469 if (Length) {
28470 Expr::EvalResult Result;
28471 if (Length->EvaluateAsInt(Result, Ctx: Context)) {
28472 // OpenMP 5.0, [2.1.5 Array Sections]
28473 // The length must evaluate to non-negative integers.
28474 llvm::APSInt LengthValue = Result.Val.getInt();
28475 if (LengthValue.isNegative()) {
28476 Diag(Loc: Length->getExprLoc(), DiagID: diag::err_omp_section_length_negative)
28477 << toString(I: LengthValue, /*Radix=*/10, /*Signed=*/true)
28478 << Length->getSourceRange();
28479 return ExprError();
28480 }
28481 }
28482 } else if (SemaRef.getLangOpts().OpenMP < 60 && ColonLocFirst.isValid() &&
28483 (OriginalTy.isNull() || (!OriginalTy->isConstantArrayType() &&
28484 !OriginalTy->isVariableArrayType()))) {
28485 // OpenMP 5.0, [2.1.5 Array Sections]
28486 // When the size of the array dimension is not known, the length must be
28487 // specified explicitly.
28488 Diag(Loc: ColonLocFirst, DiagID: diag::err_omp_section_length_undefined)
28489 << (!OriginalTy.isNull() && OriginalTy->isArrayType());
28490 return ExprError();
28491 }
28492
28493 if (Stride) {
28494 Expr::EvalResult Result;
28495 if (Stride->EvaluateAsInt(Result, Ctx: Context)) {
28496 // OpenMP 5.0, [2.1.5 Array Sections]
28497 // The stride must evaluate to a positive integer.
28498 llvm::APSInt StrideValue = Result.Val.getInt();
28499 if (!StrideValue.isStrictlyPositive()) {
28500 Diag(Loc: Stride->getExprLoc(), DiagID: diag::err_omp_section_stride_non_positive)
28501 << toString(I: StrideValue, /*Radix=*/10, /*Signed=*/true)
28502 << Stride->getSourceRange();
28503 return ExprError();
28504 }
28505 }
28506 }
28507
28508 if (!Base->hasPlaceholderType(K: BuiltinType::ArraySection)) {
28509 ExprResult Result = SemaRef.DefaultFunctionArrayLvalueConversion(E: Base);
28510 if (Result.isInvalid())
28511 return ExprError();
28512 Base = Result.get();
28513 }
28514 return new (Context) ArraySectionExpr(
28515 Base, LowerBound, Length, Stride, Context.ArraySectionTy, VK_LValue,
28516 OK_Ordinary, ColonLocFirst, ColonLocSecond, RBLoc);
28517}
28518
28519ExprResult SemaOpenMP::ActOnOMPArrayShapingExpr(
28520 Expr *Base, SourceLocation LParenLoc, SourceLocation RParenLoc,
28521 ArrayRef<Expr *> Dims, ArrayRef<SourceRange> Brackets) {
28522 ASTContext &Context = getASTContext();
28523 if (Base->hasPlaceholderType()) {
28524 ExprResult Result = SemaRef.CheckPlaceholderExpr(E: Base);
28525 if (Result.isInvalid())
28526 return ExprError();
28527 Result = SemaRef.DefaultLvalueConversion(E: Result.get());
28528 if (Result.isInvalid())
28529 return ExprError();
28530 Base = Result.get();
28531 }
28532 QualType BaseTy = Base->getType();
28533 // Delay analysis of the types/expressions if instantiation/specialization is
28534 // required.
28535 if (!BaseTy->isPointerType() && Base->isTypeDependent())
28536 return OMPArrayShapingExpr::Create(Context, T: Context.DependentTy, Op: Base,
28537 L: LParenLoc, R: RParenLoc, Dims, BracketRanges: Brackets);
28538 if (!BaseTy->isPointerType() ||
28539 (!Base->isTypeDependent() &&
28540 BaseTy->getPointeeType()->isIncompleteType()))
28541 return ExprError(Diag(Loc: Base->getExprLoc(),
28542 DiagID: diag::err_omp_non_pointer_type_array_shaping_base)
28543 << Base->getSourceRange());
28544
28545 SmallVector<Expr *, 4> NewDims;
28546 bool ErrorFound = false;
28547 for (Expr *Dim : Dims) {
28548 if (Dim->hasPlaceholderType()) {
28549 ExprResult Result = SemaRef.CheckPlaceholderExpr(E: Dim);
28550 if (Result.isInvalid()) {
28551 ErrorFound = true;
28552 continue;
28553 }
28554 Result = SemaRef.DefaultLvalueConversion(E: Result.get());
28555 if (Result.isInvalid()) {
28556 ErrorFound = true;
28557 continue;
28558 }
28559 Dim = Result.get();
28560 }
28561 if (!Dim->isTypeDependent()) {
28562 ExprResult Result =
28563 PerformOpenMPImplicitIntegerConversion(Loc: Dim->getExprLoc(), Op: Dim);
28564 if (Result.isInvalid()) {
28565 ErrorFound = true;
28566 Diag(Loc: Dim->getExprLoc(), DiagID: diag::err_omp_typecheck_shaping_not_integer)
28567 << Dim->getSourceRange();
28568 continue;
28569 }
28570 Dim = Result.get();
28571 Expr::EvalResult EvResult;
28572 if (!Dim->isValueDependent() && Dim->EvaluateAsInt(Result&: EvResult, Ctx: Context)) {
28573 // OpenMP 5.0, [2.1.4 Array Shaping]
28574 // Each si is an integral type expression that must evaluate to a
28575 // positive integer.
28576 llvm::APSInt Value = EvResult.Val.getInt();
28577 if (!Value.isStrictlyPositive()) {
28578 Diag(Loc: Dim->getExprLoc(), DiagID: diag::err_omp_shaping_dimension_not_positive)
28579 << toString(I: Value, /*Radix=*/10, /*Signed=*/true)
28580 << Dim->getSourceRange();
28581 ErrorFound = true;
28582 continue;
28583 }
28584 }
28585 }
28586 NewDims.push_back(Elt: Dim);
28587 }
28588 if (ErrorFound)
28589 return ExprError();
28590 return OMPArrayShapingExpr::Create(Context, T: Context.OMPArrayShapingTy, Op: Base,
28591 L: LParenLoc, R: RParenLoc, Dims: NewDims, BracketRanges: Brackets);
28592}
28593
28594ExprResult SemaOpenMP::ActOnOMPIteratorExpr(Scope *S,
28595 SourceLocation IteratorKwLoc,
28596 SourceLocation LLoc,
28597 SourceLocation RLoc,
28598 ArrayRef<OMPIteratorData> Data) {
28599 ASTContext &Context = getASTContext();
28600 SmallVector<OMPIteratorExpr::IteratorDefinition, 4> ID;
28601 bool IsCorrect = true;
28602 for (const OMPIteratorData &D : Data) {
28603 TypeSourceInfo *TInfo = nullptr;
28604 SourceLocation StartLoc;
28605 QualType DeclTy;
28606 if (!D.Type.getAsOpaquePtr()) {
28607 // OpenMP 5.0, 2.1.6 Iterators
28608 // In an iterator-specifier, if the iterator-type is not specified then
28609 // the type of that iterator is of int type.
28610 DeclTy = Context.IntTy;
28611 StartLoc = D.DeclIdentLoc;
28612 } else {
28613 DeclTy = Sema::GetTypeFromParser(Ty: D.Type, TInfo: &TInfo);
28614 StartLoc = TInfo->getTypeLoc().getBeginLoc();
28615 }
28616
28617 bool IsDeclTyDependent = DeclTy->isDependentType() ||
28618 DeclTy->containsUnexpandedParameterPack() ||
28619 DeclTy->isInstantiationDependentType();
28620 if (!IsDeclTyDependent) {
28621 if (!DeclTy->isIntegralType(Ctx: Context) && !DeclTy->isAnyPointerType()) {
28622 // OpenMP 5.0, 2.1.6 Iterators, Restrictions, C/C++
28623 // The iterator-type must be an integral or pointer type.
28624 Diag(Loc: StartLoc, DiagID: diag::err_omp_iterator_not_integral_or_pointer)
28625 << DeclTy;
28626 IsCorrect = false;
28627 continue;
28628 }
28629 if (DeclTy.isConstant(Ctx: Context)) {
28630 // OpenMP 5.0, 2.1.6 Iterators, Restrictions, C/C++
28631 // The iterator-type must not be const qualified.
28632 Diag(Loc: StartLoc, DiagID: diag::err_omp_iterator_not_integral_or_pointer)
28633 << DeclTy;
28634 IsCorrect = false;
28635 continue;
28636 }
28637 }
28638
28639 // Iterator declaration.
28640 assert(D.DeclIdent && "Identifier expected.");
28641 // Always try to create iterator declarator to avoid extra error messages
28642 // about unknown declarations use.
28643 auto *VD =
28644 VarDecl::Create(C&: Context, DC: SemaRef.CurContext, StartLoc, IdLoc: D.DeclIdentLoc,
28645 Id: D.DeclIdent, T: DeclTy, TInfo, S: SC_None);
28646 VD->setImplicit();
28647 if (S) {
28648 // Check for conflicting previous declaration.
28649 DeclarationNameInfo NameInfo(VD->getDeclName(), D.DeclIdentLoc);
28650 LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
28651 RedeclarationKind::ForVisibleRedeclaration);
28652 Previous.suppressDiagnostics();
28653 SemaRef.LookupName(R&: Previous, S);
28654
28655 SemaRef.FilterLookupForScope(R&: Previous, Ctx: SemaRef.CurContext, S,
28656 /*ConsiderLinkage=*/false,
28657 /*AllowInlineNamespace=*/false);
28658 if (!Previous.empty()) {
28659 NamedDecl *Old = Previous.getRepresentativeDecl();
28660 Diag(Loc: D.DeclIdentLoc, DiagID: diag::err_redefinition) << VD->getDeclName();
28661 Diag(Loc: Old->getLocation(), DiagID: diag::note_previous_definition);
28662 } else {
28663 SemaRef.PushOnScopeChains(D: VD, S);
28664 }
28665 } else {
28666 SemaRef.CurContext->addDecl(D: VD);
28667 }
28668
28669 /// Act on the iterator variable declaration.
28670 ActOnOpenMPIteratorVarDecl(VD);
28671
28672 Expr *Begin = D.Range.Begin;
28673 if (!IsDeclTyDependent && Begin && !Begin->isTypeDependent()) {
28674 ExprResult BeginRes = SemaRef.PerformImplicitConversion(
28675 From: Begin, ToType: DeclTy, Action: AssignmentAction::Converting);
28676 Begin = BeginRes.get();
28677 }
28678 Expr *End = D.Range.End;
28679 if (!IsDeclTyDependent && End && !End->isTypeDependent()) {
28680 ExprResult EndRes = SemaRef.PerformImplicitConversion(
28681 From: End, ToType: DeclTy, Action: AssignmentAction::Converting);
28682 End = EndRes.get();
28683 }
28684 Expr *Step = D.Range.Step;
28685 if (!IsDeclTyDependent && Step && !Step->isTypeDependent()) {
28686 if (!Step->getType()->isIntegralType(Ctx: Context)) {
28687 Diag(Loc: Step->getExprLoc(), DiagID: diag::err_omp_iterator_step_not_integral)
28688 << Step << Step->getSourceRange();
28689 IsCorrect = false;
28690 continue;
28691 }
28692 std::optional<llvm::APSInt> Result =
28693 Step->getIntegerConstantExpr(Ctx: Context);
28694 // OpenMP 5.0, 2.1.6 Iterators, Restrictions
28695 // If the step expression of a range-specification equals zero, the
28696 // behavior is unspecified.
28697 if (Result && Result->isZero()) {
28698 Diag(Loc: Step->getExprLoc(), DiagID: diag::err_omp_iterator_step_constant_zero)
28699 << Step << Step->getSourceRange();
28700 IsCorrect = false;
28701 continue;
28702 }
28703 }
28704 if (!Begin || !End || !IsCorrect) {
28705 IsCorrect = false;
28706 continue;
28707 }
28708 OMPIteratorExpr::IteratorDefinition &IDElem = ID.emplace_back();
28709 IDElem.IteratorDecl = VD;
28710 IDElem.AssignmentLoc = D.AssignLoc;
28711 IDElem.Range.Begin = Begin;
28712 IDElem.Range.End = End;
28713 IDElem.Range.Step = Step;
28714 IDElem.ColonLoc = D.ColonLoc;
28715 IDElem.SecondColonLoc = D.SecColonLoc;
28716 }
28717 if (!IsCorrect) {
28718 // Invalidate all created iterator declarations if error is found.
28719 for (const OMPIteratorExpr::IteratorDefinition &D : ID) {
28720 if (Decl *ID = D.IteratorDecl)
28721 ID->setInvalidDecl();
28722 }
28723 return ExprError();
28724 }
28725 SmallVector<OMPIteratorHelperData, 4> Helpers;
28726 if (!SemaRef.CurContext->isDependentContext()) {
28727 // Build number of ityeration for each iteration range.
28728 // Ni = ((Stepi > 0) ? ((Endi + Stepi -1 - Begini)/Stepi) :
28729 // ((Begini-Stepi-1-Endi) / -Stepi);
28730 for (OMPIteratorExpr::IteratorDefinition &D : ID) {
28731 // (Endi - Begini)
28732 ExprResult Res = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Sub,
28733 LHSExpr: D.Range.End, RHSExpr: D.Range.Begin);
28734 if (!Res.isUsable()) {
28735 IsCorrect = false;
28736 continue;
28737 }
28738 ExprResult St, St1;
28739 if (D.Range.Step) {
28740 St = D.Range.Step;
28741 // (Endi - Begini) + Stepi
28742 Res = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Add, LHSExpr: Res.get(),
28743 RHSExpr: St.get());
28744 if (!Res.isUsable()) {
28745 IsCorrect = false;
28746 continue;
28747 }
28748 // (Endi - Begini) + Stepi - 1
28749 Res = SemaRef.CreateBuiltinBinOp(
28750 OpLoc: D.AssignmentLoc, Opc: BO_Sub, LHSExpr: Res.get(),
28751 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: D.AssignmentLoc, Val: 1).get());
28752 if (!Res.isUsable()) {
28753 IsCorrect = false;
28754 continue;
28755 }
28756 // ((Endi - Begini) + Stepi - 1) / Stepi
28757 Res = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Div, LHSExpr: Res.get(),
28758 RHSExpr: St.get());
28759 if (!Res.isUsable()) {
28760 IsCorrect = false;
28761 continue;
28762 }
28763 St1 = SemaRef.CreateBuiltinUnaryOp(OpLoc: D.AssignmentLoc, Opc: UO_Minus,
28764 InputExpr: D.Range.Step);
28765 // (Begini - Endi)
28766 ExprResult Res1 = SemaRef.CreateBuiltinBinOp(
28767 OpLoc: D.AssignmentLoc, Opc: BO_Sub, LHSExpr: D.Range.Begin, RHSExpr: D.Range.End);
28768 if (!Res1.isUsable()) {
28769 IsCorrect = false;
28770 continue;
28771 }
28772 // (Begini - Endi) - Stepi
28773 Res1 = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Add, LHSExpr: Res1.get(),
28774 RHSExpr: St1.get());
28775 if (!Res1.isUsable()) {
28776 IsCorrect = false;
28777 continue;
28778 }
28779 // (Begini - Endi) - Stepi - 1
28780 Res1 = SemaRef.CreateBuiltinBinOp(
28781 OpLoc: D.AssignmentLoc, Opc: BO_Sub, LHSExpr: Res1.get(),
28782 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: D.AssignmentLoc, Val: 1).get());
28783 if (!Res1.isUsable()) {
28784 IsCorrect = false;
28785 continue;
28786 }
28787 // ((Begini - Endi) - Stepi - 1) / (-Stepi)
28788 Res1 = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Div, LHSExpr: Res1.get(),
28789 RHSExpr: St1.get());
28790 if (!Res1.isUsable()) {
28791 IsCorrect = false;
28792 continue;
28793 }
28794 // Stepi > 0.
28795 ExprResult CmpRes = SemaRef.CreateBuiltinBinOp(
28796 OpLoc: D.AssignmentLoc, Opc: BO_GT, LHSExpr: D.Range.Step,
28797 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: D.AssignmentLoc, Val: 0).get());
28798 if (!CmpRes.isUsable()) {
28799 IsCorrect = false;
28800 continue;
28801 }
28802 Res = SemaRef.ActOnConditionalOp(QuestionLoc: D.AssignmentLoc, ColonLoc: D.AssignmentLoc,
28803 CondExpr: CmpRes.get(), LHSExpr: Res.get(), RHSExpr: Res1.get());
28804 if (!Res.isUsable()) {
28805 IsCorrect = false;
28806 continue;
28807 }
28808 }
28809 Res = SemaRef.ActOnFinishFullExpr(Expr: Res.get(), /*DiscardedValue=*/false);
28810 if (!Res.isUsable()) {
28811 IsCorrect = false;
28812 continue;
28813 }
28814
28815 // Build counter update.
28816 // Build counter.
28817 auto *CounterVD = VarDecl::Create(C&: Context, DC: SemaRef.CurContext,
28818 StartLoc: D.IteratorDecl->getBeginLoc(),
28819 IdLoc: D.IteratorDecl->getBeginLoc(), Id: nullptr,
28820 T: Res.get()->getType(), TInfo: nullptr, S: SC_None);
28821 CounterVD->setImplicit();
28822 ExprResult RefRes =
28823 SemaRef.BuildDeclRefExpr(D: CounterVD, Ty: CounterVD->getType(), VK: VK_LValue,
28824 Loc: D.IteratorDecl->getBeginLoc());
28825 // Build counter update.
28826 // I = Begini + counter * Stepi;
28827 ExprResult UpdateRes;
28828 if (D.Range.Step) {
28829 UpdateRes = SemaRef.CreateBuiltinBinOp(
28830 OpLoc: D.AssignmentLoc, Opc: BO_Mul,
28831 LHSExpr: SemaRef.DefaultLvalueConversion(E: RefRes.get()).get(), RHSExpr: St.get());
28832 } else {
28833 UpdateRes = SemaRef.DefaultLvalueConversion(E: RefRes.get());
28834 }
28835 if (!UpdateRes.isUsable()) {
28836 IsCorrect = false;
28837 continue;
28838 }
28839 UpdateRes = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Add,
28840 LHSExpr: D.Range.Begin, RHSExpr: UpdateRes.get());
28841 if (!UpdateRes.isUsable()) {
28842 IsCorrect = false;
28843 continue;
28844 }
28845 ExprResult VDRes =
28846 SemaRef.BuildDeclRefExpr(D: cast<VarDecl>(Val: D.IteratorDecl),
28847 Ty: cast<VarDecl>(Val: D.IteratorDecl)->getType(),
28848 VK: VK_LValue, Loc: D.IteratorDecl->getBeginLoc());
28849 UpdateRes = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Assign,
28850 LHSExpr: VDRes.get(), RHSExpr: UpdateRes.get());
28851 if (!UpdateRes.isUsable()) {
28852 IsCorrect = false;
28853 continue;
28854 }
28855 UpdateRes =
28856 SemaRef.ActOnFinishFullExpr(Expr: UpdateRes.get(), /*DiscardedValue=*/true);
28857 if (!UpdateRes.isUsable()) {
28858 IsCorrect = false;
28859 continue;
28860 }
28861 ExprResult CounterUpdateRes = SemaRef.CreateBuiltinUnaryOp(
28862 OpLoc: D.AssignmentLoc, Opc: UO_PreInc, InputExpr: RefRes.get());
28863 if (!CounterUpdateRes.isUsable()) {
28864 IsCorrect = false;
28865 continue;
28866 }
28867 CounterUpdateRes = SemaRef.ActOnFinishFullExpr(Expr: CounterUpdateRes.get(),
28868 /*DiscardedValue=*/true);
28869 if (!CounterUpdateRes.isUsable()) {
28870 IsCorrect = false;
28871 continue;
28872 }
28873 OMPIteratorHelperData &HD = Helpers.emplace_back();
28874 HD.CounterVD = CounterVD;
28875 HD.Upper = Res.get();
28876 HD.Update = UpdateRes.get();
28877 HD.CounterUpdate = CounterUpdateRes.get();
28878 }
28879 } else {
28880 Helpers.assign(NumElts: ID.size(), Elt: {});
28881 }
28882 if (!IsCorrect) {
28883 // Invalidate all created iterator declarations if error is found.
28884 for (const OMPIteratorExpr::IteratorDefinition &D : ID) {
28885 if (Decl *ID = D.IteratorDecl)
28886 ID->setInvalidDecl();
28887 }
28888 return ExprError();
28889 }
28890 return OMPIteratorExpr::Create(Context, T: Context.OMPIteratorTy, IteratorKwLoc,
28891 L: LLoc, R: RLoc, Data: ID, Helpers);
28892}
28893
28894/// Check if \p AssumptionStr is a known assumption and warn if not.
28895static void checkOMPAssumeAttr(Sema &S, SourceLocation Loc,
28896 StringRef AssumptionStr) {
28897 if (llvm::getKnownAssumptionStrings().count(Key: AssumptionStr))
28898 return;
28899
28900 unsigned BestEditDistance = 3;
28901 StringRef Suggestion;
28902 for (const auto &KnownAssumptionIt : llvm::getKnownAssumptionStrings()) {
28903 unsigned EditDistance =
28904 AssumptionStr.edit_distance(Other: KnownAssumptionIt.getKey());
28905 if (EditDistance < BestEditDistance) {
28906 Suggestion = KnownAssumptionIt.getKey();
28907 BestEditDistance = EditDistance;
28908 }
28909 }
28910
28911 if (!Suggestion.empty())
28912 S.Diag(Loc, DiagID: diag::warn_omp_assume_attribute_string_unknown_suggested)
28913 << AssumptionStr << Suggestion;
28914 else
28915 S.Diag(Loc, DiagID: diag::warn_omp_assume_attribute_string_unknown)
28916 << AssumptionStr;
28917}
28918
28919void SemaOpenMP::handleOMPAssumeAttr(Decl *D, const ParsedAttr &AL) {
28920 // Handle the case where the attribute has a text message.
28921 StringRef Str;
28922 SourceLocation AttrStrLoc;
28923 if (!SemaRef.checkStringLiteralArgumentAttr(Attr: AL, ArgNum: 0, Str, ArgLocation: &AttrStrLoc))
28924 return;
28925
28926 checkOMPAssumeAttr(S&: SemaRef, Loc: AttrStrLoc, AssumptionStr: Str);
28927
28928 D->addAttr(A: ::new (getASTContext()) OMPAssumeAttr(getASTContext(), AL, Str));
28929}
28930
28931SemaOpenMP::SemaOpenMP(Sema &S)
28932 : SemaBase(S), VarDataSharingAttributesStack(nullptr) {}
28933